install.php 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.1 Alpha 1
  11. */
  12. $GLOBALS['current_smf_version'] = '2.1 Alpha 1';
  13. $GLOBALS['db_script_version'] = '2-1';
  14. $GLOBALS['required_php_version'] = '5.0.0';
  15. // Don't have PHP support, do you?
  16. // ><html dir="ltr"><head><title>Error!</title></head><body>Sorry, this installer requires PHP!<div style="display: none;">
  17. // Database info.
  18. $databases = array(
  19. 'mysql' => array(
  20. 'name' => 'MySQL',
  21. 'version' => '4.0.18',
  22. 'version_check' => 'return min(mysql_get_server_info(), mysql_get_client_info());',
  23. 'supported' => function_exists('mysql_connect'),
  24. 'default_user' => 'mysql.default_user',
  25. 'default_password' => 'mysql.default_password',
  26. 'default_host' => 'mysql.default_host',
  27. 'default_port' => 'mysql.default_port',
  28. 'utf8_support' => true,
  29. 'utf8_version' => '4.1.0',
  30. 'utf8_version_check' => 'return mysql_get_server_info();',
  31. 'utf8_default' => false,
  32. 'utf8_required' => false,
  33. 'alter_support' => true,
  34. 'validate_prefix' => create_function('&$value', '
  35. $value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
  36. return true;
  37. '),
  38. ),
  39. 'postgresql' => array(
  40. 'name' => 'PostgreSQL',
  41. 'version' => '8.0',
  42. 'function_check' => 'pg_connect',
  43. 'version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
  44. 'supported' => function_exists('pg_connect'),
  45. 'always_has_db' => true,
  46. 'utf8_default' => true,
  47. 'utf8_required' => true,
  48. 'utf8_support' => true,
  49. 'utf8_version' => '8.0',
  50. 'utf8_version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
  51. 'validate_prefix' => create_function('&$value', '
  52. $value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
  53. // Is it reserved?
  54. if ($value == \'pg_\')
  55. return $txt[\'error_db_prefix_reserved\'];
  56. // Is the prefix numeric?
  57. if (preg_match(\'~^\d~\', $value))
  58. return $txt[\'error_db_prefix_numeric\'];
  59. return true;
  60. '),
  61. ),
  62. 'sqlite' => array(
  63. 'name' => 'SQLite',
  64. 'version' => '1',
  65. 'function_check' => 'sqlite_open',
  66. 'version_check' => 'return 1;',
  67. 'supported' => function_exists('sqlite_open'),
  68. 'always_has_db' => true,
  69. 'utf8_default' => true,
  70. 'utf8_required' => true,
  71. 'validate_prefix' => create_function('&$value', '
  72. global $incontext, $txt;
  73. $value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
  74. // Is it reserved?
  75. if ($value == \'sqlite_\')
  76. return $txt[\'error_db_prefix_reserved\'];
  77. // Is the prefix numeric?
  78. if (preg_match(\'~^\d~\', $value))
  79. return $txt[\'error_db_prefix_numeric\'];
  80. return true;
  81. '),
  82. ),
  83. );
  84. // Initialize everything and load the language files.
  85. initialize_inputs();
  86. load_lang_file();
  87. // This is what we are.
  88. $installurl = $_SERVER['PHP_SELF'];
  89. // This is where SMF is.
  90. $smfsite = 'http://www.simplemachines.org/smf';
  91. // All the steps in detail.
  92. // Number,Name,Function,Progress Weight.
  93. $incontext['steps'] = array(
  94. 0 => array(1, $txt['install_step_welcome'], 'Welcome', 0),
  95. 1 => array(2, $txt['install_step_writable'], 'CheckFilesWritable', 10),
  96. 2 => array(3, $txt['install_step_databaseset'], 'DatabaseSettings', 15),
  97. 3 => array(4, $txt['install_step_forum'], 'ForumSettings', 40),
  98. 4 => array(5, $txt['install_step_databasechange'], 'DatabasePopulation', 15),
  99. 5 => array(6, $txt['install_step_admin'], 'AdminAccount', 20),
  100. 6 => array(7, $txt['install_step_delete'], 'DeleteInstall', 0),
  101. );
  102. // Default title...
  103. $incontext['page_title'] = $txt['smf_installer'];
  104. // What step are we on?
  105. $incontext['current_step'] = isset($_GET['step']) ? (int) $_GET['step'] : 0;
  106. // Loop through all the steps doing each one as required.
  107. $incontext['overall_percent'] = 0;
  108. foreach ($incontext['steps'] as $num => $step)
  109. {
  110. if ($num >= $incontext['current_step'])
  111. {
  112. // The current weight of this step in terms of overall progress.
  113. $incontext['step_weight'] = $step[3];
  114. // Make sure we reset the skip button.
  115. $incontext['skip'] = false;
  116. // Call the step and if it returns false that means pause!
  117. if (function_exists($step[2]) && $step[2]() === false)
  118. break;
  119. elseif (function_exists($step[2]))
  120. $incontext['current_step']++;
  121. // No warnings pass on.
  122. $incontext['warning'] = '';
  123. }
  124. $incontext['overall_percent'] += $step[3];
  125. }
  126. // Actually do the template stuff.
  127. installExit();
  128. function initialize_inputs()
  129. {
  130. global $databases, $incontext;
  131. // Just so people using older versions of PHP aren't left in the cold.
  132. if (!isset($_SERVER['PHP_SELF']))
  133. $_SERVER['PHP_SELF'] = isset($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF']) ? $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] : 'install.php';
  134. // Turn off magic quotes runtime and enable error reporting.
  135. if (function_exists('set_magic_quotes_runtime'))
  136. @set_magic_quotes_runtime(0);
  137. error_reporting(E_ALL);
  138. // Fun. Low PHP version...
  139. if (!isset($_GET))
  140. {
  141. $GLOBALS['_GET']['step'] = 0;
  142. return;
  143. }
  144. if (!isset($_GET['obgz']))
  145. {
  146. ob_start();
  147. if (ini_get('session.save_handler') == 'user')
  148. @ini_set('session.save_handler', 'files');
  149. if (function_exists('session_start'))
  150. @session_start();
  151. }
  152. else
  153. {
  154. ob_start('ob_gzhandler');
  155. if (ini_get('session.save_handler') == 'user')
  156. @ini_set('session.save_handler', 'files');
  157. session_start();
  158. if (!headers_sent())
  159. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  160. <html xmlns="http://www.w3.org/1999/xhtml">
  161. <head>
  162. <title>', htmlspecialchars($_GET['pass_string']), '</title>
  163. </head>
  164. <body style="background-color: #d4d4d4; margin-top: 16%; text-align: center; font-size: 16pt;">
  165. <strong>', htmlspecialchars($_GET['pass_string']), '</strong>
  166. </body>
  167. </html>';
  168. exit;
  169. }
  170. // Are we calling the backup css file?
  171. if (isset($_GET['infile_css']))
  172. {
  173. header('Content-Type: text/css');
  174. template_css();
  175. exit;
  176. }
  177. // Anybody home?
  178. if (!isset($_GET['xml']))
  179. {
  180. $incontext['remote_files_available'] = false;
  181. $test = @fsockopen('www.simplemachines.org', 80, $errno, $errstr, 1);
  182. if ($test)
  183. $incontext['remote_files_available'] = true;
  184. @fclose($test);
  185. }
  186. // Add slashes, as long as they aren't already being added.
  187. if (!function_exists('get_magic_quotes_gpc') || @get_magic_quotes_gpc() == 0)
  188. foreach ($_POST as $k => $v)
  189. $_POST[$k] = addslashes($v);
  190. // This is really quite simple; if ?delete is on the URL, delete the installer...
  191. if (isset($_GET['delete']))
  192. {
  193. if (isset($_SESSION['installer_temp_ftp']))
  194. {
  195. $ftp = new ftp_connection($_SESSION['installer_temp_ftp']['server'], $_SESSION['installer_temp_ftp']['port'], $_SESSION['installer_temp_ftp']['username'], $_SESSION['installer_temp_ftp']['password']);
  196. $ftp->chdir($_SESSION['installer_temp_ftp']['path']);
  197. $ftp->unlink('install.php');
  198. $ftp->unlink('webinstall.php');
  199. foreach ($databases as $key => $dummy)
  200. $ftp->unlink('install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
  201. $ftp->close();
  202. unset($_SESSION['installer_temp_ftp']);
  203. }
  204. else
  205. {
  206. @unlink(__FILE__);
  207. @unlink(dirname(__FILE__) . '/webinstall.php');
  208. foreach ($databases as $key => $dummy)
  209. @unlink(dirname(__FILE__) . '/install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
  210. }
  211. // Now just redirect to a blank.gif...
  212. header('Location: http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/Themes/default/images/blank.gif');
  213. exit;
  214. }
  215. // PHP 5 might cry if we don't do this now.
  216. if (function_exists('date_default_timezone_set'))
  217. {
  218. $server_offset = @mktime(0, 0, 0, 1, 1, 1970);
  219. date_default_timezone_set('Etc/GMT' . ($server_offset > 0 ? '+' : '') . ($server_offset / 3600));
  220. }
  221. // Force an integer step, defaulting to 0.
  222. $_GET['step'] = (int) @$_GET['step'];
  223. }
  224. // Load the list of language files, and the current language file.
  225. function load_lang_file()
  226. {
  227. global $txt, $incontext;
  228. $incontext['detected_languages'] = array();
  229. // Make sure the languages directory actually exists.
  230. if (file_exists(dirname(__FILE__) . '/Themes/default/languages'))
  231. {
  232. // Find all the "Install" language files in the directory.
  233. $dir = dir(dirname(__FILE__) . '/Themes/default/languages');
  234. while ($entry = $dir->read())
  235. {
  236. if (substr($entry, 0, 8) == 'Install.' && substr($entry, -4) == '.php')
  237. $incontext['detected_languages'][$entry] = ucfirst(substr($entry, 8, strlen($entry) - 12));
  238. }
  239. $dir->close();
  240. }
  241. // Didn't find any, show an error message!
  242. if (empty($incontext['detected_languages']))
  243. {
  244. // Let's not cache this message, eh?
  245. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  246. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  247. header('Cache-Control: no-cache');
  248. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  249. <html xmlns="http://www.w3.org/1999/xhtml">
  250. <head>
  251. <title>SMF Installer: Error!</title>
  252. </head>
  253. <body style="font-family: sans-serif;"><div style="width: 600px;">
  254. <h1 style="font-size: 14pt;">A critical error has occurred.</h1>
  255. <p>This installer was unable to find the installer\'s language file or files. They should be found under:</p>
  256. <div style="margin: 1ex; font-family: monospace; font-weight: bold;">', dirname($_SERVER['PHP_SELF']) != '/' ? dirname($_SERVER['PHP_SELF']) : '', '/Themes/default/languages</div>
  257. <p>In some cases, FTP clients do not properly upload files with this many folders. Please double check to make sure you <span style="font-weight: 600;">have uploaded all the files in the distribution</span>.</p>
  258. <p>If that doesn\'t help, please make sure this install.php file is in the same place as the Themes folder.</p>
  259. <p>If you continue to get this error message, feel free to <a href="http://support.simplemachines.org/">look to us for support</a>.</p>
  260. </div></body>
  261. </html>';
  262. die;
  263. }
  264. // Override the language file?
  265. if (isset($_GET['lang_file']))
  266. $_SESSION['installer_temp_lang'] = $_GET['lang_file'];
  267. elseif (isset($GLOBALS['HTTP_GET_VARS']['lang_file']))
  268. $_SESSION['installer_temp_lang'] = $GLOBALS['HTTP_GET_VARS']['lang_file'];
  269. // Make sure it exists, if it doesn't reset it.
  270. if (!isset($_SESSION['installer_temp_lang']) || preg_match('~[^\\w_\\-.]~', $_SESSION['installer_temp_lang']) === 1 || !file_exists(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']))
  271. {
  272. // Use the first one...
  273. list ($_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
  274. // If we have english and some other language, use the other language. We Americans hate english :P.
  275. if ($_SESSION['installer_temp_lang'] == 'Install.english.php' && count($incontext['detected_languages']) > 1)
  276. list (, $_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
  277. }
  278. // And now include the actual language file itself.
  279. require_once(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']);
  280. }
  281. // This handy function loads some settings and the like.
  282. function load_database()
  283. {
  284. global $db_prefix, $db_connection, $db_character_set, $sourcedir, $language;
  285. global $smcFunc, $mbname, $scripturl, $boardurl, $modSettings, $db_type, $db_name, $db_user;
  286. if (empty($sourcedir))
  287. $sourcedir = dirname(__FILE__) . '/Sources';
  288. // Need this to check whether we need the database password.
  289. require(dirname(__FILE__) . '/Settings.php');
  290. if (!defined('SMF'))
  291. define('SMF', 1);
  292. if (empty($smcFunc))
  293. $smcFunc = array();
  294. $modSettings['disableQueryCheck'] = true;
  295. // Connect the database.
  296. if (!$db_connection)
  297. {
  298. require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
  299. if (version_compare(PHP_VERSION, '5', '<'))
  300. require_once($sourcedir . '/Subs-Compat.php');
  301. if (!$db_connection)
  302. $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('persist' => $db_persist));
  303. }
  304. }
  305. // This is called upon exiting the installer, for template etc.
  306. function installExit($fallThrough = false)
  307. {
  308. global $incontext, $installurl, $txt;
  309. // Send character set.
  310. header('Content-Type: text/html; charset=' . (isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'ISO-8859-1'));
  311. // We usually dump our templates out.
  312. if (!$fallThrough)
  313. {
  314. // The top install bit.
  315. template_install_above();
  316. // Call the template.
  317. if (isset($incontext['sub_template']))
  318. {
  319. $incontext['form_url'] = $installurl . '?step=' . $incontext['current_step'];
  320. call_user_func('template_' . $incontext['sub_template']);
  321. }
  322. // @todo REMOVE THIS!!
  323. else
  324. {
  325. if (function_exists('doStep' . $_GET['step']))
  326. call_user_func('doStep' . $_GET['step']);
  327. }
  328. // Show the footer.
  329. template_install_below();
  330. }
  331. // Bang - gone!
  332. die();
  333. }
  334. function Welcome()
  335. {
  336. global $incontext, $txt, $databases, $installurl;
  337. $incontext['page_title'] = $txt['install_welcome'];
  338. $incontext['sub_template'] = 'welcome_message';
  339. // Done the submission?
  340. if (isset($_POST['contbutt']))
  341. return true;
  342. // Check the PHP version.
  343. if ((!function_exists('version_compare') || version_compare($GLOBALS['required_php_version'], PHP_VERSION, '>')))
  344. {
  345. $incontext['warning'] = $txt['error_php_too_low'];
  346. }
  347. // See if we think they have already installed it?
  348. if (is_readable(dirname(__FILE__) . '/Settings.php'))
  349. {
  350. $probably_installed = 0;
  351. foreach (file(dirname(__FILE__) . '/Settings.php') as $line)
  352. {
  353. if (preg_match('~^\$db_passwd\s=\s\'([^\']+)\';$~', $line))
  354. $probably_installed++;
  355. if (preg_match('~^\$boardurl\s=\s\'([^\']+)\';~', $line) && !preg_match('~^\$boardurl\s=\s\'http://127\.0\.0\.1/smf\';~', $line))
  356. $probably_installed++;
  357. }
  358. if ($probably_installed == 2)
  359. $incontext['warning'] = $txt['error_already_installed'];
  360. }
  361. // Is some database support even compiled in?
  362. $incontext['supported_databases'] = array();
  363. foreach ($databases as $key => $db)
  364. {
  365. if ($db['supported'])
  366. {
  367. if (!file_exists(dirname(__FILE__) . '/install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql'))
  368. {
  369. $databases[$key]['supported'] = false;
  370. $notFoundSQLFile = true;
  371. $txt['error_db_script_missing'] = sprintf($txt['error_db_script_missing'], 'install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
  372. }
  373. else
  374. {
  375. $db_type = $key;
  376. $incontext['supported_databases'][] = $db;
  377. }
  378. }
  379. }
  380. if (empty($incontext['supported_databases']))
  381. $error = empty($notFoundSQLFile) ? 'error_db_missing' : 'error_db_script_missing';
  382. // How about session support? Some crazy sysadmin remove it?
  383. elseif (!function_exists('session_start'))
  384. $error = 'error_session_missing';
  385. // Make sure they uploaded all the files.
  386. elseif (!file_exists(dirname(__FILE__) . '/index.php'))
  387. $error = 'error_missing_files';
  388. // Very simple check on the session.save_path for Windows.
  389. // @todo Move this down later if they don't use database-driven sessions?
  390. elseif (@ini_get('session.save_path') == '/tmp' && substr(__FILE__, 1, 2) == ':\\')
  391. $error = 'error_session_save_path';
  392. // Since each of the three messages would look the same, anyway...
  393. if (isset($error))
  394. $incontext['error'] = $txt[$error];
  395. // Mod_security blocks everything that smells funny. Let SMF handle security.
  396. if (!fixModSecurity() && !isset($_GET['overmodsecurity']))
  397. $incontext['error'] = $txt['error_mod_security'] . '<br /><br /><a href="' . $installurl . '?overmodsecurity=true">' . $txt['error_message_click'] . '</a> ' . $txt['error_message_bad_try_again'];
  398. return false;
  399. }
  400. function CheckFilesWritable()
  401. {
  402. global $txt, $incontext;
  403. $incontext['page_title'] = $txt['ftp_checking_writable'];
  404. $incontext['sub_template'] = 'chmod_files';
  405. $writable_files = array(
  406. 'attachments',
  407. 'avatars',
  408. 'cache',
  409. 'Packages',
  410. 'Packages/installed.list',
  411. 'Smileys',
  412. 'Themes',
  413. 'agreement.txt',
  414. 'Settings.php',
  415. 'Settings_bak.php'
  416. );
  417. $extra_files = array(
  418. 'Themes/classic/index.template.php',
  419. 'Themes/classic/style.css'
  420. );
  421. foreach ($incontext['detected_languages'] as $lang => $temp)
  422. $extra_files[] = 'Themes/default/languages/' . $lang;
  423. // With mod_security installed, we could attempt to fix it with .htaccess.
  424. if (function_exists('apache_get_modules') && in_array('mod_security', apache_get_modules()))
  425. $writable_files[] = file_exists(dirname(__FILE__) . '/.htaccess') ? '.htaccess' : '.';
  426. $failed_files = array();
  427. // On linux, it's easy - just use is_writable!
  428. if (substr(__FILE__, 1, 2) != ':\\')
  429. {
  430. foreach ($writable_files as $file)
  431. {
  432. if (!is_writable(dirname(__FILE__) . '/' . $file))
  433. {
  434. @chmod(dirname(__FILE__) . '/' . $file, 0755);
  435. // Well, 755 hopefully worked... if not, try 777.
  436. if (!is_writable(dirname(__FILE__) . '/' . $file) && !@chmod(dirname(__FILE__) . '/' . $file, 0777))
  437. $failed_files[] = $file;
  438. }
  439. }
  440. foreach ($extra_files as $file)
  441. @chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
  442. }
  443. // Windows is trickier. Let's try opening for r+...
  444. else
  445. {
  446. foreach ($writable_files as $file)
  447. {
  448. // Folders can't be opened for write... but the index.php in them can ;)
  449. if (is_dir(dirname(__FILE__) . '/' . $file))
  450. $file .= '/index.php';
  451. // Funny enough, chmod actually does do something on windows - it removes the read only attribute.
  452. @chmod(dirname(__FILE__) . '/' . $file, 0777);
  453. $fp = @fopen(dirname(__FILE__) . '/' . $file, 'r+');
  454. // Hmm, okay, try just for write in that case...
  455. if (!is_resource($fp))
  456. $fp = @fopen(dirname(__FILE__) . '/' . $file, 'w');
  457. if (!is_resource($fp))
  458. $failed_files[] = $file;
  459. @fclose($fp);
  460. }
  461. foreach ($extra_files as $file)
  462. @chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
  463. }
  464. $failure = count($failed_files) >= 1;
  465. if (!isset($_SERVER))
  466. return !$failure;
  467. // Put the list into context.
  468. $incontext['failed_files'] = $failed_files;
  469. // It's not going to be possible to use FTP on windows to solve the problem...
  470. if ($failure && substr(__FILE__, 1, 2) == ':\\')
  471. {
  472. $incontext['error'] = $txt['error_windows_chmod'] . '
  473. <ul style="margin: 2.5ex; font-family: monospace;">
  474. <li>' . implode('</li>
  475. <li>', $failed_files) . '</li>
  476. </ul>';
  477. return false;
  478. }
  479. // We're going to have to use... FTP!
  480. elseif ($failure)
  481. {
  482. // Load any session data we might have...
  483. if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp']))
  484. {
  485. $_POST['ftp_server'] = $_SESSION['installer_temp_ftp']['server'];
  486. $_POST['ftp_port'] = $_SESSION['installer_temp_ftp']['port'];
  487. $_POST['ftp_username'] = $_SESSION['installer_temp_ftp']['username'];
  488. $_POST['ftp_password'] = $_SESSION['installer_temp_ftp']['password'];
  489. $_POST['ftp_path'] = $_SESSION['installer_temp_ftp']['path'];
  490. }
  491. $incontext['ftp_errors'] = array();
  492. if (isset($_POST['ftp_username']))
  493. {
  494. $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
  495. if ($ftp->error === false)
  496. {
  497. // Try it without /home/abc just in case they messed up.
  498. if (!$ftp->chdir($_POST['ftp_path']))
  499. {
  500. $incontext['ftp_errors'][] = $ftp->last_message;
  501. $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
  502. }
  503. }
  504. }
  505. if (!isset($ftp) || $ftp->error !== false)
  506. {
  507. if (!isset($ftp))
  508. $ftp = new ftp_connection(null);
  509. // Save the error so we can mess with listing...
  510. elseif ($ftp->error !== false && empty($incontext['ftp_errors']) && !empty($ftp->last_message))
  511. $incontext['ftp_errors'][] = $ftp->last_message;
  512. list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__));
  513. if (empty($_POST['ftp_path']) && $found_path)
  514. $_POST['ftp_path'] = $detect_path;
  515. if (!isset($_POST['ftp_username']))
  516. $_POST['ftp_username'] = $username;
  517. // Set the username etc, into context.
  518. $incontext['ftp'] = array(
  519. 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : 'localhost',
  520. 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : '21',
  521. 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : '',
  522. 'path' => isset($_POST['ftp_path']) ? $_POST['ftp_path'] : '/',
  523. 'path_msg' => !empty($found_path) ? $txt['ftp_path_found_info'] : $txt['ftp_path_info'],
  524. );
  525. return false;
  526. }
  527. else
  528. {
  529. $_SESSION['installer_temp_ftp'] = array(
  530. 'server' => $_POST['ftp_server'],
  531. 'port' => $_POST['ftp_port'],
  532. 'username' => $_POST['ftp_username'],
  533. 'password' => $_POST['ftp_password'],
  534. 'path' => $_POST['ftp_path']
  535. );
  536. $failed_files_updated = array();
  537. foreach ($failed_files as $file)
  538. {
  539. if (!is_writable(dirname(__FILE__) . '/' . $file))
  540. $ftp->chmod($file, 0755);
  541. if (!is_writable(dirname(__FILE__) . '/' . $file))
  542. $ftp->chmod($file, 0777);
  543. if (!is_writable(dirname(__FILE__) . '/' . $file))
  544. {
  545. $failed_files_updated[] = $file;
  546. $incontext['ftp_errors'][] = rtrim($ftp->last_message) . ' -> ' . $file . "\n";
  547. }
  548. }
  549. $ftp->close();
  550. // Are there any errors left?
  551. if (count($failed_files_updated) >= 1)
  552. {
  553. // Guess there are...
  554. $incontext['failed_files'] = $failed_files_updated;
  555. // Set the username etc, into context.
  556. $incontext['ftp'] = $_SESSION['installer_temp_ftp'] += array(
  557. 'path_msg' => $txt['ftp_path_info'],
  558. );
  559. return false;
  560. }
  561. }
  562. }
  563. return true;
  564. }
  565. function DatabaseSettings()
  566. {
  567. global $txt, $databases, $incontext, $smcFunc, $sourcedir;
  568. $incontext['sub_template'] = 'database_settings';
  569. $incontext['page_title'] = $txt['db_settings'];
  570. $incontext['continue'] = 1;
  571. // Set up the defaults.
  572. $incontext['db']['server'] = 'localhost';
  573. $incontext['db']['user'] = '';
  574. $incontext['db']['name'] = '';
  575. $incontext['db']['pass'] = '';
  576. $incontext['db']['type'] = '';
  577. $incontext['supported_databases'] = array();
  578. $foundOne = false;
  579. foreach ($databases as $key => $db)
  580. {
  581. // Override with the defaults for this DB if appropriate.
  582. if ($db['supported'])
  583. {
  584. $incontext['supported_databases'][$key] = $db;
  585. if (!$foundOne)
  586. {
  587. if (isset($db['default_host']))
  588. $incontext['db']['server'] = ini_get($db['default_host']) or $incontext['db']['server'] = 'localhost';
  589. if (isset($db['default_user']))
  590. {
  591. $incontext['db']['user'] = ini_get($db['default_user']);
  592. $incontext['db']['name'] = ini_get($db['default_user']);
  593. }
  594. if (isset($db['default_password']))
  595. $incontext['db']['pass'] = ini_get($db['default_password']);
  596. if (isset($db['default_port']))
  597. $db_port = ini_get($db['default_port']);
  598. $incontext['db']['type'] = $key;
  599. $foundOne = true;
  600. }
  601. }
  602. }
  603. // Override for repost.
  604. if (isset($_POST['db_user']))
  605. {
  606. $incontext['db']['user'] = $_POST['db_user'];
  607. $incontext['db']['name'] = $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name'];
  608. $incontext['db']['server'] = $_POST['db_server'];
  609. $incontext['db']['prefix'] = $_POST['db_prefix'];
  610. }
  611. else
  612. $incontext['db']['prefix'] = 'smf_';
  613. // Should we use a non standard port?
  614. if (!empty($db_port))
  615. $incontext['db']['server'] .= ':' . $db_port;
  616. // Are we submitting?
  617. if (isset($_POST['db_type']))
  618. {
  619. if (isset($_POST['db_filename']))
  620. {
  621. // You better enter enter a database name for SQLite.
  622. if (trim($_POST['db_filename']) == '')
  623. {
  624. $incontext['error'] = $txt['error_db_filename'];
  625. return false;
  626. }
  627. // Duplicate name in the same dir? Can't do that with SQLite. Weird things happen.
  628. if (file_exists($_POST['db_filename'] . (substr($_POST['db_filename'], -3) != '.db' ? '.db' : '')))
  629. {
  630. $incontext['error'] = $txt['error_db_filename_exists'];
  631. return false;
  632. }
  633. }
  634. // What type are they trying?
  635. $db_type = preg_replace('~[^A-Za-z0-9]~', '', $_POST['db_type']);
  636. $db_prefix = $_POST['db_prefix'];
  637. // Validate the prefix.
  638. $valid_prefix = $databases[$db_type]['validate_prefix']($db_prefix);
  639. if ($valid_prefix !== true)
  640. {
  641. $incontext['error'] = $valid_prefix;
  642. return false;
  643. }
  644. // Take care of these variables...
  645. $vars = array(
  646. 'db_type' => $db_type,
  647. 'db_name' => $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name'],
  648. 'db_user' => $_POST['db_user'],
  649. 'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '',
  650. 'db_server' => $_POST['db_server'],
  651. 'db_prefix' => $db_prefix,
  652. // The cookiename is special; we want it to be the same if it ever needs to be reinstalled with the same info.
  653. 'cookiename' => 'SMFCookie' . abs(crc32($_POST['db_name'] . preg_replace('~[^A-Za-z0-9_$]~', '', $_POST['db_prefix'])) % 1000),
  654. );
  655. // God I hope it saved!
  656. if (!updateSettingsFile($vars) && substr(__FILE__, 1, 2) == ':\\')
  657. {
  658. $incontext['error'] = $txt['error_windows_chmod'];
  659. return false;
  660. }
  661. // Make sure it works.
  662. require(dirname(__FILE__) . '/Settings.php');
  663. if (empty($sourcedir))
  664. $sourcedir = dirname(__FILE__) . '/Sources';
  665. // Better find the database file!
  666. if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
  667. {
  668. $incontext['error'] = sprintf($txt['error_db_file'], 'Subs-Db-' . $db_type . '.php');
  669. return false;
  670. }
  671. // Now include it for database functions!
  672. define('SMF', 1);
  673. $modSettings['disableQueryCheck'] = true;
  674. if (empty($smcFunc))
  675. $smcFunc = array();
  676. require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
  677. // What - running PHP4? The shame!
  678. if (version_compare(PHP_VERSION, '5', '<'))
  679. require_once($sourcedir . '/Subs-Compat.php');
  680. // Attempt a connection.
  681. $needsDB = !empty($databases[$db_type]['always_has_db']);
  682. $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => !$needsDB));
  683. // No dice? Let's try adding the prefix they specified, just in case they misread the instructions ;)
  684. if ($db_connection == null)
  685. {
  686. $db_error = @$smcFunc['db_error']();
  687. $db_connection = smf_db_initiate($db_server, $db_name, $_POST['db_prefix'] . $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => !$needsDB));
  688. if ($db_connection != null)
  689. {
  690. $db_user = $_POST['db_prefix'] . $db_user;
  691. updateSettingsFile(array('db_user' => $db_user));
  692. }
  693. }
  694. // Still no connection? Big fat error message :P.
  695. if (!$db_connection)
  696. {
  697. $incontext['error'] = $txt['error_db_connect'] . '<div style="margin: 2.5ex; font-family: monospace;"><strong>' . $db_error . '</strong></div>';
  698. return false;
  699. }
  700. // Do they meet the install requirements?
  701. // @todo Old client, new server?
  702. if (version_compare($databases[$db_type]['version'], preg_replace('~^\D*|\-.+?$~', '', eval($databases[$db_type]['version_check']))) > 0)
  703. {
  704. $incontext['error'] = $txt['error_db_too_low'];
  705. return false;
  706. }
  707. // Let's try that database on for size... assuming we haven't already lost the opportunity.
  708. if ($db_name != '' && !$needsDB)
  709. {
  710. $smcFunc['db_query']('', "
  711. CREATE DATABASE IF NOT EXISTS `$db_name`",
  712. array(
  713. 'security_override' => true,
  714. 'db_error_skip' => true,
  715. ),
  716. $db_connection
  717. );
  718. // Okay, let's try the prefix if it didn't work...
  719. if (!$smcFunc['db_select_db']($db_name, $db_connection) && $db_name != '')
  720. {
  721. $smcFunc['db_query']('', "
  722. CREATE DATABASE IF NOT EXISTS `$_POST[db_prefix]$db_name`",
  723. array(
  724. 'security_override' => true,
  725. 'db_error_skip' => true,
  726. ),
  727. $db_connection
  728. );
  729. if ($smcFunc['db_select_db']($_POST['db_prefix'] . $db_name, $db_connection))
  730. {
  731. $db_name = $_POST['db_prefix'] . $db_name;
  732. updateSettingsFile(array('db_name' => $db_name));
  733. }
  734. }
  735. // Okay, now let's try to connect...
  736. if (!$smcFunc['db_select_db']($db_name, $db_connection))
  737. {
  738. $incontext['error'] = sprintf($txt['error_db_database'], $db_name);
  739. return false;
  740. }
  741. }
  742. return true;
  743. }
  744. return false;
  745. }
  746. // Let's start with basic forum type settings.
  747. function ForumSettings()
  748. {
  749. global $txt, $incontext, $databases, $smcFunc, $db_connection, $db_type;
  750. $incontext['sub_template'] = 'forum_settings';
  751. $incontext['page_title'] = $txt['install_settings'];
  752. // Let's see if we got the database type correct.
  753. if (isset($_POST['db_type'], $databases[$_POST['db_type']]))
  754. $db_type = $_POST['db_type'];
  755. // Else we'd better be able to get the connection.
  756. else
  757. load_database();
  758. $db_type = isset($_POST['db_type']) ? $_POST['db_type'] : $db_type;
  759. // What host and port are we on?
  760. $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
  761. // Now, to put what we've learned together... and add a path.
  762. $incontext['detected_url'] = 'http' . (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 's' : '') . '://' . $host . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
  763. // Check if the database sessions will even work.
  764. $incontext['test_dbsession'] = ini_get('session.auto_start') != 1;
  765. $incontext['utf8_should_work'] = stripos(PHP_OS, 'win') !== false;
  766. $incontext['utf8_default'] = $databases[$db_type]['utf8_default'];
  767. $incontext['utf8_required'] = $databases[$db_type]['utf8_required'];
  768. $incontext['continue'] = 1;
  769. // Submitting?
  770. if (isset($_POST['boardurl']))
  771. {
  772. if (substr($_POST['boardurl'], -10) == '/index.php')
  773. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  774. elseif (substr($_POST['boardurl'], -1) == '/')
  775. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  776. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  777. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  778. // Save these variables.
  779. $vars = array(
  780. 'boardurl' => $_POST['boardurl'],
  781. 'boarddir' => addslashes(dirname(__FILE__)),
  782. 'sourcedir' => addslashes(dirname(__FILE__)) . '/Sources',
  783. 'cachedir' => addslashes(dirname(__FILE__)) . '/cache',
  784. 'mbname' => strtr($_POST['mbname'], array('\"' => '"')),
  785. 'language' => substr($_SESSION['installer_temp_lang'], 8, -4),
  786. );
  787. // Must save!
  788. if (!updateSettingsFile($vars) && substr(__FILE__, 1, 2) == ':\\')
  789. {
  790. $incontext['error'] = $txt['error_windows_chmod'];
  791. return false;
  792. }
  793. // Make sure it works.
  794. require(dirname(__FILE__) . '/Settings.php');
  795. // UTF-8 requires a setting to override the language charset.
  796. if (isset($_POST['utf8']) && !empty($databases[$db_type]['utf8_support']))
  797. {
  798. if (version_compare($databases[$db_type]['utf8_version'], preg_replace('~\-.+?$~', '', eval($databases[$db_type]['utf8_version_check'])), '>'))
  799. {
  800. $incontext['error'] = sprintf($txt['error_utf8_version'], $databases[$db_type]['utf8_version']);
  801. return false;
  802. }
  803. else
  804. // Set the character set here.
  805. updateSettingsFile(array('db_character_set' => 'utf8'));
  806. }
  807. // Good, skip on.
  808. return true;
  809. }
  810. return false;
  811. }
  812. // Step one: Do the SQL thang.
  813. function DatabasePopulation()
  814. {
  815. global $db_character_set, $txt, $db_connection, $smcFunc, $databases, $modSettings, $db_type, $sourcedir, $db_prefix, $incontext, $db_name, $boardurl;
  816. $incontext['sub_template'] = 'populate_database';
  817. $incontext['page_title'] = $txt['db_populate'];
  818. $incontext['continue'] = 1;
  819. // Already done?
  820. if (isset($_POST['pop_done']))
  821. return true;
  822. // Reload settings.
  823. require(dirname(__FILE__) . '/Settings.php');
  824. load_database();
  825. // Before running any of the queries, let's make sure another version isn't already installed.
  826. $result = $smcFunc['db_query']('', '
  827. SELECT variable, value
  828. FROM {db_prefix}settings',
  829. array(
  830. 'db_error_skip' => true,
  831. )
  832. );
  833. $modSettings = array();
  834. if ($result !== false)
  835. {
  836. while ($row = $smcFunc['db_fetch_assoc']($result))
  837. $modSettings[$row['variable']] = $row['value'];
  838. $smcFunc['db_free_result']($result);
  839. // Do they match? If so, this is just a refresh so charge on!
  840. if (!isset($modSettings['smfVersion']) || $modSettings['smfVersion'] != $GLOBALS['current_smf_version'])
  841. {
  842. $incontext['error'] = $txt['error_versions_do_not_match'];
  843. return false;
  844. }
  845. }
  846. $modSettings['disableQueryCheck'] = true;
  847. // If doing UTF8, select it. PostgreSQL requires passing it as a string...
  848. if (!empty($db_character_set) && $db_character_set == 'utf8' && !empty($databases[$db_type]['utf8_support']))
  849. $smcFunc['db_query']('', '
  850. SET NAMES {'. ($db_type == 'postgresql' ? 'string' : 'raw') . ':utf8}',
  851. array(
  852. 'db_error_skip' => true,
  853. 'utf8' => 'utf8',
  854. )
  855. );
  856. $replaces = array(
  857. '{$db_prefix}' => $db_prefix,
  858. '{$boarddir}' => $smcFunc['db_escape_string'](dirname(__FILE__)),
  859. '{$boardurl}' => $boardurl,
  860. '{$enableCompressedOutput}' => isset($_POST['compress']) ? '1' : '0',
  861. '{$databaseSession_enable}' => isset($_POST['dbsession']) ? '1' : '0',
  862. '{$smf_version}' => $GLOBALS['current_smf_version'],
  863. '{$current_time}' => time(),
  864. '{$sched_task_offset}' => 82800 + mt_rand(0, 86399),
  865. );
  866. foreach ($txt as $key => $value)
  867. {
  868. if (substr($key, 0, 8) == 'default_')
  869. $replaces['{$' . $key . '}'] = $smcFunc['db_escape_string']($value);
  870. }
  871. $replaces['{$default_reserved_names}'] = strtr($replaces['{$default_reserved_names}'], array('\\\\n' => '\\n'));
  872. // If the UTF-8 setting was enabled, add it to the table definitions.
  873. // @todo Very MySQL specific still
  874. if ($db_type == 'mysql' && isset($_POST['utf8']) && !empty($databases[$db_type]['utf8_support']))
  875. $replaces[') ENGINE=MyISAM;'] = ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;';
  876. // Read in the SQL. Turn this on and that off... internationalize... etc.
  877. $sql_lines = explode("\n", strtr(implode(' ', file(dirname(__FILE__) . '/install_' . $GLOBALS['db_script_version'] . '_' . $db_type . '.sql')), $replaces));
  878. // Execute the SQL.
  879. $current_statement = '';
  880. $exists = array();
  881. $incontext['failures'] = array();
  882. $incontext['sql_results'] = array(
  883. 'tables' => 0,
  884. 'inserts' => 0,
  885. 'table_dups' => 0,
  886. 'insert_dups' => 0,
  887. );
  888. foreach ($sql_lines as $count => $line)
  889. {
  890. // No comments allowed!
  891. if (substr(trim($line), 0, 1) != '#')
  892. $current_statement .= "\n" . rtrim($line);
  893. // Is this the end of the query string?
  894. if (empty($current_statement) || (preg_match('~;[\s]*$~s', $line) == 0 && $count != count($sql_lines)))
  895. continue;
  896. // Does this table already exist? If so, don't insert more data into it!
  897. if (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) != 0 && in_array($match[1], $exists))
  898. {
  899. $incontext['sql_results']['insert_dups']++;
  900. $current_statement = '';
  901. continue;
  902. }
  903. if ($smcFunc['db_query']('', $current_statement, array('security_override' => true, 'db_error_skip' => true), $db_connection) === false)
  904. {
  905. // Error 1050: Table already exists!
  906. // @todo Needs to be made better!
  907. if (($db_type != 'mysql' || mysql_errno($db_connection) === 1050) && preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) == 1)
  908. {
  909. $exists[] = $match[1];
  910. $incontext['sql_results']['table_dups']++;
  911. }
  912. // Don't error on duplicate indexes (or duplicate operators in PostgreSQL.)
  913. elseif (!preg_match('~^\s*CREATE( UNIQUE)? INDEX ([^\n\r]+?)~', $current_statement, $match) && !($db_type == 'postgresql' && preg_match('~^\s*CREATE OPERATOR (^\n\r]+?)~', $current_statement, $match)))
  914. {
  915. $incontext['failures'][$count] = $smcFunc['db_error']();
  916. }
  917. }
  918. else
  919. {
  920. if (preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) == 1)
  921. $incontext['sql_results']['tables']++;
  922. else
  923. {
  924. preg_match_all('~\)[,;]~', $current_statement, $matches);
  925. if (!empty($matches[0]))
  926. $incontext['sql_results']['inserts'] += count($matches[0]);
  927. else
  928. $incontext['sql_results']['inserts']++;
  929. }
  930. }
  931. $current_statement = '';
  932. }
  933. // Sort out the context for the SQL.
  934. foreach ($incontext['sql_results'] as $key => $number)
  935. {
  936. if ($number == 0)
  937. unset($incontext['sql_results'][$key]);
  938. else
  939. $incontext['sql_results'][$key] = sprintf($txt['db_populate_' . $key], $number);
  940. }
  941. // Make sure UTF will be used globally.
  942. if (isset($_POST['utf8']) && !empty($databases[$db_type]['utf8_support']))
  943. $smcFunc['db_insert']('replace',
  944. $db_prefix . 'settings',
  945. array(
  946. 'variable' => 'string-255', 'value' => 'string-65534',
  947. ),
  948. array(
  949. 'global_character_set', 'UTF-8',
  950. ),
  951. array('variable')
  952. );
  953. // Maybe we can auto-detect better cookie settings?
  954. preg_match('~^http[s]?://([^\.]+?)([^/]*?)(/.*)?$~', $boardurl, $matches);
  955. if (!empty($matches))
  956. {
  957. // Default = both off.
  958. $localCookies = false;
  959. $globalCookies = false;
  960. // Okay... let's see. Using a subdomain other than www.? (not a perfect check.)
  961. if ($matches[2] != '' && (strpos(substr($matches[2], 1), '.') === false || in_array($matches[1], array('forum', 'board', 'community', 'forums', 'support', 'chat', 'help', 'talk', 'boards', 'www'))))
  962. $globalCookies = true;
  963. // If there's a / in the middle of the path, or it starts with ~... we want local.
  964. if (isset($matches[3]) && strlen($matches[3]) > 3 && (substr($matches[3], 0, 2) == '/~' || strpos(substr($matches[3], 1), '/') !== false))
  965. $localCookies = true;
  966. if ($globalCookies)
  967. $rows[] = array('globalCookies', '1');
  968. if ($localCookies)
  969. $rows[] = array('localCookies', '1');
  970. if (!empty($rows))
  971. {
  972. $smcFunc['db_insert']('replace',
  973. $db_prefix . 'settings',
  974. array('variable' => 'string-255', 'value' => 'string-65534'),
  975. $rows,
  976. array('variable')
  977. );
  978. }
  979. }
  980. // Are we allowing stat collection?
  981. if (isset($_POST['stats']) && strpos($_POST['boardurl'], 'http://localhost') !== 0)
  982. {
  983. // Attempt to register the site etc.
  984. $fp = @fsockopen("www.simplemachines.org", 80, $errno, $errstr);
  985. if ($fp)
  986. {
  987. $out = "GET /smf/stats/register_stats.php?site=" . base64_encode($_POST['boardurl']) . " HTTP/1.1\r\n";
  988. $out .= "Host: www.simplemachines.org\r\n";
  989. $out .= "Connection: Close\r\n\r\n";
  990. fwrite($fp, $out);
  991. $return_data = '';
  992. while (!feof($fp))
  993. $return_data .= fgets($fp, 128);
  994. fclose($fp);
  995. // Get the unique site ID.
  996. preg_match('~SITE-ID:\s(\w{10})~', $return_data, $ID);
  997. if (!empty($ID[1]))
  998. $smcFunc['db_insert']('',
  999. $db_prefix . 'settings',
  1000. array(
  1001. 'variable' => 'string-255', 'value' => 'string-65534',
  1002. ),
  1003. array(
  1004. 'allow_sm_stats', $ID[1],
  1005. ),
  1006. array('variable')
  1007. );
  1008. }
  1009. }
  1010. // As of PHP 5.1, setting a timezone is required.
  1011. if (!isset($modSettings['default_timezone']) && function_exists('date_default_timezone_set'))
  1012. {
  1013. $server_offset = mktime(0, 0, 0, 1, 1, 1970);
  1014. $timezone_id = 'Etc/GMT' . ($server_offset > 0 ? '+' : '') . ($server_offset / 3600);
  1015. if (date_default_timezone_set($timezone_id))
  1016. $smcFunc['db_insert']('',
  1017. $db_prefix . 'settings',
  1018. array(
  1019. 'variable' => 'string-255', 'value' => 'string-65534',
  1020. ),
  1021. array(
  1022. 'default_timezone', $timezone_id,
  1023. ),
  1024. array('variable')
  1025. );
  1026. }
  1027. // Let's optimize those new tables.
  1028. db_extend();
  1029. $tables = $smcFunc['db_list_tables']($db_name, $db_prefix . '%');
  1030. foreach ($tables as $table)
  1031. {
  1032. $smcFunc['db_optimize_table']($table) != -1 or $db_messed = true;
  1033. // Optimizing one sqlite table, optimizes them all
  1034. if($db_type == 'sqlite')
  1035. break;
  1036. if (!empty($db_messed))
  1037. {
  1038. $incontext['failures'][-1] = $smcFunc['db_error']();
  1039. break;
  1040. }
  1041. }
  1042. // Check for the ALTER privilege.
  1043. if (!empty($databases[$db_type]['alter_support']) && $smcFunc['db_query']('', "ALTER TABLE {$db_prefix}boards ORDER BY id_board", array('security_override' => true, 'db_error_skip' => true)) === false)
  1044. {
  1045. $incontext['error'] = $txt['error_db_alter_priv'];
  1046. return false;
  1047. }
  1048. if (!empty($exists))
  1049. {
  1050. $incontext['page_title'] = $txt['user_refresh_install'];
  1051. $incontext['was_refresh'] = true;
  1052. }
  1053. return false;
  1054. }
  1055. // Ask for the administrator login information.
  1056. function AdminAccount()
  1057. {
  1058. global $txt, $db_type, $db_connection, $databases, $smcFunc, $incontext, $db_prefix, $db_passwd, $sourcedir;
  1059. $incontext['sub_template'] = 'admin_account';
  1060. $incontext['page_title'] = $txt['user_settings'];
  1061. $incontext['continue'] = 1;
  1062. // Skipping?
  1063. if (!empty($_POST['skip']))
  1064. return true;
  1065. // Need this to check whether we need the database password.
  1066. require(dirname(__FILE__) . '/Settings.php');
  1067. load_database();
  1068. // Define the sha1 function, if it doesn't exist.
  1069. if (!function_exists('sha1') || version_compare(PHP_VERSION, '5', '<'))
  1070. require_once($sourcedir . '/Subs-Compat.php');
  1071. if (!isset($_POST['username']))
  1072. $_POST['username'] = '';
  1073. if (!isset($_POST['email']))
  1074. $_POST['email'] = '';
  1075. $incontext['username'] = htmlspecialchars(stripslashes($_POST['username']));
  1076. $incontext['email'] = htmlspecialchars(stripslashes($_POST['email']));
  1077. $incontext['require_db_confirm'] = empty($db_type) || $db_type != 'sqlite';
  1078. // Only allow skipping if we think they already have an account setup.
  1079. $request = $smcFunc['db_query']('', '
  1080. SELECT id_member
  1081. FROM {db_prefix}members
  1082. WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
  1083. LIMIT 1',
  1084. array(
  1085. 'db_error_skip' => true,
  1086. 'admin_group' => 1,
  1087. )
  1088. );
  1089. if ($smcFunc['db_num_rows']($request) != 0)
  1090. $incontext['skip'] = 1;
  1091. $smcFunc['db_free_result']($request);
  1092. // Trying to create an account?
  1093. if (isset($_POST['password1']) && !empty($_POST['contbutt']))
  1094. {
  1095. // Wrong password?
  1096. if ($incontext['require_db_confirm'] && $_POST['password3'] != $db_passwd)
  1097. {
  1098. $incontext['error'] = $txt['error_db_connect'];
  1099. return false;
  1100. }
  1101. // Not matching passwords?
  1102. if ($_POST['password1'] != $_POST['password2'])
  1103. {
  1104. $incontext['error'] = $txt['error_user_settings_again_match'];
  1105. return false;
  1106. }
  1107. // No password?
  1108. if (strlen($_POST['password1']) < 4)
  1109. {
  1110. $incontext['error'] = $txt['error_user_settings_no_password'];
  1111. return false;
  1112. }
  1113. if (!file_exists($sourcedir . '/Subs.php'))
  1114. {
  1115. $incontext['error'] = $txt['error_subs_missing'];
  1116. return false;
  1117. }
  1118. // Update the main contact email?
  1119. if (!empty($_POST['email']) && (empty($webmaster_email) || $webmaster_email == '[email protected]'))
  1120. updateSettingsFile(array('webmaster_email' => $_POST['email']));
  1121. // Work out whether we're going to have dodgy characters and remove them.
  1122. $invalid_characters = preg_match('~[<>&"\'=\\\]~', $_POST['username']) != 0;
  1123. $_POST['username'] = preg_replace('~[<>&"\'=\\\]~', '', $_POST['username']);
  1124. $result = $smcFunc['db_query']('', '
  1125. SELECT id_member, password_salt
  1126. FROM {db_prefix}members
  1127. WHERE member_name = {string:username} OR email_address = {string:email}
  1128. LIMIT 1',
  1129. array(
  1130. 'username' => stripslashes($_POST['username']),
  1131. 'email' => stripslashes($_POST['email']),
  1132. 'db_error_skip' => true,
  1133. )
  1134. );
  1135. if ($smcFunc['db_num_rows']($result) != 0)
  1136. {
  1137. list ($incontext['member_id'], $incontext['member_salt']) = $smcFunc['db_fetch_row']($result);
  1138. $smcFunc['db_free_result']($result);
  1139. $incontext['account_existed'] = $txt['error_user_settings_taken'];
  1140. }
  1141. elseif ($_POST['username'] == '' || strlen($_POST['username']) > 25)
  1142. {
  1143. // Try the previous step again.
  1144. $incontext['error'] = $_POST['username'] == '' ? $txt['error_username_left_empty'] : $txt['error_username_too_long'];
  1145. return false;
  1146. }
  1147. elseif ($invalid_characters || $_POST['username'] == '_' || $_POST['username'] == '|' || strpos($_POST['username'], '[code') !== false || strpos($_POST['username'], '[/code') !== false)
  1148. {
  1149. // Try the previous step again.
  1150. $incontext['error'] = $txt['error_invalid_characters_username'];
  1151. return false;
  1152. }
  1153. elseif (empty($_POST['email']) || preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', stripslashes($_POST['email'])) === 0 || strlen(stripslashes($_POST['email'])) > 255)
  1154. {
  1155. // One step back, this time fill out a proper email address.
  1156. $incontext['error'] = sprintf($txt['error_valid_email_needed'], $_POST['username']);
  1157. return false;
  1158. }
  1159. elseif ($_POST['username'] != '')
  1160. {
  1161. $incontext['member_salt'] = substr(md5(mt_rand()), 0, 4);
  1162. // Format the username properly.
  1163. $_POST['username'] = preg_replace('~[\t\n\r\x0B\0\xA0]+~', ' ', $_POST['username']);
  1164. $ip = isset($_SERVER['REMOTE_ADDR']) ? substr($_SERVER['REMOTE_ADDR'], 0, 255) : '';
  1165. $request = $smcFunc['db_insert']('',
  1166. $db_prefix . 'members',
  1167. array(
  1168. 'member_name' => 'string-25', 'real_name' => 'string-25', 'passwd' => 'string', 'email_address' => 'string',
  1169. 'id_group' => 'int', 'posts' => 'int', 'date_registered' => 'int', 'hide_email' => 'int',
  1170. 'password_salt' => 'string', 'lngfile' => 'string', 'personal_text' => 'string', 'avatar' => 'string',
  1171. 'member_ip' => 'string', 'member_ip2' => 'string', 'buddy_list' => 'string', 'pm_ignore_list' => 'string',
  1172. 'message_labels' => 'string', 'website_title' => 'string', 'website_url' => 'string', 'location' => 'string',
  1173. 'aim' => 'string', 'icq' => 'string', 'msn' => 'string', 'signature' => 'string', 'usertitle' => 'string', 'secret_question' => 'string',
  1174. 'additional_groups' => 'string', 'ignore_boards' => 'string', 'openid_uri' => 'string',
  1175. ),
  1176. array(
  1177. stripslashes($_POST['username']), stripslashes($_POST['username']), sha1(strtolower(stripslashes($_POST['username'])) . stripslashes($_POST['password1'])), stripslashes($_POST['email']),
  1178. 1, 0, time(), 0,
  1179. $incontext['member_salt'], '', '', '',
  1180. $ip, $ip, '', '',
  1181. '', '', '', '',
  1182. '', '', '', '', '', '',
  1183. '', '', '',
  1184. ),
  1185. array('id_member')
  1186. );
  1187. // Awww, crud!
  1188. if ($request === false)
  1189. {
  1190. $incontext['error'] = $txt['error_user_settings_query'] . '<br />
  1191. <div style="margin: 2ex;">' . nl2br(htmlspecialchars($smcFunc['db_error']($db_connection))) . '</div>';
  1192. return false;
  1193. }
  1194. $incontext['member_id'] = $smcFunc['db_insert_id']("{$db_prefix}members", 'id_member');
  1195. }
  1196. // If we're here we're good.
  1197. return true;
  1198. }
  1199. return false;
  1200. }
  1201. // Final step, clean up and a complete message!
  1202. function DeleteInstall()
  1203. {
  1204. global $txt, $db_prefix, $db_connection, $HTTP_SESSION_VARS, $cookiename, $incontext;
  1205. global $smcFunc, $db_character_set, $mbname, $context, $scripturl, $boardurl;
  1206. global $current_smf_version, $databases, $sourcedir, $forum_version, $modSettings, $user_info, $language, $db_type;
  1207. $incontext['page_title'] = $txt['congratulations'];
  1208. $incontext['sub_template'] = 'delete_install';
  1209. $incontext['continue'] = 0;
  1210. require(dirname(__FILE__) . '/Settings.php');
  1211. load_database();
  1212. chdir(dirname(__FILE__));
  1213. require_once($sourcedir . '/Errors.php');
  1214. require_once($sourcedir . '/Logging.php');
  1215. require_once($sourcedir . '/Subs.php');
  1216. require_once($sourcedir . '/Load.php');
  1217. require_once($sourcedir . '/Security.php');
  1218. require_once($sourcedir . '/Subs-Auth.php');
  1219. // Bring a warning over.
  1220. if (!empty($incontext['account_existed']))
  1221. $incontext['warning'] = $incontext['account_existed'];
  1222. if (!empty($db_character_set) && !empty($databases[$db_type]['utf8_support']))
  1223. $smcFunc['db_query']('', '
  1224. SET NAMES {raw:db_character_set}',
  1225. array(
  1226. 'db_character_set' => $db_character_set,
  1227. 'db_error_skip' => true,
  1228. )
  1229. );
  1230. // As track stats is by default enabled let's add some activity.
  1231. $smcFunc['db_insert']('ignore',
  1232. '{db_prefix}log_activity',
  1233. array('date' => 'date', 'topics' => 'int', 'posts' => 'int', 'registers' => 'int'),
  1234. array(strftime('%Y-%m-%d', time()), 1, 1, (!empty($incontext['member_id']) ? 1 : 0)),
  1235. array('date')
  1236. );
  1237. // We're going to want our lovely $modSettings now.
  1238. $request = $smcFunc['db_query']('', '
  1239. SELECT variable, value
  1240. FROM {db_prefix}settings',
  1241. array(
  1242. 'db_error_skip' => true,
  1243. )
  1244. );
  1245. // Only proceed if we can load the data.
  1246. if ($request)
  1247. {
  1248. while ($row = $smcFunc['db_fetch_row']($request))
  1249. $modSettings[$row[0]] = $row[1];
  1250. $smcFunc['db_free_result']($request);
  1251. }
  1252. // Automatically log them in ;)
  1253. if (isset($incontext['member_id']) && isset($incontext['member_salt']))
  1254. setLoginCookie(3153600 * 60, $incontext['member_id'], sha1(sha1(strtolower($_POST['username']) . $_POST['password1']) . $incontext['member_salt']));
  1255. $result = $smcFunc['db_query']('', '
  1256. SELECT value
  1257. FROM {db_prefix}settings
  1258. WHERE variable = {string:db_sessions}',
  1259. array(
  1260. 'db_sessions' => 'databaseSession_enable',
  1261. 'db_error_skip' => true,
  1262. )
  1263. );
  1264. if ($smcFunc['db_num_rows']($result) != 0)
  1265. list ($db_sessions) = $smcFunc['db_fetch_row']($result);
  1266. $smcFunc['db_free_result']($result);
  1267. if (empty($db_sessions))
  1268. $_SESSION['admin_time'] = time();
  1269. else
  1270. {
  1271. $_SERVER['HTTP_USER_AGENT'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 211);
  1272. $smcFunc['db_insert']('replace',
  1273. '{db_prefix}sessions',
  1274. array(
  1275. 'session_id' => 'string', 'last_update' => 'int', 'data' => 'string',
  1276. ),
  1277. array(
  1278. session_id(), time(), 'USER_AGENT|s:' . strlen($_SERVER['HTTP_USER_AGENT']) . ':"' . $_SERVER['HTTP_USER_AGENT'] . '";admin_time|i:' . time() . ';',
  1279. ),
  1280. array('session_id')
  1281. );
  1282. }
  1283. updateStats('member');
  1284. updateStats('message');
  1285. updateStats('topic');
  1286. // This function is needed to do the updateStats('subject') call.
  1287. $smcFunc['strtolower'] = $db_character_set === 'utf8' || $txt['lang_character_set'] === 'UTF-8' ? create_function('$string', '
  1288. return $string;') : 'strtolower';
  1289. $request = $smcFunc['db_query']('', '
  1290. SELECT id_msg
  1291. FROM {db_prefix}messages
  1292. WHERE id_msg = 1
  1293. AND modified_time = 0
  1294. LIMIT 1',
  1295. array(
  1296. 'db_error_skip' => true,
  1297. )
  1298. );
  1299. if ($smcFunc['db_num_rows']($request) > 0)
  1300. updateStats('subject', 1, htmlspecialchars($txt['default_topic_subject']));
  1301. $smcFunc['db_free_result']($request);
  1302. // Now is the perfect time to fetch the SM files.
  1303. require_once($sourcedir . '/ScheduledTasks.php');
  1304. // Sanity check that they loaded earlier!
  1305. if (isset($modSettings['recycle_board']))
  1306. {
  1307. $forum_version = $current_smf_version; // The variable is usually defined in index.php so lets just use our variable to do it for us.
  1308. scheduled_fetchSMfiles(); // Now go get those files!
  1309. // We've just installed!
  1310. $user_info['ip'] = $_SERVER['REMOTE_ADDR'];
  1311. $user_info['id'] = isset($incontext['member_id']) ? $incontext['member_id'] : 0;
  1312. logAction('install', array('version' => $forum_version), 'admin');
  1313. }
  1314. // Check if we need some stupid MySQL fix.
  1315. $server_version = $smcFunc['db_server_info']();
  1316. if ($db_type == 'mysql' && in_array(substr($server_version, 0, 6), array('5.0.50', '5.0.51')))
  1317. updateSettings(array('db_mysql_group_by_fix' => '1'));
  1318. // Some final context for the template.
  1319. $incontext['dir_still_writable'] = is_writable(dirname(__FILE__)) && substr(__FILE__, 1, 2) != ':\\';
  1320. $incontext['probably_delete_install'] = isset($_SESSION['installer_temp_ftp']) || is_writable(dirname(__FILE__)) || is_writable(__FILE__);
  1321. return false;
  1322. }
  1323. // http://www.faqs.org/rfcs/rfc959.html
  1324. class ftp_connection
  1325. {
  1326. var $connection = 'no_connection', $error = false, $last_message, $pasv = array();
  1327. // Create a new FTP connection...
  1328. function ftp_connection($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = '[email protected]')
  1329. {
  1330. if ($ftp_server !== null)
  1331. $this->connect($ftp_server, $ftp_port, $ftp_user, $ftp_pass);
  1332. }
  1333. function connect($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = '[email protected]')
  1334. {
  1335. if (substr($ftp_server, 0, 6) == 'ftp://')
  1336. $ftp_server = substr($ftp_server, 6);
  1337. elseif (substr($ftp_server, 0, 7) == 'ftps://')
  1338. $ftp_server = 'ssl://' . substr($ftp_server, 7);
  1339. if (substr($ftp_server, 0, 7) == 'http://')
  1340. $ftp_server = substr($ftp_server, 7);
  1341. $ftp_server = strtr($ftp_server, array('/' => '', ':' => '', '@' => ''));
  1342. // Connect to the FTP server.
  1343. $this->connection = @fsockopen($ftp_server, $ftp_port, $err, $err, 5);
  1344. if (!$this->connection)
  1345. {
  1346. $this->error = 'bad_server';
  1347. return;
  1348. }
  1349. // Get the welcome message...
  1350. if (!$this->check_response(220))
  1351. {
  1352. $this->error = 'bad_response';
  1353. return;
  1354. }
  1355. // Send the username, it should ask for a password.
  1356. fwrite($this->connection, 'USER ' . $ftp_user . "\r\n");
  1357. if (!$this->check_response(331))
  1358. {
  1359. $this->error = 'bad_username';
  1360. return;
  1361. }
  1362. // Now send the password... and hope it goes okay.
  1363. fwrite($this->connection, 'PASS ' . $ftp_pass . "\r\n");
  1364. if (!$this->check_response(230))
  1365. {
  1366. $this->error = 'bad_password';
  1367. return;
  1368. }
  1369. }
  1370. function chdir($ftp_path)
  1371. {
  1372. if (!is_resource($this->connection))
  1373. return false;
  1374. // No slash on the end, please...
  1375. if (substr($ftp_path, -1) == '/')
  1376. $ftp_path = substr($ftp_path, 0, -1);
  1377. fwrite($this->connection, 'CWD ' . $ftp_path . "\r\n");
  1378. if (!$this->check_response(250))
  1379. {
  1380. $this->error = 'bad_path';
  1381. return false;
  1382. }
  1383. return true;
  1384. }
  1385. function chmod($ftp_file, $chmod)
  1386. {
  1387. if (!is_resource($this->connection))
  1388. return false;
  1389. // Convert the chmod value from octal (0777) to text ("777")
  1390. fwrite($this->connection, 'SITE CHMOD ' . decoct($chmod) . ' ' . $ftp_file . "\r\n");
  1391. if (!$this->check_response(200))
  1392. {
  1393. $this->error = 'bad_file';
  1394. return false;
  1395. }
  1396. return true;
  1397. }
  1398. function unlink($ftp_file)
  1399. {
  1400. // We are actually connected, right?
  1401. if (!is_resource($this->connection))
  1402. return false;
  1403. // Delete file X.
  1404. fwrite($this->connection, 'DELE ' . $ftp_file . "\r\n");
  1405. if (!$this->check_response(250))
  1406. {
  1407. fwrite($this->connection, 'RMD ' . $ftp_file . "\r\n");
  1408. // Still no love?
  1409. if (!$this->check_response(250))
  1410. {
  1411. $this->error = 'bad_file';
  1412. return false;
  1413. }
  1414. }
  1415. return true;
  1416. }
  1417. function check_response($desired)
  1418. {
  1419. // Wait for a response that isn't continued with -, but don't wait too long.
  1420. $time = time();
  1421. do
  1422. $this->last_message = fgets($this->connection, 1024);
  1423. while (substr($this->last_message, 3, 1) != ' ' && time() - $time < 5);
  1424. // Was the desired response returned?
  1425. return is_array($desired) ? in_array(substr($this->last_message, 0, 3), $desired) : substr($this->last_message, 0, 3) == $desired;
  1426. }
  1427. function passive()
  1428. {
  1429. // We can't create a passive data connection without a primary one first being there.
  1430. if (!is_resource($this->connection))
  1431. return false;
  1432. // Request a passive connection - this means, we'll talk to you, you don't talk to us.
  1433. @fwrite($this->connection, "PASV\r\n");
  1434. $time = time();
  1435. do
  1436. $response = fgets($this->connection, 1024);
  1437. while (substr($response, 3, 1) != ' ' && time() - $time < 5);
  1438. // If it's not 227, we weren't given an IP and port, which means it failed.
  1439. if (substr($response, 0, 4) != '227 ')
  1440. {
  1441. $this->error = 'bad_response';
  1442. return false;
  1443. }
  1444. // Snatch the IP and port information, or die horribly trying...
  1445. if (preg_match('~\((\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))\)~', $response, $match) == 0)
  1446. {
  1447. $this->error = 'bad_response';
  1448. return false;
  1449. }
  1450. // This is pretty simple - store it for later use ;)
  1451. $this->pasv = array('ip' => $match[1] . '.' . $match[2] . '.' . $match[3] . '.' . $match[4], 'port' => $match[5] * 256 + $match[6]);
  1452. return true;
  1453. }
  1454. function create_file($ftp_file)
  1455. {
  1456. // First, we have to be connected... very important.
  1457. if (!is_resource($this->connection))
  1458. return false;
  1459. // I'd like one passive mode, please!
  1460. if (!$this->passive())
  1461. return false;
  1462. // Seems logical enough, so far...
  1463. fwrite($this->connection, 'STOR ' . $ftp_file . "\r\n");
  1464. // Okay, now we connect to the data port. If it doesn't work out, it's probably "file already exists", etc.
  1465. $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
  1466. if (!$fp || !$this->check_response(150))
  1467. {
  1468. $this->error = 'bad_file';
  1469. @fclose($fp);
  1470. return false;
  1471. }
  1472. // This may look strange, but we're just closing it to indicate a zero-byte upload.
  1473. fclose($fp);
  1474. if (!$this->check_response(226))
  1475. {
  1476. $this->error = 'bad_response';
  1477. return false;
  1478. }
  1479. return true;
  1480. }
  1481. function list_dir($ftp_path = '', $search = false)
  1482. {
  1483. // Are we even connected...?
  1484. if (!is_resource($this->connection))
  1485. return false;
  1486. // Passive... non-agressive...
  1487. if (!$this->passive())
  1488. return false;
  1489. // Get the listing!
  1490. fwrite($this->connection, 'LIST -1' . ($search ? 'R' : '') . ($ftp_path == '' ? '' : ' ' . $ftp_path) . "\r\n");
  1491. // Connect, assuming we've got a connection.
  1492. $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
  1493. if (!$fp || !$this->check_response(array(150, 125)))
  1494. {
  1495. $this->error = 'bad_response';
  1496. @fclose($fp);
  1497. return false;
  1498. }
  1499. // Read in the file listing.
  1500. $data = '';
  1501. while (!feof($fp))
  1502. $data .= fread($fp, 4096);
  1503. fclose($fp);
  1504. // Everything go okay?
  1505. if (!$this->check_response(226))
  1506. {
  1507. $this->error = 'bad_response';
  1508. return false;
  1509. }
  1510. return $data;
  1511. }
  1512. function locate($file, $listing = null)
  1513. {
  1514. if ($listing === null)
  1515. $listing = $this->list_dir('', true);
  1516. $listing = explode("\n", $listing);
  1517. @fwrite($this->connection, "PWD\r\n");
  1518. $time = time();
  1519. do
  1520. $response = fgets($this->connection, 1024);
  1521. while (substr($response, 3, 1) != ' ' && time() - $time < 5);
  1522. // Check for 257!
  1523. if (preg_match('~^257 "(.+?)" ~', $response, $match) != 0)
  1524. $current_dir = strtr($match[1], array('""' => '"'));
  1525. else
  1526. $current_dir = '';
  1527. for ($i = 0, $n = count($listing); $i < $n; $i++)
  1528. {
  1529. if (trim($listing[$i]) == '' && isset($listing[$i + 1]))
  1530. {
  1531. $current_dir = substr(trim($listing[++$i]), 0, -1);
  1532. $i++;
  1533. }
  1534. // Okay, this file's name is:
  1535. $listing[$i] = $current_dir . '/' . trim(strlen($listing[$i]) > 30 ? strrchr($listing[$i], ' ') : $listing[$i]);
  1536. if (substr($file, 0, 1) == '*' && substr($listing[$i], -(strlen($file) - 1)) == substr($file, 1))
  1537. return $listing[$i];
  1538. if (substr($file, -1) == '*' && substr($listing[$i], 0, strlen($file) - 1) == substr($file, 0, -1))
  1539. return $listing[$i];
  1540. if (basename($listing[$i]) == $file || $listing[$i] == $file)
  1541. return $listing[$i];
  1542. }
  1543. return false;
  1544. }
  1545. function create_dir($ftp_dir)
  1546. {
  1547. // We must be connected to the server to do something.
  1548. if (!is_resource($this->connection))
  1549. return false;
  1550. // Make this new beautiful directory!
  1551. fwrite($this->connection, 'MKD ' . $ftp_dir . "\r\n");
  1552. if (!$this->check_response(257))
  1553. {
  1554. $this->error = 'bad_file';
  1555. return false;
  1556. }
  1557. return true;
  1558. }
  1559. function detect_path($filesystem_path, $lookup_file = null)
  1560. {
  1561. $username = '';
  1562. if (isset($_SERVER['DOCUMENT_ROOT']))
  1563. {
  1564. if (preg_match('~^/home[2]?/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
  1565. {
  1566. $username = $match[1];
  1567. $path = strtr($_SERVER['DOCUMENT_ROOT'], array('/home/' . $match[1] . '/' => '', '/home2/' . $match[1] . '/' => ''));
  1568. if (substr($path, -1) == '/')
  1569. $path = substr($path, 0, -1);
  1570. if (strlen(dirname($_SERVER['PHP_SELF'])) > 1)
  1571. $path .= dirname($_SERVER['PHP_SELF']);
  1572. }
  1573. elseif (substr($filesystem_path, 0, 9) == '/var/www/')
  1574. $path = substr($filesystem_path, 8);
  1575. else
  1576. $path = strtr(strtr($filesystem_path, array('\\' => '/')), array($_SERVER['DOCUMENT_ROOT'] => ''));
  1577. }
  1578. else
  1579. $path = '';
  1580. if (is_resource($this->connection) && $this->list_dir($path) == '')
  1581. {
  1582. $data = $this->list_dir('', true);
  1583. if ($lookup_file === null)
  1584. $lookup_file = $_SERVER['PHP_SELF'];
  1585. $found_path = dirname($this->locate('*' . basename(dirname($lookup_file)) . '/' . basename($lookup_file), $data));
  1586. if ($found_path == false)
  1587. $found_path = dirname($this->locate(basename($lookup_file)));
  1588. if ($found_path != false)
  1589. $path = $found_path;
  1590. }
  1591. elseif (is_resource($this->connection))
  1592. $found_path = true;
  1593. return array($username, $path, isset($found_path));
  1594. }
  1595. function close()
  1596. {
  1597. // Goodbye!
  1598. fwrite($this->connection, "QUIT\r\n");
  1599. fclose($this->connection);
  1600. return true;
  1601. }
  1602. }
  1603. function updateSettingsFile($vars)
  1604. {
  1605. // Modify Settings.php.
  1606. $settingsArray = file(dirname(__FILE__) . '/Settings.php');
  1607. // @todo Do we just want to read the file in clean, and split it this way always?
  1608. if (count($settingsArray) == 1)
  1609. $settingsArray = preg_split('~[\r\n]~', $settingsArray[0]);
  1610. for ($i = 0, $n = count($settingsArray); $i < $n; $i++)
  1611. {
  1612. // Remove the redirect...
  1613. if (trim($settingsArray[$i]) == 'if (file_exists(dirname(__FILE__) . \'/install.php\'))' && trim($settingsArray[$i + 1]) == '{' && trim($settingsArray[$i + 3]) == '}')
  1614. {
  1615. // Get the four lines to nothing.
  1616. $settingsArray[$i] = '';
  1617. $settingsArray[++$i] = '';
  1618. $settingsArray[++$i] = '';
  1619. $settingsArray[++$i] = '';
  1620. continue;
  1621. }
  1622. if (trim($settingsArray[$i]) == '?' . '>')
  1623. $settingsArray[$i] = '';
  1624. // Don't trim or bother with it if it's not a variable.
  1625. if (substr($settingsArray[$i], 0, 1) != '$')
  1626. continue;
  1627. $settingsArray[$i] = rtrim($settingsArray[$i]) . "\n";
  1628. foreach ($vars as $var => $val)
  1629. if (strncasecmp($settingsArray[$i], '$' . $var, 1 + strlen($var)) == 0)
  1630. {
  1631. $comment = strstr($settingsArray[$i], '#');
  1632. $settingsArray[$i] = '$' . $var . ' = \'' . $val . '\';' . ($comment != '' ? "\t\t" . $comment : "\n");
  1633. unset($vars[$var]);
  1634. }
  1635. }
  1636. // Uh oh... the file wasn't empty... was it?
  1637. if (!empty($vars))
  1638. {
  1639. $settingsArray[$i++] = '';
  1640. foreach ($vars as $var => $val)
  1641. $settingsArray[$i++] = '$' . $var . ' = \'' . $val . '\';' . "\n";
  1642. }
  1643. // Blank out the file - done to fix a oddity with some servers.
  1644. $fp = @fopen(dirname(__FILE__) . '/Settings.php', 'w');
  1645. if (!$fp)
  1646. return false;
  1647. fclose($fp);
  1648. $fp = fopen(dirname(__FILE__) . '/Settings.php', 'r+');
  1649. // Gotta have one of these ;)
  1650. if (trim($settingsArray[0]) != '<?php')
  1651. fwrite($fp, "<?php\n");
  1652. $lines = count($settingsArray);
  1653. for ($i = 0; $i < $lines - 1; $i++)
  1654. {
  1655. // Don't just write a bunch of blank lines.
  1656. if ($settingsArray[$i] != '' || @$settingsArray[$i - 1] != '')
  1657. fwrite($fp, strtr($settingsArray[$i], "\r", ''));
  1658. }
  1659. fwrite($fp, $settingsArray[$i] . '?' . '>');
  1660. fclose($fp);
  1661. return true;
  1662. }
  1663. // Create an .htaccess file to prevent mod_security. SMF has filtering built-in.
  1664. function fixModSecurity()
  1665. {
  1666. $htaccess_addition = '
  1667. <IfModule mod_security.c>
  1668. # Turn off mod_security filtering. SMF is a big boy, it doesn\'t need its hands held.
  1669. SecFilterEngine Off
  1670. # The below probably isn\'t needed, but better safe than sorry.
  1671. SecFilterScanPOST Off
  1672. </IfModule>';
  1673. if (!function_exists('apache_get_modules') || !in_array('mod_security', apache_get_modules()))
  1674. return true;
  1675. elseif (file_exists(dirname(__FILE__) . '/.htaccess') && is_writable(dirname(__FILE__) . '/.htaccess'))
  1676. {
  1677. $current_htaccess = implode('', file(dirname(__FILE__) . '/.htaccess'));
  1678. // Only change something if mod_security hasn't been addressed yet.
  1679. if (strpos($current_htaccess, '<IfModule mod_security.c>') === false)
  1680. {
  1681. if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'a'))
  1682. {
  1683. fwrite($ht_handle, $htaccess_addition);
  1684. fclose($ht_handle);
  1685. return true;
  1686. }
  1687. else
  1688. return false;
  1689. }
  1690. else
  1691. return true;
  1692. }
  1693. elseif (file_exists(dirname(__FILE__) . '/.htaccess'))
  1694. return strpos(implode('', file(dirname(__FILE__) . '/.htaccess')), '<IfModule mod_security.c>') !== false;
  1695. elseif (is_writable(dirname(__FILE__)))
  1696. {
  1697. if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'w'))
  1698. {
  1699. fwrite($ht_handle, $htaccess_addition);
  1700. fclose($ht_handle);
  1701. return true;
  1702. }
  1703. else
  1704. return false;
  1705. }
  1706. else
  1707. return false;
  1708. }
  1709. function template_install_above()
  1710. {
  1711. global $incontext, $txt, $smfsite, $installurl;
  1712. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1713. <html xmlns="http://www.w3.org/1999/xhtml"', !empty($txt['lang_rtl']) ? ' dir="rtl"' : '', '>
  1714. <head>
  1715. <meta http-equiv="Content-Type" content="text/html; charset=', isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'ISO-8859-1', '" />
  1716. <meta name="robots" content="noindex" />
  1717. <title>', $txt['smf_installer'], '</title>
  1718. <link rel="stylesheet" type="text/css" href="Themes/default/css/index.css?fin20" />
  1719. <link rel="stylesheet" type="text/css" href="Themes/default/css/install.css?fin20" />
  1720. <script type="text/javascript" src="Themes/default/scripts/script.js"></script>
  1721. </head>
  1722. <body>
  1723. <div id="header"><div class="frame">
  1724. <div id="top_section">
  1725. <h1 class="forumtitle">', $txt['smf_installer'], '</h1>
  1726. <img id="smflogo" src="Themes/default/images/smflogo.png" alt="Simple Machines Forum" title="Simple Machines Forum" />
  1727. </div>
  1728. <div id="upper_section" class="middletext flow_hidden">
  1729. <div class="user"></div>
  1730. <div class="news normaltext">';
  1731. // Have we got a language drop down - if so do it on the first step only.
  1732. if (!empty($incontext['detected_languages']) && count($incontext['detected_languages']) > 1 && $incontext['current_step'] == 0)
  1733. {
  1734. echo '
  1735. <div class="righttext">
  1736. <form action="', $installurl, '" method="get">
  1737. <label for="installer_language">', $txt['installer_language'], ':</label> <select id="installer_language" name="lang_file" onchange="location.href = \'', $installurl, '?lang_file=\' + this.options[this.selectedIndex].value;">';
  1738. foreach ($incontext['detected_languages'] as $lang => $name)
  1739. echo '
  1740. <option', isset($_SESSION['installer_temp_lang']) && $_SESSION['installer_temp_lang'] == $lang ? ' selected="selected"' : '', ' value="', $lang, '">', $name, '</option>';
  1741. echo '
  1742. </select>
  1743. <noscript><input type="submit" value="', $txt['installer_language_set'], '" class="button_submit" /></noscript>
  1744. </form>
  1745. </div>';
  1746. }
  1747. echo '
  1748. </div>
  1749. </div>
  1750. </div></div>
  1751. <div id="content_section"><div class="frame">
  1752. <div id="main_content_section">
  1753. <div id="main-steps">
  1754. <h2>', $txt['upgrade_progress'], '</h2>
  1755. <ul>';
  1756. foreach ($incontext['steps'] as $num => $step)
  1757. echo '
  1758. <li class="', $num < $incontext['current_step'] ? 'stepdone' : ($num == $incontext['current_step'] ? 'stepcurrent' : 'stepwaiting'), '">', $txt['upgrade_step'], ' ', $step[0], ': ', $step[1], '</li>';
  1759. echo '
  1760. </ul>
  1761. </div>
  1762. <div style="font-size: 12pt; height: 25pt; border: 1px solid black; background: white; float: left; margin-left: 12%; width: 25%;">
  1763. <div id="overall_text" style="padding-top: 8pt; z-index: 2; color: black; margin-left: -4em; position: absolute; text-align: center; font-weight: bold;">', $incontext['overall_percent'], '%</div>
  1764. <div id="overall_progress" style="width: ', $incontext['overall_percent'], '%; height: 25pt; z-index: 1; background-color: lime;">&nbsp;</div>
  1765. <div class="overall_progress">', $txt['upgrade_overall_progress'], '</div>
  1766. </div>
  1767. <div id="main_screen" class="clear">
  1768. <h2>', $incontext['page_title'], '</h2>
  1769. <div class="panel">
  1770. <div style="max-height: 560px; overflow: auto;">';
  1771. }
  1772. function template_install_below()
  1773. {
  1774. global $incontext, $txt;
  1775. if (!empty($incontext['continue']) || !empty($incontext['skip']))
  1776. {
  1777. echo '
  1778. <div class="righttext" style="margin: 1ex;">';
  1779. if (!empty($incontext['continue']))
  1780. echo '
  1781. <input type="submit" id="contbutt" name="contbutt" value="', $txt['upgrade_continue'], '" onclick="return submitThisOnce(this);" class="button_submit" />';
  1782. if (!empty($incontext['skip']))
  1783. echo '
  1784. <input type="submit" id="skip" name="skip" value="', $txt['upgrade_skip'], '" onclick="return submitThisOnce(this);" class="button_submit" />';
  1785. echo '
  1786. </div>';
  1787. }
  1788. // Show the closing form tag and other data only if not in the last step
  1789. if (count($incontext['steps']) - 1 !== (int) $incontext['current_step'])
  1790. echo '
  1791. </form>';
  1792. echo '
  1793. </div>
  1794. </div>
  1795. </div>
  1796. </div>
  1797. </div></div>
  1798. <div id="footer_section"><div class="frame" style="height: 40px;">
  1799. <div class="smalltext"><a href="http://www.simplemachines.org/" title="Simple Machines Forum" target="_blank" class="new_win">SMF &copy; 2011, Simple Machines</a></div>
  1800. </div></div>
  1801. </body>
  1802. </html>';
  1803. }
  1804. // Welcome them to the wonderful world of SMF!
  1805. function template_welcome_message()
  1806. {
  1807. global $incontext, $installurl, $txt;
  1808. echo '
  1809. <script type="text/javascript" src="http://www.simplemachines.org/smf/current-version.js?version=' . $GLOBALS['current_smf_version'] . '"></script>
  1810. <form action="', $incontext['form_url'], '" method="post">
  1811. <p>', sprintf($txt['install_welcome_desc'], $GLOBALS['current_smf_version']), '</p>
  1812. <div id="version_warning" style="margin: 2ex; padding: 2ex; border: 2px dashed #a92174; color: black; background-color: #fbbbe2; display: none;">
  1813. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  1814. <strong style="text-decoration: underline;">', $txt['error_warning_notice'], '</strong><br />
  1815. <div style="padding-left: 6ex;">
  1816. ', sprintf($txt['error_script_outdated'], '<em id="smfVersion" style="white-space: nowrap;">??</em>', '<em id="yourVersion" style="white-space: nowrap;">' . $GLOBALS['current_smf_version'] . '</em>'), '
  1817. </div>
  1818. </div>';
  1819. // Show the warnings, or not.
  1820. if (template_warning_divs())
  1821. echo '
  1822. <h3>', $txt['install_all_lovely'], '</h3>';
  1823. echo '
  1824. <div style="height: 100px;"></div>';
  1825. // Say we want the continue button!
  1826. if (empty($incontext['error']))
  1827. $incontext['continue'] = 1;
  1828. // For the latest version stuff.
  1829. echo '
  1830. <script type="text/javascript"><!-- // --><![CDATA[
  1831. // Latest version?
  1832. function smfCurrentVersion()
  1833. {
  1834. var smfVer, yourVer;
  1835. if (!(\'smfVersion\' in window))
  1836. return;
  1837. window.smfVersion = window.smfVersion.replace(/SMF\s?/g, \'\');
  1838. smfVer = document.getElementById("smfVersion");
  1839. yourVer = document.getElementById("yourVersion");
  1840. setInnerHTML(smfVer, window.smfVersion);
  1841. var currentVersion = getInnerHTML(yourVer);
  1842. if (currentVersion < window.smfVersion)
  1843. document.getElementById(\'version_warning\').style.display = \'\';
  1844. }
  1845. addLoadEvent(smfCurrentVersion);
  1846. // ]]></script>';
  1847. }
  1848. // A shortcut for any warning stuff.
  1849. function template_warning_divs()
  1850. {
  1851. global $txt, $incontext;
  1852. // Errors are very serious..
  1853. if (!empty($incontext['error']))
  1854. echo '
  1855. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  1856. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  1857. <strong style="text-decoration: underline;">', $txt['upgrade_critical_error'], '</strong><br />
  1858. <div style="padding-left: 6ex;">
  1859. ', $incontext['error'], '
  1860. </div>
  1861. </div>';
  1862. // A warning message?
  1863. elseif (!empty($incontext['warning']))
  1864. echo '
  1865. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  1866. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  1867. <strong style="text-decoration: underline;">', $txt['upgrade_warning'], '</strong><br />
  1868. <div style="padding-left: 6ex;">
  1869. ', $incontext['warning'], '
  1870. </div>
  1871. </div>';
  1872. return empty($incontext['error']) && empty($incontext['warning']);
  1873. }
  1874. function template_chmod_files()
  1875. {
  1876. global $txt, $incontext;
  1877. echo '
  1878. <p>', $txt['ftp_setup_why_info'], '</p>
  1879. <ul style="margin: 2.5ex; font-family: monospace;">
  1880. <li>', implode('</li>
  1881. <li>', $incontext['failed_files']), '</li>
  1882. </ul>';
  1883. // This is serious!
  1884. if (!template_warning_divs())
  1885. return;
  1886. echo '
  1887. <hr />
  1888. <p>', $txt['ftp_setup_info'], '</p>';
  1889. if (!empty($incontext['ftp_errors']))
  1890. echo '
  1891. <div class="error_message">
  1892. <div style="color: red;">
  1893. ', $txt['error_ftp_no_connect'], '<br />
  1894. <br />
  1895. <code>', implode('<br />', $incontext['ftp_errors']), '</code>
  1896. </div>
  1897. </div>
  1898. <br />';
  1899. echo '
  1900. <form action="', $incontext['form_url'], '" method="post">
  1901. <table width="520" cellspacing="0" cellpadding="0" border="0" align="center" style="margin: 1em 0;">
  1902. <tr>
  1903. <td width="26%" valign="top" class="textbox"><label for="ftp_server">', $txt['ftp_server'], ':</label></td>
  1904. <td>
  1905. <div style="float: ', empty($txt['lang_rtl']) ? 'right' : 'left', '; margin-', empty($txt['lang_rtl']) ? 'right' : 'left', ': 1px;"><label for="ftp_port" class="textbox"><strong>', $txt['ftp_port'], ':&nbsp;</strong></label> <input type="text" size="3" name="ftp_port" id="ftp_port" value="', $incontext['ftp']['port'], '" class="input_text" /></div>
  1906. <input type="text" size="30" name="ftp_server" id="ftp_server" value="', $incontext['ftp']['server'], '" style="width: 70%;" class="input_text" />
  1907. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['ftp_server_info'], '</div>
  1908. </td>
  1909. </tr><tr>
  1910. <td width="26%" valign="top" class="textbox"><label for="ftp_username">', $txt['ftp_username'], ':</label></td>
  1911. <td>
  1912. <input type="text" size="50" name="ftp_username" id="ftp_username" value="', $incontext['ftp']['username'], '" style="width: 99%;" class="input_text" />
  1913. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['ftp_username_info'], '</div>
  1914. </td>
  1915. </tr><tr>
  1916. <td width="26%" valign="top" class="textbox"><label for="ftp_password">', $txt['ftp_password'], ':</label></td>
  1917. <td>
  1918. <input type="password" size="50" name="ftp_password" id="ftp_password" style="width: 99%;" class="input_password" />
  1919. <div style="font-size: smaller; margin-bottom: 3ex;">', $txt['ftp_password_info'], '</div>
  1920. </td>
  1921. </tr><tr>
  1922. <td width="26%" valign="top" class="textbox"><label for="ftp_path">', $txt['ftp_path'], ':</label></td>
  1923. <td style="padding-bottom: 1ex;">
  1924. <input type="text" size="50" name="ftp_path" id="ftp_path" value="', $incontext['ftp']['path'], '" style="width: 99%;" class="input_text" />
  1925. <div style="font-size: smaller; margin-bottom: 2ex;">', $incontext['ftp']['path_msg'], '</div>
  1926. </td>
  1927. </tr>
  1928. </table>
  1929. <div style="margin: 1ex; margin-top: 1ex; text-align: ', empty($txt['lang_rtl']) ? 'right' : 'left', ';"><input type="submit" value="', $txt['ftp_connect'], '" onclick="return submitThisOnce(this);" class="button_submit" /></div>
  1930. </form>
  1931. <a href="', $incontext['form_url'], '">', $txt['error_message_click'], '</a> ', $txt['ftp_setup_again'];
  1932. }
  1933. // Get the database settings prepared.
  1934. function template_database_settings()
  1935. {
  1936. global $incontext, $installurl, $txt;
  1937. echo '
  1938. <form action="', $incontext['form_url'], '" method="post">
  1939. <p>', $txt['db_settings_info'], '</p>';
  1940. template_warning_divs();
  1941. echo '
  1942. <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin: 1em 0;">';
  1943. // More than one database type?
  1944. if (count($incontext['supported_databases']) > 1)
  1945. {
  1946. echo '
  1947. <tr>
  1948. <td width="20%" valign="top" class="textbox"><label for="db_type_input">', $txt['db_settings_type'], ':</label></td>
  1949. <td>
  1950. <select name="db_type" id="db_type_input" onchange="toggleDBInput();">';
  1951. foreach ($incontext['supported_databases'] as $key => $db)
  1952. echo '
  1953. <option value="', $key, '"', isset($_POST['db_type']) && $_POST['db_type'] == $key ? ' selected="selected"' : '', '>', $db['name'], '</option>';
  1954. echo '
  1955. </select><div id="db_sqlite_warning" style="color: blue; display: none;" class="smalltext">', $txt['db_sqlite_warning'], '</div>
  1956. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_type_info'], '</div>
  1957. </td>
  1958. </tr>';
  1959. }
  1960. else
  1961. {
  1962. echo '
  1963. <tr style="display: none;">
  1964. <td>
  1965. <input type="hidden" name="db_type" value="', $incontext['db']['type'], '" />
  1966. </td>
  1967. </tr>';
  1968. }
  1969. echo '
  1970. <tr id="db_server_contain">
  1971. <td width="20%" valign="top" class="textbox"><label for="db_server_input">', $txt['db_settings_server'], ':</label></td>
  1972. <td>
  1973. <input type="text" name="db_server" id="db_server_input" value="', $incontext['db']['server'], '" size="30" class="input_text" /><br />
  1974. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_server_info'], '</div>
  1975. </td>
  1976. </tr><tr id="db_user_contain">
  1977. <td valign="top" class="textbox"><label for="db_user_input">', $txt['db_settings_username'], ':</label></td>
  1978. <td>
  1979. <input type="text" name="db_user" id="db_user_input" value="', $incontext['db']['user'], '" size="30" class="input_text" /><br />
  1980. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_username_info'], '</div>
  1981. </td>
  1982. </tr><tr id="db_passwd_contain">
  1983. <td valign="top" class="textbox"><label for="db_passwd_input">', $txt['db_settings_password'], ':</label></td>
  1984. <td>
  1985. <input type="password" name="db_passwd" id="db_passwd_input" value="', $incontext['db']['pass'], '" size="30" class="input_password" /><br />
  1986. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_password_info'], '</div>
  1987. </td>
  1988. </tr><tr id="db_name_contain">
  1989. <td valign="top" class="textbox"><label for="db_name_input">', $txt['db_settings_database'], ':</label></td>
  1990. <td>
  1991. <input type="text" name="db_name" id="db_name_input" value="', empty($incontext['db']['name']) ? 'smf' : $incontext['db']['name'], '" size="30" class="input_text" /><br />
  1992. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_database_info'], '
  1993. <span id="db_name_info_warning">', $txt['db_settings_database_info_note'], '</span></div>
  1994. </td>
  1995. </tr><tr id="db_filename_contain" style="display: none;">
  1996. <td valign="top" class="textbox"><label for="db_filename_input">', $txt['db_settings_database_file'], ':</label></td>
  1997. <td>
  1998. <input type="text" name="db_filename" id="db_filename_input" value="', empty($incontext['db']['name']) ? dirname(__FILE__) . '/smf_' . substr(md5(microtime()), 0, 10) : stripslashes($incontext['db']['name']), '" size="30" class="input_text" /><br />
  1999. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_database_file_info'], '</div>
  2000. </td>
  2001. </tr><tr>
  2002. <td valign="top" class="textbox"><label for="db_prefix_input">', $txt['db_settings_prefix'], ':</label></td>
  2003. <td>
  2004. <input type="text" name="db_prefix" id="db_prefix_input" value="', $incontext['db']['prefix'], '" size="30" class="input_text" /><br />
  2005. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['db_settings_prefix_info'], '</div>
  2006. </td>
  2007. </tr>
  2008. </table>';
  2009. // Allow the toggling of input boxes for SQLite etc.
  2010. echo '
  2011. <script type="text/javascript"><!-- // --><![CDATA[
  2012. function toggleDBInput()
  2013. {
  2014. // What state is it?';
  2015. if (!isset($incontext['supported_databases']['sqlite']))
  2016. echo '
  2017. var showAll = true;';
  2018. elseif (count($incontext['supported_databases']) < 2)
  2019. echo '
  2020. var showAll = false;';
  2021. // If we have more than one DB including SQLite, what should we be doing?
  2022. else
  2023. echo '
  2024. var showAll = document.getElementById(\'db_type_input\').value == \'sqlite\' ? false : true;';
  2025. echo '
  2026. document.getElementById(\'db_passwd_contain\').style.display = showAll ? \'\' : \'none\';
  2027. document.getElementById(\'db_server_contain\').style.display = showAll ? \'\' : \'none\';
  2028. document.getElementById(\'db_user_contain\').style.display = showAll ? \'\' : \'none\';
  2029. document.getElementById(\'db_name_contain\').style.display = showAll ? \'\' : \'none\';
  2030. document.getElementById(\'db_filename_contain\').style.display = !showAll ? \'\' : \'none\';
  2031. document.getElementById(\'db_sqlite_warning\').style.display = !showAll ? \'\' : \'none\';
  2032. if (document.getElementById(\'db_type_input\').value == \'postgresql\')
  2033. document.getElementById(\'db_name_info_warning\').style.display = \'none\';
  2034. else
  2035. document.getElementById(\'db_name_info_warning\').style.display = \'\';
  2036. }
  2037. toggleDBInput();
  2038. // ]]></script>';
  2039. }
  2040. // Stick in their forum settings.
  2041. function template_forum_settings()
  2042. {
  2043. global $incontext, $installurl, $txt;
  2044. echo '
  2045. <form action="', $incontext['form_url'], '" method="post">
  2046. <h3>', $txt['install_settings_info'], '</h3>';
  2047. template_warning_divs();
  2048. echo '
  2049. <table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin: 1em 0;">
  2050. <tr>
  2051. <td width="20%" valign="top" class="textbox"><label for="mbname_input">', $txt['install_settings_name'], ':</label></td>
  2052. <td>
  2053. <input type="text" name="mbname" id="mbname_input" value="', $txt['install_settings_name_default'], '" size="65" class="input_text" />
  2054. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['install_settings_name_info'], '</div>
  2055. </td>
  2056. </tr><tr>
  2057. <td valign="top" class="textbox"><label for="boardurl_input">', $txt['install_settings_url'], ':</label></td>
  2058. <td>
  2059. <input type="text" name="boardurl" id="boardurl_input" value="', $incontext['detected_url'], '" size="65" class="input_text" /><br />
  2060. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['install_settings_url_info'], '</div>
  2061. </td>
  2062. </tr><tr>
  2063. <td valign="top" class="textbox">', $txt['install_settings_compress'], ':</td>
  2064. <td>
  2065. <input type="checkbox" name="compress" id="compress_check" checked="checked" class="input_check" /> <label for="compress_check">', $txt['install_settings_compress_title'], '</label><br />
  2066. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['install_settings_compress_info'], '</div>
  2067. </td>
  2068. </tr><tr>
  2069. <td valign="top" class="textbox">', $txt['install_settings_dbsession'], ':</td>
  2070. <td>
  2071. <input type="checkbox" name="dbsession" id="dbsession_check" checked="checked" class="input_check" /> <label for="dbsession_check">', $txt['install_settings_dbsession_title'], '</label><br />
  2072. <div style="font-size: smaller; margin-bottom: 2ex;">', $incontext['test_dbsession'] ? $txt['install_settings_dbsession_info1'] : $txt['install_settings_dbsession_info2'], '</div>
  2073. </td>
  2074. </tr>';
  2075. if ($incontext['utf8_should_work'])
  2076. echo '
  2077. <tr>
  2078. <td valign="top" class="textbox">', $txt['install_settings_utf8'], ':</td>
  2079. <td>
  2080. <input type="checkbox" name="utf8" id="utf8_check"', $incontext['utf8_default'] ? ' checked="checked"' : '', ' class="input_check"', $incontext['utf8_required'] ? ' disabled="disabled"' : '', ' /> <label for="utf8_check">', $txt['install_settings_utf8_title'], '</label><br />
  2081. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['install_settings_utf8_info'], '</div>
  2082. </td>
  2083. </tr>';
  2084. echo '
  2085. <tr>
  2086. <td valign="top" class="textbox">', $txt['install_settings_stats'], ':</td>
  2087. <td>
  2088. <input type="checkbox" name="stats" id="stats_check" class="input_check" /> <label for="stats_check">', $txt['install_settings_stats_title'], '</label><br />
  2089. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['install_settings_stats_info'], '</div>
  2090. </td>
  2091. </tr>
  2092. </table>';
  2093. }
  2094. // Show results of the database population.
  2095. function template_populate_database()
  2096. {
  2097. global $incontext, $installurl, $txt;
  2098. echo '
  2099. <form action="', $incontext['form_url'], '" method="post">
  2100. <p>', !empty($incontext['was_refresh']) ? $txt['user_refresh_install_desc'] : $txt['db_populate_info'], '</p>';
  2101. if (!empty($incontext['sql_results']))
  2102. {
  2103. echo '
  2104. <ul>
  2105. <li>', implode('</li><li>', $incontext['sql_results']), '</li>
  2106. </ul>';
  2107. }
  2108. if (!empty($incontext['failures']))
  2109. {
  2110. echo '
  2111. <div style="color: red;">', $txt['error_db_queries'], '</div>
  2112. <ul>';
  2113. foreach ($incontext['failures'] as $line => $fail)
  2114. echo '
  2115. <li><strong>', $txt['error_db_queries_line'], $line + 1, ':</strong> ', nl2br(htmlspecialchars($fail)), '</li>';
  2116. echo '
  2117. </ul>';
  2118. }
  2119. echo '
  2120. <p>', $txt['db_populate_info2'], '</p>';
  2121. template_warning_divs();
  2122. echo '
  2123. <input type="hidden" name="pop_done" value="1" />';
  2124. }
  2125. // Create the admin account.
  2126. function template_admin_account()
  2127. {
  2128. global $incontext, $installurl, $txt;
  2129. echo '
  2130. <form action="', $incontext['form_url'], '" method="post">
  2131. <p>', $txt['user_settings_info'], '</p>';
  2132. template_warning_divs();
  2133. echo '
  2134. <table width="100%" cellspacing="0" cellpadding="0" border="0" style="margin: 2em 0;">
  2135. <tr>
  2136. <td width="18%" valign="top" class="textbox"><label for="username">', $txt['user_settings_username'], ':</label></td>
  2137. <td>
  2138. <input type="text" name="username" id="username" value="', $incontext['username'], '" size="40" class="input_text" />
  2139. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['user_settings_username_info'], '</div>
  2140. </td>
  2141. </tr><tr>
  2142. <td valign="top" class="textbox"><label for="password1">', $txt['user_settings_password'], ':</label></td>
  2143. <td>
  2144. <input type="password" name="password1" id="password1" size="40" class="input_password" />
  2145. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['user_settings_password_info'], '</div>
  2146. </td>
  2147. </tr><tr>
  2148. <td valign="top" class="textbox"><label for="password2">', $txt['user_settings_again'], ':</label></td>
  2149. <td>
  2150. <input type="password" name="password2" id="password2" size="40" class="input_password" />
  2151. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['user_settings_again_info'], '</div>
  2152. </td>
  2153. </tr><tr>
  2154. <td valign="top" class="textbox"><label for="email">', $txt['user_settings_email'], ':</label></td>
  2155. <td>
  2156. <input type="text" name="email" id="email" value="', $incontext['email'], '" size="40" class="input_text" />
  2157. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['user_settings_email_info'], '</div>
  2158. </td>
  2159. </tr>
  2160. </table>';
  2161. if ($incontext['require_db_confirm'])
  2162. echo '
  2163. <h2>', $txt['user_settings_database'], '</h2>
  2164. <p>', $txt['user_settings_database_info'], '</p>
  2165. <div style="margin-bottom: 2ex; padding-', empty($txt['lang_rtl']) ? 'left' : 'right', ': 50px;">
  2166. <input type="password" name="password3" size="30" class="input_password" />
  2167. </div>';
  2168. }
  2169. // Tell them it's done, and to delete.
  2170. function template_delete_install()
  2171. {
  2172. global $incontext, $installurl, $txt, $boardurl;
  2173. echo '
  2174. <p>', $txt['congratulations_help'], '</p>';
  2175. template_warning_divs();
  2176. // Install directory still writable?
  2177. if ($incontext['dir_still_writable'])
  2178. echo '
  2179. <em>', $txt['still_writable'], '</em><br />
  2180. <br />';
  2181. // Don't show the box if it's like 99% sure it won't work :P.
  2182. if ($incontext['probably_delete_install'])
  2183. echo '
  2184. <div style="margin: 1ex; font-weight: bold;">
  2185. <label for="delete_self"><input type="checkbox" id="delete_self" onclick="doTheDelete();" class="input_check" /> ', $txt['delete_installer'], !isset($_SESSION['installer_temp_ftp']) ? ' ' . $txt['delete_installer_maybe'] : '', '</label>
  2186. </div>
  2187. <script type="text/javascript"><!-- // --><![CDATA[
  2188. function doTheDelete()
  2189. {
  2190. var theCheck = document.getElementById ? document.getElementById("delete_self") : document.all.delete_self;
  2191. var tempImage = new Image();
  2192. tempImage.src = "', $installurl, '?delete=1&ts_" + (new Date().getTime());
  2193. tempImage.width = 0;
  2194. theCheck.disabled = true;
  2195. }
  2196. // ]]></script>
  2197. <br />';
  2198. echo '
  2199. ', sprintf($txt['go_to_your_forum'], $boardurl . '/index.php'), '<br />
  2200. <br />
  2201. ', $txt['good_luck'];
  2202. }
  2203. ?>