PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/textpattern/setup/index.php

https://bitbucket.org/Manfre/xpattern
PHP | 503 lines | 411 code | 65 blank | 27 comment | 29 complexity | 5442577f964289dcc4eb5f9142752925 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. This is Textpattern
  4. Copyright 2005 by Dean Allen
  5. www.textpattern.com
  6. All rights reserved
  7. Use of this software indicates acceptance of the Textpattern license agreement.
  8. $HeadURL: http://textpattern.googlecode.com/svn/development/4.0/textpattern/setup/index.php $
  9. $LastChangedRevision: 2980 $
  10. */
  11. if (!defined('txpath'))
  12. {
  13. define("txpath", dirname(dirname(__FILE__)));
  14. define("txpinterface", "admin");
  15. }
  16. if (!defined('txpadminfolder'))
  17. define('txpadminfolder', basename(txpath));
  18. error_reporting(E_ALL);
  19. @ini_set("display_errors","1");
  20. include_once txpath.'/lib/constants.php';
  21. include_once txpath.'/lib/txplib_html.php';
  22. include_once txpath.'/lib/txplib_forms.php';
  23. include_once txpath.'/lib/txplib_misc.php';
  24. include_once txpath.'/lib/txplib_admin.php';
  25. header("Content-type: text/html; charset=utf-8");
  26. $adminfoldername = txpadminfolder;
  27. $rel_siteurl = preg_replace('#^(.*)/'.txpadminfolder.'.*$#i','\\1',$_SERVER['PHP_SELF']);
  28. print <<<eod
  29. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  30. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  31. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  32. <head>
  33. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  34. <title>Textpattern &#8250; setup</title>
  35. <link rel="stylesheet" href="$rel_siteurl/{$adminfoldername}/textpattern.css" type="text/css" />
  36. </head>
  37. <body style="border-top:15px solid #FC3">
  38. <div align="center">
  39. eod;
  40. $step = ps('step');
  41. switch ($step) {
  42. case "": chooseLang(); break;
  43. case "getDbInfo": getDbInfo(); break;
  44. case "getTxpLogin": getTxpLogin(); break;
  45. case "printConfig": printConfig(); break;
  46. case "createTxp": createTxp();
  47. }
  48. ?>
  49. </div>
  50. </body>
  51. </html>
  52. <?php
  53. // -------------------------------------------------------------
  54. function chooseLang()
  55. {
  56. echo '<form action="'.$GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/setup/index.php" method="post">',
  57. '<table id="setup" cellpadding="0" cellspacing="0" border="0">',
  58. tr(
  59. tda(
  60. hed('Welcome to Textpattern',3).
  61. graf('Please choose a language:').
  62. langs().
  63. graf(fInput('submit','Submit','Submit','publish')).
  64. sInput('getDbInfo')
  65. ,' width="400" height="50" colspan="4" align="left"')
  66. ),
  67. '</table></form>';
  68. }
  69. // -------------------------------------------------------------
  70. function getDbInfo()
  71. {
  72. $GLOBALS['textarray'] = setup_load_lang(ps('lang'));
  73. @include txpath.'/config.php';
  74. if (!empty($txpcfg['db']))
  75. {
  76. exit(graf(
  77. gTxt('already_installed', array('{txpath}' => txpath))
  78. ));
  79. }
  80. if (@$_SERVER['SCRIPT_NAME'] && (@$_SERVER['SERVER_NAME'] || @$_SERVER['HTTP_HOST']))
  81. {
  82. $guess_siteurl = (@$_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  83. $guess_siteurl .= $GLOBALS['rel_siteurl'];
  84. }
  85. else
  86. {
  87. $guess_siteurl = 'mysite.com';
  88. }
  89. echo '<form action="'.$GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/setup/index.php" method="post">',
  90. '<table id="setup" cellpadding="0" cellspacing="0" border="0">',
  91. tr(
  92. tda(
  93. hed(gTxt('welcome_to_textpattern'),3).
  94. graf(gTxt('need_details'),' style="margin-bottom:3em"').
  95. hed('MySQL',3).
  96. graf(gTxt('db_must_exist'))
  97. ,' width="400" height="50" colspan="4" align="left"')
  98. ),
  99. tr(
  100. fLabelCell(gTxt('mysql_login')).fInputCell('duser','',1).
  101. fLabelCell(gTxt('mysql_password')).fInputCell('dpass','',2)
  102. ),
  103. tr(
  104. fLabelCell(gTxt('mysql_server')).fInputCell('dhost','localhost',3).
  105. fLabelCell(gTxt('mysql_database')).fInputCell('ddb','',4)
  106. ),
  107. tr(
  108. fLabelCell(gTxt('table_prefix')).fInputCell('dprefix','',5).
  109. tdcs(small(gTxt('prefix_warning')),2)
  110. ),
  111. tr(tdcs('&nbsp;',4)),
  112. tr(
  113. tdcs(
  114. hed(gTxt('site_url'),3).
  115. graf(gTxt('please_enter_url')),4)
  116. ),
  117. tr(
  118. fLabelCell('http://').
  119. tdcs(fInput('text','siteurl',$guess_siteurl,'edit','','',40).
  120. popHelp('siteurl'),3)
  121. );
  122. if (is_disabled('mail'))
  123. {
  124. echo tr(
  125. tdcs(gTxt('warn_mail_unavailable'),3,null,'" style="color:red;text-align:center')
  126. );
  127. }
  128. echo tr(
  129. td().td(fInput('submit','Submit',gTxt('next'),'publish')).td().td()
  130. );
  131. echo endTable(),
  132. hInput('lang', LANG),
  133. sInput('printConfig'),
  134. '</form>';
  135. }
  136. // -------------------------------------------------------------
  137. function printConfig()
  138. {
  139. $carry = psa(array('ddb','duser','dpass','dhost','dprefix','siteurl','lang'));
  140. extract($carry);
  141. $GLOBALS['textarray'] = setup_load_lang($lang);
  142. @include txpath.'/config.php';
  143. if (!empty($txpcfg['db']))
  144. {
  145. exit(graf(
  146. gTxt('already_installed', array(
  147. '{txpath}' => txpath
  148. ))
  149. ));
  150. }
  151. // FIXME, remove when all languages are updated with this string
  152. if (!isset($GLOBALS['textarray']['prefix_bad_characters']))
  153. $GLOBALS['textarray']['prefix_bad_characters'] =
  154. 'The Table prefix {dbprefix} contains characters that are not allowed.<br />'.
  155. 'The first character must match one of <b>a-zA-Z_</b> and all following
  156. characters must match one of <b>a-zA-Z0-9_</b>';
  157. echo graf(gTxt("checking_database"));
  158. if (($mylink = mysql_connect($dhost, $duser, $dpass)))
  159. $carry['dclient_flags'] = 0;
  160. elseif (($mylink = mysql_connect($dhost, $duser, $dpass, false, MYSQL_CLIENT_SSL)))
  161. $carry['dclient_flags'] = 'MYSQL_CLIENT_SSL';
  162. else
  163. exit(graf(gTxt('db_cant_connect')));
  164. echo graf(gTxt('db_connected'));
  165. if (! ($dprefix == '' || preg_match('#^[a-zA-Z_][a-zA-Z0-9_]*$#', $dprefix)) )
  166. {
  167. exit(graf(
  168. gTxt('prefix_bad_characters', array(
  169. '{dbprefix}' => strong($dprefix)
  170. ))
  171. ));
  172. }
  173. if (!$mydb = mysql_select_db($ddb))
  174. {
  175. exit(graf(
  176. gTxt('db_doesnt_exist', array(
  177. '{dbname}' => strong($ddb)
  178. ))
  179. ));
  180. }
  181. // use utf8-tables
  182. if (mysql_query("SET NAMES utf8"))
  183. {
  184. $carry['dbcharset'] = "utf8";
  185. }
  186. else
  187. {
  188. $carry['dbcharset'] = "latin1";
  189. }
  190. $config = makeConfig($carry);
  191. $encoded_carry = postEncode($carry);
  192. if (!file_put_contents( txpath.'/config.php', $config ))
  193. {
  194. echo graf(
  195. gTxt('using_db', array('{dbname}' => strong($ddb)))
  196. .' ('. $carry['dbcharset'] .')'
  197. ),
  198. graf(
  199. strong(gTxt('before_you_proceed')).', '.gTxt('create_config', array('{txpath}' => txpath))
  200. ),
  201. '<textarea name="config" cols="40" rows="5" style="width: 400px; height: 200px">',
  202. $config,
  203. '</textarea>',
  204. '<form action="'.$GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/setup/index.php" method="post">',
  205. fInput('submit','submit',gTxt('did_it'),'smallbox'),
  206. sInput('getTxpLogin'),hInput('carry',$encoded_carry),
  207. '</form>';
  208. }
  209. else
  210. {
  211. getTxpLogin($carry);
  212. }
  213. }
  214. // -------------------------------------------------------------
  215. function getTxpLogin($carry=array())
  216. {
  217. if (empty($carry))
  218. {
  219. $carry = postDecode(ps('carry'));
  220. }
  221. extract($carry);
  222. $GLOBALS['textarray'] = setup_load_lang($lang);
  223. @include txpath.'/config.php';
  224. if (!isset($txpcfg) || ($txpcfg['db'] != $ddb) || ($txpcfg['table_prefix'] != $dprefix))
  225. {
  226. echo graf(
  227. strong(gTxt('before_you_proceed')).', '.
  228. gTxt('create_config', array(
  229. '{txpath}' => txpath
  230. ))
  231. ),
  232. '<textarea style="width:400px;height:200px" name="config" rows="1" cols="1">',
  233. makeConfig($carry),
  234. '</textarea>',
  235. '<form action="'.$GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/setup/index.php" method="post">',
  236. fInput('submit','submit',gTxt('did_it'),'smallbox'),
  237. sInput('getTxpLogin'),hInput('carry',postEncode($carry)),
  238. '</form>';
  239. return;
  240. }
  241. echo '<form action="'.$GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/setup/index.php" method="post">',
  242. startTable('edit'),
  243. tr(
  244. tda(
  245. graf(gTxt('thanks')).
  246. graf(gTxt('about_to_create'))
  247. ,' width="400" colspan="2" align="center"')
  248. ),
  249. tr(
  250. fLabelCell(gTxt('your_full_name')).fInputCell('RealName')
  251. ),
  252. tr(
  253. fLabelCell(gTxt('setup_login')).fInputCell('name')
  254. ),
  255. tr(
  256. fLabelCell(gTxt('choose_password')).fInputCell('pass')
  257. ),
  258. tr(
  259. fLabelCell(gTxt('your_email')).fInputCell('email')
  260. ),
  261. tr(
  262. td().td(fInput('submit','Submit',gTxt('next'),'publish'))
  263. ),
  264. endTable(),
  265. sInput('createTxp'),
  266. hInput('lang', htmlspecialchars($lang)),
  267. hInput('siteurl', htmlspecialchars($siteurl)),
  268. '</form>';
  269. }
  270. // -------------------------------------------------------------
  271. function createTxp()
  272. {
  273. $GLOBALS['textarray'] = setup_load_lang(ps('lang'));
  274. if (!is_valid_email(ps('email')))
  275. {
  276. exit(graf(gTxt('email_required')));
  277. }
  278. require txpath.'/config.php';
  279. $ddb = $txpcfg['db'];
  280. $duser = $txpcfg['user'];
  281. $dpass = $txpcfg['pass'];
  282. $dhost = $txpcfg['host'];
  283. $dclient_flags = isset($txpcfg['client_flags']) ? $txpcfg['client_flags'] : 0;
  284. $dprefix = $txpcfg['table_prefix'];
  285. $dbcharset = $txpcfg['dbcharset'];
  286. $siteurl = str_replace("http://",'', ps('siteurl'));
  287. $siteurl = rtrim($siteurl,"/");
  288. $urlpath = preg_replace('#^[^/]+#', '', $siteurl);
  289. define("PFX",trim($dprefix));
  290. define('TXP_INSTALL', 1);
  291. $def_hash_alg = 'sha256';
  292. include_once txpath.'/lib/txplib_update.php';
  293. include txpath.'/setup/txpsql.php';
  294. // This has to come after txpsql.php, because otherwise we can't call mysql_real_escape_string
  295. extract(doSlash(psa(array('name','pass','RealName','email'))));
  296. $nonce = md5( uniqid( rand(), true ) );
  297. $hashpw = doSlash(xpat_hash($def_hash_alg,$pass));
  298. $def_hash_alg = doSlash($def_hash_alg);
  299. mysql_query("INSERT INTO `".PFX."txp_users` VALUES
  300. (1,'$name','$def_hash_alg','$hashpw','$RealName','$email',1,now(),'$nonce')");
  301. mysql_query("update `".PFX."txp_prefs` set val = '".doSlash($siteurl)."' where `name`='siteurl'");
  302. mysql_query("update `".PFX."txp_prefs` set val = '".LANG."' where `name`='language'");
  303. mysql_query("update `".PFX."txp_prefs` set val = '".getlocale(LANG)."' where `name`='locale'");
  304. mysql_query("update `".PFX."textpattern` set Body = replace(Body, 'siteurl', '".doSlash($urlpath)."'), Body_html = replace(Body_html, 'siteurl', '".doSlash($urlpath)."') WHERE ID = 1");
  305. mysql_query("update `".PFX."txp_prefs` set val = '".$email."' where `name`='admin_email'");
  306. echo fbCreate();
  307. }
  308. // -------------------------------------------------------------
  309. function makeConfig($ar)
  310. {
  311. define("nl","';\n");
  312. define("c",'$txpcfg[\'');
  313. define("m","'] = '");
  314. $open = chr(60).'?php';
  315. $close = '?'.chr(62);
  316. extract($ar);
  317. return
  318. $open."\n"
  319. .c.'db' .m.$ddb.nl
  320. .c.'user' .m.$duser.nl
  321. .c.'pass' .m.$dpass.nl
  322. .c.'host' .m.$dhost.nl
  323. .($dclient_flags ? c.'client_flags'."'] = ".$dclient_flags.";\n" : '')
  324. .c.'table_prefix' .m.$dprefix.nl
  325. .c.'txpath' .m.txpath.nl // remove in crockery
  326. .c.'dbcharset' .m.$dbcharset.nl
  327. .$close;
  328. }
  329. // -------------------------------------------------------------
  330. function fbCreate()
  331. {
  332. if ($GLOBALS['txp_install_successful'] === false)
  333. {
  334. return '<div width="450" valign="top" style="margin-right: auto; margin-left: auto;">'.
  335. graf(
  336. gTxt('errors_during_install', array(
  337. '{num}' => $GLOBALS['txp_err_count']
  338. ))
  339. ,' style="margin-top: 3em;"').
  340. '</div>';
  341. }
  342. else
  343. {
  344. return '<div width="450" valign="top" style="margin-right: auto; margin-left: auto;">'.
  345. graf(
  346. gTxt('that_went_well')
  347. ,' style="margin-top:3em"').
  348. graf(
  349. gTxt('you_can_access', array(
  350. 'index.php' => $GLOBALS['rel_siteurl'].'/'.txpadminfolder.'/index.php',
  351. ))
  352. ).
  353. graf(gTxt('thanks_for_interest')).
  354. '</div>';
  355. }
  356. }
  357. // -------------------------------------------------------------
  358. function postEncode($thing)
  359. {
  360. return base64_encode(serialize($thing));
  361. }
  362. // -------------------------------------------------------------
  363. function postDecode($thing)
  364. {
  365. return unserialize(base64_decode($thing));
  366. }
  367. // -------------------------------------------------------------
  368. function langs()
  369. {
  370. global $langs;
  371. $langs = array(
  372. 'ar-dz' => '?????? ????',
  373. 'bg-bg' => '?????????',
  374. 'ca-es' => 'Català',
  375. 'cs-cz' => '?eština',
  376. 'da-dk' => 'Dansk',
  377. 'de-de' => 'Deutsch',
  378. 'el-gr' => '????????',
  379. 'en-gb' => 'English (Great Britain)',
  380. 'en-us' => 'English (United States)',
  381. 'es-es' => 'Español',
  382. 'et-ee' => 'Eesti',
  383. 'fi-fi' => 'Suomi',
  384. 'fr-fr' => 'Français',
  385. 'gl-gz' => 'Galego',
  386. 'he-il' => '?????',
  387. 'hr-hr' => 'Hrvatski',
  388. 'hu-hu' => 'Magyar',
  389. 'id-id' => 'Bahasa Indonesia',
  390. 'is-is' => 'Íslenska',
  391. 'it-it' => 'Italiano',
  392. 'ja-jp' => '???',
  393. 'ko-kr' => '??? (????)',
  394. 'lv-lv' => 'Latviešu',
  395. 'nl-nl' => 'Nederlands',
  396. 'no-no' => 'Norsk',
  397. 'pl-pl' => 'Polski',
  398. 'pt-br' => 'Português (Brasil)',
  399. 'pt-pt' => 'Português (Portugal)',
  400. 'ro-ro' => 'Român?',
  401. 'ru-ru' => '???????',
  402. 'sk-sk' => 'Sloven?ina',
  403. 'sp-rs' => 'Srpski',
  404. 'sr-rs' => '??????',
  405. 'sv-se' => 'Svenska',
  406. 'th-th' => '???',
  407. 'tr-tr' => 'Türkçe',
  408. 'uk-ua' => '??????????',
  409. 'vi-vn' => 'Ti?ng Vi?t (Vi?t Nam)',
  410. 'zh-cn' => '??(??)',
  411. 'zh-tw' => '??(??)',
  412. );
  413. $default = 'en-gb';
  414. $out = n.'<select name="lang">';
  415. foreach ($langs as $a => $b)
  416. {
  417. $out .= n.t.'<option value="'.$a.'"'.
  418. ( ($a == $default) ? ' selected="selected"' : '').
  419. '>'.$b.'</option>';
  420. }
  421. $out .= n.'</select>';
  422. return $out;
  423. }
  424. // -------------------------------------------------------------
  425. function setup_load_lang($lang)
  426. {
  427. global $langs;
  428. require_once txpath.'/setup/setup-langs.php';
  429. $lang = (isset($langs[$lang]) && !empty($langs[$lang]))? $lang : 'en-gb';
  430. if (!defined('LANG'))
  431. define('LANG', $lang);
  432. return $langs[LANG];
  433. }
  434. ?>