summaryrefslogtreecommitdiff
path: root/inc/media.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/media.php')
-rw-r--r--inc/media.php513
1 files changed, 403 insertions, 110 deletions
diff --git a/inc/media.php b/inc/media.php
index d69426414..81081d5dc 100644
--- a/inc/media.php
+++ b/inc/media.php
@@ -16,6 +16,9 @@ if(!defined('NL')) define('NL',"\n");
* their CSS tags except pagenames won't be links.
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
+ *
+ * @param array $data
+ * @param string $id
*/
function media_filesinuse($data,$id){
global $lang;
@@ -41,6 +44,11 @@ function media_filesinuse($data,$id){
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $id media id
+ * @param int $auth permission level
+ * @param array $data
+ * @return false|string
*/
function media_metasave($id,$auth,$data){
if($auth < AUTH_UPLOAD) return false;
@@ -62,7 +70,7 @@ function media_metasave($id,$auth,$data){
}
$old = @filemtime($src);
- if(!@file_exists(mediaFN($id, $old)) && @file_exists($src)) {
+ if(!file_exists(mediaFN($id, $old)) && file_exists($src)) {
// add old revision to the attic
media_saveOldRevision($id);
}
@@ -86,6 +94,7 @@ function media_metasave($id,$auth,$data){
* check if a media is external source
*
* @author Gerrit Uitslag <klapinklapin@gmail.com>
+ *
* @param string $id the media ID or URL
* @return bool
*/
@@ -98,6 +107,7 @@ function media_isexternal($id){
* Check if a media item is public (eg, external URL or readable by @ALL)
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
* @param string $id the media ID or URL
* @return bool
*/
@@ -113,9 +123,13 @@ function media_ispublic($id){
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $id media id
+ * @param int $auth permission level
+ * @return bool
*/
function media_metaform($id,$auth){
- global $lang, $config_cascade;
+ global $lang;
if($auth < AUTH_UPLOAD) {
echo '<div class="nothing">'.$lang['media_perm_upload'].'</div>'.NL;
@@ -127,7 +141,7 @@ function media_metaform($id,$auth){
if(is_null($fields)){
$config_files = getConfigFiles('mediameta');
foreach ($config_files as $config_file) {
- if(@file_exists($config_file)) include($config_file);
+ if(file_exists($config_file)) include($config_file);
}
}
@@ -167,16 +181,21 @@ function media_metaform($id,$auth){
$form->addElement(form_makeButton('submit', '', $lang['btn_save'], array('accesskey' => 's', 'name' => 'mediado[save]')));
$form->addElement('</div>'.NL);
$form->printForm();
+
+ return true;
}
/**
* Convenience function to check if a media file is still in use
*
* @author Michael Klier <chi@chimeric.de>
+ *
+ * @param string $id media id
+ * @return array|bool
*/
function media_inuse($id) {
global $conf;
- $mediareferences = array();
+
if($conf['refcheck']){
$mediareferences = ft_mediause($id,true);
if(!count($mediareferences)) {
@@ -199,7 +218,10 @@ define('DOKU_MEDIA_EMPTY_NS', 8);
*
* If configured, checks for media references before deletion
*
- * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $id media id
+ * @param int $auth no longer used
* @return int One of: 0,
* DOKU_MEDIA_DELETED,
* DOKU_MEDIA_DELETED | DOKU_MEDIA_EMPTY_NS,
@@ -208,23 +230,25 @@ define('DOKU_MEDIA_EMPTY_NS', 8);
*/
function media_delete($id,$auth){
global $lang;
+ $auth = auth_quickaclcheck(ltrim(getNS($id).':*', ':'));
if($auth < AUTH_DELETE) return DOKU_MEDIA_NOT_AUTH;
if(media_inuse($id)) return DOKU_MEDIA_INUSE;
$file = mediaFN($id);
// trigger an event - MEDIA_DELETE_FILE
+ $data = array();
$data['id'] = $id;
$data['name'] = utf8_basename($file);
$data['path'] = $file;
- $data['size'] = (@file_exists($file)) ? filesize($file) : 0;
+ $data['size'] = (file_exists($file)) ? filesize($file) : 0;
$data['unl'] = false;
$data['del'] = false;
$evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
if ($evt->advise_before()) {
$old = @filemtime($file);
- if(!@file_exists(mediaFN($id, $old)) && @file_exists($file)) {
+ if(!file_exists(mediaFN($id, $old)) && file_exists($file)) {
// add old revision to the attic
media_saveOldRevision($id);
}
@@ -248,14 +272,16 @@ function media_delete($id,$auth){
/**
* Handle file uploads via XMLHttpRequest
*
- * @return mixed false on error, id of the new file on success
+ * @param string $ns target namespace
+ * @param int $auth current auth check result
+ * @return false|string false on error, id of the new file on success
*/
function media_upload_xhr($ns,$auth){
if(!checkSecurityToken()) return false;
global $INPUT;
$id = $INPUT->get->str('qqfile');
- list($ext,$mime,$dl) = mimetype($id);
+ list($ext,$mime) = mimetype($id);
$input = fopen("php://input", "r");
if (!($tmp = io_mktmpdir())) return false;
$path = $tmp.'/'.md5($id);
@@ -278,7 +304,7 @@ function media_upload_xhr($ns,$auth){
'copy'
);
unlink($path);
- if ($tmp) dir_delete($tmp);
+ if ($tmp) io_rmdir($tmp, true);
if (is_array($res)) {
msg($res[0], $res[1]);
return false;
@@ -291,7 +317,11 @@ function media_upload_xhr($ns,$auth){
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Michael Klier <chi@chimeric.de>
- * @return mixed false on error, id of the new file on success
+ *
+ * @param string $ns target namespace
+ * @param int $auth current auth check result
+ * @param bool|array $file $_FILES member, $_FILES['upload'] if false
+ * @return false|string false on error, id of the new file on success
*/
function media_upload($ns,$auth,$file=false){
if(!checkSecurityToken()) return false;
@@ -307,8 +337,8 @@ function media_upload($ns,$auth,$file=false){
if($file['error']) return false;
// check extensions
- list($fext,$fmime,$dl) = mimetype($file['name']);
- list($iext,$imime,$dl) = mimetype($id);
+ list($fext,$fmime) = mimetype($file['name']);
+ list($iext,$imime) = mimetype($id);
if($fext && !$iext){
// no extension specified in id - read original one
$id .= '.'.$fext;
@@ -335,6 +365,7 @@ function media_upload($ns,$auth,$file=false){
* Using copy, makes sure any setgid bits on the media directory are honored
*
* @see move_uploaded_file()
+ *
* @param string $from
* @param string $to
* @return bool
@@ -352,13 +383,21 @@ function copy_uploaded_file($from, $to){
* (The triggered event is preventable.)
*
* Event data:
- * $data[0] fn_tmp: the temporary file name (read from $_FILES)
- * $data[1] fn: the file name of the uploaded file
- * $data[2] id: the future directory id of the uploaded file
- * $data[3] imime: the mimetype of the uploaded file
+ * $data[0] fn_tmp: the temporary file name (read from $_FILES)
+ * $data[1] fn: the file name of the uploaded file
+ * $data[2] id: the future directory id of the uploaded file
+ * $data[3] imime: the mimetype of the uploaded file
* $data[4] overwrite: if an existing file is going to be overwritten
+ * $data[5] move: name of function that performs move/copy/..
*
* @triggers MEDIA_UPLOAD_FINISH
+ *
+ * @param array $file
+ * @param string $id media id
+ * @param bool $ow overwrite?
+ * @param int $auth permission level
+ * @param string $move name of functions that performs move/copy/..
+ * @return false|array|string
*/
function media_save($file, $id, $ow, $auth, $move) {
if($auth < AUTH_UPLOAD) {
@@ -392,7 +431,7 @@ function media_save($file, $id, $ow, $auth, $move) {
}
//check for overwrite
- $overwrite = @file_exists($fn);
+ $overwrite = file_exists($fn);
$auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
if($overwrite && (!$ow || $auth < $auth_ow)) {
return array($lang['uploadexist'], 0);
@@ -408,6 +447,7 @@ function media_save($file, $id, $ow, $auth, $move) {
}
// prepare event data
+ $data = array();
$data[0] = $file['name'];
$data[1] = $fn;
$data[2] = $id;
@@ -420,8 +460,12 @@ function media_save($file, $id, $ow, $auth, $move) {
}
/**
- * Callback adapter for media_upload_finish()
+ * Callback adapter for media_upload_finish() triggered by MEDIA_UPLOAD_FINISH
+ *
* @author Michael Klier <chi@chimeric.de>
+ *
+ * @param array $data event data
+ * @return false|array|string
*/
function _media_upload_action($data) {
// fixme do further sanity tests of given data?
@@ -438,6 +482,14 @@ function _media_upload_action($data) {
* @author Andreas Gohr <andi@splitbrain.org>
* @author Michael Klier <chi@chimeric.de>
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $fn_tmp
+ * @param string $fn
+ * @param string $id media id
+ * @param string $imime mime type
+ * @param bool $overwrite overwrite existing?
+ * @param string $move function name
+ * @return array|string
*/
function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'move_uploaded_file') {
global $conf;
@@ -445,7 +497,7 @@ function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'mov
global $REV;
$old = @filemtime($fn);
- if(!@file_exists(mediaFN($id, $old)) && @file_exists($fn)) {
+ if(!file_exists(mediaFN($id, $old)) && file_exists($fn)) {
// add old revision to the attic if missing
media_saveOldRevision($id);
}
@@ -481,6 +533,7 @@ function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'mov
* directory
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
* @param string $id
* @return int - revision date
*/
@@ -488,14 +541,15 @@ function media_saveOldRevision($id){
global $conf, $lang;
$oldf = mediaFN($id);
- if(!@file_exists($oldf)) return '';
+ if(!file_exists($oldf)) return '';
$date = filemtime($oldf);
if (!$conf['mediarevisions']) return $date;
- if (!getRevisionInfo($id, $date, 8192, true)) {
+ $medialog = new MediaChangeLog($id);
+ if (!$medialog->getRevisionInfo($date)) {
// there was an external edit,
// there is no log entry for current version of file
- if (!@file_exists(mediaMetaFN($id,'.changes'))) {
+ if (!file_exists(mediaMetaFN($id,'.changes'))) {
addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
} else {
addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_EDIT);
@@ -523,6 +577,10 @@ function media_saveOldRevision($id){
* @author Andreas Gohr <andi@splitbrain.org>
* @link http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
* @fixme check all 26 magic IE filetypes here?
+ *
+ * @param string $file path to file
+ * @param string $mime mimetype
+ * @return int
*/
function media_contentcheck($file,$mime){
global $conf;
@@ -532,14 +590,14 @@ function media_contentcheck($file,$mime){
$bytes = fread($fh, 256);
fclose($fh);
if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
- return -3;
+ return -3; //XSS: possibly malicious content
}
}
}
if(substr($mime,0,6) == 'image/'){
$info = @getimagesize($file);
if($mime == 'image/gif' && $info[2] != 1){
- return -1;
+ return -1; // uploaded content did not match the file extension
}elseif($mime == 'image/jpeg' && $info[2] != 2){
return -1;
}elseif($mime == 'image/png' && $info[2] != 3){
@@ -550,7 +608,7 @@ function media_contentcheck($file,$mime){
global $TEXT;
$TEXT = io_readFile($file);
if(checkwordblock()){
- return -2;
+ return -2; //blocked by the spam blacklist
}
}
return 0;
@@ -560,17 +618,29 @@ function media_contentcheck($file,$mime){
* Send a notify mail on uploads
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $id media id
+ * @param string $file path to file
+ * @param string $mime mime type
+ * @param bool|int $old_rev revision timestamp or false
+ * @return bool
*/
function media_notify($id,$file,$mime,$old_rev=false){
global $conf;
- if(empty($conf['notify'])) return; //notify enabled?
+ if(empty($conf['notify'])) return false; //notify enabled?
$subscription = new Subscription();
- return $subscription->send_media_diff($conf['notify'], 'uploadmail', $id, $old_rev, '');
+ return $subscription->send_media_diff($conf['notify'], 'uploadmail', $id, $old_rev);
}
/**
* List all files in a given Media namespace
+ *
+ * @param string $ns namespace
+ * @param null|int $auth permission level
+ * @param string $jump id
+ * @param bool $fullscreenview
+ * @param bool|string $sort sorting order, false skips sorting
*/
function media_filelist($ns,$auth=null,$jump='',$fullscreenview=false,$sort=false){
global $conf;
@@ -640,7 +710,8 @@ function media_tabs_files($selected_tab = ''){
* Prints tabs for files details actions
*
* @author Kate Arzamastseva <pshns@ukr.net>
- * @param string $selected_tab - opened tab
+ * @param string $image filename of the current image
+ * @param string $selected_tab opened tab
*/
function media_tabs_details($image, $selected_tab = ''){
global $lang, $conf;
@@ -649,8 +720,8 @@ function media_tabs_details($image, $selected_tab = ''){
$tabs['view'] = array('href' => media_managerURL(array('tab_details' => 'view'), '&'),
'caption' => $lang['media_viewtab']);
- list($ext, $mime) = mimetype($image);
- if ($mime == 'image/jpeg' && @file_exists(mediaFN($image))) {
+ list(, $mime) = mimetype($image);
+ if ($mime == 'image/jpeg' && file_exists(mediaFN($image))) {
$tabs['edit'] = array('href' => media_managerURL(array('tab_details' => 'edit'), '&'),
'caption' => $lang['media_edittab']);
}
@@ -669,7 +740,6 @@ function media_tabs_details($image, $selected_tab = ''){
*/
function media_tab_files_options(){
global $lang;
- global $NS;
global $INPUT;
global $ID;
$form = new Doku_Form(array('class' => 'options', 'method' => 'get',
@@ -695,7 +765,7 @@ function media_tab_files_options(){
if ($checked == $option) {
$attrs['checked'] = 'checked';
}
- $form->addElement(form_makeRadioField($group, $option,
+ $form->addElement(form_makeRadioField($group . '_dwmedia', $option,
$lang['media_' . $group . '_' . $option],
$content[0] . '__' . $option,
$option, $attrs));
@@ -713,16 +783,31 @@ function media_tab_files_options(){
* Returns type of sorting for the list of files in media manager
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
* @return string - sort type
*/
function _media_get_sort_type() {
return _media_get_display_param('sort', array('default' => 'name', 'date'));
}
+/**
+ * Returns type of listing for the list of files in media manager
+ *
+ * @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @return string - list type
+ */
function _media_get_list_type() {
return _media_get_display_param('list', array('default' => 'thumbs', 'rows'));
}
+/**
+ * Get display parameters
+ *
+ * @param string $param name of parameter
+ * @param array $values allowed values, where default value has index key 'default'
+ * @return string the parameter value
+ */
function _media_get_display_param($param, $values) {
global $INPUT;
if (in_array($INPUT->str($param), $values)) {
@@ -741,6 +826,10 @@ function _media_get_display_param($param, $values) {
* Prints tab that displays a list of all files
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $ns
+ * @param null|int $auth permission level
+ * @param string $jump item id
*/
function media_tab_files($ns,$auth=null,$jump='') {
global $lang;
@@ -757,6 +846,10 @@ function media_tab_files($ns,$auth=null,$jump='') {
* Prints tab that displays uploading form
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $ns
+ * @param null|int $auth permission level
+ * @param string $jump item id
*/
function media_tab_upload($ns,$auth=null,$jump='') {
global $lang;
@@ -774,9 +867,11 @@ function media_tab_upload($ns,$auth=null,$jump='') {
* Prints tab that displays search form
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $ns
+ * @param null|int $auth permission level
*/
function media_tab_search($ns,$auth=null) {
- global $lang;
global $INPUT;
$do = $INPUT->str('mediado');
@@ -794,9 +889,14 @@ function media_tab_search($ns,$auth=null) {
* Prints tab that displays mediafile details
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image media id
+ * @param string $ns
+ * @param null|int $auth permission level
+ * @param string|int $rev revision timestamp or empty string
*/
-function media_tab_view($image, $ns, $auth=null, $rev=false) {
- global $lang, $conf;
+function media_tab_view($image, $ns, $auth=null, $rev='') {
+ global $lang;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if ($image && $auth >= AUTH_READ) {
@@ -814,13 +914,16 @@ function media_tab_view($image, $ns, $auth=null, $rev=false) {
* Prints tab that displays form for editing mediafile metadata
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image media id
+ * @param string $ns
+ * @param null|int $auth permission level
*/
function media_tab_edit($image, $ns, $auth=null) {
- global $lang;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if ($image) {
- list($ext, $mime) = mimetype($image);
+ list(, $mime) = mimetype($image);
if ($mime == 'image/jpeg') media_metaform($image,$auth);
}
}
@@ -829,6 +932,10 @@ function media_tab_edit($image, $ns, $auth=null) {
* Prints tab that displays mediafile revisions
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image media id
+ * @param string $ns
+ * @param null|int $auth permission level
*/
function media_tab_history($image, $ns, $auth=null) {
global $lang;
@@ -852,9 +959,14 @@ function media_tab_history($image, $ns, $auth=null) {
/**
* Prints mediafile details
*
+ * @param string $image media id
+ * @param int $auth permission level
+ * @param int|string $rev revision timestamp or empty string
+ * @param JpegMeta|bool $meta
+ *
* @author Kate Arzamastseva <pshns@ukr.net>
*/
-function media_preview($image, $auth, $rev=false, $meta=false) {
+function media_preview($image, $auth, $rev='', $meta=false) {
$size = media_image_preview_size($image, $rev, $meta);
@@ -886,13 +998,17 @@ function media_preview($image, $auth, $rev=false, $meta=false) {
* Prints mediafile action buttons
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image media id
+ * @param int $auth permission level
+ * @param string|int $rev revision timestamp, or empty string
*/
-function media_preview_buttons($image, $auth, $rev=false) {
+function media_preview_buttons($image, $auth, $rev='') {
global $lang, $conf;
echo '<ul class="actions">'.NL;
- if($auth >= AUTH_DELETE && !$rev && @file_exists(mediaFN($image))){
+ if($auth >= AUTH_DELETE && !$rev && file_exists(mediaFN($image))){
// delete button
$form = new Doku_Form(array('id' => 'mediamanager__btn_delete',
@@ -915,7 +1031,7 @@ function media_preview_buttons($image, $auth, $rev=false) {
echo '</li>'.NL;
}
- if($auth >= AUTH_UPLOAD && $rev && $conf['mediarevisions'] && @file_exists(mediaFN($image, $rev))){
+ if($auth >= AUTH_UPLOAD && $rev && $conf['mediarevisions'] && file_exists(mediaFN($image, $rev))){
// restore button
$form = new Doku_Form(array('id' => 'mediamanager__btn_restore',
@@ -935,10 +1051,11 @@ function media_preview_buttons($image, $auth, $rev=false) {
* Returns image width and height for mediamanager preview panel
*
* @author Kate Arzamastseva <pshns@ukr.net>
- * @param string $image
- * @param int $rev
- * @param JpegMeta $meta
- * @return array
+ * @param string $image
+ * @param int|string $rev
+ * @param JpegMeta|bool $meta
+ * @param int $size
+ * @return array|false
*/
function media_image_preview_size($image, $rev, $meta, $size = 500) {
if (!preg_match("/\.(jpe?g|gif|png)$/", $image) || !file_exists(mediaFN($image, $rev))) return false;
@@ -959,9 +1076,10 @@ function media_image_preview_size($image, $rev, $meta, $size = 500) {
* Returns the requested EXIF/IPTC tag from the image meta
*
* @author Kate Arzamastseva <pshns@ukr.net>
- * @param array $tags
+ *
+ * @param array $tags array with tags, first existing is returned
* @param JpegMeta $meta
- * @param string $alt
+ * @param string $alt alternative value
* @return string
*/
function media_getTag($tags,$meta,$alt=''){
@@ -975,18 +1093,17 @@ function media_getTag($tags,$meta,$alt=''){
* Returns mediafile tags
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
* @param JpegMeta $meta
- * @return array
+ * @return array list of tags of the mediafile
*/
function media_file_tags($meta) {
- global $config_cascade;
-
// load the field descriptions
static $fields = null;
if(is_null($fields)){
$config_files = getConfigFiles('mediameta');
foreach ($config_files as $config_file) {
- if(@file_exists($config_file)) include($config_file);
+ if(file_exists($config_file)) include($config_file);
}
}
@@ -995,7 +1112,7 @@ function media_file_tags($meta) {
foreach($fields as $key => $tag){
$t = array();
if (!empty($tag[0])) $t = array($tag[0]);
- if(is_array($tag[3])) $t = array_merge($t,$tag[3]);
+ if(isset($tag[3]) && is_array($tag[3])) $t = array_merge($t,$tag[3]);
$value = media_getTag($t, $meta);
$tags[] = array('tag' => $tag, 'value' => $value);
}
@@ -1007,8 +1124,13 @@ function media_file_tags($meta) {
* Prints mediafile tags
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image image id
+ * @param int $auth permission level
+ * @param string|int $rev revision timestamp, or empty string
+ * @param bool|JpegMeta $meta image object, or create one if false
*/
-function media_details($image, $auth, $rev=false, $meta=false) {
+function media_details($image, $auth, $rev='', $meta=false) {
global $lang;
if (!$meta) $meta = new JpegMeta(mediaFN($image, $rev));
@@ -1018,7 +1140,7 @@ function media_details($image, $auth, $rev=false, $meta=false) {
foreach($tags as $tag){
if ($tag['value']) {
$value = cleanText($tag['value']);
- echo '<dt>'.$lang[$tag['tag'][1]].':</dt><dd>';
+ echo '<dt>'.$lang[$tag['tag'][1]].'</dt><dd>';
if ($tag['tag'][2] == 'date') echo dformat($value);
else echo hsc($value);
echo '</dd>'.NL;
@@ -1031,9 +1153,14 @@ function media_details($image, $auth, $rev=false, $meta=false) {
* Shows difference between two revisions of file
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image image id
+ * @param string $ns
+ * @param int $auth permission level
+ * @param bool $fromajax
+ * @return false|null|string
*/
function media_diff($image, $ns, $auth, $fromajax = false) {
- global $lang;
global $conf;
global $INPUT;
@@ -1071,7 +1198,8 @@ function media_diff($image, $ns, $auth, $fromajax = false) {
$l_rev = $rev1;
}else{ // no revision was given, compare previous to current
$r_rev = '';
- $revs = getRevisions($image, 0, 1, 8192, true);
+ $medialog = new MediaChangeLog($image);
+ $revs = $medialog->getRevisions(0, 1);
if (file_exists(mediaFN($image, $revs[0]))) {
$l_rev = $revs[0];
} else {
@@ -1080,6 +1208,7 @@ function media_diff($image, $ns, $auth, $fromajax = false) {
}
// prepare event data
+ $data = array();
$data[0] = $image;
$data[1] = $l_rev;
$data[2] = $r_rev;
@@ -1089,12 +1218,17 @@ function media_diff($image, $ns, $auth, $fromajax = false) {
// trigger event
return trigger_event('MEDIA_DIFF', $data, '_media_file_diff', true);
-
}
+/**
+ * Callback for media file diff
+ *
+ * @param array $data event data
+ * @return false|null
+ */
function _media_file_diff($data) {
if(is_array($data) && count($data)===6) {
- return media_file_diff($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]);
+ media_file_diff($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]);
} else {
return false;
}
@@ -1104,16 +1238,22 @@ function _media_file_diff($data) {
* Shows difference between two revisions of image
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $image
+ * @param string|int $l_rev revision timestamp, or empty string
+ * @param string|int $r_rev revision timestamp, or empty string
+ * @param string $ns
+ * @param int $auth permission level
+ * @param bool $fromajax
*/
function media_file_diff($image, $l_rev, $r_rev, $ns, $auth, $fromajax){
global $lang;
- global $config_cascade;
global $INPUT;
$l_meta = new JpegMeta(mediaFN($image, $l_rev));
$r_meta = new JpegMeta(mediaFN($image, $r_rev));
- $is_img = preg_match("/\.(jpe?g|gif|png)$/", $image);
+ $is_img = preg_match('/\.(jpe?g|gif|png)$/', $image);
if ($is_img) {
$l_size = media_image_preview_size($image, $l_rev, $l_meta);
$r_size = media_image_preview_size($image, $r_rev, $r_meta);
@@ -1140,7 +1280,7 @@ function media_file_diff($image, $l_rev, $r_rev, $ns, $auth, $fromajax){
if ($difftype == 'opacity' || $difftype == 'portions') {
media_image_diff($image, $l_rev, $r_rev, $l_size, $r_size, $difftype);
if (!$fromajax) echo '</div>';
- return '';
+ return;
}
}
@@ -1196,7 +1336,7 @@ function media_file_diff($image, $l_rev, $r_rev, $ns, $auth, $fromajax){
foreach($tags as $tag){
$value = cleanText($tag['value']);
if (!$value) $value = '-';
- echo '<dt>'.$lang[$tag['tag'][1]].':</dt>';
+ echo '<dt>'.$lang[$tag['tag'][1]].'</dt>';
echo '<dd>';
if ($tag['highlighted']) {
echo '<strong>';
@@ -1225,11 +1365,12 @@ function media_file_diff($image, $l_rev, $r_rev, $ns, $auth, $fromajax){
* and slider
*
* @author Kate Arzamastseva <pshns@ukr.net>
- * @param string $image
- * @param int $l_rev
- * @param int $r_rev
- * @param array $l_size
- * @param array $r_size
+ *
+ * @param string $image image id
+ * @param int $l_rev revision timestamp, or empty string
+ * @param int $r_rev revision timestamp, or empty string
+ * @param array $l_size array with width and height
+ * @param array $r_size array with width and height
* @param string $type
*/
function media_image_diff($image, $l_rev, $r_rev, $l_size, $r_size, $type) {
@@ -1262,10 +1403,11 @@ function media_image_diff($image, $l_rev, $r_rev, $l_size, $r_size, $type) {
/**
* Restores an old revision of a media file
*
- * @param string $image
- * @param int $rev
- * @param int $auth
+ * @param string $image media id
+ * @param int $rev revision timestamp or empty string
+ * @param int $auth
* @return string - file's id
+ *
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_restore($image, $rev, $auth){
@@ -1274,7 +1416,7 @@ function media_restore($image, $rev, $auth){
$removed = (!file_exists(mediaFN($image)) && file_exists(mediaMetaFN($image, '.changes')));
if (!$image || (!file_exists(mediaFN($image)) && !$removed)) return false;
if (!$rev || !file_exists(mediaFN($image, $rev))) return false;
- list($iext,$imime,$dl) = mimetype($image);
+ list(,$imime,) = mimetype($image);
$res = media_upload_finish(mediaFN($image, $rev),
mediaFN($image),
$image,
@@ -1295,19 +1437,24 @@ function media_restore($image, $rev, $auth){
* @author Andreas Gohr <gohr@cosmocode.de>
* @author Kate Arzamastseva <pshns@ukr.net>
* @triggers MEDIA_SEARCH
+ *
+ * @param string $query
+ * @param string $ns
+ * @param null|int $auth
+ * @param bool $fullscreen
+ * @param string $sort
*/
function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort='natural'){
global $conf;
global $lang;
$ns = cleanID($ns);
-
+ $evdata = array(
+ 'ns' => $ns,
+ 'data' => array(),
+ 'query' => $query
+ );
if ($query) {
- $evdata = array(
- 'ns' => $ns,
- 'data' => array(),
- 'query' => $query
- );
$evt = new Doku_Event('MEDIA_SEARCH', $evdata);
if ($evt->advise_before()) {
$dir = utf8_encodeFN(str_replace(':','/',$evdata['ns']));
@@ -1345,10 +1492,14 @@ function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort='natural
/**
* Formats and prints one file in the list
+ *
+ * @param array $item
+ * @param int $auth permission level
+ * @param string $jump item id
+ * @param bool $display_namespace
*/
function media_printfile($item,$auth,$jump,$display_namespace=false){
global $lang;
- global $conf;
// Prepare zebra coloring
// I always wanted to use this variable name :-D
@@ -1364,7 +1515,7 @@ function media_printfile($item,$auth,$jump,$display_namespace=false){
}
// Prepare fileicons
- list($ext,$mime,$dl) = mimetype($item['file'],false);
+ list($ext) = mimetype($item['file'],false);
$class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
$class = 'select mediafile mf_'.$class;
@@ -1419,27 +1570,36 @@ function media_printfile($item,$auth,$jump,$display_namespace=false){
echo '</div>'.NL;
}
-function media_printicon($filename){
- list($ext,$mime,$dl) = mimetype(mediaFN($filename),false);
+/**
+ * Display a media icon
+ *
+ * @param string $filename media id
+ * @param string $size the size subfolder, if not specified 16x16 is used
+ * @return string html
+ */
+function media_printicon($filename, $size=''){
+ list($ext) = mimetype(mediaFN($filename),false);
- if (@file_exists(DOKU_INC.'lib/images/fileicons/'.$ext.'.png')) {
- $icon = DOKU_BASE.'lib/images/fileicons/'.$ext.'.png';
+ if (file_exists(DOKU_INC.'lib/images/fileicons/'.$size.'/'.$ext.'.png')) {
+ $icon = DOKU_BASE.'lib/images/fileicons/'.$size.'/'.$ext.'.png';
} else {
- $icon = DOKU_BASE.'lib/images/fileicons/file.png';
+ $icon = DOKU_BASE.'lib/images/fileicons/'.$size.'/file.png';
}
return '<img src="'.$icon.'" alt="'.$filename.'" class="icon" />';
-
}
/**
* Formats and prints one file in the list in the thumbnails view
*
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param array $item
+ * @param int $auth permission level
+ * @param bool|string $jump item id
+ * @param bool $display_namespace
*/
function media_printfile_thumbs($item,$auth,$jump=false,$display_namespace=false){
- global $lang;
- global $conf;
// Prepare filename
$file = utf8_decodeFN($item['file']);
@@ -1455,7 +1615,7 @@ function media_printfile_thumbs($item,$auth,$jump=false,$display_namespace=false
echo '<a id="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'.
media_managerURL(array('image' => hsc($item['id']), 'ns' => getNS($item['id']),
'tab_details' => 'view')).'">';
- echo media_printicon($item['id']);
+ echo media_printicon($item['id'], '32x32');
echo '</a>';
}
echo '</dt>'.NL;
@@ -1485,6 +1645,9 @@ function media_printfile_thumbs($item,$auth,$jump=false,$display_namespace=false
/**
* Prints a thumbnail and metainfo
+ *
+ * @param array $item
+ * @param bool $fullscreen
*/
function media_printimgdetail($item, $fullscreen=false){
// prepare thumbnail
@@ -1548,16 +1711,17 @@ function media_printimgdetail($item, $fullscreen=false){
}
/**
- * Build link based on the current, adding/rewriting
- * parameters
+ * Build link based on the current, adding/rewriting parameters
*
* @author Kate Arzamastseva <pshns@ukr.net>
- * @param array $params
- * @param string $amp - separator
- * @return string - link
+ *
+ * @param array|bool $params
+ * @param string $amp separator
+ * @param bool $abs absolute url?
+ * @param bool $params_array return the parmeters array?
+ * @return string|array - link or link parameters
*/
function media_managerURL($params=false, $amp='&amp;', $abs=false, $params_array=false) {
- global $conf;
global $ID;
global $INPUT;
@@ -1586,6 +1750,10 @@ function media_managerURL($params=false, $amp='&amp;', $abs=false, $params_array
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $ns
+ * @param int $auth permission level
+ * @param bool $fullscreen
*/
function media_uploadform($ns, $auth, $fullscreen = false){
global $lang;
@@ -1620,10 +1788,10 @@ function media_uploadform($ns, $auth, $fullscreen = false){
$form->addElement(formSecurityToken());
$form->addHidden('ns', hsc($ns));
$form->addElement(form_makeOpenTag('p'));
- $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file'));
+ $form->addElement(form_makeFileField('upload', $lang['txt_upload'], 'upload__file'));
$form->addElement(form_makeCloseTag('p'));
$form->addElement(form_makeOpenTag('p'));
- $form->addElement(form_makeTextField('mediaid', noNS($id), $lang['txt_filename'].':', 'upload__name'));
+ $form->addElement(form_makeTextField('mediaid', noNS($id), $lang['txt_filename'], 'upload__name'));
$form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
$form->addElement(form_makeCloseTag('p'));
@@ -1673,6 +1841,10 @@ function media_getuploadsize(){
*
* @author Tobias Sarnowski <sarnowski@cosmocode.de>
* @author Kate Arzamastseva <pshns@ukr.net>
+ *
+ * @param string $ns
+ * @param string $query
+ * @param bool $fullscreen
*/
function media_searchform($ns,$query='',$fullscreen=false){
global $lang;
@@ -1700,6 +1872,8 @@ function media_searchform($ns,$query='',$fullscreen=false){
* Build a tree outline of available media namespaces
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $ns
*/
function media_nstree($ns){
global $conf;
@@ -1748,12 +1922,15 @@ function media_nstree($ns){
* Prints a media namespace tree item
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param array $item
+ * @return string html
*/
function media_nstree_item($item){
global $INPUT;
$pos = strrpos($item['id'], ':');
$label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
- if(!$item['label']) $item['label'] = $label;
+ if(empty($item['label'])) $item['label'] = $label;
$ret = '';
if (!($INPUT->str('do') == 'media'))
@@ -1771,6 +1948,9 @@ function media_nstree_item($item){
* Prints a media namespace tree item opener
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param array $item
+ * @return string html
*/
function media_nstree_li($item){
$class='media level'.$item['level'];
@@ -1792,6 +1972,12 @@ function media_nstree_li($item){
* Resizes the given image to the given size
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $file filename, path to file
+ * @param string $ext extension
+ * @param int $w desired width
+ * @param int $h desired height
+ * @return string path to resized or original size if failed
*/
function media_resize_image($file, $ext, $w, $h=0){
global $conf;
@@ -1800,6 +1986,7 @@ function media_resize_image($file, $ext, $w, $h=0){
if($info == false) return $file; // that's no image - it's a spaceship!
if(!$h) $h = round(($w * $info[1]) / $info[0]);
+ if(!$w) $w = round(($h * $info[0]) / $info[1]);
// we wont scale up to infinity
if($w > 2000 || $h > 2000) return $file;
@@ -1811,10 +1998,11 @@ function media_resize_image($file, $ext, $w, $h=0){
$local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
$mtime = @filemtime($local); // 0 if not exists
- if( $mtime > filemtime($file) ||
- media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
- media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
- if($conf['fperm']) chmod($local, $conf['fperm']);
+ if($mtime > filemtime($file) ||
+ media_resize_imageIM($ext, $file, $info[0], $info[1], $local, $w, $h) ||
+ media_resize_imageGD($ext, $file, $info[0], $info[1], $local, $w, $h)
+ ) {
+ if(!empty($conf['fperm'])) @chmod($local, $conf['fperm']);
return $local;
}
//still here? resizing failed
@@ -1829,6 +2017,12 @@ function media_resize_image($file, $ext, $w, $h=0){
* image because most pics are more interesting in that area (rule of thirds)
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $file filename, path to file
+ * @param string $ext extension
+ * @param int $w desired width
+ * @param int $h desired height
+ * @return string path to resized or original size if failed
*/
function media_crop_image($file, $ext, $w, $h=0){
global $conf;
@@ -1875,7 +2069,7 @@ function media_crop_image($file, $ext, $w, $h=0){
if( $mtime > @filemtime($file) ||
media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
- if($conf['fperm']) chmod($local, $conf['fperm']);
+ if(!empty($conf['fperm'])) @chmod($local, $conf['fperm']);
return media_resize_image($local,$ext, $w, $h);
}
@@ -1893,7 +2087,7 @@ function media_crop_image($file, $ext, $w, $h=0){
* @param string $id id of the image
* @param int $w resize/crop width
* @param int $h resize/crop height
- * @return string
+ * @return string token or empty string if no token required
*/
function media_get_token($id,$w,$h){
// token is only required for modified images
@@ -1916,6 +2110,11 @@ function media_get_token($id,$w,$h){
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Pavel Vitis <Pavel.Vitis@seznam.cz>
+ *
+ * @param string $url
+ * @param string $ext extension
+ * @param int $cache cachetime in seconds
+ * @return false|string path to cached file
*/
function media_get_from_URL($url,$ext,$cache){
global $conf;
@@ -1928,12 +2127,12 @@ function media_get_from_URL($url,$ext,$cache){
$mtime = @filemtime($local); // 0 if not exists
//decide if download needed:
- if( ($mtime == 0) || // cache does not exist
- ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired
- ){
- if(media_image_download($url,$local)){
+ if(($mtime == 0) || // cache does not exist
+ ($cache != -1 && $mtime < time() - $cache) // 'recache' and cache has expired
+ ) {
+ if(media_image_download($url, $local)) {
return $local;
- }else{
+ } else {
return false;
}
}
@@ -1949,6 +2148,10 @@ function media_get_from_URL($url,$ext,$cache){
* Download image files
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $url
+ * @param string $file path to file in which to put the downloaded content
+ * @return bool
*/
function media_image_download($url,$file){
global $conf;
@@ -1962,7 +2165,7 @@ function media_image_download($url,$file){
$data = $http->get($url);
if(!$data) return false;
- $fileexists = @file_exists($file);
+ $fileexists = file_exists($file);
$fp = @fopen($file,"w");
if(!$fp) return false;
fwrite($fp,$data);
@@ -1984,6 +2187,15 @@ function media_image_download($url,$file){
*
* @author Pavel Vitis <Pavel.Vitis@seznam.cz>
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $ext extension
+ * @param string $from filename path to file
+ * @param int $from_w original width
+ * @param int $from_h original height
+ * @param string $to path to resized file
+ * @param int $to_w desired width
+ * @param int $to_h desired height
+ * @return bool
*/
function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
global $conf;
@@ -2008,6 +2220,17 @@ function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
* crop images using external ImageMagick convert program
*
* @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param string $ext extension
+ * @param string $from filename path to file
+ * @param int $from_w original width
+ * @param int $from_h original height
+ * @param string $to path to resized file
+ * @param int $to_w desired width
+ * @param int $to_h desired height
+ * @param int $ofs_x offset of crop centre
+ * @param int $ofs_y offset of crop centre
+ * @return bool
*/
function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
global $conf;
@@ -2033,6 +2256,17 @@ function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$o
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Sebastian Wienecke <s_wienecke@web.de>
+ *
+ * @param string $ext extension
+ * @param string $from filename path to file
+ * @param int $from_w original width
+ * @param int $from_h original height
+ * @param string $to path to resized file
+ * @param int $to_w desired width
+ * @param int $to_h desired height
+ * @param int $ofs_x offset of crop centre
+ * @param int $ofs_y offset of crop centre
+ * @return bool
*/
function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
global $conf;
@@ -2045,6 +2279,7 @@ function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=
}
// create an image of the given filetype
+ $image = false;
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists("imagecreatefromjpeg")) return false;
$image = @imagecreatefromjpeg($from);
@@ -2058,6 +2293,7 @@ function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=
}
if(!$image) return false;
+ $newimg = false;
if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
$newimg = @imagecreatetruecolor ($to_w, $to_h);
}
@@ -2129,4 +2365,61 @@ function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=
return $okay;
}
+/**
+ * Return other media files with the same base name
+ * but different extensions.
+ *
+ * @param string $src - ID of media file
+ * @param string[] $exts - alternative extensions to find other files for
+ * @return array - array(mime type => file ID)
+ *
+ * @author Anika Henke <anika@selfthinker.org>
+ */
+function media_alternativefiles($src, $exts){
+
+ $files = array();
+ list($srcExt, /* $srcMime */) = mimetype($src);
+ $filebase = substr($src, 0, -1 * (strlen($srcExt)+1));
+
+ foreach($exts as $ext) {
+ $fileid = $filebase.'.'.$ext;
+ $file = mediaFN($fileid);
+ if(file_exists($file)) {
+ list(/* $fileExt */, $fileMime) = mimetype($file);
+ $files[$fileMime] = $fileid;
+ }
+ }
+ return $files;
+}
+
+/**
+ * Check if video/audio is supported to be embedded.
+ *
+ * @param string $mime - mimetype of media file
+ * @param string $type - type of media files to check ('video', 'audio', or null for all)
+ * @return boolean
+ *
+ * @author Anika Henke <anika@selfthinker.org>
+ */
+function media_supportedav($mime, $type=NULL){
+ $supportedAudio = array(
+ 'ogg' => 'audio/ogg',
+ 'mp3' => 'audio/mpeg',
+ 'wav' => 'audio/wav',
+ );
+ $supportedVideo = array(
+ 'webm' => 'video/webm',
+ 'ogv' => 'video/ogg',
+ 'mp4' => 'video/mp4',
+ );
+ if ($type == 'audio') {
+ $supportedAv = $supportedAudio;
+ } elseif ($type == 'video') {
+ $supportedAv = $supportedVideo;
+ } else {
+ $supportedAv = array_merge($supportedAudio, $supportedVideo);
+ }
+ return in_array($mime, $supportedAv);
+}
+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */