5 require_once('include/security.php');
6 require_once('include/Photo.php');
8 function photo_init(App $a) {
11 $prvcachecontrol = false;
16 $person = $a->argv[3];
17 $customres = intval($a->argv[2]);
21 $person = $a->argv[2];
34 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
35 header('HTTP/1.1 304 Not Modified');
36 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
37 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
38 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
39 header("Cache-Control: max-age=31536000");
40 if (function_exists('header_remove')) {
41 header_remove('Last-Modified');
42 header_remove('Expires');
43 header_remove('Cache-Control');
48 $default = 'images/person-175.jpg';
64 $default = 'images/person-48.jpg';
69 $default = 'images/person-80.jpg';
73 $uid = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $person);
75 foreach (Photo::supportedTypes() AS $m => $e) {
76 $uid = str_replace('.'.$e, '', $uid);
79 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
83 if (dbm::is_result($r)) {
84 $data = $r[0]['data'];
85 $mimetype = $r[0]['type'];
88 $data = file_get_contents($default);
89 $mimetype = 'image/jpeg';
98 $photo = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $photo);
100 foreach (Photo::supportedTypes() AS $m => $e) {
101 $photo = str_replace('.'.$e, '', $photo);
104 if (substr($photo, -2, 1) == '-') {
105 $resolution = intval(substr($photo, -1, 1));
106 $photo = substr($photo, 0, -2);
109 // check if the photo exists and get the owner of the photo
110 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
114 if (dbm::is_result($r)) {
116 $sql_extra = permissions_sql($r[0]['uid']);
118 // Now we'll see if we can access the photo
120 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
125 $public = (dbm::is_result($r)) && ($r[0]['allow_cid'] == '') && ($r[0]['allow_gid'] == '') && ($r[0]['deny_cid'] == '') && ($r[0]['deny_gid'] == '');
127 if (dbm::is_result($r)) {
128 $resolution = $r[0]['scale'];
129 $data = $r[0]['data'];
130 $mimetype = $r[0]['type'];
132 // The picure exists. We already checked with the first query.
133 // obviously, this is not an authorized viev!
134 $data = file_get_contents('images/nosign.jpg');
135 $mimetype = 'image/jpeg';
136 $prvcachecontrol = true;
143 if (isset($resolution)) {
144 switch ($resolution) {
147 $data = file_get_contents('images/person-175.jpg');
148 $mimetype = 'image/jpeg';
151 $data = file_get_contents('images/person-80.jpg');
152 $mimetype = 'image/jpeg';
155 $data = file_get_contents('images/person-48.jpg');
156 $mimetype = 'image/jpeg';
166 // Resize only if its not a GIF and it is supported by the library
167 if (($mimetype != "image/gif") && in_array($mimetype, Photo::supportedTypes())) {
168 $ph = new Photo($data, $mimetype);
169 if ($ph->is_valid()) {
170 if (isset($customres) && $customres > 0 && $customres < 500) {
171 $ph->scaleImageSquare($customres);
173 $data = $ph->imageString();
174 $mimetype = $ph->getType();
178 if (function_exists('header_remove')) {
179 header_remove('Pragma');
180 header_remove('pragma');
183 header("Content-type: ".$mimetype);
185 if ($prvcachecontrol) {
187 // it is a private photo that they have no permission to view.
188 // tell the browser not to cache it, in case they authenticate
189 // and subsequently have permission to see it
191 header("Cache-Control: no-store, no-cache, must-revalidate");
194 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
195 header('Etag: "'.md5($data).'"');
196 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
197 header("Cache-Control: max-age=31536000");
201 // If the photo is public and there is an existing photo directory store the photo there
202 if ($public and ($file != "")) {
203 // If the photo path isn't there, try to create it
204 $basepath = $a->get_basepath();
205 if (!is_dir($basepath."/photo")) {
206 if (is_writable($basepath)) {
207 mkdir($basepath."/photo");
211 if (is_dir($basepath."/photo")) {
212 file_put_contents($basepath."/photo/".$file, $data);