2019-01-02 15:13:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2020-02-09 15:45:36 +01:00
|
|
|
|
2019-01-02 15:13:05 +01:00
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2019-02-03 22:46:50 +01:00
|
|
|
use Friendica\Core\System;
|
2019-01-02 15:13:05 +01:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-15 22:34:11 +01:00
|
|
|
use Friendica\DI;
|
2021-10-23 12:11:38 +02:00
|
|
|
use Friendica\Core\Storage\Exception\InvalidClassStorageException;
|
|
|
|
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
2019-01-07 19:28:02 +01:00
|
|
|
use Friendica\Object\Image;
|
2019-01-02 16:34:38 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
use Friendica\Util\Mimetype;
|
2020-09-30 11:14:01 +02:00
|
|
|
use Friendica\Security\Security;
|
2019-01-02 15:13:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to handle attach dabatase table
|
|
|
|
*/
|
2019-12-15 23:28:01 +01:00
|
|
|
class Attach
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Return a list of fields that are associated with the attach table
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
|
|
|
* @return array field list
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2022-06-18 17:09:18 +02:00
|
|
|
private static function getFields(): array
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
2022-07-12 23:21:16 +02:00
|
|
|
$allfields = DI::dbaDefinition()->getAll();
|
2019-01-02 15:13:05 +01:00
|
|
|
$fields = array_keys($allfields['attach']['fields']);
|
|
|
|
array_splice($fields, array_search('data', $fields), 1);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Select rows from the attach table and return them as array
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param array $fields Array of selected fields, empty for all
|
|
|
|
* @param array $conditions Array of fields for conditions
|
|
|
|
* @param array $params Array of several parameters
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
2022-06-17 11:17:53 +02:00
|
|
|
* @return array|bool
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2019-07-27 17:47:17 +02:00
|
|
|
* @see \Friendica\Database\DBA::selectToArray
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2019-07-27 17:47:17 +02:00
|
|
|
public static function selectToArray(array $fields = [], array $conditions = [], array $params = [])
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
|
|
|
if (empty($fields)) {
|
2019-01-07 19:26:54 +01:00
|
|
|
$fields = self::getFields();
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-28 06:03:42 +02:00
|
|
|
return DBA::selectToArray('attach', $fields, $conditions, $params);
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Retrieve a single record from the attach table
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param array $fields Array of selected fields, empty for all
|
|
|
|
* @param array $conditions Array of fields for conditions
|
|
|
|
* @param array $params Array of several parameters
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
|
|
|
public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
|
|
|
|
{
|
|
|
|
if (empty($fields)) {
|
|
|
|
$fields = self::getFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::selectFirst('attach', $fields, $conditions, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Check if attachment with given conditions exists
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param array $conditions Array of extra conditions
|
2019-01-02 15:13:05 +01:00
|
|
|
*
|
|
|
|
* @return boolean
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2022-06-16 20:42:40 +02:00
|
|
|
public static function exists(array $conditions): bool
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
|
|
|
return DBA::exists('attach', $conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Retrive a single record given the ID
|
2019-01-06 22:06:53 +01:00
|
|
|
*
|
|
|
|
* @param int $id Row id of the record
|
|
|
|
*
|
2019-01-02 15:13:05 +01:00
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function getById(int $id)
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
|
|
|
return self::selectFirst([], ['id' => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Retrive a single record given the ID
|
2019-01-06 22:06:53 +01:00
|
|
|
*
|
|
|
|
* @param int $id Row id of the record
|
|
|
|
*
|
2019-01-02 15:13:05 +01:00
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function getByIdWithPermission(int $id)
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
|
|
|
$r = self::selectFirst(['uid'], ['id' => $id]);
|
|
|
|
if ($r === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql_acl = Security::getPermissionsSQLByUserId($r['uid']);
|
|
|
|
|
|
|
|
$conditions = [
|
|
|
|
'`id` = ?' . $sql_acl,
|
|
|
|
$id
|
|
|
|
];
|
|
|
|
|
|
|
|
$item = self::selectFirst([], $conditions);
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Get file data for given row id. null if row id does not exist
|
2019-01-06 22:06:53 +01:00
|
|
|
*
|
|
|
|
* @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
|
|
|
|
*
|
2022-06-17 11:17:53 +02:00
|
|
|
* @return string|null file data or null on failure
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function getData(array $item)
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
2021-04-01 06:51:55 +02:00
|
|
|
if (!empty($item['data'])) {
|
|
|
|
return $item['data'];
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
try {
|
|
|
|
$backendClass = DI::storageManager()->getByName($item['backend-class'] ?? '');
|
|
|
|
$backendRef = $item['backend-ref'];
|
|
|
|
return $backendClass->get($backendRef);
|
|
|
|
} catch (InvalidClassStorageException $storageException) {
|
2019-01-02 15:13:05 +01:00
|
|
|
// legacy data storage in 'data' column
|
|
|
|
$i = self::selectFirst(['data'], ['id' => $item['id']]);
|
|
|
|
if ($i === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $i['data'];
|
2021-08-10 23:56:30 +02:00
|
|
|
} catch (ReferenceStorageException $referenceStorageException) {
|
|
|
|
DI::logger()->debug('No data found for item', ['item' => $item, 'exception' => $referenceStorageException]);
|
|
|
|
return '';
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 16:17:29 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Store new file metadata in db and binary in default backend
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $data Binary data
|
2019-01-02 16:17:29 +01:00
|
|
|
* @param integer $uid User ID
|
|
|
|
* @param string $filename Filename
|
2019-01-02 16:34:38 +01:00
|
|
|
* @param string $filetype Mimetype. optional, default = ''
|
|
|
|
* @param integer $filesize File size in bytes. optional, default = null
|
2019-01-02 16:17:29 +01:00
|
|
|
* @param string $allow_cid Permissions, allowed contacts. optional, default = ''
|
|
|
|
* @param string $allow_gid Permissions, allowed groups. optional, default = ''
|
|
|
|
* @param string $deny_cid Permissions, denied contacts.optional, default = ''
|
|
|
|
* @param string $deny_gid Permissions, denied greoup.optional, default = ''
|
|
|
|
*
|
2022-06-17 11:17:53 +02:00
|
|
|
* @return boolean|integer Row id on success, False on errors
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2019-01-02 16:17:29 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function store(string $data, int $uid, string $filename, string $filetype = '' , int $filesize = null, string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
|
2019-01-02 16:17:29 +01:00
|
|
|
{
|
|
|
|
if ($filetype === '') {
|
|
|
|
$filetype = Mimetype::getContentType($filename);
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:34:38 +01:00
|
|
|
if (is_null($filesize)) {
|
2019-01-02 16:17:29 +01:00
|
|
|
$filesize = strlen($data);
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
$backend_ref = DI::storage()->put($data);
|
|
|
|
$data = '';
|
2019-01-02 16:17:29 +01:00
|
|
|
|
|
|
|
$hash = System::createGUID(64);
|
|
|
|
$created = DateTimeFormat::utcNow();
|
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'uid' => $uid,
|
|
|
|
'hash' => $hash,
|
|
|
|
'filename' => $filename,
|
|
|
|
'filetype' => $filetype,
|
|
|
|
'filesize' => $filesize,
|
|
|
|
'data' => $data,
|
|
|
|
'created' => $created,
|
|
|
|
'edited' => $created,
|
|
|
|
'allow_cid' => $allow_cid,
|
|
|
|
'allow_gid' => $allow_gid,
|
|
|
|
'deny_cid' => $deny_cid,
|
|
|
|
'deny_gid' => $deny_gid,
|
2020-01-05 01:58:49 +01:00
|
|
|
'backend-class' => (string)DI::storage(),
|
2019-01-02 16:17:29 +01:00
|
|
|
'backend-ref' => $backend_ref
|
|
|
|
];
|
|
|
|
|
|
|
|
$r = DBA::insert('attach', $fields);
|
|
|
|
if ($r === true) {
|
|
|
|
return DBA::lastInsertId();
|
|
|
|
}
|
2019-01-02 16:34:38 +01:00
|
|
|
return $r;
|
2019-01-02 16:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Store new file metadata in db and binary in default backend from existing file
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
2022-06-17 11:17:53 +02:00
|
|
|
* @param string $src Source file name
|
|
|
|
* @param int $uid User id
|
|
|
|
* @param string $filename Optional file name
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $allow_cid
|
|
|
|
* @param string $allow_gid
|
|
|
|
* @param string $deny_cid
|
|
|
|
* @param string $deny_gid
|
2022-06-17 11:17:53 +02:00
|
|
|
* @return boolean|int Insert id or false on failure
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2019-01-02 16:17:29 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function storeFile(string $src, int $uid, string $filename = '', string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
|
2019-01-02 16:17:29 +01:00
|
|
|
{
|
|
|
|
if ($filename === '') {
|
|
|
|
$filename = basename($src);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = @file_get_contents($src);
|
|
|
|
|
2019-01-03 22:35:46 +01:00
|
|
|
return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
|
2019-01-02 16:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Update an attached file
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
|
|
|
* @param array $fields Contains the fields that are updated
|
|
|
|
* @param array $conditions Condition array with the key values
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param Image $img Image data to update. Optional, default null.
|
2019-01-02 16:17:29 +01:00
|
|
|
* @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @return boolean Was the update successful?
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @see \Friendica\Database\DBA::update
|
2019-01-02 16:17:29 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function update(array $fields, array $conditions, Image $img = null, array $old_fields = []): bool
|
2019-01-02 16:17:29 +01:00
|
|
|
{
|
2019-01-07 19:28:02 +01:00
|
|
|
if (!is_null($img)) {
|
2019-01-02 16:17:29 +01:00
|
|
|
// get items to update
|
2019-07-27 17:47:17 +02:00
|
|
|
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
|
2019-01-02 16:17:29 +01:00
|
|
|
|
|
|
|
foreach($items as $item) {
|
2021-08-10 23:56:30 +02:00
|
|
|
try {
|
|
|
|
$backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
|
2020-01-06 23:58:41 +01:00
|
|
|
$fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
|
2021-08-10 23:56:30 +02:00
|
|
|
} catch (InvalidClassStorageException $storageException) {
|
|
|
|
DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
|
|
|
|
} catch (ReferenceStorageException $referenceStorageException) {
|
|
|
|
DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
|
2019-01-02 16:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields['edited'] = DateTimeFormat::utcNow();
|
|
|
|
|
|
|
|
return DBA::update('attach', $fields, $conditions, $old_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Delete info from table and data from storage
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param array $conditions Field condition(s)
|
|
|
|
* @param array $options Options array, Optional
|
2019-01-02 16:17:29 +01:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::delete
|
2019-01-02 16:17:29 +01:00
|
|
|
*/
|
2022-06-17 11:17:53 +02:00
|
|
|
public static function delete(array $conditions, array $options = []): bool
|
2019-01-02 16:17:29 +01:00
|
|
|
{
|
|
|
|
// get items to delete data info
|
2019-07-27 17:47:17 +02:00
|
|
|
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
|
2019-01-02 16:17:29 +01:00
|
|
|
|
|
|
|
foreach($items as $item) {
|
2021-08-10 23:56:30 +02:00
|
|
|
try {
|
|
|
|
$backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
|
|
|
|
$backend_class->delete($item['backend-ref'] ?? '');
|
|
|
|
} catch (InvalidClassStorageException $storageException) {
|
|
|
|
DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
|
|
|
|
} catch (ReferenceStorageException $referenceStorageException) {
|
|
|
|
DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
|
2019-01-02 16:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::delete('attach', $conditions, $options);
|
|
|
|
}
|
|
|
|
}
|