Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PHP Script Auth

  1. #1
    mirkocoz is offline Starter Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    7

    Default PHP Script Auth

    Exists any example script in PHP for authentication in Zimbra?

  2. #2
    ljm
    ljm is offline Project Contributor
    Join Date
    May 2006
    Posts
    81
    Rep Power
    9

    Default

    This thread might be of interest:
    [SOLVED] PHP Class for easy access to Zimbra SOAP API
    Zindus - contact sync for Thunderbird and Zimbra

  3. #3
    tjpatter is offline Junior Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    7

    Default Here you go!

    This is a PHP function that I wrote... It utilizes php_curl. Now if only I could find a way to make Zimbra redirect to my custom front-end page upon logout... Any ideas?

    We implemented our custom front-end to enforce password rules, etc.

    Here is the function:

    PHP Code:
    function zimbraLogin($username$password$client)
        {
            
    // Bring the zimbra server value into this function:
            
    global $zimbra_server;  // In the format: https://yourdomain.com/
            
            
    if($client == "")
                
    $client "preferred";
            
            
    // Attempt login to zimbra server:
            
    $ch curl_init();
        
            
    $crap fopen ("/dev/null""w"); // We don't want to log curl's stderr output - contains passwords...
            
            
    curl_setopt($chCURLOPT_URL$zimbra_server);
            
    curl_setopt($chCURLOPT_VERBOSE0); 
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0); // Stop Curl from validating SSL cert - needed for self-signed...
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
            
    curl_setopt($chCURLOPT_FAILONERROR0);
            
    curl_setopt($chCURLOPT_FOLLOWLOCATION0); // Allow redirects
            
    curl_setopt($chCURLOPT_RETURNTRANSFER1); // Return into a variable
            
    curl_setopt($chCURLOPT_TIMEOUT0); // Time out setting
            
    curl_setopt($chCURLOPT_HEADER1);  // Return the headers along with the output...
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_STDERR$crap);
            
    curl_setopt($chCURLOPT_POSTFIELDS"loginOp=login&username=$username&password=$password&client=$client");

            
    $result curl_exec($ch);
            
            
    curl_close($ch);        
            
    fclose($crap);
            
            
    $values explode("\n",$result);

            
    // Search through the headers to find the Location value to pass to the browser:
            
    foreach($values as $key => $value)
            {
                list(
    $start$good) = split(':'$value);
                if(
    $start == "Location")
                {
                    
    header($values[$key]);
                    die;
                }
            }        
            
            
    // If we get here that means that no location header was found - most likely incorrect password, zimbra is down, etc.
            
    return false;
        } 

  4. #4
    mmorse's Avatar
    mmorse is offline Moderator
    Join Date
    May 2006
    Location
    USA
    Posts
    6,242
    Rep Power
    21

    Default

    Quote Originally Posted by tjpatter View Post
    Now if only I could find a way to make Zimbra redirect to my custom front-end page upon logout... Any ideas?
    su - zimbra
    zmprov mcf zimbraWebClientLogoutURL http://www.zimbra.com
    or
    zmprov md domain.com zimbraWebClientLogoutURL http://www.zimbra.com

    I believe for the zimbraWebClientLogoutURL to work, you might need to create a virtual domain for all possible redirects:
    zmprov md domain.com +zimbraVirtualHostname http://www.zimbra.com
    (You can do this from the admin console gui as well.)
    It should come right up, but a zmmailboxdctl restart may be needed.

  5. #5
    xtremetoonz is offline Active Member
    Join Date
    Nov 2005
    Posts
    33
    Rep Power
    9

    Default why PHP? Seems basic form auth works.

    Quote Originally Posted by mirkocoz View Post
    Exists any example script in PHP for authentication in Zimbra?
    We managed to get the following to work:

    Code:
    <form name="form1" method="post" action="https://zimbra.domain.com/zimbra/">
    <input name="username" type="text" size="20"/>
    <input name="password" type="password" size="20" />                                                                                                                                                                                                                                                 <input type="hidden" name="loginOp" value="login"/>
    <input type="hidden" name="client" value="preferred"/>
    <input type="submit" name="Submit" value="Submit"/>
    </form>

  6. #6
    BraveStarr is offline Intermediate Member
    Join Date
    May 2008
    Posts
    17
    Rep Power
    7

    Default

    If you clear your cookies and then do basic form auth, it won't work.

    We have this problem if people have never been to the webmail client. They receive an error when trying to login from our custom form page.

    Any idea if this php script would have the same problem?

    Thanks,
    - J

  7. #7
    tjpatter is offline Junior Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    7

    Default

    Quote Originally Posted by tjpatter View Post
    This is a PHP function that I wrote... It utilizes php_curl. Now if only I could find a way to make Zimbra redirect to my custom front-end page upon logout... Any ideas?

    We implemented our custom front-end to enforce password rules, etc.

    Here is the function:

    PHP Code:
    function zimbraLogin($username$password$client)
        {
            
    // Bring the zimbra server value into this function:
            
    global $zimbra_server;  // In the format: https://yourdomain.com/
            
            
    if($client == "")
                
    $client "preferred";
            
            
    // Attempt login to zimbra server:
            
    $ch curl_init();
        
            
    $crap fopen ("/dev/null""w"); // We don't want to log curl's stderr output - contains passwords...
            
            
    curl_setopt($chCURLOPT_URL$zimbra_server);
            
    curl_setopt($chCURLOPT_VERBOSE0); 
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0); // Stop Curl from validating SSL cert - needed for self-signed...
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
            
    curl_setopt($chCURLOPT_FAILONERROR0);
            
    curl_setopt($chCURLOPT_FOLLOWLOCATION0); // Allow redirects
            
    curl_setopt($chCURLOPT_RETURNTRANSFER1); // Return into a variable
            
    curl_setopt($chCURLOPT_TIMEOUT0); // Time out setting
            
    curl_setopt($chCURLOPT_HEADER1);  // Return the headers along with the output...
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_STDERR$crap);
            
    curl_setopt($chCURLOPT_POSTFIELDS"loginOp=login&username=$username&password=$password&client=$client");

            
    $result curl_exec($ch);
            
            
    curl_close($ch);        
            
    fclose($crap);
            
            
    $values explode("\n",$result);

            
    // Search through the headers to find the Location value to pass to the browser:
            
    foreach($values as $key => $value)
            {
                list(
    $start$good) = split(':'$value);
                if(
    $start == "Location")
                {
                    
    header($values[$key]);
                    die;
                }
            }        
            
            
    // If we get here that means that no location header was found - most likely incorrect password, zimbra is down, etc.
            
    return false;
        } 
    I have a fix that corrects issues with browsers that have had their cookies cleared. Replace the foreach loop in my original code with the following:

    PHP Code:
    // Search through the headers to find the Location value to pass to the browser:
            
    foreach($values as $key => $value)
            {
                list(
    $start$good) = split(':'$value);
                
                
    // Found an anamoly where people with cleared cookies have to log in twice...
                // Fix it by taking the auth token out of the cookie and passing it through as a URL
                
    if($start == "Set-Cookie")
                {
                    
    $newurl $zimbra_server "?client=$client&zauthtoken=" $values[$key];
                    
    header("Location: $newurl");
                    die;
                }
                            
                if(
    $start == "Location")
                {
                    
    header($values[$key]);
                    die;
                }
            } 

  8. #8
    BraveStarr is offline Intermediate Member
    Join Date
    May 2008
    Posts
    17
    Rep Power
    7

    Default

    Thanks tjpatter!

    I'll give this a try...

    - J

  9. #9
    BraveStarr is offline Intermediate Member
    Join Date
    May 2008
    Posts
    17
    Rep Power
    7

    Default

    The only headers I ever get are these:

    LabelPragma: no-cache
    Content-Language: en-US
    Transfer-Encoding: chunked
    Cache-Control: no-store, no-cache, must-revalidate, max-age=0
    Content-Type: text/html; charset=utf-8
    Date: Tue, 07 Oct 2008 21:20:22 GMT
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Set-Cookie: ZM_TEST=true


    I never get a Location key and the Set-Cookie value doesn't look very helpful...

    Any pointers would be great!

    Thanks,
    - J

  10. #10
    tjpatter is offline Junior Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    7

    Default Oops...

    Forgot to include this in the modified code for my script:

    PHP Code:
    curl_setopt($chCURLOPT_COOKIE'ZM_TEST=true');
    curl_setopt($chCURLOPT_REFERER"$zimbra_server"); 
    Add that line with the rest of the CURLOPTs in my modified code.

    Good catch BraveStarr!

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 658
    Last Post: 04-04-2014, 09:01 AM
  2. Simple backup script for OSE version
    By soulskater in forum Administrators
    Replies: 64
    Last Post: 08-03-2011, 07:47 AM
  3. Replies: 8
    Last Post: 11-30-2007, 11:12 AM
  4. Zimbra + Samba LDAP auth problems
    By fajarpri in forum Installation
    Replies: 3
    Last Post: 07-04-2007, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •