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\Util\Security;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Mimetype;
19 * Class to handle attach dabatase table
21 class Attach extends BaseObject
25 * @brief 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(false);
33 $fields = array_keys($allfields['attach']['fields']);
34 array_splice($fields, array_search('data', $fields), 1);
39 * @brief Select rows from the attach table
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
45 * @return boolean|array
48 * @see \Friendica\Database\DBA::select
50 public static function select(array $fields = [], array $conditions = [], array $params = [])
53 $fields = self::getFields();
56 $r = DBA::select('attach', $fields, $conditions, $params);
57 return DBA::toArray($r);
61 * @brief Retrieve a single record from the attach table
63 * @param array $fields Array of selected fields, empty for all
64 * @param array $conditions Array of fields for conditions
65 * @param array $params Array of several parameters
70 * @see \Friendica\Database\DBA::select
72 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
75 $fields = self::getFields();
78 return DBA::selectFirst('attach', $fields, $conditions, $params);
82 * @brief Check if attachment with given conditions exists
84 * @param array $conditions Array of extra conditions
89 public static function exists(array $conditions)
91 return DBA::exists('attach', $conditions);
95 * @brief Retrive a single record given the ID
97 * @param int $id Row id of the record
102 * @see \Friendica\Database\DBA::select
104 public static function getById($id)
106 return self::selectFirst([], ['id' => $id]);
110 * @brief Retrive a single record given the ID
112 * @param int $id Row id of the record
117 * @see \Friendica\Database\DBA::select
119 public static function getByIdWithPermission($id)
121 $r = self::selectFirst(['uid'], ['id' => $id]);
126 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
129 '`id` = ?' . $sql_acl,
133 $item = self::selectFirst([], $conditions);
139 * @brief Get file data for given row id. null if row id does not exist
141 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
143 * @return string file data
146 public static function getData($item)
148 if ($item['backend-class'] == '') {
149 // legacy data storage in 'data' column
150 $i = self::selectFirst(['data'], ['id' => $item['id']]);
156 $backendClass = $item['backend-class'];
157 $backendRef = $item['backend-ref'];
158 return $backendClass::get($backendRef);
163 * @brief Store new file metadata in db and binary in default backend
165 * @param string $data Binary data
166 * @param integer $uid User ID
167 * @param string $filename Filename
168 * @param string $filetype Mimetype. optional, default = ''
169 * @param integer $filesize File size in bytes. optional, default = null
170 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
171 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
172 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
173 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
175 * @return boolean/integer Row id on success, False on errors
176 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
178 public function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
180 if ($filetype === '') {
181 $filetype = Mimetype::getContentType($filename);
184 if (is_null($filesize)) {
185 $filesize = strlen($data);
188 $backend_class = StorageManager::getBackend();
190 if ($backend_class !== '') {
191 $backend_ref = $backend_class::put($data);
195 $hash = System::createGUID(64);
196 $created = DateTimeFormat::utcNow();
201 'filename' => $filename,
202 'filetype' => $filetype,
203 'filesize' => $filesize,
205 'created' => $created,
206 'edited' => $created,
207 'allow_cid' => $allow_cid,
208 'allow_gid' => $allow_gid,
209 'deny_cid' => $deny_cid,
210 'deny_gid' => $deny_gid,
211 'backend-class' => $backend_class,
212 'backend-ref' => $backend_ref
215 $r = DBA::insert('attach', $fields);
217 return DBA::lastInsertId();
223 * @brief Store new file metadata in db and binary in default backend from existing file
227 * @param string $filename
228 * @param string $allow_cid
229 * @param string $allow_gid
230 * @param string $deny_cid
231 * @param string $deny_gid
232 * @return boolean True on success
233 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
235 public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
237 if ($filename === '') {
238 $filename = basename($src);
241 $data = @file_get_contents($src);
243 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
248 * @brief Update an attached file
250 * @param array $fields Contains the fields that are updated
251 * @param array $conditions Condition array with the key values
252 * @param Image $img Image data to update. Optional, default null.
253 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
255 * @return boolean Was the update successful?
257 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
258 * @see \Friendica\Database\DBA::update
260 public static function update($fields, $conditions, $img = null, array $old_fields = [])
262 if (!is_null($data)) {
263 // get items to update
264 $items = self::select(['backend-class','backend-ref'], $conditions);
266 foreach($items as $item) {
267 $backend_class = (string)$item['backend-class'];
268 if ($backend_class !== '') {
269 $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
271 $fields['data'] = $data;
276 $fields['edited'] = DateTimeFormat::utcNow();
278 return DBA::update('attach', $fields, $conditions, $old_fields);
283 * @brief Delete info from table and data from storage
285 * @param array $conditions Field condition(s)
286 * @param array $options Options array, Optional
291 * @see \Friendica\Database\DBA::delete
293 public static function delete(array $conditions, array $options = [])
295 // get items to delete data info
296 $items = self::select(['backend-class','backend-ref'], $conditions);
298 foreach($items as $item) {
299 $backend_class = (string)$item['backend-class'];
300 if ($backend_class !== '') {
301 $backend_class::delete($item['backend-ref']);
305 return DBA::delete('attach', $conditions, $options);