1<?php
2//vim :set ts=2 sw=2 expandtab enc=utf-8
3/**
4 * Supa helper plugin
5 *
6 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author   Christoph Linder <post@christoph-linder.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16
17/**
18 */
19class helper_plugin_supa_helper extends DokuWiki_Plugin {
20
21  function getMethods() {
22    $result = array();
23    $result[] = array(
24      'name'  => 'decodeScreenshotFile',
25      'desc'  => 'Decodes an uploaded screenshot file so that it is compatible to the "normal" file upload mechanism. Note: parameter &file is a ref to an entry in the $FILES superglobal!',
26      'params' => array( '&file' => 'array',
27               '&id' => 'string' ),
28      'return' => "boolean: success or failure"
29    );
30    return $result;
31  }
32
33  function html_supa_applet( $ns, $auth ) {
34    // this is just a stub to not make dokuwiki crash in case the
35    // user forgot to remove the legacy source patch
36  }
37
38  //FIXME: do not pass the $_FILES array but separate values
39  //FIXME: move this to action.php?
40  function decodeScreenshotFile( &$upload_file, &$id ) {
41    $filename = $upload_file['tmp_name'];
42
43    // read entire source file
44    $fh = fopen( $filename, 'r' );
45    $data = fread( $fh, filesize( $filename ) );
46    fclose( $fh );
47
48    // write decoded data to destination file
49    $decoded = base64_decode( $data );
50    if( !$decoded ) {
51      msg( "Err: ".$lang['err_decoding'], -1 );
52      return false;
53    }
54
55    $fh = fopen( $filename, "w" );
56    fwrite( $fh, $decoded );
57    fclose( $fh );
58
59    // change info of the uploaded file to the decoded values
60    clearstatcache();
61    $upload_file['type'] = 'image/png';
62    $upload_file['size'] = filesize( $filename );
63    //echo "sizeeeee: ".filesize( $filename );
64    $upload_file['name'] = preg_replace( '/\.supascreenshot$/i', '', $upload_file['name'] );
65    $id = preg_replace( '/\.supascreenshot$/i', '', $id );
66    //print_r( $upload_file );
67    return true;
68  }
69
70}
71
72