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\Database\DBA;
12 use Friendica\Database\DBStructure;
13 use Friendica\Util\Security;
17 * Class to handle attach dabatase table
19 class Attach extends BaseObject
23 * @brief Return a list of fields that are associated with the attach table
25 * @return array field list
27 private static function getFields()
29 $allfields = DBStructure::definition(false);
30 $fields = array_keys($allfields['attach']['fields']);
31 array_splice($fields, array_search('data', $fields), 1);
36 * @brief Select rows from the attach table
38 * @param array $fields Array of selected fields, empty for all
39 * @param array $conditions Array of fields for conditions
40 * @param array $params Array of several parameters
42 * @return boolean|array
44 * @see \Friendica\Database\DBA::select
46 public static function select(array $fields = [], array $conditions = [], array $params = [])
49 $selected = self::getFields();
52 $r = DBA::select('attach', $fields, $conditions, $params);
53 return DBA::toArray($r);
57 * @brief Retrieve a single record from the attach table
59 * @param array $fields Array of selected fields, empty for all
60 * @param array $conditions Array of fields for conditions
61 * @param array $params Array of several parameters
65 * @see \Friendica\Database\DBA::select
67 public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
70 $fields = self::getFields();
73 return DBA::selectFirst('attach', $fields, $conditions, $params);
77 * @brief Check if attachment with given conditions exists
79 * @param array $conditions Array of extra conditions
83 public static function exists(array $conditions)
85 return DBA::exists('attach', $conditions);
89 * @brief Retrive a single record given the ID
91 * @param int $id Row id of the record
95 * @see \Friendica\Database\DBA::select
97 public static function getById($id)
99 return self::selectFirst([], ['id' => $id]);
103 * @brief Retrive a single record given the ID
105 * @param int $id Row id of the record
109 * @see \Friendica\Database\DBA::select
111 public static function getByIdWithPermission($id)
113 $r = self::selectFirst(['uid'], ['id' => $id]);
118 $sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
121 '`id` = ?' . $sql_acl,
125 $item = self::selectFirst([], $conditions);
131 * @brief Get file data for given row id. null if row id does not exist
133 * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
135 * @return string file data
137 public static function getData($item)
139 if ($item['backend-class'] == '') {
140 // legacy data storage in 'data' column
141 $i = self::selectFirst(['data'], ['id' => $item['id']]);
147 $backendClass = $item['backend-class'];
148 $backendRef = $item['backend-ref'];
149 return $backendClass::get($backendRef);
154 * @brief Store new file metadata in db and binary in default backend
156 * @param string $data Binary data
157 * @param integer $uid User ID
158 * @param string $filename Filename
159 * @param string $filetype Mimetype. optional
160 * @param integer $filesize File size in bytes. optional
161 * @param string $allow_cid Permissions, allowed contacts. optional, default = ''
162 * @param string $allow_gid Permissions, allowed groups. optional, default = ''
163 * @param string $deny_cid Permissions, denied contacts.optional, default = ''
164 * @param string $deny_gid Permissions, denied greoup.optional, default = ''
166 * @return boolean/integer Row id on success, False on errors
168 public function store($data, $uid, $filename, $filetype = '' , $filesize = -1, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
170 if ($filetype === '') {
171 $filetype = Mimetype::getContentType($filename);
175 $filesize = strlen($data);
178 $backend_class = StorageManager::getBackend();
180 if ($backend_class !== '') {
181 $backend_ref = $backend_class::put($data);
185 $hash = System::createGUID(64);
186 $created = DateTimeFormat::utcNow();
191 'filename' => $filename,
192 'filetype' => $filetype,
193 'filesize' => $filesize,
195 'created' => $created,
196 'edited' => $created,
197 'allow_cid' => $allow_cid,
198 'allow_gid' => $allow_gid,
199 'deny_cid' => $deny_cid,
200 'deny_gid' => $deny_gid,
201 'backend-class' => $backend_class,
202 'backend-ref' => $backend_ref
205 $r = DBA::insert('attach', $fields);
207 return DBA::lastInsertId();
212 * @brief Store new file metadata in db and binary in default backend from existing file
214 * @return boolean True on success
216 public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
218 if ($filename === '') {
219 $filename = basename($src);
222 $data = @file_get_contents($src);
224 return self::store($data, $uid, $filename, '', '', $allow_cid, $allow_gid, $deny_cid, $deny_gid);
229 * @brief Update an attached file
231 * @param array $fields Contains the fields that are updated
232 * @param array $conditions Condition array with the key values
233 * @param string $data File data to update. Optional, default null.
234 * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
236 * @return boolean Was the update successfull?
238 * @see \Friendica\Database\DBA::update
240 public static function update($fields, $conditions, $img = null, array $old_fields = [])
242 if (!is_null($data)) {
243 // get items to update
244 $items = self::select(['backend-class','backend-ref'], $conditions);
246 foreach($items as $item) {
247 $backend_class = (string)$item['backend-class'];
248 if ($backend_class !== '') {
249 $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
251 $fields['data'] = $data;
256 $fields['edited'] = DateTimeFormat::utcNow();
258 return DBA::update('attach', $fields, $conditions, $old_fields);
263 * @brief Delete info from table and data from storage
265 * @param array $conditions Field condition(s)
266 * @param array $options Options array, Optional
270 * @see \Friendica\Database\DBA::delete
272 public static function delete(array $conditions, array $options = [])
274 // get items to delete data info
275 $items = self::select(['backend-class','backend-ref'], $conditions);
277 foreach($items as $item) {
278 $backend_class = (string)$item['backend-class'];
279 if ($backend_class !== '') {
280 $backend_class::delete($item['backend-ref']);
284 return DBA::delete('attach', $conditions, $options);