PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tine20/ActiveSync/Server/Http.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 228 lines | 122 code | 32 blank | 74 comment | 13 complexity | 020e79d1162d3c7ec15f991096a8a904 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package ActiveSync
  6. * @subpackage Server
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @copyright Copyright (c) 2008-2012 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Lars Kneschke <l.kneschke@metaways.de>
  10. */
  11. /**
  12. * http server
  13. *
  14. * @package ActiveSync
  15. * @subpackage Server
  16. */
  17. class ActiveSync_Server_Http extends Tinebase_Server_Abstract implements Tinebase_Server_Interface
  18. {
  19. /**
  20. * used into login process
  21. *
  22. * @var string
  23. */
  24. const REQUEST_TYPE = 'TineActiveSync';
  25. /**
  26. * the request
  27. *
  28. * @var Zend_Controller_Request_Http
  29. */
  30. protected $_request = NULL;
  31. /**
  32. * request body
  33. *
  34. * @var resource
  35. */
  36. protected $_body;
  37. /**
  38. * handler for ActiveSync requests
  39. *
  40. * @param Zend_Controller_Request_Http $request
  41. * @param resource $body used mostly for unittesting
  42. * @return boolean
  43. *
  44. * @todo 0007504: research input stream problems / remove the hotfix afterwards
  45. */
  46. public function handle(\Zend\Http\Request $request = null, $body = null)
  47. {
  48. $this->_request = $request instanceof \Zend\Http\Request ? $request : Tinebase_Core::get(Tinebase_Core::REQUEST);
  49. $this->_body = $this->_getBody($body);
  50. try {
  51. list($loginName, $password) = $this->_getAuthData($this->_request);
  52. } catch (Tinebase_Exception_NotFound $tenf) {
  53. header('WWW-Authenticate: Basic realm="ActiveSync for Tine 2.0"');
  54. header('HTTP/1.1 401 Unauthorized');
  55. Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " unauthorized access attempt.");
  56. return;
  57. }
  58. Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ .' is ActiveSync request.');
  59. Tinebase_Core::initFramework();
  60. try {
  61. $authResult = $this->_authenticate(
  62. $loginName,
  63. $password,
  64. $this->_request
  65. );
  66. } catch (Exception $e) {
  67. Tinebase_Exception::log($e);
  68. $authResult = false;
  69. }
  70. if ($authResult !== true) {
  71. header('WWW-Authenticate: Basic realm="ActiveSync for Tine 2.0"');
  72. header('HTTP/1.1 401 Unauthorized');
  73. return;
  74. }
  75. if (!$this->_checkUserPermissions($loginName)) {
  76. return;
  77. }
  78. $this->_initializeRegistry();
  79. $request = new Zend_Controller_Request_Http();
  80. $request->setRequestUri($this->_request->getRequestUri());
  81. $syncFrontend = new Syncroton_Server(Tinebase_Core::getUser()->accountId, $request, $this->_body);
  82. $syncFrontend->handle();
  83. Tinebase_Controller::getInstance()->logout($request->getClientIp());
  84. }
  85. /**
  86. * returns request method
  87. *
  88. * @return string|NULL
  89. */
  90. public function getRequestMethod()
  91. {
  92. return ($this->_request) ? $this->_request->getMethod() : NULL;
  93. }
  94. /**
  95. * get body
  96. *
  97. * @param resource $body used mostly for unittesting
  98. * @return resource
  99. *
  100. * @todo 0007504: research input stream problems / remove the hotfix afterwards
  101. */
  102. protected function _getBody($body)
  103. {
  104. if ($body === null) {
  105. // FIXME: this is a hotfix for 0007454: no email reply or forward (iOS/android 4.1.1)
  106. // the wbxml decoder seems to run into problems when we just pass the input stream
  107. // when the stream is copied first, the problems disappear
  108. //$this->_body = $body !== null ? $body : fopen('php://input', 'r');
  109. $tempStream = fopen("php://temp", 'r+');
  110. stream_copy_to_stream(fopen('php://input', 'r'), $tempStream);
  111. rewind($tempStream);
  112. // file_put_contents(tempnam('/var/tmp', 'wbxml'), $tempStream); // for debugging
  113. return $tempStream;
  114. } else {
  115. return $body;
  116. }
  117. }
  118. /**
  119. * authenticate user
  120. *
  121. * @param string $_username
  122. * @param string $_password
  123. * @param \Zend\Http\Request $request
  124. * @return bool
  125. */
  126. protected function _authenticate($_username, $password, \Zend\Http\Request $request)
  127. {
  128. $pos = strrchr($_username, '\\');
  129. if($pos !== false) {
  130. $username = substr(strrchr($_username, '\\'), 1);
  131. } else {
  132. $username = $_username;
  133. }
  134. return Tinebase_Controller::getInstance()->login(
  135. $username,
  136. $password,
  137. $request,
  138. self::REQUEST_TYPE
  139. );
  140. }
  141. /**
  142. * check user permissions
  143. *
  144. * @param string $loginName
  145. * @return boolean
  146. */
  147. protected function _checkUserPermissions($loginName)
  148. {
  149. try {
  150. $activeSync = Tinebase_Application::getInstance()->getApplicationByName('ActiveSync');
  151. } catch (Tinebase_Exception_NotFound $e) {
  152. header('HTTP/1.1 403 ActiveSync not enabled for account ' . $loginName);
  153. Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ActiveSync is not installed');
  154. return false;
  155. }
  156. if ($activeSync->status != 'enabled') {
  157. header('HTTP/1.1 403 ActiveSync not enabled for account ' . $loginName);
  158. Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ActiveSync is not enabled');
  159. return false;
  160. }
  161. if (Tinebase_Core::getUser()->hasRight($activeSync, Tinebase_Acl_Rights::RUN) !== true) {
  162. header('HTTP/1.1 403 ActiveSync not enabled for account ' . $loginName);
  163. Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ActiveSync is not enabled for account');
  164. return false;
  165. }
  166. return true;
  167. }
  168. /**
  169. * init registry
  170. */
  171. protected function _initializeRegistry()
  172. {
  173. ActiveSync_Controller::initSyncrotonRegistry();
  174. $applications = is_object(Tinebase_Core::getUser())
  175. ? Tinebase_Core::getUser()->getApplications()
  176. : new Tinebase_Record_RecordSet('Tinebase_Model_Application');
  177. if ($applications->find('name', 'Addressbook')) {
  178. Syncroton_Registry::setContactsDataClass('Addressbook_Frontend_ActiveSync');
  179. Syncroton_Registry::setGALDataClass('Addressbook_Frontend_ActiveSync');
  180. }
  181. if ($applications->find('name', 'Calendar')) {
  182. Syncroton_Registry::setCalendarDataClass('Calendar_Frontend_ActiveSync');
  183. }
  184. if ($applications->find('name', 'Expressomail')) {
  185. Syncroton_Registry::setEmailDataClass('Expressomail_Frontend_ActiveSync');
  186. }
  187. if ($applications->find('name', 'Felamimail')) {
  188. Syncroton_Registry::setEmailDataClass('Felamimail_Frontend_ActiveSync');
  189. }
  190. if ($applications->find('name', 'Tasks')) {
  191. Syncroton_Registry::setTasksDataClass('Tasks_Frontend_ActiveSync');
  192. }
  193. Syncroton_Registry::set(Syncroton_Registry::DEFAULT_POLICY, ActiveSync_Config::getInstance()->get(ActiveSync_Config::DEFAULT_POLICY));
  194. }
  195. }