Account.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. * getAccountId
  120. * @param string $name account name
  121. * @return string account id
  122. */
  123. function getAccountId($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. "GetAccountInfoRequest",
  133. $params
  134. );
  135. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['A'], "zimbraId");
  136. }
  137. catch (SoapFault $exception)
  138. {
  139. $result = $exception;
  140. }
  141. return $result;
  142. }
  143. /**
  144. * accountExists
  145. * @param string $idOrNameAccount account id or account name
  146. * @param string $type value of the account (auto, name, id)
  147. * @return bool exists
  148. */
  149. function accountExists($idOrNameAccount, $type="auto")
  150. {
  151. if($type == "auto")
  152. $realType = getAccountType($idOrNameAccount);
  153. else
  154. $realType = $type;
  155. $result = null;
  156. $params = array(
  157. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  158. );
  159. $options = array(
  160. 'retry' => false,
  161. );
  162. try
  163. {
  164. $result = $this->auth->execSoapCall(
  165. "GetAccountInfoRequest",
  166. $params,
  167. $options
  168. );
  169. }
  170. catch (SoapFault $exception)
  171. {
  172. $result = $exception;
  173. }
  174. return (!is_a($result, "Exception"));
  175. }
  176. /**
  177. * getAccountInfo
  178. * @param string $idOrNameAccount account id or account name
  179. * @param string $type value of the account (auto, name, id)
  180. * @return array informations
  181. */
  182. function getAccountInfo($idOrNameAccount, $type="auto")
  183. {
  184. if($type == "auto")
  185. $realType = getAccountType($idOrNameAccount);
  186. else
  187. $realType = $type;
  188. $result = null;
  189. $params = array(
  190. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  191. );
  192. try
  193. {
  194. $result = $this->auth->execSoapCall(
  195. "GetAccountInfoRequest",
  196. $params
  197. );
  198. }
  199. catch (SoapFault $exception)
  200. {
  201. $result = $exception;
  202. }
  203. return $result;
  204. }
  205. /**
  206. * getAccountOption
  207. * @param string $idOrNameAccount account id or account name
  208. * @param string $optName name of the option to get
  209. * @param int $multisingle (ATTR_SINGLEVALUE, ATTR_MULTIVALUE)
  210. * @param string $type value of the account (auto, name, id)
  211. * @return string option
  212. */
  213. function getAccountOption($idOrNameAccount, $optName, $multisingle=ATTR_SINGLEVALUE, $type="auto")
  214. {
  215. if($type == "auto")
  216. $realType = getAccountType($idOrNameAccount);
  217. else
  218. $realType = $type;
  219. $result = null;
  220. $params = array(
  221. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  222. );
  223. try
  224. {
  225. $result = $this->auth->execSoapCall(
  226. "GetAccountRequest",
  227. $params
  228. );
  229. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'], $optName, $multisingle);
  230. }
  231. catch (SoapFault $exception)
  232. {
  233. $result = $exception;
  234. }
  235. return $result;
  236. }
  237. /**
  238. * getAccountOptions
  239. * @param string $idOrNameAccount account id or account name
  240. * @param string $type value of the account (auto, name, id)
  241. * @return array options
  242. */
  243. function getAccountOptions($idOrNameAccount, $type="auto")
  244. {
  245. if($type == "auto")
  246. $realType = getAccountType($idOrNameAccount);
  247. else
  248. $realType = $type;
  249. $result = null;
  250. $params = array(
  251. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  252. );
  253. try
  254. {
  255. $result = $this->auth->execSoapCall(
  256. "GetAccountRequest",
  257. $params
  258. );
  259. $attrs = array();
  260. foreach ($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'] as $a) {
  261. $attrs[$a['N']] = $a['DATA'];
  262. }
  263. $result = $attrs;
  264. }
  265. catch (SoapFault $exception)
  266. {
  267. $result = $exception;
  268. }
  269. return $result;
  270. }
  271. /**
  272. * createAccount
  273. * @param string $name account name
  274. * @param string $password password
  275. * @param array $attrs an optional array containing the account attributes to be set
  276. * @return string the new account's id
  277. */
  278. function createAccount($name, $password, $attrs = array())
  279. {
  280. $result = null;
  281. $params = array(
  282. new SoapParam($name, "name"),
  283. new SoapParam($password, "password"),
  284. );
  285. foreach ($attrs as $key=>$value)
  286. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  287. try
  288. {
  289. $result = $this->auth->execSoapCall(
  290. "CreateAccountRequest",
  291. $params
  292. );
  293. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['CREATEACCOUNTRESPONSE']['ACCOUNT']['ID'];
  294. usleep(250000); // introduce a small delay, otherwise some troubles may arise if we modify the new account right after its creation
  295. }
  296. catch (SoapFault $exception)
  297. {
  298. $result = $exception;
  299. }
  300. return $result;
  301. }
  302. /**
  303. * setAccountPassword
  304. * @param string $idOrNameAccount account id or account name
  305. * @param string $password password
  306. * @param string $type value of the account (auto, name, id)
  307. * @return array informations
  308. */
  309. function setAccountPassword($idOrNameAccount, $password, $type="auto")
  310. {
  311. if($type == "auto")
  312. $realType = getAccountType($idOrNameAccount);
  313. else
  314. $realType = $type;
  315. if($realType == "name")
  316. $accountId = $this->getAccountId($idOrNameAccount);
  317. else
  318. $accountId = $idOrNameAccount;
  319. $result = null;
  320. $params = array(
  321. new SoapParam($accountId, "id"),
  322. new SoapParam($password, "newPassword"),
  323. );
  324. try
  325. {
  326. $result = $this->auth->execSoapCall(
  327. "SetPasswordRequest",
  328. $params
  329. );
  330. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['SETPASSWORDRESPONSE'];
  331. }
  332. catch (SoapFault $exception)
  333. {
  334. $result = $exception;
  335. }
  336. return $result;
  337. }
  338. /**
  339. * modifyAccount
  340. * @param string $idOrNameAccount account id or account name
  341. * @param array $attrs an array containing the account attributes to be set
  342. * @param string $type value of the account (auto, name, id)
  343. * @return array informations
  344. */
  345. function modifyAccount($idOrNameAccount, $attrs = array(), $type="auto")
  346. {
  347. if($type == "auto")
  348. $realType = getAccountType($idOrNameAccount);
  349. else
  350. $realType = $type;
  351. if($realType == "name")
  352. $accountId = $this->getAccountId($idOrNameAccount);
  353. else
  354. $accountId = $idOrNameAccount;
  355. $result = null;
  356. $params = array(
  357. new SoapParam($accountId, "id"),
  358. );
  359. foreach ($attrs as $key=>$value)
  360. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  361. try
  362. {
  363. $result = $this->auth->execSoapCall(
  364. "ModifyAccountRequest",
  365. $params
  366. );
  367. }
  368. catch (SoapFault $exception)
  369. {
  370. $result = $exception;
  371. }
  372. return $result;
  373. }
  374. /**
  375. * renameAccount
  376. * @param string $idOrNameAccount account id or account name
  377. * @param string $newName new account name
  378. * @param string $type value of the account (auto, name, id)
  379. * @return array informations
  380. */
  381. function renameAccount($idOrNameAccount, $newName, $type="auto")
  382. {
  383. if($type == "auto")
  384. $realType = getAccountType($idOrNameAccount);
  385. else
  386. $realType = $type;
  387. if($realType == "name")
  388. $accountId = $this->getAccountId($idOrNameAccount);
  389. else
  390. $accountId = $idOrNameAccount;
  391. $result = null;
  392. $params = array(
  393. new SoapParam($accountId, "id"),
  394. new SoapParam($newName, "newName"),
  395. );
  396. try
  397. {
  398. $result = $this->auth->execSoapCall(
  399. "RenameAccountRequest",
  400. $params
  401. );
  402. }
  403. catch (SoapFault $exception)
  404. {
  405. $result = $exception;
  406. }
  407. return $result;
  408. }
  409. /**
  410. * deleteAccount
  411. * @param string $idOrNameAccount account id or account name
  412. * @param string $type value of the account (auto, name, id)
  413. * @return array informations
  414. */
  415. function deleteAccount($idOrNameAccount, $type="auto")
  416. {
  417. if($type == "auto")
  418. $realType = getAccountType($idOrNameAccount);
  419. else
  420. $realType = $type;
  421. if($realType == "name")
  422. $accountId = $this->getAccountId($idOrNameAccount);
  423. else
  424. $accountId = $idOrNameAccount;
  425. $result = null;
  426. $params = array(
  427. new SoapParam($accountId, "id"),
  428. );
  429. try
  430. {
  431. $result = $this->auth->execSoapCall(
  432. "DeleteAccountRequest",
  433. $params
  434. );
  435. }
  436. catch (SoapFault $exception)
  437. {
  438. $result = $exception;
  439. }
  440. return $result;
  441. }
  442. /**
  443. * getAccountAliases
  444. * @param string $idOrNameAccount account id or account name
  445. * @param string $type value of the account (auto, name, id)
  446. * @return array aliases
  447. */
  448. function getAccountAliases($idOrNameAccount, $type="auto")
  449. {
  450. return $this->getAccountOption($idOrNameAccount, "zimbraMailAlias", ATTR_MULTIVALUE, $type);
  451. }
  452. /**
  453. * addAccountAlias
  454. * @param string $idOrNameAccount account id or account name
  455. * @param string $alias account alias
  456. * @param string $type value of the account (auto, name, id)
  457. * @return array informations
  458. */
  459. function addAccountAlias($idOrNameAccount, $alias, $type="auto")
  460. {
  461. if($type == "auto")
  462. $realType = getAccountType($idOrNameAccount);
  463. else
  464. $realType = $type;
  465. if($realType == "name")
  466. $accountId = $this->getAccountId($idOrNameAccount);
  467. else
  468. $accountId = $idOrNameAccount;
  469. $result = null;
  470. $params = array(
  471. new SoapParam($accountId, "id"),
  472. new SoapParam($alias, "alias"),
  473. );
  474. try
  475. {
  476. $result = $this->auth->execSoapCall(
  477. "AddAccountAliasRequest",
  478. $params
  479. );
  480. }
  481. catch (SoapFault $exception)
  482. {
  483. $result = $exception;
  484. }
  485. return $result;
  486. }
  487. /**
  488. * removeAccountAlias
  489. * @param string $idOrNameAccount account id or account name
  490. * @param string $alias account alias
  491. * @param string $type value of the account (auto, name, id)
  492. * @return array informations
  493. */
  494. function removeAccountAlias($idOrNameAccount, $alias, $type="auto")
  495. {
  496. if($type == "auto")
  497. $realType = getAccountType($idOrNameAccount);
  498. else
  499. $realType = $type;
  500. if($realType == "name")
  501. $accountId = $this->getAccountId($idOrNameAccount);
  502. else
  503. $accountId = $idOrNameAccount;
  504. $result = null;
  505. $params = array(
  506. new SoapParam($accountId, "id"),
  507. new SoapParam($alias, "alias"),
  508. );
  509. try
  510. {
  511. $result = $this->auth->execSoapCall(
  512. "RemoveAccountAliasRequest",
  513. $params
  514. );
  515. }
  516. catch (SoapFault $exception)
  517. {
  518. $result = $exception;
  519. }
  520. return $result;
  521. }
  522. /**
  523. * getAccountStatus
  524. * @param string $idOrNameAccount account id or account name
  525. * @param string $type value of the account (auto, name, id)
  526. * @return string status
  527. */
  528. function getAccountStatus($idOrNameAccount, $type="auto")
  529. {
  530. return $this->getAccountOption($idOrNameAccount, "zimbraAccountStatus", ATTR_SINGLEVALUE, $type);
  531. }
  532. /**
  533. * setAccountStatus
  534. * @param string $idOrNameAccount account id or account name
  535. * @param string $status the status (active, maintenance, pending, locked, closed)
  536. * @param string $type value of the account (auto, name, id)
  537. * @return array informations
  538. */
  539. function setAccountStatus($idOrNameAccount, $status, $type = "auto")
  540. {
  541. $hideInGAL = ($status == "active") ? "FALSE" : "TRUE";
  542. $attrs = array(
  543. "zimbraAccountStatus"=>$status,
  544. "zimbraHideInGal"=>$hideInGAL,
  545. );
  546. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  547. return $result;
  548. }
  549. /**
  550. * expireAccountSessions
  551. * @param string $idOrNameAccount account id or account name
  552. * @param string $type value of the account (auto, name, id)
  553. * @return array informations
  554. */
  555. function expireAccountSessions($idOrNameAccount, $type = "auto")
  556. {
  557. $attrName = "zimbraAuthTokenValidityValue";
  558. $oldValue = $this->getAccountOption($idOrNameAccount, $attrName);
  559. $newValue = rand($oldValue+1, 1024);
  560. $attrs = array($attrName=>$newValue);
  561. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  562. return $result;
  563. }
  564. /**
  565. * getAccountCos
  566. * @param string $idOrNameAccount account id or account name
  567. * @param string $returnType get the COS ID or NAME
  568. * @param string $type value of the account (auto, name, id)
  569. * @return string COS id or name
  570. */
  571. function getAccountCos($idOrNameAccount, $returnType = "NAME", $type = "auto")
  572. {
  573. if($type == "auto")
  574. $realType = getAccountType($idOrNameAccount);
  575. else
  576. $realType = $type;
  577. if($realType == "name")
  578. $accountId = $this->getAccountId($idOrNameAccount);
  579. else
  580. $accountId = $idOrNameAccount;
  581. $result = null;
  582. $params = array(
  583. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  584. );
  585. try
  586. {
  587. $result = $this->auth->execSoapCall(
  588. "GetAccountInfoRequest",
  589. $params
  590. );
  591. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['COS'][$returnType];
  592. }
  593. catch (SoapFault $exception)
  594. {
  595. $result = $exception;
  596. }
  597. return $result;
  598. }
  599. /**
  600. * setAccountCos
  601. * @param string $idOrNameAccount account id or account name
  602. * @param string $cosName the COS name
  603. * @param string $type value of the account (auto, name, id)
  604. * @return array informations
  605. */
  606. function setAccountCos($idOrNameAccount, $cosName, $type = "auto")
  607. {
  608. $cosId = $this->getCosId($cosName);
  609. $attrs = array("zimbraCOSId"=>$cosId);
  610. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  611. return $result;
  612. }
  613. /**
  614. * getCosId
  615. * @param string $name the COS name
  616. * @return string COS id
  617. */
  618. function getCosId($name)
  619. {
  620. $result = null;
  621. $params = array(
  622. new SoapVar('<ns1:cos by="name">' . $name . '</ns1:cos>', XSD_ANYXML),
  623. );
  624. try
  625. {
  626. $result = $this->auth->execSoapCall(
  627. "GetCosRequest",
  628. $params
  629. );
  630. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETCOSRESPONSE']['COS']['ID'];
  631. }
  632. catch (SoapFault $exception)
  633. {
  634. $result = $exception;
  635. }
  636. return $result;
  637. }
  638. }
  639. ?>