PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/tine20/Setup/Frontend/Json.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 459 lines | 263 code | 51 blank | 145 comment | 22 complexity | 5fd572edcbdd737663da5c3f85f90636 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. * @package Tinebase
  5. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  6. * @author Philipp Schuele <p.schuele@metaways.de>
  7. * @copyright Copyright (c) 2008-2009 Metaways Infosystems GmbH (http://www.metaways.de)
  8. *
  9. */
  10. /**
  11. * Setup json frontend
  12. *
  13. * @package Setup
  14. * @subpackage Frontend
  15. */
  16. class Setup_Frontend_Json extends Tinebase_Frontend_Abstract
  17. {
  18. /**
  19. * the internal name of the application
  20. *
  21. * @var string
  22. */
  23. protected $_applicationName = 'Setup';
  24. /**
  25. * setup controller
  26. *
  27. * @var Setup_Controller
  28. */
  29. protected $_controller = NULL;
  30. /**
  31. * the constructor
  32. *
  33. */
  34. public function __construct()
  35. {
  36. $this->_controller = Setup_Controller::getInstance();
  37. }
  38. /**
  39. * authenticate user by username and password
  40. *
  41. * @param string $username the username
  42. * @param string $password the password
  43. * @return array
  44. */
  45. public function login($username, $password)
  46. {
  47. Setup_Core::startSetupSession();
  48. if (Setup_Controller::getInstance()->login($username, $password)) {
  49. $response = array(
  50. 'success' => TRUE,
  51. //'account' => Tinebase_Core::getUser()->getPublicUser()->toArray(),
  52. //'jsonKey' => Setup_Core::get('jsonKey'),
  53. 'welcomeMessage' => "Welcome to Tine 2.0 Setup!"
  54. );
  55. } else {
  56. $response = array(
  57. 'success' => FALSE,
  58. 'errorMessage' => "Wrong username or password!"
  59. );
  60. }
  61. return $response;
  62. }
  63. /**
  64. * destroy session
  65. *
  66. * @return array
  67. */
  68. public function logout()
  69. {
  70. Setup_Controller::getInstance()->logout();
  71. return array(
  72. 'success'=> true,
  73. );
  74. }
  75. /**
  76. * install new applications
  77. *
  78. * @param array $applicationNames application names to install
  79. * @param array | optional $options
  80. */
  81. public function installApplications($applicationNames, $options = null)
  82. {
  83. if (is_array($applicationNames)) {
  84. $this->_controller->installApplications($applicationNames, $options);
  85. $result = TRUE;
  86. } else {
  87. Setup_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not handle param $applicationNames: ' . $decodedNames);
  88. $result = FALSE;
  89. }
  90. return array(
  91. 'success' => $result,
  92. 'setupRequired' => $this->_controller->setupRequired()
  93. );
  94. }
  95. /**
  96. * update existing applications
  97. *
  98. * @param array $applicationNames application names to update
  99. */
  100. public function updateApplications($applicationNames)
  101. {
  102. $applications = new Tinebase_Record_RecordSet('Tinebase_Model_Application');
  103. foreach ($applicationNames as $applicationName) {
  104. $applications->addRecord(Tinebase_Application::getInstance()->getApplicationByName($applicationName));
  105. }
  106. if(count($applications) > 0) {
  107. $this->_controller->updateApplications($applications);
  108. }
  109. return array(
  110. 'success'=> true,
  111. 'setupRequired' => $this->_controller->setupRequired()
  112. );
  113. }
  114. /**
  115. * uninstall applications
  116. *
  117. * @param array $applicationNames application names to uninstall
  118. */
  119. public function uninstallApplications($applicationNames)
  120. {
  121. $this->_controller->uninstallApplications($applicationNames);
  122. return array(
  123. 'success'=> true,
  124. 'setupRequired' => $this->_controller->setupRequired()
  125. );
  126. }
  127. /**
  128. * search for installed and installable applications
  129. *
  130. * @return array
  131. */
  132. public function searchApplications()
  133. {
  134. return $this->_controller->searchApplications();
  135. }
  136. /**
  137. * do the environment check
  138. *
  139. * @return array
  140. */
  141. public function envCheck()
  142. {
  143. return Setup_Controller::getInstance()->checkRequirements();
  144. }
  145. /**
  146. * load config data from config file / default data
  147. *
  148. * @return array
  149. */
  150. public function loadConfig()
  151. {
  152. $result = (! Setup_Core::configFileExists())
  153. ? Setup_Controller::getInstance()->getConfigDefaults()
  154. : ((Setup_Core::isRegistered(Setup_Core::USER)) ? Setup_Controller::getInstance()->getConfigData() : array());
  155. return $result;
  156. }
  157. /**
  158. * change domain for update
  159. *
  160. * @param array $data
  161. * @return string default domain
  162. */
  163. public function changeDomainForUpdate($data)
  164. {
  165. $targetDomain = NULL;
  166. $defaultDomain = NULL;
  167. if (isset($data['target_domain'])) {
  168. $targetDomain = $data['target_domain'];
  169. $defaultDomain = Tinebase_Config::getDomain();
  170. if ($targetDomain == $defaultDomain) {
  171. return NULL;
  172. }
  173. $this->_controller->changeDomainConfig($targetDomain);
  174. }
  175. return $defaultDomain;
  176. }
  177. /**
  178. * restore default domain
  179. *
  180. * @param string $domain
  181. */
  182. public function restoreDomain($domain)
  183. {
  184. $this->_controller->changeDomainConfig($domain);
  185. }
  186. /**
  187. * save config data in config file
  188. *
  189. * @param array $data
  190. * @return array with config data
  191. */
  192. public function saveConfig($data)
  193. {
  194. $defaultDomain = $this->changeDomainForUpdate($data);
  195. if (isset($data['target_domain'])) {
  196. unset($data['target_domain']);
  197. }
  198. Setup_Controller::getInstance()->saveConfigData($data);
  199. if ($defaultDomain) {
  200. $this->restoreDomain($defaultDomain);
  201. }
  202. return $this->checkConfig();
  203. }
  204. /**
  205. * check config and return status
  206. *
  207. * @return array
  208. *
  209. * @todo add check if db settings have changed?
  210. */
  211. public function checkConfig()
  212. {
  213. try {
  214. Tinebase_Core::getDb('asdatabase');
  215. $checkAsDB = Setup_Core::get(Setup_Core::CHECKDB);
  216. } catch(Exception $e) {
  217. $checkAsDB = false;
  218. }
  219. Setup_Core::setupDatabaseConnection();
  220. $checkDB = Setup_Core::get(Setup_Core::CHECKDB);
  221. $result = array(
  222. 'configExists' => Setup_Core::configFileExists(),
  223. 'configWritable' => Setup_Core::configFileWritable(),
  224. 'checkDB' => $checkDB,
  225. 'checkAsDB' => $checkAsDB,
  226. 'checkLogger' => $this->_controller->checkConfigLogger(),
  227. 'checkCaching' => $this->_controller->checkConfigCaching(),
  228. 'checkQueue' => $this->_controller->checkConfigQueue(),
  229. 'checkTmpDir' => $this->_controller->checkDir('tmpdir'),
  230. 'checkSession' => $this->_controller->checkConfigSession(),
  231. 'checkFilesDir' => $this->_controller->checkDir('filesdir'),
  232. 'setupRequired' => empty($checkDB) ? TRUE : $this->_controller->setupRequired(),
  233. );
  234. return $result;
  235. }
  236. /**
  237. * load auth config data
  238. *
  239. * @return array
  240. */
  241. public function loadAuthenticationData()
  242. {
  243. return $this->_controller->loadAuthenticationData();
  244. }
  245. /**
  246. * Update authentication data (needs Tinebase tables to store the data)
  247. *
  248. * Installs Tinebase if not already installed
  249. *
  250. * @todo validate $data
  251. *
  252. * @param array $data
  253. * @return array [success status]
  254. */
  255. public function saveAuthentication($data)
  256. {
  257. $defaultDomain = $this->changeDomainForUpdate($data);
  258. if (isset($data['target_domain'])) {
  259. unset($data['target_domain']);
  260. }
  261. $this->_controller->saveAuthentication($data);
  262. if ($defaultDomain) {
  263. $this->_controller->changeDomainConfig(Tinebase_Config::getDomain());
  264. }
  265. return array(
  266. 'success' => true,
  267. 'setupRequired' => $this->_controller->setupRequired()
  268. );
  269. }
  270. /**
  271. * load email config data
  272. *
  273. * @return array
  274. */
  275. public function getEmailConfig()
  276. {
  277. return Tinebase_Config_Manager::getInstance()->getEmailConfig();
  278. }
  279. /**
  280. * Update email config data
  281. *
  282. * @param array $data
  283. * @return array [success status]
  284. */
  285. public function saveEmailConfig($data)
  286. {
  287. $defaultDomain = $this->changeDomainForUpdate($data);
  288. if (isset($data['target_domain'])) {
  289. unset($data['target_domain']);
  290. }
  291. $this->_controller->saveEmailConfig($data);
  292. if ($defaultDomain) {
  293. $this->_controller->changeDomainConfig(Tinebase_Config::getDomain());
  294. }
  295. return array(
  296. 'success' => true,
  297. );
  298. }
  299. /**
  300. * Returns registry data of setup
  301. * .
  302. * @see Tinebase_Application_Json_Abstract
  303. *
  304. * @return mixed array 'variable name' => 'data'
  305. *
  306. * @todo add 'titlePostfix' => Tinebase_Config::getInstance()->getConfig(Tinebase_Config::PAGETITLEPOSTFIX, NULL, '')->value here?
  307. */
  308. public function getRegistryData()
  309. {
  310. // anonymous registry
  311. $registryData = array(
  312. 'configExists' => Setup_Core::configFileExists(),
  313. 'version' => array(
  314. 'buildType' => TINE20_BUILDTYPE,
  315. 'codeName' => TINE20SETUP_CODENAME,
  316. 'packageString' => TINE20SETUP_PACKAGESTRING,
  317. 'releaseTime' => TINE20SETUP_RELEASETIME
  318. ),
  319. 'authenticationData' => $this->loadAuthenticationData(),
  320. );
  321. if (Tinebase_Config_Manager::isMultidomain()) {
  322. $domainArray = Tinebase_Config_Manager::getDomainNames();
  323. $activeDomain = Tinebase_Config::getDomain();
  324. } else {
  325. $domainArray = array('default');
  326. $activeDomain = 'default';
  327. }
  328. // authenticated or non existent config
  329. if (! Setup_Core::configFileExists() || Setup_Core::isRegistered(Setup_Core::USER)) {
  330. $registryData = array_merge($registryData, $this->checkConfig());
  331. $registryData = array_merge($registryData, array(
  332. 'acceptedTermsVersion' => (! empty($registryData['checkDB']) && $this->_controller->isInstalled('Tinebase')) ? Setup_Controller::getInstance()->getAcceptedTerms() : 0,
  333. 'setupChecks' => $this->envCheck(),
  334. 'configData' => $this->loadConfig(),
  335. 'emailData' => $this->getEmailConfig(),
  336. ));
  337. $domainConfigDataArray = array();
  338. foreach ($domainArray as $domain) {
  339. if (sizeof($domainArray) > 1) {
  340. $this->_controller->changeDomainConfig($domain);
  341. }
  342. $domainConfig = array('configData' => $this->loadConfig());
  343. $domainConfig['emailData'] = $this->getEmailConfig();
  344. $domainConfig['authenticationData'] = $this->loadAuthenticationData();
  345. $domainConfig = array_merge($domainConfig, Tinebase_PluginManager::getPluginConfigItems());
  346. $domainConfigDataArray[$domain] = $domainConfig;
  347. }
  348. $registryData = array_merge($registryData, array('domainConfig' => $domainConfigDataArray));
  349. if (sizeof($domainArray) > 1) {
  350. $this->_controller->changeDomainConfig($activeDomain);
  351. }
  352. }
  353. $registryData = array_merge($registryData, array(
  354. 'domainData' => array(
  355. 'activeDomain' => $activeDomain,
  356. 'domains' => $domainArray,
  357. ),
  358. ));
  359. // if setup user is logged in
  360. if (Setup_Core::isRegistered(Setup_Core::USER)) {
  361. $registryData += array(
  362. 'currentAccount' => Setup_Core::getUser(),
  363. );
  364. }
  365. $registryData['customAuthenticationBackends'] = Tinebase_Auth::getCustomBackends();
  366. $registryData['customAccountsBackends'] = Tinebase_User::getCustomBackends();
  367. return $registryData;
  368. }
  369. /**
  370. * Returns registry data of all applications current user has access to
  371. * @see Tinebase_Application_Json_Abstract
  372. *
  373. * @return mixed array 'variable name' => 'data'
  374. */
  375. public function getAllRegistryData()
  376. {
  377. $registryData['Setup'] = $this->getRegistryData();
  378. $registryData['Setup'] = array_merge($registryData['Setup'], Tinebase_PluginManager::getPluginConfigItems());
  379. $registryData['Setup'] = array_merge($registryData['Setup'], Tinebase_PluginManager::getGlobalPluginConfigItems());
  380. // setup also need some core tinebase regdata
  381. $locale = Tinebase_Core::get('locale');
  382. $registryData['Tinebase'] = array(
  383. 'serviceMap' => Setup_Frontend_Http::getServiceMap(),
  384. 'timeZone' => Setup_Core::getUserTimezone(),
  385. 'jsonKey' => Setup_Core::get('jsonKey'),
  386. 'locale' => array(
  387. 'locale' => $locale->toString(),
  388. 'language' => Zend_Locale::getTranslation($locale->getLanguage(), 'language', $locale),
  389. 'region' => Zend_Locale:: getTranslation($locale->getRegion(), 'country', $locale),
  390. ),
  391. 'version' => array(
  392. 'buildType' => TINE20_BUILDTYPE,
  393. 'codeName' => TINE20SETUP_CODENAME,
  394. 'packageString' => TINE20SETUP_PACKAGESTRING,
  395. 'releaseTime' => TINE20SETUP_RELEASETIME
  396. ),
  397. 'multidomain' => Tinebase_Config_Manager::isMultidomain(),
  398. );
  399. return $registryData;
  400. }
  401. /**
  402. * @return Tinebase_Controller_Abstract
  403. */
  404. public function getController()
  405. {
  406. return $this->_controller;
  407. }
  408. }