1<?php
2
3class helper_plugin_infomail extends DokuWiki_Plugin
4{
5
6    const TPL = 'wiki:infomail:template';
7
8    /**
9     * Return info to construct a link
10     *
11     * @todo an implementation as MenuItem would be nice too
12     * @return array
13     */
14    public function getLink()
15    {
16        global $ID;
17
18        $attr['href'] = wl($ID, ['do' => 'infomail'], false, '&');
19        $attr['class'] = 'plugin_infomail';
20        $attr['rel'] = 'no-follow';
21
22        return array(
23            'goto' => $ID,
24            'text' => $this->getLang('name'),
25            'attr' => $attr,
26        );
27    }
28
29    /**
30     * Loads the recipients from a given list
31     *
32     * Returns an empty list if the list can't be found
33     *
34     * @todo this uses a very simplistic regexp to get the mails from the page
35     * @param string $list
36     * @return string[]
37     */
38    public function loadList($list)
39    {
40        $lid = cleanID("wiki:infomail:list_$list");
41        if (!page_exists($lid)) return [];
42
43        $content = rawWiki($lid);
44        preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $content, $matches);
45        return (array)$matches[0];
46    }
47
48    /**
49     * Returns a list of all available lists
50     *
51     * @return string[] lists (without list prefix or .txt suffix)
52     * @fixme make this is still not very good, it should use one of the search_* mechanisms instead
53     */
54    public function getLists()
55    {
56        global $conf;
57        $listdir = rtrim($conf['datadir'], '/') . '/wiki/infomail/';
58        $lists = glob("$listdir/list_*.txt");
59        $lists = array_map(function ($item) {
60            return substr(basename($item, '.txt'), 5);
61        }, $lists);
62
63        return $lists;
64    }
65
66    /**
67     * Load the mail template
68     *
69     * @todo using a real code field and the parser would be better
70     * @return string
71     */
72    public function loadTemplate()
73    {
74        if (page_exists(self::TPL)) {
75            $file = wikiFN(self::TPL);
76        } else {
77            $file = __DIR__ . '/template.txt';
78        }
79        $mailtext = io_readFile($file);
80        $parts = explode("###template_begin###", $mailtext);
81        $mailtext = $parts[1];
82
83        return $mailtext;
84    }
85
86}
87