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