7 use Friendica\Database\DBM;
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 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
43 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
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');
53 $default = 'images/person-175.jpg';
65 $default = 'images/person-48.jpg';
70 $default = 'images/person-80.jpg';
74 $uid = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $person);
76 foreach (Image::supportedTypes() AS $m => $e) {
77 $uid = str_replace('.' . $e, '', $uid);
80 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
84 if (DBM::is_result($r)) {
85 $data = $r[0]['data'];
86 $mimetype = $r[0]['type'];
89 $data = file_get_contents($default);
90 $mimetype = 'image/jpeg';
95 $photo = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $photo);
97 foreach (Image::supportedTypes() AS $m => $e) {
98 $photo = str_replace('.' . $e, '', $photo);
101 if (substr($photo, -2, 1) == '-') {
102 $resolution = intval(substr($photo, -1, 1));
103 $photo = substr($photo, 0, -2);
106 // check if the photo exists and get the owner of the photo
107 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
111 if (DBM::is_result($r)) {
112 $sql_extra = permissions_sql($r[0]['uid']);
114 // Now we'll see if we can access the photo
115 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
119 if (DBM::is_result($r)) {
120 $resolution = $r[0]['scale'];
121 $data = $r[0]['data'];
122 $mimetype = $r[0]['type'];
123 $public = $r[0]['allow_cid'] == '' && $r[0]['allow_gid'] == '' && $r[0]['deny_cid'] == '' && $r[0]['deny_gid'] == '';
125 // The picure exists. We already checked with the first query.
126 // obviously, this is not an authorized viev!
127 $data = file_get_contents('images/nosign.jpg');
128 $mimetype = 'image/jpeg';
129 $prvcachecontrol = true;
136 if (isset($resolution)) {
137 switch ($resolution) {
139 $data = file_get_contents('images/person-175.jpg');
140 $mimetype = 'image/jpeg';
143 $data = file_get_contents('images/person-80.jpg');
144 $mimetype = 'image/jpeg';
147 $data = file_get_contents('images/person-48.jpg');
148 $mimetype = 'image/jpeg';
158 // Resize only if its not a GIF and it is supported by the library
159 if ($mimetype != "image/gif" && in_array($mimetype, Image::supportedTypes())) {
160 $Image = new Image($data, $mimetype);
161 if ($Image->isValid()) {
162 if (isset($customres) && $customres > 0 && $customres < 500) {
163 $Image->scaleToSquare($customres);
165 $data = $Image->asString();
166 $mimetype = $Image->getType();
170 if (function_exists('header_remove')) {
171 header_remove('Pragma');
172 header_remove('pragma');
175 header("Content-type: " . $mimetype);
177 if ($prvcachecontrol) {
178 // it is a private photo that they have no permission to view.
179 // tell the browser not to cache it, in case they authenticate
180 // and subsequently have permission to see it
181 header("Cache-Control: no-store, no-cache, must-revalidate");
183 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
184 header('Etag: "' . md5($data) . '"');
185 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
186 header("Cache-Control: max-age=31536000");
190 // If the photo is public and there is an existing photo directory store the photo there
191 if ($public and $file != '') {
192 // If the photo path isn't there, try to create it
193 $basepath = $a->get_basepath();
194 if (!is_dir($basepath . "/photo")) {
195 if (is_writable($basepath)) {
196 mkdir($basepath . "/photo");
200 if (is_dir($basepath . "/photo")) {
201 file_put_contents($basepath . "/photo/" . $file, $data);