3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\Database\DBStructure;
28 use Friendica\Object\Image;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Mimetype;
31 use Friendica\Util\Security;
34 * Class to handle attach dabatase table
40 * Return a list of fields that are associated with the attach table
42 * @return array field list
45 private static function getFields()
47 $allfields = DBStructure::definition(DI::app()->getBasePath(), false);
48 $fields = array_keys($allfields['attach']['fields']);
49 array_splice($fields, array_search('data', $fields), 1);
54 * Select rows from the attach table and return them as array
56 * @param array $fields Array of selected fields, empty for all
57 * @param array $conditions Array of fields for conditions
58 * @param array $params Array of several parameters
63 * @see \Friendica\Database\DBA::selectToArray
65 public static function selectToArray(array $fields = [], array $conditions = [], array $params = [])
68 $fields = self::getFields();
71 return DBA::selectToArray('attach', $fields, $conditions, $params);
75 * Retrieve a single record from the attach table
77 * @param array $fields Array of selected fields, empty for all
78 * @param array $conditions Array of fields for conditions
79 * @param array $params Array of several parameters
84 * @see \Friendica\Database\DBA::select
86 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
89 $fields = self::getFields();
92 return DBA::selectFirst('attach', $fields, $conditions, $params);
96 * Check if attachment with given conditions exists
98 * @param array $conditions Array of extra conditions
103 public static function exists(array $conditions)
105 return DBA::exists('attach', $conditions);
109 * Retrive a single record given the ID
111 * @param int $id Row id of the record
116 * @see \Friendica\Database\DBA::select
118 public static function getById($id)
120 return self::selectFirst([], ['id' => $id]);
124 * Retrive a single record given the ID
126 * @param int $id Row id of the record
131 * @see \Friendica\Database\DBA::select
133 public static function getByIdWithPermission($id)
135 $r = self::selectFirst(['uid'], ['id' => $id]);
140 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
143 '`id` = ?' . $sql_acl,
147 $item = self::selectFirst([], $conditions);
153 * Get file data for given row id. null if row id does not exist
155 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
157 * @return string file data
160 public static function getData($item)
162 $backendClass = DI::storageManager()->getByName($photo['backend-class'] ?? '');
163 if ($backendClass === null) {
164 // legacy data storage in 'data' column
165 $i = self::selectFirst(['data'], ['id' => $item['id']]);
171 $backendRef = $item['backend-ref'];
172 return $backendClass->get($backendRef);
177 * Store new file metadata in db and binary in default backend
179 * @param string $data Binary data
180 * @param integer $uid User ID
181 * @param string $filename Filename
182 * @param string $filetype Mimetype. optional, default = ''
183 * @param integer $filesize File size in bytes. optional, default = null
184 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
185 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
186 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
187 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
189 * @return boolean/integer Row id on success, False on errors
190 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
192 public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
194 if ($filetype === '') {
195 $filetype = Mimetype::getContentType($filename);
198 if (is_null($filesize)) {
199 $filesize = strlen($data);
202 $backend_ref = DI::storage()->put($data);
205 $hash = System::createGUID(64);
206 $created = DateTimeFormat::utcNow();
211 'filename' => $filename,
212 'filetype' => $filetype,
213 'filesize' => $filesize,
215 'created' => $created,
216 'edited' => $created,
217 'allow_cid' => $allow_cid,
218 'allow_gid' => $allow_gid,
219 'deny_cid' => $deny_cid,
220 'deny_gid' => $deny_gid,
221 'backend-class' => (string)DI::storage(),
222 'backend-ref' => $backend_ref
225 $r = DBA::insert('attach', $fields);
227 return DBA::lastInsertId();
233 * Store new file metadata in db and binary in default backend from existing file
237 * @param string $filename
238 * @param string $allow_cid
239 * @param string $allow_gid
240 * @param string $deny_cid
241 * @param string $deny_gid
242 * @return boolean True on success
243 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
245 public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
247 if ($filename === '') {
248 $filename = basename($src);
251 $data = @file_get_contents($src);
253 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
258 * Update an attached file
260 * @param array $fields Contains the fields that are updated
261 * @param array $conditions Condition array with the key values
262 * @param Image $img Image data to update. Optional, default null.
263 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
265 * @return boolean Was the update successful?
267 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
268 * @see \Friendica\Database\DBA::update
270 public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
272 if (!is_null($img)) {
273 // get items to update
274 $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
276 foreach($items as $item) {
277 $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
278 if ($backend_class !== null) {
279 $fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
281 $fields['data'] = $img->asString();
286 $fields['edited'] = DateTimeFormat::utcNow();
288 return DBA::update('attach', $fields, $conditions, $old_fields);
293 * Delete info from table and data from storage
295 * @param array $conditions Field condition(s)
296 * @param array $options Options array, Optional
301 * @see \Friendica\Database\DBA::delete
303 public static function delete(array $conditions, array $options = [])
305 // get items to delete data info
306 $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
308 foreach($items as $item) {
309 $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
310 if ($backend_class !== null) {
311 $backend_class->delete($item['backend-ref'] ?? '');
315 return DBA::delete('attach', $conditions, $options);