PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/install/cli_install.php

https://gitlab.com/shapcy/opencart
PHP | 362 lines | 278 code | 63 blank | 21 comment | 24 complexity | 70c48573c1624d6e573e1671855ca26c MD5 | raw file
  1. <?php
  2. //
  3. // Command line tool for installing opencart
  4. // Author: Vineet Naik <vineet.naik@kodeplay.com> <naikvin@gmail.com>
  5. //
  6. // (Currently tested on linux only)
  7. //
  8. // Usage:
  9. //
  10. // cd install
  11. // php cli_install.php install --db_hostname localhost \
  12. // --db_username root \
  13. // --db_password pass \
  14. // --db_database opencart \
  15. // --db_driver mysqli \
  16. // --db_port 3306 \
  17. // --username admin \
  18. // --password admin \
  19. // --email youremail@example.com \
  20. // --http_server http://localhost/opencart
  21. //
  22. ini_set('display_errors', 1);
  23. error_reporting(E_ALL);
  24. // DIR
  25. define('DIR_APPLICATION', str_replace('\\', '/', realpath(dirname(__FILE__))) . '/');
  26. define('DIR_SYSTEM', str_replace('\\', '/', realpath(dirname(__FILE__) . '/../')) . '/system/');
  27. define('DIR_OPENCART', str_replace('\\', '/', realpath(DIR_APPLICATION . '../')) . '/');
  28. define('DIR_DATABASE', DIR_SYSTEM . 'database/');
  29. define('DIR_LANGUAGE', DIR_APPLICATION . 'language/');
  30. define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
  31. define('DIR_CONFIG', DIR_SYSTEM . 'config/');
  32. define('DIR_MODIFICATION', DIR_SYSTEM . 'modification/');
  33. // Startup
  34. require_once(DIR_SYSTEM . 'startup.php');
  35. // Registry
  36. $registry = new Registry();
  37. // Loader
  38. $loader = new Loader($registry);
  39. $registry->set('load', $loader);
  40. function handleError($errno, $errstr, $errfile, $errline, array $errcontext) {
  41. // error was suppressed with the @-operator
  42. if (0 === error_reporting()) {
  43. return false;
  44. }
  45. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  46. }
  47. set_error_handler('handleError');
  48. function usage() {
  49. echo "Usage:\n";
  50. echo "======\n";
  51. echo "\n";
  52. $options = implode(" ", array(
  53. '--db_hostname', 'localhost',
  54. '--db_username', 'root',
  55. '--db_password', 'pass',
  56. '--db_database', 'opencart',
  57. '--db_driver', 'mysqli',
  58. '--db_port', '3306',
  59. '--username', 'admin',
  60. '--password', 'admin',
  61. '--email', 'youremail@example.com',
  62. '--http_server', 'http://localhost/opencart'
  63. ));
  64. echo 'php cli_install.php install ' . $options . "\n\n";
  65. }
  66. function get_options($argv) {
  67. $defaults = array(
  68. 'db_hostname' => 'localhost',
  69. 'db_database' => 'opencart',
  70. 'db_prefix' => 'oc_',
  71. 'db_driver' => 'mysqli',
  72. 'db_port' => '3306',
  73. 'username' => 'admin',
  74. );
  75. $options = array();
  76. $total = count($argv);
  77. for ($i=0; $i < $total; $i=$i+2) {
  78. $is_flag = preg_match('/^--(.*)$/', $argv[$i], $match);
  79. if (!$is_flag) {
  80. throw new Exception($argv[$i] . ' found in command line args instead of a valid option name starting with \'--\'');
  81. }
  82. $options[$match[1]] = $argv[$i+1];
  83. }
  84. return array_merge($defaults, $options);
  85. }
  86. function valid($options) {
  87. $required = array(
  88. 'db_hostname',
  89. 'db_username',
  90. 'db_password',
  91. 'db_database',
  92. 'db_prefix',
  93. 'db_port',
  94. 'username',
  95. 'password',
  96. 'email',
  97. 'http_server',
  98. );
  99. $missing = array();
  100. foreach ($required as $r) {
  101. if (!array_key_exists($r, $options)) {
  102. $missing[] = $r;
  103. }
  104. }
  105. if (!preg_match('#/$#', $options['http_server'])) {
  106. $options['http_server'] = $options['http_server'] . '/';
  107. }
  108. $valid = count($missing) === 0;
  109. return array($valid, $missing);
  110. }
  111. function install($options) {
  112. $check = check_requirements();
  113. if ($check[0]) {
  114. setup_db($options);
  115. write_config_files($options);
  116. dir_permissions();
  117. } else {
  118. echo 'FAILED! Pre-installation check failed: ' . $check[1] . "\n\n";
  119. exit(1);
  120. }
  121. }
  122. function check_requirements() {
  123. $error = null;
  124. if (phpversion() < '5.0') {
  125. $error = 'Warning: You need to use PHP5 or above for OpenCart to work!';
  126. }
  127. if (!ini_get('file_uploads')) {
  128. $error = 'Warning: file_uploads needs to be enabled!';
  129. }
  130. if (ini_get('session.auto_start')) {
  131. $error = 'Warning: OpenCart will not work with session.auto_start enabled!';
  132. }
  133. if (!extension_loaded('mysqli')) {
  134. $error = 'Warning: MySQLi extension needs to be loaded for OpenCart to work!';
  135. }
  136. if (!extension_loaded('gd')) {
  137. $error = 'Warning: GD extension needs to be loaded for OpenCart to work!';
  138. }
  139. if (!extension_loaded('curl')) {
  140. $error = 'Warning: CURL extension needs to be loaded for OpenCart to work!';
  141. }
  142. if (!function_exists('mcrypt_encrypt')) {
  143. $error = 'Warning: mCrypt extension needs to be loaded for OpenCart to work!';
  144. }
  145. if (!extension_loaded('zlib')) {
  146. $error = 'Warning: ZLIB extension needs to be loaded for OpenCart to work!';
  147. }
  148. return array($error === null, $error);
  149. }
  150. function setup_db($data) {
  151. $db = new DB($data['db_driver'], $data['db_hostname'], $data['db_username'], $data['db_password'], $data['db_database'], $data['db_port']);
  152. $file = DIR_APPLICATION . 'opencart.sql';
  153. if (!file_exists($file)) {
  154. exit('Could not load sql file: ' . $file);
  155. }
  156. $lines = file($file);
  157. if ($lines) {
  158. $sql = '';
  159. foreach ($lines as $line) {
  160. if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
  161. $sql .= $line;
  162. if (preg_match('/;\s*$/', $line)) {
  163. $sql = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . $data['db_prefix'], $sql);
  164. $sql = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . $data['db_prefix'], $sql);
  165. $sql = str_replace("INSERT INTO `oc_", "INSERT INTO `" . $data['db_prefix'], $sql);
  166. $db->query($sql);
  167. $sql = '';
  168. }
  169. }
  170. }
  171. $db->query("SET CHARACTER SET utf8");
  172. $db->query("SET @@session.sql_mode = 'MYSQL40'");
  173. $db->query("DELETE FROM `" . $data['db_prefix'] . "user` WHERE user_id = '1'");
  174. $db->query("INSERT INTO `" . $data['db_prefix'] . "user` SET user_id = '1', user_group_id = '1', username = '" . $db->escape($data['username']) . "', salt = '" . $db->escape($salt = token(9)) . "', password = '" . $db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', firstname = 'John', lastname = 'Doe', email = '" . $db->escape($data['email']) . "', status = '1', date_added = NOW()");
  175. $db->query("DELETE FROM `" . $data['db_prefix'] . "setting` WHERE `key` = 'config_email'");
  176. $db->query("INSERT INTO `" . $data['db_prefix'] . "setting` SET `code` = 'config', `key` = 'config_email', value = '" . $db->escape($data['email']) . "'");
  177. $db->query("DELETE FROM `" . $data['db_prefix'] . "setting` WHERE `key` = 'config_url'");
  178. $db->query("INSERT INTO `" . $data['db_prefix'] . "setting` SET `code` = 'config', `key` = 'config_url', value = '" . $db->escape(HTTP_OPENCART) . "'");
  179. $db->query("DELETE FROM `" . $data['db_prefix'] . "setting` WHERE `key` = 'config_encryption'");
  180. $db->query("INSERT INTO `" . $data['db_prefix'] . "setting` SET `code` = 'config', `key` = 'config_encryption', value = '" . $db->escape(token(1024)) . "'");
  181. $db->query("UPDATE `" . $data['db_prefix'] . "product` SET `viewed` = '0'");
  182. $db->query("INSERT INTO `" . $data['db_prefix'] . "api` SET name = 'Default', `key` = '" . $db->escape(token(256)) . "', status = 1, date_added = NOW(), date_modified = NOW()");
  183. $api_id = $db->getLastId();
  184. $db->query("DELETE FROM `" . $data['db_prefix'] . "setting` WHERE `key` = 'config_api_id'");
  185. $db->query("INSERT INTO `" . $data['db_prefix'] . "setting` SET `code` = 'config', `key` = 'config_api_id', value = '" . (int)$api_id . "'");
  186. }
  187. }
  188. function write_config_files($options) {
  189. $output = '<?php' . "\n";
  190. $output .= '// HTTP' . "\n";
  191. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  192. $output .= 'define(\'HTTP_ADMIN\', \'' . $options['http_server'] . 'admin/\');' . "\n\n";
  193. $output .= '// HTTPS' . "\n";
  194. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  195. $output .= '// DIR' . "\n";
  196. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'catalog/\');' . "\n";
  197. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  198. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  199. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'catalog/language/\');' . "\n";
  200. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'catalog/view/theme/\');' . "\n";
  201. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  202. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  203. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/storage/cache/\');' . "\n";
  204. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'system/storage/download/\');' . "\n";
  205. $output .= 'define(\'DIR_UPLOAD\', \'' . DIR_OPENCART . 'system/storage/upload/\');' . "\n";
  206. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  207. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n\n";
  208. $output .= '// DB' . "\n";
  209. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($options['db_driver']) . '\');' . "\n";
  210. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_hostname']) . '\');' . "\n";
  211. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_username']) . '\');' . "\n";
  212. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  213. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_database']) . '\');' . "\n";
  214. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  215. $output .= 'define(\'DB_PORT\', \'' . addslashes($options['db_port']) . '\');' . "\n";
  216. $output .= '?>';
  217. $file = fopen(DIR_OPENCART . 'config.php', 'w');
  218. fwrite($file, $output);
  219. fclose($file);
  220. $output = '<?php' . "\n";
  221. $output .= '// HTTP' . "\n";
  222. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  223. $output .= 'define(\'HTTP_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  224. $output .= '// HTTPS' . "\n";
  225. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  226. $output .= 'define(\'HTTPS_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  227. $output .= '// DIR' . "\n";
  228. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'admin/\');' . "\n";
  229. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  230. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  231. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'admin/language/\');' . "\n";
  232. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'admin/view/template/\');' . "\n";
  233. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  234. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  235. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/storage/cache/\');' . "\n";
  236. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'system/storage/download/\');' . "\n";
  237. $output .= 'define(\'DIR_UPLOAD\', \'' . DIR_OPENCART . 'system/storage/upload/\');' . "\n";
  238. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n";
  239. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  240. $output .= 'define(\'DIR_CATALOG\', \'' . DIR_OPENCART . 'catalog/\');' . "\n\n";
  241. $output .= '// DB' . "\n";
  242. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($options['db_driver']) . '\');' . "\n";
  243. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_hostname']) . '\');' . "\n";
  244. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_username']) . '\');' . "\n";
  245. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  246. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_database']) . '\');' . "\n";
  247. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  248. $output .= 'define(\'DB_PORT\', \'' . addslashes($options['db_port']) . '\');' . "\n";
  249. $output .= '?>';
  250. $file = fopen(DIR_OPENCART . 'admin/config.php', 'w');
  251. fwrite($file, $output);
  252. fclose($file);
  253. }
  254. function dir_permissions() {
  255. $dirs = array(
  256. DIR_OPENCART . 'image/',
  257. DIR_OPENCART . 'system/storage/download/',
  258. DIR_OPENCART . 'system/storage/upload/',
  259. DIR_OPENCART . 'system/storage/cache/',
  260. DIR_OPENCART . 'system/storage/logs/',
  261. DIR_OPENCART . 'system/storage/modification/',
  262. );
  263. exec('chmod o+w -R ' . implode(' ', $dirs));
  264. }
  265. $argv = $_SERVER['argv'];
  266. $script = array_shift($argv);
  267. $subcommand = array_shift($argv);
  268. switch ($subcommand) {
  269. case "install":
  270. try {
  271. $options = get_options($argv);
  272. define('HTTP_OPENCART', $options['http_server']);
  273. $valid = valid($options);
  274. if (!$valid[0]) {
  275. echo "FAILED! Following inputs were missing or invalid: ";
  276. echo implode(', ', $valid[1]) . "\n\n";
  277. exit(1);
  278. }
  279. install($options);
  280. echo "SUCCESS! Opencart successfully installed on your server\n";
  281. echo "Store link: " . $options['http_server'] . "\n";
  282. echo "Admin link: " . $options['http_server'] . "admin/\n\n";
  283. } catch (ErrorException $e) {
  284. echo 'FAILED!: ' . $e->getMessage() . "\n";
  285. exit(1);
  286. }
  287. break;
  288. case "usage":
  289. default:
  290. echo usage();
  291. }