<?php
/**
 * Mailto plugin, mail the current page to several people
 * @author     Cédric Villemain <cedric.villemain@dalibo.com>
 */

if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

class action_plugin_mailto extends DokuWiki_Action_Plugin {

  /**
   * return some info
   */
  function getInfo(){
        return confToHash(dirname(__FILE__).'/info.txt');
  }

  /**
   * Register its handlers with the DokuWiki's event controller
   */
  function register(&$controller) {
	$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess', array());
	$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handle_act_unknown', array());
  }

  /**
	* Checks if 'mailto' was given as action, if so we
	* do handle the event our self and no further checking takes place
	*/
  function handle_act_preprocess(&$event, $param){
	  if($event->data != 'mailto') return; // nothing to do for us

	  $event->stopPropagation(); // this is our very own action, no need to check other plugins
	  $event->preventDefault();  // we handle it our self, thanks
  }

  function handle_act_unknown(&$event, $param){
	  if($event->data != 'mailto') return; // nothing to do for us

	  // we can handle it -> prevent others
	  $event->stopPropagation();
	  $event->preventDefault();

	  // do the job.
	  $this->_mailto();
  }


  /**
   * Send the mail.
   *
   * @author     Cédric Villemain <cedric.villemain@dalibo.com>
   */
  function _mailto() {
	  global $INFO, $USERINFO, $ID, $REV;

	  $ok=false;

	  // User is admin and admin required, ok
	  if ($this->getConf('mailto_acl_admin_only') && $INFO['isadmin'])
		$ok=true;
	  else {
	  // User is in the allowed group, ok
		foreach ((array)explode(',', $this->getConf('mailto_acl_grps')) as $grps) {
		  if (in_array($grps, $USERINFO['grps']))
			$ok=true;
		}
	  }

	  // No ok? return
	  if (!$ok) {
		msg($this->getLang('mailto_msg_bad_acl'),-1);
		return false;
	  }
	  //test mail never sent
	  $metas=p_get_metadata($ID);
	  if ($metas['plugin']['mailto']['sent']) {
			  msg($this->getLang('mailto_already_sent')
				   . " {$metas['plugin']['mailto']['to']}, {$metas['plugin']['mailto']['cc']} "
				   . strftime($this->getConf('mailto_dformat'), $metas['plugin']['mailto']['sent']), 2) ;
		  return false;
	  }

	  // test mail address are ok.
	  /// TODO test mail here but mail_isvalid does not accept 'Real Name <real.name@example.org>'

	  // send the mail
       if(!mail_send($metas['plugin']['mailto']['to'],
					 $metas['title'],
					 rawWiki($ID,$REV).$this->getConf('mailto_signature'),/// TODO improve here with a renderer email
					 $this->getConf('mailto_from'),
					 $metas['plugin']['mailto']['cc'],
					 $this->getConf('mailto_bcc'))) {
			msg($this->getLang('mailto_msg_send_fail'),-1);
            return false;
		}

	  // adjust metadata (do not send the mail again)
	  p_set_metadata($ID,
					 array('plugin' => array('mailto' => array('to' => $metas['plugin']['mailto']['to'],
															   'cc' => $metas['plugin']['mailto']['cc'],
															   'sent' => time())) ) );
	  msg ($this->getLang('mailto_sent_msg')
		   . " {$metas['plugin']['mailto']['to']}, {$metas['plugin']['mailto']['cc']} "
		   . strftime($this->getConf('mailto_dformat'), $metas['plugin']['mailto']['sent']),1) ; /// TODO there will be an strftime in the next API (perhaps)
	  return true;
  }
}
