2019-01-02 09:13:05 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file src/Model/Attach.php
|
|
|
|
* @brief This file contains the Attach class for database interface
|
|
|
|
*/
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
|
|
|
use Friendica\BaseObject;
|
|
|
|
use Friendica\Core\StorageManager;
|
2019-02-03 16:46:50 -05:00
|
|
|
use Friendica\Core\System;
|
2019-01-02 09:13:05 -05:00
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\Database\DBStructure;
|
2019-01-21 16:51:59 -05:00
|
|
|
use Friendica\Model\Storage\IStorage;
|
2019-01-07 13:28:02 -05:00
|
|
|
use Friendica\Object\Image;
|
2019-01-02 10:34:38 -05:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
use Friendica\Util\Mimetype;
|
2019-02-03 16:46:50 -05:00
|
|
|
use Friendica\Util\Security;
|
2019-01-02 09:13:05 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to handle attach dabatase table
|
|
|
|
*/
|
|
|
|
class Attach extends BaseObject
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a list of fields that are associated with the attach table
|
|
|
|
*
|
|
|
|
* @return array field list
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
private static function getFields()
|
|
|
|
{
|
2019-02-03 16:46:50 -05:00
|
|
|
$allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
|
2019-01-02 09:13:05 -05:00
|
|
|
$fields = array_keys($allfields['attach']['fields']);
|
|
|
|
array_splice($fields, array_search('data', $fields), 1);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-27 11:47:17 -04:00
|
|
|
* @brief Select rows from the attach table and return them as array
|
2019-01-02 09:13:05 -05:00
|
|
|
*
|
2019-01-06 16:06:53 -05: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 09:13:05 -05:00
|
|
|
*
|
|
|
|
* @return boolean|array
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2019-07-27 11:47:17 -04:00
|
|
|
* @see \Friendica\Database\DBA::selectToArray
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
2019-07-27 11:47:17 -04:00
|
|
|
public static function selectToArray(array $fields = [], array $conditions = [], array $params = [])
|
2019-01-02 09:13:05 -05:00
|
|
|
{
|
|
|
|
if (empty($fields)) {
|
2019-01-07 13:26:54 -05:00
|
|
|
$fields = self::getFields();
|
2019-01-02 09:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$r = DBA::select('attach', $fields, $conditions, $params);
|
|
|
|
return DBA::toArray($r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieve a single record from the attach table
|
|
|
|
*
|
2019-01-06 16:06:53 -05: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 09:13:05 -05:00
|
|
|
*
|
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
|
|
|
|
{
|
|
|
|
if (empty($fields)) {
|
|
|
|
$fields = self::getFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::selectFirst('attach', $fields, $conditions, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if attachment with given conditions exists
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param array $conditions Array of extra conditions
|
2019-01-02 09:13:05 -05:00
|
|
|
*
|
|
|
|
* @return boolean
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
public static function exists(array $conditions)
|
|
|
|
{
|
|
|
|
return DBA::exists('attach', $conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrive a single record given the ID
|
2019-01-06 16:06:53 -05:00
|
|
|
*
|
|
|
|
* @param int $id Row id of the record
|
|
|
|
*
|
2019-01-02 09:13:05 -05:00
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
public static function getById($id)
|
|
|
|
{
|
|
|
|
return self::selectFirst([], ['id' => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-06 16:06:53 -05:00
|
|
|
* @brief Retrive a single record given the ID
|
|
|
|
*
|
|
|
|
* @param int $id Row id of the record
|
|
|
|
*
|
2019-01-02 09:13:05 -05:00
|
|
|
* @return bool|array
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::select
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
public static function getByIdWithPermission($id)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get file data for given row id. null if row id does not exist
|
2019-01-06 16:06:53 -05:00
|
|
|
*
|
|
|
|
* @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
|
|
|
|
*
|
2019-01-02 09:13:05 -05:00
|
|
|
* @return string file data
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
public static function getData($item)
|
|
|
|
{
|
|
|
|
if ($item['backend-class'] == '') {
|
|
|
|
// legacy data storage in 'data' column
|
|
|
|
$i = self::selectFirst(['data'], ['id' => $item['id']]);
|
|
|
|
if ($i === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $i['data'];
|
|
|
|
} else {
|
|
|
|
$backendClass = $item['backend-class'];
|
|
|
|
$backendRef = $item['backend-ref'];
|
|
|
|
return $backendClass::get($backendRef);
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 10:17:29 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Store new file metadata in db and binary in default backend
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param string $data Binary data
|
2019-01-02 10:17:29 -05:00
|
|
|
* @param integer $uid User ID
|
|
|
|
* @param string $filename Filename
|
2019-01-02 10:34:38 -05:00
|
|
|
* @param string $filetype Mimetype. optional, default = ''
|
|
|
|
* @param integer $filesize File size in bytes. optional, default = null
|
2019-01-02 10:17:29 -05: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 = ''
|
|
|
|
*
|
|
|
|
* @return boolean/integer Row id on success, False on errors
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2019-01-02 10:17:29 -05:00
|
|
|
*/
|
2019-01-21 16:05:25 -05:00
|
|
|
public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
|
2019-01-02 10:17:29 -05:00
|
|
|
{
|
|
|
|
if ($filetype === '') {
|
|
|
|
$filetype = Mimetype::getContentType($filename);
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:34:38 -05:00
|
|
|
if (is_null($filesize)) {
|
2019-01-02 10:17:29 -05:00
|
|
|
$filesize = strlen($data);
|
|
|
|
}
|
|
|
|
|
2019-01-21 16:51:59 -05:00
|
|
|
/** @var IStorage $backend_class */
|
2019-01-02 10:17:29 -05:00
|
|
|
$backend_class = StorageManager::getBackend();
|
|
|
|
$backend_ref = '';
|
|
|
|
if ($backend_class !== '') {
|
|
|
|
$backend_ref = $backend_class::put($data);
|
|
|
|
$data = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$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,
|
|
|
|
'backend-class' => $backend_class,
|
|
|
|
'backend-ref' => $backend_ref
|
|
|
|
];
|
|
|
|
|
|
|
|
$r = DBA::insert('attach', $fields);
|
|
|
|
if ($r === true) {
|
|
|
|
return DBA::lastInsertId();
|
|
|
|
}
|
2019-01-02 10:34:38 -05:00
|
|
|
return $r;
|
2019-01-02 10:17:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Store new file metadata in db and binary in default backend from existing file
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param $src
|
|
|
|
* @param $uid
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $allow_cid
|
|
|
|
* @param string $allow_gid
|
|
|
|
* @param string $deny_cid
|
|
|
|
* @param string $deny_gid
|
2019-01-02 10:17:29 -05:00
|
|
|
* @return boolean True on success
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2019-01-02 10:17:29 -05:00
|
|
|
*/
|
2019-01-07 12:51:48 -05:00
|
|
|
public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
|
2019-01-02 10:17:29 -05:00
|
|
|
{
|
|
|
|
if ($filename === '') {
|
|
|
|
$filename = basename($src);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = @file_get_contents($src);
|
|
|
|
|
2019-01-03 16:35:46 -05:00
|
|
|
return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
|
2019-01-02 10:17:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Update an attached file
|
|
|
|
*
|
|
|
|
* @param array $fields Contains the fields that are updated
|
|
|
|
* @param array $conditions Condition array with the key values
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param Image $img Image data to update. Optional, default null.
|
2019-01-02 10:17:29 -05: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 16:06:53 -05:00
|
|
|
* @return boolean Was the update successful?
|
2019-01-02 10:17:29 -05:00
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @see \Friendica\Database\DBA::update
|
2019-01-02 10:17:29 -05:00
|
|
|
*/
|
2019-01-07 13:28:02 -05:00
|
|
|
public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
|
2019-01-02 10:17:29 -05:00
|
|
|
{
|
2019-01-07 13:28:02 -05:00
|
|
|
if (!is_null($img)) {
|
2019-01-02 10:17:29 -05:00
|
|
|
// get items to update
|
2019-07-27 11:47:17 -04:00
|
|
|
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
|
2019-01-02 10:17:29 -05:00
|
|
|
|
|
|
|
foreach($items as $item) {
|
2019-01-21 16:51:59 -05:00
|
|
|
/** @var IStorage $backend_class */
|
2019-01-02 10:17:29 -05:00
|
|
|
$backend_class = (string)$item['backend-class'];
|
|
|
|
if ($backend_class !== '') {
|
|
|
|
$fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
|
|
|
|
} else {
|
2019-01-07 13:28:02 -05:00
|
|
|
$fields['data'] = $img->asString();
|
2019-01-02 10:17:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields['edited'] = DateTimeFormat::utcNow();
|
|
|
|
|
|
|
|
return DBA::update('attach', $fields, $conditions, $old_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete info from table and data from storage
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param array $conditions Field condition(s)
|
|
|
|
* @param array $options Options array, Optional
|
2019-01-02 10:17:29 -05:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
|
|
|
* @see \Friendica\Database\DBA::delete
|
2019-01-02 10:17:29 -05:00
|
|
|
*/
|
|
|
|
public static function delete(array $conditions, array $options = [])
|
|
|
|
{
|
|
|
|
// get items to delete data info
|
2019-07-27 11:47:17 -04:00
|
|
|
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
|
2019-01-02 10:17:29 -05:00
|
|
|
|
|
|
|
foreach($items as $item) {
|
2019-01-21 16:51:59 -05:00
|
|
|
/** @var IStorage $backend_class */
|
2019-01-02 10:17:29 -05:00
|
|
|
$backend_class = (string)$item['backend-class'];
|
|
|
|
if ($backend_class !== '') {
|
|
|
|
$backend_class::delete($item['backend-ref']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::delete('attach', $conditions, $options);
|
|
|
|
}
|
|
|
|
}
|