1<?php
2/**
3 * SUPA Action Plugin: handle upload of a screenshot file
4 *
5 * @author Christoph Linder <post@christoph-linder.de>
6 */
7
8// must be run within Dokuwiki
9if (!defined('DOKU_INC')) die();
10
11if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
12if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
13if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14
15require_once DOKU_PLUGIN.'action.php';
16
17define( 'SUPA_UPLOAD_MIMETYPE', 'application/x-supa-screenshot' );
18
19class action_plugin_supa_action extends DokuWiki_Action_Plugin {
20  /**
21   * Register the eventhandlers
22   */
23  function register(&$controller) {
24    //$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'get_applet', array ());
25    $controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'decode_upload', array ());
26    //$controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'inject_mimetype', array());
27    $controller->register_hook('MEDIAMANAGER_CONTENT_OUTPUT', 'AFTER', $this, 'add_mediamanager_upload_region', array());
28  }
29
30  function inject_mimetype( &$event ) {
31    //$mime = getMimeTypes(); // initializes mime below
32
33    //print_r( $mime );
34  }
35
36  function add_mediamanager_upload_region( &$event ) {
37    global $NS;
38    $ext = 'png';
39    $default_filename = "screenshot-".date("Y-m-d_H-i-s"). "." .$ext;
40    echo "<!-- SUPA begin -->\n";
41    echo "<script type='text/javascript'>\n";
42    #echo "alert( 'loading' );";
43    echo "addInitEvent(function(){\n";
44    echo "  supa_handler.init(\n";
45    echo "    '".addslashes(getSecurityToken())."',\n";
46    echo "    '".addslashes($this->getConf("previewscaler"))."',\n";
47    echo "    '".addslashes($this->getConf("previewwidth"))."',\n";
48    echo "    '".addslashes($this->getConf("previewheight"))."',\n";
49    echo "    '".addslashes(hsc($NS))."',\n";
50    echo "    '".addslashes($default_filename)."'\n";
51    echo "  );\n";
52    echo "});\n";
53    echo "</script>\n";
54    echo "<!-- SUPA end -->\n";
55    return true;
56  }
57
58  function decode_upload( &$event ) {
59    //FIXME: handle flash uploads?
60    $f = &$_FILES['Filedata'];
61    $id = $_REQUEST['id'];
62
63    //print_r( $f );
64    if( !$f || ! preg_match( '/\.supascreenshot$/i', $f['name'] ) ) {
65      return true;
66    }
67
68    // check if the max upload size has been exceeded
69    if( empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
70      echo $this->getLang("max_datasize_exceeded");
71      die();
72    }
73
74    //print_r( $_REQUEST );
75    /*
76    echo "name: ".$f['name']."\n";
77    echo "type: ".$f['type']."\n";
78    echo "tmp_name: ".$f['tmp_name'],"\n";
79    echo "error: ".$f['error']."\n";
80    echo "size: ".$f['size']."\n";
81    */
82
83    #$supa = &plugin_load( 'helper', 'supa_helper' );
84    $supa = &plugin_load( 'helper', 'supa_helper' );
85    #$supa = $this->loadHelper( 'supa_helper', true );
86    if( !$supa ) {
87      echo "Error while initializing Supa plugin";
88      die();
89    }
90
91    //FIXME: do not pass the $_FILES array but separate values
92    $ret = $supa->decodeScreenshotFile( $f, $id );
93    if( $ret ) {
94      $_REQUEST['id'] = $id;
95      $_POST['id'] = $id;
96      $_GET['id'] = $id;
97    } else {
98      echo "Error decoding the uploaded screenshot file";
99      die();
100    }
101    return $ret;
102  }
103
104}
105
106