3 require_once('include/security.php');
4 require_once('include/Photo.php');
6 if(! function_exists('photo_init')) {
7 function photo_init(&$a) {
10 $prvcachecontrol = false;
15 $person = $a->argv[3];
16 $customres = intval($a->argv[2]);
20 $person = $a->argv[2];
33 // strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
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';
65 $default = 'images/person-48.jpg';
70 $default = 'images/person-80.jpg';
74 $uid = str_replace(array('.jpg','.png'),array('',''), $person);
76 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
81 $data = $r[0]['data'];
82 $mimetype = $r[0]['type'];
85 $data = file_get_contents($default);
86 $mimetype = 'image/jpeg';
96 foreach( Photo::supportedTypes() as $m=>$e){
97 $photo = str_replace(".$e",'',$photo);
100 if(substr($photo,-2,1) == '-') {
101 $resolution = intval(substr($photo,-1,1));
102 $photo = substr($photo,0,-2);
105 // check if the photo exists and get the owner of the photo
106 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
112 $sql_extra = permissions_sql($r[0]['uid']);
114 // Now we'll see if we can access the photo
116 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
121 $public = ($r[0]['allow_cid'] == '') AND ($r[0]['allow_gid'] == '') AND ($r[0]['deny_cid'] == '') AND ($r[0]['deny_gid'] == '');
124 $resolution = $r[0]['scale'];
125 $data = $r[0]['data'];
126 $mimetype = $r[0]['type'];
128 // The picure exists. We already checked with the first query.
129 // obviously, this is not an authorized viev!
130 $data = file_get_contents('images/nosign.jpg');
131 $mimetype = 'image/jpeg';
132 $prvcachecontrol = true;
139 if(isset($resolution)) {
140 switch($resolution) {
143 $data = file_get_contents('images/person-175.jpg');
144 $mimetype = 'image/jpeg';
147 $data = file_get_contents('images/person-80.jpg');
148 $mimetype = 'image/jpeg';
151 $data = file_get_contents('images/person-48.jpg');
152 $mimetype = 'image/jpeg';
162 // Resize only if its not a GIF
163 if ($mime != "image/gif") {
164 $ph = new Photo($data, $mimetype);
165 if($ph->is_valid()) {
166 if(isset($customres) && $customres > 0 && $customres < 500) {
167 $ph->scaleImageSquare($customres);
169 $data = $ph->imageString();
170 $mimetype = $ph->getType();
174 if(function_exists('header_remove')) {
175 header_remove('Pragma');
176 header_remove('pragma');
179 header("Content-type: ".$mimetype);
181 if($prvcachecontrol) {
183 // it is a private photo that they have no permission to view.
184 // tell the browser not to cache it, in case they authenticate
185 // and subsequently have permission to see it
187 header("Cache-Control: no-store, no-cache, must-revalidate");
191 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
192 header('Etag: "'.md5($data).'"');
193 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
194 header("Cache-Control: max-age=31536000");
198 // If the photo is public and there is an existing photo directory store the photo there
199 if ($public and ($file != "")) {
200 // If the photo path isn't there, try to create it
201 $basepath = $a->get_basepath();
202 if (!is_dir($basepath."/photo"))
203 if (is_writable($basepath))
204 mkdir($basepath."/photo");
206 if (is_dir($basepath."/photo"))
207 file_put_contents($basepath."/photo/".$file, $data);