4 * @file src/Model/Attach.php
5 * This file contains the Attach class for database interface
7 namespace Friendica\Model;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Database\DBStructure;
13 use Friendica\Object\Image;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Util\Mimetype;
16 use Friendica\Util\Security;
19 * Class to handle attach dabatase table
25 * Return a list of fields that are associated with the attach table
27 * @return array field list
30 private static function getFields()
32 $allfields = DBStructure::definition(DI::app()->getBasePath(), false);
33 $fields = array_keys($allfields['attach']['fields']);
34 array_splice($fields, array_search('data', $fields), 1);
39 * Select rows from the attach table and return them as array
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
48 * @see \Friendica\Database\DBA::selectToArray
50 public static function selectToArray(array $fields = [], array $conditions = [], array $params = [])
53 $fields = self::getFields();
56 return DBA::selectToArray('attach', $fields, $conditions, $params);
60 * Retrieve a single record from the attach table
62 * @param array $fields Array of selected fields, empty for all
63 * @param array $conditions Array of fields for conditions
64 * @param array $params Array of several parameters
69 * @see \Friendica\Database\DBA::select
71 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
74 $fields = self::getFields();
77 return DBA::selectFirst('attach', $fields, $conditions, $params);
81 * Check if attachment with given conditions exists
83 * @param array $conditions Array of extra conditions
88 public static function exists(array $conditions)
90 return DBA::exists('attach', $conditions);
94 * Retrive a single record given the ID
96 * @param int $id Row id of the record
101 * @see \Friendica\Database\DBA::select
103 public static function getById($id)
105 return self::selectFirst([], ['id' => $id]);
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 getByIdWithPermission($id)
120 $r = self::selectFirst(['uid'], ['id' => $id]);
125 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
128 '`id` = ?' . $sql_acl,
132 $item = self::selectFirst([], $conditions);
138 * Get file data for given row id. null if row id does not exist
140 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
142 * @return string file data
145 public static function getData($item)
147 $backendClass = DI::storageManager()->getByName($photo['backend-class'] ?? '');
148 if ($backendClass === null) {
149 // legacy data storage in 'data' column
150 $i = self::selectFirst(['data'], ['id' => $item['id']]);
156 $backendRef = $item['backend-ref'];
157 return $backendClass->get($backendRef);
162 * Store new file metadata in db and binary in default backend
164 * @param string $data Binary data
165 * @param integer $uid User ID
166 * @param string $filename Filename
167 * @param string $filetype Mimetype. optional, default = ''
168 * @param integer $filesize File size in bytes. optional, default = null
169 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
170 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
171 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
172 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
174 * @return boolean/integer Row id on success, False on errors
175 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177 public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
179 if ($filetype === '') {
180 $filetype = Mimetype::getContentType($filename);
183 if (is_null($filesize)) {
184 $filesize = strlen($data);
187 $backend_ref = DI::storage()->put($data);
190 $hash = System::createGUID(64);
191 $created = DateTimeFormat::utcNow();
196 'filename' => $filename,
197 'filetype' => $filetype,
198 'filesize' => $filesize,
200 'created' => $created,
201 'edited' => $created,
202 'allow_cid' => $allow_cid,
203 'allow_gid' => $allow_gid,
204 'deny_cid' => $deny_cid,
205 'deny_gid' => $deny_gid,
206 'backend-class' => (string)DI::storage(),
207 'backend-ref' => $backend_ref
210 $r = DBA::insert('attach', $fields);
212 return DBA::lastInsertId();
218 * Store new file metadata in db and binary in default backend from existing file
222 * @param string $filename
223 * @param string $allow_cid
224 * @param string $allow_gid
225 * @param string $deny_cid
226 * @param string $deny_gid
227 * @return boolean True on success
228 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
230 public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
232 if ($filename === '') {
233 $filename = basename($src);
236 $data = @file_get_contents($src);
238 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
243 * Update an attached file
245 * @param array $fields Contains the fields that are updated
246 * @param array $conditions Condition array with the key values
247 * @param Image $img Image data to update. Optional, default null.
248 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
250 * @return boolean Was the update successful?
252 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
253 * @see \Friendica\Database\DBA::update
255 public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
257 if (!is_null($img)) {
258 // get items to update
259 $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
261 foreach($items as $item) {
262 $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
263 if ($backend_class !== null) {
264 $fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
266 $fields['data'] = $img->asString();
271 $fields['edited'] = DateTimeFormat::utcNow();
273 return DBA::update('attach', $fields, $conditions, $old_fields);
278 * Delete info from table and data from storage
280 * @param array $conditions Field condition(s)
281 * @param array $options Options array, Optional
286 * @see \Friendica\Database\DBA::delete
288 public static function delete(array $conditions, array $options = [])
290 // get items to delete data info
291 $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
293 foreach($items as $item) {
294 $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
295 if ($backend_class !== null) {
296 $backend_class->delete($item['backend-ref'] ?? '');
300 return DBA::delete('attach', $conditions, $options);