Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

xmlrpc.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Requires the Zend framework is installed and in the include path.
  4. *
  5. * Usage example:
  6. * require_once('Zend/XmlRpc/Client.php');
  7. * $xmlrpc = new Zend_XmlRpc_Client('https://server/xmlrpc.php');
  8. *
  9. * $http_client = $xmlrpc->getHttpClient();
  10. * $http_client->setCookieJar();
  11. *
  12. * $login_object = $xmlrpc->getProxy('login');
  13. * $success = $login_object->login($email_address, $password);
  14. *
  15. * if($success) {
  16. * echo "We're logged in";
  17. * }
  18. * else {
  19. * die("Auth failed");
  20. * }
  21. * $user = $xmlrpc->getProxy('user');
  22. * $alias = $xmlrpc->getProxy('alias');
  23. * $vacation = $xmlrpc->getProxy('vacation');
  24. *
  25. * if($vacation->checkVacation()) {
  26. * echo "Vacation turned on for user";
  27. * }
  28. *
  29. * Note, the requirement that your XmlRpc client provides cookies with each request.
  30. * If it does not do this, then your authentication details will not persist across requests, and
  31. * this XMLRPC interface will not work.
  32. */
  33. require_once(dirname(__FILE__) . '/common.php');
  34. if($CONF['xmlrpc_enabled'] == false) {
  35. die("xmlrpc support disabled");
  36. }
  37. require_once('Zend/XmlRpc/Server.php');
  38. $server = new Zend_XmlRpc_Server();
  39. /**
  40. * @param string $username
  41. * @param string $password
  42. * @return boolean true on success, else false.
  43. */
  44. function login($username, $password) {
  45. $h = new MailboxHandler();
  46. if($h->login($username, $password)) {
  47. session_regenerate_id();
  48. $_SESSION['authenticated'] = true;
  49. $_SESSION['sessid'] = array();
  50. $_SESSION['sessid']['username'] = $username;
  51. return true;
  52. }
  53. return false;
  54. }
  55. if(!isset($_SESSION['authenticated'])) {
  56. $server->addFunction('login', 'login');
  57. }
  58. else {
  59. $server->setClass('UserProxy', 'user');
  60. $server->setClass('VacationProxy', 'vacation');
  61. $server->setClass('AliasProxy', 'alias');
  62. }
  63. echo $server->handle();
  64. class UserProxy {
  65. /**
  66. * @param string $old_password
  67. * @param string $new_password
  68. * @return boolean true on success
  69. */
  70. public function changePassword($old_password, $new_password) {
  71. $uh = new MailboxHandler();
  72. if (!$uh->init($_SESSION['sessid']['username'])) return false;
  73. return $uh->change_pw($new_password, $old_password);
  74. }
  75. /**
  76. * @param string $username
  77. * @param string $password
  78. * @return boolean true if successful.
  79. */
  80. public function login($username, $password) {
  81. $uh = new MailboxHandler(); # $_SESSION['sessid']['username']);
  82. return $uh->login($username, $password);
  83. }
  84. }
  85. class VacationProxy {
  86. /**
  87. * @return boolean true if the vacation is removed successfully. Else false.
  88. */
  89. public function remove() {
  90. $vh = new VacationHandler($_SESSION['sessid']['username']);
  91. return $vh->remove();
  92. }
  93. /**
  94. * @return boolean true if vacation stuff is enabled in this instance of postfixadmin
  95. * and the user has the ability to make changes to it.
  96. */
  97. public function isVacationSupported() {
  98. $vh = new VacationHandler($_SESSION['sessid']['username']);
  99. return $vh->vacation_supported();
  100. }
  101. /**
  102. * @return boolean true if the user has an active vacation record etc.
  103. */
  104. public function checkVacation() {
  105. $vh = new VacationHandler($_SESSION['sessid']['username']);
  106. return $vh->check_vacation();
  107. }
  108. /**
  109. * @return struct|boolean - either array of vacation details or boolean false if the user has none.
  110. */
  111. public function getDetails() {
  112. $vh = new VacationHandler($_SESSION['sessid']['username']);
  113. return $vh->get_details();
  114. }
  115. /**
  116. * @param string $subject
  117. * @param string $body
  118. * @param string $interval_time
  119. * @param string $activeFrom
  120. * @param string $activeUntil
  121. * @return boolean true on success.
  122. * Whatiis @replyType?? for
  123. */
  124. public function setAway($subject, $body, $interval_time = 0, $activeFrom = '2000-01-01', $activeUntil = '2099-12-31') {
  125. $vh = new VacationHandler($_SESSION['sessid']['username']);
  126. return $vh->set_away($subject, $body, $interval_time, $activeFrom, $activeUntil);
  127. }
  128. }
  129. class AliasProxy {
  130. /**
  131. * @return array - array of aliases this user has. Array may be empty.
  132. */
  133. public function get() {
  134. $ah = new AliasHandler();
  135. $ah->init($_SESSION['sessid']['username']);
  136. /* I see no point in returning special addresses to the user. */
  137. $ah->view();
  138. $result = $ah->result;
  139. return $result['goto'];
  140. }
  141. /**
  142. * @param array of email addresses (Strings)
  143. * @param string flag to set ('forward_and_store' or 'remote_only')
  144. * @return boolean true
  145. */
  146. public function update($addresses, $flags) {
  147. $ah = new AliasHandler();
  148. $ah->init($_SESSION['sessid']['username']);
  149. $values['goto'] = $addresses;
  150. if ($flags == 'forward_and_store') {
  151. $values['goto_mailbox'] = 1;
  152. } elseif ($flags == 'remote_only') {
  153. $values['goto_mailbox'] = 0;
  154. } else {
  155. return false; # invalid parameter
  156. }
  157. if (!$ah->set($values)) {
  158. //error_log('ah->set failed' . print_r($values, true));
  159. return false;
  160. }
  161. $store = $ah->store();
  162. return $store;
  163. }
  164. /**
  165. * @return boolean true if the user has 'store_and_forward' set.
  166. * (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only'
  167. */
  168. public function hasStoreAndForward() {
  169. $ah = new AliasHandler();
  170. $ah->init($_SESSION['sessid']['username']);
  171. $ah->view();
  172. $result = $ah->result;
  173. return $result['goto_mailbox'] == 1;
  174. }
  175. }
  176. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */