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

/upload/install/cli_install.php

https://gitlab.com/unityfamily359/opencart
PHP | 311 lines | 246 code | 44 blank | 21 comment | 16 complexity | 0e7a596c273391b3f0d470fe765ed6b3 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($dbdata) {
  151. global $loader, $registry;
  152. $loader->model('install');
  153. $model = $registry->get('model_install');
  154. $model->database($dbdata);
  155. }
  156. function write_config_files($options) {
  157. $output = '<?php' . "\n";
  158. $output .= '// HTTP' . "\n";
  159. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  160. $output .= 'define(\'HTTP_ADMIN\', \'' . $options['http_server'] . 'admin/\');' . "\n\n";
  161. $output .= '// HTTPS' . "\n";
  162. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  163. $output .= '// DIR' . "\n";
  164. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'catalog/\');' . "\n";
  165. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  166. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  167. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'catalog/language/\');' . "\n";
  168. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'catalog/view/theme/\');' . "\n";
  169. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  170. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  171. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/storage/cache/\');' . "\n";
  172. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'system/storage/download/\');' . "\n";
  173. $output .= 'define(\'DIR_UPLOAD\', \'' . DIR_OPENCART . 'system/storage/upload/\');' . "\n";
  174. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  175. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n\n";
  176. $output .= '// DB' . "\n";
  177. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($options['db_driver']) . '\');' . "\n";
  178. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_hostname']) . '\');' . "\n";
  179. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_username']) . '\');' . "\n";
  180. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  181. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_database']) . '\');' . "\n";
  182. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  183. $output .= 'define(\'DB_PORT\', \'' . addslashes($options['db_port']) . '\');' . "\n";
  184. $output .= '?>';
  185. $file = fopen(DIR_OPENCART . 'config.php', 'w');
  186. fwrite($file, $output);
  187. fclose($file);
  188. $output = '<?php' . "\n";
  189. $output .= '// HTTP' . "\n";
  190. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  191. $output .= 'define(\'HTTP_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  192. $output .= '// HTTPS' . "\n";
  193. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  194. $output .= 'define(\'HTTPS_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  195. $output .= '// DIR' . "\n";
  196. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'admin/\');' . "\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 . 'admin/language/\');' . "\n";
  200. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'admin/view/template/\');' . "\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_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n";
  207. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  208. $output .= 'define(\'DIR_CATALOG\', \'' . DIR_OPENCART . 'catalog/\');' . "\n\n";
  209. $output .= '// DB' . "\n";
  210. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($options['db_driver']) . '\');' . "\n";
  211. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_hostname']) . '\');' . "\n";
  212. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_username']) . '\');' . "\n";
  213. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  214. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_database']) . '\');' . "\n";
  215. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  216. $output .= 'define(\'DB_PORT\', \'' . addslashes($options['db_port']) . '\');' . "\n";
  217. $output .= '?>';
  218. $file = fopen(DIR_OPENCART . 'admin/config.php', 'w');
  219. fwrite($file, $output);
  220. fclose($file);
  221. }
  222. function dir_permissions() {
  223. $dirs = array(
  224. DIR_OPENCART . 'image/',
  225. DIR_OPENCART . 'system/storage/download/',
  226. DIR_OPENCART . 'system/storage/upload/',
  227. DIR_OPENCART . 'system/storage/cache/',
  228. DIR_OPENCART . 'system/storage/logs/',
  229. DIR_OPENCART . 'system/storage/modification/',
  230. );
  231. exec('chmod o+w -R ' . implode(' ', $dirs));
  232. }
  233. $argv = $_SERVER['argv'];
  234. $script = array_shift($argv);
  235. $subcommand = array_shift($argv);
  236. switch ($subcommand) {
  237. case "install":
  238. try {
  239. $options = get_options($argv);
  240. define('HTTP_OPENCART', $options['http_server']);
  241. $valid = valid($options);
  242. if (!$valid[0]) {
  243. echo "FAILED! Following inputs were missing or invalid: ";
  244. echo implode(', ', $valid[1]) . "\n\n";
  245. exit(1);
  246. }
  247. install($options);
  248. echo "SUCCESS! Opencart successfully installed on your server\n";
  249. echo "Store link: " . $options['http_server'] . "\n";
  250. echo "Admin link: " . $options['http_server'] . "admin/\n\n";
  251. } catch (ErrorException $e) {
  252. echo 'FAILED!: ' . $e->getMessage() . "\n";
  253. exit(1);
  254. }
  255. break;
  256. case "usage":
  257. default:
  258. echo usage();
  259. }