1<?php
2/**
3 * Mailto plugin, mail the current page to several people
4 * @author     Cédric Villemain <cedric.villemain@dalibo.com>
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
11class action_plugin_mailto extends DokuWiki_Action_Plugin {
12
13  /**
14   * return some info
15   */
16  function getInfo(){
17        return confToHash(dirname(__FILE__).'/info.txt');
18  }
19
20  /**
21   * Register its handlers with the DokuWiki's event controller
22   */
23  function register(&$controller) {
24	$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess', array());
25	$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handle_act_unknown', array());
26  }
27
28  /**
29	* Checks if 'mailto' was given as action, if so we
30	* do handle the event our self and no further checking takes place
31	*/
32  function handle_act_preprocess(&$event, $param){
33	  if($event->data != 'mailto') return; // nothing to do for us
34
35	  $event->stopPropagation(); // this is our very own action, no need to check other plugins
36	  $event->preventDefault();  // we handle it our self, thanks
37  }
38
39  function handle_act_unknown(&$event, $param){
40	  if($event->data != 'mailto') return; // nothing to do for us
41
42	  // we can handle it -> prevent others
43	  $event->stopPropagation();
44	  $event->preventDefault();
45
46	  // do the job.
47	  $this->_mailto();
48  }
49
50
51  /**
52   * Send the mail.
53   *
54   * @author     Cédric Villemain <cedric.villemain@dalibo.com>
55   */
56  function _mailto() {
57	  global $INFO, $USERINFO, $ID, $REV;
58
59	  $ok=false;
60
61	  // User is admin and admin required, ok
62	  if ($this->getConf('mailto_acl_admin_only') && $INFO['isadmin'])
63		$ok=true;
64	  else {
65	  // User is in the allowed group, ok
66		foreach ((array)explode(',', $this->getConf('mailto_acl_grps')) as $grps) {
67		  if (in_array($grps, $USERINFO['grps']))
68			$ok=true;
69		}
70	  }
71
72	  // No ok? return
73	  if (!$ok) {
74		msg($this->getLang('mailto_msg_bad_acl'),-1);
75		return false;
76	  }
77	  //test mail never sent
78	  $metas=p_get_metadata($ID);
79	  if ($metas['plugin']['mailto']['sent']) {
80			  msg($this->getLang('mailto_already_sent')
81				   . " {$metas['plugin']['mailto']['to']}, {$metas['plugin']['mailto']['cc']} "
82				   . strftime($this->getConf('mailto_dformat'), $metas['plugin']['mailto']['sent']), 2) ;
83		  return false;
84	  }
85
86	  // test mail address are ok.
87	  /// TODO test mail here but mail_isvalid does not accept 'Real Name <real.name@example.org>'
88
89	  // send the mail
90       if(!mail_send($metas['plugin']['mailto']['to'],
91					 $metas['title'],
92					 rawWiki($ID,$REV).$this->getConf('mailto_signature'),/// TODO improve here with a renderer email
93					 $this->getConf('mailto_from'),
94					 $metas['plugin']['mailto']['cc'],
95					 $this->getConf('mailto_bcc'))) {
96			msg($this->getLang('mailto_msg_send_fail'),-1);
97            return false;
98		}
99
100	  // adjust metadata (do not send the mail again)
101	  p_set_metadata($ID,
102					 array('plugin' => array('mailto' => array('to' => $metas['plugin']['mailto']['to'],
103															   'cc' => $metas['plugin']['mailto']['cc'],
104															   'sent' => time())) ) );
105	  msg ($this->getLang('mailto_sent_msg')
106		   . " {$metas['plugin']['mailto']['to']}, {$metas['plugin']['mailto']['cc']} "
107		   . strftime($this->getConf('mailto_dformat'), $metas['plugin']['mailto']['sent']),1) ; /// TODO there will be an strftime in the next API (perhaps)
108	  return true;
109  }
110}
111