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