Tuesday, March 9, 2010

PHP Class for Windows Live Admin Center

I've been using Windows Live Admin Center for quite some time, so I though it would be a good idea to directly interact with the provided API's, rather than manually manipulating each user.
The SOAP API's provided are used by the Windows Live Admin Center SDK, which can be downloaded from Microsoft's website.

I quickly decided to write a PHP class that extends the Admin Center functions exposed by the API to be natively available to PHP.
Hopefully, someone else will find it useful in the not too distant future.

/////////////////////////////////////////
// Windows Live Admin Center PHP Class //
// Author: Joshua "JD" Davison         //
// Email:  jd [at] ozchat [dot] org    //
/////////////////////////////////////////


class AdminCenter {
  public function __construct() {
    $this->client=new SoapClient('https://domains.live.com/service/managedomain2.asmx?WSDL');
  }


  private function doPost($url, $data) {
    $ctx=stream_context_create(array('http'=>array('method'=>'POST','content'=>$data)));
    $fp=@fopen($url,'rb',false,$ctx);
    if (!$fp) { return; }
    return @stream_get_contents($fp);
  }


  public function LiveLogin($memberName,$password) {
    $this->LoginURL=$this->GetLoginURL($memberName);
    $this->LoginDataTemplate=$this->GetLoginDataTemplate();
    $LoginData=str_replace(array("%NAME%","%PASSWORD%"),array($memberName,$password),$this->LoginDataTemplate);
    $namespace='http://domains.live.com/Service/ManageDomain/V1.0';
    $name='ManageDomain2Authorization';
    $this->authData=$this->doPost($this->LoginURL,$LoginData);
    $headers=new SoapHeader($namespace,$name,array('authorizationType'=>'PassportTicket','authorizationData'=>$this->authData));
    $this->client->__setSoapHeaders($headers);
  }


  public function GetLoginUrl($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->GetLoginUrl($req)->GetLoginUrlResult;
  }


  public function GetLoginDataTemplate() {
    return $this->client->GetLoginDataTemplate()->GetLoginDataTemplateResult;
  }


  public function VerifyAuthData($authData) {
    $req->authData=$authData;
    return $this->client->VerifyAuthData($req)->VerifyAuthDataResult;
  }


  public function CreateMember($memberName, $password, $resetPassword=false, $firstName="", $lastName="", $lcid="") {
    $req->memberNameIn=$memberName;
    $req->password=$password;
    $req->resetPassword=$resetPassword;
    $req->firstName=$firstName;
    $req->lastName=$lastName;
    $req->lcid=$lcid;
    return $this->client->CreateMember($req)->CreateMemberResult;
  }


  public function DeleteMember($memberName) {
    //Be careful when using this function. Deleted accounts will lose all e-mail. This Passport account will be placed in a forced rename state; so the next time this member logs in, the member will be asked to change that Passport account name, but the member won’t be able to change it to a name within this domain because now this domain name is reserved by Admin Center.
    $req->memberNameIn=$memberName;
    return $this->client->DeleteMember($req)->DeleteMemberResult;
  }


  public function EnumMembers($domainName,$start="",$num=5000) {
    $req->domainName=$domainName;
    $req->start=$start;
    $req->num=$num;
    return $this->client->EnumMembers($req)->EnumMembersResult;
  }


  public function EvictUnmanagedMember($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->EvictUnmanagedMember($req)->EvictUnmanagedMemberResult;
  }


  public function GetMemberCount($domainName) {
    $req->domainName=$domainName;
    return $this->client->GetMemberCount($req)->GetMemberCountResult;
  }


  public function GetMemberInfo($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->GetMemberInfo($req)->GetMemberInfoResult;
  }


  public function GetMemberNameState($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->GetMemberNameState($req)->GetMemberNameStateResult;
  }


  public function ImportUnmanagedMember($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->ImportUnmanagedMember($req)->ImportUnmanagedMemberResult;
  }


  public function MemberNameToNetId($memberName) {
    $req->memberNameIn=$memberName;
    return $this->client->MemberNameToNetId($req)->MemberNameToNetIdResult;
  }


  public function NetIdToMemberName($netid) {
    $req->netIdIn=$netid;
    return $this->client->NetIdToMemberName($req)->NetIdToMemberNameResult;
  }


  public function RenameMember($memberNameOld,$memberNameNew) {
    $req->memberNameOldIn=$memberNameOld;
    $req->memberNameNewIn=$memberNameNew;
    return $this->client->RenameMember($req)->RenameMemberResult;
  }


  public function SetMemberEmailAccess($memberName,$emailAccess) {
    $req->memberNameIn=$memberName;
    $req->value=$emailAccess;
    return $this->client->SetMemberEmailAccess($req)->SetMemberEmailAccessResult;
  }


  public function EnumDomains() {
    return $this->client->EnumDomains()->EnumDomainsResult;
  }


  public function GetDomainInfo($domainName) {
    $req->domainName=$domainName;
    return $this->client->GetDomainInfo($req)->GetDomainInfoResult;
  }


  public function TestConnection($testString) {
    $req->name=$testString;
    return $this->client->TestConnection($req)->TestConnectionResult;
  }
}

5 comments:

  1. i am gettin this error can u help me with this

    Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://domains.live.com/service/managedomain2.asmx?WSDL' : failed to load external entity "https://domains.live.com/service/managedomain2.asmx?WSDL" in C:\xampp\htdocs\adminedu1\soapclass1.php:11 Stack trace: #0 C:\xampp\htdocs\adminedu1\soapclass1.php(11): SoapClient->SoapClient('https://domains...') #1 C:\xampp\htdocs\adminedu1\index.php(11): AdminCenter->__construct() #2 {main} thrown in C:\xampp\htdocs\adminedu1\soapclass1.php on line 11

    ReplyDelete
  2. I'm not sure to be honest, although I'd recommend you check that your PHP configuration allows sockets to external locations.

    How are you attempting to load it?

    Something like this should work (well, does for me):
    $WL->User="someone@example.net";
    $WL->Password="SomeRanDomPasSw0rd";
    $WLAC=new AdminCenter;
    $WLAC->LiveLogin($WL->User,$WL->Password);
    echo $WLAC->TestConnection("Connection test");

    ReplyDelete
  3. Hello. This class is just that I need for manage accounts under Windows live (now Outlook.com)

    I have copied the php class and try it under last line:

    echo LiveLogin("myaccount@outlook.com","mypassword");

    and i'm getting: Fatal error: Call to undefined function livelogin() in...... why??

    Please sample of use !! pls...

    Thanks!

    ReplyDelete
  4. sorry for not read first your comment with example !! I tried it and Works OK!!! Feb-2013 and stills work!

    ReplyDelete
  5. I'm very surprised (however, pleased) that this still works as expected.

    ReplyDelete