3 require_once('Photo.php');
5 function photos_init(&$a) {
9 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
16 $a->data['user'] = $r[0];
18 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
19 intval($a->data['user']['uid'])
23 $a->data['albums'] = $albums;
25 $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
26 $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
29 foreach($albums as $album) {
30 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
32 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>';
36 $a->page['aside'] .= $o;
45 function photos_post(&$a) {
49 notice( t('Permission denied.') . EOL );
53 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
54 intval($_SESSION['uid'])
57 $contact_record = $r[0];
59 if(! x($_FILES,'userfile'))
62 if($_POST['partitionCount'])
67 $album = notags(trim($_POST['album']));
68 $newalbum = notags(trim($_POST['newalbum']));
70 if(! strlen($album)) {
74 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
77 $src = $_FILES['userfile']['tmp_name'];
78 $filename = basename($_FILES['userfile']['name']);
79 $filesize = intval($_FILES['userfile']['size']);
81 $imagedata = @file_get_contents($src);
82 $ph = new Photo($imagedata);
84 if(! ($image = $ph->getImage())) {
85 notice( t('Unable to process image.') . EOL );
92 $width = $ph->getWidth();
93 $height = $ph->getHeight();
97 $photo_hash = hash('md5',uniqid(mt_rand(),true));
99 $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 );
102 notice( t('Image upload failed.') . EOL );
106 if($width > 640 || $height > 640) {
107 $ph->scaleImage(640);
108 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1 );
112 if($width > 320 || $height > 320) {
113 $ph->scaleImage(320);
114 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2 );
118 $basename = basename($filename);
120 // Create item container
122 $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']'
123 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
128 $item_hash = random_string();
130 $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $item_hash;
132 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
136 } while($dups == true);
139 $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
140 `edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
141 VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
142 intval($_SESSION['uid']),
145 intval($contact_record['id']),
146 dbesc($contact_record['name']),
147 dbesc($contact_record['url']),
148 dbesc($contact_record['thumb']),
155 dbesc($str_contact_allow),
156 dbesc($str_group_allow),
157 dbesc($str_contact_deny),
158 dbesc($str_group_deny)
163 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
167 q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
174 // if album has no featured photo, promote one.
178 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
179 return; // NOTREACHED
183 return; // NOTREACHED
189 function photos_content(&$a) {
193 // photos/name/upload
194 // photos/name/album/xxxxx
195 // photos/name/album/xxxxx/edit
196 // photos/name/album/xxxxx/drop
197 // photos/name/image/xxxxx
198 // photos/name/image/xxxxx/edit
199 // photos/name/image/xxxxx/drop
201 if(! x($a->data,'user')) {
202 notice( t('No photos selected') . EOL );
206 $_SESSION['photo_return'] = $a->cmd;
213 $datatype = $a->argv[2];
214 $datum = $a->argv[3];
216 elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
217 $datatype = 'upload';
219 $datatype = 'summary';
227 // Setup permissions structures
230 $owner_uid = $a->data['user']['uid'];
233 $contact_id = $_SESSION['visitor_id'];
234 $groups = init_groups_visitor($contact_id);
237 // default permissions - anonymous user
239 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
241 // Profile owner - everything is visible
243 if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
246 elseif(remote_user()) {
247 // authenticated visitor - here lie dragons
248 $gs = '<<>>'; // should be impossible to match
250 foreach($groups as $g)
251 $gs .= '|<' . intval($g) . '>';
253 $sql_extra = sprintf(
254 " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
255 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
256 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
257 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
259 intval($_SESSION['visitor_id']),
260 intval($_SESSION['visitor_id']),
271 if($datatype == 'upload') {
272 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
273 notice( t('Permission denied.'));
276 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
278 $albumselect .= '<option value="" selected="selected" > </option>';
279 if(count($a->data['albums'])) {
280 foreach($a->data['albums'] as $album) {
281 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
283 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
286 $albumselect .= '</select>';
287 $tpl = file_get_contents('view/photos_upload.tpl');
288 $o .= replace_macros($tpl,array(
289 '$pagename' => t('Upload Photos'),
290 '$sessid' => session_id(),
291 '$newalbum' => t('New album name: '),
292 '$existalbumtext' => t('or existing album name: '),
293 '$filestext' => t('Select files to upload: '),
294 '$albumselect' => $albumselect,
295 '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
296 '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
297 '$uploadurl' => $a->get_baseurl() . '/photos',
298 '$submit' => t('Submit')
305 if($datatype == 'album') {
307 $album = hex2bin($datum);
309 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
310 $sql_extra GROUP BY `resource-id`",
311 intval($a->data['user']['uid']),
315 $a->set_pager_total(count($r));
318 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
319 $sql_extra GROUP BY `resource-id` LIMIT %d , %d",
320 intval($a->data['user']['uid']),
322 intval($a->pager['start']),
323 intval($a->pager['itemspage'])
326 $o .= '<h3>' . $album . '</h3>';
328 $tpl = file_get_contents('view/photo_album.tpl');
331 $o .= replace_macros($tpl,array(
333 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
334 '$phototitle' => t('View Photo'),
335 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
336 '$imgalt' => $rr['filename']
340 $o .= '<div id="photo-album-end"></div>';
346 if($datatype == 'image') {
348 // fetch item containing image, then comments
349 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
350 $sql_extra ORDER BY `scale` ASC ",
351 intval($a->data['user']['uid']),
356 notice( t('Photo not available') . EOL );
361 $hires = $lores = $r[0];
367 $o .= '<a href="' . $a->get_baseurl() . '/photo/'
368 . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="'
369 . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/'
370 . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
376 // Default - show recent photos with upload link (if applicable)
378 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
379 $sql_extra GROUP BY `resource-id`",
380 intval($a->data['user']['uid']),
381 dbesc( t('Contact Photos'))
384 $a->set_pager_total(count($r));
387 $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
388 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
389 intval($a->data['user']['uid']),
390 dbesc( t('Contact Photos')),
391 intval($a->pager['start']),
392 intval($a->pager['itemspage'])
395 $o .= '<h3>' . t('Recent Photos') . '</h3>';
397 if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
398 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/'
399 . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
402 $tpl = file_get_contents('view/photo_top.tpl');
405 $o .= replace_macros($tpl,array(
407 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname']
408 . '/image/' . $rr['resource-id'],
409 '$phototitle' => t('View Photo'),
410 '$imgsrc' => $a->get_baseurl() . '/photo/'
411 . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
412 '$albumlink' => $a->get_baseurl . '/photos/'
413 . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
414 '$albumname' => $rr['album'],
415 '$albumalt' => t('View Album'),
416 '$imgalt' => $rr['filename']
420 $o .= '<div id="photo-top-end"></div>';