1<?php
2/**
3 * Redirect plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Class helper_plugin_redirect
14 *
15 * Save and load the config file
16 */
17class helper_plugin_redirect extends DokuWiki_Admin_Plugin {
18
19    const CONFIG_FILE = DOKU_CONF . '/redirect.conf';
20    const LEGACY_FILE = __DIR__ . '/redirect.conf';
21
22    /**
23     * helper_plugin_redirect constructor.
24     *
25     * handles the legacy file
26     */
27    public function __construct() {
28        // move config from plugin directory to conf directory
29        if(!file_exists(self::CONFIG_FILE) &&
30            file_exists(self::LEGACY_FILE)) {
31            rename(self::LEGACY_FILE, self::CONFIG_FILE);
32        }
33    }
34
35    /**
36     * Saves the config file
37     *
38     * @param string $config the raw text for the config
39     * @return bool
40     */
41    public function saveConfigFile($config) {
42        return io_saveFile(self::CONFIG_FILE, cleanText($config));
43    }
44
45    /**
46     * Load the config file
47     *
48     * @return string the raw text of the config
49     */
50    public function loadConfigFile() {
51        if(!file_exists(self::CONFIG_FILE)) return '';
52        return io_readFile(self::CONFIG_FILE);
53    }
54
55    /**
56     * Get the redirect URL for a given ID
57     *
58     * Handles conf['showmsg']
59     *
60     * @param string $id the ID for which the redirect is wanted
61     * @return bool|string the full URL to redirect to
62     */
63    public function getRedirectURL($id) {
64        $redirects = confToHash(self::CONFIG_FILE);
65        if(empty($redirects[$id])) return false;
66
67        if(preg_match('/^https?:\/\//', $redirects[$id])) {
68            $url = $redirects[$id];
69        } else {
70            if($this->getConf('showmsg')) {
71                msg(sprintf($this->getLang('redirected'), hsc($id)));
72            }
73            $link = explode('#', $redirects[$id], 2);
74            $url = wl($link[0], '', true, '&');
75            if(isset($link[1])) $url .= '#' . rawurlencode($link[1]);
76        }
77
78        return $url;
79    }
80
81    /**
82     * Dummy implementation of an abstract method
83     */
84    public function html()
85    {
86        return '';
87    }
88}
89