1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <andi@splitbrain.org>
6 */
7
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_imageshack extends DokuWiki_Action_Plugin {
15
16    /**
17     * register the eventhandlers and initialize some options
18     */
19    function register(Doku_Event_Handler $controller){
20        $controller->register_hook('MEDIAMANAGER_STARTED',
21                                   'AFTER',
22                                   $this,
23                                   'handle_start',
24                                   array());
25
26
27        $controller->register_hook('MEDIAMANAGER_CONTENT_OUTPUT',
28                                   'BEFORE',
29                                   $this,
30                                   'handle_output',
31                                   array());
32    }
33
34    function handle_start(&$event, $param){
35        if(!isset($_FILES['imageshack_file'])) return;
36
37        if($_FILES['imageshack_file']['error'] ||
38           !is_uploaded_file($_FILES['imageshack_file']['tmp_name'])){
39            msg(sprintf('The was a problem receiving the file from you (error %d)',
40                $_FILES['imageshack_file']['error']),-1);
41            return;
42        }
43
44        require_once(DOKU_INC.'/inc/HTTPClient.php');
45        $http = new HTTPClient();
46        $http->timeout = 60;
47        $http->headers['Content-Type'] = 'multipart/form-data';
48
49        $data = array(
50                    'xml'  => 'yes',
51                    'fileupload' => array(
52                            'filename' => $_FILES['imageshack_file']['name'],
53                            'mimetype' => $_FILES['imageshack_file']['type'],
54                            'body' => file_get_contents($_FILES['imageshack_file']['tmp_name'])
55                        )
56                );
57        $xml = $http->post('http://imageshack.us/index.php',$data);
58
59        if(!$xml){
60            msg('There was a problem with uploading your file to imageshack: '.$http->error,-1);
61            return;
62        }
63
64        $xml = new SimpleXMLElement($xml);
65        if(!$xml){
66            msg('ImageShack did not accept your upload',-1);
67            return;
68        }
69
70        list($w,$h) = explode('x',(string) $xml->resolution[0]);
71        $_SESSION['imageshack'][] = array(
72            'link'   => (string) $xml->image_link[0],
73            'adlink' => (string) $xml->ad_link[0],
74            'name'   => (string) $xml->image_name[0],
75            'width'  => $w,
76            'height' => $h,
77            'size'   => (string) $xml->filesize[0]
78        );
79    }
80
81    function handle_output(&$event, $param){
82        if($event->data['do'] != 'imageshack') return;
83        global $lang;
84
85        echo '<h1 id="media__ns">'.$this->getLang('name').'</h1>';
86        echo '<p>'.$this->getLang('intro').'</p>';
87        echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" method="post" enctype="multipart/form-data">';
88        echo '<input type="hidden" name="do" value="imageshack" />';
89        echo '<input type="file" name="imageshack_file" />';
90        echo '<input type="submit" value="'.$lang['btn_upload'].'" class="button" />';
91        echo '</form>';
92
93        // output the uploads stored in the current session
94        if(is_array($_SESSION['imageshack'])){
95            $files = array_reverse($_SESSION['imageshack']);
96
97            $twibble = 1;
98            foreach($files as $item){
99                $twibble *= -1;
100                $zebra = ($twibble == -1) ? 'odd' : 'even';
101                list($ext,$mime,$dl) = mimetype($item['name']);
102                $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
103                $class = 'select mediafile mf_'.$class;
104
105                echo '<div class="'.$zebra.'">'.NL;
106                echo '<a name="h_'.$item['link'].'" class="'.$class.'">'.hsc($item['name']).'</a> ';
107                echo '<span class="info">('.$item['width'].'&#215;'.$item['height'].' '.filesize_h($item['size']).')</span>'.NL;
108                echo ' <a href="'.$item['adlink'].'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
109                     'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>'.NL;
110                echo '<div class="example" id="ex_'.str_replace(':','_',$item['link']).'">';
111                echo $lang['mediausage'].' <code>{{'.hsc($item['link']).'}}</code>';
112                echo '</div>';
113
114                if($item['width'] > 120 || $item['height'] > 100){
115                    $w = 120;
116                    $h = 100;
117                }else{
118                    $w = $item['width'];
119                    $h = $item['height'];
120                }
121
122                $src = ml($item['link'],array('w'=>$w,'h'=>$h));
123                $p = array();
124                $p['width']  = $w;
125                $p['height'] = $h;
126                $p['alt']    = $item['name'];
127                $p['class']  = 'thumb';
128                $att = buildAttributes($p);
129
130                // output
131                echo '<div class="detail">';
132                echo '<div class="thumb">';
133                echo '<a name="d_'.$item['link'].'" class="select">';
134                echo '<img src="'.$src.'" '.$att.' />';
135                echo '</a>';
136                echo '</div>';
137                echo '</div>';
138
139                echo '<div class="clearer"></div>'.NL;
140                echo '</div>'.NL;
141
142
143            }
144
145        }
146        $event->preventDefault();
147    }
148
149
150}
151
152