PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Install/Controller/InstallController.php

https://github.com/kareypowell/croogo
PHP | 251 lines | 155 code | 25 blank | 71 comment | 20 complexity | 8b459402cd53c63cdc6f6f8a2b708689 MD5 | raw file
  1. <?php
  2. App::uses('Controller', 'Controller');
  3. App::uses('File', 'Utility');
  4. App::uses('InstallManager', 'Install.Lib');
  5. /**
  6. * Install Controller
  7. *
  8. * @category Controller
  9. * @package Croogo
  10. * @version 1.0
  11. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class InstallController extends Controller {
  16. /**
  17. * Components
  18. *
  19. * @var array
  20. * @access public
  21. */
  22. public $components = array('Session');
  23. /**
  24. * Helpers
  25. *
  26. * @var array
  27. * @access public
  28. */
  29. public $helpers = array(
  30. 'Html' => array(
  31. 'className' => 'CroogoHtml',
  32. ),
  33. 'Form' => array(
  34. 'className' => 'CroogoForm',
  35. ),
  36. 'Croogo.Layout',
  37. );
  38. /**
  39. * beforeFilter
  40. *
  41. * @return void
  42. * @access public
  43. */
  44. public function beforeFilter() {
  45. parent::beforeFilter();
  46. $this->layout = 'install';
  47. $this->_generateAssets();
  48. }
  49. /**
  50. * Generate assets
  51. */
  52. protected function _generateAssets() {
  53. $file = CakePlugin::path('Croogo') . 'webroot' . DS . 'css' . DS . 'croogo-bootstrap.css';
  54. if (!file_exists($file)) {
  55. App::uses('AssetGenerator', 'Install.Lib');
  56. $generator = new AssetGenerator();
  57. try {
  58. $generator->generate();
  59. } catch (Exception $e) {
  60. $this->log($e->getMessage());
  61. $this->Session->setFlash('Asset generation failed. Please verify that dependencies exists and readable.', 'default', array('class' => 'error'));
  62. }
  63. }
  64. }
  65. /**
  66. * If settings.json exists, app is already installed
  67. *
  68. * @return void
  69. */
  70. protected function _check() {
  71. if (Configure::read('Croogo.installed') && Configure::read('Install.secured')) {
  72. $this->Session->setFlash('Already Installed');
  73. return $this->redirect('/');
  74. }
  75. }
  76. /**
  77. * Step 0: welcome
  78. *
  79. * A simple welcome message for the installer.
  80. *
  81. * @return void
  82. * @access public
  83. */
  84. public function index() {
  85. $this->_check();
  86. $this->set('title_for_layout', __d('croogo', 'Installation: Welcome'));
  87. }
  88. /**
  89. * Step 1: database
  90. *
  91. * Try to connect to the database and give a message if that's not possible so the user can check their
  92. * credentials or create the missing database
  93. * Create the database file and insert the submitted details
  94. *
  95. * @return void
  96. * @access public
  97. */
  98. public function database() {
  99. $this->_check();
  100. $this->set('title_for_layout', __d('croogo', 'Step 1: Database'));
  101. if (Configure::read('Croogo.installed')) {
  102. return $this->redirect(array('action' => 'adminuser'));
  103. }
  104. if (!empty($this->request->data)) {
  105. $InstallManager = new InstallManager();
  106. $result = $InstallManager->createDatabaseFile(array(
  107. 'Install' => $this->request->data,
  108. ));
  109. if ($result !== true) {
  110. $this->Session->setFlash($result, 'default', array('class' => 'error'));
  111. } else {
  112. return $this->redirect(array('action' => 'data'));
  113. }
  114. }
  115. $currentConfiguration = array(
  116. 'exists' => false,
  117. 'valid' => false,
  118. );
  119. if (file_exists(APP . 'Config' . DS . 'database.php')) {
  120. $currentConfiguration['exists'] = true;
  121. }
  122. if ($currentConfiguration['exists']) {
  123. try {
  124. $this->loadModel('Install.Install');
  125. $ds = $this->Install->getDataSource();
  126. $ds->cacheSources = false;
  127. $sources = $ds->listSources();
  128. $currentConfiguration['valid'] = true;
  129. } catch (Exception $e) {
  130. }
  131. }
  132. $this->set(compact('currentConfiguration'));
  133. }
  134. /**
  135. * Step 2: Run the initial sql scripts to create the db and seed it with data
  136. *
  137. * @return void
  138. * @access public
  139. */
  140. public function data() {
  141. $this->_check();
  142. $this->set('title_for_layout', __d('croogo', 'Step 2: Build database'));
  143. $this->loadModel('Install.Install');
  144. $ds = $this->Install->getDataSource();
  145. $ds->cacheSources = false;
  146. $sources = $ds->listSources();
  147. if (!empty($sources)) {
  148. $this->Session->setFlash(
  149. __d('croogo', 'Warning: Database "%s" is not empty.', $ds->config['database']),
  150. 'default', array('class' => 'error')
  151. );
  152. }
  153. if ($this->request->query('run')) {
  154. set_time_limit(10 * MINUTE);
  155. $this->Install->setupDatabase();
  156. $InstallManager = new InstallManager();
  157. $result = $InstallManager->createCroogoFile();
  158. if ($result !== true) {
  159. return $this->Session->setFlash($result, 'default', array('class' => 'error'));
  160. }
  161. return $this->redirect(array('action' => 'adminuser'));
  162. }
  163. }
  164. /**
  165. * Step 3: get username and passwords for administrative user
  166. */
  167. public function adminuser() {
  168. if (!file_exists(APP . 'Config' . DS . 'database.php')) {
  169. return $this->redirect('/');
  170. }
  171. if ($this->request->is('post')) {
  172. if (!CakePlugin::loaded('Users')) {
  173. CakePlugin::load('Users');
  174. }
  175. $this->loadModel('Users.User');
  176. $this->User->set($this->request->data);
  177. if ($this->User->validates()) {
  178. $user = $this->Install->addAdminUser($this->request->data);
  179. if ($user) {
  180. $this->Session->write('Install.user', $user);
  181. return $this->redirect(array('action' => 'finish'));
  182. }
  183. }
  184. }
  185. }
  186. /**
  187. * Step 4: finish
  188. *
  189. * Copy settings.json file into place and create user obtained in step 3
  190. *
  191. * @return void
  192. * @access public
  193. */
  194. public function finish($token = null) {
  195. $this->set('title_for_layout', __d('croogo', 'Installation successful'));
  196. $this->_check();
  197. $InstallManager = new InstallManager();
  198. $installed = $InstallManager->createSettingsFile();
  199. if ($installed === true) {
  200. $InstallManager->installCompleted();
  201. } else {
  202. $this->set('title_for_layout', __d('croogo', 'Installation failed'));
  203. $msg = __d('croogo', 'Installation failed: Unable to create settings file');
  204. $this->Session->setFlash($msg, 'default', array('class' => 'error'));
  205. }
  206. $urlBlogAdd = Router::url(array(
  207. 'plugin' => 'nodes',
  208. 'admin' => true,
  209. 'controller' => 'nodes',
  210. 'action' => 'add',
  211. 'blog',
  212. ));
  213. $urlSettings = Router::url(array(
  214. 'plugin' => 'settings',
  215. 'admin' => true,
  216. 'controller' => 'settings',
  217. 'action' => 'prefix',
  218. 'Site',
  219. ));
  220. $this->set('user', $this->Session->read('Install.user'));
  221. if ($installed) {
  222. $this->Session->destroy();
  223. }
  224. $this->set(compact('urlBlogAdd', 'urlSettings', 'installed'));
  225. }
  226. }