PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/shop/installation-2013-05-08/cli_install.php

https://bitbucket.org/jojoluzifer/gold-light-project
PHP | 333 lines | 249 code | 60 blank | 24 comment | 26 complexity | 2b68ccbf8a79cc37a9059a71a66aa449 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_host localhost \
  12. // --db_user root \
  13. // --db_password pass \
  14. // --db_name opencart \
  15. // --username admin \
  16. // --password admin \
  17. // --email youremail@example.com \
  18. // --agree_tnc yes \
  19. // --http_server http://localhost/opencart
  20. //
  21. ini_set('display_errors', 1);
  22. error_reporting(E_ALL);
  23. // DIR
  24. define('DIR_APPLICATION', str_replace('\'', '/', realpath(dirname(__FILE__))) . '/');
  25. define('DIR_SYSTEM', str_replace('\'', '/', realpath(dirname(__FILE__) . '/../')) . '/system/');
  26. define('DIR_OPENCART', str_replace('\'', '/', realpath(DIR_APPLICATION . '../')) . '/');
  27. define('DIR_DATABASE', DIR_SYSTEM . 'database/');
  28. define('DIR_LANGUAGE', DIR_APPLICATION . 'language/');
  29. define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
  30. define('DIR_CONFIG', DIR_SYSTEM . 'config/');
  31. // Startup
  32. require_once(DIR_SYSTEM . 'startup.php');
  33. // Registry
  34. $registry = new Registry();
  35. // Loader
  36. $loader = new Loader($registry);
  37. $registry->set('load', $loader);
  38. function handleError($errno, $errstr, $errfile, $errline, array $errcontext) {
  39. // error was suppressed with the @-operator
  40. if (0 === error_reporting()) {
  41. return false;
  42. }
  43. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  44. }
  45. set_error_handler('handleError');
  46. function usage() {
  47. echo "Usage:\n";
  48. echo "======\n";
  49. echo "\n";
  50. $options = implode(" ", array('--db_host', 'localhost',
  51. '--db_user', 'root',
  52. '--db_password', 'pass',
  53. '--db_name', 'opencart',
  54. '--username', 'admin',
  55. '--password', 'admin',
  56. '--email', 'youremail@example.com',
  57. '--agree_tnc', 'yes',
  58. '--http_server', 'http://localhost/opencart'));
  59. echo 'php cli_install.php install ' . $options . "\n\n";
  60. }
  61. function get_options($argv) {
  62. $defaults = array(
  63. 'db_host' => 'localhost',
  64. 'db_name' => 'opencart',
  65. 'db_prefix' => '',
  66. 'username' => 'admin',
  67. 'agree_tnc' => 'no',
  68. );
  69. $options = array();
  70. $total = count($argv);
  71. for ($i=0; $i < $total; $i=$i+2) {
  72. $is_flag = preg_match('/^--(.*)$/', $argv[$i], $match);
  73. if (!$is_flag) {
  74. throw new Exception($argv[$i] . ' found in command line args instead of a valid option name starting with \'--\'');
  75. }
  76. $options[$match[1]] = $argv[$i+1];
  77. }
  78. return array_merge($defaults, $options);
  79. }
  80. function valid($options) {
  81. $required = array(
  82. 'db_host',
  83. 'db_user',
  84. 'db_password',
  85. 'db_name',
  86. 'db_prefix',
  87. 'username',
  88. 'password',
  89. 'email',
  90. 'agree_tnc',
  91. 'http_server',
  92. );
  93. $missing = array();
  94. foreach ($required as $r) {
  95. if (!array_key_exists($r, $options)) {
  96. $missing[] = $r;
  97. }
  98. }
  99. if ($options['agree_tnc'] !== 'yes') {
  100. $missing[] = 'agree_tnc (should be yes)';
  101. }
  102. $valid = count($missing) === 0 && $options['agree_tnc'] === 'yes';
  103. return array($valid, $missing);
  104. }
  105. function install($options) {
  106. $check = check_requirements();
  107. if ($check[0]) {
  108. setup_mysql($options);
  109. write_config_files($options);
  110. dir_permissions();
  111. } else {
  112. echo 'FAILED! Pre-installation check failed: ' . $check[1] . "\n\n";
  113. exit(1);
  114. }
  115. }
  116. function check_requirements() {
  117. $error = null;
  118. if (phpversion() < '5.0') {
  119. $error = 'Warning: You need to use PHP5 or above for OpenCart to work!';
  120. }
  121. if (!ini_get('file_uploads')) {
  122. $error = 'Warning: file_uploads needs to be enabled!';
  123. }
  124. if (ini_get('session.auto_start')) {
  125. $error = 'Warning: OpenCart will not work with session.auto_start enabled!';
  126. }
  127. if (!extension_loaded('mysql')) {
  128. $error = 'Warning: MySQL extension needs to be loaded for OpenCart to work!';
  129. }
  130. if (!extension_loaded('gd')) {
  131. $error = 'Warning: GD extension needs to be loaded for OpenCart to work!';
  132. }
  133. if (!extension_loaded('curl')) {
  134. $error = 'Warning: CURL extension needs to be loaded for OpenCart to work!';
  135. }
  136. if (!function_exists('mcrypt_encrypt')) {
  137. $error = 'Warning: mCrypt extension needs to be loaded for OpenCart to work!';
  138. }
  139. if (!extension_loaded('zlib')) {
  140. $error = 'Warning: ZLIB extension needs to be loaded for OpenCart to work!';
  141. }
  142. if (!is_writable(DIR_OPENCART . 'config.php')) {
  143. $error = 'Warning: config.php needs to be writable for OpenCart to be installed!';
  144. }
  145. if (!is_writable(DIR_OPENCART . 'admin/config.php')) {
  146. $error = 'Warning: admin/config.php needs to be writable for OpenCart to be installed!';
  147. }
  148. if (!is_writable(DIR_SYSTEM . 'cache')) {
  149. $error = 'Warning: Cache directory needs to be writable for OpenCart to work!';
  150. }
  151. if (!is_writable(DIR_SYSTEM . 'logs')) {
  152. $error = 'Warning: Logs directory needs to be writable for OpenCart to work!';
  153. }
  154. if (!is_writable(DIR_OPENCART . 'image')) {
  155. $error = 'Warning: Image directory needs to be writable for OpenCart to work!';
  156. }
  157. if (!is_writable(DIR_OPENCART . 'image/cache')) {
  158. $error = 'Warning: Image cache directory needs to be writable for OpenCart to work!';
  159. }
  160. if (!is_writable(DIR_OPENCART . 'image/data')) {
  161. $error = 'Warning: Image data directory needs to be writable for OpenCart to work!';
  162. }
  163. if (!is_writable(DIR_OPENCART . 'download')) {
  164. $error = 'Warning: Download directory needs to be writable for OpenCart to work!';
  165. }
  166. return array($error === null, $error);
  167. }
  168. function setup_mysql($dbdata) {
  169. global $loader, $registry;
  170. $loader->model('install');
  171. $model = $registry->get('model_install');
  172. $model->mysql($dbdata);
  173. }
  174. function write_config_files($options) {
  175. $output = '<?php' . "\n";
  176. $output .= '// HTTP' . "\n";
  177. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  178. $output .= 'define(\'HTTP_IMAGE\', \'' . $options['http_server'] . 'image/\');' . "\n";
  179. $output .= 'define(\'HTTP_ADMIN\', \'' . $options['http_server'] . 'admin/\');' . "\n\n";
  180. $output .= '// HTTPS' . "\n";
  181. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . '\');' . "\n";
  182. $output .= 'define(\'HTTPS_IMAGE\', \'' . $options['http_server'] . 'image/\');' . "\n\n";
  183. $output .= '// DIR' . "\n";
  184. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'catalog/\');' . "\n";
  185. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART. 'system/\');' . "\n";
  186. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  187. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'catalog/language/\');' . "\n";
  188. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'catalog/view/theme/\');' . "\n";
  189. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  190. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  191. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/cache/\');' . "\n";
  192. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'download/\');' . "\n";
  193. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/logs/\');' . "\n\n";
  194. $output .= '// DB' . "\n";
  195. $output .= 'define(\'DB_DRIVER\', \'mysql\');' . "\n";
  196. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_host']) . '\');' . "\n";
  197. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_user']) . '\');' . "\n";
  198. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  199. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_name']) . '\');' . "\n";
  200. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  201. $output .= '?>';
  202. $file = fopen(DIR_OPENCART . 'config.php', 'w');
  203. fwrite($file, $output);
  204. fclose($file);
  205. $output = '<?php' . "\n";
  206. $output .= '// HTTP' . "\n";
  207. $output .= 'define(\'HTTP_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  208. $output .= 'define(\'HTTP_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  209. $output .= 'define(\'HTTP_IMAGE\', \'' . $options['http_server'] . 'image/\');' . "\n\n";
  210. $output .= '// HTTPS' . "\n";
  211. $output .= 'define(\'HTTPS_SERVER\', \'' . $options['http_server'] . 'admin/\');' . "\n";
  212. $output .= 'define(\'HTTPS_CATALOG\', \'' . $options['http_server'] . '\');' . "\n";
  213. $output .= 'define(\'HTTPS_IMAGE\', \'' . $options['http_server'] . 'image/\');' . "\n\n";
  214. $output .= '// DIR' . "\n";
  215. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'admin/\');' . "\n";
  216. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  217. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  218. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'admin/language/\');' . "\n";
  219. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'admin/view/template/\');' . "\n";
  220. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  221. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  222. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/cache/\');' . "\n";
  223. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'download/\');' . "\n";
  224. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/logs/\');' . "\n";
  225. $output .= 'define(\'DIR_CATALOG\', \'' . DIR_OPENCART . 'catalog/\');' . "\n\n";
  226. $output .= '// DB' . "\n";
  227. $output .= 'define(\'DB_DRIVER\', \'mysql\');' . "\n";
  228. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($options['db_host']) . '\');' . "\n";
  229. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($options['db_user']) . '\');' . "\n";
  230. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($options['db_password']) . '\');' . "\n";
  231. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($options['db_name']) . '\');' . "\n";
  232. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($options['db_prefix']) . '\');' . "\n";
  233. $output .= '?>';
  234. $file = fopen(DIR_OPENCART . 'admin/config.php', 'w');
  235. fwrite($file, $output);
  236. fclose($file);
  237. }
  238. function dir_permissions() {
  239. $dirs = array(
  240. DIR_OPENCART . 'image/',
  241. DIR_OPENCART . 'download/',
  242. DIR_SYSTEM . 'cache/',
  243. DIR_SYSTEM . 'logs/',
  244. );
  245. exec('chmod o+w -R ' . implode(' ', $dirs));
  246. }
  247. $argv = $_SERVER['argv'];
  248. $script = array_shift($argv);
  249. $subcommand = array_shift($argv);
  250. switch ($subcommand) {
  251. case "install":
  252. try {
  253. $options = get_options($argv);
  254. define('HTTP_OPENCART', $options['http_server']);
  255. $valid = valid($options);
  256. if (!$valid[0]) {
  257. echo "FAILED! Following inputs were missing or invalid: ";
  258. echo implode(', ', $valid[1]) . "\n\n";
  259. exit(1);
  260. }
  261. install($options);
  262. echo "SUCCESS! Opencart successfully installed on your server\n";
  263. echo "Store link: " . $options['http_server'] . "\n";
  264. echo "Admin link: " . $options['http_server'] . "admin/\n\n";
  265. } catch (ErrorException $e) {
  266. echo 'FAILED!: ' . $e->getMessage() . "\n";
  267. exit(1);
  268. }
  269. break;
  270. case "usage":
  271. default:
  272. echo usage();
  273. }