PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/install/module/install_database/task/add_config_settings.php

https://github.com/bantu/phpbb
PHP | 372 lines | 224 code | 70 blank | 78 comment | 8 complexity | c6489f5a82f44a3647955ddfd2d9ad6e MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\install\module\install_database\task;
  14. use phpbb\install\exception\resource_limit_reached_exception;
  15. /**
  16. * Create database schema
  17. */
  18. class add_config_settings extends \phpbb\install\task_base
  19. {
  20. /**
  21. * @var \phpbb\db\driver\driver_interface
  22. */
  23. protected $db;
  24. /**
  25. * @var \phpbb\filesystem\filesystem_interface
  26. */
  27. protected $filesystem;
  28. /**
  29. * @var \phpbb\install\helper\config
  30. */
  31. protected $install_config;
  32. /**
  33. * @var \phpbb\install\helper\iohandler\iohandler_interface
  34. */
  35. protected $iohandler;
  36. /**
  37. * @var \phpbb\language\language
  38. */
  39. protected $language;
  40. /**
  41. * @var \phpbb\passwords\manager
  42. */
  43. protected $password_manager;
  44. /**
  45. * @var string
  46. */
  47. protected $phpbb_root_path;
  48. /**
  49. * @var string
  50. */
  51. protected $config_table;
  52. /**
  53. * @var string
  54. */
  55. protected $user_table;
  56. /**
  57. * @var string
  58. */
  59. protected $topics_table;
  60. /**
  61. * @var string
  62. */
  63. protected $forums_table;
  64. /**
  65. * @var string
  66. */
  67. protected $posts_table;
  68. /**
  69. * @var string
  70. */
  71. protected $moderator_cache_table;
  72. /**
  73. * Constructor
  74. *
  75. * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem service
  76. * @param \phpbb\install\helper\config $install_config Installer's config helper
  77. * @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler Installer's input-output handler
  78. * @param \phpbb\install\helper\container_factory $container Installer's DI container
  79. * @param \phpbb\language\language $language Language service
  80. * @param string $phpbb_root_path Path to phpBB's root
  81. */
  82. public function __construct(\phpbb\filesystem\filesystem_interface $filesystem,
  83. \phpbb\install\helper\config $install_config,
  84. \phpbb\install\helper\iohandler\iohandler_interface $iohandler,
  85. \phpbb\install\helper\container_factory $container,
  86. \phpbb\language\language $language,
  87. $phpbb_root_path)
  88. {
  89. $this->db = $container->get('dbal.conn');
  90. $this->filesystem = $filesystem;
  91. $this->install_config = $install_config;
  92. $this->iohandler = $iohandler;
  93. $this->language = $language;
  94. $this->password_manager = $container->get('passwords.manager');
  95. $this->phpbb_root_path = $phpbb_root_path;
  96. // Table names
  97. $this->config_table = $container->get_parameter('tables.config');
  98. $this->forums_table = $container->get_parameter('tables.forums');
  99. $this->topics_table = $container->get_parameter('tables.topics');
  100. $this->user_table = $container->get_parameter('tables.users');
  101. $this->moderator_cache_table = $container->get_parameter('tables.moderator_cache');
  102. $this->posts_table = $container->get_parameter('tables.posts');
  103. parent::__construct(true);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function run()
  109. {
  110. $this->db->sql_return_on_error(true);
  111. $server_name = $this->install_config->get('server_name');
  112. $current_time = time();
  113. $user_ip = phpbb_ip_normalise($this->iohandler->get_server_variable('REMOTE_ADDR'));
  114. $user_ip = ($user_ip === false) ? '' : $user_ip;
  115. $referer = $this->iohandler->get_server_variable('REFERER');
  116. // Calculate cookie domain
  117. $cookie_domain = $server_name;
  118. if (strpos($cookie_domain, 'www.') === 0)
  119. {
  120. $cookie_domain = substr($cookie_domain, 3);
  121. }
  122. // Set default config and post data, this applies to all DB's
  123. $sql_ary = array(
  124. 'INSERT INTO ' . $this->config_table . " (config_name, config_value)
  125. VALUES ('board_startdate', '$current_time')",
  126. 'INSERT INTO ' . $this->config_table . " (config_name, config_value)
  127. VALUES ('default_lang', '" . $this->db->sql_escape($this->install_config->get('default_lang')) . "')",
  128. 'UPDATE ' . $this->config_table . "
  129. SET config_value = '" . $this->db->sql_escape($this->install_config->get('img_imagick')) . "'
  130. WHERE config_name = 'img_imagick'",
  131. 'UPDATE ' . $this->config_table . "
  132. SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_name')) . "'
  133. WHERE config_name = 'server_name'",
  134. 'UPDATE ' . $this->config_table . "
  135. SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_port')) . "'
  136. WHERE config_name = 'server_port'",
  137. 'UPDATE ' . $this->config_table . "
  138. SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_email')) . "'
  139. WHERE config_name = 'board_email'",
  140. 'UPDATE ' . $this->config_table . "
  141. SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_email')) . "'
  142. WHERE config_name = 'board_contact'",
  143. 'UPDATE ' . $this->config_table . "
  144. SET config_value = '" . $this->db->sql_escape($cookie_domain) . "'
  145. WHERE config_name = 'cookie_domain'",
  146. 'UPDATE ' . $this->config_table . "
  147. SET config_value = '" . $this->db->sql_escape($this->language->lang('default_dateformat')) . "'
  148. WHERE config_name = 'default_dateformat'",
  149. 'UPDATE ' . $this->config_table . "
  150. SET config_value = '" . $this->db->sql_escape($this->install_config->get('email_enable')) . "'
  151. WHERE config_name = 'email_enable'",
  152. 'UPDATE ' . $this->config_table . "
  153. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_delivery')) . "'
  154. WHERE config_name = 'smtp_delivery'",
  155. 'UPDATE ' . $this->config_table . "
  156. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_host')) . "'
  157. WHERE config_name = 'smtp_host'",
  158. 'UPDATE ' . $this->config_table . "
  159. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_port')) . "'
  160. WHERE config_name = 'smtp_port'",
  161. 'UPDATE ' . $this->config_table . "
  162. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_auth')) . "'
  163. WHERE config_name = 'smtp_auth_method'",
  164. 'UPDATE ' . $this->config_table . "
  165. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_user')) . "'
  166. WHERE config_name = 'smtp_username'",
  167. 'UPDATE ' . $this->config_table . "
  168. SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_pass')) . "'
  169. WHERE config_name = 'smtp_password'",
  170. 'UPDATE ' . $this->config_table . "
  171. SET config_value = '" . $this->db->sql_escape($this->install_config->get('cookie_secure')) . "'
  172. WHERE config_name = 'cookie_secure'",
  173. 'UPDATE ' . $this->config_table . "
  174. SET config_value = '" . $this->db->sql_escape($this->install_config->get('force_server_vars')) . "'
  175. WHERE config_name = 'force_server_vars'",
  176. 'UPDATE ' . $this->config_table . "
  177. SET config_value = '" . $this->db->sql_escape($this->install_config->get('script_path')) . "'
  178. WHERE config_name = 'script_path'",
  179. 'UPDATE ' . $this->config_table . "
  180. SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_protocol')) . "'
  181. WHERE config_name = 'server_protocol'",
  182. 'UPDATE ' . $this->config_table . "
  183. SET config_value = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
  184. WHERE config_name = 'newest_username'",
  185. 'UPDATE ' . $this->config_table . "
  186. SET config_value = '" . md5(mt_rand()) . "'
  187. WHERE config_name = 'avatar_salt'",
  188. 'UPDATE ' . $this->config_table . "
  189. SET config_value = '" . md5(mt_rand()) . "'
  190. WHERE config_name = 'plupload_salt'",
  191. 'UPDATE ' . $this->config_table . "
  192. SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_name')) . "'
  193. WHERE config_name = 'sitename'",
  194. 'UPDATE ' . $this->config_table . "
  195. SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_description')) . "'
  196. WHERE config_name = 'site_desc'",
  197. 'UPDATE ' . $this->user_table . "
  198. SET username = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "',
  199. user_password='" . $this->password_manager->hash($this->install_config->get('admin_passwd')) . "',
  200. user_ip = '" . $this->db->sql_escape($user_ip) . "',
  201. user_lang = '" . $this->db->sql_escape($this->install_config->get('user_language', 'en')) . "',
  202. user_email='" . $this->db->sql_escape($this->install_config->get('board_email')) . "',
  203. user_dateformat='" . $this->db->sql_escape($this->language->lang('default_dateformat')) . "',
  204. user_email_hash = " . $this->db->sql_escape(phpbb_email_hash($this->install_config->get('board_email'))) . ",
  205. username_clean = '" . $this->db->sql_escape(utf8_clean_string($this->install_config->get('admin_name'))) . "'
  206. WHERE username = 'Admin'",
  207. 'UPDATE ' . $this->moderator_cache_table . "
  208. SET username = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
  209. WHERE username = 'Admin'",
  210. 'UPDATE ' . $this->forums_table . "
  211. SET forum_last_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
  212. WHERE forum_last_poster_name = 'Admin'",
  213. 'UPDATE ' . $this->topics_table . "
  214. SET topic_first_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "',
  215. topic_last_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
  216. WHERE topic_first_poster_name = 'Admin'
  217. OR topic_last_poster_name = 'Admin'",
  218. 'UPDATE ' . $this->user_table . "
  219. SET user_regdate = $current_time",
  220. 'UPDATE ' . $this->posts_table . "
  221. SET post_time = $current_time, poster_ip = '" . $this->db->sql_escape($user_ip) . "'",
  222. 'UPDATE ' . $this->topics_table . "
  223. SET topic_time = $current_time, topic_last_post_time = $current_time",
  224. 'UPDATE ' . $this->forums_table . "
  225. SET forum_last_post_time = $current_time",
  226. 'UPDATE ' . $this->config_table . "
  227. SET config_value = '" . $this->db->sql_escape($this->db->sql_server_info(true)) . "'
  228. WHERE config_name = 'dbms_version'",
  229. );
  230. if (@extension_loaded('gd'))
  231. {
  232. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  233. SET config_value = 'core.captcha.plugins.gd'
  234. WHERE config_name = 'captcha_plugin'";
  235. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  236. SET config_value = '1'
  237. WHERE config_name = 'captcha_gd'";
  238. }
  239. $ref = substr($referer, strpos($referer, '://') + 3);
  240. if (!(stripos($ref, $server_name) === 0))
  241. {
  242. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  243. SET config_value = '0'
  244. WHERE config_name = 'referer_validation'";
  245. }
  246. // We set a (semi-)unique cookie name to bypass login issues related to the cookie name.
  247. $cookie_name = 'phpbb3_';
  248. $rand_str = md5(mt_rand());
  249. $rand_str = str_replace('0', 'z', base_convert($rand_str, 16, 35));
  250. $rand_str = substr($rand_str, 0, 5);
  251. $cookie_name .= strtolower($rand_str);
  252. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  253. SET config_value = '" . $this->db->sql_escape($cookie_name) . "'
  254. WHERE config_name = 'cookie_name'";
  255. // Disable avatars if upload directory is not writable
  256. if (!$this->filesystem->is_writable($this->phpbb_root_path . 'images/avatars/upload/'))
  257. {
  258. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  259. SET config_value = '0'
  260. WHERE config_name = 'allow_avatar'";
  261. $sql_ary[] = 'UPDATE ' . $this->config_table . "
  262. SET config_value = '0'
  263. WHERE config_name = 'allow_avatar_upload'";
  264. }
  265. $i = $this->install_config->get('add_config_settings_index', 0);
  266. $total = sizeof($sql_ary);
  267. $sql_ary = array_slice($sql_ary, $i);
  268. foreach ($sql_ary as $sql)
  269. {
  270. if (!$this->db->sql_query($sql))
  271. {
  272. $error = $this->db->sql_error($this->db->get_sql_error_sql());
  273. $this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
  274. }
  275. $i++;
  276. // Stop execution if resource limit is reached
  277. if ($this->install_config->get_time_remaining() <= 0 || $this->install_config->get_memory_remaining() <= 0)
  278. {
  279. break;
  280. }
  281. }
  282. if ($i < $total)
  283. {
  284. $this->install_config->set('add_config_settings_index', $i);
  285. throw new resource_limit_reached_exception();
  286. }
  287. }
  288. /**
  289. * {@inheritdoc}
  290. */
  291. static public function get_step_count()
  292. {
  293. return 1;
  294. }
  295. /**
  296. * {@inheritdoc}
  297. */
  298. public function get_task_lang_name()
  299. {
  300. return 'TASK_ADD_CONFIG_SETTINGS';
  301. }
  302. }