diff options
Diffstat (limited to 'lib/exe/media.php')
-rw-r--r-- | lib/exe/media.php | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/exe/media.php b/lib/exe/media.php new file mode 100644 index 000000000..5ca3bd360 --- /dev/null +++ b/lib/exe/media.php @@ -0,0 +1,133 @@ +<?php + if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/'); + require_once(DOKU_INC.'inc/init.php'); + require_once(DOKU_INC.'inc/common.php'); + require_once(DOKU_INC.'lang/en/lang.php'); + require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php'); + require_once(DOKU_INC.'inc/html.php'); + require_once(DOKU_INC.'inc/search.php'); + require_once(DOKU_INC.'inc/template.php'); + require_once(DOKU_INC.'inc/auth.php'); + + header('Content-Type: text/html; charset='.$lang['encoding']); + + //get namespace to display (either direct or from deletion order) + if($_REQUEST['delete']){ + $DEL = cleanID($_REQUEST['delete']); + $NS = getNS($DEL); + }else{ + $NS = $_REQUEST['ns']; + $NS = cleanID($NS); + } + + //check upload permissions + $AUTH = auth_quickaclcheck("$NS:*"); + if($AUTH >= AUTH_UPLOAD){ + $UPLOADOK = true; + //create the given namespace (just for beautification) + $mdir = $conf['mediadir'].'/'.utf8_encodeFN(str_replace(':','/',$NS)); + io_makeFileDir("$mdir/xxx"); + }else{ + $UPLOADOK = false; + } + + //handle deletion + if($DEL && $AUTH >= AUTH_DELETE){ + media_delete($DEL); + } + + //handle upload + if($_FILES['upload']['tmp_name'] && $UPLOADOK){ + media_upload($NS,$AUTH); + } + + //start output and load template + header('Content-Type: text/html; charset=utf-8'); + include(DOKU_INC.'lib/tpl/'.$conf['template'].'/lib/exe/media.php'); + + //restore old umask + umask($conf['oldumask']); + +/**********************************************/ + +/** + * Deletes mediafiles - Auth is not handled here! + * + * @author Andreas Gohr <andi@splitbrain.org> + */ +function media_delete($delid){ + $file = mediaFN($delid); + if(@unlink($file)){ + return true; + } + //something went wrong + msg("'$file' couldn't be deleted - check permissions",-1); + return false; +} + +/** + * Handles Mediafile uploads + * + * @author Andreas Gohr <andi@splitbrain.org> + */ +function media_upload($NS,$AUTH){ + require_once(DOKU_INC.'inc/confutils.php'); + global $lang; + global $conf; + + // get file + $id = $_POST['id']; + $file = $_FILES['upload']; + // get id + if(empty($id)) $id = $file['name']; + $id = cleanID($NS.':'.$id); + // get filename + $fn = mediaFN($id); + + // get filetype regexp + $types = array_keys(getMimeTypes()); + $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); + $regex = join('|',$types); + + // we set the umask here but this doesn't really help + // because a temp file was created already + umask($conf['umask']); + if(preg_match('/\.('.$regex.')$/i',$fn)){ + //check for overwrite + if(@file_exists($fn) && (!$_POST['ow'] || $AUTH < AUTH_DELETE)){ + msg($lang['uploadexist'],0); + return false; + } + // prepare directory + io_makeFileDir($fn); + if(move_uploaded_file($file['tmp_name'], $fn)) { + // set the correct permission here + chmod($fn, 0777 - $conf['umask']); + msg($lang['uploadsucc'],1); + return true; + }else{ + msg($lang['uploadfail'],-1); + } + }else{ + msg($lang['uploadwrong'],-1); + } + return false; +} + +/** + * Userfunction for html_buildlist + * + * Prints available media namespaces + * + * @author Andreas Gohr <andi@splitbrain.org> + */ +function media_html_list_namespaces($item){ + $ret = ''; + $ret .= '<a href="'.DOKU_BASE.'lib/exe/media.php?ns='.idfilter($item['id']).'" class="idx_dir">'; + $pos = strrpos($item['id'], ':'); + $ret .= substr($item['id'], $pos > 0 ? $pos + 1 : 0); + $ret .= '</a>'; + return $ret; +} + +?> |