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