1<?php
2/**
3 * rtmchecklist plugin, mail a tasks list to RTM
4 * @author     Fabien Lisiecki
5 */
6
7if(!defined('DOKU_INC')) die();
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9require_once(DOKU_PLUGIN.'action.php');
10
11if(!defined('RTMCHECKLIST_ROOTDIR')) define('RTMCHECKLIST_ROOTDIR', DOKU_PLUGIN."rtmchecklist/");
12
13class action_plugin_rtmchecklist extends DokuWiki_Action_Plugin {
14
15  /**
16   * return some info
17   */
18  function getInfo(){
19        return confToHash(dirname(__FILE__).'/plugin.info.txt');
20  }
21
22  /**
23   * Register its handlers with the DokuWiki's event controller
24   */
25  function register(Doku_Event_Handler $controller) {
26    $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handleTplActUnknown', array());
27    $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActPreprocess', array());
28    $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_profile_form', array());
29  }
30
31    /*
32     * Add a field for RTM import email
33     */
34    function handle_profile_form(&$event, $param){
35            global $INFO;
36
37            if (empty($_SERVER['REMOTE_USER'])) {
38                return; // Not logged in
39            }
40            // include conf file for user
41            $username = $_SERVER['REMOTE_USER'];
42            $profileFilename = RTMCHECKLIST_ROOTDIR.'conf/profile_'.$username.'.php';
43            if (is_writable($profileFilename)) {
44                include($profileFilename);
45            }
46            $currentRtmEmail = $INFO['userinfo']['rtmemail'];
47
48            $inputRtmEmail = '<label class="block"><span>'.$this->getLang('rtmemail').'</span> ';
49            $inputRtmEmail.= '<input type="text" name="rtmemail" class="edit" size="200" value="'.$currentRtmEmail.'"/>';
50            $inputRtmEmail.= '</label><br />';
51
52            $rtm_form = '<form id="dw__rtmemailregister" method="post" action="" accept-charset="utf-8"><div class="no">';
53            $rtm_form = '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
54            $rtm_form.= '<fieldset ><legend>'.$this->getLang('legend_title').'</legend>';
55            $rtm_form.= '<input type="hidden" name="do" value="rtmemailregister" />';
56            $rtm_form.= $inputRtmEmail;
57            $rtm_form.= '<input type="submit" value="Save" class="button" />';
58            $rtm_form.= '<input type="reset" value="Reset" class="button" />';
59            $rtm_form.= '</fieldset>';
60            $rtm_form.= '</div></form>';
61            $pos = $event->data->findElementByAttribute('type', 'reset');
62            $event->data->insertElement($pos+3, $rtm_form);
63    }
64
65
66  function handleActPreprocess(&$event, $param){
67    if($event->data == 'plugin_rtmsend'){
68        // Accept the action
69        $event->preventDefault();
70        $event->stopPropagation();
71        return;
72    }
73    else if($event->data == 'rtmemailregister') {
74        // Accept the action
75        $event->preventDefault();
76        $event->stopPropagation();
77        return;
78    }
79    else {
80        // nothing to do for us
81        return;
82    }
83    return;
84  }
85
86    /**
87     * Hook for event TPL_ACT_UNKNOWN, action
88     */
89    function handleTplActUnknown(&$event, $param) {
90        if ($event->data == 'plugin_rtmsend') {
91            global $INPUT; //available since release 2012-10-13 "Adora Belle"
92            global $INFO;
93
94            $wikitext = "=== ".$this->getLang('Results')." ===\n";
95            if(!checksecuritytoken()) {
96                return;
97            }
98            // send the checklist to RTM
99            $sendResult = false;
100            if (empty($_SERVER['REMOTE_USER'])) {
101                $event->data = 'login';
102                return; // Not logged in
103            }
104            // include conf file for user
105            $username = $_SERVER['REMOTE_USER'];
106            $profileFilename = RTMCHECKLIST_ROOTDIR.'conf/profile_'.$username.'.php';
107            if (is_writable($profileFilename)) {
108                include($profileFilename);
109            }
110            //
111            $tasksList = $INPUT->str('taskslist');
112            $rtmEmail  = $INFO['userinfo']['rtmemail'];
113            if(empty($rtmEmail)) {
114                $wikitext .= $this->getLang('error_fill_email');
115            }
116            // send the mail
117            if(mail_send($rtmEmail,
118                         "", // no title
119                         $tasksList)) {
120                $sendResult = true;
121            }
122
123            // Accept the action
124            $event->preventDefault();
125            $event->stopPropagation();
126
127            // show final result
128            if($sendResult)
129                $wikitext.= $this->getLang('checklist_sent');
130            else
131                $wikitext.= $this->getLang('checklist_not_sent');
132            $pageId = $INPUT->str('id');
133            $wikitext.= "[[".htmlspecialchars($pageId)."|".$this->getLang('go_back_to').htmlspecialchars($pageId)." ".$this->getLang('page')."]]";
134            // parse and render. TODO: cache
135            $ret = p_render('xhtml',p_get_instructions($wikitext),$info);
136            echo $ret;
137        }
138        else if($event->data == 'rtmemailregister') {
139            global $INPUT; //available since release 2012-10-13 "Adora Belle"
140
141            if(!checksecuritytoken()) {
142                return;
143            }
144            // better safe than sorry
145            if (empty($_SERVER['REMOTE_USER'])) {
146                $event->data = 'login';
147                return; // Not logged in
148            }
149            $username = $_SERVER['REMOTE_USER'];
150            // make sure dir RTMCHECKLIST_ROOTDIR is created
151            mkdir(RTMCHECKLIST_ROOTDIR.'conf', 0700);
152            //write file: RTMCHECKLIST_ROOTDIR.'conf/profile_'.$username.'.php'
153            $fp = fopen(RTMCHECKLIST_ROOTDIR.'conf/profile_'.$username.'.php', 'w');
154            fwrite($fp, "<?php\n");
155            fwrite($fp, "global \$INFO;\n\n");
156            fwrite($fp, "\$INFO['userinfo']['rtmemail'] = '".htmlspecialchars($INPUT->str('rtmemail'))."';\n");
157            fwrite($fp, '?>');
158            fclose($fp);
159            echo $this->getLang('your_email_add').htmlspecialchars($INPUT->str('rtmemail')).$this->getLang('is_now_saved');
160            // Accept the action
161            $event->preventDefault();
162            $event->stopPropagation();
163            // all work has been done in handleActPreprocess()
164            //$event->data = 'profile';
165        }
166    }
167}
168