<?php

/*
 +--------------------------------------------------------------------+
 | CiviCRM version 3.0                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2009                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007.                                       |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License along with this program; if not, contact CiviCRM LLC       |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2009
 * $Id$
 *
 */


/**
 * This class handles all SOAP client requests.
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2009
 * $Id$
 *
 */

require_once 'api/utils.php';
require_once 'api/Mailer.php';

class CRM_Utils_SoapServer
{
    /**
     * Number of seconds we should let a soap process idle
     * @static
     */
    static $soap_timeout = 0;
    
    /**
     * Cache the actual UF Class
     */
    public $ufClass;

    /**
     * Class constructor.  This caches the real user framework class locally,
     * so we can use it for authentication and validation.
     *
     * @param  string $uf       The userframework class
     */
    public function __construct() {
        // any external program which call SoapServer is responsible for
        // creating and attaching the session
        $args = func_get_args( );
        $this->ufClass = array_shift( $args );
    }

    /**
     * Simple ping function to test for liveness.
     *
     * @param string $var   The string to be echoed
     * @return string       $var
     * @access public
     */
    public function ping($var) {
        $session =& CRM_Core_Session::singleton();
        $key = $session->get('key');
        $session->set( 'key', $var );
        return "PONG: $var ($key)";
    }


    /**
     * Verify a SOAP key
     *
     * @param string $key   The soap key generated by authenticate()
     * @return none
     * @access public
     */
    public function verify($key) {
        $session =& CRM_Core_Session::singleton();

        $soap_key = $session->get('soap_key');
        $t = time();
        
        if ( $key !== sha1($soap_key) ) {
            throw new SoapFault('Client', 'Invalid key');
        }
        

        if (    self::$soap_timeout && 
                $t > ($session->get('soap_time') + self::$soap_timeout)) {
            throw new SoapFault('Client', 'Expired key');
        }
        
        /* otherwise, we're ok.  update the timestamp */
        $session->set('soap_time', $t);
    }
    
    /**
     * Authentication wrapper to the UF Class
     *
     * @param string $name      Login name
     * @param string $pass      Password
     * @return string           The SOAP Client key
     * @access public
     * @static
     */
    public function authenticate($name, $pass) {
        require_once( str_replace( '_', DIRECTORY_SEPARATOR, $this->ufClass ) . '.php' );
        eval ('$result =& ' . $this->ufClass . '::authenticate($name, $pass);');

        if (empty($result)) {
            throw new SoapFault('Client', 'Invalid login');
        }
        
        $session =& CRM_Core_Session::singleton();
        $session->set('soap_key', $result[2]);
        $session->set('soap_time', time());
        
        return sha1($result[2]);
    }

    /*** MAILER API ***/
    public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
        $this->verify($key);
        return crm_mailer_event_bounce($job, $queue, $hash, $body);
    }

    public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
        $this->verify($key);
        return crm_mailer_event_unsubscribe($job, $queue, $hash);
    }

    public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
        $this->verify($key);
        return crm_mailer_event_domain_unsubscribe($job, $queue, $hash);
    }

    public function mailer_event_resubscribe($key, $job, $queue, $hash) {
        $this->verify($key);
        return crm_mailer_event_resubscribe($job, $queue, $hash);
    }

    public function mailer_event_subscribe($key, $email, $domain, $group) {
        $this->verify($key);
        return crm_mailer_event_subscribe($email, $domain, $group);
    }

    public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
        $this->verify($key);
        return crm_mailer_event_confirm($contact, $subscribe, $hash);
    }

    public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = null, $fullEmail = null) {
        $this->verify($key);
        return crm_mailer_event_reply($job, $queue, $hash, $bodyTxt, $rt, $bodyHTML, $fullEmail);
    }

    public function mailer_event_forward($key, $job, $queue, $hash, $email) {
        $this->verify($key);
        return crm_mailer_event_forward($job, $queue, $hash, $email);
    }

    public function get_contact($key, $params) { 
        $this->verify($key); 
        require_once 'api/v2/Contact.php';
        return civicrm_contact_get( $params );
    }

}


