PageRenderTime 820ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/install/install.php

https://github.com/sahilbabu/phpb2b
PHP | 441 lines | 422 code | 9 blank | 10 comment | 72 complexity | ec8127de1a1987c1ab40819d825ee23a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * [PHPB2B] Copyright (C) 2007-2099, Ualink Inc. All Rights Reserved.
  4. * The contents of this file are subject to the License; you may not use this file except in compliance with the License.
  5. *
  6. * @version $Revision: 2048 $
  7. */
  8. session_start();
  9. error_reporting(E_ERROR);
  10. @set_magic_quotes_runtime(0);
  11. @ini_set('magic_quotes_sybase', 0);
  12. if (isset($_GET['act'])) {
  13. if($_GET['act'] == "phpinfo"){
  14. die(phpinfo());
  15. }
  16. }
  17. if (!defined('DIRECTORY_SEPARATOR')) {
  18. define('DIRECTORY_SEPARATOR','/');
  19. }
  20. define('DS', DIRECTORY_SEPARATOR);
  21. define('TIME', time());
  22. define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  23. define('PHPB2B_ROOT', substr(dirname(__FILE__), 0, -7));
  24. define('JSMIN_AS_LIB', true); // prevents auto-run on include
  25. require '../data/phpb2b_version.php';
  26. require '../configs/config.inc.php';
  27. require '../configs/paths.php';
  28. define('IN_PHPB2B',true);
  29. if(!defined('LIB_PATH')) define('LIB_PATH',PHPB2B_ROOT.'libraries'.DS);
  30. require '../libraries/global.func.php';
  31. require '../libraries/func.sql.php';
  32. require "../libraries/db_mysql.inc.php";
  33. require "../libraries/json_config.php";
  34. require "../libraries/core/object.php";
  35. require "../libraries/core/controller.php";
  36. require "../libraries/file.class.php";
  37. require("../libraries/error.class.php");
  38. list($accept_language) = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  39. if(file_exists(PHPB2B_ROOT. 'languages'.DS.strtolower($accept_language).DS."default.dot"))
  40. $app_lang = strtolower($accept_language);
  41. if (!empty($_GET['app_lang'])) {
  42. $app_lang = $_GET['app_lang'];
  43. }
  44. if (!defined('CACHE_PATH')) {
  45. define('CACHE_PATH', PHPB2B_ROOT."data".DS."cache".DS.$app_lang.DS);
  46. define('CACHE_LANG_PATH', PHPB2B_ROOT."data".DS."cache".DS.$app_lang.DS);
  47. define('CACHE_COMMON_PATH', PHPB2B_ROOT."data".DS."cache".DS);
  48. }
  49. //language
  50. $params = array('file'=>'default.dot','section'=>'install', 'scope' => 'global');
  51. $_file = isset($params['file']) ? dequote($params['file']) : null;
  52. $_section = isset($params['section']) ? dequote($params['section']) : null;
  53. $config_dir = PHPB2B_ROOT. 'languages'.DS. $app_lang.DS;
  54. require_once PHPB2B_ROOT.'libraries/language.class.php';
  55. $smarty->_conf_obj = new ConfigFile($config_dir);
  56. $_params['source_content'] = '';
  57. $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
  58. $arrTemplate = array_merge($smarty->_conf_obj->get($_file),
  59. $smarty->_conf_obj->get($_file, $_section));
  60. extract($arrTemplate);
  61. //:~
  62. $db = new DB_Sql();
  63. $file_cls = new Files();
  64. $pb_protocol = 'http';
  65. if ( isset( $_SERVER['HTTPS'] ) && ( strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) {
  66. $pb_protocol = 'https';
  67. }
  68. $PHP_SELF = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : preg_replace("/(.*)\.php(.*)/i", "\\1.php", $_SERVER['PHP_SELF']);
  69. $BASESCRIPT = basename($PHP_SELF);
  70. list($BASEFILENAME) = explode('.', $BASESCRIPT);
  71. $install_url = htmlspecialchars($pb_protocol."://".pb_getenv('HTTP_HOST').preg_replace("/\/+(api|wap)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/');
  72. $siteUrl = substr($install_url,0,-(strlen($BASEFILENAME)+1));
  73. $time_stamp = TIME;
  74. if($_REQUEST)
  75. {
  76. if(!MAGIC_QUOTES_GPC)
  77. {
  78. $_REQUEST = pb_addslashes($_REQUEST);
  79. if($_COOKIE) $_COOKIE = pb_addslashes($_COOKIE);
  80. }
  81. extract($_REQUEST, EXTR_SKIP);
  82. }
  83. if(!isset($_GET['step'])) {
  84. $step = '1';
  85. }else{
  86. $step = intval($_GET['step']);
  87. }
  88. if (isset($_GET['do'])) {
  89. $do = trim($_GET['do']);
  90. if($do == "complete"){
  91. include "step".$step.".inc.php";
  92. exit;
  93. }
  94. }
  95. if(file_exists(PHPB2B_ROOT.'data/install.lock')) {
  96. $msg = L("install_locked", "tpl");
  97. Errors::showError($msg);
  98. exit;
  99. }
  100. $license_file_name = "LICENSE.txt";
  101. if (!file_exists(PHPB2B_ROOT.$license_file_name)) {
  102. $msg = L("license_not_exists");
  103. Errors::showError($msg);
  104. exit;
  105. }
  106. $backupdir = pb_radom(6);
  107. $db_error = false;
  108. switch($step)
  109. {
  110. case '1':
  111. include "step".$step.".inc.php";
  112. break;
  113. case '2':
  114. $license = file_get_contents(PHPB2B_ROOT.$license_file_name);
  115. include "step".$step.".inc.php";
  116. break;
  117. case '3':
  118. $gd_support = '';
  119. if(extension_loaded('gd'))
  120. {
  121. if(function_exists('imagepng')) $gd_support .= 'png';
  122. if(function_exists('imagejpeg')) $gd_support .= ' jpg';
  123. if(function_exists('imagegif')) $gd_support .= ' gif';
  124. }
  125. $is_right = (phpversion() >= '4.3.0' && extension_loaded('mysql')) ? 1 : 0;
  126. include "step".$step.".inc.php";
  127. break;
  128. case '4':
  129. $files = file("chmod.txt");
  130. $files = array_filter($files);
  131. $writablefile = $no_writablefile = null;
  132. foreach($files as $file)
  133. {
  134. $file = str_replace('*','',$file);
  135. $file = trim($file);
  136. if(!is_writable('../'.$file)){
  137. $no_writablefile .= $file.' '."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&times;<br>";
  138. }else{
  139. $writablefile .= $file.' '.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&radic;<br>';
  140. }
  141. }
  142. include "step".$step.".inc.php";
  143. break;
  144. case '5':
  145. include "step".$step.".inc.php";
  146. break;
  147. case '6':
  148. $dbhost = $_POST['dbhost'];
  149. $dbuser = $_POST['dbuser'];
  150. $dbpasswd = $_POST['dbpw'];
  151. $dbname = $_POST['dbname'];
  152. $tablepre = $_POST['tablepre'];
  153. $username = $_POST['username'];
  154. $password = $_POST['password'];
  155. $createdb = $_POST['db']['create'];
  156. $email = $_POST['email'];
  157. $passwordkey = $_POST['password_key'];
  158. $conn = mysql_connect($dbhost, $dbuser, $dbpasswd);
  159. if(!$conn){
  160. $error_info = mysql_errno()." : ".mysql_error();
  161. $db_error = true;
  162. }
  163. include "step".$step.".inc.php";
  164. break;
  165. case '7':
  166. $sitename = $_POST['sitename'];
  167. if(isset($_POST['testdata'])){
  168. $testdata = $_POST['testdata'];
  169. }
  170. $configs['dbhost'] = $dbhost = $_POST['dbhost'];
  171. $configs['dbuser'] = $dbuser = $_POST['dbuser'];
  172. $configs['dbpasswd'] = $dbpasswd = $_POST['dbpw'];
  173. $configs['dbname'] = $dbname = $_POST['dbname'];
  174. $configs['tb_prefix'] = $tb_prefix = $_POST['tablepre'];
  175. $configs['dbcharset'] = $dbcharset;
  176. $configs['pconnect'] = $pconnect;
  177. $username = $_POST['username'];
  178. $password = $_POST['password'];
  179. $createdb = $_POST['createdb'];
  180. $configs['admin_email'] = $email = $_POST['email'];
  181. $passwordkey = $_POST['password_key'];
  182. if (empty($passwordkey)) {
  183. $passwordkey = pb_radom(16);
  184. }
  185. $configs['absolute_uri'] = $siteurl = $_POST['siteurl'];
  186. if(empty($passwordkey)){
  187. $passwordkey = pb_radom(16);
  188. }
  189. if(empty($sitename)){
  190. $sitename = L("a_new_b2b_site", "tpl");
  191. }
  192. if (empty($sitetitle)) {
  193. $sitetitle = L("a_new_b2b_title", "tpl");
  194. }
  195. $conn = mysql_connect($dbhost, $dbuser, $dbpasswd);
  196. if($conn){
  197. $version = mysql_get_server_info();
  198. $set_names = "SET NAMES '$dbcharset'";
  199. $set_modes = "SET sql_mode=''";
  200. config_edit($configs);
  201. if($version > '4.1' && $charset)
  202. {
  203. mysql_query($set_names, $conn);
  204. }
  205. if($version > '5.0')
  206. {
  207. mysql_query($set_modes, $conn);
  208. }
  209. if(!mysql_select_db($dbname))
  210. {
  211. if ($createdb==1) {
  212. if(mysql_get_server_info() > '4.1') {
  213. mysql_query("CREATE DATABASE IF NOT EXISTS"
  214. ." $dbname DEFAULT CHARACTER SET $dbcharset;");
  215. } else {
  216. mysql_query("CREATE DATABASE IF NOT EXISTS $dbname;");
  217. }
  218. mysql_close();
  219. }else{
  220. $error_info = mysql_errno()." : ".mysql_error()."<br>";
  221. $db_error = true;
  222. break;
  223. }
  224. } else {
  225. $sqldump = null;
  226. $conn = $db->connect($dbname,$dbhost,$dbuser,$dbpasswd);
  227. if($version > '4.1' && $charset)
  228. {
  229. $db->query($set_names);
  230. }
  231. if($version > '5.0')
  232. {
  233. $db->query($set_modes);
  234. }
  235. $tables = $db->table_names();
  236. if(!empty($tables)){
  237. foreach ($tables as $names) {
  238. if(!function_exists("stripos")){
  239. function stripos($str,$needle) {
  240. return strpos(strtolower($str),strtolower($needle));
  241. }
  242. }
  243. if(stripos($names['table_name'],$tb_prefix) ===0){
  244. $sqldump.=data2sql($names['table_name']);
  245. }
  246. }
  247. pb_create_folder(PHPB2B_ROOT. DS. "data".DS."backup_".$backupdir);
  248. $file_path = PHPB2B_ROOT. DS. "data".DS."backup_".$backupdir.DS.date('ymd').'_'.pb_radom().".sql";
  249. if(trim($sqldump)) {
  250. file_put_contents($file_path ,$sqldump);
  251. unset($sqldump);
  252. }
  253. }
  254. $db->free();
  255. }
  256. ob_start();
  257. $schema_path = "data/schemas/".$app_lang."/";
  258. $schema_common_path = "data/schemas/";
  259. if (!file_exists($schema_path)) {
  260. die(L("congratulate", "msg", $schema_path));
  261. }
  262. if(file_exists($schema_common_path. "mysql.sql"))
  263. {
  264. $conn = $db->connect($dbname,$dbhost,$dbuser,$dbpasswd);
  265. if($version > '4.1' && $charset)
  266. {
  267. $db->query($set_names);
  268. }
  269. if($version > '5.0')
  270. {
  271. $db->query($set_modes);
  272. }
  273. $sqls = file_get_contents($schema_common_path. "mysql.sql");
  274. $r = sql_run($sqls);
  275. if (!$r) {
  276. Errors::showError(mysql_error(), 'db');
  277. exit;
  278. }
  279. $must_sql_data = file_get_contents($schema_path. "mysql.data.sql");
  280. $r = sql_run($must_sql_data);
  281. if (!$r) {
  282. Errors::showError(mysql_error(), 'db');
  283. exit;
  284. }
  285. $structure_sql_data1 = file_get_contents($schema_common_path. "mysql.data.area.sql");
  286. $r = sql_run($structure_sql_data1);
  287. $structure_sql_data2 = file_get_contents($schema_common_path. "mysql.data.industry.sql");
  288. $r = sql_run($structure_sql_data2);
  289. @touch(PHPB2B_ROOT.'./data/install.lock');
  290. if(!empty($testdata)){
  291. $source = "data/attachment/sample";
  292. $dest ="../attachment/sample";
  293. $sqls = file_get_contents($schema_path. "mysql.sample.sql");
  294. sql_run($sqls);
  295. dir_copy($source,$dest,1);
  296. }
  297. //language:~
  298. $show_languages = showLanguages(true);
  299. $_languages = serialize($show_languages);
  300. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('languages', '$_languages')");
  301. //:~
  302. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('install_dateline', '".$time_stamp."')");
  303. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('site_name', '$sitename')");
  304. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('site_title', '".htmlspecialchars($sitetitle)." - Powered By ".$arrTemplate['_software_name']."')");
  305. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('backup_dir', '".$backupdir."')");
  306. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('site_url', '".$siteurl."')");
  307. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('watertext', '".$siteurl."')");
  308. $db->query("REPLACE INTO {$tb_prefix}settings (variable, valued) VALUES ('auth_key', '$passwordkey')");
  309. $aminer_id = 1;
  310. $db->query("REPLACE INTO {$tb_prefix}members (id,username, userpass,email,membertype_id,membergroup_id,created,modified,status) VALUES ({$aminer_id},'{$username}','".md5($password)."','{$email}',2,9,".$time_stamp.",".$time_stamp.",'1')");
  311. $db->query("REPLACE INTO {$tb_prefix}adminfields (member_id,last_name,created,modified) VALUES ('{$aminer_id}','".L("administrator", "tpl")."',".$time_stamp.",".$time_stamp.")");
  312. $db->free();
  313. require(PHPB2B_ROOT. "libraries".DS.'adodb'.DS.'adodb.inc.php');
  314. require(PHPB2B_ROOT. "libraries".DS."cache.class.php");
  315. $cache = new Caches();
  316. $pdb = &NewADOConnection($database);
  317. $conn = $pdb->PConnect($dbhost,$dbuser,$dbpasswd,$dbname);
  318. if($dbcharset && mysql_get_server_info() > '4.1') {
  319. $pdb->Execute("SET NAMES '{$dbcharset}'");
  320. }
  321. $cache->writeCache("setting", "setting");
  322. $cache->writeCache("industry", "industry");
  323. $cache->writeCache("area", "area");
  324. $cache->writeCache("membergroup", "membergroup");
  325. $cache->writeCache("userpage", "userpage");
  326. $cache->writeCache("trusttype", "trusttype");
  327. $cache->writeCache("form", "form");
  328. $cache->writeCache("nav", "nav");
  329. $cache->writeCache("country", "country");
  330. $cache->updateTypevars();
  331. $cache->updateLanguages();
  332. $cache->writeCache("javascript", "javascript");
  333. $cache->updateTypes();
  334. $cache->updateIndexCache();
  335. header("Location:install.php?step={$step}&do=complete&app_lang=".$app_lang);
  336. }
  337. else
  338. {
  339. $db_error = true;
  340. break;
  341. }
  342. }else{
  343. $db_error = true;
  344. break;
  345. }
  346. break;
  347. }
  348. function config_edit($configs) {
  349. global $dbcharset, $app_lang;
  350. if (!is_array($configs)) {
  351. return;
  352. }
  353. extract($configs);
  354. $configfile = PHPB2B_ROOT. 'configs'.DS.'config.inc.php';
  355. $configfiles = file_get_contents($configfile);
  356. $configfiles = trim($configfiles);
  357. $configfiles = preg_replace("/[$]dbhost\s*\=\s*[\"'].*?[\"'];/is", "\$dbhost = '$dbhost';", $configfiles);
  358. $configfiles = preg_replace("/[$]app_lang\s*\=\s*[\"'].*?[\"'];/is", "\$app_lang = '$app_lang';", $configfiles);
  359. $configfiles = preg_replace("/[$]dbuser\s*\=\s*[\"'].*?[\"'];/is", "\$dbuser = '$dbuser';", $configfiles);
  360. $configfiles = preg_replace("/[$]dbpasswd\s*\=\s*[\"'].*?[\"'];/is", "\$dbpasswd = '$dbpasswd';", $configfiles);
  361. $configfiles = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"'];/is", "\$dbname = '$dbname';", $configfiles);
  362. $configfiles = preg_replace("/[$]admin_email\s*\=\s*[\"'].*?[\"'];/is", "\$admin_email = '$admin_email';", $configfiles);
  363. $configfiles = preg_replace("/[$]tb_prefix\s*\=\s*[\"'].*?[\"'];/is", "\$tb_prefix = '$tb_prefix';", $configfiles);
  364. $configfiles = preg_replace("/[$]cookiepre\s*\=\s*[\"'].*?[\"'];/is", "\$cookiepre = '".pb_radom(3)."_';", $configfiles);
  365. $configfiles = preg_replace("/[$]absolute_uri\s*\=\s*[\"'].*?[\"'];/is", "\$absolute_uri = '".$absolute_uri."';", $configfiles);
  366. if(file_put_contents($configfile, $configfiles)){
  367. return true;
  368. }else{
  369. return false;
  370. }
  371. }
  372. function dir_copy($source, $destination, $child){
  373. if(!is_dir($destination)){
  374. mkdir($destination,0777);
  375. }
  376. $handle=dir($source);
  377. while($entry=$handle->read()) {
  378. if(!in_array($entry, array('.', '..', '.svn'))){
  379. if(is_dir($source."/".$entry)){
  380. if($child) {
  381. dir_copy($source."/".$entry,$destination."/".$entry,$child);
  382. }
  383. }else{
  384. copy($source."/".$entry,$destination."/".$entry);
  385. }
  386. }
  387. }
  388. return true;
  389. }
  390. function showLanguages($return_arr = false)
  391. {
  392. global $app_lang;
  393. $return = $datas = array();
  394. $path = '../languages/';
  395. $handle = opendir($path);
  396. $setting_controller = new PbController();
  397. while(false !== $file=(readdir($handle))){
  398. $dir = $path.$file;
  399. if(is_dir($dir) && !in_array($file, array('.', '..', '.svn'))){
  400. $tmp = "<option value='".$file."'";
  401. if($app_lang==$file) {
  402. $tmp.=" selected='selected'";
  403. }elseif ($_GET['app_lang'] == $file){
  404. $tmp.=" selected='selected'";
  405. }
  406. $templet_file = PHPB2B_ROOT."languages/".$file."/default.dot";
  407. $data = $setting_controller->getSkinData($templet_file);
  408. $tmp.=">".$data['Name']."</option>";
  409. $return[] = $tmp;
  410. $datas[$file]['title'] = $data['Name'];
  411. if(is_file(PHPB2B_ROOT."languages/".$file."/icon.gif"))
  412. $datas[$file]['img'] = "languages/".$file."/icon.gif";
  413. }
  414. }
  415. if($return_arr){
  416. return $datas;
  417. }elseif (!empty($return)) {
  418. return implode("\r\n", $return);
  419. }else{
  420. return false;
  421. }
  422. closedir($handle);
  423. }
  424. function dequote($string)
  425. {
  426. if ((substr($string, 0, 1) == "'" || substr($string, 0, 1) == '"') &&
  427. substr($string, -1) == substr($string, 0, 1))
  428. return substr($string, 1, -1);
  429. else
  430. return $string;
  431. }
  432. ?>