]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Attach.php
Forums now are working with AP as well
[friendica.git] / src / Model / Attach.php
index 2911136188d53b9ab2a492ea33f74c6690c3f817..d65e67fe3e6a8c34adec09eb7ffca2091f8e7f2c 100644 (file)
@@ -8,11 +8,15 @@ namespace Friendica\Model;
 
 use Friendica\BaseObject;
 use Friendica\Core\StorageManager;
+use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
+use Friendica\Model\Storage\IStorage;
+use Friendica\Object\Image;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Mimetype;
 use Friendica\Util\Security;
 
-
 /**
  * Class to handle attach dabatase table
  */
@@ -23,10 +27,11 @@ class Attach extends BaseObject
         * @brief Return a list of fields that are associated with the attach table
         *
         * @return array field list
+        * @throws \Exception
         */
        private static function getFields()
        {
-               $allfields = DBStructure::definition(false);
+               $allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
                $fields = array_keys($allfields['attach']['fields']);
                array_splice($fields, array_search('data', $fields), 1);
                return $fields;
@@ -35,18 +40,19 @@ class Attach extends BaseObject
        /**
         * @brief Select rows from the attach table
         *
-        * @param array  $fields     Array of selected fields, empty for all
-        * @param array  $conditions Array of fields for conditions
-        * @param array  $params     Array of several parameters
+        * @param array $fields     Array of selected fields, empty for all
+        * @param array $conditions Array of fields for conditions
+        * @param array $params     Array of several parameters
         *
         * @return boolean|array
         *
-        * @see \Friendica\Database\DBA::select
+        * @throws \Exception
+        * @see   \Friendica\Database\DBA::select
         */
        public static function select(array $fields = [], array $conditions = [], array $params = [])
        {
                if (empty($fields)) {
-                       $selected = self::getFields();
+                       $fields = self::getFields();
                }
 
                $r = DBA::select('attach', $fields, $conditions, $params);
@@ -56,13 +62,14 @@ class Attach extends BaseObject
        /**
         * @brief Retrieve a single record from the attach table
         *
-        * @param array  $fields     Array of selected fields, empty for all
-        * @param array  $conditions Array of fields for conditions
-        * @param array  $params     Array of several parameters
+        * @param array $fields     Array of selected fields, empty for all
+        * @param array $conditions Array of fields for conditions
+        * @param array $params     Array of several parameters
         *
         * @return bool|array
         *
-        * @see \Friendica\Database\DBA::select
+        * @throws \Exception
+        * @see   \Friendica\Database\DBA::select
         */
        public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
        {
@@ -76,9 +83,10 @@ class Attach extends BaseObject
        /**
         * @brief Check if attachment with given conditions exists
         *
-        * @param array   $conditions  Array of extra conditions
+        * @param array $conditions Array of extra conditions
         *
         * @return boolean
+        * @throws \Exception
         */
        public static function exists(array $conditions)
        {
@@ -87,12 +95,13 @@ class Attach extends BaseObject
 
        /**
         * @brief Retrive a single record given the ID
-        * 
-        * @param int  $id  Row id of the record
-        * 
+        *
+        * @param int $id Row id of the record
+        *
         * @return bool|array
         *
-        * @see \Friendica\Database\DBA::select
+        * @throws \Exception
+        * @see   \Friendica\Database\DBA::select
         */
        public static function getById($id)
        {
@@ -100,13 +109,14 @@ class Attach extends BaseObject
        }
 
        /**
-        * @brief Retrive a single record given the ID 
-        * 
-        * @param int  $id  Row id of the record
-        * 
+        * @brief Retrive a single record given the ID
+        *
+        * @param int $id Row id of the record
+        *
         * @return bool|array
         *
-        * @see \Friendica\Database\DBA::select
+        * @throws \Exception
+        * @see   \Friendica\Database\DBA::select
         */
        public static function getByIdWithPermission($id)
        {
@@ -129,10 +139,11 @@ class Attach extends BaseObject
 
        /**
         * @brief Get file data for given row id. null if row id does not exist
-        * 
-        * @param array  $item  Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
-        * 
+        *
+        * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
+        *
         * @return string  file data
+        * @throws \Exception
         */
        public static function getData($item)
        {
@@ -153,28 +164,30 @@ class Attach extends BaseObject
        /**
         * @brief Store new file metadata in db and binary in default backend
         *
-        * @param string  $data  Binary data
+        * @param string  $data      Binary data
         * @param integer $uid       User ID
         * @param string  $filename  Filename
-        * @param string  $filetype  Mimetype. optional
-        * @param integer $filesize  File size in bytes. optional
+        * @param string  $filetype  Mimetype. optional, default = ''
+        * @param integer $filesize  File size in bytes. optional, default = null
         * @param string  $allow_cid Permissions, allowed contacts. optional, default = ''
         * @param string  $allow_gid Permissions, allowed groups. optional, default = ''
         * @param string  $deny_cid  Permissions, denied contacts.optional, default = ''
         * @param string  $deny_gid  Permissions, denied greoup.optional, default = ''
         *
         * @return boolean/integer Row id on success, False on errors
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function store($data, $uid, $filename, $filetype = '' , $filesize = -1, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
+       public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
        {
                if ($filetype === '') {
                        $filetype = Mimetype::getContentType($filename);
                }
 
-               if ($filesize < 0) {
+               if (is_null($filesize)) {
                        $filesize = strlen($data);
                }
 
+               /** @var IStorage $backend_class */
                $backend_class = StorageManager::getBackend();
                $backend_ref = '';
                if ($backend_class !== '') {
@@ -206,14 +219,23 @@ class Attach extends BaseObject
                if ($r === true) {
                        return DBA::lastInsertId();
                }
+               return $r;
        }
 
        /**
         * @brief Store new file metadata in db and binary in default backend from existing file
         *
+        * @param        $src
+        * @param        $uid
+        * @param string $filename
+        * @param string $allow_cid
+        * @param string $allow_gid
+        * @param string $deny_cid
+        * @param string $deny_gid
         * @return boolean True on success
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
+       public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
        {
                if ($filename === '') {
                        $filename = basename($src);
@@ -221,7 +243,7 @@ class Attach extends BaseObject
 
                $data = @file_get_contents($src);
 
-               return  self::store($data, $uid, $filename, '', '', $allow_cid, $allow_gid,  $deny_cid, $deny_gid);
+               return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid,  $deny_cid, $deny_gid);
        }
 
 
@@ -230,25 +252,27 @@ class Attach extends BaseObject
         *
         * @param array         $fields     Contains the fields that are updated
         * @param array         $conditions Condition array with the key values
-        * @param string        $data       File data to update. Optional, default null.
+        * @param Image         $img        Image data to update. Optional, default null.
         * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
         *
-        * @return boolean  Was the update successfull?
+        * @return boolean  Was the update successful?
         *
-        * @see \Friendica\Database\DBA::update
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @see   \Friendica\Database\DBA::update
         */
-       public static function update($fields, $conditions, $img = null, array $old_fields = [])
+       public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
        {
-               if (!is_null($data)) {
+               if (!is_null($img)) {
                        // get items to update
                        $items = self::select(['backend-class','backend-ref'], $conditions);
 
                        foreach($items as $item) {
+                               /** @var IStorage $backend_class */
                                $backend_class = (string)$item['backend-class'];
                                if ($backend_class !== '') {
                                        $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
                                } else {
-                                       $fields['data'] = $data;
+                                       $fields['data'] = $img->asString();
                                }
                        }
                }
@@ -262,12 +286,13 @@ class Attach extends BaseObject
        /**
         * @brief Delete info from table and data from storage
         *
-        * @param array  $conditions  Field condition(s)
-        * @param array  $options     Options array, Optional
+        * @param array $conditions Field condition(s)
+        * @param array $options    Options array, Optional
         *
         * @return boolean
         *
-        * @see \Friendica\Database\DBA::delete
+        * @throws \Exception
+        * @see   \Friendica\Database\DBA::delete
         */
        public static function delete(array $conditions, array $options = [])
        {
@@ -275,6 +300,7 @@ class Attach extends BaseObject
                $items = self::select(['backend-class','backend-ref'], $conditions);
 
                foreach($items as $item) {
+                       /** @var IStorage $backend_class */
                        $backend_class = (string)$item['backend-class'];
                        if ($backend_class !== '') {
                                $backend_class::delete($item['backend-ref']);