PageRenderTime 67ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/CoreVersions/0.2.0/Frameworks/BaikalAdmin/Controller/Install/Initialize.php

http://github.com/jeromeschneider/Baikal
PHP | 187 lines | 93 code | 38 blank | 56 comment | 10 complexity | 9111082674820200dfef4913ef94d68d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. #################################################################
  3. # Copyright notice
  4. #
  5. # (c) 2012 Jérôme Schneider <mail@jeromeschneider.fr>
  6. # All rights reserved
  7. #
  8. # http://baikal.codr.fr
  9. #
  10. # This script is part of the Baďkal Server project. The Baďkal
  11. # Server project is free software; you can redistribute it
  12. # and/or modify it under the terms of the GNU General Public
  13. # License as published by the Free Software Foundation; either
  14. # version 2 of the License, or (at your option) any later version.
  15. #
  16. # The GNU General Public License can be found at
  17. # http://www.gnu.org/copyleft/gpl.html.
  18. #
  19. # This script is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # This copyright notice MUST APPEAR in all copies of the script!
  25. #################################################################
  26. namespace BaikalAdmin\Controller\Install;
  27. class Initialize extends \Flake\Core\Controller {
  28. protected $aMessages = array();
  29. protected $oModel;
  30. protected $oForm; # \Formal\Form
  31. public function __construct() {
  32. parent::__construct();
  33. # Assert that /Specific is writable
  34. if(!file_exists(PROJECT_PATH_SPECIFIC) || !is_dir(PROJECT_PATH_SPECIFIC) || !is_writable(PROJECT_PATH_SPECIFIC)) {
  35. throw new \Exception("Specific/ dir is readonly. Baďkal Admin requires write permissions on this dir.");
  36. }
  37. $this->createDefaultConfigFilesIfNeeded();
  38. $this->oModel = new \Baikal\Model\Config\Standard(PROJECT_PATH_SPECIFIC . "config.php");
  39. # Assert that config file is writable
  40. if(!$this->oModel->writable()) {
  41. throw new \Exception("Config file is not writable;" . __FILE__ . " > " . __LINE__);
  42. }
  43. $this->oForm = $this->oModel->formForThisModelInstance(array(
  44. "close" => FALSE
  45. ));
  46. }
  47. public function execute() {
  48. if($this->oForm->submitted()) {
  49. $this->oForm->execute();
  50. if($this->oForm->persisted()) {
  51. $sContent = file_get_contents(PROJECT_PATH_SPECIFIC . "config.system.php");
  52. $sBaikalVersion = BAIKAL_VERSION;
  53. $sEncryptionKey = md5(microtime() . rand());
  54. # Setting "BAIKAL_CONFIGURED_VERSION"
  55. $sNewConstants =<<<PHP
  56. # A random 32 bytes key that will be used to encrypt data
  57. define("BAIKAL_ENCRYPTION_KEY", "{$sEncryptionKey}");
  58. # The currently configured Baďkal version
  59. define("BAIKAL_CONFIGURED_VERSION", "{$sBaikalVersion}");
  60. PHP;
  61. # Writing results to file
  62. file_put_contents(PROJECT_PATH_SPECIFIC . "config.system.php", $sContent . "\n\n" . $sNewConstants);
  63. }
  64. }
  65. }
  66. public function render() {
  67. $sBigIcon = "glyph2x-magic";
  68. $sBaikalVersion = BAIKAL_VERSION;
  69. $oView = new \BaikalAdmin\View\Install\Initialize();
  70. $oView->setData("baikalversion", BAIKAL_VERSION);
  71. if($this->oForm->persisted()) {
  72. $sMessage = "<p>Baďkal is now configured. You may now <a class='btn btn-success' href='" . PROJECT_URI . "admin/'>Access the Baďkal admin</a></h2>";
  73. $sForm = "";
  74. } else {
  75. $sMessage = "";
  76. $sForm = $this->oForm->render();
  77. }
  78. $oView->setData("message", $sMessage);
  79. $oView->setData("form", $sForm);
  80. return $oView->render();
  81. }
  82. protected function tagConfiguredVersion() {
  83. file_put_contents(PROJECT_PATH_SPECIFIC . "config.php", $sContent);
  84. }
  85. protected function createDefaultConfigFilesIfNeeded() {
  86. # Create empty config.php if needed
  87. if(!file_exists(PROJECT_PATH_SPECIFIC . "config.php")) {
  88. @touch(PROJECT_PATH_SPECIFIC . "config.php");
  89. $sContent = "<?php\n" . \Baikal\Core\Tools::getCopyrightNotice() . "\n\n";
  90. $sContent .= $this->getDefaultConfig();
  91. file_put_contents(PROJECT_PATH_SPECIFIC . "config.php", $sContent);
  92. }
  93. # Create empty config.system.php if needed
  94. if(!file_exists(PROJECT_PATH_SPECIFIC . "config.system.php")) {
  95. @touch(PROJECT_PATH_SPECIFIC . "config.system.php");
  96. $sContent = "<?php\n" . \Baikal\Core\Tools::getCopyrightNotice() . "\n\n";
  97. $sContent .= $this->getDefaultSystemConfig();
  98. file_put_contents(PROJECT_PATH_SPECIFIC . "config.system.php", $sContent);
  99. }
  100. }
  101. protected function getDefaultConfig() {
  102. $sCode =<<<CODE
  103. ##############################################################################
  104. # Required configuration
  105. # You *have* to review these settings for Baďkal to run properly
  106. #
  107. # Timezone of your users, if unsure, check http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  108. define("PROJECT_TIMEZONE", "Europe/Paris");
  109. # CardDAV ON/OFF switch; default TRUE
  110. define("BAIKAL_CARD_ENABLED", TRUE);
  111. # CalDAV ON/OFF switch; default TRUE
  112. define("BAIKAL_CAL_ENABLED", TRUE);
  113. # Baďkal Web Admin ON/OFF switch; default TRUE
  114. define("BAIKAL_ADMIN_ENABLED", TRUE);
  115. # Baďkal Web Admin autolock ON/OFF switch; default TRUE
  116. define("BAIKAL_ADMIN_AUTOLOCKENABLED", TRUE);
  117. # Baďkal Web admin password hash; Set by Core/Scripts/adminpassword.php or via Baďkal Web Admin
  118. define("BAIKAL_ADMIN_PASSWORDHASH", "");
  119. CODE;
  120. $sCode = trim($sCode);
  121. return $sCode;
  122. }
  123. protected function getDefaultSystemConfig() {
  124. $sCode =<<<CODE
  125. ##############################################################################
  126. # System configuration
  127. # Should not be changed, unless YNWYD
  128. #
  129. # RULES
  130. # 0. All folder pathes *must* be suffixed by "/"
  131. # 1. All URIs *must* be suffixed by "/" if pointing to a folder
  132. #
  133. # Standalone Server, allowed or not; default FALSE
  134. define("BAIKAL_STANDALONE_ALLOWED", FALSE);
  135. # Standalone Server, port number; default 8888
  136. define("BAIKAL_STANDALONE_PORT", 8888);
  137. # PATH to SabreDAV
  138. define("BAIKAL_PATH_SABREDAV", PROJECT_PATH_FRAMEWORKS . "SabreDAV/lib/Sabre/");
  139. # If you change this value, you'll have to re-generate passwords for all your users
  140. define("BAIKAL_AUTH_REALM", "BaikalDAV");
  141. # Should begin and end with a "/"
  142. define("BAIKAL_CARD_BASEURI", PROJECT_BASEURI . "card.php/");
  143. # Should begin and end with a "/"
  144. define("BAIKAL_CAL_BASEURI", PROJECT_BASEURI . "cal.php/");
  145. CODE;
  146. $sCode = trim($sCode);
  147. return $sCode;
  148. }
  149. }