PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/b2b/install/install.core.php

http://phpfor.googlecode.com/
PHP | 426 lines | 364 code | 54 blank | 8 comment | 58 complexity | 04767349ae9aa20bf4010622154affea 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. if($_POST['dbname'] && $_POST['dbuser'] && $_POST['dbhost']){
  8. $this->type = 'autoInstall';
  9. if(file_exists('../config/install.lock')){
  10. $this->outJson('false','Access denied by install.lock');
  11. exit();
  12. }
  13. $_POST['uname'] = $_POST['dbuser'];
  14. $_POST['pwd'] = $_POST['dbpass'];
  15. $_POST['prefix']=$_POST['prefix']?$_POST['prefix']:'sdb_';
  16. $_POST['use_demo']=($_POST['data']=='true')?true:false;
  17. $_POST['stimezone']=$_POST['stimezone']?$_POST['stimezone']:8;
  18. parent::kernel();
  19. $this->installReady();
  20. }else{
  21. if(file_exists('../config/install.lock')){
  22. $this->responseCode('403');
  23. echo 'Access denied by install.lock';
  24. exit();
  25. }else{
  26. parent::kernel();
  27. $this->checkPerm();
  28. $this->run();
  29. }
  30. }
  31. }
  32. function installReady(){
  33. if(!($link = mysql_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass']))){
  34. $this->outJson('false','Can not connect database!');
  35. exit();
  36. }
  37. if(!$dbselect = mysql_select_db($_POST['dbname'],$link)){
  38. $this->outJson('false','Please select a Database!');
  39. exit();
  40. }
  41. $this->step_setdb();
  42. $this->loadConfig();
  43. $this->step_install();
  44. }
  45. function run(){
  46. $_GET['step'] = $_GET['step']?$_GET['step']:1;
  47. $method = 'step_'.$_GET['step'];
  48. if(method_exists($this,$method)){
  49. call_user_func(array(&$this,$method));
  50. }else{
  51. header("HTTP/1.0 404 Not Found",true,404);
  52. echo '<h1>404 Not Found</h1><hr />';
  53. }
  54. }
  55. function loadConfig(){
  56. ob_start();
  57. if(include('../config/config.php')){
  58. if(defined('DB_NAME') && defined('DB_USER') && defined('DB_PASSWORD') && defined('DB_HOST')){
  59. ob_end_clean();
  60. return true;
  61. }
  62. }
  63. ob_end_clean();
  64. return false;
  65. }
  66. function installCore(){
  67. $this->__construct();
  68. }
  69. function step_checkdb(){
  70. if(!($link = mysql_connect($_POST['dbhost'], $_POST['uname'], $_POST['pwd']))){
  71. header("Cache-Control: no-cache, must-revalidate",true,503);
  72. exit();
  73. }
  74. $database = array();
  75. if($result = mysql_query('show databases')){
  76. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  77. if($row['Database']!='mysql' && $row['Database']!='information_schema')
  78. $database[] = $row['Database'];
  79. }
  80. }
  81. mysql_free_result($result);
  82. mysql_close($link);
  83. if(count($database)>0){
  84. $html = '<select id="db_name" name="dbname">';
  85. foreach($database as $db){
  86. $html .= '<option value="'.htmlspecialchars($db).'">'.$db.'</option>';
  87. }
  88. echo $html .= '</select>';
  89. }
  90. }
  91. function step_1(){
  92. $this->output('step_1.html');
  93. }
  94. function step_2(){
  95. if(is_file(BASE_DIR.'/config/config.php')){
  96. require_once(BASE_DIR.'/config/config.php');
  97. $this->pagedata['database_username'] = DB_USER;
  98. $this->pagedata['database_password'] = DB_PASSWORD;
  99. $this->pagedata['database_name'] = DB_NAME;
  100. $this->pagedata['host'] = DB_HOST;
  101. $this->pagedata['database_prefix'] = DB_PREFIX;
  102. }
  103. $this->pagedata['timezone'] = timezone_list();
  104. $this->pagedata['default_timezone'] = 8;
  105. $this->output('step_2.html');
  106. }
  107. /**
  108. * step_mysqlver ????????
  109. *
  110. * @access public
  111. * @return void
  112. */
  113. function step_mysqlver(){
  114. $this->pagedata['type'] = $_GET['type'];
  115. $this->output('mysqlver.html');
  116. }
  117. function step_setdb(){
  118. define('DB_NAME', $_POST['dbname']); // The name of the database
  119. define('DB_USER', $_POST['uname']); // Your MySQL username
  120. define('DB_PASSWORD', $_POST['pwd']); // ...and password
  121. define('DB_HOST', $_POST['dbhost']);
  122. define('DB_PREFIX', $_POST['prefix']);
  123. define('SERVER_TIMEZONE', $_POST['stimezone']);
  124. if(php_sapi_name()=='isapi'){
  125. $constant['WITHOUT_FLOCK'] = true;
  126. }
  127. if(!defined('STORE_KEY') || strlen(trim(STORE_KEY))==0){
  128. $constant['STORE_KEY'] = md5(print_r(microtime(),true).print_r($_SERVER,true));
  129. }
  130. $this->db = &$this->database();
  131. if(!$this->db->rwInstance(true)){
  132. $this->output('wrong_db.html');
  133. }else{
  134. if(file_exists(BASE_DIR.'/config/config.php')){
  135. $sample = file_get_contents(BASE_DIR.'/config/config.php');
  136. }else{
  137. $sample = file_get_contents(BASE_DIR.'/config/config.sample.php');
  138. }
  139. $s = array('DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST','STORE_KEY','DB_PREFIX','SERVER_TIMEZONE');
  140. foreach($s as $v){
  141. if(defined($v) || isset($constant[$v])){
  142. $arr['#(define\\s*\\(\\s*[\'"]'.$v.'[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1\''.str_replace('\'','\\\'',isset($constant[$v])?$constant[$v]:constant($v)).'\');';
  143. }else{
  144. echo 'error: undefined constant: '.$v;
  145. exit();
  146. }
  147. }
  148. if(RANDOM_HOME) $arr['#(define\\(\\s*[\'"]HOME_DIR[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1dirname(__FILE__).\'/home'.'_'.substr(md5(microtime()),3,6).'\');';
  149. if(file_put_contents(BASE_DIR.'/config/config.php',preg_replace(array_keys($arr),array_values($arr),$sample))){
  150. //header('Location: index.php?step=ready');
  151. }else{
  152. if($this->type == 'autoInstall'){
  153. $this->outJson('false','configure file write error!');
  154. exit();
  155. }
  156. echo 'error: configure file write error! ';
  157. exit();
  158. }
  159. }
  160. }
  161. function step_ready(){
  162. $this->step_setdb();
  163. if($this->loadConfig()) {
  164. include('svinfo.php');
  165. $tester = new mdl_serverinfo();
  166. $this->pagedata['svinfo'] = $tester->run();
  167. $this->pagedata['status'] = 0;
  168. $this->db = &$this->database();
  169. if(!$this->db->rwInstance(true)){
  170. echo 'error: can\'t connect MySql Server! '.mysql_error();
  171. exit();
  172. }
  173. if(!defined('DB_PREFIX')){
  174. define('DB_PREFIX','');
  175. }
  176. $this->pagedata['db_pre'] = DB_PREFIX;
  177. $this->set_timezone(SERVER_TIMEZONE);
  178. $tz = timezone_list();
  179. $timelist = array();
  180. foreach($tz as $z=>$t){
  181. $timelist[$z] = date('H:i',time()+($z-SERVER_TIMEZONE)*3600).'-'.$t;
  182. }
  183. $this->pagedata['defaultHour'] = SERVER_TIMEZONE;
  184. $this->pagedata['timelist'] = &$timelist;
  185. $this->pagedata['stimezone'] = $tz[SERVER_TIMEZONE];
  186. $this->output('step_ready.html');
  187. }else header('Location: index.php?step=1');
  188. }
  189. function step_cluster(){
  190. if($this->loadConfig()) {
  191. $this->output('step_cluster.html');
  192. }else header('Location: index.php?step=1');
  193. }
  194. function step_install(){
  195. set_time_limit(0);
  196. if($this->loadConfig()) {
  197. $this->db = &$this->database();
  198. $this->db->exec('SET NAMES utf8');
  199. if(!$this->db->rwInstance(true)){
  200. $this->output('wrong_db.html');
  201. exit;
  202. }else{
  203. $sqlContent = file_get_contents('dbscripts/mysql.sql');
  204. $sqlContent .= file_get_contents('dbscripts/init.sql');
  205. if($_POST['use_demo']){
  206. $sqlContent .= file_get_contents('dbscripts/demo.sql');
  207. }
  208. foreach($this->db->splitSql($sqlContent) as $sql){
  209. if(!$this->db->exec($sql,true)){
  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->type == 'autoInstall'){
  216. $password = $_POST['password'];
  217. }else{
  218. $password = md5($_POST['password']);
  219. }
  220. $uname = $_POST['username']?$_POST['username']:'admin';
  221. $rs = $this->db->exec('select * from sdb_operators where 0=1');
  222. $sql = $this->db->getInsertSQL($rs,array(
  223. 'username'=>$uname,
  224. 'userpass'=>$password,
  225. 'status'=>1,
  226. 'super'=>1
  227. ));
  228. if(!$this->db->exec($sql,true)){
  229. if($this->type == 'autoInstall'){
  230. $this->outJson('false','cannot create administrator account.');
  231. exit();
  232. }
  233. echo '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. echo '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. echo '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.test.database',serialize($data));
  278. if($this->type == 'autoInstall'){
  279. $this->outJson('true','Shop had installed!');
  280. exit;
  281. }
  282. $this->output('step_install.html');
  283. exit();
  284. }
  285. }
  286. header('Location: index.php?step=1');
  287. }
  288. function init(){
  289. $ver = $this->version();
  290. $db = &$this->database();
  291. $db->exec('drop table if exists sdb_dbver');
  292. $ver['dbver'] = $ver['dbver']?$ver['dbver']:'dbver';
  293. $db->exec("create table sdb_dbver(`{$ver['dbver']}` varchar(255)) type = MYISAM");
  294. $o = &$this->loadModel('content/sitemap');
  295. $o->update();
  296. $o = &$this->loadModel('system/template');
  297. $o->initTheme('b2b_dgfc');
  298. $result=$o->setDefault('b2b_dgfc');
  299. $usedTpl = $o->getDefault();
  300. $o->getList();
  301. $o = &$this->loadModel('system/local');
  302. $rs = $o->use_package(defined('DEFAULT_LOCAL')?DEFAULT_LOCAL:'mainland');
  303. }
  304. function step_reinstall(){
  305. $this->step_install();
  306. }
  307. function output($file){
  308. header('Content-type: text/html;charset=utf-8');
  309. $this->pagedata['PAGE'] = $file;
  310. $this->pagedata['version'] = $this->version();
  311. $smarty = &$this->template();
  312. $smarty->ctl = &$this;
  313. $smarty->plugins_dir[] = CORE_DIR.'/admin/smartyplugin';
  314. $smarty->template_dir = 'view';
  315. if(defined('HOME_DIR')){
  316. $smarty->compile_dir = HOME_DIR.'/cache/front_tmpl';
  317. }
  318. $smarty->left_delimiter='{';
  319. $smarty->right_delimiter='}';
  320. foreach($this->pagedata as $k=>$v){
  321. $smarty->assign($k,$v);
  322. }
  323. $smarty->display('main.html');
  324. exit();
  325. }
  326. function checkPerm(){
  327. if(!is_writable('templates_c')){
  328. echo '<strong>Error:</strong> <i>install/templates_c</i> is not writable!';
  329. exit();
  330. }
  331. $arr = array(
  332. array('path'=>'install/templates_c','is_writable'=>true)
  333. );
  334. chdir('../');
  335. $this->_checkDirPerm('config',$arr);
  336. $this->_checkDirPerm('themes',$arr);
  337. $this->_checkDirPerm('home',$arr,true);
  338. chdir('install');
  339. foreach($arr as $item){
  340. if(!$item['is_writable']){
  341. $this->pagedata['dirs'] = $arr;
  342. $this->output('permview.html');
  343. exit();
  344. }
  345. }
  346. }
  347. function _checkDirPerm($dir,&$arr,$r=false){
  348. $arr[] = array(
  349. 'path'=>$dir,
  350. 'is_writable'=>is_writable($dir),
  351. );
  352. if($r){
  353. if($handle = opendir($dir)){
  354. while(false != ($file = readdir($handle))){
  355. if($file{0}!='.' && is_dir($dir.'/'.$file)){
  356. $this->_checkDirPerm($dir.'/'.$file,$arr,$r);
  357. }
  358. }
  359. closedir($handle);
  360. }
  361. }
  362. }
  363. function base_url(){
  364. return dirname(dirname($_SERVER["PHP_SELF"]));
  365. }
  366. function outJson($res,$rsp){
  367. //$res:?? $rsp:????
  368. $outjson = array("res"=>"$res","rsp"=>"$rsp");
  369. echo json_encode($outjson);
  370. }
  371. }
  372. ?>