6 use Friendica\Database\DBM;
7 use Friendica\Object\Image;
9 require_once 'include/security.php';
11 function photo_init(App $a) {
14 $prvcachecontrol = false;
19 $person = $a->argv[3];
20 $customres = intval($a->argv[2]);
24 $person = $a->argv[2];
37 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
38 header('HTTP/1.1 304 Not Modified');
39 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
40 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
41 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
42 header("Cache-Control: max-age=31536000");
43 if (function_exists('header_remove')) {
44 header_remove('Last-Modified');
45 header_remove('Expires');
46 header_remove('Cache-Control');
51 $default = 'images/person-175.jpg';
67 $default = 'images/person-48.jpg';
72 $default = 'images/person-80.jpg';
76 $uid = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $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 (DBM::is_result($r)) {
87 $data = $r[0]['data'];
88 $mimetype = $r[0]['type'];
91 $data = file_get_contents($default);
92 $mimetype = 'image/jpeg';
101 $photo = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $photo);
103 foreach (Image::supportedTypes() AS $m => $e) {
104 $photo = str_replace('.'.$e, '', $photo);
107 if (substr($photo, -2, 1) == '-') {
108 $resolution = intval(substr($photo, -1, 1));
109 $photo = substr($photo, 0, -2);
112 // check if the photo exists and get the owner of the photo
113 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
117 if (DBM::is_result($r)) {
119 $sql_extra = permissions_sql($r[0]['uid']);
121 // Now we'll see if we can access the photo
123 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
128 $public = (DBM::is_result($r)) && ($r[0]['allow_cid'] == '') && ($r[0]['allow_gid'] == '') && ($r[0]['deny_cid'] == '') && ($r[0]['deny_gid'] == '');
130 if (DBM::is_result($r)) {
131 $resolution = $r[0]['scale'];
132 $data = $r[0]['data'];
133 $mimetype = $r[0]['type'];
135 // The picure exists. We already checked with the first query.
136 // obviously, this is not an authorized viev!
137 $data = file_get_contents('images/nosign.jpg');
138 $mimetype = 'image/jpeg';
139 $prvcachecontrol = true;
146 if (isset($resolution)) {
147 switch ($resolution) {
150 $data = file_get_contents('images/person-175.jpg');
151 $mimetype = 'image/jpeg';
154 $data = file_get_contents('images/person-80.jpg');
155 $mimetype = 'image/jpeg';
158 $data = file_get_contents('images/person-48.jpg');
159 $mimetype = 'image/jpeg';
169 // Resize only if its not a GIF and it is supported by the library
170 if (($mimetype != "image/gif") && in_array($mimetype, Image::supportedTypes())) {
171 $Image = new Image($data, $mimetype);
172 if ($Image->isValid()) {
173 if (isset($customres) && $customres > 0 && $customres < 500) {
174 $Image->scaleToSquare($customres);
176 $data = $Image->asString();
177 $mimetype = $Image->getType();
181 if (function_exists('header_remove')) {
182 header_remove('Pragma');
183 header_remove('pragma');
186 header("Content-type: ".$mimetype);
188 if ($prvcachecontrol) {
190 // it is a private photo that they have no permission to view.
191 // tell the browser not to cache it, in case they authenticate
192 // and subsequently have permission to see it
194 header("Cache-Control: no-store, no-cache, must-revalidate");
197 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
198 header('Etag: "'.md5($data).'"');
199 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
200 header("Cache-Control: max-age=31536000");
204 // If the photo is public and there is an existing photo directory store the photo there
205 if ($public and ($file != "")) {
206 // If the photo path isn't there, try to create it
207 $basepath = $a->get_basepath();
208 if (!is_dir($basepath."/photo")) {
209 if (is_writable($basepath)) {
210 mkdir($basepath."/photo");
214 if (is_dir($basepath."/photo")) {
215 file_put_contents($basepath."/photo/".$file, $data);