PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Expressodriver/Controller.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 184 lines | 93 code | 20 blank | 71 comment | 9 complexity | b4292ce7256b81f775c3e561f9460024 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * MAIN controller for expressodriver, does event and container handling
  6. *
  7. * @package Expressodriver
  8. * @subpackage Controller
  9. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  10. * @copyright Copyright (c) 2007-2014 Metaways Infosystems GmbH (http://www.metaways.de)
  11. * @copyright Copyright (c) 2014 Serpro (http://www.serpro.gov.br)
  12. * @author Marcelo Teixeira <marcelo.teixeira@serpro.gov.br>
  13. * @author Edgar de Lucca <edgar.lucca@serpro.gov.br>
  14. *
  15. */
  16. /**
  17. * main controller for Expressodriver
  18. *
  19. * @package Expressodriver
  20. * @subpackage Controller
  21. */
  22. class Expressodriver_Controller extends Tinebase_Controller_Event
  23. {
  24. /**
  25. * holds the default Model of this application
  26. * @var string
  27. */
  28. protected static $_defaultModel = 'Expressodriver_Model_Node';
  29. /**
  30. * holds the instance of the singleton
  31. *
  32. * @var Filemamager_Controller
  33. */
  34. private static $_instance = NULL;
  35. /**
  36. * constructor (get current user)
  37. */
  38. private function __construct()
  39. {
  40. }
  41. /**
  42. * don't clone. Use the singleton.
  43. *
  44. */
  45. private function __clone()
  46. {
  47. }
  48. /**
  49. * the singleton pattern
  50. *
  51. * @return Expressodriver_Controller
  52. */
  53. public static function getInstance()
  54. {
  55. if (self::$_instance === NULL) {
  56. self::$_instance = new Expressodriver_Controller;
  57. }
  58. return self::$_instance;
  59. }
  60. /**
  61. * event handler function
  62. *
  63. * all events get routed through this function
  64. *
  65. * @param Tinebase_Event_Abstract $_eventObject the eventObject
  66. *
  67. * @todo write test
  68. */
  69. protected function _handleEvent(Tinebase_Event_Abstract $_eventObject)
  70. {
  71. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' (' . __LINE__ . ') handle event of type ' . get_class($_eventObject));
  72. switch(get_class($_eventObject)) {
  73. case 'Admin_Event_AddAccount':
  74. break;
  75. case 'Admin_Event_DeleteAccount':
  76. break;
  77. }
  78. }
  79. /**
  80. * get expressodriver settings
  81. *
  82. * @param bool $_resolve
  83. * @return array
  84. */
  85. public function getConfigSettings($_resolve = FALSE)
  86. {
  87. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
  88. . ' Fetching Expressodriver Settings ...');
  89. $defaults = array(
  90. 'default' => array(
  91. 'useCache' => true,
  92. 'cacheLifetime' => 86400, // one day
  93. ),
  94. 'adapters' => array(),
  95. );
  96. $result = Expressodriver_Config::getInstance()->get('expressodriverSettings', new Tinebase_Config_Struct($defaults))->toArray();
  97. return $result;
  98. }
  99. /**
  100. * save expressodriver settings
  101. *
  102. * @param array $_settings
  103. * @return Crm_Model_Config
  104. *
  105. */
  106. public function saveConfigSettings($_settings)
  107. {
  108. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
  109. . ' Updating Crm Settings: ' . print_r($_settings, TRUE));
  110. $_settings = array(
  111. 'default' => $_settings['default'],
  112. 'adapters' => $_settings['adapters'],
  113. );
  114. Expressodriver_Config::getInstance()->set('expressodriverSettings', $_settings);
  115. return $this->getConfigSettings();
  116. }
  117. /**
  118. * set credentials for given adapter
  119. *
  120. * @param string $adapterName
  121. * @param string $password
  122. * @return array
  123. */
  124. public function setCredentials($adapterName, $password)
  125. {
  126. $adapter = null;
  127. $config = $this->getConfigSettings();
  128. foreach ($config['adapters'] as $adapterConfig) {
  129. if ($adapterName === $adapterConfig['name']) {
  130. $adapter = $adapterConfig;
  131. }
  132. }
  133. $url = $adapter['url'];
  134. if ($adapter['adapter'] == 'owncloud') {
  135. $url = rtrim($url, '/');
  136. $url .= '/remote.php/webdav/';
  137. }
  138. $username = $adapter['useEmailAsLoginName']
  139. ? Tinebase_Core::getUser()->accountEmailAddress
  140. : Tinebase_Core::getUser()->accountLoginName;
  141. $options = array(
  142. 'host' => $adapter['url'],
  143. 'user' => $username,
  144. 'password' => $password,
  145. 'root' => '/',
  146. 'name' => $adapter['name'],
  147. 'useCache' => $config['default']['useCache'],
  148. 'cacheLifetime' => $config['default']['cacheLifetime'],
  149. );
  150. $adapterInstance = Expressodriver_Backend_Storage_Abstract::factory($adapter['adapter'], $options);
  151. // check authentication for owncloud/webdav
  152. if ($adapterInstance->checkCredentials($url, $username, $password)) {
  153. Expressodriver_Session::getSessionNamespace()->password[$adapterName] = $password;
  154. return array(
  155. 'success' => true
  156. );
  157. } else {
  158. return array(
  159. 'success' => false,
  160. 'errorMessage' => 'Invalid Credentials'
  161. );
  162. }
  163. }
  164. }