1<?php
2/**
3 * DokuWiki plugin template helper
4 *
5 * @license    GPL3 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Samuel Fischer <sf@notomorrow.de>
7 */
8
9
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_templateconfhelper_fetchaction extends DokuWiki_Action_Plugin {
13
14  function getInfo(){
15    return array(
16        'author' => 'Samuel Fischer',
17        'email'  => 'sf',
18        'date'   => '2010-02-07',
19        'name'   => 'template functions',
20        'desc'   => 'serv files from templatedir',
21        'url'    => 'samfisch.de',
22    );
23  }
24
25  function register(&$controller) {/*{{{*/
26
27      $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'fetch_action' );
28
29  }/*}}}*/
30
31  function fetch_action( $evt ) {/*{{{*/
32    #$data = $evt->data;
33    global $data, $conf, $MEDIA, $EXT, $WIDTH, $HEIGHT;
34
35    if( isset( $_GET['mode'] ) && $_GET['mode'] == 'styleimg' ) {
36        // fallthrough for other errors than not found
37        if( $data['status'] != '404' ) {
38            return false;
39        }
40	$tpl = $_GET['template'];
41
42        if( !preg_match( '/^[\w-]*$/', $tpl )) {
43            return false;
44        }
45
46        $plugins = plugin_list( );
47
48        if( !$file = getConfigPath( 'template_dir', $tpl.'/images/'.$MEDIA ))
49          $file = getConfigPath( 'template_dir', $conf['base_tpl'].'/images/'.$MEDIA );
50
51        //fall through with 404
52        if(!@file_exists( $file )) {
53            return false;
54        }
55
56        $orig = $file;
57
58      //handle image resizing/cropping
59        if((substr($MIME,0,5) == 'image') && $WIDTH){
60         if($HEIGHT){
61            $file = media_crop_image($file,$EXT,$WIDTH,$HEIGHT);
62         }else{
63            $file = media_resize_image($file,$EXT,$WIDTH,$HEIGHT);
64         }
65       }
66
67       $data['status'] = '200';
68       $data['statusmessage'] = null;
69       $data['orig'] = $orig;
70       $data['file'] = $file;
71    }
72
73  }/*}}}*/
74}
75