14df872e4SAndreas Gohr<?php 24df872e4SAndreas Gohr/** 34df872e4SAndreas Gohr * DokuWiki Plugin imgpaste (Action Component) 44df872e4SAndreas Gohr * 54df872e4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 64df872e4SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 74df872e4SAndreas Gohr */ 84df872e4SAndreas Gohr 94df872e4SAndreas Gohr// must be run within Dokuwiki 104df872e4SAndreas Gohrif(!defined('DOKU_INC')) die(); 114df872e4SAndreas Gohr 124df872e4SAndreas Gohrif(!defined('DOKU_LF')) define('DOKU_LF', "\n"); 134df872e4SAndreas Gohrif(!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 144df872e4SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 154df872e4SAndreas Gohr 164df872e4SAndreas Gohrrequire_once DOKU_PLUGIN . 'action.php'; 174df872e4SAndreas Gohr 184df872e4SAndreas Gohrclass action_plugin_imgpaste extends DokuWiki_Action_Plugin { 194df872e4SAndreas Gohr 204df872e4SAndreas Gohr private $tempdir = ''; 214df872e4SAndreas Gohr private $tempfile = ''; 224df872e4SAndreas Gohr 23*3250b209SAndreas Gohr public function register(Doku_Event_Handler $controller) { 244df872e4SAndreas Gohr 254df872e4SAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown'); 264df872e4SAndreas Gohr 274df872e4SAndreas Gohr } 284df872e4SAndreas Gohr 294df872e4SAndreas Gohr public function handle_ajax_call_unknown(Doku_Event &$event, $param) { 304df872e4SAndreas Gohr if($event->data != 'plugin_imgpaste') return; 314df872e4SAndreas Gohr global $lang; 324df872e4SAndreas Gohr 334df872e4SAndreas Gohr // get data 344df872e4SAndreas Gohr global $INPUT; 354df872e4SAndreas Gohr $data = $INPUT->post->str('data'); 364df872e4SAndreas Gohr list($type, $data) = explode(';', $data); 374df872e4SAndreas Gohr if(!$data) $this->fail(400, $this->getLang('e_nodata')); 384df872e4SAndreas Gohr 394df872e4SAndreas Gohr // process data encoding 404df872e4SAndreas Gohr $type = strtolower(substr($type, 5)); // strip 'data:' prefix 414df872e4SAndreas Gohr $data = substr($data, 7); // strip 'base64,' prefix 424df872e4SAndreas Gohr $data = base64_decode($data); 434df872e4SAndreas Gohr 444df872e4SAndreas Gohr // check for supported mime type 454df872e4SAndreas Gohr $mimetypes = array_flip(getMimeTypes()); 464df872e4SAndreas Gohr if(!isset($mimetypes[$type])) $this->fail(415, $lang['uploadwrong']); 474df872e4SAndreas Gohr 484df872e4SAndreas Gohr // prepare file names 494df872e4SAndreas Gohr $tempname = $this->storetemp($data); 504df872e4SAndreas Gohr $filename = $this->getConf('filename'); 514df872e4SAndreas Gohr $filename = str_replace( 524df872e4SAndreas Gohr array( 534df872e4SAndreas Gohr '@NS@', 544df872e4SAndreas Gohr '@ID@', 554df872e4SAndreas Gohr '@USER@' 564df872e4SAndreas Gohr ), 574df872e4SAndreas Gohr array( 584df872e4SAndreas Gohr getNS($INPUT->post->str('id')), 594df872e4SAndreas Gohr $INPUT->post->str('id'), 604df872e4SAndreas Gohr $_SERVER['REMOTE_USER'] 614df872e4SAndreas Gohr ), 624df872e4SAndreas Gohr $filename 634df872e4SAndreas Gohr ); 644df872e4SAndreas Gohr $filename = strftime($filename); 654df872e4SAndreas Gohr $filename .= '.'.$mimetypes[$type]; 664df872e4SAndreas Gohr $filename = cleanID($filename); 674df872e4SAndreas Gohr 684df872e4SAndreas Gohr // check ACLs 694df872e4SAndreas Gohr $auth = auth_quickaclcheck($filename); 704df872e4SAndreas Gohr if($auth < AUTH_UPLOAD) $this->fail(403, $lang['uploadfail']); 714df872e4SAndreas Gohr 724df872e4SAndreas Gohr // do the actual saving 734df872e4SAndreas Gohr $result = media_save( 744df872e4SAndreas Gohr array( 754df872e4SAndreas Gohr 'name' => $tempname, 764df872e4SAndreas Gohr 'mime' => $type, 774df872e4SAndreas Gohr 'ext' => $mimetypes[$type] 784df872e4SAndreas Gohr ), 794df872e4SAndreas Gohr $filename, 804df872e4SAndreas Gohr false, 814df872e4SAndreas Gohr $auth, 824df872e4SAndreas Gohr 'copy' 834df872e4SAndreas Gohr ); 844df872e4SAndreas Gohr if(is_array($result)) $this->fail(500, $result[0]); 854df872e4SAndreas Gohr 864df872e4SAndreas Gohr //Still here? We had a successful upload 874df872e4SAndreas Gohr $this->clean(); 884df872e4SAndreas Gohr header('Content-Type: application/json'); 894df872e4SAndreas Gohr $json = new JSON(); 904df872e4SAndreas Gohr echo $json->encode( 914df872e4SAndreas Gohr array( 924df872e4SAndreas Gohr 'message' => $lang['uploadsucc'], 934df872e4SAndreas Gohr 'id' => $result 944df872e4SAndreas Gohr ) 954df872e4SAndreas Gohr ); 964df872e4SAndreas Gohr 974df872e4SAndreas Gohr $event->preventDefault(); 984df872e4SAndreas Gohr $event->stopPropagation(); 994df872e4SAndreas Gohr } 1004df872e4SAndreas Gohr 1014df872e4SAndreas Gohr /** 1024df872e4SAndreas Gohr * Create a temporary file from the given data 1034df872e4SAndreas Gohr * 1044df872e4SAndreas Gohr * exits if an error occurs 1054df872e4SAndreas Gohr * 1064df872e4SAndreas Gohr * @param $data 1074df872e4SAndreas Gohr * @return string 1084df872e4SAndreas Gohr */ 1094df872e4SAndreas Gohr private function storetemp($data){ 1104df872e4SAndreas Gohr // store in temporary file 1114df872e4SAndreas Gohr $this->tempdir = io_mktmpdir(); 1124df872e4SAndreas Gohr if(!$this->tempdir) $this->fail(500); 1134df872e4SAndreas Gohr $this->tempfile = $this->tempdir.'/'.md5($data); 1144df872e4SAndreas Gohr if(!io_saveFile($this->tempfile, $data)) $this->fail(500); 1154df872e4SAndreas Gohr return $this->tempfile; 1164df872e4SAndreas Gohr } 1174df872e4SAndreas Gohr 1184df872e4SAndreas Gohr /** 1194df872e4SAndreas Gohr * remove temporary file and directory 1204df872e4SAndreas Gohr */ 1214df872e4SAndreas Gohr private function clean(){ 1224df872e4SAndreas Gohr if($this->tempfile && file_exists($this->tempfile)) @unlink($this->tempfile); 1234df872e4SAndreas Gohr if($this->tempdir && is_dir($this->tempdir)) @rmdir($this->tempdir); 1244df872e4SAndreas Gohr $this->tempfile = ''; 1254df872e4SAndreas Gohr $this->tempdir = ''; 1264df872e4SAndreas Gohr } 1274df872e4SAndreas Gohr 1284df872e4SAndreas Gohr /** 1294df872e4SAndreas Gohr * End the execution with a HTTP error code 1304df872e4SAndreas Gohr * 1314df872e4SAndreas Gohr * Calls clean 1324df872e4SAndreas Gohr * 1334df872e4SAndreas Gohr * @param int $status HTTP status code 1344df872e4SAndreas Gohr * @param string $text 1354df872e4SAndreas Gohr */ 1364df872e4SAndreas Gohr private function fail($status, $text=''){ 1374df872e4SAndreas Gohr $this->clean(); 1384df872e4SAndreas Gohr http_status($status, $text); 1394df872e4SAndreas Gohr exit; 1404df872e4SAndreas Gohr } 1414df872e4SAndreas Gohr} 1424df872e4SAndreas Gohr 1434df872e4SAndreas Gohr// vim:ts=4:sw=4:et: 144