PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/aoliz/install/install.core.php

http://phpfor.googlecode.com/
PHP | 609 lines | 532 code | 70 blank | 7 comment | 83 complexity | c84d4003864264983c340fd522e57e87 MD5 | raw file
  1. <?php
  2. define('IN_INSTALLER',true);
  3. require(CORE_DIR.'/kernel.php');
  4. require(CORE_DIR.'/func_ext.php');
  5. class installCore extends kernel{
  6. function __construct(){
  7. define('CORE_INCLUDE_DIR',CORE_DIR.
  8. ((file_exists(CORE_DIR.'/include_v5') && version_compare(PHP_VERSION,'5.0','>='))?'/include_v5':'/include'));
  9. if(isset( $_POST['dbname'] , $_POST['dbuser'] , $_POST['dbhost'] , $_POST['data'])){
  10. error_reporting(E_ALL ^ E_NOTICE);
  11. $this->install_mode='service';
  12. if(file_exists('../config/install.lock')){
  13. $this->service_install_res('false','Access denied by install.lock');
  14. }
  15. $_POST['uname']=$_POST['dbuser'];
  16. $_POST['pwd']=$_POST['dbpass'];
  17. $_POST['prefix']=$_POST['prefix']?$_POST['prefix']:'sdb_';
  18. $_POST['use_demo']=($_POST['data']=='true')?true:false;
  19. $_POST['stimezone']=$_POST['stimezone']?$_POST['stimezone']:8;
  20. parent::kernel();
  21. $this->service_install_mode();
  22. }
  23. if(file_exists('../config/install.lock') && (!isset($_GET['step']) || $_GET['step']!='complete')){
  24. $this->responseCode('403');
  25. echo 'Access denied by install.lock';
  26. exit();
  27. }else{
  28. parent::kernel();
  29. $this->model_dir = 'model';
  30. $this->checkPerm();
  31. $this->run();
  32. }
  33. }
  34. function service_install_res($res='false',$rsp=null){
  35. if($this->install_mode=='service'){
  36. $return['res']=$res;
  37. $return['rsp']=$rsp;
  38. //$this->_header();
  39. echo json_encode($return);
  40. exit();
  41. }else{
  42. echo $rsp;
  43. }
  44. }
  45. function service_install_mode(){
  46. if(!($link = @mysql_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass']))){
  47. $this->service_install_res('false','mysql connect error');
  48. }
  49. $this->step_setdb();
  50. if($this->install_mode=='service'){
  51. $this->loadConfig();
  52. }
  53. $this->do_install(false);
  54. $this->service_install_res('true','install succ');
  55. exit();
  56. }
  57. function run(){
  58. $_GET['step'] = isset($_GET['step'])?$_GET['step']:1;
  59. $method = 'step_'.$_GET['step'];
  60. if(method_exists($this,$method)){
  61. call_user_func(array(&$this,$method));
  62. }else{
  63. header("HTTP/1.0 404 Not Found",true,404);
  64. echo '<h1>404 Not Found</h1><hr />';
  65. }
  66. }
  67. function loadConfig(){
  68. ob_start();
  69. if(include('../config/config.php')){
  70. $this->model_dir = ((!defined('SHOP_DEVELOPER') || !constant('SHOP_DEVELOPER')) && version_compare(PHP_VERSION,'5.0','>=') && file_exists(CORE_DIR.'/model_v5')?'model_v5':'model');
  71. if(defined('DB_NAME') && defined('DB_USER') && defined('DB_PASSWORD') && defined('DB_HOST')){
  72. ob_end_clean();
  73. return true;
  74. }
  75. }
  76. ob_end_clean();
  77. return false;
  78. }
  79. function installCore(){
  80. $this->__construct();
  81. }
  82. function step_checkdb(){
  83. if(!($link = mysql_connect($_POST['dbhost'], $_POST['uname'], $_POST['pwd']))){
  84. header("Cache-Control: no-cache,no-store, must-revalidate",true,503);
  85. exit();
  86. }
  87. $database = array();
  88. if($result = mysql_query('show databases')){
  89. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  90. if($row['Database']!='mysql' && $row['Database']!='information_schema')
  91. $database[] = $row['Database'];
  92. }
  93. }
  94. mysql_free_result($result);
  95. mysql_close($link);
  96. if(count($database)>0){
  97. $html = '<select id="db_name" name="dbname">';
  98. foreach($database as $db){
  99. $html .= '<option value="'.htmlspecialchars($db).'">'.$db.'</option>';
  100. }
  101. echo $html .= '</select>';
  102. }
  103. }
  104. function step_1(){
  105. $this->output('step_1.html');
  106. }
  107. function step_2(){
  108. if(is_file(BASE_DIR.'/config/config.php')){
  109. require_once(BASE_DIR.'/config/config.php');
  110. $this->pagedata['database_username'] = DB_USER;
  111. $this->pagedata['database_password'] = DB_PASSWORD;
  112. $this->pagedata['database_name'] = DB_NAME;
  113. $this->pagedata['host'] = DB_HOST;
  114. $this->pagedata['database_prefix'] = DB_PREFIX;
  115. }
  116. $this->pagedata['timezone'] = timezone_list();
  117. $this->pagedata['default_timezone'] = 8;
  118. $this->output('step_2.html');
  119. }
  120. function step_setdb(){
  121. define('DB_NAME', $_POST['dbname']); // The name of the database
  122. define('DB_USER', $_POST['uname']); // Your MySQL username
  123. define('DB_PASSWORD', $_POST['pwd']); // ...and password
  124. define('DB_HOST', $_POST['dbhost']);
  125. define('DB_PREFIX', $_POST['prefix']);
  126. define('SERVER_TIMEZONE', $_POST['stimezone']);
  127. if(php_sapi_name()=='isapi'){
  128. $constant['WITHOUT_FLOCK'] = true;
  129. }
  130. if(!defined('STORE_KEY') || strlen(trim(STORE_KEY))==0){
  131. $constant['STORE_KEY'] = md5(implode(',',microtime()).implode(',',$_SERVER));
  132. }
  133. $this->db = &$this->database();
  134. if(!$this->db->_rw_conn(true)){
  135. if($this->install_mode=='service'){
  136. $this->service_install_res('false','db connect error,db has not exists');
  137. }
  138. $this->output('wrong_db.html');
  139. }else{
  140. if(file_exists(BASE_DIR.'/config/config.php')){
  141. $sample = file_get_contents(BASE_DIR.'/config/config.php');
  142. }else{
  143. $sample = file_get_contents(BASE_DIR.'/config/config.sample.php');
  144. }
  145. $s = array('DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST','STORE_KEY','DB_PREFIX','SERVER_TIMEZONE');
  146. foreach($s as $v){
  147. if(defined($v) || isset($constant[$v])){
  148. $arr['#(define\\s*\\(\\s*[\'"]'.$v.'[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1\''.str_replace('\'','\\\'',isset($constant[$v])?$constant[$v]:constant($v)).'\');';
  149. }else{
  150. echo 'error: undefined constant: '.$v;
  151. exit();
  152. }
  153. }
  154. if(RANDOM_HOME) $arr['#(define\\(\\s*[\'"]HOME_DIR[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1dirname(__FILE__).\'/home'.'_'.substr(md5(microtime()),3,6).'\');';
  155. if(file_put_contents(BASE_DIR.'/config/config.php',preg_replace(array_keys($arr),array_values($arr),$sample))){
  156. //header('Location: index.php?step=ready');
  157. }else{
  158. $this->service_install_res('false','error: configure file write error! ');
  159. exit();
  160. }
  161. }
  162. }
  163. function step_ready(){
  164. $this->step_setdb();
  165. if($this->loadConfig()) {
  166. include('svinfo.php');
  167. $tester = new mdl_serverinfo();
  168. $this->pagedata['svinfo'] = $tester->run();
  169. $this->pagedata['status'] = 0;
  170. $this->db = &$this->database();
  171. if(!$this->db->_rw_conn()){
  172. echo 'error: can\'t connect MySql Server! '.mysql_error();
  173. exit();
  174. }
  175. if(!defined('DB_PREFIX')){
  176. define('DB_PREFIX','sdb_');
  177. }
  178. $this->pagedata['db_pre'] = DB_PREFIX;
  179. $this->set_timezone(SERVER_TIMEZONE);
  180. $tz = timezone_list();
  181. $timelist = array();
  182. foreach($tz as $z=>$t){
  183. $timelist[$z] = date('H:i',time()+($z-SERVER_TIMEZONE)*3600).'-'.$t;
  184. }
  185. $this->pagedata['defaultHour'] = SERVER_TIMEZONE;
  186. $this->pagedata['timelist'] = &$timelist;
  187. $this->pagedata['stimezone'] = $tz[SERVER_TIMEZONE];
  188. $this->output('step_ready.html');
  189. }else header('Location: index.php?step=1');
  190. }
  191. function step_cluster(){
  192. if($this->loadConfig()) {
  193. $this->output('step_cluster.html');
  194. }else header('Location: index.php?step=1');
  195. }
  196. function do_install($full_install=true){
  197. if(!$full_install){
  198. $this->install_basic_db();
  199. }
  200. //$sqlContent = file_get_contents('dbscripts/mysql.sql');
  201. $sqlContent.= file_get_contents('dbscripts/init.sql');
  202. if($_POST['use_demo']){
  203. $sqlContent .= file_get_contents('dbscripts/demo.sql');
  204. }
  205. $this->db->exec('SET NAMES utf8');
  206. //$this->install_basic_db();
  207. foreach($this->db->splitSql($sqlContent) as $sql){
  208. if(!$this->db->exec($sql,true)){
  209. $this->service_install_res('false','<h3>Sql Error</h3><textarea style="width:500px;height:300px">'.htmlspecialchars($sql).'</textarea><br />');
  210. //echo '<h3>Sql Error</h3><textarea style="width:500px;height:300px">'.htmlspecialchars($sql).'</textarea><br />';
  211. echo $this->db->errorInfo();
  212. exit();
  213. }
  214. }
  215. if($this->install_mode=='service'){
  216. $password = $_POST['password'];
  217. }else{
  218. $password = md5($_POST['password']);
  219. }
  220. if($this->install_mode=='usual'){
  221. $uname = $_POST['uname']?$_POST['uname']:'admin';
  222. }else{
  223. $uname = 'admin';
  224. }
  225. $rs = $this->db->exec('select * from sdb_operators where 0=1');
  226. $sql = $this->db->getInsertSQL($rs,array(
  227. 'username'=>$uname,
  228. 'userpass'=>$password,
  229. 'status'=>1,
  230. 'super'=>1
  231. ));
  232. if(!$this->db->exec($sql,true)){
  233. $this->service_install_res('false','error: cannot create administrator account.');
  234. exit();
  235. }
  236. if(RANDOM_HOME){
  237. if(!is_dir(HOME_DIR)){
  238. if(!mkdir(HOME_DIR,0777)){
  239. $this->service_install_res('false','error: can\' mk home dir');
  240. exit();
  241. }
  242. }
  243. if(
  244. !mkdir_p(HOME_DIR.'/cache/data') ||
  245. !mkdir_p(HOME_DIR.'/cache/front_tmpl') ||
  246. !mkdir_p(HOME_DIR.'/cache/admin_tmpl') ||
  247. !mkdir_p(HOME_DIR.'/download') ||
  248. !mkdir_p(HOME_DIR.'/fonts') ||
  249. !mkdir_p(HOME_DIR.'/logs') ||
  250. !mkdir_p(HOME_DIR.'/template') ||
  251. !mkdir_p(HOME_DIR.'/upload')
  252. ){
  253. $this->service_install_res('false','error: can\' mk home dir');
  254. exit();
  255. }
  256. }
  257. $this->setConf('system.timezone.default',$_POST['timezone']);
  258. $this->init();
  259. $this->pagedata['password'] = $_POST['password'];
  260. $this->pagedata['uname'] = $uname;
  261. file_put_contents(BASE_DIR.'config/install.lock','If you want to reinstall system, delete this file!');
  262. $this->cache->clear();
  263. $data['article'] = $this->db->selectrow("SELECT max(article_id) as max FROM sdb_articles");
  264. $data['goods'] = $this->db->selectrow("SELECT max(goods_id) as max FROM sdb_goods");
  265. $data['goods_cat'] = $this->db->selectrow("SELECT max(cat_id) as max FROM sdb_goods_cat");
  266. $data['goods_memo'] = $this->db->selectrow("SELECT max(goods_id) as max FROM sdb_goods_memo");
  267. $data['goods_type'] = $this->db->selectrow("SELECT max(type_id) as max FROM sdb_goods_type");
  268. $data['package_product'] = $this->db->selectrow("SELECT max(product_id) as max FROM sdb_package_product");
  269. $data['products'] = $this->db->selectrow("SELECT max(product_id) as max FROM sdb_products");
  270. $data['spec_values'] = $this->db->selectrow("SELECT max(spec_value_id) as max FROM sdb_spec_values");
  271. $data['specification'] = $this->db->selectrow("SELECT max(spec_id) as max FROM sdb_specification");
  272. $data['type_brand'] = $this->db->selectrow("SELECT max(type_id) as max FROM sdb_type_brand");
  273. $data['brand'] = $this->db->selectrow("SELECT max(brand_id) as max FROM sdb_brand");
  274. $data['goods_virtual_cat'] = $this->db->selectrow("SELECT max(virtual_cat_id) as max FROM sdb_goods_virtual_cat");
  275. $data['gimages'] = $this->db->selectrow("SELECT max(gimage_id) as max FROM sdb_gimages");
  276. $data['status'] = $this->db->selectrow("SELECT max(last_update) as max FROM sdb_status");
  277. $this->setConf('system.last_backup',time());
  278. $this->setConf('system.test.database',serialize($data));
  279. }
  280. function step_full_install(){
  281. set_time_limit(0);
  282. if($this->loadConfig()) {
  283. $this->db = &$this->database();
  284. if(!$this->db->_rw_conn(true)){
  285. $this->output('wrong_db.html');
  286. exit;
  287. }else{
  288. $this->do_install();
  289. $this->output('step_install.html');
  290. exit();
  291. }
  292. }
  293. header('Location: index.php?step=1');
  294. }
  295. function step_install_mysql_db(){
  296. $this->loadConfig();
  297. if(constant('DB_HOST')){
  298. $lnk = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
  299. if(!$lnk){
  300. $dbver = 0;
  301. }else{
  302. if(preg_match('/[0-9\.]+/is',mysql_get_server_info($lnk),$match)){
  303. $dbver = $match[0];
  304. }
  305. }
  306. }else{
  307. $dbver = 0;
  308. }
  309. header("mysql:".$dbver);
  310. if($this->install_basic_db()){
  311. echo '???????|index.php?step=install_init_db|??????';
  312. }else{
  313. echo '???????!|fail';
  314. }
  315. }
  316. function step_install_init_db(){
  317. $this->loadConfig();
  318. $this->get_exec_sql('dbscripts/init.sql','??????????!|fail');
  319. if($_POST['use_demo']=="true"){
  320. echo '??????????!|index.php?step=install_demo_db|DEMO??';
  321. }else{
  322. echo '??????????!|index.php?step=install_plugins|????';
  323. }
  324. }
  325. function db_connect(){
  326. if(!$this->loadConfig()){
  327. return false;
  328. }
  329. if(!$this->db){
  330. $this->db = &$this->database();
  331. }
  332. if(!defined('DB_PREFIX')){
  333. define('DB_PREFIX','sdb_');
  334. }
  335. if(!$this->db->_rw_conn(true)){
  336. return false;
  337. }
  338. return true;
  339. }
  340. function get_exec_sql($file,$errormsg='????'){
  341. if($this->db_connect()) {
  342. $sqlContent= file_get_contents($file);
  343. foreach($this->db->splitSql($sqlContent) as $sql){
  344. if(!$this->db->exec($sql,true)){
  345. echo $errormsg.'|fail';
  346. exit();
  347. }
  348. }
  349. }else{
  350. echo $errormsg.'|fail';
  351. exit();
  352. }
  353. }
  354. function step_install_demo_db(){
  355. $this->loadConfig();
  356. $this->get_exec_sql('dbscripts/demo.sql','????DEMO????!|fail');
  357. echo '????DEMO????!|index.php?step=install_plugins|????';
  358. }
  359. function step_install_plugins(){
  360. $this->loadConfig();
  361. $addons = &$this->loadModel('system/addons');
  362. $addons->refresh();
  363. $appmgr = &$this->loadModel('system/appmgr');
  364. $apps = $appmgr->getList('no_compare');
  365. foreach($apps as $k =>$v){
  366. if($v['plugin_ident']){
  367. if($v['plugin_ident']!="shopex_stat"){
  368. $appmgr->install($v['plugin_ident']);
  369. }else{
  370. if($_POST['install_stat']){
  371. if($appmgr->load("shopex_stat")){
  372. $appmgr->install($v['plugin_ident']);
  373. $appmgr->enable($v['plugin_ident']);
  374. }
  375. }
  376. }
  377. }
  378. if(substr($item['plugin_ident'],0,4) == 'pay_'){
  379. $appmgr->enable($val['plugin_ident']);
  380. }
  381. }
  382. echo '??????!|index.php?step=install_success|?????';
  383. }
  384. function step_install_success(){
  385. if(!$this->db_connect()){
  386. echo '?????????!|fail';
  387. exit();
  388. }
  389. $password = $_POST['password'];
  390. $uname = $_POST['uname']?$_POST['uname']:'admin';
  391. $rs = $this->db->exec('select * from sdb_operators where 0=1');
  392. $sql = $this->db->getInsertSQL($rs,array(
  393. 'username'=>$uname,
  394. 'userpass'=>md5($password),
  395. 'status'=>1,
  396. 'super'=>1
  397. ));
  398. if(!$this->db->exec($sql,true)){
  399. $this->service_install_res('false','error: cannot create administrator account.');
  400. exit();
  401. }
  402. if(RANDOM_HOME){
  403. if(!is_dir(HOME_DIR)){
  404. if(!mkdir(HOME_DIR,0777)){
  405. $this->service_install_res('false','error: can\' mk home dir');
  406. exit();
  407. }
  408. }
  409. if(
  410. !mkdir_p(HOME_DIR.'/cache/data') ||
  411. !mkdir_p(HOME_DIR.'/cache/front_tmpl') ||
  412. !mkdir_p(HOME_DIR.'/cache/admin_tmpl') ||
  413. !mkdir_p(HOME_DIR.'/download') ||
  414. !mkdir_p(HOME_DIR.'/fonts') ||
  415. !mkdir_p(HOME_DIR.'/logs') ||
  416. !mkdir_p(HOME_DIR.'/template') ||
  417. !mkdir_p(HOME_DIR.'/upload')
  418. ){
  419. echo 'error: can\' mk home dir';
  420. exit();
  421. }
  422. }
  423. $this->setConf('system.timezone.default',$_POST['timezone']);
  424. $this->setConf('system.last_backup',time());
  425. $this->init();
  426. $this->cache->clear();
  427. //file_put_contents(BASE_DIR.'config/install.lock','If you want to reinstall system, delete this file!');
  428. //$this->fetch('step_install.html',array('goodsItem'=>$goodsItem, 'giftsItem'=>$giftsItem));
  429. echo '???????!|index.php?step=install_fake_html|?????';
  430. }
  431. function step_install_fake_html(){
  432. if(!$this->db_connect()){
  433. echo '?????????!|fail';
  434. exit();
  435. }
  436. $o = &$this->loadModel('utility/tools');
  437. if($o->test_fake_html(true,$msg)){
  438. echo '???????!|index.php?step=install_done|????';
  439. }else{
  440. echo '???????'.$msg.'!|index.php?step=install_done|????';
  441. }
  442. }
  443. function step_install_done(){
  444. file_put_contents(BASE_DIR.'config/install.lock','If you want to reinstall system, delete this file!');
  445. echo 'success';
  446. }
  447. function step_complete(){
  448. $this->pagedata['link_url'] = dirname(dirname($_SERVER['PHP_SELF'])).'/shopadmin/';
  449. $this->pagedata['password'] = $_POST['password'];
  450. $this->pagedata['uname'] = $_POST['uname']?$_POST['uname']:'admin';
  451. $this->output('step_install.html');
  452. }
  453. function install_basic_db(){
  454. $o = &$this->loadModel('utility/schemas');
  455. $aDb = $o->get_system_schemas();
  456. $aDb = array_merge( array( 'settings'=>$aDb['settings'], 'plugins'=>$aDb['plugins']), $aDb );
  457. foreach( $aDb as $name=>$schema){
  458. $db = &$this->database();
  459. $db->exec('drop table if exists '.$db->prefix.$name);
  460. $db->exec($o->get_insert_sql(CORE_DIR.'/schemas/'.$schema));
  461. }
  462. return true;
  463. }
  464. function init(){
  465. $ver = $this->version();
  466. $db = &$this->database();
  467. $db->exec('drop table if exists sdb_dbver');
  468. $ver['dbver'] = $ver['dbver']?$ver['dbver']:'dbver';
  469. $db->exec("create table sdb_dbver(`{$ver['dbver']}` varchar(255)) type = MYISAM");
  470. $o = &$this->loadModel('content/sitemap');
  471. $o->update();
  472. $o = &$this->loadModel('system/template');
  473. $o->initTheme('purple');
  474. $result=$o->setDefault('purple');
  475. $usedTpl = $o->getDefault();
  476. $o->getList();
  477. $o = &$this->loadModel('system/local');
  478. $rs = $o->use_package(defined('DEFAULT_LOCAL')?DEFAULT_LOCAL:'mainland');
  479. }
  480. function step_reinstall(){
  481. $this->step_install();
  482. }
  483. function output($file){
  484. header('Content-type: text/html;charset=utf-8');
  485. $this->pagedata['PAGE'] = $file;
  486. $this->pagedata['version'] = $this->version();
  487. $smarty = &$this->loadModel('system/frontend');
  488. $smarty->ctl = &$this;
  489. $smarty->template_dir = 'view/';
  490. $smarty->compile_dir = 'templates_c/';
  491. $smarty->left_delimiter='<{';
  492. $smarty->right_delimiter='}>';
  493. $smarty->_vars = &$this->pagedata;
  494. $smarty->display('main.html');
  495. exit();
  496. }
  497. function checkPerm(){
  498. if(!is_writable('templates_c')){
  499. echo '<strong>Error:</strong> <i>install/templates_c</i> is not writable!';
  500. exit();
  501. }
  502. $arr = array(
  503. array('path'=>'install/templates_c','is_writable'=>true)
  504. );
  505. chdir('../');
  506. $this->_checkDirPerm('config',$arr);
  507. $this->_checkDirPerm('themes',$arr);
  508. $this->_checkDirPerm('images',$arr);
  509. $this->_checkDirPerm('home',$arr,true);
  510. chdir('install');
  511. foreach($arr as $item){
  512. if(!$item['is_writable']){
  513. $this->pagedata['dirs'] = $arr;
  514. $this->output('permview.html');
  515. exit();
  516. }
  517. }
  518. }
  519. function _checkDirPerm($dir,&$arr,$r=false){
  520. $arr[] = array(
  521. 'path'=>$dir,
  522. 'is_writable'=>is_writable($dir),
  523. );
  524. if($r){
  525. if($handle = opendir($dir)){
  526. while(false != ($file = readdir($handle))){
  527. if($file{0}!='.' && is_dir($dir.'/'.$file)){
  528. $this->_checkDirPerm($dir.'/'.$file,$arr,$r);
  529. }
  530. }
  531. closedir($handle);
  532. }
  533. }
  534. }
  535. function base_url(){
  536. return dirname(dirname($_SERVER["PHP_SELF"]));
  537. }
  538. }
  539. ?>