You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlrpc.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. } else {
  58. $server->setClass('UserProxy', 'user');
  59. $server->setClass('VacationProxy', 'vacation');
  60. $server->setClass('AliasProxy', 'alias');
  61. }
  62. echo $server->handle();
  63. class UserProxy {
  64. /**
  65. * @param string $old_password
  66. * @param string $new_password
  67. * @return boolean true on success
  68. */
  69. public function changePassword($old_password, $new_password) {
  70. $uh = new MailboxHandler();
  71. if (!$uh->init($_SESSION['sessid']['username'])) {
  72. return false;
  73. }
  74. return $uh->change_pw($new_password, $old_password);
  75. }
  76. /**
  77. * @param string $username
  78. * @param string $password
  79. * @return boolean true if successful.
  80. */
  81. public function login($username, $password) {
  82. $uh = new MailboxHandler(); # $_SESSION['sessid']['username']);
  83. return $uh->login($username, $password);
  84. }
  85. }
  86. class VacationProxy {
  87. /**
  88. * @return boolean true if the vacation is removed successfully. Else false.
  89. */
  90. public function remove() {
  91. $vh = new VacationHandler($_SESSION['sessid']['username']);
  92. return $vh->remove();
  93. }
  94. /**
  95. * @return boolean true if vacation stuff is enabled in this instance of postfixadmin
  96. * and the user has the ability to make changes to it.
  97. */
  98. public function isVacationSupported() {
  99. $vh = new VacationHandler($_SESSION['sessid']['username']);
  100. return $vh->vacation_supported();
  101. }
  102. /**
  103. * @return boolean true if the user has an active vacation record etc.
  104. */
  105. public function checkVacation() {
  106. $vh = new VacationHandler($_SESSION['sessid']['username']);
  107. return $vh->check_vacation();
  108. }
  109. /**
  110. * @return struct|boolean - either array of vacation details or boolean false if the user has none.
  111. */
  112. public function getDetails() {
  113. $vh = new VacationHandler($_SESSION['sessid']['username']);
  114. return $vh->get_details();
  115. }
  116. /**
  117. * @param string $subject
  118. * @param string $body
  119. * @param string $interval_time
  120. * @param string $activeFrom
  121. * @param string $activeUntil
  122. * @return boolean true on success.
  123. * Whatiis @replyType?? for
  124. */
  125. public function setAway($subject, $body, $interval_time = 0, $activeFrom = '2000-01-01', $activeUntil = '2099-12-31') {
  126. $vh = new VacationHandler($_SESSION['sessid']['username']);
  127. return $vh->set_away($subject, $body, $interval_time, $activeFrom, $activeUntil);
  128. }
  129. }
  130. class AliasProxy {
  131. /**
  132. * @return array - array of aliases this user has. Array may be empty.
  133. */
  134. public function get() {
  135. $ah = new AliasHandler();
  136. $ah->init($_SESSION['sessid']['username']);
  137. /* I see no point in returning special addresses to the user. */
  138. $ah->view();
  139. $result = $ah->result;
  140. return $result['goto'];
  141. }
  142. /**
  143. * @param array of email addresses (Strings)
  144. * @param string flag to set ('forward_and_store' or 'remote_only')
  145. * @return boolean true
  146. */
  147. public function update($addresses, $flags) {
  148. $ah = new AliasHandler();
  149. $ah->init($_SESSION['sessid']['username']);
  150. $values['goto'] = $addresses;
  151. if ($flags == 'forward_and_store') {
  152. $values['goto_mailbox'] = 1;
  153. } elseif ($flags == 'remote_only') {
  154. $values['goto_mailbox'] = 0;
  155. } else {
  156. return false; # invalid parameter
  157. }
  158. if (!$ah->set($values)) {
  159. //error_log('ah->set failed' . print_r($values, true));
  160. return false;
  161. }
  162. $store = $ah->store();
  163. return $store;
  164. }
  165. /**
  166. * @return boolean true if the user has 'store_and_forward' set.
  167. * (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only'
  168. */
  169. public function hasStoreAndForward() {
  170. $ah = new AliasHandler();
  171. $ah->init($_SESSION['sessid']['username']);
  172. $ah->view();
  173. $result = $ah->result;
  174. return $result['goto_mailbox'] == 1;
  175. }
  176. }
  177. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */