4 * @file src/Model/Attach.php
5 * @brief This file contains the Attach class for database interface
7 namespace Friendica\Model;
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\Object\Image;
15 use Friendica\Util\Security;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Mimetype;
20 * Class to handle attach dabatase table
22 class Attach extends BaseObject
26 * @brief Return a list of fields that are associated with the attach table
28 * @return array field list
31 private static function getFields()
33 $allfields = DBStructure::definition(false);
34 $fields = array_keys($allfields['attach']['fields']);
35 array_splice($fields, array_search('data', $fields), 1);
40 * @brief Select rows from the attach table
42 * @param array $fields Array of selected fields, empty for all
43 * @param array $conditions Array of fields for conditions
44 * @param array $params Array of several parameters
46 * @return boolean|array
49 * @see \Friendica\Database\DBA::select
51 public static function select(array $fields = [], array $conditions = [], array $params = [])
54 $fields = self::getFields();
57 $r = DBA::select('attach', $fields, $conditions, $params);
58 return DBA::toArray($r);
62 * @brief Retrieve a single record from the attach table
64 * @param array $fields Array of selected fields, empty for all
65 * @param array $conditions Array of fields for conditions
66 * @param array $params Array of several parameters
71 * @see \Friendica\Database\DBA::select
73 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
76 $fields = self::getFields();
79 return DBA::selectFirst('attach', $fields, $conditions, $params);
83 * @brief Check if attachment with given conditions exists
85 * @param array $conditions Array of extra conditions
90 public static function exists(array $conditions)
92 return DBA::exists('attach', $conditions);
96 * @brief Retrive a single record given the ID
98 * @param int $id Row id of the record
103 * @see \Friendica\Database\DBA::select
105 public static function getById($id)
107 return self::selectFirst([], ['id' => $id]);
111 * @brief Retrive a single record given the ID
113 * @param int $id Row id of the record
118 * @see \Friendica\Database\DBA::select
120 public static function getByIdWithPermission($id)
122 $r = self::selectFirst(['uid'], ['id' => $id]);
127 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
130 '`id` = ?' . $sql_acl,
134 $item = self::selectFirst([], $conditions);
140 * @brief Get file data for given row id. null if row id does not exist
142 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
144 * @return string file data
147 public static function getData($item)
149 if ($item['backend-class'] == '') {
150 // legacy data storage in 'data' column
151 $i = self::selectFirst(['data'], ['id' => $item['id']]);
157 $backendClass = $item['backend-class'];
158 $backendRef = $item['backend-ref'];
159 return $backendClass::get($backendRef);
164 * @brief Store new file metadata in db and binary in default backend
166 * @param string $data Binary data
167 * @param integer $uid User ID
168 * @param string $filename Filename
169 * @param string $filetype Mimetype. optional, default = ''
170 * @param integer $filesize File size in bytes. optional, default = null
171 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
172 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
173 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
174 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
176 * @return boolean/integer Row id on success, False on errors
177 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179 public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
181 if ($filetype === '') {
182 $filetype = Mimetype::getContentType($filename);
185 if (is_null($filesize)) {
186 $filesize = strlen($data);
189 $backend_class = StorageManager::getBackend();
191 if ($backend_class !== '') {
192 $backend_ref = $backend_class::put($data);
196 $hash = System::createGUID(64);
197 $created = DateTimeFormat::utcNow();
202 'filename' => $filename,
203 'filetype' => $filetype,
204 'filesize' => $filesize,
206 'created' => $created,
207 'edited' => $created,
208 'allow_cid' => $allow_cid,
209 'allow_gid' => $allow_gid,
210 'deny_cid' => $deny_cid,
211 'deny_gid' => $deny_gid,
212 'backend-class' => $backend_class,
213 'backend-ref' => $backend_ref
216 $r = DBA::insert('attach', $fields);
218 return DBA::lastInsertId();
224 * @brief Store new file metadata in db and binary in default backend from existing file
228 * @param string $filename
229 * @param string $allow_cid
230 * @param string $allow_gid
231 * @param string $deny_cid
232 * @param string $deny_gid
233 * @return boolean True on success
234 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236 public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
238 if ($filename === '') {
239 $filename = basename($src);
242 $data = @file_get_contents($src);
244 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
249 * @brief Update an attached file
251 * @param array $fields Contains the fields that are updated
252 * @param array $conditions Condition array with the key values
253 * @param Image $img Image data to update. Optional, default null.
254 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
256 * @return boolean Was the update successful?
258 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
259 * @see \Friendica\Database\DBA::update
261 public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
263 if (!is_null($img)) {
264 // get items to update
265 $items = self::select(['backend-class','backend-ref'], $conditions);
267 foreach($items as $item) {
268 $backend_class = (string)$item['backend-class'];
269 if ($backend_class !== '') {
270 $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
272 $fields['data'] = $img->asString();
277 $fields['edited'] = DateTimeFormat::utcNow();
279 return DBA::update('attach', $fields, $conditions, $old_fields);
284 * @brief Delete info from table and data from storage
286 * @param array $conditions Field condition(s)
287 * @param array $options Options array, Optional
292 * @see \Friendica\Database\DBA::delete
294 public static function delete(array $conditions, array $options = [])
296 // get items to delete data info
297 $items = self::select(['backend-class','backend-ref'], $conditions);
299 foreach($items as $item) {
300 $backend_class = (string)$item['backend-class'];
301 if ($backend_class !== '') {
302 $backend_class::delete($item['backend-ref']);
306 return DBA::delete('attach', $conditions, $options);