1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <gohr@cosmocode.de>
5 */
6
7/**
8 * Class helper_plugin_blogtng_tools
9 */
10class helper_plugin_blogtng_tools extends DokuWiki_Plugin {
11
12    /**
13     * Values accepted in syntax
14     */
15    static $data_whitelist = [
16        'sortorder' => ['asc', 'desc'],
17        'sortby' => ['created', 'lastmod', 'title', 'page', 'random'],
18    ];
19
20    /**
21     * Return a page id based on the given format and title.
22     *
23     * @param string $format the format of the id to generate
24     * @param string $title the title of the page to create
25     * @return string a page id
26     */
27    static public function mkpostid($format,$title){
28        global $conf, $INPUT;
29
30        $replace = array(
31            '%{title}' => str_replace(':',$conf['sepchar'],$title),
32            '%{user}' => $INPUT->server->str('REMOTE_USER'),
33        );
34
35        $out = $format;
36        $out = str_replace(array_keys($replace), array_values($replace), $out);
37        $out = dformat(null, $out);
38        return cleanID($out);
39    }
40
41    /**
42     * @param string $string comma separated values
43     * @return array cleaned splited values
44     */
45    public static function filterExplodeCSVinput($string) {
46        return array_filter(array_map('trim', explode(',', $string)));
47    }
48
49    /**
50     * @param array $conf
51     */
52    static public function cleanConf(&$conf) {
53        if (!in_array($conf['sortorder'], self::$data_whitelist['sortorder'])) {
54            unset($conf['sortorder']);
55        }
56        if (!in_array($conf['sortby'], self::$data_whitelist['sortby'])) {
57            unset($conf['sortby']);
58        }
59        if (!is_int($conf['limit'])) {
60            unset($conf['limit']);
61        }
62        if (!is_int($conf['offset'])) {
63            unset($conf['offset']);
64        }
65    }
66
67    /**
68     * @param string $tpl
69     * @param string $type
70     * @return bool|string
71     */
72    static public function getTplFile($tpl, $type) {
73        $res = false;
74        foreach(array('/', '_') as $sep) {
75            $fname = DOKU_PLUGIN . "blogtng/tpl/$tpl$sep$type.php";
76            if (file_exists($fname)) {
77                $res = $fname;
78                break;
79            }
80        }
81
82        if($res === false){
83            msg("blogtng plugin: template file $type for template $tpl does not exist!", -1);
84            return false;
85        }
86
87        return $res;
88    }
89}
90