Doxygen include/attach.php
Some Doxygen as far as I understood and some styleguides.
This commit is contained in:
parent
aa44d3abab
commit
d755fb83a7
@ -1,7 +1,8 @@
|
|||||||
<?php /** @file */
|
<?php
|
||||||
|
|
||||||
/*
|
/** @file
|
||||||
* File/attach API with the potential for revision control.
|
*
|
||||||
|
* @brief File/attach API with the potential for revision control.
|
||||||
*
|
*
|
||||||
* TODO: a filesystem storage abstraction which maintains security (and 'data' contains a system filename
|
* TODO: a filesystem storage abstraction which maintains security (and 'data' contains a system filename
|
||||||
* which is inaccessible from the web). This could get around PHP storage limits and store videos and larger
|
* which is inaccessible from the web). This could get around PHP storage limits and store videos and larger
|
||||||
@ -12,6 +13,15 @@
|
|||||||
require_once('include/permissions.php');
|
require_once('include/permissions.php');
|
||||||
require_once('include/security.php');
|
require_once('include/security.php');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Guess the mimetype from file ending.
|
||||||
|
*
|
||||||
|
* This function takes a file name and guess the mimetype from the
|
||||||
|
* filename extension.
|
||||||
|
*
|
||||||
|
* @param $filename a string filename
|
||||||
|
* @return string The mimetype according to a file ending.
|
||||||
|
*/
|
||||||
function z_mime_content_type($filename) {
|
function z_mime_content_type($filename) {
|
||||||
|
|
||||||
$mime_types = array(
|
$mime_types = array(
|
||||||
@ -80,8 +90,6 @@ function z_mime_content_type($filename) {
|
|||||||
if (array_key_exists($ext, $mime_types)) {
|
if (array_key_exists($ext, $mime_types)) {
|
||||||
return $mime_types[$ext];
|
return $mime_types[$ext];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'application/octet-stream';
|
return 'application/octet-stream';
|
||||||
@ -89,7 +97,20 @@ function z_mime_content_type($filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Count files/attachments.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param $channel_id
|
||||||
|
* @param $observer
|
||||||
|
* @param $hash (optional)
|
||||||
|
* @param $filename (optional)
|
||||||
|
* @param $filetype (optional)
|
||||||
|
* @return array
|
||||||
|
* $ret['success'] boolean
|
||||||
|
* $ret['results'] amount of found results, or false
|
||||||
|
* $ret['message'] string with error messages if any
|
||||||
|
*/
|
||||||
function attach_count_files($channel_id, $observer, $hash = '', $filename = '', $filetype = '') {
|
function attach_count_files($channel_id, $observer, $hash = '', $filename = '', $filetype = '') {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -121,6 +142,22 @@ function attach_count_files($channel_id, $observer, $hash = '', $filename = '',
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a list of files/attachments.
|
||||||
|
*
|
||||||
|
* @param $channel_id
|
||||||
|
* @param $observer
|
||||||
|
* @param $hash (optional)
|
||||||
|
* @param $filename (optional)
|
||||||
|
* @param $filetype (optional)
|
||||||
|
* @param $orderby
|
||||||
|
* @param $start
|
||||||
|
* @param $entries
|
||||||
|
* @return array
|
||||||
|
* $ret['success'] boolean
|
||||||
|
* $ret['results'] array with results, or false
|
||||||
|
* $ret['message'] string with error messages if any
|
||||||
|
*/
|
||||||
function attach_list_files($channel_id, $observer, $hash = '', $filename = '', $filetype = '', $orderby = 'created desc', $start = 0, $entries = 0) {
|
function attach_list_files($channel_id, $observer, $hash = '', $filename = '', $filetype = '', $orderby = 'created desc', $start = 0, $entries = 0) {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -157,9 +194,16 @@ function attach_list_files($channel_id, $observer, $hash = '', $filename = '', $
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find an attachment by hash and revision. Returns the entire attach structure including data.
|
/**
|
||||||
// This could exhaust memory so most useful only when immediately sending the data.
|
* @brief Find an attachment by hash and revision.
|
||||||
|
*
|
||||||
|
* Returns the entire attach structure including data.
|
||||||
|
*
|
||||||
|
* This could exhaust memory so most useful only when immediately sending the data.
|
||||||
|
*
|
||||||
|
* @param $hash
|
||||||
|
* @param $rev
|
||||||
|
*/
|
||||||
function attach_by_hash($hash, $rev = 0) {
|
function attach_by_hash($hash, $rev = 0) {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -190,7 +234,6 @@ function attach_by_hash($hash,$rev = 0) {
|
|||||||
|
|
||||||
// Now we'll see if we can access the attachment
|
// Now we'll see if we can access the attachment
|
||||||
|
|
||||||
|
|
||||||
$r = q("SELECT * FROM attach WHERE hash = '%s' and uid = %d $sql_extra LIMIT 1",
|
$r = q("SELECT * FROM attach WHERE hash = '%s' and uid = %d $sql_extra LIMIT 1",
|
||||||
dbesc($hash),
|
dbesc($hash),
|
||||||
intval($r[0]['uid'])
|
intval($r[0]['uid'])
|
||||||
@ -207,8 +250,15 @@ function attach_by_hash($hash,$rev = 0) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Find an attachment by hash and revision.
|
||||||
|
*
|
||||||
|
* Returns the entire attach structure excluding data.
|
||||||
|
*
|
||||||
|
* @see attach_by_hash()
|
||||||
|
* @param $hash
|
||||||
|
* @param $ref
|
||||||
|
*/
|
||||||
function attach_by_hash_nodata($hash, $rev = 0) {
|
function attach_by_hash_nodata($hash, $rev = 0) {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -254,12 +304,16 @@ function attach_by_hash_nodata($hash,$rev = 0) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param $channel channel array of owner
|
||||||
|
* @param $observer_hash hash of current observer
|
||||||
|
* @param $options (optional)
|
||||||
|
* @param $arr (optional)
|
||||||
|
*/
|
||||||
function attach_store($channel, $observer_hash, $options = '', $arr = null) {
|
function attach_store($channel, $observer_hash, $options = '', $arr = null) {
|
||||||
|
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
$channel_id = $channel['channel_id'];
|
$channel_id = $channel['channel_id'];
|
||||||
$sql_options = '';
|
$sql_options = '';
|
||||||
@ -379,7 +433,6 @@ function attach_store($channel,$observer_hash,$options = '',$arr = null) {
|
|||||||
dbesc($x[0]['deny_gid'])
|
dbesc($x[0]['deny_gid'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
elseif($options === 'update') {
|
elseif($options === 'update') {
|
||||||
$r = q("update attach set filename = '%s', filetype = '%s', edited = '%s',
|
$r = q("update attach set filename = '%s', filetype = '%s', edited = '%s',
|
||||||
allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where id = %d and uid = %d limit 1",
|
allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where id = %d and uid = %d limit 1",
|
||||||
@ -394,7 +447,6 @@ function attach_store($channel,$observer_hash,$options = '',$arr = null) {
|
|||||||
intval($x[0]['uid'])
|
intval($x[0]['uid'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
$r = q("INSERT INTO attach ( aid, uid, hash, creator, filename, filetype, filesize, revision, data, created, edited, allow_cid, allow_gid,deny_cid, deny_gid )
|
$r = q("INSERT INTO attach ( aid, uid, hash, creator, filename, filetype, filesize, revision, data, created, edited, allow_cid, allow_gid,deny_cid, deny_gid )
|
||||||
VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
|
VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
|
||||||
@ -441,12 +493,11 @@ function attach_store($channel,$observer_hash,$options = '',$arr = null) {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a virtual directory and return contents, checking permissions of all parent components.
|
* Read a virtual directory and return contents, checking permissions of all parent components.
|
||||||
* @function z_readdir
|
* @function z_readdir
|
||||||
* @param integer $channel_id
|
* @param integer $channel_id
|
||||||
* @param string $observer_hash
|
* @param string $observer_hash hash of current observer
|
||||||
* @param string $pathname
|
* @param string $pathname
|
||||||
* @param string $parent_hash (optional)
|
* @param string $parent_hash (optional)
|
||||||
*
|
*
|
||||||
@ -455,7 +506,6 @@ function attach_store($channel,$observer_hash,$options = '',$arr = null) {
|
|||||||
* $ret['message'] = error message if success is false
|
* $ret['message'] = error message if success is false
|
||||||
* $ret['data'] = array of attach DB entries without data component
|
* $ret['data'] = array of attach DB entries without data component
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function z_readdir($channel_id, $observer_hash, $pathname, $parent_hash = '') {
|
function z_readdir($channel_id, $observer_hash, $pathname, $parent_hash = '') {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -464,7 +514,6 @@ function z_readdir($channel_id,$observer_hash,$pathname, $parent_hash = '') {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(strpos($pathname, '/')) {
|
if(strpos($pathname, '/')) {
|
||||||
$paths = explode('/', $pathname);
|
$paths = explode('/', $pathname);
|
||||||
if(count($paths) > 1) {
|
if(count($paths) > 1) {
|
||||||
@ -501,20 +550,17 @@ function z_readdir($channel_id,$observer_hash,$pathname, $parent_hash = '') {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function attach_mkdir($channel,$observer_hash,$arr);
|
* @function attach_mkdir($channel,$observer_hash,$arr);
|
||||||
*
|
*
|
||||||
* Create directory
|
* @brief Create directory.
|
||||||
*
|
*
|
||||||
* @param $channel channel array of owner
|
* @param $channel channel array of owner
|
||||||
* @param $observer_hash hash of current observer
|
* @param $observer_hash hash of current observer
|
||||||
* @param $arr parameter array to fulfil request
|
* @param $arr parameter array to fulfil request
|
||||||
*
|
|
||||||
* Required:
|
* Required:
|
||||||
* $arr['filename']
|
* $arr['filename']
|
||||||
* $arr['folder'] // hash of parent directory, empty string for root directory
|
* $arr['folder'] // hash of parent directory, empty string for root directory
|
||||||
*
|
|
||||||
* Optional:
|
* Optional:
|
||||||
* $arr['hash'] // precumputed hash for this node
|
* $arr['hash'] // precumputed hash for this node
|
||||||
* $arr['allow_cid']
|
* $arr['allow_cid']
|
||||||
@ -522,7 +568,6 @@ function z_readdir($channel_id,$observer_hash,$pathname, $parent_hash = '') {
|
|||||||
* $arr['deny_cid']
|
* $arr['deny_cid']
|
||||||
* $arr['deny_gid']
|
* $arr['deny_gid']
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function attach_mkdir($channel, $observer_hash, $arr = null) {
|
function attach_mkdir($channel, $observer_hash, $arr = null) {
|
||||||
|
|
||||||
$ret = array('success' => false);
|
$ret = array('success' => false);
|
||||||
@ -536,7 +581,6 @@ function attach_mkdir($channel,$observer_hash,$arr = null) {
|
|||||||
if(! is_dir($basepath))
|
if(! is_dir($basepath))
|
||||||
mkdir($basepath,STORAGE_DEFAULT_PERMISSIONS, true);
|
mkdir($basepath,STORAGE_DEFAULT_PERMISSIONS, true);
|
||||||
|
|
||||||
|
|
||||||
if(! perm_is_allowed($channel_id, $observer_hash, 'write_storage')) {
|
if(! perm_is_allowed($channel_id, $observer_hash, 'write_storage')) {
|
||||||
$ret['message'] = t('Permission denied.');
|
$ret['message'] = t('Permission denied.');
|
||||||
return $ret;
|
return $ret;
|
||||||
@ -547,10 +591,8 @@ function attach_mkdir($channel,$observer_hash,$arr = null) {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$arr['hash'] = (($arr['hash']) ? $arr['hash'] : random_string());
|
$arr['hash'] = (($arr['hash']) ? $arr['hash'] : random_string());
|
||||||
|
|
||||||
|
|
||||||
// Check for duplicate name.
|
// Check for duplicate name.
|
||||||
// Check both the filename and the hash as we will be making use of both.
|
// Check both the filename and the hash as we will be making use of both.
|
||||||
|
|
||||||
@ -576,7 +618,6 @@ function attach_mkdir($channel,$observer_hash,$arr = null) {
|
|||||||
$sql_options = permissions_sql($channel['channel_id']);
|
$sql_options = permissions_sql($channel['channel_id']);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
$r = q("select filename, hash, flags, folder from attach where uid = %d and hash = '%s' and ( flags & %d )
|
$r = q("select filename, hash, flags, folder from attach where uid = %d and hash = '%s' and ( flags & %d )
|
||||||
$sql_options limit 1",
|
$sql_options limit 1",
|
||||||
intval($channel['channel_id']),
|
intval($channel['channel_id']),
|
||||||
@ -594,7 +635,6 @@ function attach_mkdir($channel,$observer_hash,$arr = null) {
|
|||||||
$lfile = $r[0]['folder'];
|
$lfile = $r[0]['folder'];
|
||||||
} while ( ($r[0]['folder']) && ($r[0]['flags'] & ATTACH_FLAG_DIR)) ;
|
} while ( ($r[0]['folder']) && ($r[0]['flags'] & ATTACH_FLAG_DIR)) ;
|
||||||
$path = $basepath . '/' . $lpath;
|
$path = $basepath . '/' . $lpath;
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$path = $basepath . '/';
|
$path = $basepath . '/';
|
||||||
@ -641,8 +681,17 @@ function attach_mkdir($channel,$observer_hash,$arr = null) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Changes permissions of a file.
|
||||||
|
*
|
||||||
|
* @param $channel_id
|
||||||
|
* @param $resource
|
||||||
|
* @param $allow_cid
|
||||||
|
* @param $allow_gid
|
||||||
|
* @param $deny_cid
|
||||||
|
* @param $deny_gid
|
||||||
|
* @param $recurse
|
||||||
|
*/
|
||||||
function attach_change_permissions($channel_id, $resource, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $recurse = false) {
|
function attach_change_permissions($channel_id, $resource, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $recurse = false) {
|
||||||
|
|
||||||
$r = q("select hash, flags from attach where hash = '%s' and uid = %d limit 1",
|
$r = q("select hash, flags from attach where hash = '%s' and uid = %d limit 1",
|
||||||
@ -679,8 +728,12 @@ function attach_change_permissions($channel_id,$resource,$allow_cid,$allow_gid,$
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Delete a file.
|
||||||
|
*
|
||||||
|
* @param $channel_id
|
||||||
|
* @param $resource
|
||||||
|
*/
|
||||||
function attach_delete($channel_id, $resource) {
|
function attach_delete($channel_id, $resource) {
|
||||||
|
|
||||||
|
|
||||||
@ -733,8 +786,12 @@ function attach_delete($channel_id,$resource) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns path to file in cloud/.
|
||||||
|
*
|
||||||
|
* @param $arr
|
||||||
|
* @return string with the path the file to cloud/
|
||||||
|
*/
|
||||||
function get_cloudpath($arr) {
|
function get_cloudpath($arr) {
|
||||||
|
|
||||||
$basepath = 'cloud/';
|
$basepath = 'cloud/';
|
||||||
@ -746,7 +803,6 @@ function get_cloudpath($arr) {
|
|||||||
$basepath .= $r[0]['channel_address'] . '/';
|
$basepath .= $r[0]['channel_address'] . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$path = $basepath;
|
$path = $basepath;
|
||||||
|
|
||||||
if($arr['folder']) {
|
if($arr['folder']) {
|
||||||
@ -772,17 +828,17 @@ function get_cloudpath($arr) {
|
|||||||
} while ( ($r[0]['folder']) && ($r[0]['flags'] & ATTACH_FLAG_DIR)) ;
|
} while ( ($r[0]['folder']) && ($r[0]['flags'] & ATTACH_FLAG_DIR)) ;
|
||||||
|
|
||||||
$path .= $lpath;
|
$path .= $lpath;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$path .= $arr['filename'];
|
$path .= $arr['filename'];
|
||||||
return $path;
|
return $path;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param $in
|
||||||
|
* @param $out
|
||||||
|
*/
|
||||||
function pipe_streams($in, $out) {
|
function pipe_streams($in, $out) {
|
||||||
$size = 0;
|
$size = 0;
|
||||||
while (!feof($in))
|
while (!feof($in))
|
||||||
|
Reference in New Issue
Block a user