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

/concrete/core/controllers/single_pages/install.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 337 lines | 295 code | 35 blank | 7 comment | 45 complexity | 3eca14df67c1b6249486e399ee8a7865 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. ini_set('display_errors', 1);
  4. if (!ini_get('safe_mode')) {
  5. @set_time_limit(150);
  6. }
  7. date_default_timezone_set(@date_default_timezone_get());
  8. define('UPLOAD_FILE_EXTENSIONS_ALLOWED','*.flv;*.jpg;*.gif;*.jpeg;*.ico;*.docx;*.xla;*.png;*.psd;*.swf;*.doc;*.txt;*.xls;*.xlsx;*.csv;*.pdf;*.tiff;*.rtf;*.m4a;*.mov;*.wmv;*.mpeg;*.mpg;*.wav;*.avi;*.m4v;*.mp4;*.mp3;*.qt;*.ppt;*.pptx;*.kml;*.xml');
  9. if (!defined('DIR_FILES_UPLOADED')) {
  10. define('DIR_FILES_UPLOADED', DIR_FILES_UPLOADED_STANDARD);
  11. }
  12. if (!defined('DIR_FILES_TRASH')) {
  13. define('DIR_FILES_TRASH', DIR_FILES_TRASH_STANDARD);
  14. }
  15. define('DIR_FILES_INCOMING', DIR_FILES_UPLOADED . '/incoming');
  16. define('DIR_FILES_UPLOADED_THUMBNAILS', DIR_FILES_UPLOADED . '/thumbnails');
  17. define('DIR_FILES_UPLOADED_THUMBNAILS_LEVEL2', DIR_FILES_UPLOADED . '/thumbnails/level2');
  18. define('DIR_FILES_UPLOADED_THUMBNAILS_LEVEL3', DIR_FILES_UPLOADED . '/thumbnails/level3');
  19. define('DIR_FILES_AVATARS', DIR_FILES_UPLOADED . '/avatars');
  20. class Concrete5_Controller_Install extends Controller {
  21. public $helpers = array('form', 'html');
  22. protected function getLocales() {
  23. Loader::library('3rdparty/Zend/Locale');
  24. $languages = Localization::getAvailableInterfaceLanguages();
  25. if (count($languages) > 0) {
  26. array_unshift($languages, 'en_US');
  27. }
  28. $locales = array();
  29. foreach($languages as $lang) {
  30. $loc = new Zend_Locale($lang);
  31. $locales[$lang] = Zend_Locale::getTranslation($loc->getLanguage(), 'language', $lang);
  32. }
  33. return $locales;
  34. }
  35. public function view() {
  36. $locales = $this->getLocales();
  37. $this->set('locales', $locales);
  38. $this->testAndRunInstall();
  39. }
  40. public function setup() {
  41. }
  42. public function select_language() {
  43. }
  44. /**
  45. * Testing
  46. */
  47. public function on_start() {
  48. if (isset($_POST['locale']) && $_POST['locale']) {
  49. define("ACTIVE_LOCALE", $_POST['locale']);
  50. $this->set('locale', $_POST['locale']);
  51. }
  52. require(DIR_BASE_CORE . '/config/file_types.php');
  53. Cache::disableCache();
  54. Cache::disableLocalCache();
  55. $this->setRequiredItems();
  56. $this->setOptionalItems();
  57. Loader::model('package/starting_point');
  58. if (file_exists(DIR_CONFIG_SITE . '/site.php')) {
  59. throw new Exception(t('concrete5 is already installed.'));
  60. }
  61. if (!isset($_COOKIE['CONCRETE5_INSTALL_TEST'])) {
  62. setcookie('CONCRETE5_INSTALL_TEST', '1', 0, DIR_REL . '/');
  63. }
  64. }
  65. protected function testAndRunInstall() {
  66. if (file_exists(DIR_CONFIG_SITE . '/site_install_user.php')) {
  67. require(DIR_CONFIG_SITE . '/site_install.php');
  68. @include(DIR_CONFIG_SITE . '/site_install_user.php');
  69. if (defined('ACTIVE_LOCALE') && Localization::activeLocale() !== ACTIVE_LOCALE) {
  70. Localization::changeLocale(ACTIVE_LOCALE);
  71. }
  72. $e = Loader::helper('validation/error');
  73. $e = $this->validateDatabase($e);
  74. if ($e->has()) {
  75. $this->set('error', $e);
  76. } else {
  77. $this->addHeaderItem(Loader::helper('html')->css('jquery.ui.css'));
  78. $this->addHeaderItem(Loader::helper('html')->javascript('jquery.ui.js'));
  79. if (defined('INSTALL_STARTING_POINT') && INSTALL_STARTING_POINT) {
  80. $spl = Loader::startingPointPackage(INSTALL_STARTING_POINT);
  81. } else {
  82. $spl = Loader::startingPointPackage('standard');
  83. }
  84. $this->set('installPackage', $spl->getPackageHandle());
  85. $this->set('installRoutines', $spl->getInstallRoutines());
  86. $this->set('successMessage', t('Congratulations. concrete5 has been installed. You have been logged in as <b>%s</b> with the password you chose. If you wish to change this password, you may do so from the users area of the dashboard.', USER_SUPER, $uPassword));
  87. }
  88. }
  89. }
  90. private function setRequiredItems() {
  91. $this->set('imageTest', function_exists('imagecreatetruecolor'));
  92. $this->set('mysqlTest', function_exists('mysql_connect'));
  93. $this->set('xmlTest', function_exists('xml_parse') && function_exists('simplexml_load_file'));
  94. $this->set('fileWriteTest', $this->testFileWritePermissions());
  95. $phpVmin = '5.2.4';
  96. if (version_compare(PHP_VERSION, $phpVmin, '>=')) {
  97. $phpVtest = true;
  98. } else {
  99. $phpVtest = false;
  100. }
  101. $this->set('phpVmin',$phpVmin);
  102. $this->set('phpVtest',$phpVtest);
  103. }
  104. private function setOptionalItems() {
  105. // no longer need lucene
  106. //$this->set('searchTest', function_exists('iconv') && function_exists('mb_strtolower') && (@preg_match('/\pL/u', 'a') == 1));
  107. $this->set('remoteFileUploadTest', function_exists('iconv'));
  108. }
  109. public function passedRequiredItems() {
  110. if ($this->get('imageTest') && $this->get('mysqlTest') && $this->get('fileWriteTest') && $this->get('xmlTest') && $this->get('phpVtest')) {
  111. return true;
  112. }
  113. }
  114. private function testFileWritePermissions() {
  115. $e = Loader::helper('validation/error');
  116. if (!is_writable(DIR_CONFIG_SITE)) {
  117. $e->add(t('Your configuration directory config/ does not appear to be writable by the web server.'));
  118. }
  119. if (!is_writable(DIR_FILES_UPLOADED)) {
  120. $e->add(t('Your files directory files/ does not appear to be writable by the web server.'));
  121. }
  122. if (!is_writable(DIR_PACKAGES)) {
  123. $e->add(t('Your packages directory packages/ does not appear to be writable by the web server.'));
  124. }
  125. $this->fileWriteErrors = $e;
  126. if ($this->fileWriteErrors->has()) {
  127. return false;
  128. } else {
  129. return true;
  130. }
  131. }
  132. public function getDBErrorMsg() {
  133. return t('Function mysql_connect() not found. Your system does not appear to have MySQL available within PHP.');
  134. }
  135. public function test_url($num1, $num2) {
  136. $js = Loader::helper('json');
  137. $num = $num1 + $num2;
  138. print $js->encode(array('response' => $num));
  139. exit;
  140. }
  141. public function run_routine($pkgHandle, $routine) {
  142. $spl = Loader::startingPointPackage($pkgHandle);
  143. require(DIR_CONFIG_SITE . '/site_install.php');
  144. @include(DIR_CONFIG_SITE . '/site_install_user.php');
  145. $jsx = Loader::helper('json');
  146. $js = new stdClass;
  147. try {
  148. call_user_func(array($spl, $routine));
  149. $js->error = false;
  150. } catch(Exception $e) {
  151. $js->error = true;
  152. $js->message = $e->getMessage();
  153. $this->reset();
  154. }
  155. print $jsx->encode($js);
  156. exit;
  157. }
  158. protected function validateSampleContent($e) {
  159. $pkg = Loader::startingPointPackage($this->post('SAMPLE_CONTENT'));
  160. if (!is_object($pkg)) {
  161. $e->add(t("You must select a valid sample content starting point."));
  162. }
  163. return $e;
  164. }
  165. protected function validateDatabase($e) {
  166. if (!function_exists('mysql_connect')) {
  167. $e->add($this->getDBErrorMsg());
  168. } else {
  169. // attempt to connect to the database
  170. if (defined('DB_SERVER')) {
  171. $db = Loader::db($DB_SERVER, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE, true);
  172. $DB_SERVER = DB_SERVER;
  173. $DB_DATABASE = DB_DATABASE;
  174. } else {
  175. $db = Loader::db( $_POST['DB_SERVER'], $_POST['DB_USERNAME'], $_POST['DB_PASSWORD'], $_POST['DB_DATABASE'], true);
  176. $DB_SERVER = $_POST['DB_SERVER'];
  177. $DB_DATABASE = $_POST['DB_DATABASE'];
  178. }
  179. if ($DB_SERVER && $DB_DATABASE) {
  180. if (!$db) {
  181. $e->add(t('Unable to connect to database.'));
  182. } else {
  183. $num = $db->GetCol("show tables");
  184. if (count($num) > 0) {
  185. $e->add(t('There are already %s tables in this database. concrete5 must be installed in an empty database.', count($num)));
  186. }
  187. }
  188. }
  189. }
  190. return $e;
  191. }
  192. public function reset() {
  193. // remove site.php so that we can try again ?
  194. return;
  195. if (is_resource($this->fp)) {
  196. fclose($this->fp);
  197. }
  198. if (file_exists(DIR_CONFIG_SITE . '/site_install.php')) {
  199. unlink(DIR_CONFIG_SITE . '/site_install.php');
  200. }
  201. if (file_exists(DIR_CONFIG_SITE . '/site_install_user.php')) {
  202. unlink(DIR_CONFIG_SITE . '/site_install_user.php');
  203. }
  204. if (file_exists(DIR_CONFIG_SITE . '/site.php')) {
  205. unlink(DIR_CONFIG_SITE . '/site.php');
  206. }
  207. }
  208. public function configure() {
  209. try {
  210. $val = Loader::helper('validation/form');
  211. $val->setData($this->post());
  212. $val->addRequired("SITE", t("Please specify your site's name"));
  213. $val->addRequiredEmail("uEmail", t('Please specify a valid email address'));
  214. $val->addRequired("DB_DATABASE", t('You must specify a valid database name'));
  215. $val->addRequired("DB_SERVER", t('You must specify a valid database server'));
  216. $password = $_POST['uPassword'];
  217. $passwordConfirm = $_POST['uPasswordConfirm'];
  218. $e = Loader::helper('validation/error');
  219. $uh = Loader::helper('concrete/user');
  220. $uh->validNewPassword($password, $e);
  221. if ($password) {
  222. if ($password != $passwordConfirm) {
  223. $e->add(t('The two passwords provided do not match.'));
  224. }
  225. }
  226. if(is_object($this->fileWriteErrors)) {
  227. $e = $this->fileWriteErrors;
  228. }
  229. $e = $this->validateDatabase($e);
  230. $e = $this->validateSampleContent($e);
  231. if ($val->test() && (!$e->has())) {
  232. // write the config file
  233. $vh = Loader::helper('validation/identifier');
  234. $salt = ( defined('MANUAL_PASSWORD_SALT') ) ? MANUAL_PASSWORD_SALT : $vh->getString(64);
  235. $this->fp = @fopen(DIR_CONFIG_SITE . '/site_install.php', 'w+');
  236. $this->fpu = @fopen(DIR_CONFIG_SITE . '/site_install_user.php', 'w+');
  237. if ($this->fp) {
  238. $configuration = "<?php \n";
  239. $configuration .= "define('DB_SERVER', '" . addslashes($_POST['DB_SERVER']) . "');\n";
  240. $configuration .= "define('DB_USERNAME', '" . addslashes($_POST['DB_USERNAME']) . "');\n";
  241. $configuration .= "define('DB_PASSWORD', '" . addslashes($_POST['DB_PASSWORD']) . "');\n";
  242. $configuration .= "define('DB_DATABASE', '" . addslashes($_POST['DB_DATABASE']) . "');\n";
  243. if (isset($setPermissionsModel)) {
  244. $configuration .= "define('PERMISSIONS_MODEL', '" . addslashes($setPermissionsModel) . "');\n";
  245. }
  246. $configuration .= "define('PASSWORD_SALT', '{$salt}');\n";
  247. if (is_array($_POST['SITE_CONFIG'])) {
  248. foreach($_POST['SITE_CONFIG'] as $key => $value) {
  249. $configuration .= "define('" . $key . "', '" . $value . "');\n";
  250. }
  251. }
  252. $res = fwrite($this->fp, $configuration);
  253. fclose($this->fp);
  254. chmod(DIR_CONFIG_SITE . '/site_install.php', 0700);
  255. } else {
  256. throw new Exception(t('Unable to open config/site.php for writing.'));
  257. }
  258. if ($this->fpu) {
  259. $configuration = "<?php \n";
  260. $configuration .= "define('INSTALL_USER_EMAIL', '" . $_POST['uEmail'] . "');\n";
  261. $configuration .= "define('INSTALL_USER_PASSWORD_HASH', '" . User::encryptPassword($_POST['uPassword'], $salt) . "');\n";
  262. $configuration .= "define('INSTALL_STARTING_POINT', '" . $this->post('SAMPLE_CONTENT') . "');\n";
  263. $configuration .= "define('SITE', '" . addslashes($_POST['SITE']) . "');\n";
  264. if (defined('ACTIVE_LOCALE') && ACTIVE_LOCALE != '' && ACTIVE_LOCALE != 'en_US') {
  265. $configuration .= "define('ACTIVE_LOCALE', '" . ACTIVE_LOCALE . "');\n";
  266. }
  267. $res = fwrite($this->fpu, $configuration);
  268. fclose($this->fpu);
  269. chmod(DIR_CONFIG_SITE . '/site_install_user.php', 0700);
  270. if (PHP_SAPI != 'cli') {
  271. $this->redirect('/');
  272. }
  273. } else {
  274. throw new Exception(t('Unable to open config/site_user.php for writing.'));
  275. }
  276. } else {
  277. if ($e->has()) {
  278. $this->set('error', $e);
  279. } else {
  280. $this->set('error', $val->getError());
  281. }
  282. }
  283. } catch (Exception $e) {
  284. $this->reset();
  285. $this->set('error', $e);
  286. }
  287. }
  288. }