]> git.mxchange.org Git - friendica.git/blobdiff - include/Photo.php
Issue 3857: There is the possibility of a bad handling of dislikes
[friendica.git] / include / Photo.php
index 1a97fe2fe47b1d31f95ae0abc2acd5fafcc5c918..343082d1dfa1c8978cc4a6e557ac4f0dc9247b44 100644 (file)
@@ -4,6 +4,9 @@
  * @brief This file contains the Photo class for image processing
  */
 
+use Friendica\App;
+use Friendica\Core\System;
+
 require_once("include/photos.php");
 
 class Photo {
@@ -23,7 +26,7 @@ class Photo {
        /**
         * @brief supported mimetypes and corresponding file extensions
         */
-       static function supportedTypes() {
+       public static function supportedTypes() {
                if (class_exists('Imagick')) {
 
                        // Imagick::queryFormats won't help us a lot there...
@@ -46,7 +49,7 @@ class Photo {
 
        public function __construct($data, $type=null) {
                $this->imagick = class_exists('Imagick');
-               $this->types = $this->supportedTypes();
+               $this->types = static::supportedTypes();
                if (!array_key_exists($type, $this->types)){
                        $type='image/jpeg';
                }
@@ -68,7 +71,9 @@ class Photo {
                                $this->image->destroy();
                                return;
                        }
-                       imagedestroy($this->image);
+                       if (is_resource($this->image)) {
+                               imagedestroy($this->image);
+                       }
                }
        }
 
@@ -283,7 +288,7 @@ class Photo {
                        do {
 
                                // FIXME - implement horizantal bias for scaling as in followin GD functions
-                               // to allow very tall images to be constrained only horizontally. 
+                               // to allow very tall images to be constrained only horizontally.
 
                                $this->image->scaleImage($dest_width, $dest_height);
                        } while ($this->image->nextImage());
@@ -324,6 +329,7 @@ class Photo {
                        return;
                }
 
+               // if script dies at this point check memory_limit setting in php.ini
                $this->image  = imagerotate($this->image,$degrees,0);
                $this->width  = imagesx($this->image);
                $this->height = imagesy($this->image);
@@ -620,91 +626,26 @@ class Photo {
 
 
 
-       public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
+       public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '') {
 
-               $r = q("SELECT `guid` FROM `photo` WHERE `resource-id` = '%s' AND `guid` != '' LIMIT 1",
-                       dbesc($rid)
-               );
+               $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
                if (dbm::is_result($r)) {
-                       $guid = $r[0]['guid'];
+                       $guid = $r['guid'];
                } else {
                        $guid = get_guid();
                }
 
-               $x = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `contact-id` = %d AND `scale` = %d LIMIT 1",
-                       dbesc($rid),
-                       intval($uid),
-                       intval($cid),
-                       intval($scale)
-               );
+               $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
+
+               $fields = array('uid' => $uid, 'contact-id' => $cid, 'guid' => $guid, 'resource-id' => $rid, 'created' => datetime_convert(), 'edited' => datetime_convert(),
+                               'filename' => basename($filename), 'type' => $this->getType(), 'album' => $album, 'height' => $this->getHeight(), 'width' => $this->getWidth(),
+                               'datasize' => strlen($this->imageString()), 'data' => $this->imageString(), 'scale' => $scale, 'profile' => $profile,
+                               'allow_cid' => $allow_cid, 'allow_gid' => $allow_gid, 'deny_cid' => $deny_cid, 'deny_gid' => $deny_gid, 'desc' => $desc);
+
                if (dbm::is_result($x)) {
-                       $r = q("UPDATE `photo`
-                               SET `uid` = %d,
-                               `contact-id` = %d,
-                               `guid` = '%s',
-                               `resource-id` = '%s',
-                               `created` = '%s',
-                               `edited` = '%s',
-                               `filename` = '%s',
-                               `type` = '%s',
-                               `album` = '%s',
-                               `height` = %d,
-                               `width` = %d,
-                               `datasize` = %d,
-                               `data` = '%s',
-                               `scale` = %d,
-                               `profile` = %d,
-                               `allow_cid` = '%s',
-                               `allow_gid` = '%s',
-                               `deny_cid` = '%s',
-                               `deny_gid` = '%s'
-                               WHERE `id` = %d",
-
-                               intval($uid),
-                               intval($cid),
-                               dbesc($guid),
-                               dbesc($rid),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
-                               dbesc(basename($filename)),
-                               dbesc($this->getType()),
-                               dbesc($album),
-                               intval($this->getHeight()),
-                               intval($this->getWidth()),
-                               dbesc(strlen($this->imageString())),
-                               dbesc($this->imageString()),
-                               intval($scale),
-                               intval($profile),
-                               dbesc($allow_cid),
-                               dbesc($allow_gid),
-                               dbesc($deny_cid),
-                               dbesc($deny_gid),
-                               intval($x[0]['id'])
-                       );
+                       $r = dba::update('photo', $fields, array('id' => $x['id']));
                } else {
-                       $r = q("INSERT INTO `photo`
-                               (`uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `datasize`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
-                               VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s')",
-                               intval($uid),
-                               intval($cid),
-                               dbesc($guid),
-                               dbesc($rid),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
-                               dbesc(basename($filename)),
-                               dbesc($this->getType()),
-                               dbesc($album),
-                               intval($this->getHeight()),
-                               intval($this->getWidth()),
-                               dbesc(strlen($this->imageString())),
-                               dbesc($this->imageString()),
-                               intval($scale),
-                               intval($profile),
-                               dbesc($allow_cid),
-                               dbesc($allow_gid),
-                               dbesc($deny_cid),
-                               dbesc($deny_gid)
-                       );
+                       $r = dba::insert('photo', $fields);
                }
 
                return $r;
@@ -770,21 +711,29 @@ function guess_image_type($filename, $fromcurl=false) {
  * @return array Returns array of the different avatar sizes
  */
 function update_contact_avatar($avatar, $uid, $cid, $force = false) {
-
-       $r = q("SELECT `avatar`, `photo`, `thumb`, `micro` FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid));
+       $r = q("SELECT `avatar`, `photo`, `thumb`, `micro`, `nurl` FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid));
        if (!dbm::is_result($r)) {
                return false;
        } else {
                $data = array($r[0]["photo"], $r[0]["thumb"], $r[0]["micro"]);
        }
 
-       if (($r[0]["avatar"] != $avatar) OR $force) {
+       if (($r[0]["avatar"] != $avatar) || $force) {
                $photos = import_profile_photo($avatar, $uid, $cid, true);
 
                if ($photos) {
                        q("UPDATE `contact` SET `avatar` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
                                dbesc($avatar), dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]),
                                dbesc(datetime_convert()), intval($cid));
+
+                       // Update the public contact (contact id = 0)
+                       if ($uid != 0) {
+                               $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
+                               if (dbm::is_result($pcontact)) {
+                                       update_contact_avatar($avatar, 0, $pcontact['id'], $force);
+                               }
+                       }
+
                        return $photos;
                }
        }
@@ -809,7 +758,7 @@ function import_profile_photo($photo, $uid, $cid, $quit_on_error = false) {
        $filename = basename($photo);
        $img_str = fetch_url($photo, true);
 
-       if ($quit_on_error AND ($img_str == "")) {
+       if ($quit_on_error && ($img_str == "")) {
                return false;
        }
 
@@ -839,21 +788,42 @@ function import_profile_photo($photo, $uid, $cid, $quit_on_error = false) {
                        $photo_failure = true;
                }
 
-               $photo = App::get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
-               $thumb = App::get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
-               $micro = App::get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
+               $suffix = '?ts='.time();
+
+               $photo = System::baseUrl() . '/photo/' . $hash . '-4.' . $img->getExt() . $suffix;
+               $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $img->getExt() . $suffix;
+               $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $img->getExt() . $suffix;
+
+               // Remove the cached photo
+               $a = get_app();
+               $basepath = $a->get_basepath();
+
+               if (is_dir($basepath."/photo")) {
+                       $filename = $basepath.'/photo/'.$hash.'-4.'.$img->getExt();
+                       if (file_exists($filename)) {
+                               unlink($filename);
+                       }
+                       $filename = $basepath.'/photo/'.$hash.'-5.'.$img->getExt();
+                       if (file_exists($filename)) {
+                               unlink($filename);
+                       }
+                       $filename = $basepath.'/photo/'.$hash.'-6.'.$img->getExt();
+                       if (file_exists($filename)) {
+                               unlink($filename);
+                       }
+               }
        } else {
                $photo_failure = true;
        }
 
-       if ($photo_failure AND $quit_on_error) {
+       if ($photo_failure && $quit_on_error) {
                return false;
        }
 
        if ($photo_failure) {
-               $photo = App::get_baseurl() . '/images/person-175.jpg';
-               $thumb = App::get_baseurl() . '/images/person-80.jpg';
-               $micro = App::get_baseurl() . '/images/person-48.jpg';
+               $photo = System::baseUrl() . '/images/person-175.jpg';
+               $thumb = System::baseUrl() . '/images/person-80.jpg';
+               $micro = System::baseUrl() . '/images/person-48.jpg';
        }
 
        return(array($photo,$thumb,$micro));
@@ -865,7 +835,7 @@ function get_photo_info($url) {
 
        $data = Cache::get($url);
 
-       if (is_null($data) OR !$data OR !is_array($data)) {
+       if (is_null($data) || !$data || !is_array($data)) {
                $img_str = fetch_url($url, true, $redirects, 4);
                $filesize = strlen($img_str);
 
@@ -943,7 +913,7 @@ function scale_image($width, $height, $max) {
        return array("width" => $dest_width, "height" => $dest_height);
 }
 
-function store_photo($a, $uid, $imagedata = "", $url = "") {
+function store_photo(App $a, $uid, $imagedata = "", $url = "") {
        $r = q("SELECT `user`.`nickname`, `user`.`page-flags`, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
                WHERE `user`.`uid` = %d AND `user`.`blocked` = 0 AND `contact`.`self` = 1 LIMIT 1",
                intval($uid));
@@ -959,7 +929,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
        /// $default_cid      = $r[0]['id'];
        /// $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
 
-       if ((strlen($imagedata) == 0) AND ($url == "")) {
+       if ((strlen($imagedata) == 0) && ($url == "")) {
                logger("No image data and no url provided", LOGGER_DEBUG);
                return(array());
        } elseif (strlen($imagedata) == 0) {
@@ -977,19 +947,6 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
                return(array());
        }
 
-/*
-       $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
-               intval($uid)
-       );
-
-       $limit = service_class_fetch($uid,'photo_upload_limit');
-
-       if (($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
-               logger("Image exceeds personal limit of uid ".$uid, LOGGER_DEBUG);
-               return(array());
-       }
-*/
-
        $tempfile = tempnam(get_temppath(), "cache");
 
        $stamp1 = microtime(true);
@@ -1042,18 +999,18 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
                return(array());
        }
 
-       $image = array("page" => App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash,
-                       "full" => App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt());
+       $image = array("page" => System::baseUrl().'/photos/'.$page_owner_nick.'/image/'.$hash,
+                       "full" => System::baseUrl()."/photo/{$hash}-0.".$ph->getExt());
 
        if ($width > 800 || $height > 800) {
-               $image["large"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
+               $image["large"] = System::baseUrl()."/photo/{$hash}-0.".$ph->getExt();
        }
 
        if ($width > 640 || $height > 640) {
                $ph->scaleImage(640);
                $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 1, 0, $defperm);
                if ($r) {
-                       $image["medium"] = App::get_baseurl()."/photo/{$hash}-1.".$ph->getExt();
+                       $image["medium"] = System::baseUrl()."/photo/{$hash}-1.".$ph->getExt();
                }
        }
 
@@ -1061,11 +1018,11 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
                $ph->scaleImage(320);
                $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 2, 0, $defperm);
                if ($r) {
-                       $image["small"] = App::get_baseurl()."/photo/{$hash}-2.".$ph->getExt();
+                       $image["small"] = System::baseUrl()."/photo/{$hash}-2.".$ph->getExt();
                }
        }
 
-       if ($width > 160 AND $height > 160) {
+       if ($width > 160 && $height > 160) {
                $x = 0;
                $y = 0;
 
@@ -1086,7 +1043,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
 
                $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 3, 0, $defperm);
                if ($r) {
-                       $image["thumb"] = App::get_baseurl()."/photo/{$hash}-3.".$ph->getExt();
+                       $image["thumb"] = System::baseUrl()."/photo/{$hash}-3.".$ph->getExt();
                }
        }