summaryrefslogtreecommitdiff
path: root/lib/exe
diff options
context:
space:
mode:
Diffstat (limited to 'lib/exe')
-rw-r--r--lib/exe/ajax.php53
-rw-r--r--lib/exe/fetch.php230
-rw-r--r--lib/exe/media.php133
3 files changed, 416 insertions, 0 deletions
diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php
new file mode 100644
index 000000000..dfe0d2ceb
--- /dev/null
+++ b/lib/exe/ajax.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * DokuWiki AJAX call handler
+ *
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+
+//fix for Opera XMLHttpRequests
+if(!count($_POST) && $HTTP_RAW_POST_DATA){
+ parse_str($HTTP_RAW_POST_DATA, $_POST);
+}
+
+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.'inc/pageutils.php');
+require_once(DOKU_INC.'inc/auth.php');
+
+//call the requested function
+$call = 'ajax_'.$_POST['call'];
+if(function_exists($call)){
+ $call();
+}else{
+ print "The called function does not exist!";
+}
+
+/**
+ * Searches for matching pagenames
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function ajax_qsearch(){
+ global $conf;
+ global $lang;
+
+ $query = cleanID($_POST['q']);
+ if(empty($query)) return;
+
+ $nsdir = str_replace(':','/',getNS($query));
+ require_once(DOKU_INC.'inc/search.php');
+ require_once(DOKU_INC.'inc/html.php');
+
+ $data = array();
+ search($data,$conf['datadir'],'search_qsearch',array(query => $query),$nsdir);
+
+ if(!count($data)) return;
+
+ print '<b>'.$lang['quickhits'].'</b>';
+ print html_buildlist($data,'qsearch','html_list_index');
+}
+
+?>
diff --git a/lib/exe/fetch.php b/lib/exe/fetch.php
new file mode 100644
index 000000000..3fa8777f1
--- /dev/null
+++ b/lib/exe/fetch.php
@@ -0,0 +1,230 @@
+<?php
+/**
+ * DokuWiki media passthrough file
+ *
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+
+ 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.'inc/pageutils.php');
+ require_once(DOKU_INC.'inc/confutils.php');
+ require_once(DOKU_INC.'inc/auth.php');
+ $mimetypes = getMimeTypes();
+
+ //get input
+ $MEDIA = $_REQUEST['media'];
+ $CACHE = calc_cache($_REQUEST['cache']);
+ $WIDTH = $_REQUEST['w'];
+ $HEIGHT = $_REQUEST['h'];
+ list($EXT,$MIME) = mimetype($MEDIA);
+ if($EXT === false){
+ $EXT = 'unknown';
+ $MIME = 'application/octet-stream';
+ }
+
+ //media to local file
+ if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
+ //handle external media
+ $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
+ if(!$FILE){
+ //download failed - redirect to original URL
+ header('Location: '.$MEDIA);
+ exit;
+ }
+ }else{
+ $MEDIA = cleanID($MEDIA);
+ if(empty($MEDIA)){
+ header("HTTP/1.0 400 Bad Request");
+ print 'Bad request';
+ exit;
+ }
+
+ //check permissions (namespace only)
+ if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
+ header("HTTP/1.0 401 Unauthorized");
+ //fixme add some image for imagefiles
+ print 'Unauthorized';
+ exit;
+ }
+ $FILE = mediaFN($MEDIA);
+ }
+
+ //check file existance
+ if(!@file_exists($FILE)){
+ header("HTTP/1.0 404 Not Found");
+ //FIXME add some default broken image
+ print 'Not Found';
+ exit;
+ }
+
+ //handle image resizing
+ if((substr($MIME,0,5) == 'image') && $WIDTH){
+ $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
+ }
+
+
+ //FIXME set sane cachecontrol headers
+ //FIXME handle conditional and partial requests
+
+ //send file
+ header("Content-Type: $MIME");
+ header('Last-Modified: '.date('r',filemtime($FILE)));
+ header('Content-Length: '.filesize($FILE));
+
+ //application mime type is downloadable
+ if(substr($MIME,0,11) == 'application'){
+ header('Content-Disposition: attachment; filename="'.basename($FILE).'"');
+ }
+
+ $fp = @fopen($FILE,"rb");
+ if($fp){
+ fpassthru($fp); //does a close itself
+ }else{
+ header("HTTP/1.0 500 Internal Server Error");
+ print "Could not read $FILE - bad permissions?";
+ }
+
+/* ------------------------------------------------------------------------ */
+
+/**
+ * Resizes the given image to the given size
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function get_resized($file, $ext, $w, $h=0){
+ global $conf;
+
+ $md5 = md5($file);
+ $info = getimagesize($file);
+ if(!$h) $h = round(($w * $info[1]) / $info[0]);
+
+
+ //cache
+ $local = $conf['mediadir'].'/_cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
+ $mtime = @filemtime($local); // 0 if not exists
+
+ if( $mtime > filemtime($file) || resize_image($ext,$file,$info[0],$info[1],$local,$w,$h) ){
+ return $local;
+ }
+ //still here? resizing failed
+ return $file;
+}
+
+/**
+ * Returns the wanted cachetime in seconds
+ *
+ * Resolves named constants
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function calc_cache($cache){
+ global $conf;
+
+ if(strtolower($cache) == 'nocache') return 0; //never cache
+ if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
+ return -1; //cache endless
+}
+
+/**
+ * Download a remote file and return local filename
+ *
+ * returns false if download fails. Uses cached file if available and
+ * wanted
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function get_from_URL($url,$ext,$cache){
+ global $conf;
+
+ $url = strtolower($url);
+ $md5 = md5($url);
+
+ $local = $conf['mediadir']."/_cache/$md5.$ext";
+ $mtime = @filemtime($local); // 0 if not exists
+
+ //decide if download needed:
+
+ // never cache exists but no endless cache not exists or expired
+ if( $cache == 0 || ($mtime != 0 && $cache != -1) || $mtime < time()-$cache ){
+ if(io_download($url,$local)){
+ return $local;
+ }else{
+ return false;
+ }
+ }
+
+ //if cache exists use it else
+ if($mtime) return $local;
+
+ //else return false
+ return false;
+}
+
+/**
+ * resize images
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function resize_image($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
+ global $conf;
+
+ if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
+
+ // create an image of the given filetype
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists("imagecreatefromjpeg")) return false;
+ $image = @imagecreatefromjpeg($from);
+ }elseif($ext == 'png') {
+ if(!function_exists("imagecreatefrompng")) return false;
+ $image = @imagecreatefrompng($from);
+
+ }elseif($ext == 'gif') {
+ if(!function_exists("imagecreatefromgif")) return false;
+ $image = @imagecreatefromgif($from);
+ }
+ if(!$image) return false;
+
+ if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
+ $newimg = @imagecreatetruecolor ($to_w, $to_h);
+ }
+ if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
+ if(!$newimg) return false;
+
+ //keep png alpha channel if possible
+ if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
+ imagealphablending($newimg, false);
+ imagesavealpha($newimg,true);
+ }
+
+ // create cachedir
+ io_makeFileDir($to);
+
+ //try resampling first
+ if(function_exists("imagecopyresampled")){
+ if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
+ imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
+ }
+ }else{
+ imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
+ }
+
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists("imagejpeg")) return false;
+ return imagejpeg($newimg, $to, 70);
+ }elseif($ext == 'png') {
+ if(!function_exists("imagepng")) return false;
+ return imagepng($newimg, $to);
+ }elseif($ext == 'gif') {
+ if(!function_exists("imagegif")) return false;
+ return imagegif($newimg, $to);
+ }
+
+ return false;
+}
+
+
+//Setup VIM: ex: et ts=2 enc=utf-8 :
+?>
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;
+}
+
+?>