Account.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. <?php
  2. /**
  3. * Zm_Account
  4. *
  5. * @author Yannick Lorenz <ylorenz@1g6.biz>
  6. * @author Fabrizio La Rosa <fabrizio.larosa@unime.it>
  7. * @version 2.2
  8. * @copyright Copyright (c) 2009, Yannick Lorenz
  9. * @copyright Copyright (c) 2012, Fabrizio La Rosa
  10. * @example ../test.php
  11. */
  12. /**
  13. * Zm_Account class documentation
  14. */
  15. // utils.php contains a small collection of useful functions
  16. require_once ("utils.php");
  17. /**
  18. * Zm_Account is a class which allows to manage Zimbra accounts via SOAP
  19. *
  20. * You may create, modify, rename, delete and get the attributes of a Zimbra account using this class
  21. *
  22. * For the usage examples of all class methods check the source code of test.php
  23. */
  24. class Zm_Account
  25. {
  26. /**
  27. * $auth
  28. * @var Zm_Auth $auth soap authentication
  29. */
  30. private $auth;
  31. /**
  32. * Constructor
  33. * @param Zm_Auth $auth soap authentication
  34. */
  35. function __construct($auth)
  36. {
  37. $this->auth = $auth;
  38. }
  39. /**
  40. * getAllAccounts
  41. * @deprecated it may take a long time to complete and fail on servers with lots of accounts
  42. *
  43. * use fetchAccounts instead
  44. * @param string $idOrNameDomain domain id or domain name
  45. * @param string $type value of the domain (auto, name, id)
  46. * @return array informations for all accounts
  47. */
  48. function getAllAccounts($idOrNameDomain, $type="auto")
  49. {
  50. if($type == "auto")
  51. $realType = getDomainType($idOrNameDomain);
  52. else
  53. $realType = $type;
  54. $result = null;
  55. $params = array(
  56. new SoapVar('<ns1:domain by="' . $realType . '">' . $idOrNameDomain . '</ns1:domain>', XSD_ANYXML),
  57. );
  58. try
  59. {
  60. $result = $this->auth->execSoapCall(
  61. "GetAllAccountsRequest",
  62. $params
  63. );
  64. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETALLACCOUNTSRESPONSE']['ACCOUNT'];
  65. }
  66. catch (SoapFault $exception)
  67. {
  68. $result = $exception;
  69. }
  70. return $result;
  71. }
  72. /**
  73. * fetchAccounts
  74. * @param string $ldapQuery LDAP-style filter string (RFC 2254)
  75. * @param array $attrList names of requested attributes
  76. * @param string $nameDomain domain name to restrict search request
  77. * @return array informations for accounts as specified in $ldapQuery
  78. * @author Marc Lamouche <marc.lamouche@ined.fr>
  79. */
  80. function fetchAccounts($ldapQuery, $attrList, $nameDomain = null)
  81. {
  82. $result = null;
  83. $params = array(
  84. new SoapVar('<ns1:query>'.$ldapQuery.'</ns1:query>', XSD_ANYXML),
  85. new SoapParam("accounts", "types"),
  86. new SoapParam(implode(',', $attrList), "attrs"),
  87. new SoapParam("0", "limit"),
  88. );
  89. if ( is_string($nameDomain) ) $params[] = new SoapParam($nameDomain, "domain");
  90. try
  91. {
  92. $response = $this->auth->execSoapCall(
  93. "SearchDirectoryRequest",
  94. $params
  95. );
  96. $result = array();
  97. $resultCount = intval($response['SOAP:ENVELOPE']['SOAP:BODY']['SEARCHDIRECTORYRESPONSE']['SEARCHTOTAL']);
  98. if ( !$resultCount ) return $result;
  99. if ( $resultCount > 1 )
  100. $accountList = &$response['SOAP:ENVELOPE']['SOAP:BODY']['SEARCHDIRECTORYRESPONSE']['ACCOUNT'];
  101. else
  102. $accountList = array(&$response['SOAP:ENVELOPE']['SOAP:BODY']['SEARCHDIRECTORYRESPONSE']['ACCOUNT']);
  103. foreach($accountList as $account)
  104. {
  105. $data = array();
  106. foreach($attrList as $attrName)
  107. $data[$attrName] = getSoapAttribute($account['A'], $attrName, ATTR_MULTIVALUE);
  108. $result[] = $data;
  109. unset($data);
  110. }
  111. }
  112. catch (SoapFault $exception)
  113. {
  114. $result = $exception;
  115. }
  116. return $result;
  117. }
  118. /**
  119. * getAccount
  120. * @param string $name account name
  121. * @return string account
  122. */
  123. function getAccount($name)
  124. {
  125. $result = null;
  126. $params = array(
  127. new SoapVar('<ns1:account by="name">' . $name . '</ns1:account>', XSD_ANYXML),
  128. );
  129. try
  130. {
  131. $result = $this->auth->execSoapCall(
  132. "GetAccountRequest",
  133. $params
  134. );
  135. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE'];
  136. }
  137. catch (SoapFault $exception)
  138. {
  139. $result = $exception;
  140. }
  141. return $result;
  142. }
  143. /**
  144. * getAccountId
  145. * @param string $name account name
  146. * @return string account id
  147. */
  148. function getAccountId($name)
  149. {
  150. $result = null;
  151. $params = array(
  152. new SoapVar('<ns1:account by="name">' . $name . '</ns1:account>', XSD_ANYXML),
  153. );
  154. try
  155. {
  156. $result = $this->auth->execSoapCall(
  157. "GetAccountInfoRequest",
  158. $params
  159. );
  160. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['A'], "zimbraId");
  161. }
  162. catch (SoapFault $exception)
  163. {
  164. $result = $exception;
  165. }
  166. return $result;
  167. }
  168. /**
  169. * accountExists
  170. * @param string $idOrNameAccount account id or account name
  171. * @param string $type value of the account (auto, name, id)
  172. * @return bool exists
  173. */
  174. function accountExists($idOrNameAccount, $type="auto")
  175. {
  176. if($type == "auto")
  177. $realType = getAccountType($idOrNameAccount);
  178. else
  179. $realType = $type;
  180. $result = null;
  181. $params = array(
  182. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  183. );
  184. $options = array(
  185. 'retry' => false,
  186. );
  187. try
  188. {
  189. $result = $this->auth->execSoapCall(
  190. "GetAccountInfoRequest",
  191. $params,
  192. $options
  193. );
  194. }
  195. catch (SoapFault $exception)
  196. {
  197. $result = $exception;
  198. }
  199. return (!is_a($result, "Exception"));
  200. }
  201. /**
  202. * getAccountInfo
  203. * @param string $idOrNameAccount account id or account name
  204. * @param string $type value of the account (auto, name, id)
  205. * @return array informations
  206. */
  207. function getAccountInfo($idOrNameAccount, $type="auto")
  208. {
  209. if($type == "auto")
  210. $realType = getAccountType($idOrNameAccount);
  211. else
  212. $realType = $type;
  213. $result = null;
  214. $params = array(
  215. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  216. );
  217. try
  218. {
  219. $result = $this->auth->execSoapCall(
  220. "GetAccountInfoRequest",
  221. $params
  222. );
  223. }
  224. catch (SoapFault $exception)
  225. {
  226. $result = $exception;
  227. }
  228. return $result;
  229. }
  230. /**
  231. * getAccountOption
  232. * @param string $idOrNameAccount account id or account name
  233. * @param string $optName name of the option to get
  234. * @param int $multisingle (ATTR_SINGLEVALUE, ATTR_MULTIVALUE)
  235. * @param string $type value of the account (auto, name, id)
  236. * @return string option
  237. */
  238. function getAccountOption($idOrNameAccount, $optName, $multisingle=ATTR_SINGLEVALUE, $type="auto")
  239. {
  240. if($type == "auto")
  241. $realType = getAccountType($idOrNameAccount);
  242. else
  243. $realType = $type;
  244. $result = null;
  245. $params = array(
  246. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  247. );
  248. try
  249. {
  250. $result = $this->auth->execSoapCall(
  251. "GetAccountRequest",
  252. $params
  253. );
  254. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'], $optName, $multisingle);
  255. }
  256. catch (SoapFault $exception)
  257. {
  258. $result = $exception;
  259. }
  260. return $result;
  261. }
  262. /**
  263. * getAccountOptions
  264. * @param string $idOrNameAccount account id or account name
  265. * @param string $type value of the account (auto, name, id)
  266. * @return array options
  267. */
  268. function getAccountOptions($idOrNameAccount, $type="auto")
  269. {
  270. if($type == "auto")
  271. $realType = getAccountType($idOrNameAccount);
  272. else
  273. $realType = $type;
  274. $result = null;
  275. $params = array(
  276. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  277. );
  278. try
  279. {
  280. $result = $this->auth->execSoapCall(
  281. "GetAccountRequest",
  282. $params
  283. );
  284. $attrs = array();
  285. foreach ($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'] as $a) {
  286. $attrs[$a['N']] = $a['DATA'];
  287. }
  288. $result = $attrs;
  289. }
  290. catch (SoapFault $exception)
  291. {
  292. $result = $exception;
  293. }
  294. return $result;
  295. }
  296. /**
  297. * createAccount
  298. * @param string $name account name
  299. * @param string $password password
  300. * @param array $attrs an optional array containing the account attributes to be set
  301. * @return string the new account's id
  302. */
  303. function createAccount($name, $password, $attrs = array())
  304. {
  305. $result = null;
  306. $params = array(
  307. new SoapParam($name, "name"),
  308. new SoapParam($password, "password"),
  309. );
  310. foreach ($attrs as $key=>$value)
  311. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  312. try
  313. {
  314. $result = $this->auth->execSoapCall(
  315. "CreateAccountRequest",
  316. $params
  317. );
  318. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['CREATEACCOUNTRESPONSE']['ACCOUNT']['ID'];
  319. usleep(250000); // introduce a small delay, otherwise some troubles may arise if we modify the new account right after its creation
  320. }
  321. catch (SoapFault $exception)
  322. {
  323. $result = $exception;
  324. }
  325. return $result;
  326. }
  327. /**
  328. * setAccountPassword
  329. * @param string $idOrNameAccount account id or account name
  330. * @param string $password password
  331. * @param string $type value of the account (auto, name, id)
  332. * @return array informations
  333. */
  334. function setAccountPassword($idOrNameAccount, $password, $type="auto")
  335. {
  336. if($type == "auto")
  337. $realType = getAccountType($idOrNameAccount);
  338. else
  339. $realType = $type;
  340. if($realType == "name")
  341. $accountId = $this->getAccountId($idOrNameAccount);
  342. else
  343. $accountId = $idOrNameAccount;
  344. $result = null;
  345. $params = array(
  346. new SoapParam($accountId, "id"),
  347. new SoapParam($password, "newPassword"),
  348. );
  349. try
  350. {
  351. $result = $this->auth->execSoapCall(
  352. "SetPasswordRequest",
  353. $params
  354. );
  355. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['SETPASSWORDRESPONSE'];
  356. }
  357. catch (SoapFault $exception)
  358. {
  359. $result = $exception;
  360. }
  361. return $result;
  362. }
  363. /**
  364. * modifyAccount
  365. * @param string $idOrNameAccount account id or account name
  366. * @param array $attrs an array containing the account attributes to be set
  367. * @param string $type value of the account (auto, name, id)
  368. * @return array informations
  369. */
  370. function modifyAccount($idOrNameAccount, $attrs = array(), $type="auto")
  371. {
  372. if($type == "auto")
  373. $realType = getAccountType($idOrNameAccount);
  374. else
  375. $realType = $type;
  376. if($realType == "name")
  377. $accountId = $this->getAccountId($idOrNameAccount);
  378. else
  379. $accountId = $idOrNameAccount;
  380. $result = null;
  381. $params = array(
  382. new SoapParam($accountId, "id"),
  383. );
  384. foreach ($attrs as $key=>$value)
  385. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  386. try
  387. {
  388. $result = $this->auth->execSoapCall(
  389. "ModifyAccountRequest",
  390. $params
  391. );
  392. }
  393. catch (SoapFault $exception)
  394. {
  395. $result = $exception;
  396. }
  397. return $result;
  398. }
  399. /**
  400. * renameAccount
  401. * @param string $idOrNameAccount account id or account name
  402. * @param string $newName new account name
  403. * @param string $type value of the account (auto, name, id)
  404. * @return array informations
  405. */
  406. function renameAccount($idOrNameAccount, $newName, $type="auto")
  407. {
  408. if($type == "auto")
  409. $realType = getAccountType($idOrNameAccount);
  410. else
  411. $realType = $type;
  412. if($realType == "name")
  413. $accountId = $this->getAccountId($idOrNameAccount);
  414. else
  415. $accountId = $idOrNameAccount;
  416. $result = null;
  417. $params = array(
  418. new SoapParam($accountId, "id"),
  419. new SoapParam($newName, "newName"),
  420. );
  421. try
  422. {
  423. $result = $this->auth->execSoapCall(
  424. "RenameAccountRequest",
  425. $params
  426. );
  427. }
  428. catch (SoapFault $exception)
  429. {
  430. $result = $exception;
  431. }
  432. return $result;
  433. }
  434. /**
  435. * deleteAccount
  436. * @param string $idOrNameAccount account id or account name
  437. * @param string $type value of the account (auto, name, id)
  438. * @return array informations
  439. */
  440. function deleteAccount($idOrNameAccount, $type="auto")
  441. {
  442. if($type == "auto")
  443. $realType = getAccountType($idOrNameAccount);
  444. else
  445. $realType = $type;
  446. if($realType == "name")
  447. $accountId = $this->getAccountId($idOrNameAccount);
  448. else
  449. $accountId = $idOrNameAccount;
  450. $result = null;
  451. $params = array(
  452. new SoapParam($accountId, "id"),
  453. );
  454. try
  455. {
  456. $result = $this->auth->execSoapCall(
  457. "DeleteAccountRequest",
  458. $params
  459. );
  460. }
  461. catch (SoapFault $exception)
  462. {
  463. $result = $exception;
  464. }
  465. return $result;
  466. }
  467. /**
  468. * getAccountAliases
  469. * @param string $idOrNameAccount account id or account name
  470. * @param string $type value of the account (auto, name, id)
  471. * @return array aliases
  472. */
  473. function getAccountAliases($idOrNameAccount, $type="auto")
  474. {
  475. return $this->getAccountOption($idOrNameAccount, "zimbraMailAlias", ATTR_MULTIVALUE, $type);
  476. }
  477. /**
  478. * addAccountAlias
  479. * @param string $idOrNameAccount account id or account name
  480. * @param string $alias account alias
  481. * @param string $type value of the account (auto, name, id)
  482. * @return array informations
  483. */
  484. function addAccountAlias($idOrNameAccount, $alias, $type="auto")
  485. {
  486. if($type == "auto")
  487. $realType = getAccountType($idOrNameAccount);
  488. else
  489. $realType = $type;
  490. if($realType == "name")
  491. $accountId = $this->getAccountId($idOrNameAccount);
  492. else
  493. $accountId = $idOrNameAccount;
  494. $result = null;
  495. $params = array(
  496. new SoapParam($accountId, "id"),
  497. new SoapParam($alias, "alias"),
  498. );
  499. try
  500. {
  501. $result = $this->auth->execSoapCall(
  502. "AddAccountAliasRequest",
  503. $params
  504. );
  505. }
  506. catch (SoapFault $exception)
  507. {
  508. $result = $exception;
  509. }
  510. return $result;
  511. }
  512. /**
  513. * removeAccountAlias
  514. * @param string $idOrNameAccount account id or account name
  515. * @param string $alias account alias
  516. * @param string $type value of the account (auto, name, id)
  517. * @return array informations
  518. */
  519. function removeAccountAlias($idOrNameAccount, $alias, $type="auto")
  520. {
  521. if($type == "auto")
  522. $realType = getAccountType($idOrNameAccount);
  523. else
  524. $realType = $type;
  525. if($realType == "name")
  526. $accountId = $this->getAccountId($idOrNameAccount);
  527. else
  528. $accountId = $idOrNameAccount;
  529. $result = null;
  530. $params = array(
  531. new SoapParam($accountId, "id"),
  532. new SoapParam($alias, "alias"),
  533. );
  534. try
  535. {
  536. $result = $this->auth->execSoapCall(
  537. "RemoveAccountAliasRequest",
  538. $params
  539. );
  540. }
  541. catch (SoapFault $exception)
  542. {
  543. $result = $exception;
  544. }
  545. return $result;
  546. }
  547. /**
  548. * getAccountStatus
  549. * @param string $idOrNameAccount account id or account name
  550. * @param string $type value of the account (auto, name, id)
  551. * @return string status
  552. */
  553. function getAccountStatus($idOrNameAccount, $type="auto")
  554. {
  555. return $this->getAccountOption($idOrNameAccount, "zimbraAccountStatus", ATTR_SINGLEVALUE, $type);
  556. }
  557. /**
  558. * setAccountStatus
  559. * @param string $idOrNameAccount account id or account name
  560. * @param string $status the status (active, maintenance, pending, locked, closed)
  561. * @param string $type value of the account (auto, name, id)
  562. * @return array informations
  563. */
  564. function setAccountStatus($idOrNameAccount, $status, $type = "auto")
  565. {
  566. $hideInGAL = ($status == "active") ? "FALSE" : "TRUE";
  567. $attrs = array(
  568. "zimbraAccountStatus"=>$status,
  569. "zimbraHideInGal"=>$hideInGAL,
  570. );
  571. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  572. return $result;
  573. }
  574. /**
  575. * expireAccountSessions
  576. * @param string $idOrNameAccount account id or account name
  577. * @param string $type value of the account (auto, name, id)
  578. * @return array informations
  579. */
  580. function expireAccountSessions($idOrNameAccount, $type = "auto")
  581. {
  582. $attrName = "zimbraAuthTokenValidityValue";
  583. $oldValue = $this->getAccountOption($idOrNameAccount, $attrName);
  584. $newValue = rand($oldValue+1, 1024);
  585. $attrs = array($attrName=>$newValue);
  586. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  587. return $result;
  588. }
  589. /**
  590. * getAccountCos
  591. * @param string $idOrNameAccount account id or account name
  592. * @param string $returnType get the COS ID or NAME
  593. * @param string $type value of the account (auto, name, id)
  594. * @return string COS id or name
  595. */
  596. function getAccountCos($idOrNameAccount, $returnType = "NAME", $type = "auto")
  597. {
  598. if($type == "auto")
  599. $realType = getAccountType($idOrNameAccount);
  600. else
  601. $realType = $type;
  602. if($realType == "name")
  603. $accountId = $this->getAccountId($idOrNameAccount);
  604. else
  605. $accountId = $idOrNameAccount;
  606. $result = null;
  607. $params = array(
  608. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  609. );
  610. try
  611. {
  612. $result = $this->auth->execSoapCall(
  613. "GetAccountInfoRequest",
  614. $params
  615. );
  616. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['COS'][$returnType];
  617. }
  618. catch (SoapFault $exception)
  619. {
  620. $result = $exception;
  621. }
  622. return $result;
  623. }
  624. /**
  625. * setAccountCos
  626. * @param string $idOrNameAccount account id or account name
  627. * @param string $cosName the COS name
  628. * @param string $type value of the account (auto, name, id)
  629. * @return array informations
  630. */
  631. function setAccountCos($idOrNameAccount, $cosName, $type = "auto")
  632. {
  633. $cosId = $this->getCosId($cosName);
  634. $attrs = array("zimbraCOSId"=>$cosId);
  635. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  636. return $result;
  637. }
  638. /**
  639. * getCosId
  640. * @param string $name the COS name
  641. * @return string COS id
  642. */
  643. function getCosId($name)
  644. {
  645. $result = null;
  646. $params = array(
  647. new SoapVar('<ns1:cos by="name">' . $name . '</ns1:cos>', XSD_ANYXML),
  648. );
  649. try
  650. {
  651. $result = $this->auth->execSoapCall(
  652. "GetCosRequest",
  653. $params
  654. );
  655. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETCOSRESPONSE']['COS']['ID'];
  656. }
  657. catch (SoapFault $exception)
  658. {
  659. $result = $exception;
  660. }
  661. return $result;
  662. }
  663. /**
  664. * getAllCos
  665. * @return array informations
  666. */
  667. function getAllCos()
  668. {
  669. $result = null;
  670. try
  671. {
  672. $result = $this->auth->execSoapCall(
  673. "GetAllCosRequest"
  674. );
  675. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETALLCOSRESPONSE'];
  676. }
  677. catch (SoapFault $exception)
  678. {
  679. $result = $exception;
  680. }
  681. return $result;
  682. }
  683. }
  684. ?>