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 // strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
35 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
36 header('HTTP/1.1 304 Not Modified');
37 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
38 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
39 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
40 header("Cache-Control: max-age=31536000");
41 if(function_exists('header_remove')) {
42 header_remove('Last-Modified');
43 header_remove('Expires');
44 header_remove('Cache-Control');
49 $default = 'images/person-175.jpg';
66 $default = 'images/person-48.jpg';
71 $default = 'images/person-80.jpg';
75 $uid = str_replace(array('.jpg','.png'),array('',''), $person);
77 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
81 if (dbm::is_result($r)) {
82 $data = $r[0]['data'];
83 $mimetype = $r[0]['type'];
86 $data = file_get_contents($default);
87 $mimetype = 'image/jpeg';
97 foreach( Photo::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)) {
113 $sql_extra = permissions_sql($r[0]['uid']);
115 // 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",
122 $public = (dbm::is_result($r)) && ($r[0]['allow_cid'] == '') && ($r[0]['allow_gid'] == '') && ($r[0]['deny_cid'] == '') && ($r[0]['deny_gid'] == '');
124 if (dbm::is_result($r)) {
125 $resolution = $r[0]['scale'];
126 $data = $r[0]['data'];
127 $mimetype = $r[0]['type'];
129 // The picure exists. We already checked with the first query.
130 // obviously, this is not an authorized viev!
131 $data = file_get_contents('images/nosign.jpg');
132 $mimetype = 'image/jpeg';
133 $prvcachecontrol = true;
140 if(isset($resolution)) {
141 switch($resolution) {
144 $data = file_get_contents('images/person-175.jpg');
145 $mimetype = 'image/jpeg';
148 $data = file_get_contents('images/person-80.jpg');
149 $mimetype = 'image/jpeg';
152 $data = file_get_contents('images/person-48.jpg');
153 $mimetype = 'image/jpeg';
163 // Resize only if its not a GIF
164 if ($mime != "image/gif") {
165 $ph = new Photo($data, $mimetype);
166 if($ph->is_valid()) {
167 if(isset($customres) && $customres > 0 && $customres < 500) {
168 $ph->scaleImageSquare($customres);
170 $data = $ph->imageString();
171 $mimetype = $ph->getType();
175 if(function_exists('header_remove')) {
176 header_remove('Pragma');
177 header_remove('pragma');
180 header("Content-type: ".$mimetype);
182 if($prvcachecontrol) {
184 // it is a private photo that they have no permission to view.
185 // tell the browser not to cache it, in case they authenticate
186 // and subsequently have permission to see it
188 header("Cache-Control: no-store, no-cache, must-revalidate");
192 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
193 header('Etag: "'.md5($data).'"');
194 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
195 header("Cache-Control: max-age=31536000");
199 // If the photo is public and there is an existing photo directory store the photo there
200 if ($public and ($file != "")) {
201 // If the photo path isn't there, try to create it
202 $basepath = $a->get_basepath();
203 if (!is_dir($basepath."/photo"))
204 if (is_writable($basepath))
205 mkdir($basepath."/photo");
207 if (is_dir($basepath."/photo"))
208 file_put_contents($basepath."/photo/".$file, $data);