]> git.mxchange.org Git - friendica.git/blob - src/Model/Attach.php
Fix PHPDoc comments project-wide
[friendica.git] / src / Model / Attach.php
1 <?php
2
3 /**
4  * @file src/Model/Attach.php
5  * @brief This file contains the Attach class for database interface
6  */
7 namespace Friendica\Model;
8
9 use Friendica\BaseObject;
10 use Friendica\Core\System;
11 use Friendica\Core\StorageManager;
12 use Friendica\Database\DBA;
13 use Friendica\Database\DBStructure;
14 use Friendica\Util\Security;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Mimetype;
17
18 /**
19  * Class to handle attach dabatase table
20  */
21 class Attach extends BaseObject
22 {
23
24         /**
25          * @brief Return a list of fields that are associated with the attach table
26          *
27          * @return array field list
28          * @throws \Exception
29          */
30         private static function getFields()
31         {
32                 $allfields = DBStructure::definition(false);
33                 $fields = array_keys($allfields['attach']['fields']);
34                 array_splice($fields, array_search('data', $fields), 1);
35                 return $fields;
36         }
37
38         /**
39          * @brief Select rows from the attach table
40          *
41          * @param array $fields     Array of selected fields, empty for all
42          * @param array $conditions Array of fields for conditions
43          * @param array $params     Array of several parameters
44          *
45          * @return boolean|array
46          *
47          * @throws \Exception
48          * @see   \Friendica\Database\DBA::select
49          */
50         public static function select(array $fields = [], array $conditions = [], array $params = [])
51         {
52                 if (empty($fields)) {
53                         $selected = self::getFields();
54                 }
55
56                 $r = DBA::select('attach', $fields, $conditions, $params);
57                 return DBA::toArray($r);
58         }
59
60         /**
61          * @brief Retrieve a single record from the attach table
62          *
63          * @param array $fields     Array of selected fields, empty for all
64          * @param array $conditions Array of fields for conditions
65          * @param array $params     Array of several parameters
66          *
67          * @return bool|array
68          *
69          * @throws \Exception
70          * @see   \Friendica\Database\DBA::select
71          */
72         public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
73         {
74                 if (empty($fields)) {
75                         $fields = self::getFields();
76                 }
77
78                 return DBA::selectFirst('attach', $fields, $conditions, $params);
79         }
80
81         /**
82          * @brief Check if attachment with given conditions exists
83          *
84          * @param array $conditions Array of extra conditions
85          *
86          * @return boolean
87          * @throws \Exception
88          */
89         public static function exists(array $conditions)
90         {
91                 return DBA::exists('attach', $conditions);
92         }
93
94         /**
95          * @brief Retrive a single record given the ID
96          *
97          * @param int $id Row id of the record
98          *
99          * @return bool|array
100          *
101          * @throws \Exception
102          * @see   \Friendica\Database\DBA::select
103          */
104         public static function getById($id)
105         {
106                 return self::selectFirst([], ['id' => $id]);
107         }
108
109         /**
110          * @brief Retrive a single record given the ID
111          *
112          * @param int $id Row id of the record
113          *
114          * @return bool|array
115          *
116          * @throws \Exception
117          * @see   \Friendica\Database\DBA::select
118          */
119         public static function getByIdWithPermission($id)
120         {
121                 $r = self::selectFirst(['uid'], ['id' => $id]);
122                 if ($r === false) {
123                         return false;
124                 }
125
126                 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
127
128                 $conditions = [
129                         '`id` = ?' . $sql_acl,
130                         $id
131                 ];
132
133                 $item = self::selectFirst([], $conditions);
134
135                 return $item;
136         }
137
138         /**
139          * @brief Get file data for given row id. null if row id does not exist
140          *
141          * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
142          *
143          * @return string  file data
144          * @throws \Exception
145          */
146         public static function getData($item)
147         {
148                 if ($item['backend-class'] == '') {
149                         // legacy data storage in 'data' column
150                         $i = self::selectFirst(['data'], ['id' => $item['id']]);
151                         if ($i === false) {
152                                 return null;
153                         }
154                         return $i['data'];
155                 } else {
156                         $backendClass = $item['backend-class'];
157                         $backendRef = $item['backend-ref'];
158                         return $backendClass::get($backendRef);
159                 }
160         }
161
162         /**
163          * @brief Store new file metadata in db and binary in default backend
164          *
165          * @param string  $data      Binary data
166          * @param integer $uid       User ID
167          * @param string  $filename  Filename
168          * @param string  $filetype  Mimetype. optional, default = ''
169          * @param integer $filesize  File size in bytes. optional, default = null
170          * @param string  $allow_cid Permissions, allowed contacts. optional, default = ''
171          * @param string  $allow_gid Permissions, allowed groups. optional, default = ''
172          * @param string  $deny_cid  Permissions, denied contacts.optional, default = ''
173          * @param string  $deny_gid  Permissions, denied greoup.optional, default = ''
174          *
175          * @return boolean/integer Row id on success, False on errors
176          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177          */
178         public function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
179         {
180                 if ($filetype === '') {
181                         $filetype = Mimetype::getContentType($filename);
182                 }
183
184                 if (is_null($filesize)) {
185                         $filesize = strlen($data);
186                 }
187
188                 $backend_class = StorageManager::getBackend();
189                 $backend_ref = '';
190                 if ($backend_class !== '') {
191                         $backend_ref = $backend_class::put($data);
192                         $data = '';
193                 }
194
195                 $hash = System::createGUID(64);
196                 $created = DateTimeFormat::utcNow();
197
198                 $fields = [
199                         'uid' => $uid,
200                         'hash' => $hash,
201                         'filename' => $filename,
202                         'filetype' => $filetype,
203                         'filesize' => $filesize,
204                         'data' => $data,
205                         'created' => $created,
206                         'edited' => $created,
207                         'allow_cid' => $allow_cid,
208                         'allow_gid' => $allow_gid,
209                         'deny_cid' => $deny_cid,
210                         'deny_gid' => $deny_gid,
211                         'backend-class' => $backend_class,
212                         'backend-ref' => $backend_ref
213                 ];
214
215                 $r = DBA::insert('attach', $fields);
216                 if ($r === true) {
217                         return DBA::lastInsertId();
218                 }
219                 return $r;
220         }
221
222         /**
223          * @brief Store new file metadata in db and binary in default backend from existing file
224          *
225          * @param        $src
226          * @param        $uid
227          * @param string $filename
228          * @param string $allow_cid
229          * @param string $allow_gid
230          * @param string $deny_cid
231          * @param string $deny_gid
232          * @return boolean True on success
233          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
234          */
235         public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
236         {
237                 if ($filename === '') {
238                         $filename = basename($src);
239                 }
240
241                 $data = @file_get_contents($src);
242
243                 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid,  $deny_cid, $deny_gid);
244         }
245
246
247         /**
248          * @brief Update an attached file
249          *
250          * @param array         $fields     Contains the fields that are updated
251          * @param array         $conditions Condition array with the key values
252          * @param Image         $img        Image data to update. Optional, default null.
253          * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
254          *
255          * @return boolean  Was the update successful?
256          *
257          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
258          * @see   \Friendica\Database\DBA::update
259          */
260         public static function update($fields, $conditions, $img = null, array $old_fields = [])
261         {
262                 if (!is_null($data)) {
263                         // get items to update
264                         $items = self::select(['backend-class','backend-ref'], $conditions);
265
266                         foreach($items as $item) {
267                                 $backend_class = (string)$item['backend-class'];
268                                 if ($backend_class !== '') {
269                                         $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
270                                 } else {
271                                         $fields['data'] = $data;
272                                 }
273                         }
274                 }
275
276                 $fields['edited'] = DateTimeFormat::utcNow();
277
278                 return DBA::update('attach', $fields, $conditions, $old_fields);
279         }
280
281
282         /**
283          * @brief Delete info from table and data from storage
284          *
285          * @param array $conditions Field condition(s)
286          * @param array $options    Options array, Optional
287          *
288          * @return boolean
289          *
290          * @throws \Exception
291          * @see   \Friendica\Database\DBA::delete
292          */
293         public static function delete(array $conditions, array $options = [])
294         {
295                 // get items to delete data info
296                 $items = self::select(['backend-class','backend-ref'], $conditions);
297
298                 foreach($items as $item) {
299                         $backend_class = (string)$item['backend-class'];
300                         if ($backend_class !== '') {
301                                 $backend_class::delete($item['backend-ref']);
302                         }
303                 }
304
305                 return DBA::delete('attach', $conditions, $options);
306         }
307 }