PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

223 lines
6.0 KiB

8 years ago
8 years ago
7 years ago
  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('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. {
  46. $login = new Login('mailbox');
  47. if ($login->login($username, $password)) {
  48. session_regenerate_id();
  49. $_SESSION['authenticated'] = true;
  50. $_SESSION['sessid'] = array();
  51. $_SESSION['sessid']['username'] = $username;
  52. return true;
  53. }
  54. return false;
  55. }
  56. if (!isset($_SESSION['authenticated'])) {
  57. $server->addFunction('login', 'login');
  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. /**
  67. * @param string $old_password
  68. * @param string $new_password
  69. * @return boolean true on success
  70. */
  71. public function changePassword($old_password, $new_password)
  72. {
  73. $uh = new MailboxHandler();
  74. $username = $_SESSION['sessid']['username'] ?? '';
  75. if (empty($username)) {
  76. throw new \Exception("not logged in? invalid session");
  77. }
  78. if (!$uh->init($username)) {
  79. return false; // user doesn't exist.
  80. }
  81. $login = new Login('mailbox');
  82. try {
  83. return $login->changePassword($username, $new_password, $old_password);
  84. } catch (\Exception $e) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * @param string $username
  90. * @param string $password
  91. * @return boolean true if successful.
  92. */
  93. public function login($username, $password)
  94. {
  95. $login = new Login('mailbox');
  96. return $login->login($username, $password);
  97. }
  98. }
  99. class VacationProxy
  100. {
  101. /**
  102. * @return boolean true if the vacation is removed successfully. Else false.
  103. */
  104. public function remove()
  105. {
  106. $vh = new VacationHandler($_SESSION['sessid']['username']);
  107. return $vh->remove();
  108. }
  109. /**
  110. * @return boolean true if vacation stuff is enabled in this instance of postfixadmin
  111. * and the user has the ability to make changes to it.
  112. */
  113. public function isVacationSupported()
  114. {
  115. $vh = new VacationHandler($_SESSION['sessid']['username']);
  116. return $vh->vacation_supported();
  117. }
  118. /**
  119. * @return boolean true if the user has an active vacation record etc.
  120. */
  121. public function checkVacation()
  122. {
  123. $vh = new VacationHandler($_SESSION['sessid']['username']);
  124. return $vh->check_vacation();
  125. }
  126. /**
  127. * @return array|bool - either array of vacation details or boolean false if the user has none.
  128. */
  129. public function getDetails()
  130. {
  131. $vh = new VacationHandler($_SESSION['sessid']['username']);
  132. return $vh->get_details();
  133. }
  134. /**
  135. * @param string $subject
  136. * @param string $body
  137. * @param int $interval_time
  138. * @param string $activeFrom
  139. * @param string $activeUntil
  140. * @return boolean true on success.
  141. * Whatiis @replyType?? for
  142. */
  143. public function setAway($subject, $body, $interval_time = 0, $activeFrom = '2000-01-01', $activeUntil = '2099-12-31')
  144. {
  145. $vh = new VacationHandler($_SESSION['sessid']['username']);
  146. return $vh->set_away($subject, $body, $interval_time, $activeFrom, $activeUntil);
  147. }
  148. }
  149. class AliasProxy
  150. {
  151. /**
  152. * @return array - array of aliases this user has. Array may be empty.
  153. */
  154. public function get()
  155. {
  156. $ah = new AliasHandler();
  157. $ah->init($_SESSION['sessid']['username']);
  158. /* I see no point in returning special addresses to the user. */
  159. $ah->view();
  160. $result = $ah->result;
  161. return $result['goto'];
  162. }
  163. /**
  164. * @param array of email addresses (Strings)
  165. * @param string flag to set ('forward_and_store' or 'remote_only')
  166. * @return boolean true
  167. */
  168. public function update($addresses, $flags)
  169. {
  170. $ah = new AliasHandler();
  171. $ah->init($_SESSION['sessid']['username']);
  172. $values = ['goto' => $addresses];
  173. if ($flags == 'forward_and_store') {
  174. $values['goto_mailbox'] = 1;
  175. } elseif ($flags == 'remote_only') {
  176. $values['goto_mailbox'] = 0;
  177. } else {
  178. return false; # invalid parameter
  179. }
  180. if (!$ah->set($values)) {
  181. //error_log('ah->set failed' . print_r($values, true));
  182. return false;
  183. }
  184. $store = $ah->save();
  185. return $store;
  186. }
  187. /**
  188. * @return boolean true if the user has 'store_and_forward' set.
  189. * (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only'
  190. */
  191. public function hasStoreAndForward()
  192. {
  193. $ah = new AliasHandler();
  194. $ah->init($_SESSION['sessid']['username']);
  195. $ah->view();
  196. $result = $ah->result;
  197. return $result['goto_mailbox'] == 1;
  198. }
  199. }
  200. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */