install.php 85 KB

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