Browse Source

Address code standards issues

fabrixxm 5 months ago
parent
commit
9317a1c054

+ 1
- 1
.gitignore View File

@@ -73,4 +73,4 @@ venv/
73 73
 .htaccess
74 74
 
75 75
 #ignore filesystem storage default path
76
-/storage
76
+/storage

+ 6
- 4
src/Model/Photo.php View File

@@ -62,7 +62,7 @@ class Photo extends BaseObject
62 62
 		}
63 63
 
64 64
 		return DBA::selectFirst("photo", $fields, $condition, $params);
65
-   	}
65
+	}
66 66
 
67 67
 	/**
68 68
 	 * @brief Get a single photo given resource id and scale
@@ -79,7 +79,9 @@ class Photo extends BaseObject
79 79
 	public static function getPhoto($resourceid, $scale = 0)
80 80
 	{
81 81
 		$r = self::selectFirst(["uid"], ["resource-id" => $resourceid]);
82
-		if ($r===false) return false;
82
+		if ($r === false) {
83
+			return false;
84
+		}
83 85
 
84 86
 		$sql_acl = Security::getPermissionsSQLByUserId($r["uid"]);
85 87
 
@@ -122,7 +124,7 @@ class Photo extends BaseObject
122 124
 		if ($photo["backend-class"] == "") {
123 125
 			// legacy data storage in "data" column
124 126
 			$i = self::selectFirst(["data"], ["id"=>$photo["id"]]);
125
-			if ($i===false) {
127
+			if ($i === false) {
126 128
 				return null;
127 129
 			}
128 130
 			$data = $i["data"];
@@ -207,7 +209,7 @@ class Photo extends BaseObject
207 209
 		$data = "";
208 210
 		$backend_ref = "";
209 211
 		$backend_class = Config::get("storage", "class", "");
210
-		if ($backend_class==="") {
212
+		if ($backend_class === "") {
211 213
 			$data = $Image->asString();
212 214
 		} else {
213 215
 			$backend_ref = $backend_class::put($Image->asString());

+ 14
- 12
src/Model/Storage/Filesystem.php View File

@@ -24,7 +24,7 @@ use Friendica\Util\Strings;
24 24
 class Filesystem implements IStorage
25 25
 {
26 26
 	// Default base folder
27
-	const DEFAULT_BASE_FOLDER="storage";
27
+	const DEFAULT_BASE_FOLDER = "storage";
28 28
 
29 29
 	private static function getBasePath()
30 30
 	{
@@ -39,28 +39,26 @@ class Filesystem implements IStorage
39 39
 	private static function pathForRef($ref)
40 40
 	{
41 41
 		$base = self::getBasePath();
42
-		$fold1 = substr($ref,0,2);
43
-		$fold2 = substr($ref,2,2);
44
-		$file = substr($ref,4);
42
+		$fold1 = substr($ref, 0, 2);
43
+		$fold2 = substr($ref, 2, 2);
44
+		$file = substr($ref, 4);
45 45
 
46 46
 		return "{$base}/{$fold1}/{$fold2}/{$file}";
47 47
 	}
48
-	/*
49
-
50
-	}
51
-	*/
52 48
 
53 49
 	public static function get($ref)
54 50
 	{
55 51
 		$file = self::pathForRef($ref);
56
-		if (!is_file($file)) return "";
52
+		if (!is_file($file)) {
53
+			return "";
54
+		}
57 55
 
58 56
 		return file_get_contents($file);
59 57
 	}
60 58
 
61
-	public static function put($data, $ref = null)
59
+	public static function put($data, $ref = "")
62 60
 	{
63
-		if (is_null($ref)) {
61
+		if ($ref === "") {
64 62
 			$ref = Strings::getRandomHex();
65 63
 		}
66 64
 
@@ -87,7 +85,11 @@ class Filesystem implements IStorage
87 85
 	public static function delete($ref)
88 86
 	{
89 87
 		$file = self::pathForRef($ref);
88
+		// return true if file doesn't exists. we want to delete it: success with zero work!
89
+		if (!is_file($file)) { 
90
+			return true;
91
+		}
90 92
 		return unlink($file);
91 93
 	}
92 94
 
93
-}
95
+}

+ 3
- 3
src/Model/Storage/IStorage.php View File

@@ -19,12 +19,12 @@ interface IStorage
19 19
 	public static function get($ref);
20 20
 
21 21
 	/**
22
-	 * @brief Put data in backend as $ref. If $ref is null a new reference is created.
22
+	 * @brief Put data in backend as $ref. If $ref is not defiend a new reference is created.
23 23
 	 * @param string  $data  Data to save
24 24
 	 * @param string  $ref   Data referece. Optional.
25 25
 	 * @return string Saved data referece
26 26
 	 */
27
-	public static function put($data, $ref = null);
27
+	public static function put($data, $ref = "");
28 28
 
29 29
 	/**
30 30
 	 * @brief Remove data from backend
@@ -32,4 +32,4 @@ interface IStorage
32 32
 	 * @return boolean  True on success
33 33
 	 */
34 34
 	public static function delete($ref);
35
-}
35
+}

+ 8
- 4
src/Model/Storage/SystemResource.php View File

@@ -15,18 +15,22 @@ namespace Friendica\Model\Storage;
15 15
 class SystemResource implements IStorage
