7 use Friendica\Database\DBA;
8 use Friendica\Object\Image;
10 require_once 'include/security.php';
12 function photo_init(App $a)
16 $prvcachecontrol = false;
21 $person = $a->argv[3];
22 $customres = intval($a->argv[2]);
26 $person = $a->argv[2];
39 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
40 header('HTTP/1.1 304 Not Modified');
41 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
42 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
43 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
45 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
46 header("Cache-Control: max-age=31536000");
47 if (function_exists('header_remove')) {
48 header_remove('Last-Modified');
49 header_remove('Expires');
50 header_remove('Cache-Control');
55 $default = 'images/person-175.jpg';
67 $default = 'images/person-48.jpg';
72 $default = 'images/person-80.jpg';
76 $uid = str_replace(['.jpg', '.png', '.gif'], ['', '', ''], $person);
78 foreach (Image::supportedTypes() AS $m => $e) {
79 $uid = str_replace('.' . $e, '', $uid);
82 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
86 if (DBA::isResult($r)) {
87 $data = $r[0]['data'];
88 $mimetype = $r[0]['type'];
91 $data = file_get_contents($default);
92 $mimetype = 'image/jpeg';
97 $photo = str_replace(['.jpg', '.png', '.gif'], ['', '', ''], $photo);
99 foreach (Image::supportedTypes() AS $m => $e) {
100 $photo = str_replace('.' . $e, '', $photo);
103 if (substr($photo, -2, 1) == '-') {
104 $resolution = intval(substr($photo, -1, 1));
105 $photo = substr($photo, 0, -2);
108 // check if the photo exists and get the owner of the photo
109 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
113 if (DBA::isResult($r)) {
114 $sql_extra = permissions_sql($r[0]['uid']);
116 // Now we'll see if we can access the photo
117 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
121 if (DBA::isResult($r)) {
122 $resolution = $r[0]['scale'];
123 $data = $r[0]['data'];
124 $mimetype = $r[0]['type'];
125 $public = $r[0]['allow_cid'] == '' && $r[0]['allow_gid'] == '' && $r[0]['deny_cid'] == '' && $r[0]['deny_gid'] == '';
127 // The picure exists. We already checked with the first query.
128 // obviously, this is not an authorized viev!
129 $data = file_get_contents('images/nosign.jpg');
130 $mimetype = 'image/jpeg';
131 $prvcachecontrol = true;
138 if (isset($resolution)) {
139 switch ($resolution) {
141 $data = file_get_contents('images/person-175.jpg');
142 $mimetype = 'image/jpeg';
145 $data = file_get_contents('images/person-80.jpg');
146 $mimetype = 'image/jpeg';
149 $data = file_get_contents('images/person-48.jpg');
150 $mimetype = 'image/jpeg';
160 // Resize only if its not a GIF and it is supported by the library
161 if ($mimetype != "image/gif" && in_array($mimetype, Image::supportedTypes())) {
162 $Image = new Image($data, $mimetype);
163 if ($Image->isValid()) {
164 if (isset($customres) && $customres > 0 && $customres < 500) {
165 $Image->scaleToSquare($customres);
167 $data = $Image->asString();
168 $mimetype = $Image->getType();
172 if (function_exists('header_remove')) {
173 header_remove('Pragma');
174 header_remove('pragma');
177 header("Content-type: " . $mimetype);
179 if ($prvcachecontrol) {
180 // it is a private photo that they have no permission to view.
181 // tell the browser not to cache it, in case they authenticate
182 // and subsequently have permission to see it
183 header("Cache-Control: no-store, no-cache, must-revalidate");
185 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
186 header('Etag: "' . md5($data) . '"');
187 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
188 header("Cache-Control: max-age=31536000");
192 // If the photo is public and there is an existing photo directory store the photo there
193 if ($public and $file != '') {
194 // If the photo path isn't there, try to create it
195 $basepath = $a->get_basepath();
196 if (!is_dir($basepath . "/photo")) {
197 if (is_writable($basepath)) {
198 mkdir($basepath . "/photo");
202 if (is_dir($basepath . "/photo")) {
203 file_put_contents($basepath . "/photo/" . $file, $data);