7 use Friendica\Database\DBA;
8 use Friendica\Object\Image;
9 use Friendica\Util\Security;
11 function photo_init(App $a)
15 $prvcachecontrol = false;
20 $person = $a->argv[3];
21 $customres = intval($a->argv[2]);
25 $person = $a->argv[2];
38 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
39 header('HTTP/1.1 304 Not Modified');
40 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
41 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
42 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
44 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
45 header("Cache-Control: max-age=31536000");
46 if (function_exists('header_remove')) {
47 header_remove('Last-Modified');
48 header_remove('Expires');
49 header_remove('Cache-Control');
54 $default = 'images/person-300.jpg';
66 $default = 'images/person-48.jpg';
71 $default = 'images/person-80.jpg';
75 $uid = str_replace(['.jpg', '.png', '.gif'], ['', '', ''], $person);
77 foreach (Image::supportedTypes() AS $m => $e) {
78 $uid = str_replace('.' . $e, '', $uid);
81 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
85 if (DBA::isResult($r)) {
86 $data = $r[0]['data'];
87 $mimetype = $r[0]['type'];
90 $data = file_get_contents($default);
91 $mimetype = 'image/jpeg';
96 $photo = str_replace(['.jpg', '.png', '.gif'], ['', '', ''], $photo);
98 foreach (Image::supportedTypes() AS $m => $e) {
99 $photo = str_replace('.' . $e, '', $photo);
102 if (substr($photo, -2, 1) == '-') {
103 $resolution = intval(substr($photo, -1, 1));
104 $photo = substr($photo, 0, -2);
107 // check if the photo exists and get the owner of the photo
108 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
112 if (DBA::isResult($r)) {
113 $sql_extra = Security::getPermissionsSQLByUserId($r[0]['uid']);
115 // Now we'll see if we can access the photo
116 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
120 if (DBA::isResult($r)) {
121 $resolution = $r[0]['scale'];
122 $data = $r[0]['data'];
123 $mimetype = $r[0]['type'];
124 $public = $r[0]['allow_cid'] == '' && $r[0]['allow_gid'] == '' && $r[0]['deny_cid'] == '' && $r[0]['deny_gid'] == '';
126 // The picure exists. We already checked with the first query.
127 // obviously, this is not an authorized viev!
128 $data = file_get_contents('images/nosign.jpg');
129 $mimetype = 'image/jpeg';
130 $prvcachecontrol = true;
137 if (isset($resolution)) {
138 switch ($resolution) {
140 $data = file_get_contents('images/person-300.jpg');
141 $mimetype = 'image/jpeg';
144 $data = file_get_contents('images/person-80.jpg');
145 $mimetype = 'image/jpeg';
148 $data = file_get_contents('images/person-48.jpg');
149 $mimetype = 'image/jpeg';
159 // Resize only if its not a GIF and it is supported by the library
160 if ($mimetype != "image/gif" && in_array($mimetype, Image::supportedTypes())) {
161 $Image = new Image($data, $mimetype);
162 if ($Image->isValid()) {
163 if (isset($customres) && $customres > 0 && $customres < 500) {
164 $Image->scaleToSquare($customres);
166 $data = $Image->asString();
167 $mimetype = $Image->getType();
171 if (function_exists('header_remove')) {
172 header_remove('Pragma');
173 header_remove('pragma');
176 header("Content-type: " . $mimetype);
178 if ($prvcachecontrol) {
179 // it is a private photo that they have no permission to view.
180 // tell the browser not to cache it, in case they authenticate
181 // and subsequently have permission to see it
182 header("Cache-Control: no-store, no-cache, must-revalidate");
184 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
185 header('Etag: "' . md5($data) . '"');
186 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
187 header("Cache-Control: max-age=31536000");
191 // If the photo is public and there is an existing photo directory store the photo there
192 if ($public and $file != '') {
193 // If the photo path isn't there, try to create it
194 $basepath = $a->getBasePath();
195 if (!is_dir($basepath . "/photo")) {
196 if (is_writable($basepath)) {
197 mkdir($basepath . "/photo");
201 if (is_dir($basepath . "/photo")) {
202 file_put_contents($basepath . "/photo/" . $file, $data);