1<?php
2/**
3 * DokuWiki Plugin oss (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  daxingplay <daxingplay@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_cloudstorage extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'handle_fetch_media_status');
23//       $controller->register_hook('MEDIA_UPLOAD_FINISH', 'BEFORE', $this, 'handle_media_upload_finish');
24
25    }
26
27    /**
28     * [Custom event handler which performs action]
29     *
30     * @param Doku_Event $event  event object by reference
31     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
32     *                           handler was registered]
33     * @return void
34     */
35
36    public function handle_fetch_media_status(Doku_Event &$event, $param) {
37        global $conf;
38        $data = $event->data;
39        $status = $data['status'];
40        $cdn = $this->getConf('cdn_url');
41        if ($status == 200 && $cdn) {
42            $file = $data['file'];
43            $file_relative_path = str_replace($conf['mediadir'], '', $file);
44            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || intval($_SERVER['SERVER_PORT']) === 443) ? 'https:' : 'http:';
45            $cdn = $protocol . str_replace(array('http:', 'https:'), '', $cdn);
46
47            if ($this->getConf('force_decode_file_name')) {
48                $file_relative_path = urlencode($file_relative_path);
49            } else {
50                $file_relative_path = utf8_decodeFN($file_relative_path);
51            }
52            $event->data['status'] = 301;
53            $event->data['statusmessage'] = $cdn . $file_relative_path;
54        }
55    }
56
57    public function handle_media_upload_finish(Doku_Event &$event, $param) {
58//        var_dump($event->data);
59    }
60
61}
62
63// vim:ts=4:sw=4:et:
64