PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/install_update/controllers/InstallController.php

https://gitlab.com/lcp0578/simpleforum
PHP | 294 lines | 259 code | 19 blank | 16 comment | 23 complexity | 9b2b684d141a52eff1282f540691b2a3 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * @link http://www.simpleforum.org/
  4. * @copyright Copyright (c) 2015 Simple Forum
  5. * @author Jiandong Yu admin@simpleforum.org
  6. */
  7. namespace app\install_update\controllers;
  8. use Yii;
  9. use yii\filters\AccessControl;
  10. use yii\web\ServerErrorHttpException;
  11. use yii\web\ForbiddenHttpException;
  12. use app\lib\Util;
  13. use app\install_update\lib\RequirementChecker;
  14. use app\install_update\models\DatabaseForm;
  15. use app\install_update\models\AdminSignupForm;
  16. class InstallController extends \yii\web\Controller
  17. {
  18. public function behaviors()
  19. {
  20. return [
  21. 'access' => [
  22. 'class' => AccessControl::className(),
  23. 'rules' => [
  24. [
  25. 'allow' => true,
  26. 'matchCallback' => function ($rule, $action) {
  27. return !file_exists(Yii::getAlias('@app/runtime/install.lock'));
  28. },
  29. ],
  30. ],
  31. 'denyCallback' => function ($rule, $action) {
  32. throw new ForbiddenHttpException('你已安装过本程序。如确定要重装,请'."\n".'1.做好数据备份,'."\n".'2.删除runtime/install.lock文件,'."\n".'3.重新执行安装程序。');
  33. },
  34. ],
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function actions()
  41. {
  42. return [
  43. 'error' => [
  44. 'class' => 'yii\web\ErrorAction',
  45. ],
  46. ];
  47. }
  48. public function actionIndex()
  49. {
  50. if (version_compare(PHP_VERSION, '4.3', '<')) {
  51. echo 'php版本过低,请先安装php4.3以上版本';
  52. exist;
  53. }
  54. $requirementsChecker = new RequirementChecker();
  55. $gdMemo = $imagickMemo = '启用验证码时需要安装php的gd组件或imagick组件。';
  56. $gdOK = $imagickOK = false;
  57. if (extension_loaded('imagick')) {
  58. $imagick = new Imagick();
  59. $imagickFormats = $imagick->queryFormats('PNG');
  60. if (in_array('PNG', $imagickFormats)) {
  61. $imagickOK = true;
  62. } else {
  63. $imagickMemo = 'Imagick组件不支持png。';
  64. }
  65. }
  66. if (extension_loaded('gd')) {
  67. $gdInfo = gd_info();
  68. if (!empty($gdInfo['FreeType Support'])) {
  69. $gdOK = true;
  70. } else {
  71. $gdMemo = 'gd组件不支持FreeType。';
  72. }
  73. }
  74. /**
  75. * Adjust requirements according to your application specifics.
  76. */
  77. $requirements = array(
  78. array(
  79. 'name' => 'PHP版本',
  80. 'mandatory' => true,
  81. 'condition' => version_compare(PHP_VERSION, '5.4.0', '>='),
  82. 'by' => '<a href="http://www.yiiframework.com">Yii Framework</a>',
  83. 'memo' => 'PHP 5.4.0及以上',
  84. ),
  85. array(
  86. 'name' => 'config目录写权限',
  87. 'mandatory' => true,
  88. 'condition' => is_writeable(Yii::getAlias('@app/config')),
  89. 'by' => '<a href="http://simpleforum.org">Simple Forum</a>',
  90. 'memo' => 'config目录需要写权限',
  91. ),
  92. array(
  93. 'name' => 'runtime目录写权限',
  94. 'mandatory' => true,
  95. 'condition' => is_writeable(Yii::getAlias('@app/runtime')),
  96. 'by' => '<a href="http://simpleforum.org">Simple Forum</a>',
  97. 'memo' => 'runtime目录需要写权限',
  98. ),
  99. array(
  100. 'name' => 'web/assets目录写权限',
  101. 'mandatory' => true,
  102. 'condition' => is_writeable(Yii::getAlias('@webroot/assets')),
  103. 'by' => '<a href="http://simpleforum.org">Simple Forum</a>',
  104. 'memo' => 'web/assets目录需要写权限',
  105. ),
  106. array(
  107. 'name' => 'web/avatar目录写权限',
  108. 'mandatory' => true,
  109. 'condition' => is_writeable(Yii::getAlias('@webroot/avatar')),
  110. 'by' => '<a href="http://simpleforum.org">Simple Forum</a>',
  111. 'memo' => 'web/avatar目录需要写权限',
  112. ),
  113. // Database :
  114. array(
  115. 'name' => 'PDO扩展',
  116. 'mandatory' => true,
  117. 'condition' => extension_loaded('pdo'),
  118. 'by' => 'DB连接',
  119. 'memo' => 'MySQL连接。',
  120. ),
  121. array(
  122. 'name' => 'PDO_MySQL扩展',
  123. 'mandatory' => true,
  124. 'condition' => extension_loaded('pdo_mysql'),
  125. 'by' => 'DB连接',
  126. 'memo' => 'MySQL连接。',
  127. ),
  128. // openssl :
  129. array(
  130. 'name' => 'OpenSSL扩展',
  131. 'mandatory' => true,
  132. 'condition' => extension_loaded('openssl'),
  133. 'by' => '<a href="http://simpleforum.org">Simple Forum</a>',
  134. 'memo' => '用于用户密码加密',
  135. ),
  136. // Cache :
  137. array(
  138. 'name' => 'Memcache(d)扩展',
  139. 'mandatory' => false,
  140. 'condition' => extension_loaded('memcache') || extension_loaded('memcached'),
  141. 'by' => 'memcache/memcached缓存',
  142. 'memo' => '用于开启缓存',
  143. ),
  144. array(
  145. 'name' => 'APC扩展',
  146. 'mandatory' => false,
  147. 'condition' => extension_loaded('apc'),
  148. 'by' => 'APC缓存',
  149. 'memo' => '用于开启缓存',
  150. ),
  151. // CAPTCHA:
  152. array(
  153. 'name' => 'GD扩展(支持FreeType)',
  154. 'mandatory' => false,
  155. 'condition' => $gdOK,
  156. 'by' => '验证码',
  157. 'memo' => $gdMemo,
  158. ),
  159. array(
  160. 'name' => 'ImageMagick扩展(支持png)',
  161. 'mandatory' => false,
  162. 'condition' => $imagickOK,
  163. 'by' => '验证码',
  164. 'memo' => $imagickMemo,
  165. ),
  166. // PHP ini :
  167. 'phpExposePhp' => array(
  168. 'name' => 'php.ini的expose_php设值',
  169. 'mandatory' => false,
  170. 'condition' => $requirementsChecker->checkPhpIniOff("expose_php"),
  171. 'by' => '安全问题',
  172. 'memo' => '请修改为 expose_php = Off',
  173. ),
  174. 'phpAllowUrlInclude' => array(
  175. 'name' => 'php.ini的allow_url_include设值',
  176. 'mandatory' => false,
  177. 'condition' => $requirementsChecker->checkPhpIniOff("allow_url_include"),
  178. 'by' => '安全问题',
  179. 'memo' => '请修改为 allow_url_include = Off',
  180. ),
  181. 'phpSmtp' => array(
  182. 'name' => 'PHP SMTP邮件',
  183. 'mandatory' => false,
  184. 'condition' => strlen(ini_get('SMTP'))>0,
  185. 'by' => '邮件',
  186. 'memo' => '用于发送邮件',
  187. ),
  188. array(
  189. 'name' => 'MBString扩展',
  190. 'mandatory' => true,
  191. 'condition' => extension_loaded('mbstring'),
  192. 'by' => '<a href="http://www.php.net/manual/en/book.mbstring.php">Multibyte string</a> processing',
  193. 'memo' => ''
  194. ),
  195. array(
  196. 'name' => 'Reflection扩展',
  197. 'mandatory' => true,
  198. 'condition' => class_exists('Reflection', false),
  199. 'by' => '<a href="http://www.yiiframework.com">Yii Framework</a>',
  200. ),
  201. array(
  202. 'name' => 'PCRE扩展',
  203. 'mandatory' => true,
  204. 'condition' => extension_loaded('pcre'),
  205. 'by' => '<a href="http://www.yiiframework.com">Yii Framework</a>',
  206. ),
  207. array(
  208. 'name' => 'SPL扩展',
  209. 'mandatory' => true,
  210. 'condition' => extension_loaded('SPL'),
  211. 'by' => '<a href="http://www.yiiframework.com">Yii Framework</a>',
  212. ),
  213. );
  214. $requirementsChecker->check($requirements);
  215. return $this->render('index', ['check'=>$requirementsChecker]);
  216. }
  217. public function actionDbSetting()
  218. {
  219. $session = Yii::$app->getSession();
  220. if ( !$session->has('install-step') || $session->get('install-step') < 1 ) {
  221. return $this->redirect(['index']);
  222. }
  223. $model = new DatabaseForm();
  224. $error = false;
  225. if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
  226. try {
  227. $model->excuteSql($this->module->basePath . '/data/simpleforum.sql');
  228. $model->createDbConfig();
  229. $session->set('install-step', 2);
  230. return $this->redirect(['create-admin']);
  231. } catch (\yii\db\Exception $e) {
  232. $error = '数据库连接出错,请确认数据库连接信息:<br />' . $e->getMessage();
  233. }
  234. }
  235. return $this->render('dbSetting', ['model'=>$model, 'error'=>$error]);
  236. }
  237. public function actionCreateAdmin()
  238. {
  239. $session = Yii::$app->getSession();
  240. if ( !$session->has('install-step') || $session->get('install-step') < 1 ) {
  241. return $this->redirect(['index']);
  242. } else if ($session->get('install-step') == 1) {
  243. return $this->redirect(['db-setting']);
  244. } else if ($session->get('install-step') == 9) {
  245. return $this->render('completed');
  246. }
  247. $model = new AdminSignupForm();
  248. if ($model->load(Yii::$app->getRequest()->post())) {
  249. if ($user = $model->signup()) {
  250. if (Yii::$app->getUser()->login($user)) {
  251. $session->set('install-step', 9);
  252. $this->createInstallLockFile();
  253. return $this->render('completed');
  254. }
  255. }
  256. }
  257. return $this->render('signup', [
  258. 'model' => $model,
  259. ]);
  260. }
  261. private function createConfigFile($settings)
  262. {
  263. $config = '<?php'."\n";
  264. $config = $config. 'return ';
  265. $config = $config. \app\lib\Util::convertArrayToString($settings, '');
  266. $config = $config. ';'."\n";
  267. $fp = fopen(Yii::getAlias('@app/config/db.php'), "w");
  268. fwrite($fp, $config);
  269. fclose($fp);
  270. }
  271. private function createInstallLockFile()
  272. {
  273. return touch(Yii::getAlias('@app/runtime/install.lock'));
  274. }
  275. }