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;
  }
}