]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Documentation
[friendica.git] / src / Model / Photo.php
index ef59eee7e9aa932ce09808c40273521d2f992e37..a9b4c0fb736fd139957518921de46f03cac308d5 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * @file src/Model/Photo.php
  * @brief This file contains the Photo class for database interface
@@ -7,11 +8,13 @@ namespace Friendica\Model;
 
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
-use Friendica\Core\PConfig;
+use Friendica\Core\L10n;
 use Friendica\Core\System;
-use Friendica\Database\DBM;
+use Friendica\Database\DBA;
 use Friendica\Object\Image;
-use dba;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
+use Friendica\Util\Security;
 
 require_once 'include/dba.php';
 
@@ -38,22 +41,22 @@ class Photo
         */
        public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
        {
-               $photo = dba::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
-               if (DBM::is_result($photo)) {
+               $photo = DBA::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
+               if (DBA::isResult($photo)) {
                        $guid = $photo['guid'];
                } else {
-                       $guid = get_guid();
+                       $guid = System::createGUID();
                }
 
-               $existing_photo = dba::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
+               $existing_photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
 
                $fields = [
                        'uid' => $uid,
                        'contact-id' => $cid,
                        'guid' => $guid,
                        'resource-id' => $rid,
-                       'created' => datetime_convert(),
-                       'edited' => datetime_convert(),
+                       'created' => DateTimeFormat::utcNow(),
+                       'edited' => DateTimeFormat::utcNow(),
                        'filename' => basename($filename),
                        'type' => $Image->getType(),
                        'album' => $album,
@@ -70,10 +73,10 @@ class Photo
                        'desc' => $desc
                ];
 
-               if (DBM::is_result($existing_photo)) {
-                       $r = dba::update('photo', $fields, ['id' => $existing_photo['id']]);
+               if (DBA::isResult($existing_photo)) {
+                       $r = DBA::update('photo', $fields, ['id' => $existing_photo['id']]);
                } else {
-                       $r = dba::insert('photo', $fields);
+                       $r = DBA::insert('photo', $fields);
                }
 
                return $r;
@@ -88,19 +91,22 @@ class Photo
         */
        public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
        {
-               $photo = dba::selectFirst(
+               $thumb = '';
+               $micro = '';
+
+               $photo = DBA::selectFirst(
                        'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
                );
                if (x($photo['resource-id'])) {
                        $hash = $photo['resource-id'];
                } else {
-                       $hash = photo_new_resource();
+                       $hash = self::newResource();
                }
 
                $photo_failure = false;
 
                $filename = basename($image_url);
-               $img_str = fetch_url($image_url, true);
+               $img_str = Network::fetchUrl($image_url, true);
 
                if ($quit_on_error && ($img_str == "")) {
                        return false;
@@ -109,7 +115,7 @@ class Photo
                $type = Image::guessType($image_url, true);
                $Image = new Image($img_str, $type);
                if ($Image->isValid()) {
-                       $Image->scaleToSquare(175);
+                       $Image->scaleToSquare(300);
 
                        $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
 
@@ -141,7 +147,7 @@ class Photo
 
                        // Remove the cached photo
                        $a = get_app();
-                       $basepath = $a->get_basepath();
+                       $basepath = $a->getBasePath();
 
                        if (is_dir($basepath . "/photo")) {
                                $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
@@ -166,7 +172,7 @@ class Photo
                }
 
                if ($photo_failure) {
-                       $image_url = System::baseUrl() . '/images/person-175.jpg';
+                       $image_url = System::baseUrl() . '/images/person-300.jpg';
                        $thumb = System::baseUrl() . '/images/person-80.jpg';
                        $micro = System::baseUrl() . '/images/person-48.jpg';
                }
@@ -221,7 +227,7 @@ class Photo
         */
        public static function getAlbums($uid, $update = false)
        {
-               $sql_extra = permissions_sql($uid);
+               $sql_extra = Security::getPermissionsSQLByUserId($uid);
 
                $key = "photo_albums:".$uid.":".local_user().":".remote_user();
                $albums = Cache::get($key);
@@ -234,8 +240,8 @@ class Photo
                                        WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
                                        GROUP BY `album` ORDER BY `created` DESC",
                                        intval($uid),
-                                       dbesc('Contact Photos'),
-                                       dbesc(t('Contact Photos'))
+                                       DBA::escape('Contact Photos'),
+                                       DBA::escape(L10n::t('Contact Photos'))
                                );
                        } else {
                                // This query doesn't do the count and is much faster
@@ -243,11 +249,11 @@ class Photo
                                        FROM `photo` USE INDEX (`uid_album_scale_created`)
                                        WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra",
                                        intval($uid),
-                                       dbesc('Contact Photos'),
-                                       dbesc(t('Contact Photos'))
+                                       DBA::escape('Contact Photos'),
+                                       DBA::escape(L10n::t('Contact Photos'))
                                );
                        }
-                       Cache::set($key, $albums, CACHE_DAY);
+                       Cache::set($key, $albums, Cache::DAY);
                }
                return $albums;
        }
@@ -259,6 +265,16 @@ class Photo
        public static function clearAlbumCache($uid)
        {
                $key = "photo_albums:".$uid.":".local_user().":".remote_user();
-               Cache::set($key, null, CACHE_DAY);
+               Cache::set($key, null, Cache::DAY);
+       }
+
+       /**
+        * Generate a unique photo ID.
+        *
+        * @return string
+        */
+       public static function newResource()
+       {
+               return system::createGUID(32, false);
        }
 }