Load.php 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  1. <?php
  2. /**
  3. * This file has the hefty job of loading information for the forum.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Load the $modSettings array.
  18. *
  19. * @todo okay question of the day: why a function for loading settings is called reloadSettings()
  20. *
  21. */
  22. function reloadSettings()
  23. {
  24. global $modSettings, $boarddir, $smcFunc, $txt, $db_character_set, $sourcedir, $context;
  25. // Most database systems have not set UTF-8 as their default input charset.
  26. if (!empty($db_character_set))
  27. $smcFunc['db_query']('set_character_set', '
  28. SET NAMES ' . $db_character_set,
  29. array(
  30. )
  31. );
  32. // Try to load it from the cache first; it'll never get cached if the setting is off.
  33. if (($modSettings = cache_get_data('modSettings', 90)) == null)
  34. {
  35. $request = $smcFunc['db_query']('', '
  36. SELECT variable, value
  37. FROM {db_prefix}settings',
  38. array(
  39. )
  40. );
  41. $modSettings = array();
  42. if (!$request)
  43. display_db_error();
  44. while ($row = $smcFunc['db_fetch_row']($request))
  45. $modSettings[$row[0]] = $row[1];
  46. $smcFunc['db_free_result']($request);
  47. // Do a few things to protect against missing settings or settings with invalid values...
  48. if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999)
  49. $modSettings['defaultMaxTopics'] = 20;
  50. if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999)
  51. $modSettings['defaultMaxMessages'] = 15;
  52. if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999)
  53. $modSettings['defaultMaxMembers'] = 30;
  54. if (!empty($modSettings['cache_enable']))
  55. cache_put_data('modSettings', $modSettings, 90);
  56. }
  57. // UTF-8 ?
  58. $utf8 = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
  59. // Set a list of common functions.
  60. $ent_list = empty($modSettings['disableEntityCheck']) ? '&(#\d{1,7}|quot|amp|lt|gt|nbsp);' : '&(#021|quot|amp|lt|gt|nbsp);';
  61. $ent_check = empty($modSettings['disableEntityCheck']) ? array('preg_replace_callback(\'~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~\', \'entity_fix__callback\', ', ')') : array('', '');
  62. // Preg_replace space characters depend on the character set in use
  63. $space_chars = $utf8 ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0';
  64. // global array of anonymous helper functions, used mosly to properly handle multi byte strings
  65. $smcFunc += array(
  66. 'entity_fix' => create_function('$string', '
  67. $num = $string[0] === \'x\' ? hexdec(substr($string, 1)) : (int) $string;
  68. return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num === 0x202E || $num === 0x202D ? \'\' : \'&#\' . $num . \';\';'),
  69. 'htmlspecialchars' => create_function('$string, $quote_style = ENT_COMPAT, $charset = \'ISO-8859-1\'', '
  70. global $smcFunc;
  71. return ' . strtr($ent_check[0], array('&' => '&amp;')) . 'htmlspecialchars($string, $quote_style, ' . ($utf8 ? '\'UTF-8\'' : '$charset') . ')' . $ent_check[1] . ';'),
  72. 'htmltrim' => create_function('$string', '
  73. global $smcFunc;
  74. return preg_replace(\'~^(?:[ \t\n\r\x0B\x00' . $space_chars . ']|&nbsp;)+|(?:[ \t\n\r\x0B\x00' . $space_chars . ']|&nbsp;)+$~' . ($utf8 ? 'u' : '') . '\', \'\', ' . implode('$string', $ent_check) . ');'),
  75. 'strlen' => create_function('$string', '
  76. global $smcFunc;
  77. return strlen(preg_replace(\'~' . $ent_list . ($utf8 ? '|.~u' : '~') . '\', \'_\', ' . implode('$string', $ent_check) . '));'),
  78. 'strpos' => create_function('$haystack, $needle, $offset = 0', '
  79. global $smcFunc;
  80. $haystack_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\', ' . implode('$haystack', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  81. $haystack_size = count($haystack_arr);
  82. if (strlen($needle) === 1)
  83. {
  84. $result = array_search($needle, array_slice($haystack_arr, $offset));
  85. return is_int($result) ? $result + $offset : false;
  86. }
  87. else
  88. {
  89. $needle_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\', ' . implode('$needle', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  90. $needle_size = count($needle_arr);
  91. $result = array_search($needle_arr[0], array_slice($haystack_arr, $offset));
  92. while ((int) $result === $result)
  93. {
  94. $offset += $result;
  95. if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr)
  96. return $offset;
  97. $result = array_search($needle_arr[0], array_slice($haystack_arr, ++$offset));
  98. }
  99. return false;
  100. }'),
  101. 'substr' => create_function('$string, $start, $length = null', '
  102. global $smcFunc;
  103. $ent_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\', ' . implode('$string', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  104. return $length === null ? implode(\'\', array_slice($ent_arr, $start)) : implode(\'\', array_slice($ent_arr, $start, $length));'),
  105. 'strtolower' => $utf8 ? (function_exists('mb_strtolower') ? create_function('$string', '
  106. return mb_strtolower($string, \'UTF-8\');') : create_function('$string', '
  107. global $sourcedir;
  108. require_once($sourcedir . \'/Subs-Charset.php\');
  109. return utf8_strtolower($string);')) : 'strtolower',
  110. 'strtoupper' => $utf8 ? (function_exists('mb_strtoupper') ? create_function('$string', '
  111. return mb_strtoupper($string, \'UTF-8\');') : create_function('$string', '
  112. global $sourcedir;
  113. require_once($sourcedir . \'/Subs-Charset.php\');
  114. return utf8_strtoupper($string);')) : 'strtoupper',
  115. 'truncate' => create_function('$string, $length', (empty($modSettings['disableEntityCheck']) ? '
  116. global $smcFunc;
  117. $string = ' . implode('$string', $ent_check) . ';' : '') . '
  118. preg_match(\'~^(' . $ent_list . '|.){\' . $smcFunc[\'strlen\'](substr($string, 0, $length)) . \'}~'. ($utf8 ? 'u' : '') . '\', $string, $matches);
  119. $string = $matches[0];
  120. while (strlen($string) > $length)
  121. $string = preg_replace(\'~(?:' . $ent_list . '|.)$~'. ($utf8 ? 'u' : '') . '\', \'\', $string);
  122. return $string;'),
  123. 'ucfirst' => $utf8 ? create_function('$string', '
  124. global $smcFunc;
  125. return $smcFunc[\'strtoupper\']($smcFunc[\'substr\']($string, 0, 1)) . $smcFunc[\'substr\']($string, 1);') : 'ucfirst',
  126. 'ucwords' => $utf8 ? create_function('$string', '
  127. global $smcFunc;
  128. $words = preg_split(\'~([\s\r\n\t]+)~\', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
  129. for ($i = 0, $n = count($words); $i < $n; $i += 2)
  130. $words[$i] = $smcFunc[\'ucfirst\']($words[$i]);
  131. return implode(\'\', $words);') : 'ucwords',
  132. );
  133. // Setting the timezone is a requirement for some functions in PHP >= 5.1.
  134. if (isset($modSettings['default_timezone']) && function_exists('date_default_timezone_set'))
  135. date_default_timezone_set($modSettings['default_timezone']);
  136. // Check the load averages?
  137. if (!empty($modSettings['loadavg_enable']))
  138. {
  139. if (($modSettings['load_average'] = cache_get_data('loadavg', 90)) == null)
  140. {
  141. $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
  142. if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) != 0)
  143. $modSettings['load_average'] = (float) $matches[1];
  144. elseif (($modSettings['load_average'] = @`uptime`) != null && preg_match('~load average[s]?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) != 0)
  145. $modSettings['load_average'] = (float) $matches[1];
  146. else
  147. unset($modSettings['load_average']);
  148. if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
  149. cache_put_data('loadavg', $modSettings['load_average'], 90);
  150. }
  151. if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
  152. call_integration_hook('integrate_load_average', array($modSettings['load_average']));
  153. if (!empty($modSettings['loadavg_forum']) && !empty($modSettings['load_average']) && $modSettings['load_average'] >= $modSettings['loadavg_forum'])
  154. display_loadavg_error();
  155. }
  156. // Is post moderation alive and well? Everywhere else assumes this has been defined, so let's make sure it is.
  157. $modSettings['postmod_active'] = !empty($modSettings['postmod_active']);
  158. // Here to justify the name of this function. :P
  159. // It should be added to the install and upgrade scripts.
  160. // But since the convertors need to be updated also. This is easier.
  161. if (empty($modSettings['currentAttachmentUploadDir']))
  162. {
  163. updateSettings(array(
  164. 'attachmentUploadDir' => serialize(array(1 => $modSettings['attachmentUploadDir'])),
  165. 'currentAttachmentUploadDir' => 1,
  166. ));
  167. }
  168. // Integration is cool.
  169. if (defined('SMF_INTEGRATION_SETTINGS'))
  170. {
  171. $integration_settings = unserialize(SMF_INTEGRATION_SETTINGS);
  172. foreach ($integration_settings as $hook => $function)
  173. add_integration_function($hook, $function, '', false);
  174. }
  175. // Any files to pre include?
  176. if (!empty($modSettings['integrate_pre_include']))
  177. {
  178. $pre_includes = explode(',', $modSettings['integrate_pre_include']);
  179. foreach ($pre_includes as $include)
  180. {
  181. $include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
  182. if (file_exists($include))
  183. require_once($include);
  184. }
  185. }
  186. // Call pre load integration functions.
  187. call_integration_hook('integrate_pre_load');
  188. // This determines the server... not used in many places, except for login fixing.
  189. $context['server'] = array(
  190. 'is_iis' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false,
  191. 'is_apache' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false,
  192. 'is_litespeed' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false,
  193. 'is_lighttpd' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false,
  194. 'is_nginx' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false,
  195. 'is_cgi' => isset($_SERVER['SERVER_SOFTWARE']) && strpos(php_sapi_name(), 'cgi') !== false,
  196. 'is_windows' => strpos(PHP_OS, 'WIN') === 0,
  197. 'iso_case_folding' => ord(strtolower(chr(138))) === 154,
  198. );
  199. // A bug in some versions of IIS under CGI (older ones) makes cookie setting not work with Location: headers.
  200. $context['server']['needs_login_fix'] = $context['server']['is_cgi'] && $context['server']['is_iis'];
  201. }
  202. /**
  203. * Load all the important user information.
  204. * What it does:
  205. * - sets up the $user_info array
  206. * - assigns $user_info['query_wanna_see_board'] for what boards the user can see.
  207. * - first checks for cookie or integration validation.
  208. * - uses the current session if no integration function or cookie is found.
  209. * - checks password length, if member is activated and the login span isn't over.
  210. * - if validation fails for the user, $id_member is set to 0.
  211. * - updates the last visit time when needed.
  212. */
  213. function loadUserSettings()
  214. {
  215. global $modSettings, $user_settings, $sourcedir, $smcFunc;
  216. global $cookiename, $user_info, $language, $context;
  217. // Check first the integration, then the cookie, and last the session.
  218. if (count($integration_ids = call_integration_hook('integrate_verify_user')) > 0)
  219. {
  220. $id_member = 0;
  221. foreach ($integration_ids as $integration_id)
  222. {
  223. $integration_id = (int) $integration_id;
  224. if ($integration_id > 0)
  225. {
  226. $id_member = $integration_id;
  227. $already_verified = true;
  228. break;
  229. }
  230. }
  231. }
  232. else
  233. $id_member = 0;
  234. if (empty($id_member) && isset($_COOKIE[$cookiename]))
  235. {
  236. // Fix a security hole in PHP 4.3.9 and below...
  237. if (preg_match('~^a:[34]:\{i:0;i:\d{1,7};i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~i', $_COOKIE[$cookiename]) == 1)
  238. {
  239. list ($id_member, $password) = @unserialize($_COOKIE[$cookiename]);
  240. $id_member = !empty($id_member) && strlen($password) > 0 ? (int) $id_member : 0;
  241. }
  242. else
  243. $id_member = 0;
  244. }
  245. elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $_SERVER['HTTP_USER_AGENT'] || !empty($modSettings['disableCheckUA'])))
  246. {
  247. // @todo Perhaps we can do some more checking on this, such as on the first octet of the IP?
  248. list ($id_member, $password, $login_span) = @unserialize($_SESSION['login_' . $cookiename]);
  249. $id_member = !empty($id_member) && strlen($password) == 40 && $login_span > time() ? (int) $id_member : 0;
  250. }
  251. // Only load this stuff if the user isn't a guest.
  252. if ($id_member != 0)
  253. {
  254. // Is the member data cached?
  255. if (empty($modSettings['cache_enable']) || $modSettings['cache_enable'] < 2 || ($user_settings = cache_get_data('user_settings-' . $id_member, 60)) == null)
  256. {
  257. $request = $smcFunc['db_query']('', '
  258. SELECT mem.*, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type
  259. FROM {db_prefix}members AS mem
  260. LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = {int:id_member})
  261. WHERE mem.id_member = {int:id_member}
  262. LIMIT 1',
  263. array(
  264. 'id_member' => $id_member,
  265. )
  266. );
  267. $user_settings = $smcFunc['db_fetch_assoc']($request);
  268. $smcFunc['db_free_result']($request);
  269. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  270. cache_put_data('user_settings-' . $id_member, $user_settings, 60);
  271. }
  272. // Did we find 'im? If not, junk it.
  273. if (!empty($user_settings))
  274. {
  275. // As much as the password should be right, we can assume the integration set things up.
  276. if (!empty($already_verified) && $already_verified === true)
  277. $check = true;
  278. // SHA-1 passwords should be 40 characters long.
  279. elseif (strlen($password) == 40)
  280. $check = sha1($user_settings['passwd'] . $user_settings['password_salt']) == $password;
  281. else
  282. $check = false;
  283. // Wrong password or not activated - either way, you're going nowhere.
  284. $id_member = $check && ($user_settings['is_activated'] == 1 || $user_settings['is_activated'] == 11) ? (int) $user_settings['id_member'] : 0;
  285. }
  286. else
  287. $id_member = 0;
  288. // If we no longer have the member maybe they're being all hackey, stop brute force!
  289. if (!$id_member)
  290. {
  291. require_once($sourcedir . '/LogInOut.php');
  292. validatePasswordFlood(!empty($user_settings['id_member']) ? $user_settings['id_member'] : $id_member, !empty($user_settings['passwd_flood']) ? $user_settings['passwd_flood'] : false, $id_member != 0);
  293. }
  294. }
  295. // Found 'im, let's set up the variables.
  296. if ($id_member != 0)
  297. {
  298. // Let's not update the last visit time in these cases...
  299. // 1. SSI doesn't count as visiting the forum.
  300. // 2. RSS feeds and XMLHTTP requests don't count either.
  301. // 3. If it was set within this session, no need to set it again.
  302. // 4. New session, yet updated < five hours ago? Maybe cache can help.
  303. if (SMF != 'SSI' && !isset($_REQUEST['xml']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != '.xml') && empty($_SESSION['id_msg_last_visit']) && (empty($modSettings['cache_enable']) || ($_SESSION['id_msg_last_visit'] = cache_get_data('user_last_visit-' . $id_member, 5 * 3600)) === null))
  304. {
  305. // @todo can this be cached?
  306. // Do a quick query to make sure this isn't a mistake.
  307. $result = $smcFunc['db_query']('', '
  308. SELECT poster_time
  309. FROM {db_prefix}messages
  310. WHERE id_msg = {int:id_msg}
  311. LIMIT 1',
  312. array(
  313. 'id_msg' => $user_settings['id_msg_last_visit'],
  314. )
  315. );
  316. list ($visitTime) = $smcFunc['db_fetch_row']($result);
  317. $smcFunc['db_free_result']($result);
  318. $_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
  319. // If it was *at least* five hours ago...
  320. if ($visitTime < time() - 5 * 3600)
  321. {
  322. updateMemberData($id_member, array('id_msg_last_visit' => (int) $modSettings['maxMsgID'], 'last_login' => time(), 'member_ip' => $_SERVER['REMOTE_ADDR'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']));
  323. $user_settings['last_login'] = time();
  324. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  325. cache_put_data('user_settings-' . $id_member, $user_settings, 60);
  326. if (!empty($modSettings['cache_enable']))
  327. cache_put_data('user_last_visit-' . $id_member, $_SESSION['id_msg_last_visit'], 5 * 3600);
  328. }
  329. }
  330. elseif (empty($_SESSION['id_msg_last_visit']))
  331. $_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
  332. $username = $user_settings['member_name'];
  333. if (empty($user_settings['additional_groups']))
  334. $user_info = array(
  335. 'groups' => array($user_settings['id_group'], $user_settings['id_post_group'])
  336. );
  337. else
  338. $user_info = array(
  339. 'groups' => array_merge(
  340. array($user_settings['id_group'], $user_settings['id_post_group']),
  341. explode(',', $user_settings['additional_groups'])
  342. )
  343. );
  344. // Because history has proven that it is possible for groups to go bad - clean up in case.
  345. foreach ($user_info['groups'] as $k => $v)
  346. $user_info['groups'][$k] = (int) $v;
  347. // This is a logged in user, so definitely not a spider.
  348. $user_info['possibly_robot'] = false;
  349. }
  350. // If the user is a guest, initialize all the critical user settings.
  351. else
  352. {
  353. // This is what a guest's variables should be.
  354. $username = '';
  355. $user_info = array('groups' => array(-1));
  356. $user_settings = array();
  357. if (isset($_COOKIE[$cookiename]))
  358. $_COOKIE[$cookiename] = '';
  359. // Create a login token if it doesn't exist yet.
  360. if (!isset($_SESSION['token']['post-login']))
  361. createToken('login');
  362. else
  363. list ($context['login_token_var'],,, $context['login_token']) = $_SESSION['token']['post-login'];
  364. // Do we perhaps think this is a search robot? Check every five minutes just in case...
  365. if ((!empty($modSettings['spider_mode']) || !empty($modSettings['spider_group'])) && (!isset($_SESSION['robot_check']) || $_SESSION['robot_check'] < time() - 300))
  366. {
  367. require_once($sourcedir . '/ManageSearchEngines.php');
  368. $user_info['possibly_robot'] = SpiderCheck();
  369. }
  370. elseif (!empty($modSettings['spider_mode']))
  371. $user_info['possibly_robot'] = isset($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
  372. // If we haven't turned on proper spider hunts then have a guess!
  373. else
  374. {
  375. $ci_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  376. $user_info['possibly_robot'] = (strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla') === false && strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') === false) || strpos($ci_user_agent, 'googlebot') !== false || strpos($ci_user_agent, 'slurp') !== false || strpos($ci_user_agent, 'crawl') !== false || strpos($ci_user_agent, 'msnbot') !== false;
  377. }
  378. }
  379. // Set up the $user_info array.
  380. $user_info += array(
  381. 'id' => $id_member,
  382. 'username' => $username,
  383. 'name' => isset($user_settings['real_name']) ? $user_settings['real_name'] : '',
  384. 'email' => isset($user_settings['email_address']) ? $user_settings['email_address'] : '',
  385. 'passwd' => isset($user_settings['passwd']) ? $user_settings['passwd'] : '',
  386. 'language' => empty($user_settings['lngfile']) || empty($modSettings['userLanguage']) ? $language : $user_settings['lngfile'],
  387. 'is_guest' => $id_member == 0,
  388. 'is_admin' => in_array(1, $user_info['groups']),
  389. 'theme' => empty($user_settings['id_theme']) ? 0 : $user_settings['id_theme'],
  390. 'last_login' => empty($user_settings['last_login']) ? 0 : $user_settings['last_login'],
  391. 'ip' => $_SERVER['REMOTE_ADDR'],
  392. 'ip2' => $_SERVER['BAN_CHECK_IP'],
  393. 'posts' => empty($user_settings['posts']) ? 0 : $user_settings['posts'],
  394. 'time_format' => empty($user_settings['time_format']) ? $modSettings['time_format'] : $user_settings['time_format'],
  395. 'time_offset' => empty($user_settings['time_offset']) ? 0 : $user_settings['time_offset'],
  396. 'avatar' => array(
  397. 'url' => isset($user_settings['avatar']) ? $user_settings['avatar'] : '',
  398. 'filename' => empty($user_settings['filename']) ? '' : $user_settings['filename'],
  399. 'custom_dir' => !empty($user_settings['attachment_type']) && $user_settings['attachment_type'] == 1,
  400. 'id_attach' => isset($user_settings['id_attach']) ? $user_settings['id_attach'] : 0
  401. ),
  402. 'smiley_set' => isset($user_settings['smiley_set']) ? $user_settings['smiley_set'] : '',
  403. 'messages' => empty($user_settings['instant_messages']) ? 0 : $user_settings['instant_messages'],
  404. 'unread_messages' => empty($user_settings['unread_messages']) ? 0 : $user_settings['unread_messages'],
  405. 'alerts' => empty($user_settings['alerts']) ? 0 : $user_settings['alerts'],
  406. 'total_time_logged_in' => empty($user_settings['total_time_logged_in']) ? 0 : $user_settings['total_time_logged_in'],
  407. 'buddies' => !empty($modSettings['enable_buddylist']) && !empty($user_settings['buddy_list']) ? explode(',', $user_settings['buddy_list']) : array(),
  408. 'ignoreboards' => !empty($user_settings['ignore_boards']) && !empty($modSettings['allow_ignore_boards']) ? explode(',', $user_settings['ignore_boards']) : array(),
  409. 'ignoreusers' => !empty($user_settings['pm_ignore_list']) ? explode(',', $user_settings['pm_ignore_list']) : array(),
  410. 'warning' => isset($user_settings['warning']) ? $user_settings['warning'] : 0,
  411. 'permissions' => array(),
  412. );
  413. $user_info['groups'] = array_unique($user_info['groups']);
  414. // Make sure that the last item in the ignore boards array is valid. If the list was too long it could have an ending comma that could cause problems.
  415. if (!empty($user_info['ignoreboards']) && empty($user_info['ignoreboards'][$tmp = count($user_info['ignoreboards']) - 1]))
  416. unset($user_info['ignoreboards'][$tmp]);
  417. // Do we have any languages to validate this?
  418. if (!empty($modSettings['userLanguage']) && (!empty($_GET['language']) || !empty($_SESSION['language'])))
  419. $languages = getLanguages();
  420. // Allow the user to change their language if its valid.
  421. if (!empty($modSettings['userLanguage']) && !empty($_GET['language']) && isset($languages[strtr($_GET['language'], './\\:', '____')]))
  422. {
  423. $user_info['language'] = strtr($_GET['language'], './\\:', '____');
  424. $_SESSION['language'] = $user_info['language'];
  425. }
  426. elseif (!empty($modSettings['userLanguage']) && !empty($_SESSION['language']) && isset($languages[strtr($_SESSION['language'], './\\:', '____')]))
  427. $user_info['language'] = strtr($_SESSION['language'], './\\:', '____');
  428. // Just build this here, it makes it easier to change/use - administrators can see all boards.
  429. if ($user_info['is_admin'])
  430. $user_info['query_see_board'] = '1=1';
  431. // Otherwise just the groups in $user_info['groups'].
  432. else
  433. $user_info['query_see_board'] = '((FIND_IN_SET(' . implode(', b.member_groups) != 0 OR FIND_IN_SET(', $user_info['groups']) . ', b.member_groups) != 0)' . (!empty($modSettings['deny_boards_access']) ? ' AND (FIND_IN_SET(' . implode(', b.deny_member_groups) = 0 AND FIND_IN_SET(', $user_info['groups']) . ', b.deny_member_groups) = 0)' : '') . (isset($user_info['mod_cache']) ? ' OR ' . $user_info['mod_cache']['mq'] : '') . ')';
  434. // Build the list of boards they WANT to see.
  435. // This will take the place of query_see_boards in certain spots, so it better include the boards they can see also
  436. // If they aren't ignoring any boards then they want to see all the boards they can see
  437. if (empty($user_info['ignoreboards']))
  438. $user_info['query_wanna_see_board'] = $user_info['query_see_board'];
  439. // Ok I guess they don't want to see all the boards
  440. else
  441. $user_info['query_wanna_see_board'] = '(' . $user_info['query_see_board'] . ' AND b.id_board NOT IN (' . implode(',', $user_info['ignoreboards']) . '))';
  442. call_integration_hook('integrate_user_info');
  443. }
  444. /**
  445. * Check for moderators and see if they have access to the board.
  446. * What it does:
  447. * - sets up the $board_info array for current board information.
  448. * - if cache is enabled, the $board_info array is stored in cache.
  449. * - redirects to appropriate post if only message id is requested.
  450. * - is only used when inside a topic or board.
  451. * - determines the local moderators for the board.
  452. * - adds group id 3 if the user is a local moderator for the board they are in.
  453. * - prevents access if user is not in proper group nor a local moderator of the board.
  454. */
  455. function loadBoard()
  456. {
  457. global $txt, $scripturl, $context, $modSettings;
  458. global $board_info, $board, $topic, $user_info, $smcFunc;
  459. // Assume they are not a moderator.
  460. $user_info['is_mod'] = false;
  461. $context['user']['is_mod'] = &$user_info['is_mod'];
  462. // Start the linktree off empty..
  463. $context['linktree'] = array();
  464. // Have they by chance specified a message id but nothing else?
  465. if (empty($_REQUEST['action']) && empty($topic) && empty($board) && !empty($_REQUEST['msg']))
  466. {
  467. // Make sure the message id is really an int.
  468. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  469. // Looking through the message table can be slow, so try using the cache first.
  470. if (($topic = cache_get_data('msg_topic-' . $_REQUEST['msg'], 120)) === null)
  471. {
  472. $request = $smcFunc['db_query']('', '
  473. SELECT id_topic
  474. FROM {db_prefix}messages
  475. WHERE id_msg = {int:id_msg}
  476. LIMIT 1',
  477. array(
  478. 'id_msg' => $_REQUEST['msg'],
  479. )
  480. );
  481. // So did it find anything?
  482. if ($smcFunc['db_num_rows']($request))
  483. {
  484. list ($topic) = $smcFunc['db_fetch_row']($request);
  485. $smcFunc['db_free_result']($request);
  486. // Save save save.
  487. cache_put_data('msg_topic-' . $_REQUEST['msg'], $topic, 120);
  488. }
  489. }
  490. // Remember redirection is the key to avoiding fallout from your bosses.
  491. if (!empty($topic))
  492. redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg']);
  493. else
  494. {
  495. loadPermissions();
  496. loadTheme();
  497. fatal_lang_error('topic_gone', false);
  498. }
  499. }
  500. // Load this board only if it is specified.
  501. if (empty($board) && empty($topic))
  502. {
  503. $board_info = array('moderators' => array(), 'moderator_groups' => array());
  504. return;
  505. }
  506. if (!empty($modSettings['cache_enable']) && (empty($topic) || $modSettings['cache_enable'] >= 3))
  507. {
  508. // @todo SLOW?
  509. if (!empty($topic))
  510. $temp = cache_get_data('topic_board-' . $topic, 120);
  511. else
  512. $temp = cache_get_data('board-' . $board, 120);
  513. if (!empty($temp))
  514. {
  515. $board_info = $temp;
  516. $board = $board_info['id'];
  517. }
  518. }
  519. if (empty($temp))
  520. {
  521. $request = $smcFunc['db_query']('', '
  522. SELECT
  523. c.id_cat, b.name AS bname, b.description, b.num_topics, b.member_groups, b.deny_member_groups,
  524. b.id_parent, c.name AS cname, IFNULL(mg.id_group, 0) AS id_moderator_group, mg.group_name,
  525. IFNULL(mem.id_member, 0) AS id_moderator,
  526. mem.real_name' . (!empty($topic) ? ', b.id_board' : '') . ', b.child_level,
  527. b.id_theme, b.override_theme, b.count_posts, b.id_profile, b.redirect,
  528. b.unapproved_topics, b.unapproved_posts' . (!empty($topic) ? ', t.approved, t.id_member_started' : '') . '
  529. FROM {db_prefix}boards AS b' . (!empty($topic) ? '
  530. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})' : '') . '
  531. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  532. LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = {raw:board_link})
  533. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = modgs.id_group)
  534. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = {raw:board_link})
  535. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  536. WHERE b.id_board = {raw:board_link}',
  537. array(
  538. 'current_topic' => $topic,
  539. 'board_link' => empty($topic) ? $smcFunc['db_quote']('{int:current_board}', array('current_board' => $board)) : 't.id_board',
  540. )
  541. );
  542. // If there aren't any, skip.
  543. if ($smcFunc['db_num_rows']($request) > 0)
  544. {
  545. $row = $smcFunc['db_fetch_assoc']($request);
  546. // Set the current board.
  547. if (!empty($row['id_board']))
  548. $board = $row['id_board'];
  549. // Basic operating information. (globals... :/)
  550. $board_info = array(
  551. 'id' => $board,
  552. 'moderators' => array(),
  553. 'moderator_groups' => array(),
  554. 'cat' => array(
  555. 'id' => $row['id_cat'],
  556. 'name' => $row['cname']
  557. ),
  558. 'name' => $row['bname'],
  559. 'description' => $row['description'],
  560. 'num_topics' => $row['num_topics'],
  561. 'unapproved_topics' => $row['unapproved_topics'],
  562. 'unapproved_posts' => $row['unapproved_posts'],
  563. 'unapproved_user_topics' => 0,
  564. 'parent_boards' => getBoardParents($row['id_parent']),
  565. 'parent' => $row['id_parent'],
  566. 'child_level' => $row['child_level'],
  567. 'theme' => $row['id_theme'],
  568. 'override_theme' => !empty($row['override_theme']),
  569. 'profile' => $row['id_profile'],
  570. 'redirect' => $row['redirect'],
  571. 'recycle' => !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) && $modSettings['recycle_board'] == $board,
  572. 'posts_count' => empty($row['count_posts']),
  573. 'cur_topic_approved' => empty($topic) || $row['approved'],
  574. 'cur_topic_starter' => empty($topic) ? 0 : $row['id_member_started'],
  575. );
  576. // Load the membergroups allowed, and check permissions.
  577. $board_info['groups'] = $row['member_groups'] == '' ? array() : explode(',', $row['member_groups']);
  578. $board_info['deny_groups'] = $row['deny_member_groups'] == '' ? array() : explode(',', $row['deny_member_groups']);
  579. do
  580. {
  581. if (!empty($row['id_moderator']))
  582. $board_info['moderators'][$row['id_moderator']] = array(
  583. 'id' => $row['id_moderator'],
  584. 'name' => $row['real_name'],
  585. 'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
  586. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
  587. );
  588. if (!empty($row['id_moderator_group']))
  589. $board_info['moderator_groups'][$row['id_moderator_group']] = array(
  590. 'id' => $row['id_moderator_group'],
  591. 'name' => $row['group_name'],
  592. 'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
  593. 'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
  594. );
  595. }
  596. while ($row = $smcFunc['db_fetch_assoc']($request));
  597. // If the board only contains unapproved posts and the user isn't an approver then they can't see any topics.
  598. // If that is the case do an additional check to see if they have any topics waiting to be approved.
  599. if ($board_info['num_topics'] == 0 && $modSettings['postmod_active'] && !allowedTo('approve_posts'))
  600. {
  601. // Free the previous result
  602. $smcFunc['db_free_result']($request);
  603. // @todo why is this using id_topic?
  604. // @todo Can this get cached?
  605. $request = $smcFunc['db_query']('', '
  606. SELECT COUNT(id_topic)
  607. FROM {db_prefix}topics
  608. WHERE id_member_started={int:id_member}
  609. AND approved = {int:unapproved}
  610. AND id_board = {int:board}',
  611. array(
  612. 'id_member' => $user_info['id'],
  613. 'unapproved' => 0,
  614. 'board' => $board,
  615. )
  616. );
  617. list ($board_info['unapproved_user_topics']) = $smcFunc['db_fetch_row']($request);
  618. }
  619. if (!empty($modSettings['cache_enable']) && (empty($topic) || $modSettings['cache_enable'] >= 3))
  620. {
  621. // @todo SLOW?
  622. if (!empty($topic))
  623. cache_put_data('topic_board-' . $topic, $board_info, 120);
  624. cache_put_data('board-' . $board, $board_info, 120);
  625. }
  626. }
  627. else
  628. {
  629. // Otherwise the topic is invalid, there are no moderators, etc.
  630. $board_info = array(
  631. 'moderators' => array(),
  632. 'moderator_groups' => array(),
  633. 'error' => 'exist'
  634. );
  635. $topic = null;
  636. $board = 0;
  637. }
  638. $smcFunc['db_free_result']($request);
  639. }
  640. if (!empty($topic))
  641. $_GET['board'] = (int) $board;
  642. if (!empty($board))
  643. {
  644. // Get this into an array of keys for array_intersect
  645. $moderator_groups = array_keys($board_info['moderator_groups']);
  646. // Now check if the user is a moderator.
  647. $user_info['is_mod'] = isset($board_info['moderators'][$user_info['id']]) || count(array_intersect($user_info['groups'], $moderator_groups)) != 0;
  648. if (count(array_intersect($user_info['groups'], $board_info['groups'])) == 0 && !$user_info['is_admin'])
  649. $board_info['error'] = 'access';
  650. if (!empty($modSettings['deny_boards_access']) && count(array_intersect($user_info['groups'], $board_info['deny_groups'])) != 0 && !$user_info['is_admin'])
  651. $board_info['error'] = 'access';
  652. // Build up the linktree.
  653. $context['linktree'] = array_merge(
  654. $context['linktree'],
  655. array(array(
  656. 'url' => $scripturl . '#c' . $board_info['cat']['id'],
  657. 'name' => $board_info['cat']['name']
  658. )),
  659. array_reverse($board_info['parent_boards']),
  660. array(array(
  661. 'url' => $scripturl . '?board=' . $board . '.0',
  662. 'name' => $board_info['name']
  663. ))
  664. );
  665. }
  666. // Set the template contextual information.
  667. $context['user']['is_mod'] = &$user_info['is_mod'];
  668. $context['current_topic'] = $topic;
  669. $context['current_board'] = $board;
  670. // Hacker... you can't see this topic, I'll tell you that. (but moderators can!)
  671. if (!empty($board_info['error']) && (!empty($modSettings['deny_boards_access']) || $board_info['error'] != 'access' || !$user_info['is_mod']))
  672. {
  673. // The permissions and theme need loading, just to make sure everything goes smoothly.
  674. loadPermissions();
  675. loadTheme();
  676. $_GET['board'] = '';
  677. $_GET['topic'] = '';
  678. // The linktree should not give the game away mate!
  679. $context['linktree'] = array(
  680. array(
  681. 'url' => $scripturl,
  682. 'name' => $context['forum_name_html_safe']
  683. )
  684. );
  685. // If it's a prefetching agent or we're requesting an attachment.
  686. if ((isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') || (!empty($_REQUEST['action']) && $_REQUEST['action'] === 'dlattach'))
  687. {
  688. ob_end_clean();
  689. header('HTTP/1.1 403 Forbidden');
  690. die;
  691. }
  692. elseif ($user_info['is_guest'])
  693. {
  694. loadLanguage('Errors');
  695. is_not_guest($txt['topic_gone']);
  696. }
  697. else
  698. fatal_lang_error('topic_gone', false);
  699. }
  700. if ($user_info['is_mod'])
  701. $user_info['groups'][] = 3;
  702. }
  703. /**
  704. * Load this user's permissions.
  705. *
  706. */
  707. function loadPermissions()
  708. {
  709. global $user_info, $board, $board_info, $modSettings, $smcFunc, $sourcedir;
  710. if ($user_info['is_admin'])
  711. {
  712. banPermissions();
  713. return;
  714. }
  715. if (!empty($modSettings['cache_enable']))
  716. {
  717. $cache_groups = $user_info['groups'];
  718. asort($cache_groups);
  719. $cache_groups = implode(',', $cache_groups);
  720. // If it's a spider then cache it different.
  721. if ($user_info['possibly_robot'])
  722. $cache_groups .= '-spider';
  723. if ($modSettings['cache_enable'] >= 2 && !empty($board) && ($temp = cache_get_data('permissions:' . $cache_groups . ':' . $board, 240)) != null && time() - 240 > $modSettings['settings_updated'])
  724. {
  725. list ($user_info['permissions']) = $temp;
  726. banPermissions();
  727. return;
  728. }
  729. elseif (($temp = cache_get_data('permissions:' . $cache_groups, 240)) != null && time() - 240 > $modSettings['settings_updated'])
  730. list ($user_info['permissions'], $removals) = $temp;
  731. }
  732. // If it is detected as a robot, and we are restricting permissions as a special group - then implement this.
  733. $spider_restrict = $user_info['possibly_robot'] && !empty($modSettings['spider_group']) ? ' OR (id_group = {int:spider_group} AND add_deny = 0)' : '';
  734. if (empty($user_info['permissions']))
  735. {
  736. // Get the general permissions.
  737. $request = $smcFunc['db_query']('', '
  738. SELECT permission, add_deny
  739. FROM {db_prefix}permissions
  740. WHERE id_group IN ({array_int:member_groups})
  741. ' . $spider_restrict,
  742. array(
  743. 'member_groups' => $user_info['groups'],
  744. 'spider_group' => !empty($modSettings['spider_group']) ? $modSettings['spider_group'] : 0,
  745. )
  746. );
  747. $removals = array();
  748. while ($row = $smcFunc['db_fetch_assoc']($request))
  749. {
  750. if (empty($row['add_deny']))
  751. $removals[] = $row['permission'];
  752. else
  753. $user_info['permissions'][] = $row['permission'];
  754. }
  755. $smcFunc['db_free_result']($request);
  756. if (isset($cache_groups))
  757. cache_put_data('permissions:' . $cache_groups, array($user_info['permissions'], $removals), 240);
  758. }
  759. // Get the board permissions.
  760. if (!empty($board))
  761. {
  762. // Make sure the board (if any) has been loaded by loadBoard().
  763. if (!isset($board_info['profile']))
  764. fatal_lang_error('no_board');
  765. $request = $smcFunc['db_query']('', '
  766. SELECT permission, add_deny
  767. FROM {db_prefix}board_permissions
  768. WHERE (id_group IN ({array_int:member_groups})
  769. ' . $spider_restrict . ')
  770. AND id_profile = {int:id_profile}',
  771. array(
  772. 'member_groups' => $user_info['groups'],
  773. 'id_profile' => $board_info['profile'],
  774. 'spider_group' => !empty($modSettings['spider_group']) ? $modSettings['spider_group'] : 0,
  775. )
  776. );
  777. while ($row = $smcFunc['db_fetch_assoc']($request))
  778. {
  779. if (empty($row['add_deny']))
  780. $removals[] = $row['permission'];
  781. else
  782. $user_info['permissions'][] = $row['permission'];
  783. }
  784. $smcFunc['db_free_result']($request);
  785. }
  786. // Remove all the permissions they shouldn't have ;).
  787. if (!empty($modSettings['permission_enable_deny']))
  788. $user_info['permissions'] = array_diff($user_info['permissions'], $removals);
  789. if (isset($cache_groups) && !empty($board) && $modSettings['cache_enable'] >= 2)
  790. cache_put_data('permissions:' . $cache_groups . ':' . $board, array($user_info['permissions'], null), 240);
  791. // Banned? Watch, don't touch..
  792. banPermissions();
  793. // Load the mod cache so we can know what additional boards they should see, but no sense in doing it for guests
  794. if (!$user_info['is_guest'])
  795. {
  796. if (!isset($_SESSION['mc']) || $_SESSION['mc']['time'] <= $modSettings['settings_updated'])
  797. {
  798. require_once($sourcedir . '/Subs-Auth.php');
  799. rebuildModCache();
  800. }
  801. else
  802. $user_info['mod_cache'] = $_SESSION['mc'];
  803. // This is a useful phantom permission added to the current user, and only the current user while they are logged in.
  804. // For example this drastically simplifies certain changes to the profile area.
  805. $user_info['permissions'][] = 'is_not_guest';
  806. // And now some backwards compatibility stuff for mods and whatnot that aren't expecting the new permissions.
  807. $user_info['permissions'][] = 'profile_view_own';
  808. if (in_array('profile_view', $user_info['permissions']))
  809. $user_info['permissions'][] = 'profile_view_any';
  810. }
  811. }
  812. /**
  813. * Loads an array of users' data by ID or member_name.
  814. *
  815. * @param array|string $users An array of users by id or name or a single username/id
  816. * @param bool $is_name Whether $users contains names
  817. * @param string $set What kind of data to load (normal, profile, minimal)
  818. * @return array|bool The ids of the members loaded or false if no data was loaded
  819. */
  820. function loadMemberData($users, $is_name = false, $set = 'normal')
  821. {
  822. global $user_profile, $modSettings, $board_info, $smcFunc, $context;
  823. // Can't just look for no users :P.
  824. if (empty($users))
  825. return false;
  826. // Pass the set value
  827. $context['loadMemberContext_set'] = $set;
  828. // Make sure it's an array.
  829. $users = !is_array($users) ? array($users) : array_unique($users);
  830. $loaded_ids = array();
  831. if (!$is_name && !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3)
  832. {
  833. $users = array_values($users);
  834. for ($i = 0, $n = count($users); $i < $n; $i++)
  835. {
  836. $data = cache_get_data('member_data-' . $set . '-' . $users[$i], 240);
  837. if ($data == null)
  838. continue;
  839. $loaded_ids[] = $data['id_member'];
  840. $user_profile[$data['id_member']] = $data;
  841. unset($users[$i]);
  842. }
  843. }
  844. // Used by default
  845. $select_columns = '
  846. IFNULL(lo.log_time, 0) AS is_online, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type,
  847. mem.signature, mem.personal_text, mem.location, mem.gender, mem.avatar, mem.id_member, mem.member_name,
  848. mem.real_name, mem.email_address, mem.hide_email, mem.date_registered, mem.website_title, mem.website_url,
  849. mem.birthdate, mem.member_ip, mem.member_ip2, mem.icq, mem.aim, mem.yim, mem.skype, mem.posts, mem.last_login,
  850. mem.karma_good, mem.id_post_group, mem.karma_bad, mem.lngfile, mem.id_group, mem.time_offset, mem.show_online,
  851. mg.online_color AS member_group_color, IFNULL(mg.group_name, {string:blank_string}) AS member_group,
  852. pg.online_color AS post_group_color, IFNULL(pg.group_name, {string:blank_string}) AS post_group,
  853. mem.is_activated, mem.warning, ' . (!empty($modSettings['titlesEnable']) ? 'mem.usertitle, ' : '') . '
  854. CASE WHEN mem.id_group = 0 OR mg.icons = {string:blank_string} THEN pg.icons ELSE mg.icons END AS icons';
  855. $select_tables = '
  856. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
  857. LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
  858. LEFT JOIN {db_prefix}membergroups AS pg ON (pg.id_group = mem.id_post_group)
  859. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)';
  860. // We add or replace according the the set
  861. switch ($set)
  862. {
  863. case 'normal':
  864. $select_columns .= ', mem.buddy_list, mem.additional_groups';
  865. break;
  866. case 'profile':
  867. $select_columns .= ', mem.additional_groups, mem.openid_uri, mem.id_theme, mem.pm_ignore_list, mem.pm_email_notify, mem.pm_receive_from,
  868. mem.time_format, mem.secret_question, mem.smiley_set,
  869. mem.total_time_logged_in, mem.notify_announcements, mem.notify_regularity, mem.notify_send_body,
  870. mem.notify_types, lo.url, mem.ignore_boards, mem.password_salt, mem.pm_prefs, mem.buddy_list';
  871. break;
  872. case 'minimal':
  873. $select_columns = '
  874. mem.id_member, mem.member_name, mem.real_name, mem.email_address, mem.hide_email, mem.date_registered,
  875. mem.posts, mem.last_login, mem.member_ip, mem.member_ip2, mem.lngfile, mem.id_group';
  876. $select_tables = '';
  877. break;
  878. default:
  879. trigger_error('loadMemberData(): Invalid member data set \'' . $set . '\'', E_USER_WARNING);
  880. }
  881. // Allow mods to easily add to the selected member data
  882. call_integration_hook('integrate_load_member_data', array(&$select_columns, &$select_tables, &$set));
  883. if (!empty($users))
  884. {
  885. // Load the member's data.
  886. $request = $smcFunc['db_query']('', '
  887. SELECT' . $select_columns . '
  888. FROM {db_prefix}members AS mem' . $select_tables . '
  889. WHERE mem.' . ($is_name ? 'member_name' : 'id_member') . ' IN ({' . ($is_name ? 'array_string' : 'array_int') . ':users})',
  890. array(
  891. 'blank_string' => '',
  892. 'users' => $users,
  893. )
  894. );
  895. $new_loaded_ids = array();
  896. while ($row = $smcFunc['db_fetch_assoc']($request))
  897. {
  898. $new_loaded_ids[] = $row['id_member'];
  899. $loaded_ids[] = $row['id_member'];
  900. $row['options'] = array();
  901. $user_profile[$row['id_member']] = $row;
  902. }
  903. $smcFunc['db_free_result']($request);
  904. }
  905. if (!empty($new_loaded_ids) && $set !== 'minimal')
  906. {
  907. $request = $smcFunc['db_query']('', '
  908. SELECT *
  909. FROM {db_prefix}themes
  910. WHERE id_member IN ({array_int:loaded_ids})',
  911. array(
  912. 'loaded_ids' => $new_loaded_ids,
  913. )
  914. );
  915. while ($row = $smcFunc['db_fetch_assoc']($request))
  916. $user_profile[$row['id_member']]['options'][$row['variable']] = $row['value'];
  917. $smcFunc['db_free_result']($request);
  918. }
  919. $additional_mods = array();
  920. // Are any of these users in groups assigned to moderate this board?
  921. if (!empty($loaded_ids) && !empty($board_info['moderator_groups']) && $set === 'normal')
  922. {
  923. foreach ($loaded_ids as $a_member)
  924. {
  925. if (!empty($user_profile[$a_member]['additional_groups']))
  926. $groups = array_merge(array($user_profile[$a_member]['id_group']), explode(',', $user_profile[$a_member]['additional_groups']));
  927. else
  928. $groups = array($user_profile[$a_member]['id_group']);
  929. $temp = array_intersect($groups, array_keys($board_info['moderator_groups']));
  930. if (!empty($temp))
  931. {
  932. $additional_mods[] = $a_member;
  933. }
  934. }
  935. }
  936. if (!empty($new_loaded_ids) && !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3)
  937. {
  938. for ($i = 0, $n = count($new_loaded_ids); $i < $n; $i++)
  939. cache_put_data('member_data-' . $set . '-' . $new_loaded_ids[$i], $user_profile[$new_loaded_ids[$i]], 240);
  940. }
  941. // Are we loading any moderators? If so, fix their group data...
  942. if (!empty($loaded_ids) && (!empty($board_info['moderators']) || !empty($board_info['moderator_groups'])) && $set === 'normal' && count($temp_mods = array_merge(array_intersect($loaded_ids, array_keys($board_info['moderators'])), $additional_mods)) !== 0)
  943. {
  944. if (($row = cache_get_data('moderator_group_info', 480)) == null)
  945. {
  946. $request = $smcFunc['db_query']('', '
  947. SELECT group_name AS member_group, online_color AS member_group_color, icons
  948. FROM {db_prefix}membergroups
  949. WHERE id_group = {int:moderator_group}
  950. LIMIT 1',
  951. array(
  952. 'moderator_group' => 3,
  953. )
  954. );
  955. $row = $smcFunc['db_fetch_assoc']($request);
  956. $smcFunc['db_free_result']($request);
  957. cache_put_data('moderator_group_info', $row, 480);
  958. }
  959. foreach ($temp_mods as $id)
  960. {
  961. // By popular demand, don't show admins or global moderators as moderators.
  962. if ($user_profile[$id]['id_group'] != 1 && $user_profile[$id]['id_group'] != 2)
  963. $user_profile[$id]['member_group'] = $row['member_group'];
  964. // If the Moderator group has no color or icons, but their group does... don't overwrite.
  965. if (!empty($row['icons']))
  966. $user_profile[$id]['icons'] = $row['icons'];
  967. if (!empty($row['member_group_color']))
  968. $user_profile[$id]['member_group_color'] = $row['member_group_color'];
  969. }
  970. }
  971. return empty($loaded_ids) ? false : $loaded_ids;
  972. }
  973. /**
  974. * Loads the user's basic values... meant for template/theme usage.
  975. *
  976. * @param int $user The ID of a user previously loaded by {@link loadMemberData()}
  977. * @param bool $display_custom_fields Whether or not to display custom profile fields
  978. * @return boolean Whether or not the data was loaded successfully
  979. */
  980. function loadMemberContext($user, $display_custom_fields = false)
  981. {
  982. global $memberContext, $user_profile, $txt, $scripturl, $user_info;
  983. global $context, $modSettings, $settings;
  984. global $smcFunc;
  985. static $dataLoaded = array();
  986. // If this person's data is already loaded, skip it.
  987. if (isset($dataLoaded[$user]))
  988. return true;
  989. // We can't load guests or members not loaded by loadMemberData()!
  990. if ($user == 0)
  991. return false;
  992. if (!isset($user_profile[$user]))
  993. {
  994. trigger_error('loadMemberContext(): member id ' . $user . ' not previously loaded by loadMemberData()', E_USER_WARNING);
  995. return false;
  996. }
  997. // Well, it's loaded now anyhow.
  998. $dataLoaded[$user] = true;
  999. $profile = $user_profile[$user];
  1000. // Censor everything.
  1001. censorText($profile['signature']);
  1002. censorText($profile['personal_text']);
  1003. censorText($profile['location']);
  1004. // Set things up to be used before hand.
  1005. $gendertxt = $profile['gender'] == 2 ? $txt['female'] : ($profile['gender'] == 1 ? $txt['male'] : '');
  1006. $profile['signature'] = str_replace(array("\n", "\r"), array('<br>', ''), $profile['signature']);
  1007. $profile['signature'] = parse_bbc($profile['signature'], true, 'sig' . $profile['id_member']);
  1008. $profile['is_online'] = (!empty($profile['show_online']) || allowedTo('moderate_forum')) && $profile['is_online'] > 0;
  1009. $profile['icons'] = empty($profile['icons']) ? array('', '') : explode('#', $profile['icons']);
  1010. // Setup the buddy status here (One whole in_array call saved :P)
  1011. $profile['buddy'] = in_array($profile['id_member'], $user_info['buddies']);
  1012. $buddy_list = !empty($profile['buddy_list']) ? explode(',', $profile['buddy_list']) : array();
  1013. // These minimal values are always loaded
  1014. $memberContext[$user] = array(
  1015. 'username' => $profile['member_name'],
  1016. 'name' => $profile['real_name'],
  1017. 'id' => $profile['id_member'],
  1018. 'href' => $scripturl . '?action=profile;u=' . $profile['id_member'],
  1019. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $profile['real_name'] . '">' . $profile['real_name'] . '</a>',
  1020. 'email' => $profile['email_address'],
  1021. 'show_email' => !$user_info['is_guest'] && ($user_info['id'] == $profile['id_member'] || allowedTo('moderate_forum')),
  1022. 'registered' => empty($profile['date_registered']) ? $txt['not_applicable'] : timeformat($profile['date_registered']),
  1023. 'registered_timestamp' => empty($profile['date_registered']) ? 0 : forum_time(true, $profile['date_registered']),
  1024. );
  1025. // If the set isn't minimal then load the monstrous array.
  1026. if ($context['loadMemberContext_set'] != 'minimal')
  1027. $memberContext[$user] += array(
  1028. 'username_color' => '<span '. (!empty($profile['member_group_color']) ? 'style="color:'. $profile['member_group_color'] .';"' : '') .'>'. $profile['member_name'] .'</span>',
  1029. 'name_color' => '<span '. (!empty($profile['member_group_color']) ? 'style="color:'. $profile['member_group_color'] .';"' : '') .'>'. $profile['real_name'] .'</span>',
  1030. 'link_color' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $profile['real_name'] . '" '. (!empty($profile['member_group_color']) ? 'style="color:'. $profile['member_group_color'] .';"' : '') .'>' . $profile['real_name'] . '</a>',
  1031. 'is_buddy' => $profile['buddy'],
  1032. 'is_reverse_buddy' => in_array($user_info['id'], $buddy_list),
  1033. 'buddies' => $buddy_list,
  1034. 'title' => !empty($modSettings['titlesEnable']) ? $profile['usertitle'] : '',
  1035. 'blurb' => $profile['personal_text'],
  1036. 'gender' => array(
  1037. 'name' => $gendertxt,
  1038. 'image' => !empty($profile['gender']) ? '<span class="generic_icons ' . ($profile['gender'] == 1 ? 'gender_male' : 'gender_female') . '" title="' . $gendertxt . '"></span>' : ''
  1039. ),
  1040. 'website' => array(
  1041. 'title' => $profile['website_title'],
  1042. 'url' => $profile['website_url'],
  1043. ),
  1044. 'birth_date' => empty($profile['birthdate']) || $profile['birthdate'] === '0001-01-01' ? '0000-00-00' : (substr($profile['birthdate'], 0, 4) === '0004' ? '0000' . substr($profile['birthdate'], 4) : $profile['birthdate']),
  1045. 'signature' => $profile['signature'],
  1046. 'location' => $profile['location'],
  1047. 'icq' => $profile['icq'] != '' && !$user_info['is_guest'] ? array(
  1048. 'name' => $profile['icq'],
  1049. 'href' => 'http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'],
  1050. 'link' => '<a class="icq new_win" href="http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'] . '" target="_blank" title="' . $txt['icq_title'] . ' - ' . $profile['icq'] . '"><img src="' . $settings['images_url'] . '/icq.png" alt="' . $txt['icq'] . ' - ' . $profile['icq'] . '"></a>',
  1051. 'link_text' => '<a class="icq extern" href="http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'] . '" title="' . $txt['icq_title'] . ' - ' . $profile['icq'] . '">' . $profile['icq'] . '</a>',
  1052. ) : array('name' => '', 'add' => '', 'href' => '', 'link' => '', 'link_text' => ''),
  1053. 'aim' => $profile['aim'] != '' && !$user_info['is_guest'] ? array(
  1054. 'name' => $profile['aim'],
  1055. 'href' => 'aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'],
  1056. 'link' => '<a class="aim" href="aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'] . '" title="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '"><img src="' . $settings['images_url'] . '/aim.png" alt="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '"></a>',
  1057. 'link_text' => '<a class="aim" href="aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'] . '" title="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '">' . $profile['aim'] . '</a>'
  1058. ) : array('name' => '', 'href' => '', 'link' => '', 'link_text' => ''),
  1059. 'yim' => $profile['yim'] != '' && !$user_info['is_guest'] ? array(
  1060. 'name' => $profile['yim'],
  1061. 'href' => 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']),
  1062. 'link' => '<a class="yim" href="http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']) . '" title="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '"><img src="' . $settings['images_url'] . '/yahoo.png" alt="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '"></a>',
  1063. 'link_text' => '<a class="yim" href="http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']) . '" title="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '">' . $profile['yim'] . '</a>'
  1064. ) : array('name' => '', 'href' => '', 'link' => '', 'link_text' => ''),
  1065. 'skype' => !empty($profile['skype']) && !$user_info['is_guest'] ? array(
  1066. 'name' => $profile['skype'],
  1067. 'href' => 'skype:' . $profile['skype'] . '?chat',
  1068. 'link' => '<a class="skype new_win" href="skype:' . $profile['skype'] . '?chat" title="' . $txt['skype'] . ' - ' . $profile['skype'] . '"><img src="' . $settings['images_url'] . '/skype.png" alt="' . $txt['skype'] . ' - ' . $profile['skype'] . '"></a>',
  1069. 'link_text' => '<a class="skype new_win" href="skype:' . $profile['skype'] . '?chat" title="' . $txt['skype'] . ' - ' . $profile['skype'] . '">' . $profile['skype'] . '</a>',
  1070. ) : array('name' => '', 'href' => '', 'link' => '', 'link_text' => '',),
  1071. 'real_posts' => $profile['posts'],
  1072. 'posts' => $profile['posts'] > 500000 ? $txt['geek'] : comma_format($profile['posts']),
  1073. 'avatar' => array(
  1074. 'name' => $profile['avatar'],
  1075. 'image' => $profile['avatar'] == '' ? ($profile['id_attach'] > 0 ? '<img class="avatar" src="' . (empty($profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $profile['filename']) . '" alt="">' : '') : (stristr($profile['avatar'], 'http://') || stristr($profile['avatar'], 'https://') ? '<img class="avatar" src="' . $profile['avatar'] . '" alt="">' : '<img class="avatar" src="' . $modSettings['avatar_url'] . '/' . $smcFunc['htmlspecialchars']($profile['avatar']) . '" alt="">'),
  1076. 'href' => $profile['avatar'] == '' ? ($profile['id_attach'] > 0 ? (empty($profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $profile['filename']) : '') : (stristr($profile['avatar'], 'http://') || stristr($profile['avatar'], 'https://') ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar']),
  1077. 'url' => $profile['avatar'] == '' ? '' : (stristr($profile['avatar'], 'http://') || stristr($profile['avatar'], 'https://') ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar'])
  1078. ),
  1079. 'last_login' => empty($profile['last_login']) ? $txt['never'] : timeformat($profile['last_login']),
  1080. 'last_login_timestamp' => empty($profile['last_login']) ? 0 : forum_time(0, $profile['last_login']),
  1081. 'karma' => array(
  1082. 'good' => $profile['karma_good'],
  1083. 'bad' => $profile['karma_bad'],
  1084. 'allow' => !$user_info['is_guest'] && !empty($modSettings['karmaMode']) && $user_info['id'] != $user && allowedTo('karma_edit') &&
  1085. ($user_info['posts'] >= $modSettings['karmaMinPosts'] || $user_info['is_admin']),
  1086. ),
  1087. 'ip' => $smcFunc['htmlspecialchars']($profile['member_ip']),
  1088. 'ip2' => $smcFunc['htmlspecialchars']($profile['member_ip2']),
  1089. 'online' => array(
  1090. 'is_online' => $profile['is_online'],
  1091. 'text' => $smcFunc['htmlspecialchars']($txt[$profile['is_online'] ? 'online' : 'offline']),
  1092. 'member_online_text' => sprintf($txt[$profile['is_online'] ? 'member_is_online' : 'member_is_offline'], $smcFunc['htmlspecialchars']($profile['real_name'])),
  1093. 'href' => $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'],
  1094. 'link' => '<a href="' . $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'] . '">' . $txt[$profile['is_online'] ? 'online' : 'offline'] . '</a>',
  1095. 'image_href' => $settings['images_url'] . '/' . ($profile['buddy'] ? 'buddy_' : '') . ($profile['is_online'] ? 'useron' : 'useroff') . '.png',
  1096. 'label' => $txt[$profile['is_online'] ? 'online' : 'offline']
  1097. ),
  1098. 'language' => $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))),
  1099. 'is_activated' => isset($profile['is_activated']) ? $profile['is_activated'] : 1,
  1100. 'is_banned' => isset($profile['is_activated']) ? $profile['is_activated'] >= 10 : 0,
  1101. 'options' => $profile['options'],
  1102. 'is_guest' => false,
  1103. 'group' => $profile['member_group'],
  1104. 'group_color' => $profile['member_group_color'],
  1105. 'group_id' => $profile['id_group'],
  1106. 'post_group' => $profile['post_group'],
  1107. 'post_group_color' => $profile['post_group_color'],
  1108. 'group_icons' => str_repeat('<img src="' . str_replace('$language', $context['user']['language'], isset($profile['icons'][1]) ? $settings['images_url'] . '/membericons/' . $profile['icons'][1] : '') . '" alt="*">', empty($profile['icons'][0]) || empty($profile['icons'][1]) ? 0 : $profile['icons'][0]),
  1109. 'warning' => $profile['warning'],
  1110. 'warning_status' => !empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $profile['warning'] ? 'mute' : (!empty($modSettings['warning_moderate']) && $modSettings['warning_moderate'] <= $profile['warning'] ? 'moderate' : (!empty($modSettings['warning_watch']) && $modSettings['warning_watch'] <= $profile['warning'] ? 'watch' : (''))),
  1111. 'local_time' => timeformat(time() + ($profile['time_offset'] - $user_info['time_offset']) * 3600, false),
  1112. );
  1113. // First do a quick run through to make sure there is something to be shown.
  1114. $memberContext[$user]['has_messenger'] = false;
  1115. foreach (array('icq', 'skype', 'aim', 'yim') as $messenger)
  1116. {
  1117. if (!isset($context['disabled_fields'][$messenger]) && !empty($memberContext[$user][$messenger]['link']))
  1118. {
  1119. $memberContext[$user]['has_messenger'] = true;
  1120. break;
  1121. }
  1122. }
  1123. // Are we also loading the members custom fields into context?
  1124. if ($display_custom_fields && !empty($modSettings['displayFields']))
  1125. {
  1126. $memberContext[$user]['custom_fields'] = array();
  1127. if (!isset($context['display_fields']))
  1128. $context['display_fields'] = unserialize($modSettings['displayFields']);
  1129. foreach ($context['display_fields'] as $custom)
  1130. {
  1131. if (!isset($custom['title']) || trim($custom['title']) == '' || empty($profile['options'][$custom['colname']]))
  1132. continue;
  1133. $value = $profile['options'][$custom['colname']];
  1134. // BBC?
  1135. if ($custom['bbc'])
  1136. $value = parse_bbc($value);
  1137. // ... or checkbox?
  1138. elseif (isset($custom['type']) && $custom['type'] == 'check')
  1139. $value = $value ? $txt['yes'] : $txt['no'];
  1140. // Enclosing the user input within some other text?
  1141. if (!empty($custom['enclose']))
  1142. $value = strtr($custom['enclose'], array(
  1143. '{SCRIPTURL}' => $scripturl,
  1144. '{IMAGES_URL}' => $settings['images_url'],
  1145. '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
  1146. '{INPUT}' => $value,
  1147. ));
  1148. $memberContext[$user]['custom_fields'][] = array(
  1149. 'title' => $custom['title'],
  1150. 'colname' => $custom['colname'],
  1151. 'value' => $value,
  1152. 'placement' => !empty($custom['placement']) ? $custom['placement'] : 0,
  1153. );
  1154. }
  1155. }
  1156. call_integration_hook('integrate_member_context', array(&$user, $display_custom_fields));
  1157. return true;
  1158. }
  1159. /**
  1160. * Loads the user's custom profile fields
  1161. *
  1162. * @param integer|array $users A single user ID or an array of user IDs
  1163. * @param string|array $param Either a string or an array of strings with profile field names
  1164. * @return array|boolean An array of data about the fields and their values or false if nothing was loaded
  1165. */
  1166. function loadMemberCustomFields($users, $params)
  1167. {
  1168. global $smcFunc, $txt, $scripturl, $settings;
  1169. // Do not waste my time...
  1170. if (empty($users) || empty($params))
  1171. return false;
  1172. // Make sure it's an array.
  1173. $users = !is_array($users) ? array($users) : array_unique($users);
  1174. $params = !is_array($params) ? array($params) : array_unique($params);
  1175. $return = array();
  1176. $request = $smcFunc['db_query']('', '
  1177. SELECT c.id_field, c.col_name, c.field_name, c.field_desc, c.field_type, c.field_length, c.field_options, c.mask, show_reg,
  1178. c.show_display, c.show_profile, c.private, c.active, c.bbc, c.can_search, c.default_value, c.enclose, c.placement, t.variable, t.value, t.id_member
  1179. FROM {db_prefix}themes AS t
  1180. LEFT JOIN {db_prefix}custom_fields AS c ON (c.col_name = t.variable)
  1181. WHERE id_member IN ({array_int:loaded_ids})
  1182. AND variable IN ({array_string:params})',
  1183. array(
  1184. 'loaded_ids' => $users,
  1185. 'params' => $params,
  1186. )
  1187. );
  1188. while ($row = $smcFunc['db_fetch_assoc']($request))
  1189. {
  1190. // BBC?
  1191. if (!empty($row['bbc']))
  1192. $row['value'] = parse_bbc($row['value']);
  1193. // ... or checkbox?
  1194. elseif (isset($row['type']) && $row['type'] == 'check')
  1195. $row['value'] = !empty($row['value']) ? $txt['yes'] : $txt['no'];
  1196. // Enclosing the user input within some other text?
  1197. if (!empty($row['enclose']))
  1198. $row['value'] = strtr($row['enclose'], array(
  1199. '{SCRIPTURL}' => $scripturl,
  1200. '{IMAGES_URL}' => $settings['images_url'],
  1201. '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
  1202. '{INPUT}' => $row['value'],
  1203. ));
  1204. // Send a simple array if there is just 1 param
  1205. if (count($params) == 1)
  1206. $return[$row['id_member']] = $row;
  1207. // More than 1? knock yourself out...
  1208. else
  1209. $return[$row['id_member']][$row['id_field']] = $row;
  1210. }
  1211. $smcFunc['db_free_result']($request);
  1212. return !empty($return) ? $return : false;
  1213. }
  1214. /**
  1215. * Loads information about what browser the user is viewing with and places it in $context
  1216. * - uses the class from Class-BrowerDetect.php
  1217. *
  1218. */
  1219. function detectBrowser()
  1220. {
  1221. // Load the current user's browser of choice
  1222. $detector = new browser_detector;
  1223. $detector->detectBrowser();
  1224. }
  1225. /**
  1226. * Are we using this browser?
  1227. *
  1228. * Wrapper function for detectBrowser
  1229. * @param string $browser The browser we are checking for.
  1230. */
  1231. function isBrowser($browser)
  1232. {
  1233. global $context;
  1234. // @todo REMOVE THIS BEFORE BETA 1 RELEASE.
  1235. if (in_array($browser, array('ie7', 'ie6', 'ie5.5', 'ie5', 'ie5', 'ie4', 'mac_ie', 'firefox1')))
  1236. {
  1237. $line = $file = null;
  1238. foreach (debug_backtrace() as $step)
  1239. {
  1240. // Found it?
  1241. if (strpos($step['function'], 'query') === false && !in_array(substr($step['function'], 0, 7), array('smf_db_', 'preg_re', 'db_erro', 'call_us')) && strpos($step['function'], '__') !== 0)
  1242. {
  1243. $function = '<br>Function: ' . $step['function'];
  1244. break;
  1245. }
  1246. if (isset($step['line']))
  1247. {
  1248. $file = $step['file'];
  1249. $line = $step['line'];
  1250. }
  1251. }
  1252. log_error('Old browser support' . $function, 'debug', $file, $line);
  1253. }
  1254. // Don't know any browser!
  1255. if (empty($context['browser']))
  1256. detectBrowser();
  1257. return !empty($context['browser'][$browser]) || !empty($context['browser']['is_' . $browser]) ? true : false;
  1258. }
  1259. /**
  1260. * Load a theme, by ID.
  1261. *
  1262. * @param int $id_theme The ID of the theme to load
  1263. * @param bool $initialize Whether or not to initialize a bunch of theme-related variables/settings
  1264. */
  1265. function loadTheme($id_theme = 0, $initialize = true)
  1266. {
  1267. global $user_info, $user_settings, $board_info, $boarddir;
  1268. global $txt, $boardurl, $scripturl, $mbname, $modSettings;
  1269. global $context, $settings, $options, $sourcedir, $ssi_theme, $smcFunc, $language;
  1270. // The theme was specified by parameter.
  1271. if (!empty($id_theme))
  1272. $id_theme = (int) $id_theme;
  1273. // The theme was specified by REQUEST.
  1274. elseif (!empty($_REQUEST['theme']) && (!empty($modSettings['theme_allow']) || allowedTo('admin_forum')))
  1275. {
  1276. $id_theme = (int) $_REQUEST['theme'];
  1277. $_SESSION['id_theme'] = $id_theme;
  1278. }
  1279. // The theme was specified by REQUEST... previously.
  1280. elseif (!empty($_SESSION['id_theme']) && (!empty($modSettings['theme_allow']) || allowedTo('admin_forum')))
  1281. $id_theme = (int) $_SESSION['id_theme'];
  1282. // The theme is just the user's choice. (might use ?board=1;theme=0 to force board theme.)
  1283. elseif (!empty($user_info['theme']) && !isset($_REQUEST['theme']))
  1284. $id_theme = $user_info['theme'];
  1285. // The theme was specified by the board.
  1286. elseif (!empty($board_info['theme']))
  1287. $id_theme = $board_info['theme'];
  1288. // The theme is the forum's default.
  1289. else
  1290. $id_theme = $modSettings['theme_guests'];
  1291. // Verify the id_theme... no foul play.
  1292. // Always allow the board specific theme, if they are overriding.
  1293. if (!empty($board_info['theme']) && $board_info['override_theme'])
  1294. $id_theme = $board_info['theme'];
  1295. // If they have specified a particular theme to use with SSI allow it to be used.
  1296. elseif (!empty($ssi_theme) && $id_theme == $ssi_theme)
  1297. $id_theme = (int) $id_theme;
  1298. elseif (!empty($modSettings['enableThemes']) && !allowedTo('admin_forum'))
  1299. {
  1300. $themes = explode(',', $modSettings['enableThemes']);
  1301. if (!in_array($id_theme, $themes))
  1302. $id_theme = $modSettings['theme_guests'];
  1303. else
  1304. $id_theme = (int) $id_theme;
  1305. }
  1306. else
  1307. $id_theme = (int) $id_theme;
  1308. $member = empty($user_info['id']) ? -1 : $user_info['id'];
  1309. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2 && ($temp = cache_get_data('theme_settings-' . $id_theme . ':' . $member, 60)) != null && time() - 60 > $modSettings['settings_updated'])
  1310. {
  1311. $themeData = $temp;
  1312. $flag = true;
  1313. }
  1314. elseif (($temp = cache_get_data('theme_settings-' . $id_theme, 90)) != null && time() - 60 > $modSettings['settings_updated'])
  1315. $themeData = $temp + array($member => array());
  1316. else
  1317. $themeData = array(-1 => array(), 0 => array(), $member => array());
  1318. if (empty($flag))
  1319. {
  1320. // Load variables from the current or default theme, global or this user's.
  1321. $result = $smcFunc['db_query']('', '
  1322. SELECT variable, value, id_member, id_theme
  1323. FROM {db_prefix}themes
  1324. WHERE id_member' . (empty($themeData[0]) ? ' IN (-1, 0, {int:id_member})' : ' = {int:id_member}') . '
  1325. AND id_theme' . ($id_theme == 1 ? ' = {int:id_theme}' : ' IN ({int:id_theme}, 1)'),
  1326. array(
  1327. 'id_theme' => $id_theme,
  1328. 'id_member' => $member,
  1329. )
  1330. );
  1331. // Pick between $settings and $options depending on whose data it is.
  1332. while ($row = $smcFunc['db_fetch_assoc']($result))
  1333. {
  1334. // There are just things we shouldn't be able to change as members.
  1335. if ($row['id_member'] != 0 && in_array($row['variable'], array('actual_theme_url', 'actual_images_url', 'base_theme_dir', 'base_theme_url', 'default_images_url', 'default_theme_dir', 'default_theme_url', 'default_template', 'images_url', 'number_recent_posts', 'smiley_sets_default', 'theme_dir', 'theme_id', 'theme_layers', 'theme_templates', 'theme_url')))
  1336. continue;
  1337. // If this is the theme_dir of the default theme, store it.
  1338. if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1' && empty($row['id_member']))
  1339. $themeData[0]['default_' . $row['variable']] = $row['value'];
  1340. // If this isn't set yet, is a theme option, or is not the default theme..
  1341. if (!isset($themeData[$row['id_member']][$row['variable']]) || $row['id_theme'] != '1')
  1342. $themeData[$row['id_member']][$row['variable']] = substr($row['variable'], 0, 5) == 'show_' ? $row['value'] == '1' : $row['value'];
  1343. }
  1344. $smcFunc['db_free_result']($result);
  1345. if (!empty($themeData[-1]))
  1346. foreach ($themeData[-1] as $k => $v)
  1347. {
  1348. if (!isset($themeData[$member][$k]))
  1349. $themeData[$member][$k] = $v;
  1350. }
  1351. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  1352. cache_put_data('theme_settings-' . $id_theme . ':' . $member, $themeData, 60);
  1353. // Only if we didn't already load that part of the cache...
  1354. elseif (!isset($temp))
  1355. cache_put_data('theme_settings-' . $id_theme, array(-1 => $themeData[-1], 0 => $themeData[0]), 90);
  1356. }
  1357. $settings = $themeData[0];
  1358. $options = $themeData[$member];
  1359. $settings['theme_id'] = $id_theme;
  1360. $settings['actual_theme_url'] = $settings['theme_url'];
  1361. $settings['actual_images_url'] = $settings['images_url'];
  1362. $settings['actual_theme_dir'] = $settings['theme_dir'];
  1363. $settings['template_dirs'] = array();
  1364. // This theme first.
  1365. $settings['template_dirs'][] = $settings['theme_dir'];
  1366. // Based on theme (if there is one).
  1367. if (!empty($settings['base_theme_dir']))
  1368. $settings['template_dirs'][] = $settings['base_theme_dir'];
  1369. // Lastly the default theme.
  1370. if ($settings['theme_dir'] != $settings['default_theme_dir'])
  1371. $settings['template_dirs'][] = $settings['default_theme_dir'];
  1372. if (!$initialize)
  1373. return;
  1374. // Check to see if they're accessing it from the wrong place.
  1375. if (isset($_SERVER['HTTP_HOST']) || isset($_SERVER['SERVER_NAME']))
  1376. {
  1377. $detected_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https://' : 'http://';
  1378. $detected_url .= empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
  1379. $temp = preg_replace('~/' . basename($scripturl) . '(/.+)?$~', '', strtr(dirname($_SERVER['PHP_SELF']), '\\', '/'));
  1380. if ($temp != '/')
  1381. $detected_url .= $temp;
  1382. }
  1383. if (isset($detected_url) && $detected_url != $boardurl)
  1384. {
  1385. // Try #1 - check if it's in a list of alias addresses.
  1386. if (!empty($modSettings['forum_alias_urls']))
  1387. {
  1388. $aliases = explode(',', $modSettings['forum_alias_urls']);
  1389. foreach ($aliases as $alias)
  1390. {
  1391. // Rip off all the boring parts, spaces, etc.
  1392. if ($detected_url == trim($alias) || strtr($detected_url, array('http://' => '', 'https://' => '')) == trim($alias))
  1393. $do_fix = true;
  1394. }
  1395. }
  1396. // Hmm... check #2 - is it just different by a www? Send them to the correct place!!
  1397. if (empty($do_fix) && strtr($detected_url, array('://' => '://www.')) == $boardurl && (empty($_GET) || count($_GET) == 1) && SMF != 'SSI')
  1398. {
  1399. // Okay, this seems weird, but we don't want an endless loop - this will make $_GET not empty ;).
  1400. if (empty($_GET))
  1401. redirectexit('wwwRedirect');
  1402. else
  1403. {
  1404. list ($k, $v) = each($_GET);
  1405. if ($k != 'wwwRedirect')
  1406. redirectexit('wwwRedirect;' . $k . '=' . $v);
  1407. }
  1408. }
  1409. // #3 is just a check for SSL...
  1410. if (strtr($detected_url, array('https://' => 'http://')) == $boardurl)
  1411. $do_fix = true;
  1412. // Okay, #4 - perhaps it's an IP address? We're gonna want to use that one, then. (assuming it's the IP or something...)
  1413. if (!empty($do_fix) || preg_match('~^http[s]?://(?:[\d\.:]+|\[[\d:]+\](?::\d+)?)(?:$|/)~', $detected_url) == 1)
  1414. {
  1415. // Caching is good ;).
  1416. $oldurl = $boardurl;
  1417. // Fix $boardurl and $scripturl.
  1418. $boardurl = $detected_url;
  1419. $scripturl = strtr($scripturl, array($oldurl => $boardurl));
  1420. $_SERVER['REQUEST_URL'] = strtr($_SERVER['REQUEST_URL'], array($oldurl => $boardurl));
  1421. // Fix the theme urls...
  1422. $settings['theme_url'] = strtr($settings['theme_url'], array($oldurl => $boardurl));
  1423. $settings['default_theme_url'] = strtr($settings['default_theme_url'], array($oldurl => $boardurl));
  1424. $settings['actual_theme_url'] = strtr($settings['actual_theme_url'], array($oldurl => $boardurl));
  1425. $settings['images_url'] = strtr($settings['images_url'], array($oldurl => $boardurl));
  1426. $settings['default_images_url'] = strtr($settings['default_images_url'], array($oldurl => $boardurl));
  1427. $settings['actual_images_url'] = strtr($settings['actual_images_url'], array($oldurl => $boardurl));
  1428. // And just a few mod settings :).
  1429. $modSettings['smileys_url'] = strtr($modSettings['smileys_url'], array($oldurl => $boardurl));
  1430. $modSettings['avatar_url'] = strtr($modSettings['avatar_url'], array($oldurl => $boardurl));
  1431. // Clean up after loadBoard().
  1432. if (isset($board_info['moderators']))
  1433. {
  1434. foreach ($board_info['moderators'] as $k => $dummy)
  1435. {
  1436. $board_info['moderators'][$k]['href'] = strtr($dummy['href'], array($oldurl => $boardurl));
  1437. $board_info['moderators'][$k]['link'] = strtr($dummy['link'], array('"' . $oldurl => '"' . $boardurl));
  1438. }
  1439. }
  1440. foreach ($context['linktree'] as $k => $dummy)
  1441. $context['linktree'][$k]['url'] = strtr($dummy['url'], array($oldurl => $boardurl));
  1442. }
  1443. }
  1444. // Set up the contextual user array.
  1445. if (!empty($user_info))
  1446. {
  1447. $context['user'] = array(
  1448. 'id' => $user_info['id'],
  1449. 'is_logged' => !$user_info['is_guest'],
  1450. 'is_guest' => &$user_info['is_guest'],
  1451. 'is_admin' => &$user_info['is_admin'],
  1452. 'is_mod' => &$user_info['is_mod'],
  1453. // A user can mod if they have permission to see the mod center, or they are a board/group/approval moderator.
  1454. 'can_mod' => allowedTo('access_mod_center') || (!$user_info['is_guest'] && ($user_info['mod_cache']['gq'] != '0=1' || $user_info['mod_cache']['bq'] != '0=1' || ($modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap'])))),
  1455. 'username' => $user_info['username'],
  1456. 'language' => $user_info['language'],
  1457. 'email' => $user_info['email'],
  1458. 'ignoreusers' => $user_info['ignoreusers'],
  1459. );
  1460. if (!$context['user']['is_guest'])
  1461. $context['user']['name'] = $user_info['name'];
  1462. elseif ($context['user']['is_guest'] && !empty($txt['guest_title']))
  1463. $context['user']['name'] = $txt['guest_title'];
  1464. // Determine the current smiley set.
  1465. $user_info['smiley_set'] = (!in_array($user_info['smiley_set'], explode(',', $modSettings['smiley_sets_known'])) && $user_info['smiley_set'] != 'none') || empty($modSettings['smiley_sets_enable']) ? (!empty($settings['smiley_sets_default']) ? $settings['smiley_sets_default'] : $modSettings['smiley_sets_default']) : $user_info['smiley_set'];
  1466. $context['user']['smiley_set'] = $user_info['smiley_set'];
  1467. }
  1468. else
  1469. {
  1470. $context['user'] = array(
  1471. 'id' => -1,
  1472. 'is_logged' => false,
  1473. 'is_guest' => true,
  1474. 'is_mod' => false,
  1475. 'can_mod' => false,
  1476. 'name' => $txt['guest_title'],
  1477. 'language' => $language,
  1478. 'email' => '',
  1479. 'ignoreusers' => array(),
  1480. );
  1481. }
  1482. // Some basic information...
  1483. if (!isset($context['html_headers']))
  1484. $context['html_headers'] = '';
  1485. if (!isset($context['javascript_files']))
  1486. $context['javascript_files'] = array();
  1487. if (!isset($context['css_files']))
  1488. $context['css_files'] = array();
  1489. if (!isset($context['css_header']))
  1490. $context['css_header'] = '';
  1491. if (!isset($context['javascript_inline']))
  1492. $context['javascript_inline'] = array('standard' => array(), 'defer' => array());
  1493. if (!isset($context['javascript_vars']))
  1494. $context['javascript_vars'] = array();
  1495. $context['menu_separator'] = !empty($settings['use_image_buttons']) ? ' ' : ' | ';
  1496. $context['session_var'] = $_SESSION['session_var'];
  1497. $context['session_id'] = $_SESSION['session_value'];
  1498. $context['forum_name'] = $mbname;
  1499. $context['forum_name_html_safe'] = $smcFunc['htmlspecialchars']($context['forum_name']);
  1500. $context['header_logo_url_html_safe'] = empty($settings['header_logo_url']) ? '' : $smcFunc['htmlspecialchars']($settings['header_logo_url']);
  1501. $context['current_action'] = isset($_REQUEST['action']) ? $smcFunc['htmlspecialchars']($_REQUEST['action']) : null;
  1502. $context['current_subaction'] = isset($_REQUEST['sa']) ? $_REQUEST['sa'] : null;
  1503. $context['can_register'] = empty($modSettings['registration_method']) || $modSettings['registration_method'] != 3;
  1504. if (isset($modSettings['load_average']))
  1505. $context['load_average'] = $modSettings['load_average'];
  1506. // Detect the browser. This is separated out because it's also used in attachment downloads
  1507. detectBrowser();
  1508. // Set the top level linktree up.
  1509. array_unshift($context['linktree'], array(
  1510. 'url' => $scripturl,
  1511. 'name' => $context['forum_name_html_safe']
  1512. ));
  1513. // This allows sticking some HTML on the page output - useful for controls.
  1514. $context['insert_after_template'] = '';
  1515. if (!isset($txt))
  1516. $txt = array();
  1517. $simpleActions = array(
  1518. 'findmember',
  1519. 'helpadmin',
  1520. 'printpage',
  1521. 'quotefast',
  1522. 'spellcheck',
  1523. );
  1524. // Wireless mode? Load up the wireless stuff.
  1525. if (WIRELESS)
  1526. {
  1527. $context['template_layers'] = array(WIRELESS_PROTOCOL);
  1528. loadTemplate('Wireless');
  1529. loadLanguage('Wireless+index+Modifications');
  1530. }
  1531. // Output is fully XML, so no need for the index template.
  1532. elseif (isset($_REQUEST['xml']))
  1533. {
  1534. loadLanguage('index+Modifications');
  1535. loadTemplate('Xml');
  1536. $context['template_layers'] = array();
  1537. }
  1538. // These actions don't require the index template at all.
  1539. elseif (!empty($_REQUEST['action']) && in_array($_REQUEST['action'], $simpleActions))
  1540. {
  1541. loadLanguage('index+Modifications');
  1542. $context['template_layers'] = array();
  1543. }
  1544. else
  1545. {
  1546. // Custom templates to load, or just default?
  1547. if (isset($settings['theme_templates']))
  1548. $templates = explode(',', $settings['theme_templates']);
  1549. else
  1550. $templates = array('index');
  1551. // Load each template...
  1552. foreach ($templates as $template)
  1553. loadTemplate($template);
  1554. // ...and attempt to load their associated language files.
  1555. $required_files = implode('+', array_merge($templates, array('Modifications')));
  1556. loadLanguage($required_files, '', false);
  1557. // Custom template layers?
  1558. if (isset($settings['theme_layers']))
  1559. $context['template_layers'] = explode(',', $settings['theme_layers']);
  1560. else
  1561. $context['template_layers'] = array('html', 'body');
  1562. }
  1563. // Initialize the theme.
  1564. loadSubTemplate('init', 'ignore');
  1565. // Allow overriding the board wide time/number formats.
  1566. if (empty($user_settings['time_format']) && !empty($txt['time_format']))
  1567. $user_info['time_format'] = $txt['time_format'];
  1568. // Set the character set from the template.
  1569. $context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
  1570. $context['utf8'] = $context['character_set'] === 'UTF-8';
  1571. $context['right_to_left'] = !empty($txt['lang_rtl']);
  1572. // Guests may still need a name.
  1573. if ($context['user']['is_guest'] && empty($context['user']['name']))
  1574. $context['user']['name'] = $txt['guest_title'];
  1575. // Any theme-related strings that need to be loaded?
  1576. if (!empty($settings['require_theme_strings']))
  1577. loadLanguage('ThemeStrings', '', false);
  1578. // Sort out some themes.
  1579. if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'always')
  1580. {
  1581. $settings['theme_url'] = $settings['default_theme_url'];
  1582. $settings['images_url'] = $settings['default_images_url'];
  1583. $settings['theme_dir'] = $settings['default_theme_dir'];
  1584. }
  1585. // Make a special URL for the language.
  1586. $settings['lang_images_url'] = $settings['images_url'] . '/' . (!empty($txt['image_lang']) ? $txt['image_lang'] : $user_info['language']);
  1587. // And of course, let's load the default CSS file.
  1588. loadCSSFile('index.css');
  1589. if ($context['right_to_left'])
  1590. loadCSSFile('rtl.css');
  1591. // We allow theme variants, because we're cool.
  1592. $context['theme_variant'] = '';
  1593. $context['theme_variant_url'] = '';
  1594. if (!empty($settings['theme_variants']))
  1595. {
  1596. // Overriding - for previews and that ilk.
  1597. if (!empty($_REQUEST['variant']))
  1598. $_SESSION['id_variant'] = $_REQUEST['variant'];
  1599. // User selection?
  1600. if (empty($settings['disable_user_variant']) || allowedTo('admin_forum'))
  1601. $context['theme_variant'] = !empty($_SESSION['id_variant']) ? $_SESSION['id_variant'] : (!empty($options['theme_variant']) ? $options['theme_variant'] : '');
  1602. // If not a user variant, select the default.
  1603. if ($context['theme_variant'] == '' || !in_array($context['theme_variant'], $settings['theme_variants']))
  1604. $context['theme_variant'] = !empty($settings['default_variant']) && in_array($settings['default_variant'], $settings['theme_variants']) ? $settings['default_variant'] : $settings['theme_variants'][0];
  1605. // Do this to keep things easier in the templates.
  1606. $context['theme_variant'] = '_' . $context['theme_variant'];
  1607. $context['theme_variant_url'] = $context['theme_variant'] . '/';
  1608. if (!empty($context['theme_variant']))
  1609. {
  1610. loadCSSFile('index' . $context['theme_variant'] . '.css');
  1611. if ($context['right_to_left'])
  1612. loadCSSFile('rtl' . $context['theme_variant'] . '.css');
  1613. }
  1614. }
  1615. // Let's be compatible with old themes!
  1616. if (!function_exists('template_html_above') && in_array('html', $context['template_layers']))
  1617. $context['template_layers'] = array('main');
  1618. $context['tabindex'] = 1;
  1619. // Compatibility.
  1620. if (!isset($settings['theme_version']))
  1621. $modSettings['memberCount'] = $modSettings['totalMembers'];
  1622. // Default JS variables for use in every theme
  1623. $context['javascript_vars'] = array(
  1624. 'smf_theme_url' => '"' . $settings['theme_url'] . '"',
  1625. 'smf_default_theme_url' => '"' . $settings['default_theme_url'] . '"',
  1626. 'smf_images_url' => '"' . $settings['images_url'] . '"',
  1627. 'smf_scripturl' => '"' . $scripturl . '"',
  1628. 'smf_iso_case_folding' => $context['server']['iso_case_folding'] ? 'true' : 'false',
  1629. 'smf_charset' => '"' . $context['character_set'] . '"',
  1630. 'smf_session_id' => '"' . $context['session_id'] . '"',
  1631. 'smf_session_var' => '"' . $context['session_var'] . '"',
  1632. 'smf_member_id' => $context['user']['id'],
  1633. 'ajax_notification_text' => JavaScriptEscape($txt['ajax_in_progress']),
  1634. 'help_popup_heading_text' => JavaScriptEscape($txt['help_popup']),
  1635. );
  1636. // Add the JQuery library to the list of files to load.
  1637. if (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'cdn')
  1638. loadJavascriptFile('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), 'jquery');
  1639. elseif (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'local')
  1640. loadJavascriptFile('jquery-1.7.1.min.js', array('default_theme' => true, 'seed' => false), 'jquery');
  1641. elseif (isset($modSettings['jquery_source'], $modSettings['jquery_custom']) && $modSettings['jquery_source'] == 'custom')
  1642. loadJavascriptFile($modSettings['jquery_custom'], array(), 'jquery');
  1643. // Auto loading? template_javascript() will take care of the local half of this.
  1644. else
  1645. loadJavascriptFile('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), 'jquery');
  1646. // Queue our JQuery plugins!
  1647. loadJavascriptFile('smf_jquery_plugins.js', array('default_theme' => true));
  1648. // script.js and theme.js, always required, so always add them! Makes index.template.php cleaner and all.
  1649. loadJavascriptFile('script.js', array('default_theme' => true), 'smf_scripts');
  1650. loadJavascriptFile('theme.js', array(), 'theme_scripts');
  1651. // If we think we have mail to send, let's offer up some possibilities... robots get pain (Now with scheduled task support!)
  1652. if ((!empty($modSettings['mail_next_send']) && $modSettings['mail_next_send'] < time() && empty($modSettings['mail_queue_use_cron'])) || empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time())
  1653. {
  1654. if (isBrowser('possibly_robot'))
  1655. {
  1656. // @todo Maybe move this somewhere better?!
  1657. require_once($sourcedir . '/ScheduledTasks.php');
  1658. // What to do, what to do?!
  1659. if (empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time())
  1660. AutoTask();
  1661. else
  1662. ReduceMailQueue();
  1663. }
  1664. else
  1665. {
  1666. $type = empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time() ? 'task' : 'mailq';
  1667. $ts = $type == 'mailq' ? $modSettings['mail_next_send'] : $modSettings['next_task_time'];
  1668. addInlineJavascript('
  1669. function smfAutoTask()
  1670. {
  1671. var tempImage = new Image();
  1672. tempImage.src = smf_scripturl + "?scheduled=' . $type . ';ts=' . $ts . '";
  1673. }
  1674. window.setTimeout("smfAutoTask();", 1);');
  1675. }
  1676. }
  1677. // And we should probably trigger the cron too.
  1678. if (empty($modSettings['cron_is_real_cron']))
  1679. {
  1680. $ts = time();
  1681. $ts -= $ts % 15;
  1682. addInlineJavaScript('
  1683. function triggerCron() {
  1684. var tempImage = new Image();
  1685. tempImage.src = ' . JavaScriptEscape($boardurl) . ' + "/cron.php?ts=' . $ts . '";
  1686. }
  1687. window.setTimeout(triggerCron, 1);', true);
  1688. }
  1689. // Any files to include at this point?
  1690. if (!empty($modSettings['integrate_theme_include']))
  1691. {
  1692. $theme_includes = explode(',', $modSettings['integrate_theme_include']);
  1693. foreach ($theme_includes as $include)
  1694. {
  1695. $include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir']));
  1696. if (file_exists($include))
  1697. require_once($include);
  1698. }
  1699. }
  1700. // Call load theme integration functions.
  1701. call_integration_hook('integrate_load_theme');
  1702. // We are ready to go.
  1703. $context['theme_loaded'] = true;
  1704. }
  1705. /**
  1706. * Load a template - if the theme doesn't include it, use the default.
  1707. * What this function does:
  1708. * - loads a template file with the name template_name from the current, default, or base theme.
  1709. * - detects a wrong default theme directory and tries to work around it.
  1710. *
  1711. * @uses the template_include() function to include the file.
  1712. * @param string $template_name The name of the template to load
  1713. * @param array|string $style_sheets The name of a single stylesheet or an array of names of stylesheets to load
  1714. * @param bool $fatal If true, dies with an error message if the template cannot be found
  1715. * @return boolean True if the template was loaded, false otherwise
  1716. */
  1717. function loadTemplate($template_name, $style_sheets = array(), $fatal = true)
  1718. {
  1719. global $context, $settings, $txt, $scripturl, $boarddir, $db_show_debug;
  1720. // Do any style sheets first, cause we're easy with those.
  1721. if (!empty($style_sheets))
  1722. {
  1723. if (!is_array($style_sheets))
  1724. $style_sheets = array($style_sheets);
  1725. foreach ($style_sheets as $sheet)
  1726. loadCSSFile($sheet . '.css', array(), $sheet);
  1727. }
  1728. // No template to load?
  1729. if ($template_name === false)
  1730. return true;
  1731. $loaded = false;
  1732. foreach ($settings['template_dirs'] as $template_dir)
  1733. {
  1734. if (file_exists($template_dir . '/' . $template_name . '.template.php'))
  1735. {
  1736. $loaded = true;
  1737. template_include($template_dir . '/' . $template_name . '.template.php', true);
  1738. break;
  1739. }
  1740. }
  1741. if ($loaded)
  1742. {
  1743. // For compatibility reasons, if this is the index template without new functions, include compatible stuff.
  1744. if (substr($template_name, 0, 5) == 'index' && !function_exists('template_button_strip'))
  1745. loadTemplate('Compat');
  1746. if ($db_show_debug === true)
  1747. $context['debug']['templates'][] = $template_name . ' (' . basename($template_dir) . ')';
  1748. // If they have specified an initialization function for this template, go ahead and call it now.
  1749. if (function_exists('template_' . $template_name . '_init'))
  1750. call_user_func('template_' . $template_name . '_init');
  1751. }
  1752. // Hmmm... doesn't exist?! I don't suppose the directory is wrong, is it?
  1753. elseif (!file_exists($settings['default_theme_dir']) && file_exists($boarddir . '/Themes/default'))
  1754. {
  1755. $settings['default_theme_dir'] = $boarddir . '/Themes/default';
  1756. $settings['template_dirs'][] = $settings['default_theme_dir'];
  1757. if (!empty($context['user']['is_admin']) && !isset($_GET['th']))
  1758. {
  1759. loadLanguage('Errors');
  1760. echo '
  1761. <div class="alert errorbox">
  1762. <a href="', $scripturl . '?action=admin;area=theme;sa=list;th=1;' . $context['session_var'] . '=' . $context['session_id'], '" class="alert">', $txt['theme_dir_wrong'], '</a>
  1763. </div>';
  1764. }
  1765. loadTemplate($template_name);
  1766. }
  1767. // Cause an error otherwise.
  1768. elseif ($template_name != 'Errors' && $template_name != 'index' && $fatal)
  1769. fatal_lang_error('theme_template_error', 'template', array((string) $template_name));
  1770. elseif ($fatal)
  1771. die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load Themes/default/%s.template.php!', (string) $template_name), 'template'));
  1772. else
  1773. return false;
  1774. }
  1775. /**
  1776. * Load a sub-template.
  1777. * What it does:
  1778. * - loads the sub template specified by sub_template_name, which must be in an already-loaded template.
  1779. * - if ?debug is in the query string, shows administrators a marker after every sub template
  1780. * for debugging purposes.
  1781. *
  1782. * @todo get rid of reading $_REQUEST directly
  1783. *
  1784. * @param string $sub_template_name The name of the sub-template to load
  1785. * @param bool Whether to die with an error if the sub-template can't be loaded
  1786. */
  1787. function loadSubTemplate($sub_template_name, $fatal = false)
  1788. {
  1789. global $context, $txt, $db_show_debug;
  1790. if ($db_show_debug === true)
  1791. $context['debug']['sub_templates'][] = $sub_template_name;
  1792. // Figure out what the template function is named.
  1793. $theme_function = 'template_' . $sub_template_name;
  1794. if (function_exists($theme_function))
  1795. $theme_function();
  1796. elseif ($fatal === false)
  1797. fatal_lang_error('theme_template_error', 'template', array((string) $sub_template_name));
  1798. elseif ($fatal !== 'ignore')
  1799. die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load the %s sub template!', (string) $sub_template_name), 'template'));
  1800. // Are we showing debugging for templates? Just make sure not to do it before the doctype...
  1801. if (allowedTo('admin_forum') && isset($_REQUEST['debug']) && !in_array($sub_template_name, array('init', 'main_below')) && ob_get_length() > 0 && !isset($_REQUEST['xml']))
  1802. {
  1803. echo '
  1804. <div style="font-size: 8pt; border: 1px dashed red; background: orange; text-align: center; font-weight: bold;">---- ', $sub_template_name, ' ends ----</div>';
  1805. }
  1806. }
  1807. /**
  1808. * Add a CSS file for output later
  1809. *
  1810. * @param string $filename THe name of the file to load
  1811. * @param array $params An array of parameters
  1812. * Keys are the following:
  1813. * - ['local'] (true/false): define if the file is local
  1814. * - ['default_theme'] (true/false): force use of default theme url
  1815. * - ['force_current'] (true/false): if this is false, we will attempt to load the file from the default theme if not found in the current theme
  1816. * - ['validate'] (true/false): if true script will validate the local file exists
  1817. * - ['seed'] (true/false/string): if true or null, use cache stale, false do not, or used a supplied string
  1818. * @param string $id An ID to stick on the end of the filename for caching purposes
  1819. */
  1820. function loadCSSFile($filename, $params = array(), $id = '')
  1821. {
  1822. global $settings, $context;
  1823. $params['seed'] = (!isset($params['seed']) || $params['seed'] === true) ? '?alph21' : (is_string($params['seed']) ? ($params['seed'] = $params['seed'][0] === '?' ? $params['seed'] : '?' . $params['seed']) : '');
  1824. $params['force_current'] = !empty($params['force_current']) ? $params['force_current'] : false;
  1825. $theme = !empty($params['default_theme']) ? 'default_theme' : 'theme';
  1826. // account for shorthand like admin.css?alp21 filenames
  1827. $has_seed = strpos($filename, '.css?');
  1828. $id = empty($id) ? strtr(basename($filename), '?', '_') : $id;
  1829. // Is this a local file?
  1830. if (strpos($filename, 'http') === false || !empty($params['local']))
  1831. {
  1832. // Are we validating the the file exists?
  1833. if (!empty($params['validate']) && !file_exists($settings[$theme . '_dir'] . '/css/' . $filename))
  1834. {
  1835. // Maybe the default theme has it?
  1836. if ($theme === 'theme' && !$params['force_current'] && file_exists($settings['default_theme_dir'] . '/css/' . $filename))
  1837. $filename = $settings['default_theme_url'] . '/css/' . $filename . ($has_seed ? '' : $params['seed']);
  1838. else
  1839. $filename = false;
  1840. }
  1841. else
  1842. $filename = $settings[$theme . '_url'] . '/css/' . $filename . ($has_seed ? '' : $params['seed']);
  1843. }
  1844. // Add it to the array for use in the template
  1845. if (!empty($filename))
  1846. $context['css_files'][$id] = array('filename' => $filename, 'options' => $params);
  1847. if (!empty($context['right_to_left']) && !empty($params['rtl']))
  1848. loadCSSFile($params['rtl'], array_diff_key($params, array('rtl' => 0)));
  1849. }
  1850. /**
  1851. * Add a Javascript file for output later
  1852. * @param string $filename The name of the file to load
  1853. * @param array $params An array of parameter info
  1854. * Keys are the following:
  1855. * - ['local'] (true/false): define if the file is local
  1856. * - ['default_theme'] (true/false): force use of default theme url
  1857. * - ['defer'] (true/false): define if the file should load in <head> or before the closing <html> tag
  1858. * - ['force_current'] (true/false): if this is false, we will attempt to load the file from the
  1859. * default theme if not found in the current theme
  1860. * - ['async'] (true/false): if the script should be loaded asynchronously (HTML5)
  1861. * - ['validate'] (true/false): if true script will validate the local file exists
  1862. * - ['seed'] (true/false/string): if true or null, use cache stale, false do not, or used a supplied string
  1863. *
  1864. * @param string $id An ID to stik on the end of the filename
  1865. */
  1866. function loadJavascriptFile($filename, $params = array(), $id = '')
  1867. {
  1868. global $settings, $context;
  1869. $params['seed'] = (!isset($params['seed']) || $params['seed'] === true) ? '?alph21' : (is_string($params['seed']) ? ($params['seed'] = $params['seed'][0] === '?' ? $params['seed'] : '?' . $params['seed']) : '');
  1870. $params['force_current'] = !empty($params['force_current']) ? $params['force_current'] : false;
  1871. $theme = !empty($params['default_theme']) ? 'default_theme' : 'theme';
  1872. // account for shorthand like admin.js?alp21 filenames
  1873. $has_seed = strpos($filename, '.js?');
  1874. $id = empty($id) ? strtr(basename($filename), '?', '_') : $id;
  1875. // Is this a local file?
  1876. if (strpos($filename, 'http') === false || !empty($params['local']))
  1877. {
  1878. // Are we validating it exists on disk?
  1879. if (!empty($params['validate']) && !file_exists($settings[$theme . '_dir'] . '/scripts/' . $filename))
  1880. {
  1881. // can't find it in this theme, how about the default?
  1882. if ($theme === 'theme' && !$params['force_current'] && file_exists($settings['default_theme_dir'] . '/' . $filename))
  1883. $filename = $settings['default_theme_url'] . '/scripts/' . $filename . ($has_seed ? '' : $params['seed']);
  1884. else
  1885. $filename = false;
  1886. }
  1887. else
  1888. $filename = $settings[$theme . '_url'] . '/scripts/' . $filename . ($has_seed ? '' : $params['seed']);
  1889. }
  1890. // Add it to the array for use in the template
  1891. if (!empty($filename))
  1892. $context['javascript_files'][$id] = array('filename' => $filename, 'options' => $params);
  1893. }
  1894. /**
  1895. * Add a Javascript variable for output later (for feeding text strings and similar to JS)
  1896. * Cleaner and easier (for modders) than to use the function below.
  1897. *
  1898. * @param string $key The key for this variable
  1899. * @param string $value The value
  1900. * @param bool $escape Whether or not to escape the value
  1901. */
  1902. function addJavascriptVar($key, $value, $escape = false)
  1903. {
  1904. global $context;
  1905. if (!empty($key) && !empty($value))
  1906. $context['javascript_vars'][$key] = !empty($escape) ? JavaScriptEscape($value) : $value;
  1907. }
  1908. /**
  1909. * Add a block of inline Javascript code to be executed later
  1910. *
  1911. * - only use this if you have to, generally external JS files are better, but for very small scripts
  1912. * or for scripts that require help from PHP/whatever, this can be useful.
  1913. * - all code added with this function is added to the same <script> tag so do make sure your JS is clean!
  1914. *
  1915. * @param string $javascript Some JS code
  1916. * @param bool Whether the script should load in <head> or before the closing <html> tag
  1917. */
  1918. function addInlineJavascript($javascript, $defer = false)
  1919. {
  1920. global $context;
  1921. $context['javascript_inline'][($defer === true ? 'defer' : 'standard')][] = $javascript;
  1922. }
  1923. /**
  1924. * Load a language file. Tries the current and default themes as well as the user and global languages.
  1925. *
  1926. * @param string $template_name The name of a template file
  1927. * @param string $lang A specific language to load this file from
  1928. * @param bool $fatal Whether to die with an error if it can't be loaded
  1929. * @param bool $force_reload Whether to load the file again if it's already loaded
  1930. * @return string The language actually loaded.
  1931. */
  1932. function loadLanguage($template_name, $lang = '', $fatal = true, $force_reload = false)
  1933. {
  1934. global $user_info, $language, $settings, $context, $modSettings;
  1935. global $db_show_debug, $sourcedir, $txt, $birthdayEmails, $txtBirthdayEmails;
  1936. static $already_loaded = array();
  1937. // Default to the user's language.
  1938. if ($lang == '')
  1939. $lang = isset($user_info['language']) ? $user_info['language'] : $language;
  1940. // Do we want the English version of language file as fallback?
  1941. if (empty($modSettings['disable_language_fallback']) && $lang != 'english')
  1942. loadLanguage($template_name, 'english', false);
  1943. if (!$force_reload && isset($already_loaded[$template_name]) && $already_loaded[$template_name] == $lang)
  1944. return $lang;
  1945. // Make sure we have $settings - if not we're in trouble and need to find it!
  1946. if (empty($settings['default_theme_dir']))
  1947. {
  1948. require_once($sourcedir . '/ScheduledTasks.php');
  1949. loadEssentialThemeData();
  1950. }
  1951. // What theme are we in?
  1952. $theme_name = basename($settings['theme_url']);
  1953. if (empty($theme_name))
  1954. $theme_name = 'unknown';
  1955. // For each file open it up and write it out!
  1956. foreach (explode('+', $template_name) as $template)
  1957. {
  1958. // Obviously, the current theme is most important to check.
  1959. $attempts = array(
  1960. array($settings['theme_dir'], $template, $lang, $settings['theme_url']),
  1961. array($settings['theme_dir'], $template, $language, $settings['theme_url']),
  1962. );
  1963. // Do we have a base theme to worry about?
  1964. if (isset($settings['base_theme_dir']))
  1965. {
  1966. $attempts[] = array($settings['base_theme_dir'], $template, $lang, $settings['base_theme_url']);
  1967. $attempts[] = array($settings['base_theme_dir'], $template, $language, $settings['base_theme_url']);
  1968. }
  1969. // Fall back on the default theme if necessary.
  1970. $attempts[] = array($settings['default_theme_dir'], $template, $lang, $settings['default_theme_url']);
  1971. $attempts[] = array($settings['default_theme_dir'], $template, $language, $settings['default_theme_url']);
  1972. // Fall back on the English language if none of the preferred languages can be found.
  1973. if (!in_array('english', array($lang, $language)))
  1974. {
  1975. $attempts[] = array($settings['theme_dir'], $template, 'english', $settings['theme_url']);
  1976. $attempts[] = array($settings['default_theme_dir'], $template, 'english', $settings['default_theme_url']);
  1977. }
  1978. // Try to find the language file.
  1979. $found = false;
  1980. foreach ($attempts as $k => $file)
  1981. {
  1982. if (file_exists($file[0] . '/languages/' . $file[1] . '.' . $file[2] . '.php'))
  1983. {
  1984. // Include it!
  1985. template_include($file[0] . '/languages/' . $file[1] . '.' . $file[2] . '.php');
  1986. // Note that we found it.
  1987. $found = true;
  1988. break;
  1989. }
  1990. }
  1991. // That couldn't be found! Log the error, but *try* to continue normally.
  1992. if (!$found && $fatal)
  1993. {
  1994. log_error(sprintf($txt['theme_language_error'], $template_name . '.' . $lang, 'template'));
  1995. break;
  1996. }
  1997. // For the sake of backward compatibility
  1998. if (!empty($txt['emails']))
  1999. {
  2000. foreach ($txt['emails'] as $key => $value)
  2001. {
  2002. $txt[$key . '_subject'] = $value['subject'];
  2003. $txt[$key . '_body'] = $value['body'];
  2004. }
  2005. $txt['emails'] = array();
  2006. }
  2007. // For sake of backward compatibility: $birthdayEmails is supposed to be
  2008. // empty in a normal install. If it isn't it means the forum is using
  2009. // something "old" (it may be the translation, it may be a mod) and this
  2010. // code (like the piece above) takes care of converting it to the new format
  2011. if (!empty($birthdayEmails))
  2012. {
  2013. foreach ($birthdayEmails as $key => $value)
  2014. {
  2015. $txtBirthdayEmails[$key . '_subject'] = $value['subject'];
  2016. $txtBirthdayEmails[$key . '_body'] = $value['body'];
  2017. $txtBirthdayEmails[$key . '_author'] = $value['author'];
  2018. }
  2019. $birthdayEmails = array();
  2020. }
  2021. }
  2022. // Keep track of what we're up to soldier.
  2023. if ($db_show_debug === true)
  2024. $context['debug']['language_files'][] = $template_name . '.' . $lang . ' (' . $theme_name . ')';
  2025. // Remember what we have loaded, and in which language.
  2026. $already_loaded[$template_name] = $lang;
  2027. // Return the language actually loaded.
  2028. return $lang;
  2029. }
  2030. /**
  2031. * Get all parent boards (requires first parent as parameter)
  2032. * It finds all the parents of id_parent, and that board itself.
  2033. * Additionally, it detects the moderators of said boards.
  2034. *
  2035. * @param int $id_parent The ID of the parent board
  2036. * @return array An array of information about the boards found.
  2037. */
  2038. function getBoardParents($id_parent)
  2039. {
  2040. global $scripturl, $smcFunc;
  2041. // First check if we have this cached already.
  2042. if (($boards = cache_get_data('board_parents-' . $id_parent, 480)) === null)
  2043. {
  2044. $boards = array();
  2045. $original_parent = $id_parent;
  2046. // Loop while the parent is non-zero.
  2047. while ($id_parent != 0)
  2048. {
  2049. $result = $smcFunc['db_query']('', '
  2050. SELECT
  2051. b.id_parent, b.name, {int:board_parent} AS id_board, IFNULL(mem.id_member, 0) AS id_moderator,
  2052. mem.real_name, b.child_level, IFNULL(mg.id_group, 0) AS id_moderator_group, mg.group_name
  2053. FROM {db_prefix}boards AS b
  2054. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board)
  2055. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  2056. LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = b.id_board)
  2057. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = modgs.id_group)
  2058. WHERE b.id_board = {int:board_parent}',
  2059. array(
  2060. 'board_parent' => $id_parent,
  2061. )
  2062. );
  2063. // In the EXTREMELY unlikely event this happens, give an error message.
  2064. if ($smcFunc['db_num_rows']($result) == 0)
  2065. fatal_lang_error('parent_not_found', 'critical');
  2066. while ($row = $smcFunc['db_fetch_assoc']($result))
  2067. {
  2068. if (!isset($boards[$row['id_board']]))
  2069. {
  2070. $id_parent = $row['id_parent'];
  2071. $boards[$row['id_board']] = array(
  2072. 'url' => $scripturl . '?board=' . $row['id_board'] . '.0',
  2073. 'name' => $row['name'],
  2074. 'level' => $row['child_level'],
  2075. 'moderators' => array(),
  2076. 'moderator_groups' => array()
  2077. );
  2078. }
  2079. // If a moderator exists for this board, add that moderator for all children too.
  2080. if (!empty($row['id_moderator']))
  2081. foreach ($boards as $id => $dummy)
  2082. {
  2083. $boards[$id]['moderators'][$row['id_moderator']] = array(
  2084. 'id' => $row['id_moderator'],
  2085. 'name' => $row['real_name'],
  2086. 'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
  2087. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
  2088. );
  2089. }
  2090. // If a moderator group exists for this board, add that moderator group for all children too
  2091. if (!empty($row['id_moderator_group']))
  2092. foreach ($boards as $id => $dummy)
  2093. {
  2094. $boards[$id]['moderator_groups'][$row['id_moderator_group']] = array(
  2095. 'id' => $row['id_moderator_group'],
  2096. 'name' => $row['group_name'],
  2097. 'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
  2098. 'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
  2099. );
  2100. }
  2101. }
  2102. $smcFunc['db_free_result']($result);
  2103. }
  2104. cache_put_data('board_parents-' . $original_parent, $boards, 480);
  2105. }
  2106. return $boards;
  2107. }
  2108. /**
  2109. * Attempt to reload our known languages.
  2110. * It will try to choose only utf8 or non-utf8 languages.
  2111. *
  2112. * @param bool $use_cache Whether or not to use the cache
  2113. * @param bool $favor_utf8 Whether or not to favor UTF-8 files
  2114. * @return array An array of information about available languages
  2115. */
  2116. function getLanguages($use_cache = true, $favor_utf8 = true)
  2117. {
  2118. global $context, $smcFunc, $settings, $modSettings;
  2119. // Either we don't use the cache, or its expired.
  2120. if (!$use_cache || ($context['languages'] = cache_get_data('known_languages' . ($favor_utf8 ? '' : '_all'), !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600)) == null)
  2121. {
  2122. // If we don't have our theme information yet, lets get it.
  2123. if (empty($settings['default_theme_dir']))
  2124. loadTheme(0, false);
  2125. // Default language directories to try.
  2126. $language_directories = array(
  2127. $settings['default_theme_dir'] . '/languages',
  2128. );
  2129. if (!empty($settings['actual_theme_dir']) && $settings['actual_theme_dir'] != $settings['default_theme_dir'])
  2130. $language_directories[] = $settings['actual_theme_dir'] . '/languages';
  2131. // We possibly have a base theme directory.
  2132. if (!empty($settings['base_theme_dir']))
  2133. $language_directories[] = $settings['base_theme_dir'] . '/languages';
  2134. // Remove any duplicates.
  2135. $language_directories = array_unique($language_directories);
  2136. foreach ($language_directories as $language_dir)
  2137. {
  2138. // Can't look in here... doesn't exist!
  2139. if (!file_exists($language_dir))
  2140. continue;
  2141. $dir = dir($language_dir);
  2142. while ($entry = $dir->read())
  2143. {
  2144. // Look for the index language file....
  2145. if (!preg_match('~^index\.(.+)\.php$~', $entry, $matches))
  2146. continue;
  2147. $context['languages'][$matches[1]] = array(
  2148. 'name' => $smcFunc['ucwords'](strtr($matches[1], array('_' => ' '))),
  2149. 'selected' => false,
  2150. 'filename' => $matches[1],
  2151. 'location' => $language_dir . '/index.' . $matches[1] . '.php',
  2152. );
  2153. }
  2154. $dir->close();
  2155. }
  2156. // Favoring UTF8? Then prevent us from selecting non-UTF8 versions.
  2157. if ($favor_utf8)
  2158. {
  2159. foreach ($context['languages'] as $lang)
  2160. if (substr($lang['filename'], strlen($lang['filename']) - 5, 5) != '-utf8' && isset($context['languages'][$lang['filename'] . '-utf8']))
  2161. unset($context['languages'][$lang['filename']]);
  2162. }
  2163. // Lets cash in on this deal.
  2164. if (!empty($modSettings['cache_enable']))
  2165. cache_put_data('known_languages' . ($favor_utf8 ? '' : '_all'), $context['languages'], !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
  2166. }
  2167. return $context['languages'];
  2168. }
  2169. /**
  2170. * Replace all vulgar words with respective proper words. (substring or whole words..)
  2171. * What this function does:
  2172. * - it censors the passed string.
  2173. * - if the theme setting allow_no_censored is on, and the theme option
  2174. * show_no_censored is enabled, does not censor, unless force is also set.
  2175. * - it caches the list of censored words to reduce parsing.
  2176. *
  2177. * @param string &$text The text to censor
  2178. * @param bool $force Whether to censor the text regardless of settings
  2179. * @return string The censored text
  2180. */
  2181. function censorText(&$text, $force = false)
  2182. {
  2183. global $modSettings, $options, $txt;
  2184. static $censor_vulgar = null, $censor_proper;
  2185. if ((!empty($options['show_no_censored']) && !empty($modSettings['allow_no_censored']) && !$force) || empty($modSettings['censor_vulgar']) || trim($text) === '')
  2186. return $text;
  2187. // If they haven't yet been loaded, load them.
  2188. if ($censor_vulgar == null)
  2189. {
  2190. $censor_vulgar = explode("\n", $modSettings['censor_vulgar']);
  2191. $censor_proper = explode("\n", $modSettings['censor_proper']);
  2192. // Quote them for use in regular expressions.
  2193. if (!empty($modSettings['censorWholeWord']))
  2194. {
  2195. for ($i = 0, $n = count($censor_vulgar); $i < $n; $i++)
  2196. {
  2197. $censor_vulgar[$i] = str_replace(array('\\\\\\*', '\\*', '&', '\''), array('[*]', '[^\s]*?', '&amp;', '&#039;'), preg_quote($censor_vulgar[$i], '/'));
  2198. $censor_vulgar[$i] = '/(?<=^|\W)' . $censor_vulgar[$i] . '(?=$|\W)/' . (empty($modSettings['censorIgnoreCase']) ? '' : 'i') . ((empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8' ? 'u' : '');
  2199. // @todo I'm thinking the old way is some kind of bug and this is actually fixing it.
  2200. //if (strpos($censor_vulgar[$i], '\'') !== false)
  2201. //$censor_vulgar[$i] = str_replace('\'', '&#039;', $censor_vulgar[$i]);
  2202. }
  2203. }
  2204. }
  2205. // Censoring isn't so very complicated :P.
  2206. if (empty($modSettings['censorWholeWord']))
  2207. $text = empty($modSettings['censorIgnoreCase']) ? str_ireplace($censor_vulgar, $censor_proper, $text) : str_replace($censor_vulgar, $censor_proper, $text);
  2208. else
  2209. $text = preg_replace($censor_vulgar, $censor_proper, $text);
  2210. return $text;
  2211. }
  2212. /**
  2213. * Load the template/language file using eval or require? (with eval we can show an error message!)
  2214. * - loads the template or language file specified by filename.
  2215. * - uses eval unless disableTemplateEval is enabled.
  2216. * - outputs a parse error if the file did not exist or contained errors.
  2217. * - attempts to detect the error and line, and show detailed information.
  2218. *
  2219. * @param string $filename The name of the file to include
  2220. * @param bool $once If true only includes the file once (like include_once)
  2221. */
  2222. function template_include($filename, $once = false)
  2223. {
  2224. global $context, $settings, $txt, $scripturl, $modSettings;
  2225. global $boardurl, $boarddir, $sourcedir;
  2226. global $maintenance, $mtitle, $mmessage;
  2227. static $templates = array();
  2228. // We want to be able to figure out any errors...
  2229. @ini_set('track_errors', '1');
  2230. // Don't include the file more than once, if $once is true.
  2231. if ($once && in_array($filename, $templates))
  2232. return;
  2233. // Add this file to the include list, whether $once is true or not.
  2234. else
  2235. $templates[] = $filename;
  2236. // Are we going to use eval?
  2237. if (empty($modSettings['disableTemplateEval']))
  2238. {
  2239. $file_found = file_exists($filename) && eval('?' . '>' . rtrim(file_get_contents($filename))) !== false;
  2240. $settings['current_include_filename'] = $filename;
  2241. }
  2242. else
  2243. {
  2244. $file_found = file_exists($filename);
  2245. if ($once && $file_found)
  2246. require_once($filename);
  2247. elseif ($file_found)
  2248. require($filename);
  2249. }
  2250. if ($file_found !== true)
  2251. {
  2252. ob_end_clean();
  2253. if (!empty($modSettings['enableCompressedOutput']))
  2254. @ob_start('ob_gzhandler');
  2255. else
  2256. ob_start();
  2257. if (isset($_GET['debug']) && !WIRELESS)
  2258. header('Content-Type: application/xhtml+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  2259. // Don't cache error pages!!
  2260. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  2261. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  2262. header('Cache-Control: no-cache');
  2263. if (!isset($txt['template_parse_error']))
  2264. {
  2265. $txt['template_parse_error'] = 'Template Parse Error!';
  2266. $txt['template_parse_error_message'] = 'It seems something has gone sour on the forum with the template system. This problem should only be temporary, so please come back later and try again. If you continue to see this message, please contact the administrator.<br><br>You can also try <a href="javascript:location.reload();">refreshing this page</a>.';
  2267. $txt['template_parse_error_details'] = 'There was a problem loading the <tt><strong>%1$s</strong></tt> template or language file. Please check the syntax and try again - remember, single quotes (<tt>\'</tt>) often have to be escaped with a slash (<tt>\\</tt>). To see more specific error information from PHP, try <a href="' . $boardurl . '%1$s" class="extern">accessing the file directly</a>.<br><br>You may want to try to <a href="javascript:location.reload();">refresh this page</a> or <a href="' . $scripturl . '?theme=1">use the default theme</a>.';
  2268. $txt['template_parse_errmsg'] = 'Unfortunately more information is not available at this time as to exactly what is wrong.';
  2269. }
  2270. // First, let's get the doctype and language information out of the way.
  2271. echo '<!DOCTYPE html>
  2272. <html', !empty($context['right_to_left']) ? ' dir="rtl"' : '', '>
  2273. <head>';
  2274. if (isset($context['character_set']))
  2275. echo '
  2276. <meta http-equiv="Content-Type" content="text/html; charset=', $context['character_set'], '">';
  2277. if (!empty($maintenance) && !allowedTo('admin_forum'))
  2278. echo '
  2279. <title>', $mtitle, '</title>
  2280. </head>
  2281. <body>
  2282. <h3>', $mtitle, '</h3>
  2283. ', $mmessage, '
  2284. </body>
  2285. </html>';
  2286. elseif (!allowedTo('admin_forum'))
  2287. echo '
  2288. <title>', $txt['template_parse_error'], '</title>
  2289. </head>
  2290. <body>
  2291. <h3>', $txt['template_parse_error'], '</h3>
  2292. ', $txt['template_parse_error_message'], '
  2293. </body>
  2294. </html>';
  2295. else
  2296. {
  2297. require_once($sourcedir . '/Subs-Package.php');
  2298. $error = fetch_web_data($boardurl . strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
  2299. if (empty($error) && ini_get('track_errors') && !empty($php_errormsg))
  2300. $error = $php_errormsg;
  2301. if (empty($error))
  2302. $error = $txt['template_parse_errmsg'];
  2303. $error = strtr($error, array('<b>' => '<strong>', '</b>' => '</strong>'));
  2304. echo '
  2305. <title>', $txt['template_parse_error'], '</title>
  2306. </head>
  2307. <body>
  2308. <h3>', $txt['template_parse_error'], '</h3>
  2309. ', sprintf($txt['template_parse_error_details'], strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
  2310. if (!empty($error))
  2311. echo '
  2312. <hr>
  2313. <div style="margin: 0 20px;"><tt>', strtr(strtr($error, array('<strong>' . $boarddir => '<strong>...', '<strong>' . strtr($boarddir, '\\', '/') => '<strong>...')), '\\', '/'), '</tt></div>';
  2314. // I know, I know... this is VERY COMPLICATED. Still, it's good.
  2315. if (preg_match('~ <strong>(\d+)</strong><br( /)?' . '>$~i', $error, $match) != 0)
  2316. {
  2317. $data = file($filename);
  2318. $data2 = highlight_php_code(implode('', $data));
  2319. $data2 = preg_split('~\<br( /)?\>~', $data2);
  2320. // Fix the PHP code stuff...
  2321. if (!isBrowser('gecko'))
  2322. $data2 = str_replace("\t", '<span style="white-space: pre;">' . "\t" . '</span>', $data2);
  2323. else
  2324. $data2 = str_replace('<pre style="display: inline;">' . "\t" . '</pre>', "\t", $data2);
  2325. // Now we get to work around a bug in PHP where it doesn't escape <br>s!
  2326. $j = -1;
  2327. foreach ($data as $line)
  2328. {
  2329. $j++;
  2330. if (substr_count($line, '<br>') == 0)
  2331. continue;
  2332. $n = substr_count($line, '<br>');
  2333. for ($i = 0; $i < $n; $i++)
  2334. {
  2335. $data2[$j] .= '&lt;br /&gt;' . $data2[$j + $i + 1];
  2336. unset($data2[$j + $i + 1]);
  2337. }
  2338. $j += $n;
  2339. }
  2340. $data2 = array_values($data2);
  2341. array_unshift($data2, '');
  2342. echo '
  2343. <div style="margin: 2ex 20px; width: 96%; overflow: auto;"><pre style="margin: 0;">';
  2344. // Figure out what the color coding was before...
  2345. $line = max($match[1] - 9, 1);
  2346. $last_line = '';
  2347. for ($line2 = $line - 1; $line2 > 1; $line2--)
  2348. if (strpos($data2[$line2], '<') !== false)
  2349. {
  2350. if (preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line2], $color_match) != 0)
  2351. $last_line = $color_match[1];
  2352. break;
  2353. }
  2354. // Show the relevant lines...
  2355. for ($n = min($match[1] + 4, count($data2) + 1); $line <= $n; $line++)
  2356. {
  2357. if ($line == $match[1])
  2358. echo '</pre><div style="background-color: #ffb0b5;"><pre style="margin: 0;">';
  2359. echo '<span style="color: black;">', sprintf('%' . strlen($n) . 's', $line), ':</span> ';
  2360. if (isset($data2[$line]) && $data2[$line] != '')
  2361. echo substr($data2[$line], 0, 2) == '</' ? preg_replace('~^</[^>]+>~', '', $data2[$line]) : $last_line . $data2[$line];
  2362. if (isset($data2[$line]) && preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line], $color_match) != 0)
  2363. {
  2364. $last_line = $color_match[1];
  2365. echo '</', substr($last_line, 1, 4), '>';
  2366. }
  2367. elseif ($last_line != '' && strpos($data2[$line], '<') !== false)
  2368. $last_line = '';
  2369. elseif ($last_line != '' && $data2[$line] != '')
  2370. echo '</', substr($last_line, 1, 4), '>';
  2371. if ($line == $match[1])
  2372. echo '</pre></div><pre style="margin: 0;">';
  2373. else
  2374. echo "\n";
  2375. }
  2376. echo '</pre></div>';
  2377. }
  2378. echo '
  2379. </body>
  2380. </html>';
  2381. }
  2382. die;
  2383. }
  2384. }
  2385. /**
  2386. * Initialize a database connection.
  2387. */
  2388. function loadDatabase()
  2389. {
  2390. global $db_persist, $db_connection, $db_server, $db_user, $db_passwd;
  2391. global $db_type, $db_name, $ssi_db_user, $ssi_db_passwd, $sourcedir, $db_prefix, $db_port;
  2392. // Figure out what type of database we are using.
  2393. if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
  2394. $db_type = 'mysql';
  2395. // Load the file for the database.
  2396. require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
  2397. $db_options = array();
  2398. // Add in the port if needed
  2399. if (!empty($db_port))
  2400. $db_options['port'] = $db_port;
  2401. // If we are in SSI try them first, but don't worry if it doesn't work, we have the normal username and password we can use.
  2402. if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
  2403. {
  2404. $options = array_merge($db_options, array('persist' => $db_persist, 'non_fatal' => true, 'dont_select_db' => true));
  2405. $db_connection = smf_db_initiate($db_server, $db_name, $ssi_db_user, $ssi_db_passwd, $db_prefix, $options);
  2406. }
  2407. // Either we aren't in SSI mode, or it failed.
  2408. if (empty($db_connection))
  2409. {
  2410. $options = array_merge($db_options, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI'));
  2411. $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
  2412. }
  2413. // Safe guard here, if there isn't a valid connection lets put a stop to it.
  2414. if (!$db_connection)
  2415. display_db_error();
  2416. // If in SSI mode fix up the prefix.
  2417. if (SMF == 'SSI')
  2418. db_fix_prefix($db_prefix, $db_name);
  2419. }
  2420. /**
  2421. * Try to retrieve a cache entry. On failure, call the appropriate function.
  2422. *
  2423. * @param string $key The key for this entry
  2424. * @param string $file The file associated with this entry
  2425. * @param string $function The function to call
  2426. * @param array $params Parameters to be passed to the specified function
  2427. * @param int $level The cache level
  2428. * @return string The cached data
  2429. */
  2430. function cache_quick_get($key, $file, $function, $params, $level = 1)
  2431. {
  2432. global $modSettings, $sourcedir;
  2433. // @todo Why are we doing this if caching is disabled?
  2434. if (function_exists('call_integration_hook'))
  2435. call_integration_hook('pre_cache_quick_get', array(&$key, &$file, &$function, &$params, &$level));
  2436. /* Refresh the cache if either:
  2437. 1. Caching is disabled.
  2438. 2. The cache level isn't high enough.
  2439. 3. The item has not been cached or the cached item expired.
  2440. 4. The cached item has a custom expiration condition evaluating to true.
  2441. 5. The expire time set in the cache item has passed (needed for Zend).
  2442. */
  2443. if (empty($modSettings['cache_enable']) || $modSettings['cache_enable'] < $level || !is_array($cache_block = cache_get_data($key, 3600)) || (!empty($cache_block['refresh_eval']) && eval($cache_block['refresh_eval'])) || (!empty($cache_block['expires']) && $cache_block['expires'] < time()))
  2444. {
  2445. require_once($sourcedir . '/' . $file);
  2446. $cache_block = call_user_func_array($function, $params);
  2447. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= $level)
  2448. cache_put_data($key, $cache_block, $cache_block['expires'] - time());
  2449. }
  2450. // Some cached data may need a freshening up after retrieval.
  2451. if (!empty($cache_block['post_retri_eval']))
  2452. eval($cache_block['post_retri_eval']);
  2453. if (function_exists('call_integration_hook'))
  2454. call_integration_hook('post_cache_quick_get', array(&$cache_block));
  2455. return $cache_block['data'];
  2456. }
  2457. /**
  2458. * Puts value in the cache under key for ttl seconds.
  2459. *
  2460. * - It may "miss" so shouldn't be depended on
  2461. * - Uses the cache engine chosen in the ACP and saved in settings.php
  2462. * - It supports:
  2463. * Xcache: http://xcache.lighttpd.net/wiki/XcacheApi
  2464. * memcache: http://www.php.net/memcache
  2465. * APC: http://www.php.net/apc
  2466. * Zend: http://files.zend.com/help/Zend-Platform/output_cache_functions.htm
  2467. * Zend: http://files.zend.com/help/Zend-Platform/zend_cache_functions.htm
  2468. *
  2469. * @param string $key A key for this value
  2470. * @param mixed $value The data to cache
  2471. * @param int $ttl How long (in seconds) the data should be cached for
  2472. */
  2473. function cache_put_data($key, $value, $ttl = 120)
  2474. {
  2475. global $boardurl, $sourcedir, $modSettings, $memcached;
  2476. global $cache_hits, $cache_count, $db_show_debug, $cachedir;
  2477. global $cache_accelerator, $cache_enable;
  2478. if (empty($cache_enable))
  2479. return;
  2480. $cache_count = isset($cache_count) ? $cache_count + 1 : 1;
  2481. if (isset($db_show_debug) && $db_show_debug === true)
  2482. {
  2483. $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value)));
  2484. $st = microtime();
  2485. }
  2486. $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . strtr($key, ':/', '-_');
  2487. $value = $value === null ? null : serialize($value);
  2488. switch ($cache_accelerator)
  2489. {
  2490. case 'memcached':
  2491. // The simple yet efficient memcached.
  2492. if (function_exists('memcached_set') || function_exists('memcache_set') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '')
  2493. {
  2494. // Not connected yet?
  2495. if (empty($memcached))
  2496. get_memcached_server();
  2497. if (!$memcached)
  2498. return;
  2499. memcache_set($memcached, $key, $value, 0, $ttl);
  2500. }
  2501. break;
  2502. case 'apc':
  2503. // Alternative PHP Cache, ahoy!
  2504. if (function_exists('apc_store'))
  2505. {
  2506. // An extended key is needed to counteract a bug in APC.
  2507. if ($value === null)
  2508. apc_delete($key . 'smf');
  2509. else
  2510. apc_store($key . 'smf', $value, $ttl);
  2511. }
  2512. break;
  2513. case 'zend':
  2514. // Zend Platform/ZPS/etc.
  2515. if (function_exists('zend_shm_cache_store'))
  2516. zend_shm_cache_store('SMF::' . $key, $value, $ttl);
  2517. elseif (function_exists('output_cache_put'))
  2518. output_cache_put($key, $value);
  2519. break;
  2520. case 'xcache':
  2521. if (function_exists('xcache_set') && ini_get('xcache.var_size') > 0)
  2522. {
  2523. if ($value === null)
  2524. xcache_unset($key);
  2525. else
  2526. xcache_set($key, $value, $ttl);
  2527. }
  2528. break;
  2529. default:
  2530. // Otherwise custom cache?
  2531. if ($value === null)
  2532. @unlink($cachedir . '/data_' . $key . '.php');
  2533. else
  2534. {
  2535. $cache_data = '<' . '?' . 'php if (!defined(\'SMF\')) die; if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}' . '?' . '>';
  2536. // Write out the cache file, check that the cache write was successful; all the data must be written
  2537. // If it fails due to low diskspace, or other, remove the cache file
  2538. if (file_put_contents($cachedir . '/data_' . $key . '.php', $cache_data, LOCK_EX) !== strlen($cache_data))
  2539. @unlink($cachedir . '/data_' . $key . '.php');
  2540. }
  2541. break;
  2542. }
  2543. if (function_exists('call_integration_hook'))
  2544. call_integration_hook('cache_put_data', array(&$key, &$value, &$ttl));
  2545. if (isset($db_show_debug) && $db_show_debug === true)
  2546. $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
  2547. }
  2548. /**
  2549. * Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
  2550. * - It may often "miss", so shouldn't be depended on.
  2551. * - It supports the same as cache_put_data().
  2552. *
  2553. * @param string $key The key for the value to retrieve
  2554. * @param int $ttl The maximum age of the cached data
  2555. * @return string The cached data or null if nothing was loaded
  2556. */
  2557. function cache_get_data($key, $ttl = 120)
  2558. {
  2559. global $boardurl, $sourcedir, $modSettings, $memcached;
  2560. global $cache_hits, $cache_count, $db_show_debug, $cachedir;
  2561. global $cache_accelerator, $cache_enable;
  2562. if (empty($cache_enable))
  2563. return;
  2564. $cache_count = isset($cache_count) ? $cache_count + 1 : 1;
  2565. if (isset($db_show_debug) && $db_show_debug === true)
  2566. {
  2567. $cache_hits[$cache_count] = array('k' => $key, 'd' => 'get');
  2568. $st = microtime();
  2569. }
  2570. $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . strtr($key, ':/', '-_');
  2571. switch ($cache_accelerator)
  2572. {
  2573. case 'memcache':
  2574. // Okay, let's go for it memcached!
  2575. if ((function_exists('memcache_get') || function_exists('memcached_get')) && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '')
  2576. {
  2577. // Not connected yet?
  2578. if (empty($memcached))
  2579. get_memcached_server();
  2580. if (!$memcached)
  2581. return null;
  2582. $value = (function_exists('memcache_get')) ? memcache_get($cache['connection'], $key) : memcached_get($cache['connection'], $key);
  2583. }
  2584. break;
  2585. case 'apc':
  2586. // This is the free APC from PECL.
  2587. if (function_exists('apc_fetch'))
  2588. $value = apc_fetch($key . 'smf');
  2589. break;
  2590. case 'zend':
  2591. // Zend's pricey stuff.
  2592. if (function_exists('zend_shm_cache_fetch'))
  2593. $value = zend_shm_cache_fetch('SMF::' . $key);
  2594. elseif (function_exists('output_cache_get'))
  2595. $value = output_cache_get($key, $ttl);
  2596. break;
  2597. case 'xcache':
  2598. if (function_exists('xcache_get') && ini_get('xcache.var_size') > 0)
  2599. $value = xcache_get($key);
  2600. break;
  2601. default:
  2602. // Otherwise it's SMF data!
  2603. if (file_exists($cachedir . '/data_' . $key . '.php') && filesize($cachedir . '/data_' . $key . '.php') > 10)
  2604. {
  2605. // php will cache file_exists et all, we can't 100% depend on its results so proceed with caution
  2606. @include($cachedir . '/data_' . $key . '.php');
  2607. if (!empty($expired) && isset($value))
  2608. {
  2609. @unlink($cachedir . '/data_' . $key . '.php');
  2610. unset($value);
  2611. }
  2612. }
  2613. break;
  2614. }
  2615. if (isset($db_show_debug) && $db_show_debug === true)
  2616. {
  2617. $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
  2618. $cache_hits[$cache_count]['s'] = isset($value) ? strlen($value) : 0;
  2619. }
  2620. if (function_exists('call_integration_hook') && isset($value))
  2621. call_integration_hook('cache_get_data', array(&$key, &$ttl, &$value));
  2622. return empty($value) ? null : @unserialize($value);
  2623. }
  2624. /**
  2625. * Get memcache servers.
  2626. *
  2627. * - This function is used by cache_get_data() and cache_put_data().
  2628. * - It attempts to connect to a random server in the cache_memcached setting.
  2629. * - It recursively calls itself up to $level times.
  2630. *
  2631. * @param int $level The maximum number of times to call this function recursively
  2632. */
  2633. function get_memcached_server($level = 3)
  2634. {
  2635. global $memcached, $db_persist, $cache_memcached;
  2636. $servers = explode(',', $cache_memcached);
  2637. $server = explode(':', trim($servers[array_rand($servers)]));
  2638. $cache = (function_exists('memcache_get')) ? 'memcache' : ((function_exists('memcached_get') ? 'memcached' : ''));
  2639. // Don't try more times than we have servers!
  2640. $level = min(count($servers), $level);
  2641. // Don't wait too long: yes, we want the server, but we might be able to run the query faster!
  2642. if (empty($db_persist))
  2643. {
  2644. if ($cache === 'memcached')
  2645. $memcached = memcached_connect($server[0], empty($server[1]) ? 11211 : $server[1]);
  2646. if ($cache === 'memcache')
  2647. $memcached = memcache_connect($server[0], empty($server[1]) ? 11211 : $server[1]);
  2648. }
  2649. else
  2650. {
  2651. if ($cache === 'memcached')
  2652. $memcached = memcached_pconnect($server[0], empty($server[1]) ? 11211 : $server[1]);
  2653. if ($cache === 'memcache')
  2654. $memcached = memcache_pconnect($server[0], empty($server[1]) ? 11211 : $server[1]);
  2655. }
  2656. if (!$memcached && $level > 0)
  2657. get_memcached_server($level - 1);
  2658. }
  2659. ?>