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\StorageManager;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Database\DBStructure;
14 use Friendica\Model\Storage\IStorage;
15 use Friendica\Object\Image;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Mimetype;
18 use Friendica\Util\Security;
21 * Class to handle attach dabatase table
23 class Attach extends BaseObject
27 * @brief Return a list of fields that are associated with the attach table
29 * @return array field list
32 private static function getFields()
34 $allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
35 $fields = array_keys($allfields['attach']['fields']);
36 array_splice($fields, array_search('data', $fields), 1);
41 * @brief Select rows from the attach table
43 * @param array $fields Array of selected fields, empty for all
44 * @param array $conditions Array of fields for conditions
45 * @param array $params Array of several parameters
47 * @return boolean|array
50 * @see \Friendica\Database\DBA::select
52 public static function select(array $fields = [], array $conditions = [], array $params = [])
55 $fields = self::getFields();
58 $r = DBA::select('attach', $fields, $conditions, $params);
59 return DBA::toArray($r);
63 * @brief Retrieve a single record from the attach table
65 * @param array $fields Array of selected fields, empty for all
66 * @param array $conditions Array of fields for conditions
67 * @param array $params Array of several parameters
72 * @see \Friendica\Database\DBA::select
74 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
77 $fields = self::getFields();
80 return DBA::selectFirst('attach', $fields, $conditions, $params);
84 * @brief Check if attachment with given conditions exists
86 * @param array $conditions Array of extra conditions
91 public static function exists(array $conditions)
93 return DBA::exists('attach', $conditions);
97 * @brief Retrive a single record given the ID
99 * @param int $id Row id of the record
104 * @see \Friendica\Database\DBA::select
106 public static function getById($id)
108 return self::selectFirst([], ['id' => $id]);
112 * @brief Retrive a single record given the ID
114 * @param int $id Row id of the record
119 * @see \Friendica\Database\DBA::select
121 public static function getByIdWithPermission($id)
123 $r = self::selectFirst(['uid'], ['id' => $id]);
128 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
131 '`id` = ?' . $sql_acl,
135 $item = self::selectFirst([], $conditions);
141 * @brief Get file data for given row id. null if row id does not exist
143 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
145 * @return string file data
148 public static function getData($item)
150 if ($item['backend-class'] == '') {
151 // legacy data storage in 'data' column
152 $i = self::selectFirst(['data'], ['id' => $item['id']]);
158 $backendClass = $item['backend-class'];
159 $backendRef = $item['backend-ref'];
160 return $backendClass::get($backendRef);
165 * @brief Store new file metadata in db and binary in default backend
167 * @param string $data Binary data
168 * @param integer $uid User ID
169 * @param string $filename Filename
170 * @param string $filetype Mimetype. optional, default = ''
171 * @param integer $filesize File size in bytes. optional, default = null
172 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
173 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
174 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
175 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
177 * @return boolean/integer Row id on success, False on errors
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
182 if ($filetype === '') {
183 $filetype = Mimetype::getContentType($filename);
186 if (is_null($filesize)) {
187 $filesize = strlen($data);
190 /** @var IStorage $backend_class */
191 $backend_class = StorageManager::getBackend();
193 if ($backend_class !== '') {
194 $backend_ref = $backend_class::put($data);
198 $hash = System::createGUID(64);
199 $created = DateTimeFormat::utcNow();
204 'filename' => $filename,
205 'filetype' => $filetype,
206 'filesize' => $filesize,
208 'created' => $created,
209 'edited' => $created,
210 'allow_cid' => $allow_cid,
211 'allow_gid' => $allow_gid,
212 'deny_cid' => $deny_cid,
213 'deny_gid' => $deny_gid,
214 'backend-class' => $backend_class,
215 'backend-ref' => $backend_ref
218 $r = DBA::insert('attach', $fields);
220 return DBA::lastInsertId();
226 * @brief Store new file metadata in db and binary in default backend from existing file
230 * @param string $filename
231 * @param string $allow_cid
232 * @param string $allow_gid
233 * @param string $deny_cid
234 * @param string $deny_gid
235 * @return boolean True on success
236 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
238 public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
240 if ($filename === '') {
241 $filename = basename($src);
244 $data = @file_get_contents($src);
246 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
251 * @brief Update an attached file
253 * @param array $fields Contains the fields that are updated
254 * @param array $conditions Condition array with the key values
255 * @param Image $img Image data to update. Optional, default null.
256 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
258 * @return boolean Was the update successful?
260 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
261 * @see \Friendica\Database\DBA::update
263 public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
265 if (!is_null($img)) {
266 // get items to update
267 $items = self::select(['backend-class','backend-ref'], $conditions);
269 foreach($items as $item) {
270 /** @var IStorage $backend_class */
271 $backend_class = (string)$item['backend-class'];
272 if ($backend_class !== '') {
273 $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
275 $fields['data'] = $img->asString();
280 $fields['edited'] = DateTimeFormat::utcNow();
282 return DBA::update('attach', $fields, $conditions, $old_fields);
287 * @brief Delete info from table and data from storage
289 * @param array $conditions Field condition(s)
290 * @param array $options Options array, Optional
295 * @see \Friendica\Database\DBA::delete
297 public static function delete(array $conditions, array $options = [])
299 // get items to delete data info
300 $items = self::select(['backend-class','backend-ref'], $conditions);
302 foreach($items as $item) {
303 /** @var IStorage $backend_class */
304 $backend_class = (string)$item['backend-class'];
305 if ($backend_class !== '') {
306 $backend_class::delete($item['backend-ref']);
310 return DBA::delete('attach', $conditions, $options);