<?php

/**
 * Authentication backend using a Sympa Soap server
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     David Pepin <sympa-authors AT cru.fr>
 * Version: 1.3
 * last modified: 2008-02-15
 *
 * Work based on :
 *    ssl authentication backend:
 *       @author     Dominique Launay <dominique.launay AT cru.fr>
 *    htaccess authentication backend:
 *       @author     Samuele Tognini<samuele@cli.di.unipi.it
 *
 * Soap has to be enabled in php to use SoapClient
 *
 **/


define('DOKU_AUTH', dirname(__FILE__));
require_once(DOKU_AUTH.'/basic.class.php');



class auth_sympa extends auth_basic {
  var $users = null;
  var $sympaDefaultGroup = null;
  var $soap_client = null;
  var $sympaSoapService = null;  
  /**
   *
   * Constructor
   *
   * @author  David Pepin <sympa-authors AT cru.fr>
   *
   **/
  function auth_sympa() {
    global $conf;
    if (method_exists($this, 'auth_basic'))
      parent::auth_basic();
      
    // check if the server configuration has correctly been done
    if (empty($conf['plugin']['sympaauth']['sympaSoapService'])) {
      $this->success = false;
      return;
    }
    if (isset($conf['defaultgroup'])) {
      $this->sympaDefaultGroup = $conf['defaultgroup'];
    }
    
    if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])){
      $this->success = true;
      return ;
    }

    $this->sympaSoapService = $conf['plugin']['sympaauth']['sympaSoapService'];
    try{
      $this->soap_client = new SoapClient($this->sympaSoapService);
    }catch(SoapFault $fault){
      $this->success = false;
      return;
    }

    $this->success = true;
    return;
  }



  /**
   * Checks user and password using the Soap frontend to Sympa
   *
   * @author  David Pepin <sympa-authors AT cru.fr>
   *    based on nusoap.php which provides functions to access sympa through soap
   *
   **/

  function checkPass($user,$pass){
    global $conf;
    if (!isset($this->soap_client) || empty($this->soap_client)){
      $this->soap_client = new SoapClient($conf['plugin']['sympaauth']['sympaSoapService']);
    }
    try{
      $auth_key = $this->soap_client->login($user,$pass);
    }catch (SoapFault $fault){
      return(false);
    };
    if(!empty($auth_key)) {
      $_SESSION['sympapass'] = $auth_key;
      return(true);
    }else{
      return(false);
    }
  }



  /**
   * Return user info
   *
   * Accessing to sympa is done through the nusoap library
   *
   * The returned field are :
   *
   * name string  full name of the user 
   * mail string  email addres of the user 
   * groups array   list of groups the user is in
   *
   * @author  David Pepin <sympa-authors AT cru.fr>
   *
   **/
  function getUserData($user) {
    // Name is mail
    $name = $user;

    // mail address
    $mail = $user;


    if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) {
      return ($_SESSION[DOKU_COOKIE]['auth']['info']);
    }else{ 
      // groups the person belongs to
      $grps = array();
      try{
	$res = $this->soap_client->authenticateAndRun($user,$_SESSION['sympapass'],'complexWhich');
      }catch(SoapFault $fault){
	return false;
      }
      if (isset($res) && gettype($res) == 'array') {
	foreach ($res as $list) {
	  if (empty($grps[0])) { 
	    $grps[0] = $list->listAddress;
	  }else{
	    array_unshift($grps,$list->listAddress);
	  }
	  if($list->isOwner == 'true'){
	    $listrequest = preg_replace('/@/','-request@',$list->listAddress);
	    array_unshift($grps,$listrequest);
	  }
	  if ($list->isEditor == 'true') {
	    $listeditor = preg_replace('/@/','-editor@',$list->listAddress);
	    array_unshift($grps,$listeditor);
	  }
	}
      }
      //sympadefaultgroup is put in first position, if set
      if(isset($this->sympaDefaultGroup)) {
	(empty($grps[0]) ? $grps[0] = $this->sympaDefaultGroup : array_unshift($grps,$this->sympaDefaultGroup) );
      }
      
      // Return the collected pieces of information about the user
      $this->users[$user] = compact('name','mail','grps');
      return (isset($this->users[$user]) ? $this->users[$user] : false);
    }
  }


  /**
   * Logoff user : 
   *
   * everything that was about the session is unset
   *
   * @author David Pepin <sympa-authors AT cru.fr>
   *
   **/
  function logOff() {
    unset($this->soap_client);
    unset($_SESSION['sympapass']);
    exit;
  }

  function server() {
    return($this->sympaSoapService);
  }
}
