PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/legacy/scripts/php/functions.php

http://novaboard.googlecode.com/
PHP | 1566 lines | 986 code | 383 blank | 197 comment | 171 complexity | b0335e8a218c9c0b23dc142c06441a63 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | NovaBoard
  5. | ========================================
  6. | By The NovaBoard team
  7. | Released under the Artistic License 2.0
  8. | http://www.novaboard.net
  9. | ========================================
  10. |+--------------------------------------------------------------------------
  11. | functions.php - This holds PHP functions for NovaBoard
  12. */
  13. if (!defined('NOVA_RUN')){
  14. echo "<h1>ACCESS DENIED</h1>You cannot access this file directly.";
  15. exit();
  16. }
  17. // if this file exists in a theme folder, then use it, otherwise run the below code...
  18. if (!isset($theme)){
  19. $theme = "novaboard_default";
  20. }
  21. if (file_exists("themes/$theme/scripts/php/functions.php")){
  22. include "themes/$theme/scripts/php/functions.php";
  23. }
  24. else{
  25. ///////////////////////////////////////////////////
  26. // Latin character SEO
  27. function clean_url_seo ($makeFriendly){
  28. $charCheck = array(
  29. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  30. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  31. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  32. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  33. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  34. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  35. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  36. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  37. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  38. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  39. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  40. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  41. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  42. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  43. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  44. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  45. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  46. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  47. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  48. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  49. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  50. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  51. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  52. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  53. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  54. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  55. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  56. chr(195).chr(191) => 'y',
  57. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  58. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  59. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  60. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  61. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  62. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  63. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  64. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  65. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  66. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  67. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  68. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  69. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  70. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  71. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  72. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  73. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  74. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  75. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  76. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  77. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  78. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  79. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  80. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  81. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  82. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  83. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  84. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  85. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  86. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  87. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  88. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  89. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  90. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  91. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  92. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  93. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  94. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  95. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  96. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  97. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  98. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  99. chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
  100. chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
  101. chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
  102. chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
  103. chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
  104. chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
  105. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  106. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  107. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  108. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  109. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  110. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  111. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  112. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  113. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  114. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  115. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  116. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  117. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  118. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  119. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  120. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  121. chr(226).chr(130).chr(172) => 'E',
  122. chr(194).chr(163) => ''
  123. );
  124. $makeFriendly = strtr($makeFriendly, $charCheck);
  125. $makeFriendly = strtolower ($makeFriendly);
  126. $makeFriendly = preg_replace ('#&.+?;#', '', $makeFriendly);
  127. $makeFriendly = preg_replace ('#[^%a-z0-9 _-]#', '', $makeFriendly);
  128. $makeFriendly = str_replace ( array( ' ', '+', '.', '?' ), '-', $makeFriendly);
  129. $makeFriendly = preg_replace ("#-{2,}#", '-', $makeFriendly);
  130. $makeFriendly = trim ($makeFriendly, '-');
  131. return $makeFriendly;
  132. }
  133. ///////////////////////////////////////////////////
  134. // Copy files from one server to another
  135. function nova_remote($url,$dirname){
  136. $file = basename($url);
  137. if ($dirname =='modules/'){
  138. $url ="http://plugins.novaboard.net/uploads/$file";
  139. }
  140. else{
  141. $url ="http://themes.novaboard.net/uploads/$file";
  142. }
  143. $filename = basename($url);
  144. $fc = fopen($dirname."$filename", "wb") or die("CREATE NEW");
  145. $contents = file_get_contents($url) or die("CONTENTS");
  146. fwrite($fc,$contents) or die("LOCAL WRITE");
  147. fclose($fc);
  148. return true;
  149. }
  150. ///////////////////////////////////////////////////
  151. // Copy files from one place to another
  152. function nova_copy($source, $target)
  153. {
  154. if ( is_dir( $source ) )
  155. {
  156. chmod ($source, 0777); // chmod the directory to make sure the file copies...
  157. @mkdir( $target );
  158. $d = dir( $source );
  159. while ( FALSE !== ( $entry = $d->read() ) )
  160. {
  161. if ( $entry == '.' || $entry == '..' )
  162. {
  163. continue;
  164. }
  165. $Entry = $source . '/' . $entry;
  166. if ( is_dir( $Entry ) )
  167. {
  168. nova_copy( $Entry, $target . '/' . $entry );
  169. continue;
  170. }
  171. copy( $Entry, $target . '/' . $entry );
  172. }
  173. $d->close();
  174. chmod ($source, 0755); // chmod the directory to make it safe again...
  175. }else
  176. {
  177. copy( $source, $target );
  178. }
  179. }
  180. ///////////////////////////////////////////////////
  181. // Remove directories and files
  182. function nova_remove($dirname)
  183. {
  184. // Sanity check
  185. if (!file_exists($dirname)) {
  186. }
  187. // Simple delete for a file
  188. if (is_file($dirname)) {
  189. return unlink($dirname);
  190. }
  191. // Loop through the folder
  192. $dir = dir($dirname);
  193. while (false !== $entry = $dir->read()) {
  194. // Skip pointers
  195. if ($entry == '.' || $entry == '..') {
  196. continue;
  197. }
  198. // Recurse
  199. nova_remove("$dirname/$entry");
  200. }// end while looping
  201. // Clean up
  202. $dir->close();
  203. return rmdir($dirname);
  204. }
  205. ///////////////////////////////////////////////////
  206. // Search engine friendly URL's
  207. function nova_link($link_off, $link_on){
  208. global $nova_domain, $sef_urls;
  209. if($sef_urls=='1'){
  210. $link="$nova_domain/$link_on";
  211. }
  212. else{
  213. $link="$nova_domain/$link_off";
  214. }
  215. return $link;
  216. }
  217. ///////////////////////////////////////////////////
  218. // Redirect function
  219. function nova_redirect($link, $sef){
  220. session_write_close();
  221. header('HTTP/1.0 200 OK');
  222. header('Location: ' . nova_link($link, $sef));
  223. exit;
  224. }
  225. ///////////////////////////////////////////////////
  226. // Create short url's in post entries
  227. // Example: http://www.domain.com/index.php?pa...6452332
  228. function short_url(&$addy){
  229. $links = explode('<a', $addy);
  230. $countlinks = count($links);
  231. for ($i = 0; $i < $countlinks; $i++){
  232. $link = $links[$i];
  233. $link = (preg_match('#(.*)(href=")#is', $link)) ? '<a' . $link : $link;
  234. $begin = strpos($link, '>') + 1;
  235. $end = @strpos($link, '<', $begin);
  236. $length = $end - $begin;
  237. $urlname = substr($link, $begin, $length);
  238. $shorturlname = (strlen($urlname) > 50 && preg_match('#^(http://|ftp://|www\.)#is', $urlname)) ? substr_replace($urlname, '...', 30, -10) : $urlname;
  239. $addy = str_replace('>' . $urlname . '<', '>' . $shorturlname . '<', $addy);
  240. }
  241. }
  242. ///////////////////////////////////////////////////
  243. // List all themes in themes folder and put into a select form
  244. function list_themes(){
  245. global $db_prefix;
  246. $query29 = "select DISPLAY_NAME, THEME_NAME from {$db_prefix}themes WHERE INSTALLED='1'";
  247. $result29 = mysql_query($query29) or die("modules.php - Error in query: $query29") ;
  248. while ($results29 = mysql_fetch_array($result29)){
  249. $display_name = $results29['DISPLAY_NAME'];
  250. $theme_name = $results29['THEME_NAME'];
  251. echo "<option value=\"$theme_name\">$display_name</option>";
  252. }
  253. }
  254. ///////////////////////////////////////////////////
  255. // List all languages in lang folder and put into a select form
  256. function list_lang($dir){
  257. global $db_prefix, $flag_path, $nova_domain;
  258. if(is_dir($dir)){
  259. if($handle = opendir($dir)){
  260. while(($file = readdir($handle)) !== false){
  261. if($file != "." && $file != ".." && $file != "Thumbs.db" && $file!="index.html"){
  262. // explode into 2 parts {lang}_{flag} then put into options for selections
  263. $lang_option = explode("_", $file);
  264. $lang_option[1] = mysql_real_escape_string($lang_option[1]);
  265. // check if this language exists, if it dosen't then we don't want to give the user the option to use it
  266. $check_language = mysql_query("SELECT `nation_short` FROM `{$db_prefix}nations` WHERE `nation_short` = '{$lang_option[1]}' LIMIT 1");
  267. if(mysql_num_rows($check_language) === 1) {
  268. // Capital letters please...
  269. $lang_option[0] = ucfirst($lang_option[0]);
  270. echo "<option value='$file' style='background: url($nova_domain/$flag_path/$lang_option[1].png) no-repeat;'>&nbsp;$lang_option[0]</option>";
  271. }
  272. }
  273. }
  274. }
  275. closedir($handle);
  276. }
  277. }
  278. ///////////////////////////////////////////////////
  279. // Escape string
  280. function escape_string($string) {
  281. $string = addslashes($string);
  282. $string = htmlspecialchars($string);
  283. return $string;
  284. }
  285. ///////////////////////////////////////////////////
  286. // Unescape string
  287. function strip_slashes($string) {
  288. if (get_magic_quotes_gpc()){
  289. $string = stripslashes($string);
  290. }
  291. $string = str_replace("&amp;", "&", $string);
  292. return $string;
  293. }
  294. ///////////////////////////////////////////////////
  295. // random password generator
  296. function createRandomPassword() {
  297. $chars = "abcdefghijkmnopqrstuvwxyz023456789";
  298. srand((double)microtime()*1000000);
  299. $i = 0;
  300. $pass = '' ;
  301. while ($i <= 7) {
  302. $num = rand() % 33;
  303. $tmp = substr($chars, $num, 1);
  304. $pass = $pass . $tmp;
  305. $i++;
  306. }
  307. return $pass;
  308. }
  309. ///////////////////////////////////////////////////
  310. // format date to show today/yesterday
  311. // and convert to language member has selected
  312. function format_date($var, $format = null) {
  313. global $time_offset, $board_lang, $format_time, $date_today, $date_yesterday, $date_minute, $date_minutes, $date_hour, $date_hours;
  314. // set locale based on board lang
  315. $board_locale = explode("_",$board_lang);
  316. $board_locale[1] = $board_locale[1]."_".(strtoupper($board_locale[1]));
  317. // check PHP version
  318. if(version_compare(phpversion(),"4.3.0")=="-1") {
  319. setlocale(LC_TIME, $board_locale[0]);
  320. }else{
  321. setlocale(LC_TIME, $board_locale[0].'.UTF-8', $board_locale[1].'.UTF-8', $board_locale[0].'.UTF8', $board_locale[1].'.UTF8');
  322. }
  323. $time = $var + ($time_offset * 60 * 60);
  324. $today=time() + ($time_offset * 60 * 60);
  325. $yesterday=time() + (($time_offset * 60 * 60)-86400);
  326. $today=date('l, M jS', $today);
  327. $yesterday=date('l, M jS', $yesterday);
  328. $current_day=date('l, M jS', $time);
  329. if (isset($format)){
  330. $time = strftime($format, $time);
  331. $time = ucwords($time);
  332. }
  333. elseif ($current_day==$today){
  334. // check if it was posted less than an hour ago...
  335. if ((time() - $var) < '3600'){
  336. $minutes = round((time() - $var)/60);
  337. if ($minutes =='1'){
  338. $time = "$minutes $date_minute";
  339. }
  340. else{
  341. $time = "$minutes $date_minutes";
  342. }
  343. }
  344. elseif ((time() - $var) < '43200'){
  345. $hours = round((time() - $var)/60/60);
  346. if ($hours =='1'){
  347. $time = "$hours $date_hour";
  348. }
  349. else{
  350. $time = "$hours $date_hours";
  351. }
  352. }
  353. else{
  354. $day=$date_today;
  355. $time = date(', H:i', $time);
  356. $time="$day$time";
  357. }
  358. }
  359. elseif($current_day==$yesterday){
  360. $day=$date_yesterday;
  361. $time = date(', H:i', $time);
  362. $time="$day$time";
  363. }
  364. else{
  365. $time = strftime($format_time, $time);
  366. $time = ucwords($time);
  367. }
  368. // Return locale to default
  369. setlocale(LC_ALL, null);
  370. return $time;
  371. }
  372. ///////////////////////////////////////////////////
  373. // check image exists, and decide if it is theme/default
  374. // that shows up
  375. function image_check($var){
  376. global $nova_domain, $theme, $board_lang;
  377. $theme=str_replace("%20", " ", $theme);
  378. if (file_exists("../themes/$theme/images/$var")){
  379. $path = "$nova_domain/themes/$theme/images/$var";
  380. }
  381. elseif (file_exists("themes/$theme/images/$var")){
  382. $path = "$nova_domain/themes/$theme/images/$var";
  383. }
  384. else{
  385. $path = "$nova_domain/images/$var";
  386. }
  387. return $path;
  388. }
  389. ///////////////////////////////////////////////////
  390. // check folder exists, and decide if it is theme/default
  391. // that shows up
  392. function folder_check($var){
  393. global $nova_domain, $theme;
  394. if (file_exists("themes/$theme/images/$var")){
  395. $path = "themes/$theme/images/$var";
  396. }
  397. else{
  398. $path = "images/$var";
  399. }
  400. return $path;
  401. }
  402. ///////////////////////////////////////////////////
  403. // Template and plug-in function
  404. function template_hook($page_location, $template_hook)
  405. {
  406. extract($GLOBALS, EXTR_SKIP);
  407. // for modules, drop the .template.php
  408. $template_folder = str_replace(".template.php", "", $page_location);
  409. /*
  410. Hooks before this template?
  411. */
  412. /*
  413. Directory separators aren't allowed in filenames,
  414. change them to hypens (-)
  415. */
  416. $file = str_replace('/', '-', $template_folder);
  417. /*
  418. Now check for any modules and run the code if found.
  419. */
  420. if ($code = $Plugin->hook('template_hook', $file . '_' . $template_hook . '_before'))
  421. {
  422. eval($code);
  423. }
  424. /*
  425. ---------------------------------------------------------
  426. CODE BELOW IS DEPRECATED DUE TO THE NEW HOOKS SYSTEM.
  427. WILL BE REMOVED IN 1.3
  428. ---------------------------------------------------------
  429. */
  430. // check modules folder for any other folders...
  431. $dir = "modules/";
  432. $modules_installed="0";
  433. # Load cached modules
  434. include $nova_root . '/cache/modules.php';
  435. $modules_installed = count($cache);
  436. // if there are no installed modules, skip this part...
  437. $installed = 0;
  438. $runDefault = true;
  439. $runDefaultFile = '';
  440. $runDefaultFileLocation = '';
  441. if ($modules_installed != 0)
  442. {
  443. // is it in the database?
  444. foreach ($cache as $mod)
  445. {
  446. $module = $mod['module_name'];
  447. if (file_exists($nova_root . 'modules/'.$module.'/templates/includes/'.$template_folder.'/replace/'.$template_hook.'.php'))
  448. {
  449. $runDefaultFile = $module;
  450. $runDefaultFileLocation = 'replace';
  451. continue;
  452. }
  453. elseif (file_exists($nova_root . 'modules/'.$module.'/templates/includes/'.$template_folder.'/before/'.$template_hook.'.php'))
  454. {
  455. $runDefaultFile = $module;
  456. $runDefaultFileLocation = 'before';
  457. continue;
  458. }
  459. elseif (file_exists($nova_root . 'modules/'.$module.'/templates/includes/'.$template_folder.'/after/'.$template_hook.'.php') && $runDefaultFileLocation != 'after') {
  460. $runDefaultFile = $module;
  461. $runDefaultFileLocation = 'after';
  462. continue;
  463. }
  464. else
  465. {
  466. if ($runDefaultFile == '')
  467. {
  468. $runDefaultFile = $module;
  469. $runDefaultFileLocation = '';
  470. continue;
  471. }
  472. }
  473. }
  474. foreach ($cache as $mod)
  475. {
  476. $module = $mod['module_name'];
  477. if (file_exists("modules/$module/templates/includes/$template_folder/before/$template_hook.php"))
  478. {
  479. if (file_exists("themes/$theme/modules/$module/templates/includes/$template_folder/before/$template_hook.php"))
  480. {
  481. include $nova_root . "themes/$theme/modules/$module/templates/includes/$template_folder/before/$template_hook.php";
  482. }
  483. else
  484. {
  485. include $nova_root . "modules/$module/templates/includes/$template_folder/before/$template_hook.php";
  486. }
  487. if ($runDefaultFile == $module && $runDefaultFileLocation == 'before')
  488. {
  489. if (file_exists("themes/$theme/templates/includes/$page_location"))
  490. {
  491. include $nova_root . "themes/$theme/templates/includes/$page_location";
  492. }
  493. else
  494. {
  495. include $nova_root . "templates/includes/$page_location";
  496. }
  497. $runDefault = false;
  498. }
  499. }
  500. if (file_exists("modules/$module/templates/includes/$template_folder/replace/$template_hook.php"))
  501. {
  502. if ($runDefaultFile == $module && $runDefaultFileLocation == 'replace')
  503. {
  504. if (file_exists("themes/$theme/modules/$module/templates/includes/$template_folder/replace/$template_hook.php"))
  505. {
  506. include $nova_root . "themes/$theme/modules/$module/templates/includes/$template_folder/replace/$template_hook.php";
  507. }
  508. else
  509. {
  510. include $nova_root . "modules/$module/templates/includes/$template_folder/replace/$template_hook.php";
  511. }
  512. $runDefault = false;
  513. }
  514. }
  515. if (file_exists("modules/$module/templates/includes/$template_folder/after/$template_hook.php"))
  516. {
  517. if ($runDefaultFile == $module && $runDefaultFileLocation == 'after')
  518. {
  519. if (file_exists("themes/$theme/templates/includes/$page_location"))
  520. {
  521. include $nova_root . "themes/$theme/templates/includes/$page_location";
  522. }
  523. else
  524. {
  525. include $nova_root . "templates/includes/$page_location";
  526. }
  527. $runDefault = false;
  528. }
  529. if (file_exists("themes/$theme/modules/$module/templates/includes/$template_folder/after/$template_hook.php"))
  530. {
  531. include $nova_root . "themes/$theme/modules/$module/templates/includes/$template_folder/after/$template_hook.php";
  532. }
  533. else
  534. {
  535. include $nova_root . "modules/$module/templates/includes/$template_folder/after/$template_hook.php";
  536. }
  537. }
  538. if ($runDefault == true)
  539. {
  540. if ($runDefaultFile == $module)
  541. {
  542. if (file_exists("themes/$theme/templates/includes/$page_location")){
  543. include $nova_root . "themes/$theme/templates/includes/$page_location";
  544. }
  545. else{
  546. include $nova_root . "templates/includes/$page_location";
  547. }
  548. $runDefault = false;
  549. }
  550. }
  551. }
  552. }
  553. else
  554. {
  555. if ($runDefault == true)
  556. {
  557. if (file_exists("themes/$theme/templates/includes/$page_location")){
  558. include $nova_root . "themes/$theme/templates/includes/$page_location";
  559. }
  560. else{
  561. include $nova_root . "templates/includes/$page_location";
  562. }
  563. }
  564. }
  565. /*
  566. ---------------------------------------------------------
  567. END DEPRECATED CODE
  568. ---------------------------------------------------------
  569. */
  570. /*
  571. Hooks afterwards...
  572. */
  573. if ($code = $Plugin->hook('template_hook', $file . '_' . $template_hook . '_after'))
  574. {
  575. eval($code);
  576. }
  577. }
  578. ///////////////////////////////////////////////////
  579. // List all modules
  580. function list_modules($dir){
  581. extract($GLOBALS, EXTR_SKIP);
  582. $dir="modules/";
  583. if(is_dir($dir)){
  584. if($handle = opendir($dir)){
  585. while(($file = readdir($handle)) !== false){
  586. if($file != "." && $file != ".." && $file != "Thumbs.db" && $file!="index.html"){
  587. $pos = strpos($file, ".zip");
  588. if ($pos === FALSE){
  589. // is it in the database?
  590. $file_str = str_replace("%20", " ", $file);
  591. $installed="0";
  592. $query29 = "select INSTALLED from {$db_prefix}modules WHERE MODULE_NAME='$file_str'";
  593. $result29 = mysql_query($query29) or die("modules.php - Error in query: $query29") ;
  594. $installed = mysql_result($result29, 0);
  595. if ($installed=='0' OR $installed==''){
  596. echo "<table class=\"forum-jump module-not-installed\" cellpadding=\"0\" cellspacing=\"0\">";
  597. }
  598. else{
  599. echo "<table class=\"forum-jump module-installed\" cellpadding=\"0\" cellspacing=\"0\">";
  600. }
  601. $xml_file = "modules/$file/information.xml";
  602. $xml_handle = fopen($xml_file, "r");
  603. $xml_data = fread($xml_handle, filesize($xml_file));
  604. fclose($xml_handle);
  605. echo "<tr><td class=\"forum-jump-content\">";
  606. preg_match('/<version>(.*)<\/version>/s',$xml_data,$installed_version);
  607. preg_match('/<update>(.*)<\/update>/s',$xml_data,$update_url_check);
  608. $xml_data = htmlspecialchars($xml_data);
  609. $installed_version = $installed_version[1];
  610. $update_url_check = $update_url_check[1];
  611. $xml_data = str_replace("&lt;", "<", $xml_data);
  612. $xml_data = str_replace("&gt;", ">", $xml_data);
  613. $xml_data = preg_replace("#\<module\>(.*?)\</module\>(.*?)#is", "<strong>$1</strong>$2", $xml_data);
  614. $xml_data = preg_replace("#(.*?)\<version\>(.*?)\</version\>(.*?)#is", "$1 ($2)$3", $xml_data);
  615. $xml_data = preg_replace("#(.*?)\<author\>(.*?)\</author\>(.*?)#is", "$1<br /><i>By $2</i>$3", $xml_data);
  616. $xml_data = preg_replace("#(.*?)\<site\>(.*?)\</site\>(.*?)#is", "$1 - <a href=\"$2\"><i>$2</i></a><br /><br />$3", $xml_data);
  617. $xml_data = preg_replace("#(.*?)\<description\>(.*?)\</description\>(.*?)#is", "$1$2", $xml_data);
  618. $xml_data = preg_replace("#(.*?)\<update\>(.*?)\</update\>(.*?)#is", "$1", $xml_data);
  619. echo "<table cellpadding=\"0\" cellspacing=\"0\"><tr>";
  620. echo "<td style=\"vertical-align: top;\">";
  621. echo "<img src=\"$nova_domain/modules/$file/screenshot.jpg\" alt=\"screenshot\" />";
  622. echo "</td>";
  623. echo "<td style=\"padding-left: 5px; vertical-align: top;\">";
  624. echo "$xml_data";
  625. echo "<br /><br />";
  626. echo "<span style='text-align: right;'>";
  627. if ($installed=='0' OR $installed==''){
  628. echo "<a class='submit-button img-plugin-add' href='$nova_domain/index.php?page=admin&act=modules&func=install&module=$file'>$lang[button_install]</a>";
  629. echo " <a class='submit-button button-remove img-plugin-del' href='$nova_domain/index.php?page=admin&act=modules&func=delete&module=$file'>$lang[button_remove]</a>";
  630. }
  631. else{
  632. // check if an update is available, if so, provide a link...
  633. $filename = $update_url_check;
  634. $data = file_get_contents($filename);
  635. preg_match('/<version>(.*)<\/version>/s',$data,$current_version);
  636. preg_match('/<update>(.*)<\/update>/s',$data,$update_url);
  637. $current_version = $current_version[1];
  638. $update_url = $update_url[1];
  639. if ($current_version!=$installed_version && $update_url!=''){
  640. echo "<a class='submit-button button-update img-error' href='$nova_domain/index.php?page=admin&act=modules&func=update&module=$file&update_url=$update_url'>$lang[button_update]</a>&nbsp;";
  641. }
  642. echo "<a class='submit-button img-plugin-uninstall' href='$nova_domain/index.php?page=admin&act=modules&func=remove&module=$file'>$lang[button_uninstall]</a>";
  643. }
  644. echo "</span>";
  645. echo "</td></tr></table>";
  646. echo "</td></tr></table>";
  647. echo "<div class=\"spacer\">&nbsp;</div>";
  648. }
  649. }
  650. }
  651. }
  652. closedir($handle);
  653. }
  654. }
  655. ///////////////////////////////////////////////////
  656. // List all themes
  657. function list_themes_admin($dir){
  658. extract($GLOBALS, EXTR_SKIP);
  659. $dir="themes/";
  660. if(is_dir($dir)){
  661. if($handle = opendir($dir)){
  662. while(($file = readdir($handle)) !== false){
  663. if($file != "." && $file != ".." && $file != "Thumbs.db" && $file!="index.html"){
  664. $pos = strpos($file, ".zip");
  665. if ($pos === FALSE){
  666. // is it in the database?
  667. $installed = "0";
  668. $file_str = str_replace("%20", " ", $file);
  669. $query29 = "select INSTALLED from {$db_prefix}themes WHERE THEME_NAME='$file_str'";
  670. $result29 = mysql_query($query29) or die("themes.php - Error in query: $query29") ;
  671. $installed = mysql_result($result29, 0);
  672. if ($installed=='0' OR $installed==''){
  673. echo "<table class=\"forum-jump\" cellpadding=\"0\" cellspacing=\"0\" class=\"module-not-installed\">";
  674. }
  675. else{
  676. echo "<table class=\"forum-jump\" cellpadding=\"0\" cellspacing=\"0\" class=\"module-installed\">";
  677. }
  678. $xml_file = "themes/$file/information.xml";
  679. $xml_handle = fopen($xml_file, "r");
  680. $xml_data = fread($xml_handle, filesize($xml_file));
  681. fclose($xml_handle);
  682. echo "<tr><td class=\"forum-jump-content\">";
  683. $xml_data = htmlspecialchars($xml_data);
  684. $xml_data = str_replace("&lt;", "<", $xml_data);
  685. $xml_data = str_replace("&gt;", ">", $xml_data);
  686. $xml_data = preg_replace("#\<theme\>(.*?)\</theme\>(.*?)#is", "<strong>$1</strong>$2", $xml_data);
  687. $xml_data = preg_replace("#(.*?)\<version\>(.*?)\</version\>(.*?)#is", "$1 ($2)$3", $xml_data);
  688. $xml_data = preg_replace("#(.*?)\<author\>(.*?)\</author\>(.*?)#is", "$1<br /><i>By $2</i>$3", $xml_data);
  689. $xml_data = preg_replace("#(.*?)\<site\>(.*?)\</site\>(.*?)#is", "$1 - <a href=\"$2\"><i>$2</i></a><br /><br />$3", $xml_data);
  690. $xml_data = preg_replace("#(.*?)\<description\>(.*?)\</description\>(.*?)#is", "$1$2", $xml_data);
  691. echo "<table cellpadding=\"0\" cellspacing=\"0\"><tr>";
  692. echo "<td style=\"vertical-align: top;\">";
  693. echo "<img src=\"$nova_domain/themes/$file/screenshot.jpg\" alt=\"screenshot\" />";
  694. echo "</td>";
  695. echo "<td style=\"padding-left: 5px; vertical-align: top;\">";
  696. echo "$xml_data";
  697. echo "<br /><br />";
  698. echo "<span style='text-align: right;'>";
  699. if ($installed=='0' OR $installed==''){
  700. echo "<a class='submit-button img-plugin-add' href='$nova_domain/index.php?page=admin&act=themes&func=install&theme=$file'>$lang[button_install]</a>";
  701. if ($file!='novaboard_default'){
  702. echo " <a class='submit-button button-remove img-plugin-del' href='$nova_domain/index.php?page=admin&act=themes&func=delete&theme=$file'>$lang[button_remove]</a>";
  703. }
  704. }
  705. else{
  706. echo "<a class='submit-button img-plugin-uninstall' href='$nova_domain/index.php?page=admin&act=themes&func=remove&theme=$file'>$lang[button_uninstall]</a>";
  707. }
  708. echo "</span>";
  709. echo "</td></tr></table>";
  710. echo "</td></tr></table>";
  711. echo "<div class=\"spacer\">&nbsp;</div>";
  712. }
  713. }
  714. }
  715. }
  716. closedir($handle);
  717. }
  718. }
  719. ///////////////////////////////////////////////////
  720. // Member name construct function
  721. function member_link($memberid, $flag=1, $color=1, $time=null)
  722. {
  723. global $Plugin, $db_prefix, $nova_domain, $flag_path;
  724. #unset($member_html);
  725. if (isset($memberid) && $memberid!='0' OR $memberid!='')
  726. {
  727. $field = ($time == 1) ? ', s.time' : '';
  728. $extra = ($time == 1) ? 'LEFT JOIN ' . $db_prefix . 'sessions s ON m.id = s.id' : '';
  729. # Get name, flag, group, group colour & online time (if needed)
  730. $query = mysql_query('
  731. SELECT m.name, m.nationality, m.role, g.group_color ' . $field . '
  732. FROM ' . $db_prefix . 'members m
  733. INNER JOIN ' . $db_prefix . 'groups g
  734. ON m.role = g.group_id
  735. ' . $extra . '
  736. WHERE m.id = ' . $memberid
  737. );
  738. $row = mysql_fetch_assoc($query);
  739. $member_name = strip_slashes($row['name']);
  740. $member_flag = $row['nationality'];
  741. $member_role = $row['role'];
  742. $member_color = $row['group_color'];
  743. $time = ( $time == 1 ) ? format_date( $row['time'] ) : '';
  744. // construct the html...
  745. $member_html = "<span style=\"white-space: nowrap;\">";
  746. if ($flag == 1)
  747. {
  748. if ($member_flag=='')
  749. {
  750. $member_html .= "<img src=\"$nova_domain/$flag_path/0.png\" alt=\"\" />";
  751. }
  752. else
  753. {
  754. $member_html .= "<img src=\"$nova_domain/$flag_path/$member_flag.png\" alt=\"\" />";
  755. }
  756. }
  757. $title = clean_url_seo($member_name);
  758. $member_html .= " <a class=\"forum-index-link-to-topic\" href=\"".nova_link("index.php?page=members&id=$memberid","members/$title-$memberid")."\" title=\"$time\">";
  759. if ($color == 1)
  760. {
  761. $member_html .= "<span style=\"color: $member_color;\"><strong>$member_name</strong></span>";
  762. }
  763. else
  764. {
  765. $member_html .= "<strong>$member_name</strong>";
  766. }
  767. $member_html .= "</a>";
  768. /*
  769. Allow modules to display something after the name
  770. */
  771. if ($code = $Plugin->hook('member_link', 'after'))
  772. {
  773. eval($code);
  774. }
  775. $member_html .= "</span>";
  776. }
  777. else
  778. {
  779. $member_html = ' ';
  780. }
  781. return $member_html;
  782. }
  783. ///////////////////////////////////////////////////
  784. // .htaccess creator
  785. function htaccess_create($sef_urls = 1){
  786. global $db_prefix, $nova_domain;
  787. if ($sef_urls=='1'){
  788. $write="Options +FollowSymLinks
  789. RewriteEngine On
  790. ";
  791. $query21 = "select SEO_OFF, SEO_ON from {$db_prefix}seo ORDER BY SEO_ON desc" ;
  792. $result21 = mysql_query($query21) or die("settings.php - Error in query: $query21") ;
  793. while ($results21 = mysql_fetch_array($result21)){
  794. $seo_off = $results21['SEO_OFF'];
  795. $seo_on = $results21['SEO_ON'];
  796. $write.="RewriteRule ^$seo_on $seo_off\n";
  797. }
  798. // add support for hooks...
  799. template_hook("functions.template.php", "htaccess");
  800. $handling = fopen(".htaccess", "w");
  801. fwrite($handling, $write);
  802. fclose($handling);
  803. }
  804. }
  805. ///////////////////////////////////////////////////
  806. // Check Spam
  807. function checkspam($username, $email, $address){
  808. // prepare the checks...
  809. $username = strtolower($username);
  810. $email = strtolower($email);
  811. $address = strtolower($address);
  812. // first, check the email address....
  813. $spam_response = file_get_contents("http://www.stopforumspam.com/api?email=".$email."");
  814. $spam_check = strpos($spam_response, "yes");
  815. if($spam_check !== false){
  816. $spammer = "true";
  817. }
  818. // now check the ip address
  819. else{
  820. $spam_response = file_get_contents("http://www.stopforumspam.com/api?ip=".$address."");
  821. $spam_check = strpos($spam_response, "yes");
  822. if($spam_check !== false){
  823. if($address != "127.0.0.1"){
  824. $spammer = "true";
  825. }
  826. else{
  827. $spammer = "false";
  828. }
  829. }
  830. else{
  831. $spammer = "false";
  832. }
  833. }
  834. return $spammer;
  835. }
  836. ///////////////////////////////////////////////////
  837. // "CASPIAN" Trigger Envoke
  838. function caspian_trigger($id){
  839. global $db_prefix, $nova_domain, $my_id, $avoid_caspian, $akismet_key;
  840. // Your WordPress API key
  841. $GLOBALS["akismet_key"] = $akismet_key;
  842. // The name of the blog you're protecting
  843. $GLOBALS["akismet_home"] = $nova_domain;
  844. // Your User-Agent string
  845. $GLOBALS["akismet_ua"] = "NovaBoard/1.0";
  846. /**
  847. * Advanced settings below, only change these if you know what you're doing
  848. */
  849. // The Akismet hostname
  850. $GLOBALS["akismet_host"] = "rest.akismet.com";
  851. // Base URL to append to host and prepend to all queries
  852. $GLOBALS["akismet_url"] = "1.1";
  853. include "scripts/php/akismet.php";
  854. $never_spam="0"; // reset spam check
  855. if ($akismet_key!=''){
  856. // first, if the member is not a spammer, then
  857. // we can skip the rest...
  858. if ($avoid_caspian=='1'){ // can their group bypass caspian?
  859. $never_spam="1"; // if so, tell it.
  860. }
  861. if ($never_spam=='0'){
  862. if ($my_id > '0'){
  863. $query = "select NEVER_SPAM from {$db_prefix}members WHERE ID ='$my_id'" ;
  864. $result = mysql_query($query) or die("functions.php - Error in query: $query") ;
  865. $never_spam = mysql_result($result, 0);
  866. }
  867. else{
  868. $never_spam="0"; // we're not sure, so it's a potential spammer
  869. }
  870. }
  871. if ($never_spam=='0'){ // potential spammer
  872. // calculate the messages spam level...
  873. $query = "select CONTENT from {$db_prefix}posts WHERE ID ='$id'" ;
  874. $result = mysql_query($query) or die("functions.php - Error in query: $query") ;
  875. $comment = mysql_result($result, 0);
  876. $query = "select NAME from {$db_prefix}members WHERE ID ='$my_id'" ;
  877. $result = mysql_query($query) or die("functions.php - Error in query: $query") ;
  878. $name = mysql_result($result, 0);
  879. $query = "select EMAIL from {$db_prefix}members WHERE ID ='$my_id'" ;
  880. $result = mysql_query($query) or die("functions.php - Error in query: $query") ;
  881. $email = mysql_result($result, 0);
  882. // The array of data we need
  883. $vars = array();
  884. // Add the contents of the $_SERVER array, to help Akismet out
  885. foreach ( $_SERVER as $key => $val ) { $vars[ $key ] = $val; }
  886. // Mandatory fields of information
  887. $vars["user_ip"] = $_SERVER["REMOTE_ADDR"];
  888. $vars["user_agent"] = $_SERVER["HTTP_USER_AGENT"];
  889. // The body of the message to check, the name of the person who
  890. // posted it, and their email address
  891. $vars["comment_content"] = $comment;
  892. $vars["comment_author"] = $name;
  893. $vars["comment_author_email"] = $email;
  894. // ... Add more fields if you want
  895. // Check if it's spam
  896. if ( akismet_check( $vars ) ) {
  897. mysql_query("UPDATE {$db_prefix}posts SET approved='0' WHERE id='$id'"); // mod-queue the post
  898. mysql_query("UPDATE {$db_prefix}members SET moderate='1' WHERE id='$my_id'"); // mod-queue the member to prevent future posts
  899. $time=time();
  900. mysql_query("INSERT INTO {$db_prefix}moderate (postid, title, member_id, member_name, time) VALUES ('$id', 'Potential SPAM', '$my_id', 'CASPIAN', '$time')");
  901. }
  902. }
  903. }
  904. }
  905. ///////////////////////////////////////////////////
  906. // Topic Titles SEO
  907. function topic_title($topicid){
  908. global $db_prefix;
  909. $query219 = "select TITLE from {$db_prefix}posts WHERE TOPIC_ID='$topicid' AND TITLE!=''";
  910. $result219 = mysql_query($query219) or die("topic.php - Error in query: $query219") ;
  911. while ($results219 = mysql_fetch_array($result219)){
  912. $title = $results219['TITLE'];
  913. }
  914. // now fix the title
  915. $title = clean_url_seo($title);
  916. return $title;
  917. }
  918. ///////////////////////////////////////////////////
  919. // Category Names SEO
  920. function forum_title($forumid){
  921. global $db_prefix;
  922. $query219 = "select NAME AS TITLE from {$db_prefix}categories WHERE ID='$forumid'";
  923. $result219 = mysql_query($query219) or die("topic.php - Error in query: $query219") ;
  924. while ($results219 = mysql_fetch_array($result219)){
  925. $title = $results219['TITLE'];
  926. }
  927. // now fix the title
  928. $title = clean_url_seo($title);
  929. return $title;
  930. }
  931. // check if number is odd or even
  932. function checkNum($num){
  933. return ($num%2) ? TRUE : FALSE;
  934. }
  935. /*
  936. Adds/removes given language strings to the specified language file.
  937. @param string $file : The filename to add the strings to. e.g. 'lang_help'
  938. @param array $vars : An array of the language strings to add/delete
  939. @param bool $delete : Whether to add or remove the strings.
  940. @return null
  941. */
  942. function rebuild_lang($file, $vars, $delete = false)
  943. {
  944. global $nova_root;
  945. # The directory to search through
  946. $d = dir($nova_root . 'lang/');
  947. /*
  948. Loop through each entry found in this directory
  949. */
  950. while (($entry = $d->read()) !== false)
  951. {
  952. if ($entry != '.' && $entry != '..')
  953. {
  954. if (is_dir($nova_root . 'lang/' . $entry))
  955. {
  956. $langFile = $nova_root . 'lang/' . $entry . '/' . $file . '.php';
  957. /*
  958. Does the file we're looking for even exist?
  959. */
  960. if (file_exists($langFile))
  961. {
  962. require $langFile;
  963. if ($file == 'lang_forum')
  964. {
  965. $contents = $lang;
  966. }
  967. elseif ($file == 'lang_myoptions')
  968. {
  969. $contents = $lang_user;
  970. }
  971. else
  972. {
  973. $contents = $$file;
  974. }
  975. /*
  976. If deleting, remove the strings
  977. */
  978. if ($delete)
  979. {
  980. foreach ($vars as $key)
  981. {
  982. if (array_key_exists($key, $contents))
  983. {
  984. unset($contents[$key]);
  985. }
  986. }
  987. }
  988. /*
  989. Otherwise, add them...
  990. */
  991. else
  992. {
  993. foreach ($vars as $key => $value)
  994. {
  995. $contents[$key] = $value;
  996. }
  997. }
  998. /*
  999. Build the file contents
  1000. */
  1001. $save = '<?php' . "\n";
  1002. /*
  1003. Some files have different array names
  1004. */
  1005. if ($file == 'lang_forum')
  1006. {
  1007. $array_name = 'lang';
  1008. }
  1009. elseif ($file == 'lang_myoptions')
  1010. {
  1011. $array_name = 'lang_user';
  1012. }
  1013. else
  1014. {
  1015. $array_name = $file;
  1016. }
  1017. if (!empty($contents))
  1018. {
  1019. $save .= '$' . $array_name . ' = ' . var_export($contents, true) . ';';
  1020. }
  1021. else
  1022. {
  1023. $save .= '$' . $array_name . ' = array();';
  1024. }
  1025. $save .= "\n" . '?>';
  1026. /*
  1027. Save the contents
  1028. */
  1029. file_put_contents($langFile, $save);
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. // str_ireplace for PHP4
  1037. function stri_replace($find,$replace,$string)
  1038. {
  1039. if(!is_array($find))
  1040. $find = array($find);
  1041. if(!is_array($replace))
  1042. {
  1043. if(!is_array($find))
  1044. $replace = array($replace);
  1045. else
  1046. {
  1047. // this will duplicate the string into an array the size of $find
  1048. $c = count($find);
  1049. $rString = $replace;
  1050. unset($replace);
  1051. for ($i = 0; $i < $c; $i++)
  1052. {
  1053. $replace[$i] = $rString;
  1054. }
  1055. }
  1056. }
  1057. foreach($find as $fKey => $fItem)
  1058. {
  1059. $between = explode(strtolower($fItem),strtolower($string));
  1060. $pos = 0;
  1061. foreach($between as $bKey => $bItem)
  1062. {
  1063. $between[$bKey] = substr($string,$pos,strlen($bItem));
  1064. $pos += strlen($bItem) + strlen($fItem);
  1065. }
  1066. $string = implode($replace[$fKey],$between);
  1067. }
  1068. return($string);
  1069. }
  1070. function tokenCheck($name, $values = '')
  1071. {
  1072. $token_id = $_POST['token_id'];
  1073. $values = (is_array($values)) ? implode('', $values) : $values;
  1074. $token_name = 'token_' . $name . '_' . $values . $token_id;
  1075. if(isset($_POST[$token_name], $_SESSION[$token_name]) && $_POST[$token_name] == $_SESSION[$token_name])
  1076. {
  1077. return true;
  1078. }
  1079. else
  1080. {
  1081. return false;
  1082. }
  1083. }
  1084. function tokenCreate($name, $values = '')
  1085. {
  1086. $token_id = md5(microtime());
  1087. $token = md5(uniqid(mt_rand(), true));
  1088. $values = (is_array($values)) ? implode('', $values) : $values;
  1089. $token_name = 'token_' . $name . '_' . $values . $token_id;
  1090. $_SESSION[$token_name] = $token;
  1091. return array($token_id, $token, $token_name);
  1092. }
  1093. /*
  1094. Function: buildQuery
  1095. Takes parameters and formats then for use in an SQL query
  1096. @param array $fields : An array of field names.
  1097. @param array $tables : An array of table names.
  1098. @param array $where : An array of conditions for the query.
  1099. @param array $order : Sorting options.
  1100. @param array $limit : The number of records to limit.
  1101. @return array : Array of the formatted variables.
  1102. */
  1103. function buildQuery($fields, $tables, $where = array(), $order = array(), $limit = array(), $selectQuery = true)
  1104. {
  1105. $fields = implode(',', $fields);
  1106. $tables = buildTables($tables);
  1107. if (!empty($where))
  1108. {
  1109. $returnWhere = 'WHERE ';
  1110. $returnWhere .= implode(' AND ', $where);
  1111. }
  1112. $order = (empty($order)) ? '' : 'ORDER BY ' . implode(',', $order);
  1113. if (!empty($limit))
  1114. {
  1115. $limit = 'LIMIT ';
  1116. if ($limit['start'] != '')
  1117. {
  1118. $limit .= $limit['start'] . ',';
  1119. }
  1120. $limit .= $limit['num'];
  1121. }
  1122. else
  1123. {
  1124. $limit = '';
  1125. }
  1126. if ($selectQuery)
  1127. {
  1128. return 'SELECT ' . $fields . ' FROM ' . $tables . ' ' . $returnWhere . ' ' . $order . ' ' . $limit;
  1129. }
  1130. else
  1131. {
  1132. return array($fields, $tables, $returnWhere, $order, $limit);
  1133. }
  1134. }
  1135. /*
  1136. Function: buildTables
  1137. Takes an array of table data and formats them into an sql format.
  1138. @param array $tables : Table data.
  1139. @return string : SQL format.
  1140. */
  1141. function buildTables(&$tables)
  1142. {
  1143. global $db_prefix;
  1144. $return = '';
  1145. foreach ($tables as $key => $info)
  1146. {
  1147. if ($info['added'] != true)
  1148. {
  1149. $tables[$key]['added'] = true;
  1150. foreach ($info as $key2 => $info2)
  1151. {
  1152. if ($key2 !== 'added')
  1153. {
  1154. /*
  1155. Build the sql
  1156. */
  1157. $return .= $info2['join_type'] . ' ' . $db_prefix . $info2['name'] . ' ' . $info2['short'] . ' ';
  1158. if ($info2['on'] != '')
  1159. {
  1160. $return .= 'ON ' . $info2['on'] . ' ';
  1161. }
  1162. /*
  1163. Any tables wanting to join onto this table?
  1164. */
  1165. if (isset($tables[$info2['name']]))
  1166. {
  1167. $return .= buildTables($tables, $info2['name']);
  1168. }
  1169. }
  1170. }
  1171. }
  1172. }
  1173. return $return;
  1174. }
  1175. ?>