3 require_once('include/security.php');
4 require_once('include/Photo.php');
6 function photo_init(App &$a) {
9 $prvcachecontrol = false;
14 $person = $a->argv[3];
15 $customres = intval($a->argv[2]);
19 $person = $a->argv[2];
32 // strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
33 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
34 header('HTTP/1.1 304 Not Modified');
35 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
36 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
37 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
38 header("Cache-Control: max-age=31536000");
39 if(function_exists('header_remove')) {
40 header_remove('Last-Modified');
41 header_remove('Expires');
42 header_remove('Cache-Control');
47 $default = 'images/person-175.jpg';
64 $default = 'images/person-48.jpg';
69 $default = 'images/person-80.jpg';
73 $uid = str_replace(array('.jpg','.png'),array('',''), $person);
75 $r = qu("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
79 if (dbm::is_result($r)) {
80 $data = $r[0]['data'];
81 $mimetype = $r[0]['type'];
84 $data = file_get_contents($default);
85 $mimetype = 'image/jpeg';
95 foreach( Photo::supportedTypes() as $m=>$e){
96 $photo = str_replace(".$e",'',$photo);
99 if(substr($photo,-2,1) == '-') {
100 $resolution = intval(substr($photo,-1,1));
101 $photo = substr($photo,0,-2);
104 // check if the photo exists and get the owner of the photo
105 $r = qu("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
109 if (dbm::is_result($r)) {
111 $sql_extra = permissions_sql($r[0]['uid']);
113 // Now we'll see if we can access the photo
115 $r = qu("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
120 $public = (dbm::is_result($r)) AND ($r[0]['allow_cid'] == '') AND ($r[0]['allow_gid'] == '') AND ($r[0]['deny_cid'] == '') AND ($r[0]['deny_gid'] == '');
122 if (dbm::is_result($r)) {
123 $resolution = $r[0]['scale'];
124 $data = $r[0]['data'];
125 $mimetype = $r[0]['type'];
127 // The picure exists. We already checked with the first query.
128 // obviously, this is not an authorized viev!
129 $data = file_get_contents('images/nosign.jpg');
130 $mimetype = 'image/jpeg';
131 $prvcachecontrol = true;
138 if(isset($resolution)) {
139 switch($resolution) {
142 $data = file_get_contents('images/person-175.jpg');
143 $mimetype = 'image/jpeg';
146 $data = file_get_contents('images/person-80.jpg');
147 $mimetype = 'image/jpeg';
150 $data = file_get_contents('images/person-48.jpg');
151 $mimetype = 'image/jpeg';
161 // Resize only if its not a GIF
162 if ($mime != "image/gif") {
163 $ph = new Photo($data, $mimetype);
164 if($ph->is_valid()) {
165 if(isset($customres) && $customres > 0 && $customres < 500) {
166 $ph->scaleImageSquare($customres);
168 $data = $ph->imageString();
169 $mimetype = $ph->getType();
173 if(function_exists('header_remove')) {
174 header_remove('Pragma');
175 header_remove('pragma');
178 header("Content-type: ".$mimetype);
180 if($prvcachecontrol) {
182 // it is a private photo that they have no permission to view.
183 // tell the browser not to cache it, in case they authenticate
184 // and subsequently have permission to see it
186 header("Cache-Control: no-store, no-cache, must-revalidate");
190 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
191 header('Etag: "'.md5($data).'"');
192 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
193 header("Cache-Control: max-age=31536000");
197 // If the photo is public and there is an existing photo directory store the photo there
198 if ($public and ($file != "")) {
199 // If the photo path isn't there, try to create it
200 $basepath = $a->get_basepath();
201 if (!is_dir($basepath."/photo"))
202 if (is_writable($basepath))
203 mkdir($basepath."/photo");
205 if (is_dir($basepath."/photo"))
206 file_put_contents($basepath."/photo/".$file, $data);