Account.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 informations
  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']['GETACCOUNTRESPONSE']['ACCOUNT'];
  136. }
  137. catch (SoapFault $exception)
  138. {
  139. $result = $exception;
  140. }
  141. return $result;
  142. }
  143. /**
  144. * getQuota
  145. * @param string $name account name
  146. * @return string account informations
  147. */
  148. function getQuota($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. "GetAccountRequest",
  158. $params
  159. );
  160. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'], "zimbraMailQuota");
  161. }
  162. catch (SoapFault $exception)
  163. {
  164. $result = $exception;
  165. }
  166. return $result;
  167. }
  168. /**
  169. * getAccountId
  170. * @param string $name account name
  171. * @return string account id
  172. */
  173. function getAccountId($name)
  174. {
  175. $result = null;
  176. $params = array(
  177. new SoapVar('<ns1:account by="name">' . $name . '</ns1:account>', XSD_ANYXML),
  178. );
  179. try
  180. {
  181. $result = $this->auth->execSoapCall(
  182. "GetAccountInfoRequest",
  183. $params
  184. );
  185. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['A'], "zimbraId");
  186. }
  187. catch (SoapFault $exception)
  188. {
  189. $result = $exception;
  190. }
  191. return $result;
  192. }
  193. /**
  194. * accountExists
  195. * @param string $idOrNameAccount account id or account name
  196. * @param string $type value of the account (auto, name, id)
  197. * @return bool exists
  198. */
  199. function accountExists($idOrNameAccount, $type="auto")
  200. {
  201. if($type == "auto")
  202. $realType = getAccountType($idOrNameAccount);
  203. else
  204. $realType = $type;
  205. $result = null;
  206. $params = array(
  207. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  208. );
  209. $options = array(
  210. 'retry' => false,
  211. );
  212. try
  213. {
  214. $result = $this->auth->execSoapCall(
  215. "GetAccountInfoRequest",
  216. $params,
  217. $options
  218. );
  219. }
  220. catch (SoapFault $exception)
  221. {
  222. $result = $exception;
  223. }
  224. return (!is_a($result, "Exception"));
  225. }
  226. /**
  227. * getAccountInfo
  228. * @param string $idOrNameAccount account id or account name
  229. * @param string $type value of the account (auto, name, id)
  230. * @return array informations
  231. */
  232. function getAccountInfo($idOrNameAccount, $type="auto")
  233. {
  234. if($type == "auto")
  235. $realType = getAccountType($idOrNameAccount);
  236. else
  237. $realType = $type;
  238. $result = null;
  239. $params = array(
  240. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  241. );
  242. try
  243. {
  244. $result = $this->auth->execSoapCall(
  245. "GetAccountInfoRequest",
  246. $params
  247. );
  248. }
  249. catch (SoapFault $exception)
  250. {
  251. $result = $exception;
  252. }
  253. return $result;
  254. }
  255. /**
  256. * getMailbox
  257. * @param string $idOrNameAccount account id or account name
  258. * @return array informations
  259. */
  260. function getMailbox($idOrNameAccount, $type="auto")
  261. {
  262. if($type == "auto")
  263. $realType = getAccountType($idOrNameAccount);
  264. else
  265. $realType = $type;
  266. if($realTyp = 'name')
  267. $id = $this->getAccountId($idOrNameAccount);
  268. $result = null;
  269. $params = array(
  270. new SoapVar('<ns1:mbox id="' . $id . '" />', XSD_ANYXML),
  271. );
  272. try
  273. {
  274. $result = $this->auth->execSoapCall(
  275. "GetMailboxRequest",
  276. $params
  277. );
  278. $result = $result['SOAP:ENVELOPE']['SOAP:BODY'];
  279. }
  280. catch (SoapFault $exception)
  281. {
  282. $result = $exception;
  283. }
  284. return $result;
  285. }
  286. /**
  287. * getAccountOption
  288. * @param string $idOrNameAccount account id or account name
  289. * @param string $optName name of the option to get
  290. * @param int $multisingle (ATTR_SINGLEVALUE, ATTR_MULTIVALUE)
  291. * @param string $type value of the account (auto, name, id)
  292. * @return string option
  293. */
  294. function getAccountOption($idOrNameAccount, $optName, $multisingle=ATTR_SINGLEVALUE, $type="auto")
  295. {
  296. if($type == "auto")
  297. $realType = getAccountType($idOrNameAccount);
  298. else
  299. $realType = $type;
  300. $result = null;
  301. $params = array(
  302. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  303. );
  304. try
  305. {
  306. $result = $this->auth->execSoapCall(
  307. "GetAccountRequest",
  308. $params
  309. );
  310. $result = getSoapAttribute($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'], $optName, $multisingle);
  311. }
  312. catch (SoapFault $exception)
  313. {
  314. $result = $exception;
  315. }
  316. return $result;
  317. }
  318. /**
  319. * getAccountOptions
  320. * @param string $idOrNameAccount account id or account name
  321. * @param string $type value of the account (auto, name, id)
  322. * @return array options
  323. */
  324. function getAccountOptions($idOrNameAccount, $type="auto")
  325. {
  326. if($type == "auto")
  327. $realType = getAccountType($idOrNameAccount);
  328. else
  329. $realType = $type;
  330. $result = null;
  331. $params = array(
  332. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  333. );
  334. try
  335. {
  336. $result = $this->auth->execSoapCall(
  337. "GetAccountRequest",
  338. $params
  339. );
  340. $attrs = array();
  341. foreach ($result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTRESPONSE']['ACCOUNT']['A'] as $a) {
  342. $attrs[$a['N']] = $a['DATA'];
  343. }
  344. $result = $attrs;
  345. }
  346. catch (SoapFault $exception)
  347. {
  348. $result = $exception;
  349. }
  350. return $result;
  351. }
  352. /**
  353. * createAccount
  354. * @param string $name account name
  355. * @param string $password password
  356. * @param array $attrs an optional array containing the account attributes to be set
  357. * @return string the new account's id
  358. */
  359. function createAccount($name, $password, $attrs = array())
  360. {
  361. $result = null;
  362. $params = array(
  363. new SoapParam($name, "name"),
  364. new SoapParam($password, "password"),
  365. );
  366. foreach ($attrs as $key=>$value)
  367. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  368. try
  369. {
  370. $result = $this->auth->execSoapCall(
  371. "CreateAccountRequest",
  372. $params
  373. );
  374. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['CREATEACCOUNTRESPONSE']['ACCOUNT']['ID'];
  375. usleep(250000); // introduce a small delay, otherwise some troubles may arise if we modify the new account right after its creation
  376. }
  377. catch (SoapFault $exception)
  378. {
  379. $result = $exception;
  380. }
  381. return $result;
  382. }
  383. /**
  384. * setAccountPassword
  385. * @param string $idOrNameAccount account id or account name
  386. * @param string $password password
  387. * @param string $type value of the account (auto, name, id)
  388. * @return array informations
  389. */
  390. function setAccountPassword($idOrNameAccount, $password, $type="auto")
  391. {
  392. if($type == "auto")
  393. $realType = getAccountType($idOrNameAccount);
  394. else
  395. $realType = $type;
  396. if($realType == "name")
  397. $accountId = $this->getAccountId($idOrNameAccount);
  398. else
  399. $accountId = $idOrNameAccount;
  400. $result = null;
  401. $params = array(
  402. new SoapParam($accountId, "id"),
  403. new SoapParam($password, "newPassword"),
  404. );
  405. try
  406. {
  407. $result = $this->auth->execSoapCall(
  408. "SetPasswordRequest",
  409. $params
  410. );
  411. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['SETPASSWORDRESPONSE'];
  412. }
  413. catch (SoapFault $exception)
  414. {
  415. $result = $exception;
  416. }
  417. return $result;
  418. }
  419. /**
  420. * modifyAccount
  421. * @param string $idOrNameAccount account id or account name
  422. * @param array $attrs an array containing the account attributes to be set
  423. * @param string $type value of the account (auto, name, id)
  424. * @return array informations
  425. */
  426. function modifyAccount($idOrNameAccount, $attrs = array(), $type="auto")
  427. {
  428. if($type == "auto")
  429. $realType = getAccountType($idOrNameAccount);
  430. else
  431. $realType = $type;
  432. if($realType == "name")
  433. $accountId = $this->getAccountId($idOrNameAccount);
  434. else
  435. $accountId = $idOrNameAccount;
  436. $result = null;
  437. $params = array(
  438. new SoapParam($accountId, "id"),
  439. );
  440. foreach ($attrs as $key=>$value)
  441. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  442. try
  443. {
  444. $result = $this->auth->execSoapCall(
  445. "ModifyAccountRequest",
  446. $params
  447. );
  448. }
  449. catch (SoapFault $exception)
  450. {
  451. $result = $exception;
  452. }
  453. return $result;
  454. }
  455. /**
  456. * renameAccount
  457. * @param string $idOrNameAccount account id or account name
  458. * @param string $newName new account name
  459. * @param string $type value of the account (auto, name, id)
  460. * @return array informations
  461. */
  462. function renameAccount($idOrNameAccount, $newName, $type="auto")
  463. {
  464. if($type == "auto")
  465. $realType = getAccountType($idOrNameAccount);
  466. else
  467. $realType = $type;
  468. if($realType == "name")
  469. $accountId = $this->getAccountId($idOrNameAccount);
  470. else
  471. $accountId = $idOrNameAccount;
  472. $result = null;
  473. $params = array(
  474. new SoapParam($accountId, "id"),
  475. new SoapParam($newName, "newName"),
  476. );
  477. try
  478. {
  479. $result = $this->auth->execSoapCall(
  480. "RenameAccountRequest",
  481. $params
  482. );
  483. }
  484. catch (SoapFault $exception)
  485. {
  486. $result = $exception;
  487. }
  488. return $result;
  489. }
  490. /**
  491. * deleteAccount
  492. * @param string $idOrNameAccount account id or account name
  493. * @param string $type value of the account (auto, name, id)
  494. * @return array informations
  495. */
  496. function deleteAccount($idOrNameAccount, $type="auto")
  497. {
  498. if($type == "auto")
  499. $realType = getAccountType($idOrNameAccount);
  500. else
  501. $realType = $type;
  502. if($realType == "name")
  503. $accountId = $this->getAccountId($idOrNameAccount);
  504. else
  505. $accountId = $idOrNameAccount;
  506. $result = null;
  507. $params = array(
  508. new SoapParam($accountId, "id"),
  509. );
  510. try
  511. {
  512. $result = $this->auth->execSoapCall(
  513. "DeleteAccountRequest",
  514. $params
  515. );
  516. }
  517. catch (SoapFault $exception)
  518. {
  519. $result = $exception;
  520. }
  521. return $result;
  522. }
  523. /**
  524. * getAccountAliases
  525. * @param string $idOrNameAccount account id or account name
  526. * @param string $type value of the account (auto, name, id)
  527. * @return array aliases
  528. */
  529. function getAccountAliases($idOrNameAccount, $type="auto")
  530. {
  531. return $this->getAccountOption($idOrNameAccount, "zimbraMailAlias", ATTR_MULTIVALUE, $type);
  532. }
  533. /**
  534. * addAccountAlias
  535. * @param string $idOrNameAccount account id or account name
  536. * @param string $alias account alias
  537. * @param string $type value of the account (auto, name, id)
  538. * @return array informations
  539. */
  540. function addAccountAlias($idOrNameAccount, $alias, $type="auto")
  541. {
  542. if($type == "auto")
  543. $realType = getAccountType($idOrNameAccount);
  544. else
  545. $realType = $type;
  546. if($realType == "name")
  547. $accountId = $this->getAccountId($idOrNameAccount);
  548. else
  549. $accountId = $idOrNameAccount;
  550. $result = null;
  551. $params = array(
  552. new SoapParam($accountId, "id"),
  553. new SoapParam($alias, "alias"),
  554. );
  555. try
  556. {
  557. $result = $this->auth->execSoapCall(
  558. "AddAccountAliasRequest",
  559. $params
  560. );
  561. }
  562. catch (SoapFault $exception)
  563. {
  564. $result = $exception;
  565. }
  566. return $result;
  567. }
  568. /**
  569. * removeAccountAlias
  570. * @param string $idOrNameAccount account id or account name
  571. * @param string $alias account alias
  572. * @param string $type value of the account (auto, name, id)
  573. * @return array informations
  574. */
  575. function removeAccountAlias($idOrNameAccount, $alias, $type="auto")
  576. {
  577. if($type == "auto")
  578. $realType = getAccountType($idOrNameAccount);
  579. else
  580. $realType = $type;
  581. if($realType == "name")
  582. $accountId = $this->getAccountId($idOrNameAccount);
  583. else
  584. $accountId = $idOrNameAccount;
  585. $result = null;
  586. $params = array(
  587. new SoapParam($accountId, "id"),
  588. new SoapParam($alias, "alias"),
  589. );
  590. try
  591. {
  592. $result = $this->auth->execSoapCall(
  593. "RemoveAccountAliasRequest",
  594. $params
  595. );
  596. }
  597. catch (SoapFault $exception)
  598. {
  599. $result = $exception;
  600. }
  601. return $result;
  602. }
  603. /**
  604. * getAccountStatus
  605. * @param string $idOrNameAccount account id or account name
  606. * @param string $type value of the account (auto, name, id)
  607. * @return string status
  608. */
  609. function getAccountStatus($idOrNameAccount, $type="auto")
  610. {
  611. return $this->getAccountOption($idOrNameAccount, "zimbraAccountStatus", ATTR_SINGLEVALUE, $type);
  612. }
  613. /**
  614. * setAccountStatus
  615. * @param string $idOrNameAccount account id or account name
  616. * @param string $status the status (active, maintenance, pending, locked, closed)
  617. * @param string $type value of the account (auto, name, id)
  618. * @return array informations
  619. */
  620. function setAccountStatus($idOrNameAccount, $status, $type = "auto")
  621. {
  622. $hideInGAL = ($status == "active") ? "FALSE" : "TRUE";
  623. $attrs = array(
  624. "zimbraAccountStatus"=>$status,
  625. "zimbraHideInGal"=>$hideInGAL,
  626. );
  627. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  628. return $result;
  629. }
  630. /**
  631. * expireAccountSessions
  632. * @param string $idOrNameAccount account id or account name
  633. * @param string $type value of the account (auto, name, id)
  634. * @return array informations
  635. */
  636. function expireAccountSessions($idOrNameAccount, $type = "auto")
  637. {
  638. $attrName = "zimbraAuthTokenValidityValue";
  639. $oldValue = $this->getAccountOption($idOrNameAccount, $attrName);
  640. $newValue = rand($oldValue+1, 1024);
  641. $attrs = array($attrName=>$newValue);
  642. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  643. return $result;
  644. }
  645. /**
  646. * getAccountToken
  647. * @param string $idOrNameAccount account id or account name
  648. * @param string $preauthKey preauth key
  649. * @param string $expire lifetime of token
  650. * @return string authToken
  651. */
  652. function getAccountToken($idOrNameAccount, $preauthKey, $expire, $type="auto")
  653. {
  654. if($type == "auto")
  655. $realType = getAccountType($idOrNameAccount);
  656. else
  657. $realType = $type;
  658. if($realType == "name")
  659. $accountId = $this->getAccountId($idOrNameAccount);
  660. else
  661. $accountId = $idOrNameAccount;
  662. $result = null;
  663. $params = array(
  664. new SoapParam($accountId, "id"),
  665. new SoapParam($preauthKey, "preauth"),
  666. );
  667. try
  668. {
  669. $result = $this->auth->execSoapCall(
  670. "AuthRequest",
  671. $params
  672. );
  673. // $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['AUTHTOKEN'];
  674. }
  675. catch (SoapFault $exception)
  676. {
  677. $result = $exception;
  678. }
  679. return $result;
  680. }
  681. /**
  682. * getAccountCos
  683. * @param string $idOrNameAccount account id or account name
  684. * @param string $returnType get the COS ID or NAME
  685. * @param string $type value of the account (auto, name, id)
  686. * @return string COS id or name
  687. */
  688. function getAccountCos($idOrNameAccount, $returnType = "NAME", $type = "auto")
  689. {
  690. if($type == "auto")
  691. $realType = getAccountType($idOrNameAccount);
  692. else
  693. $realType = $type;
  694. if($realType == "name")
  695. $accountId = $this->getAccountId($idOrNameAccount);
  696. else
  697. $accountId = $idOrNameAccount;
  698. $result = null;
  699. $params = array(
  700. new SoapVar('<ns1:account by="' . $realType . '">' . $idOrNameAccount . '</ns1:account>', XSD_ANYXML),
  701. );
  702. try
  703. {
  704. $result = $this->auth->execSoapCall(
  705. "GetAccountInfoRequest",
  706. $params
  707. );
  708. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETACCOUNTINFORESPONSE']['COS'][$returnType];
  709. }
  710. catch (SoapFault $exception)
  711. {
  712. $result = $exception;
  713. }
  714. return $result;
  715. }
  716. /**
  717. * setAccountCos
  718. * @param string $idOrNameAccount account id or account name
  719. * @param string $cosName the COS name
  720. * @param string $type value of the account (auto, name, id)
  721. * @return array informations
  722. */
  723. function setAccountCos($idOrNameAccount, $cosName, $type = "auto")
  724. {
  725. $cosId = $this->getCosId($cosName);
  726. $attrs = array("zimbraCOSId"=>$cosId);
  727. $result = $this->modifyAccount($idOrNameAccount, $attrs, $type);
  728. return $result;
  729. }
  730. /**
  731. * getCosId
  732. * @param string $name the COS name
  733. * @return string COS id
  734. */
  735. function getCosId($name)
  736. {
  737. $result = null;
  738. $params = array(
  739. new SoapVar('<ns1:cos by="name">' . $name . '</ns1:cos>', XSD_ANYXML),
  740. );
  741. try
  742. {
  743. $result = $this->auth->execSoapCall(
  744. "GetCosRequest",
  745. $params
  746. );
  747. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETCOSRESPONSE']['COS']['ID'];
  748. }
  749. catch (SoapFault $exception)
  750. {
  751. $result = $exception;
  752. }
  753. return $result;
  754. }
  755. /**
  756. * getAllCos
  757. * @return array informations
  758. */
  759. function getAllCos()
  760. {
  761. $result = null;
  762. try
  763. {
  764. $result = $this->auth->execSoapCall(
  765. "GetAllCosRequest"
  766. );
  767. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETALLCOSRESPONSE'];
  768. }
  769. catch (SoapFault $exception)
  770. {
  771. $result = $exception;
  772. }
  773. return $result;
  774. }
  775. }
  776. ?>