aboutsummaryrefslogtreecommitdiffstats
path: root/include/photos.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/photos.php')
-rw-r--r--include/photos.php244
1 files changed, 158 insertions, 86 deletions
diff --git a/include/photos.php b/include/photos.php
index 43386d626..29e2f32e2 100644
--- a/include/photos.php
+++ b/include/photos.php
@@ -1,10 +1,21 @@
-<?php /** @file */
+<?php
+/**
+ * @file include/photos.php
+ * @brief Functions related to photo handling.
+ */
require_once('include/permissions.php');
require_once('include/items.php');
require_once('include/photo/photo_driver.php');
-
+/**
+ * @brief
+ *
+ * @param array $channel
+ * @param array $observer
+ * @param array $args
+ * @return array
+ */
function photo_upload($channel, $observer, $args) {
$ret = array('success' => false);
@@ -18,12 +29,12 @@ function photo_upload($channel, $observer, $args) {
call_hooks('photo_upload_begin', $args);
- /**
+ /*
* Determine the album to use
*/
- $album = $args['album'];
- $newalbum = $args['newalbum'];
+ $album = $args['album'];
+ $newalbum = $args['newalbum'];
logger('photo_upload: album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG);
@@ -31,39 +42,20 @@ function photo_upload($channel, $observer, $args) {
if($newalbum)
$album = $newalbum;
else
- $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
+ $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y-m');
}
- /**
- *
- * We create a wall item for every photo, but we don't want to
- * overwhelm the data stream with a hundred newly uploaded photos.
- * So we will make the first photo uploaded to this album in the last several hours
- * visible by default, the rest will become visible over time when and if
- * they acquire comments, likes, dislikes, and/or tags
- *
- */
-
- $r = q("SELECT * FROM photo WHERE album = '%s' AND uid = %d AND created > %s - INTERVAL %s ",
- dbesc($album),
- intval($channel_id),
- db_utcnow(), db_quoteinterval('3 HOUR')
- );
- if((! $r) || ($album == t('Profile Photos')))
+ if(intval($args['visible']) || $args['visible'] === 'true')
$visible = 1;
else
$visible = 0;
-
- if(intval($args['not_visible']) || $args['not_visible'] === 'true')
- $visible = 0;
$str_group_allow = perms2str(((is_array($args['group_allow'])) ? $args['group_allow'] : explode(',',$args['group_allow'])));
$str_contact_allow = perms2str(((is_array($args['contact_allow'])) ? $args['contact_allow'] : explode(',',$args['contact_allow'])));
$str_group_deny = perms2str(((is_array($args['group_deny'])) ? $args['group_deny'] : explode(',',$args['group_deny'])));
$str_contact_deny = perms2str(((is_array($args['contact_deny'])) ? $args['contact_deny'] : explode(',',$args['contact_deny'])));
-
- if($args['data']) {
+ if ($args['data']) {
// allow an import from a binary string representing the image.
// This bypasses the upload step and max size limit checking
@@ -74,23 +66,21 @@ function photo_upload($channel, $observer, $args) {
// this is going to be deleted if it exists
$src = '/tmp/deletemenow';
$type = $args['type'];
- }
- else {
+ } else {
$f = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
call_hooks('photo_upload_file',$f);
- if(x($f,'src') && x($f,'filesize')) {
+ if (x($f,'src') && x($f,'filesize')) {
$src = $f['src'];
$filename = $f['filename'];
$filesize = $f['filesize'];
$type = $f['type'];
- }
- else {
- $src = $_FILES['userfile']['tmp_name'];
- $filename = basename($_FILES['userfile']['name']);
- $filesize = intval($_FILES['userfile']['size']);
- $type = $_FILES['userfile']['type'];
+ } else {
+ $src = $_FILES['userfile']['tmp_name'];
+ $filename = basename($_FILES['userfile']['name']);
+ $filesize = intval($_FILES['userfile']['size']);
+ $type = $_FILES['userfile']['type'];
}
if (! $type)
@@ -98,17 +88,16 @@ function photo_upload($channel, $observer, $args) {
logger('photo_upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
-
$maximagesize = get_config('system','maximagesize');
- if(($maximagesize) && ($filesize > $maximagesize)) {
+ if (($maximagesize) && ($filesize > $maximagesize)) {
$ret['message'] = sprintf ( t('Image exceeds website size limit of %lu bytes'), $maximagesize);
@unlink($src);
call_hooks('photo_upload_end',$ret);
return $ret;
}
- if(! $filesize) {
+ if (! $filesize) {
$ret['message'] = t('Image file is empty.');
@unlink($src);
call_hooks('photo_post_end',$ret);
@@ -120,24 +109,22 @@ function photo_upload($channel, $observer, $args) {
$imagedata = @file_get_contents($src);
}
-
$r = q("select sum(size) as total from photo where aid = %d and scale = 0 ",
intval($account_id)
);
$limit = service_class_fetch($channel_id,'photo_upload_limit');
- if(($r) && ($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
+ if (($r) && ($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
$ret['message'] = upgrade_message();
@unlink($src);
call_hooks('photo_post_end',$ret);
return $ret;
}
-
$ph = photo_factory($imagedata, $type);
- if(! $ph->is_valid()) {
+ if (! $ph->is_valid()) {
$ret['message'] = t('Unable to process image');
logger('photo_upload: unable to process image');
@unlink($src);
@@ -145,13 +132,14 @@ function photo_upload($channel, $observer, $args) {
return $ret;
}
- $ph->orient($src);
+ $exif = $ph->orient($src);
+
@unlink($src);
$max_length = get_config('system','max_image_length');
- if(! $max_length)
+ if (! $max_length)
$max_length = MAX_IMAGE_LENGTH;
- if($max_length > 0)
+ if ($max_length > 0)
$ph->scaleImage($max_length);
$width = $ph->getWidth();
@@ -162,7 +150,7 @@ function photo_upload($channel, $observer, $args) {
$photo_hash = (($args['resource_id']) ? $args['resource_id'] : photo_new_resource());
$visitor = '';
- if($channel['channel_hash'] !== $observer['xchan_hash'])
+ if ($channel['channel_hash'] !== $observer['xchan_hash'])
$visitor = $observer['xchan_hash'];
$errors = false;
@@ -181,11 +169,10 @@ function photo_upload($channel, $observer, $args) {
if($args['description'])
$p['description'] = $args['description'];
-
$r1 = $ph->save($p);
if(! $r1)
$errors = true;
-
+
if(($width > 640 || $height > 640) && (! $errors)) {
$ph->scaleImage(640);
$p['scale'] = 1;
@@ -204,7 +191,6 @@ function photo_upload($channel, $observer, $args) {
$errors = true;
}
-
if($errors) {
q("delete from photo where resource_id = '%s' and uid = %d",
dbesc($photo_hash),
@@ -220,17 +206,31 @@ function photo_upload($channel, $observer, $args) {
$width_x_height = $ph->getWidth() . 'x' . $ph->getHeight();
- $basename = basename($filename);
$mid = item_message_id();
// Create item container
+
$item_hidden = (($visible) ? 0 : 1 );
+
+ $lat = $lon = null;
+
+ if($exif && $exif['GPS']) {
+ if(feature_enabled($channel_id,'photo_location')) {
+ $lat = getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
+ $lon = getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
+ }
+ }
+
+
$title = '';
$mid = item_message_id();
-
+
$arr = array();
+ if($lat && $lon)
+ $arr['coord'] = $lat . ' ' . $lon;
+
$arr['aid'] = $account_id;
$arr['uid'] = $channel_id;
$arr['mid'] = $mid;
@@ -249,12 +249,13 @@ function photo_upload($channel, $observer, $args) {
$arr['item_wall'] = 1;
$arr['item_origin'] = 1;
$arr['item_thread_top'] = 1;
+
$arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid'];
// We should also put a width_x_height on large photos. Left as an exercise for
// devs looking fo simple stuff to fix.
- $larger = feature_enabled($channel['channel_id'],'large_photos');
+ $larger = feature_enabled($channel['channel_id'], 'large_photos');
if($larger) {
$tag = '[zmg]';
if($r2)
@@ -269,11 +270,10 @@ function photo_upload($channel, $observer, $args) {
$tag = '[zmg]';
}
+ $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']'
+ . $tag . z_root() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/zmg]'
+ . '[/zrl]';
- $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']'
- . $tag . z_root() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/zmg]'
- . '[/zrl]';
-
$result = item_store($arr);
$item_id = $result['item_id'];
@@ -291,18 +291,26 @@ function photo_upload($channel, $observer, $args) {
return $ret;
}
-
-
-
-function photos_albums_list($channel,$observer) {
+/**
+ * @brief Returns a list with all photo albums observer is allowed to see.
+ *
+ * Returns an associative array with all albums where observer has permissions.
+ *
+ * @param array $channel
+ * @param array $observer
+ * @return bool|array false if no view_photos permission or an array
+ * * success (bool)
+ * * albums (array)
+ */
+function photos_albums_list($channel, $observer) {
$channel_id = $channel['channel_id'];
$observer_xchan = (($observer) ? $observer['xchan_hash'] : '');
- if(! perm_is_allowed($channel_id,$observer_xchan,'view_photos'))
+ if(! perm_is_allowed($channel_id, $observer_xchan, 'view_photos'))
return false;
- // FIXME - create a permissions SQL which works on arbitrary observers and channels, regardless of login or web status
+ /** @FIXME create a permissions SQL which works on arbitrary observers and channels, regardless of login or web status */
$sql_extra = permissions_sql($channel_id);
@@ -310,7 +318,6 @@ function photos_albums_list($channel,$observer) {
intval($channel_id),
intval(PHOTO_NORMAL),
intval(PHOTO_PROFILE)
-
);
// add various encodings to the array so we can just loop through and pick them out in a template
@@ -326,12 +333,13 @@ function photos_albums_list($channel,$observer) {
'total' => $album['total'],
'url' => z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album['album']),
'urlencode' => urlencode($album['album']),
- 'bin2hex' => bin2hex($album['album']));
+ 'bin2hex' => bin2hex($album['album'])
+ );
$ret['albums'][] = $entry;
}
}
- return $ret;
+ return $ret;
}
function photos_album_widget($channelx,$observer,$albums = null) {
@@ -359,11 +367,19 @@ function photos_album_widget($channelx,$observer,$albums = null) {
? t('Upload New Photos') : '')
));
}
+
return $o;
}
-
-function photos_list_photos($channel,$observer,$album = '') {
+/**
+ * @brief
+ *
+ * @param array $channel
+ * @param array $observer
+ * @param string $album default empty
+ * @return boolean|array
+ */
+function photos_list_photos($channel, $observer, $album = '') {
$channel_id = $channel['channel_id'];
$observer_xchan = (($observer) ? $observer['xchan_hash'] : '');
@@ -383,7 +399,7 @@ function photos_list_photos($channel,$observer,$album = '') {
intval(PHOTO_NORMAL),
intval(PHOTO_PROFILE)
);
-
+
if($r) {
for($x = 0; $x < count($r); $x ++) {
$r[$x]['src'] = z_root() . '/photo/' . $r[$x]['resource_id'] . '-' . $r[$x]['scale'];
@@ -395,17 +411,33 @@ function photos_list_photos($channel,$observer,$album = '') {
return $ret;
}
-
-
-function photos_album_exists($channel_id,$album) {
- $r = q("SELECT id from photo where album = '%s' and uid = %d limit 1",
+/**
+ * @brief Check if given photo album exists in channel.
+ *
+ * @param int $channel_id id of the channel
+ * @param string $album name of the album
+ * @return boolean
+ */
+function photos_album_exists($channel_id, $album) {
+ $r = q("SELECT id FROM photo WHERE album = '%s' AND uid = %d limit 1",
dbesc($album),
intval($channel_id)
);
+
return (($r) ? true : false);
}
-function photos_album_rename($channel_id,$oldname,$newname) {
+/**
+ * @brief Renames a photo album in a channel.
+ *
+ * @todo Do we need to check if new album name already exists?
+ *
+ * @param int $channel_id id of the channel
+ * @param string $oldname The name of the album to rename
+ * @param string $newname The new name of the album
+ * @return bool|array
+ */
+function photos_album_rename($channel_id, $oldname, $newname) {
return q("UPDATE photo SET album = '%s' WHERE album = '%s' AND uid = %d",
dbesc($newname),
dbesc($oldname),
@@ -413,43 +445,58 @@ function photos_album_rename($channel_id,$oldname,$newname) {
);
}
-
-function photos_album_get_db_idstr($channel_id,$album,$remote_xchan = '') {
-
- if($remote_xchan) {
+/**
+ * @brief
+ *
+ * @param int $channel_id
+ * @param string $album
+ * @param string $remote_xchan
+ * @return string|boolean
+ */
+function photos_album_get_db_idstr($channel_id, $album, $remote_xchan = '') {
+
+ if ($remote_xchan) {
$r = q("SELECT distinct resource_id as from photo where xchan = '%s' and uid = %d and album = '%s' ",
dbesc($remote_xchan),
intval($channel_id),
dbesc($album)
);
- }
- else {
+ } else {
$r = q("SELECT distinct resource_id from photo where uid = %d and album = '%s' ",
intval($channel_id),
dbesc($album)
);
}
- if($r) {
+ if ($r) {
$arr = array();
- foreach($r as $rr) {
+ foreach ($r as $rr) {
$arr[] = "'" . dbesc($rr['resource_id']) . "'" ;
}
$str = implode(',',$arr);
return $str;
}
- return false;
+ return false;
}
+/**
+ * @brief Creates a new photo item.
+ *
+ * @param array $channel
+ * @param string $creator_hash
+ * @param array $photo
+ * @param boolean $visible default false
+ * @return int item_id
+ */
function photos_create_item($channel, $creator_hash, $photo, $visible = false) {
// Create item container
+
$item_hidden = (($visible) ? 0 : 1 );
- $title = '';
$mid = item_message_id();
-
+
$arr = array();
$arr['aid'] = $channel['channel_account_id'];
@@ -475,9 +522,34 @@ function photos_create_item($channel, $creator_hash, $photo, $visible = false) {
$arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo['resource_id'] . ']'
. '[zmg]' . z_root() . '/photo/' . $photo['resource_id'] . '-' . $photo['scale'] . '[/zmg]'
. '[/zrl]';
-
+
$result = item_store($arr);
$item_id = $result['item_id'];
+
return $item_id;
+}
+
+
+function getGps($exifCoord, $hemi) {
+
+ $degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
+ $minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
+ $seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
+
+ $flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
+
+ return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
+}
+
+function gps2Num($coordPart) {
+
+ $parts = explode('/', $coordPart);
+
+ if (count($parts) <= 0)
+ return 0;
+
+ if (count($parts) == 1)
+ return $parts[0];
+ return floatval($parts[0]) / floatval($parts[1]);
}