View Single Post
  #7 (permalink)  
Old 06-08-2007, 06:42 PM
gettyless gettyless is offline
Loyal Member
 
Posts: 92
Default my modified version as a class

Thanks for sharing your code - I can verify that it works in my test environment! Using your code, I can create a user programmatically through PHP and SOAP. For my test machine, I have win32 apache running - so I had to enable these extensions in my php.ini:

Code:
extension=php_openssl.dll
extension=php_soap.dll
extension=php_curl.dll
I'd like to be able to quickly crank out scripts to do what I an across multiple account, for whatever features that aren't officially implemented yet .. if I really need that feature. For example, I'd like to auto-subscribe all employees to the company calendar.

So, I will probably reuse the authentication code. I've rewritten it into an OO way not sure if it's the best way, design-wise, but it's a start:

Code:
<form method="POST" action="soap.php">
username<br>
<input type="text" name="username"><br>
password<br>
<input type="text" name="password"><br>
<input type="submit" name="submit" value="submit"><br>
</form>

<?php
if (count($_POST))
{
    $password = $_POST['password'];
    $username = $_POST['username'];

    $ZimbraAuth = new ZimbraAuth($username, $password); 
    echo $ZimbraAuth->exec();
}

class ZimbraAuth
{
    private $client; 
    private $params; 
    
    function __construct($username, $password)
    {

        $this->client = new SoapClient(null,
            array(
                'location' => "https://zimbra.microsoft.com:7071/service/admin/soap/",
                'uri' => "urn:zimbraAdmin",
                'trace' => 1,
                'exceptions' => 1,
                'soap_version' => SOAP_1_1,
                'style' => SOAP_RPC,
                'use' => SOAP_LITERAL
            )
        );
        
        $this->params = array( new SoapParam($username, "name"), new SoapParam($password, "password") );
        
    }
    
    function exec()
    {
        try 
        {
            //echo "creating header...<br>";
            $soapHeader = new SoapHeader('urn:zimbra','context');
        
            //echo "trying...<br>";        
            $result = $this->client->__soapCall("AuthRequest", $this->params, null,$soapHeader);
            //echo "executed...<br>";    
        } 
        catch (SoapFault $exception) 
        {
            echo "exception caught!...<br><br>EXCEPTION START <<<<<<<<<<< <p>";
            echo $exception . "<br><br>";
            echo ">>>>>>>>>>>> EXCEPTION END<p>";
        
            echo htmlentities($client->__getLastRequest()) . "<br><br>";
            echo htmlentities($client->__getLastRequestHeaders()) . "<br><br>";
            echo htmlentities($client->__getLastResponseHeaders()) . "<br><br>";
            echo htmlentities($client->__getLastResponse()) . "<br><br>";
        
        }

        //echo "authToken says: ".$result['authToken'] . "<br>";
        //echo "logged in, continuing<br>";
        return $result['authToken'];
            
            
    }
    
} 
?>
I'm guessing it would make sense to have a separate ZimbraAuth class, like how people often have a separate database authentication class. Then I use the token for something like $ZimbraUser->create($authToken) or $ZimbraUser->ChangePassword($old, $new, $authToken).
Reply With Quote