16 16
 {
17 17
 	// Valid folders to look for resources
18
-	const VALID_FOLDERS = [ "images" ];
18
+	const VALID_FOLDERS = ["images"];
19 19
 
20 20
 	public static function get($filename)
21 21
 	{
22 22
 		$folder = dirname($filename);
23
-		if (!in_array($folder, self::VALID_FOLDERS)) return "";
24
-		if (!file_exists($filename)) return "";
23
+		if (!in_array($folder, self::VALID_FOLDERS)) {
24
+			return "";
25
+		}
26
+		if (!file_exists($filename)) {
27
+			return "";
28
+		}
25 29
 		return file_get_contents($filename);
26 30
 	}
27 31
 
28 32
 
29
-	public static function put($data, $filename=null)
33
+	public static function put($data, $filename="")
30 34
 	{
31 35
 		throw new \BadMethodCallException();
32 36
 	}

+ 21
- 20
src/Module/Photo.php View File

@@ -7,6 +7,7 @@ namespace Friendica\Module;
7 7
 
8 8
 use Friendica\BaseModule;
9 9
 use Friendica\Core\Logger;
10
+use Friendica\Core\System;
10 11
 use Friendica\Model\Photo as MPhoto;
11 12
 use Friendica\Object\Image;
12 13
 use Friendica\Util\Security;
@@ -30,22 +31,21 @@ class Photo extends BaseModule
30 31
 	{
31 32
 		$a = self::getApp();
32 33
 		if ($a->argc <= 1 || $a->argc > 4) {
33
-			throw new BadRequestException();
34
-			killme();
34
+			System::httpExit(400, "Bad Request");
35 35
 		}
36 36
 
37
-		if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
38
-			header('HTTP/1.1 304 Not Modified');
37
+		if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
38
+			header("HTTP/1.1 304 Not Modified");
39 39
 			header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
40
-			if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
41
-				header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
40
+			if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
41
+				header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
42 42
 			}
43 43
 			header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
44 44
 			header("Cache-Control: max-age=31536000");
45
-			if (function_exists('header_remove')) {
46
-				header_remove('Last-Modified');
47
-				header_remove('Expires');
48
-				header_remove('Cache-Control');
45
+			if (function_exists("header_remove")) {
46
+				header_remove("Last-Modified");
47
+				header_remove("Expires");
48
+				header_remove("Cache-Control");
49 49
 			}
50 50
 			exit;
51 51
 		}
@@ -64,7 +64,7 @@ class Photo extends BaseModule
64 64
 		case 2:
65 65
 			$photoid = self::stripExtension($a->argv[1]);
66 66
 			$scale = 0;
67
-			if (substr($photoid, -2, 1) == '-') {
67
+			if (substr($photoid, -2, 1) == "-") {
68 68
 				$scale = intval(substr($photoid, -1, 1));
69 69
 				$photoid = substr($photoid, 0, -2);
70 70
 			}
@@ -73,6 +73,7 @@ class Photo extends BaseModule
73 73
 		}
74 74
 
75 75
 		if ($photo===false) {
76
+			// not using System::httpExit() because we don't want html here.
76 77
 			header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
77 78
 			killme();
78 79
 		}
@@ -82,8 +83,8 @@ class Photo extends BaseModule
82 83
 		$img = MPhoto::getImageForPhoto($photo);
83 84
 
84 85
 		if (is_null($img) || !$img->isValid()) {
85
-			Logger::log("Invalid photo with id {$photo['id']}.");
86
-			throw new InternalServerErrorException();
86
+			Logger::log("Invalid photo with id {$photo["id"]}.");
87
+			System::httpExit(500, "Internal Server Error");
87 88
 		}
88 89
 
89 90
 
@@ -93,9 +94,9 @@ class Photo extends BaseModule
93 94
 		}
94 95
 
95 96
 
96
-		if (function_exists('header_remove')) {
97
-			header_remove('Pragma');
98
-			header_remove('pragma');
97
+		if (function_exists("header_remove")) {
98
+			header_remove("Pragma");
99
+			header_remove("pragma");
99 100
 		}
100 101
 
101 102
 		header("Content-type: " . $img->getType());
@@ -106,14 +107,14 @@ class Photo extends BaseModule
106 107
 			// and subsequently have permission to see it
107 108
 			header("Cache-Control: no-store, no-cache, must-revalidate");
108 109
 		} else {
110
+			$md5 = md5($img->asString());
109 111
 			header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
110
-			header('Etag: "' . md5($img->asString()) . '"');
112
+			header("Etag: \"{$md5}\"");
111 113
 			header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
112 114
 			header("Cache-Control: max-age=31536000");
113 115
 		}
114 116
 
115 117
 
116
-
117 118
 		echo $img->asString();
118 119
 
119 120
 
@@ -124,7 +125,7 @@ class Photo extends BaseModule
124 125
 	{
125 126
 		$name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
126 127
 		foreach (Image::supportedTypes() AS $m => $e) {
127
-			$name = str_replace('.' . $e, '', $name);
128
+			$name = str_replace("." . $e, "", $name);
128 129
 		}
129 130
 		return $name;
130 131
 	}
@@ -155,4 +156,4 @@ class Photo extends BaseModule
155 156
 		return $photo;
156 157
 	}
157 158
 
158
-}
159
+}

Loading…
Cancel
Save