View Single Post
  #7 (permalink)  
Old 07-08-2007, 12:46 PM
mikes2277 mikes2277 is offline
Member
 
Posts: 11
Default DeleteAccount Function

And because I'm such a nice and cool guy here is the code for deleting an account:

Code:
function ZimbraAdminDeleteAccount($Trace, $ServerAddress, $AdminUserName, $AdminPassword, $DeleteUserName)
{
        $CurlHandle = curl_init();
        curl_setopt($CurlHandle, CURLOPT_URL,           "https://$ServerAddress:7071/service/admin/soap");
        curl_setopt($CurlHandle, CURLOPT_POST,           TRUE);
        curl_setopt($CurlHandle, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYHOST, FALSE);

        // ------ Send the zimbraAdmin AuthRequest -----

        $SOAPMessage  = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                                <soap:Header>
                                        <context xmlns="urn:zimbra"/>
                                </soap:Header>
                                <soap:Body>
                                        <AuthRequest xmlns="urn:zimbraAdmin">
                                                <name>' . $AdminUserName . '</name>
                                                <password>' . $AdminPassword . '</password>
                                        </AuthRequest>
                                </soap:Body>
                        </soap:Envelope>';

        curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $SOAPMessage);

        if(!($ZimbraSOAPResponse = curl_exec($CurlHandle)))
        {
                print("ERROR: curl_exec - (" . curl_errno($CurlHandle) . ") " . curl_error($CurlHandle));
                return(FALSE);
        }

        // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");

        // Parse for the sessionId
        // <sessionId type="admin" id="123">123</sessionId>
        $sessionId = strstr($ZimbraSOAPResponse, "<sessionId");
        $sessionId = strstr($sessionId, ">");
        $sessionId = substr($sessionId, 1, strpos($sessionId, "<") - 1);
        // print("sessionId = $sessionId<BR>\n");

        // Parse for the authToken
        // <authToken>123</authToken>
        $authToken = strstr($ZimbraSOAPResponse, "<authToken");
        $authToken = strstr($authToken, ">");
        $authToken = substr($authToken, 1, strpos($authToken, "<") - 1);
        // print("authToken = $authToken<BR>\n");


        // ------ Send the GetAccountRequest request -----
        $SOAPMessage = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                                <soap:Header>
                                        <context xmlns="urn:zimbra">
                                                <authToken>' . $authToken . '</authToken>
                                                <sessionId id="' . $sessionId . '">' . $sessionId . '</sessionId>
                                        </context>
                                </soap:Header>
                                <soap:Body>
                                        <GetAccountRequest xmlns="urn:zimbraAdmin">
                                                <account by="name">' . $DeleteUserName . '</account>
                                        </GetAccountRequest>
                                </soap:Body>
                        </soap:Envelope>';

        curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $SOAPMessage);

        if(!($ZimbraSOAPResponse = curl_exec($CurlHandle)))
        {
                print("ERROR: curl_exec - (" . curl_errno($CurlHandle) . ") " . curl_error($CurlHandle));
                return(FALSE);
        }

        // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");

        // Parse for zimbraId
        // <a n="zimbraId">0ffaa258-048f-47f2-9efd-5901f2fd6913</a>
        $zimbraId = strstr($ZimbraSOAPResponse, "<a n=\"zimbraId\"");
        $zimbraId = strstr($zimbraId, ">");
        $zimbraId = substr($zimbraId, 1, strpos($zimbraId, "<") - 1);
        // print("zimbraId = $zimbraId<BR>\n");
        // Check if we got a zimbraId
        if($zimbraId == "")
        {
                // No zimbraId found!  This usually means that the account is not found.  You can parse the message if you really want to know
                // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");
                return(FALSE);
        }

        // ------ Send the zimbraCreateAccount request -----
        $SOAPMessage = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                                <soap:Header>
                                        <context xmlns="urn:zimbra">
                                                <authToken>' . $authToken . '</authToken>
                                                <sessionId id="' . $sessionId . '">' . $sessionId . '</sessionId>
                                        </context>
                                </soap:Header>
                                <soap:Body>
                                        <DeleteAccountRequest xmlns="urn:zimbraAdmin">
                                                <id>' . $zimbraId . '</id>
                                        </DeleteAccountRequest>
                                </soap:Body>
                        </soap:Envelope>';

        curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $SOAPMessage);

        if(!($ZimbraSOAPResponse = curl_exec($CurlHandle)))
        {
                print("ERROR: curl_exec - (" . curl_errno($CurlHandle) . ") " . curl_error($CurlHandle));
                return(FALSE);
        }

        // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");

        return($ZimbraSOAPResponse);
}
Reply With Quote