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
29 private static function getFields()
31 $allfields = DBStructure::definition(false);
32 $fields = array_keys($allfields['attach']['fields']);
33 array_splice($fields, array_search('data', $fields), 1);
38 * @brief Select rows from the attach table
40 * @param array $fields Array of selected fields, empty for all
41 * @param array $conditions Array of fields for conditions
42 * @param array $params Array of several parameters
44 * @return boolean|array
46 * @see \Friendica\Database\DBA::select
48 public static function select(array $fields = [], array $conditions = [], array $params = [])
51 $selected = self::getFields();
54 $r = DBA::select('attach', $fields, $conditions, $params);
55 return DBA::toArray($r);
59 * @brief Retrieve a single record from the attach table
61 * @param array $fields Array of selected fields, empty for all
62 * @param array $conditions Array of fields for conditions
63 * @param array $params Array of several parameters
67 * @see \Friendica\Database\DBA::select
69 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
72 $fields = self::getFields();
75 return DBA::selectFirst('attach', $fields, $conditions, $params);
79 * @brief Check if attachment with given conditions exists
81 * @param array $conditions Array of extra conditions
85 public static function exists(array $conditions)
87 return DBA::exists('attach', $conditions);
91 * @brief Retrive a single record given the ID
93 * @param int $id Row id of the record
97 * @see \Friendica\Database\DBA::select
99 public static function getById($id)
101 return self::selectFirst([], ['id' => $id]);
105 * @brief Retrive a single record given the ID
107 * @param int $id Row id of the record
111 * @see \Friendica\Database\DBA::select
113 public static function getByIdWithPermission($id)
115 $r = self::selectFirst(['uid'], ['id' => $id]);
120 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
123 '`id` = ?' . $sql_acl,
127 $item = self::selectFirst([], $conditions);
133 * @brief Get file data for given row id. null if row id does not exist
135 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
137 * @return string file data
139 public static function getData($item)
141 if ($item['backend-class'] == '') {
142 // legacy data storage in 'data' column
143 $i = self::selectFirst(['data'], ['id' => $item['id']]);
149 $backendClass = $item['backend-class'];
150 $backendRef = $item['backend-ref'];
151 return $backendClass::get($backendRef);
156 * @brief Store new file metadata in db and binary in default backend
158 * @param string $data Binary data
159 * @param integer $uid User ID
160 * @param string $filename Filename
161 * @param string $filetype Mimetype. optional, default = ''
162 * @param integer $filesize File size in bytes. optional, default = null
163 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
164 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
165 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
166 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
168 * @return boolean/integer Row id on success, False on errors
170 public function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
172 if ($filetype === '') {
173 $filetype = Mimetype::getContentType($filename);
176 if (is_null($filesize)) {
177 $filesize = strlen($data);
180 $backend_class = StorageManager::getBackend();
182 if ($backend_class !== '') {
183 $backend_ref = $backend_class::put($data);
187 $hash = System::createGUID(64);
188 $created = DateTimeFormat::utcNow();
193 'filename' => $filename,
194 'filetype' => $filetype,
195 'filesize' => $filesize,
197 'created' => $created,
198 'edited' => $created,
199 'allow_cid' => $allow_cid,
200 'allow_gid' => $allow_gid,
201 'deny_cid' => $deny_cid,
202 'deny_gid' => $deny_gid,
203 'backend-class' => $backend_class,
204 'backend-ref' => $backend_ref
207 $r = DBA::insert('attach', $fields);
209 return DBA::lastInsertId();
215 * @brief Store new file metadata in db and binary in default backend from existing file
217 * @return boolean True on success
219 public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
221 if ($filename === '') {
222 $filename = basename($src);
225 $data = @file_get_contents($src);
227 return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
232 * @brief Update an attached file
234 * @param array $fields Contains the fields that are updated
235 * @param array $conditions Condition array with the key values
236 * @param string $data File data to update. Optional, default null.
237 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
239 * @return boolean Was the update successfull?
241 * @see \Friendica\Database\DBA::update
243 public static function update($fields, $conditions, $img = null, array $old_fields = [])
245 if (!is_null($data)) {
246 // get items to update
247 $items = self::select(['backend-class','backend-ref'], $conditions);
249 foreach($items as $item) {
250 $backend_class = (string)$item['backend-class'];
251 if ($backend_class !== '') {
252 $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
254 $fields['data'] = $data;
259 $fields['edited'] = DateTimeFormat::utcNow();
261 return DBA::update('attach', $fields, $conditions, $old_fields);
266 * @brief Delete info from table and data from storage
268 * @param array $conditions Field condition(s)
269 * @param array $options Options array, Optional
273 * @see \Friendica\Database\DBA::delete
275 public static function delete(array $conditions, array $options = [])
277 // get items to delete data info
278 $items = self::select(['backend-class','backend-ref'], $conditions);
280 foreach($items as $item) {
281 $backend_class = (string)$item['backend-class'];
282 if ($backend_class !== '') {
283 $backend_class::delete($item['backend-ref']);
287 return DBA::delete('attach', $conditions, $options);