PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/classes/class_func.php

http://lansuite.googlecode.com/
PHP | 845 lines | 731 code | 50 blank | 64 comment | 44 complexity | 6b6cd6b9f284064898fc4a900f11c8a1 MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Global Functions
  4. *
  5. * @package lansuite_core
  6. * @author ls_admin
  7. * @version $Id: class_func.php 2359 2012-04-28 07:42:54Z Jochen.Jung $
  8. * @access public
  9. * @todo Remove Dialogfunctions and create own class
  10. */
  11. // Rewrite PHP's htmlspecialchars for ' is replaced by &#039;, instead of &#39;
  12. function htmlspecchars ($string, $quote_style=ENT_COMPAT, $format=null) {
  13. $aTransSpecchar = array('&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;');
  14. if (ENT_NOQUOTES == $quote_style) unset($aTransSpecchar['"']);
  15. elseif (ENT_QUOTES == $quote_style) $aTransSpecchar["'"] = '&#39;'; // (apos) htmlspecialchars() uses '&#039;'
  16. return strtr($string,$aTransSpecchar);
  17. }
  18. class func {
  19. var $ActiveModules = array();
  20. /**
  21. * CONSTRUCTOR : Get referer and transform in a internal Link
  22. *
  23. */
  24. function func() {
  25. define('NO_LINK', -1);
  26. $url_array = parse_url($_SERVER['HTTP_REFERER']);
  27. $this->internal_referer = "index.php?".$url_array['query'].$url_array['fragment'];
  28. }
  29. /**
  30. * Read the Config-settings from DB
  31. * @global mixed Databaseobject
  32. * @global array Baseconfig from File
  33. * @return $cfg
  34. */
  35. function read_db_config() {
  36. global $db;
  37. // Idea: Select current mod only, does not work with plugin.
  38. // $res = $db->qry('SELECT cfg_value, cfg_key, cfg_type FROM %prefix%config WHERE cfg_module = "install" OR cfg_module = %string%', $mod);
  39. $res = $db->qry('SELECT cfg_value, cfg_key, cfg_type FROM %prefix%config');
  40. while ($row = $db->fetch_array($res, 0)) {
  41. switch ($row['cfg_type']) {
  42. case 'integer':
  43. case 'int':
  44. $cfg["{$row['cfg_key']}"] = (int)$row['cfg_value'];
  45. break;
  46. case 'boolean':
  47. case 'bool':
  48. $cfg["{$row['cfg_key']}"] = (bool)$row['cfg_value'];
  49. break;
  50. case 'float':
  51. $cfg["{$row['cfg_key']}"] = (float)$row['cfg_value'];
  52. break;
  53. default:
  54. $cfg["{$row['cfg_key']}"] = $row['cfg_value'];
  55. break;
  56. }
  57. }
  58. $db->free_result($res);
  59. return $cfg;
  60. }
  61. #### Date Conversions ####
  62. /**
  63. * Convert a date string to a timestamp
  64. * @string strStr Date to convert
  65. * @optional string Format Format, the date is in
  66. * @return timestamp
  67. */
  68. function str2time($strStr, $strPattern = 'Y-m-d H:i:s') {
  69. // an array of the valide date characters, see: http://php.net/date#AEN21898
  70. $arrCharacters = array(
  71. 'd', // day
  72. 'm', // month
  73. 'y', // year, 2 digits
  74. 'Y', // year, 4 digits
  75. 'H', // hours
  76. 'i', // minutes
  77. 's' // seconds
  78. );
  79. // transform the characters array to a string
  80. $strCharacters = implode('', $arrCharacters);
  81. // splits up the pattern by the date characters to get an array of the delimiters between the date characters
  82. $arrDelimiters = preg_split('~['.$strCharacters.']~', $strPattern);
  83. // transform the delimiters array to a string
  84. $strDelimiters = quotemeta(implode('', array_unique($arrDelimiters)));
  85. // splits up the date by the delimiters to get an array of the declaration
  86. $arrStr = preg_split('~['.$strDelimiters.']~', $strStr);
  87. // splits up the pattern by the delimiters to get an array of the used characters
  88. $arrPattern = preg_split('~['.$strDelimiters.']~', $strPattern);
  89. // if the numbers of the two array are not the same, return false, because the cannot belong together
  90. if (count($arrStr) !== count($arrPattern)) return false;
  91. // creates a new array which has the keys from the $arrPattern array and the values from the $arrStr array
  92. $arrTime = array();
  93. for ($i = 0;$i < count($arrStr);$i++) $arrTime[$arrPattern[$i]] = $arrStr[$i];
  94. // gernerates a 4 digit year declaration of a 2 digit one by using the current year
  95. if (isset($arrTime['y']) && !isset($arrTime['Y'])) $arrTime['Y'] = substr(date('Y'), 0, 2) . $arrTime['y'];
  96. // if a declaration is empty, it will be filled with the current date declaration
  97. foreach ($arrCharacters as $strCharacter) if (empty($arrTime[$strCharacter])) $arrTime[$strCharacter] = date($strCharacter);
  98. // checks if the date is a valide date
  99. if (!checkdate($arrTime['m'], $arrTime['d'], $arrTime['Y'])) return false;
  100. $intTime = mktime($arrTime['H'], $arrTime['i'], $arrTime['s'], $arrTime['m'], $arrTime['d'], $arrTime['Y']);
  101. return $intTime;
  102. }
  103. /**
  104. * Convert a timestamp, to a nice, readable string
  105. * @string timestamp
  106. * @string type One of year, month, date, time, shorttime, datetime, daydatetime, daydate, or shortdaytime
  107. * @return string
  108. */
  109. function unixstamp2date($func_timestamp, $func_art) {
  110. if ((int)$func_timestamp == 0) return '---';
  111. else switch($func_art) {
  112. case "year": $func_date = date("Y", $func_timestamp); break;
  113. case "month": $func_date = date("Y", $func_timestamp) ." - ". t(date("F", $func_timestamp)); break;
  114. case "date": $func_date = date("d.m.Y", $func_timestamp); break;
  115. case "time": $func_date = date("H:i", $func_timestamp); break;
  116. case "shorttime": $func_date = date("H:i", $func_timestamp); break;
  117. case "datetime": $func_date = date("d.m.Y H:i", $func_timestamp); break;
  118. case "daydatetime":
  119. $day[0] = t('Sonntag');
  120. $day[1] = t('Montag');
  121. $day[2] = t('Dienstag');
  122. $day[3] = t('Mittwoch');
  123. $day[4] = t('Donnerstag');
  124. $day[5] = t('Freitag');
  125. $day[6] = t('Samstag');
  126. $func_date .= $day[date("w", $func_timestamp)];
  127. $func_date .= ", ";
  128. $func_date .= date("d.m.Y H:i", $func_timestamp);
  129. break;
  130. case "daydate":
  131. $day[0] = t('Sonntag');
  132. $day[1] = t('Montag');
  133. $day[2] = t('Dienstag');
  134. $day[3] = t('Mittwoch');
  135. $day[4] = t('Donnerstag');
  136. $day[5] = t('Freitag');
  137. $day[6] = t('Samstag');
  138. $func_date .= date("d.m.Y", $func_timestamp) . " (". $day[date("w", $func_timestamp)] .")";
  139. break;
  140. case "shortdaytime":
  141. $day[0] = t('So');
  142. $day[1] = t('Mo');
  143. $day[2] = t('Di');
  144. $day[3] = t('Mi');
  145. $day[4] = t('Do');
  146. $day[5] = t('Fr');
  147. $day[6] = t('Sa');
  148. $func_date .= $day[date("w", $func_timestamp)];
  149. $func_date .= ", ";
  150. $func_date .= date("H:i", $func_timestamp);
  151. break;
  152. }
  153. return $func_date;
  154. }
  155. /**
  156. * Calculates the age to a given birthday timestamp
  157. * @timestamp birthhday
  158. * @optional timestamp attime Calculates the age the person will have at this time
  159. * @return int age
  160. */
  161. function age($birthday, $attime = 0) {
  162. if ($attime) {
  163. $yeardiff = date("Y", $attime) - date("Y", $birthday);
  164. $monthdiff = date("m", $attime) - date("m", $birthday);
  165. $daydiff = date("j", $attime) - date("j", $birthday);
  166. } else {
  167. $yeardiff = date("Y") - date("Y", $birthday);
  168. $monthdiff = date("m") - date("m", $birthday);
  169. $daydiff = date("j") - date("j", $birthday);
  170. }
  171. if (($monthdiff < 0) || ($monthdiff == 0 and $daydiff < 0)) $age = $yeardiff - 1;
  172. else $age = $yeardiff;
  173. return (int)$age;
  174. }
  175. #### Infobox ####
  176. function setainfo( $text, $userid, $priority, $item, $itemid) {
  177. global $db;
  178. if ($priority != "0" AND $priority != "1" AND $priority != "2") {
  179. echo(t('Function setainfo needs Priority defined as Integer: 0 low (grey), 1 middle (green), 2 high (orange)'));
  180. } else {
  181. $date = date("U");
  182. $db->qry("INSERT INTO %prefix%infobox SET userid=%int%, class=%string%, id_in_class = %int%, text=%string%, date=%string%, priority=%string%", $userid, $item, $itemid, $text, $date, $priority);
  183. }
  184. }
  185. #### Dialog functions ####
  186. function GeneralDialog($type, $text, $link_target = '', $JustReturn = 0, $link_type = '') {
  187. global $smarty, $dsp, $FrameworkMessages;
  188. // Link
  189. if ($link_target == NO_LINK) $smarty->assign('link', '');
  190. else {
  191. switch($link_type) {
  192. case "FORWARD":
  193. $link_text = t('Weiter');
  194. $link_description = t('Weiter zur naechsten Seite');
  195. break;
  196. default: // i.e. BACK
  197. $link_text = t('Zurόck');
  198. $link_description = t('Zurόck zur vorherigen Seite');
  199. break;
  200. }
  201. if (!$link_target) $link_target = $this->internal_referer;
  202. $smarty->assign('link', $dsp->FetchCssButton($link_text, $link_target, $link_description));
  203. }
  204. // Text
  205. switch($text) {
  206. case "ACCESS_DENIED": $text = t('Du hast keine Zugriffsrechte fόr diesen Bereich.'); break;
  207. case "NO_LOGIN": $text = t('Du bist nicht eingeloggt. Bitte logge dich erst ein, bevor du diesen Bereich betritst.'); break;
  208. case "NO_REFRESH": $text = t('Du hast diese Anfrage wiederholt ausgefόhrt.'); break;
  209. }
  210. $smarty->assign('msg', $text);
  211. if ($JustReturn) $FrameworkMessages .= $smarty->fetch('design/templates/'. $type .'.htm');
  212. else $dsp->AddContentLine($smarty->fetch('design/templates/'. $type .'.htm'));
  213. }
  214. function confirmation($text, $link_target = '', $JustReturn = 0, $link_type = '') {
  215. return $this->GeneralDialog('confirmation', $text, $link_target, $JustReturn, $link_type);
  216. }
  217. function information($text, $link_target = '', $JustReturn = 0, $link_type = '') {
  218. return $this->GeneralDialog('information', $text, $link_target, $JustReturn, $link_type);
  219. }
  220. function error($text, $link_target = '', $JustReturn = 0, $link_type = '') {
  221. $this->log_event('LS Error: '. $text, 3, 'LS-Fehler');
  222. return $this->GeneralDialog('error', $text, $link_target, $JustReturn, $link_type);
  223. }
  224. function question($text, $link_target_yes, $link_target_no = '') {
  225. global $smarty, $dsp;
  226. if ($link_target_no == '') $link_target_no = $this->internal_referer;
  227. $smarty->assign('question', $text);
  228. $smarty->assign('action', $link_target_yes);
  229. $smarty->assign('yes', $dsp->FetchIcon($link_target_yes, 'yes'));
  230. $smarty->assign('no', $dsp->FetchIcon($link_target_no, 'no'));
  231. $dsp->AddContentLine($smarty->fetch('design/templates/question.htm'));
  232. }
  233. function multiquestion($questionarray, $linkarray, $text = '') {
  234. global $smarty, $dsp;
  235. if ($text == '') $text = t('Bitte wδhle eine Mφglichkeit aus:');
  236. $smarty->assign('msg', $text);
  237. if (is_array($questionarray)) foreach($questionarray as $ind => $question)
  238. $row .= '<br /><br /><a href="'. $linkarray[$ind] .'">'. $question .'</a>';
  239. $smarty->assign('row', $row);
  240. $dsp->AddContentLine($smarty->fetch("design/templates/multiquestion.htm"));
  241. }
  242. #### String conversion (Anti-Hacking + Nicer look) ####
  243. // NoHTML is applied to every field retrieved from an SQL-Query, as well as GET, Request_Uri, Http_Referrer and Query_String
  244. // If you would like to use HTML code in one of these, you have to transform the code again, using AllowHTML
  245. function NoHTML($string, $soft = 0) {
  246. if ($soft) $aTransSpecchar = array('"' => '&quot;', '<' => '&lt;', '>' => '&gt;');
  247. else $aTransSpecchar = array('&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;');
  248. return strtr($string, $aTransSpecchar);
  249. }
  250. // See above
  251. function AllowHTML($string) {
  252. $aTransSpecchar = array('&amp;' => '&', '&quot;' => '"', '&lt;' => '<', '&gt;' => '>');
  253. return strtr($string, $aTransSpecchar);
  254. }
  255. // Add slashes at any non GPC-variable
  256. // This function musst be used, if ' come from other sources, than $_GET, or $_POST
  257. // for example language-files
  258. function escape_sql($text) {
  259. $text = addslashes(stripslashes($text));
  260. return $text;
  261. }
  262. // If ls-code should be displayed
  263. function text2html($string, $mode = 0) { // mode 0: default; 1: wiki before; 2: wiki after; 4: basic
  264. global $db, $auth;
  265. if ($mode != 4) {
  266. if ($mode != 1) {
  267. preg_replace_callback(
  268. '#\[c\]((.)*)\[\/c\]#sUi',
  269. create_function(
  270. '$treffer',
  271. 'global $HighlightCode, $HighlightCount; $HighlightCount++; $HighlightCode[$HighlightCount] = $treffer[1];'
  272. ),
  273. $string
  274. );
  275. }
  276. if ($mode != 2) {
  277. $img_start = "<img src=\"design/".$auth["design"]."/images/";
  278. $img_start2 = '<img src="ext_inc/smilies/';
  279. $img_end = '" border="0" alt="" />';
  280. $string = preg_replace('#\\[img\\]([^[]*)\\[/img\\]#sUi', '<img src="\1" border="0" class="img" alt="" style="max-width:468px; max-height:450px; overflow:hidden;" />', $string);
  281. $string = preg_replace('#\\[url=(index\.php\?[^\\]]*)\\]([^[]*)\\[/url\\]#sUi', '<a href="\\1" rel="nofollow">\\2</a>', $string);
  282. $string = preg_replace('#\\[url=([^\\]]*)\\]([^[]*)\\[/url\\]#sUi', '<a target="_blank" href="\\1" rel="nofollow">\\2</a>', $string);
  283. if ($mode != 1) {
  284. $string = preg_replace('#(\\s|^)([a-zA-Z]+://(.)*)(\\s|$)#sUi', '\\1<a target="_blank" href="\\2" rel="nofollow">\\2</a>\\4', $string);
  285. $string = preg_replace('#(\\s|^)(mailto:(.)*)(\\s|$)#sUi', '\\1<a target="_blank" href="\\2">\\3</a>\\4', $string);
  286. $string = preg_replace('#(\\s|^)(www\\.(.)*)(\\s|$)#sUi', '\\1<a target="_blank" href="http://\\2" rel="nofollow">\\2</a>\\4', $string);
  287. }
  288. }
  289. }
  290. if ($mode != 2) {
  291. $string = str_replace("\r", '', $string);
  292. $string = str_replace("\n", "<br />\n", $string);
  293. $string = str_replace("[br]", "<br />\n", $string);
  294. $string = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $string);
  295. $string = preg_replace('#\[b\](.*)\[/b\]#sUi', '<b>\\1</b>', $string);
  296. $string = preg_replace('#\[i\](.*)\[/i\]#sUi', '<i>\\1</i>', $string);
  297. $string = preg_replace('#\[u\](.*)\[/u\]#sUi', '<u>\\1</u>', $string);
  298. $string = preg_replace('#\[s\](.*)\[/s\]#sUi', '<s>\\1</s>', $string);
  299. $string = preg_replace('#\[sub\](.*)\[/sub\]#sUi', '<sub>\\1</sub>', $string);
  300. $string = preg_replace('#\[sup\](.*)\[/sup\]#sUi', '<sup>\\1</sup>', $string);
  301. }
  302. if ($mode != 4) {
  303. if ($mode != 2) {
  304. $string = preg_replace('#\[quote\](.*)\[/quote\]#sUi', '<blockquote><div class="tbl_small">Zitat:</div><div class="tbl_7">\\1</div></blockquote>', $string);
  305. $string = preg_replace('#\[size=([0-9]+)\]#sUi', '<font style="font-size:\1px">', $string);
  306. $string = str_replace('[/size]', '</font>', $string);
  307. $string = preg_replace('#\[color=([a-z]+)\]#sUi', '<font color="\1">', $string);
  308. $string = str_replace('[/color]', '</font>', $string);
  309. }
  310. if ($mode != 1) {
  311. $string = preg_replace_callback(
  312. '#\[c\](.)*\[\/c\]#sUi',
  313. create_function(
  314. '$treffer',
  315. 'global $func, $HighlightCode, $HighlightCount2; $HighlightCount2++; include_once(\'ext_scripts/geshi/geshi.php\'); return \'<blockquote><div class="tbl_small">Code:</div><div class="tbl_7">\'. $func->AllowHTML(geshi_highlight($HighlightCode[$HighlightCount2], \'php\', \'ext_scripts/geshi/geshi\', true)) .\'</div></blockquote>\';'
  316. ),
  317. $string
  318. );
  319. }
  320. if ($mode != 1) {
  321. $res = $db->qry("SELECT shortcut, image FROM %prefix%smilies");
  322. while ($row = $db->fetch_array($res)) $string = str_replace($row['shortcut'], $img_start2 . $row['image'] . $img_end, $string);
  323. $db->free_result($res);
  324. }
  325. }
  326. return $string;
  327. }
  328. // Wiki Syntax
  329. function Text2Wiki($string) {
  330. $arr = explode("\n", $this->Text2HTML($string, 1));
  331. $COpen = 0;
  332. $UlOpen = 0;
  333. $OlOpen = 0;
  334. foreach ($arr as $key => $line) {
  335. #$arr[$key] = preg_replace("#^<br />$#sUi", '', $arr[$key]);
  336. $arr[$key] = preg_replace('#^====== (.*) ======#sUi', '<div class="wikiH6">\\1</div>', $arr[$key]);
  337. $arr[$key] = preg_replace('#^===== (.*) =====#sUi', '<div class="wikiH5">\\1</div>', $arr[$key]);
  338. $arr[$key] = preg_replace('#^==== (.*) ====#sUi', '<div class="wikiH4">\\1</div>', $arr[$key]);
  339. $arr[$key] = preg_replace('#^=== (.*) ===#sUi', '<div class="wikiH3">\\1</div>', $arr[$key]);
  340. $arr[$key] = preg_replace('#^== (.*) ==#sUi', '<div class="wikiH2">\\1</div>', $arr[$key]);
  341. $arr[$key] = preg_replace('#^= (.*) =#sUi', '<div class="wikiH1">\\1</div>', $arr[$key]);
  342. $arr[$key] = preg_replace('#\\[\\[Bild:(.*)\\]\\]#sUi', '<img src="ext_inc/wiki/\\1" alt="\\1">', $arr[$key]);
  343. $arr[$key] = preg_replace('#\\[(http://[^ ]*) ([^\\]]*)\\]#sUi', '<a target="_blank" href="\\1" rel="nofollow">\\2</a>', $arr[$key]);
  344. $arr[$key] = preg_replace('#\\[\\[([^\\|\\]]*)\\]\\]#sUi', '<a href="index.php?mod=wiki&action=show&name=\\1">\\1</a>', $arr[$key]);
  345. $arr[$key] = preg_replace('#\\[\\[([^\\|]*)\\|([^\\]]*)\\]\\]#sUi', '<a href="index.php?mod=wiki&action=show&name=\\1">\\2</a>', $arr[$key]);
  346. $arr[$key] = preg_replace("#'''(.*)'''#sUi", "<b>\\1</b>", $arr[$key]);
  347. if ($UlOpen) {
  348. $arr[$key] = preg_replace("#^\\* (.*)<br />#sUi", "<li>\\1</li>", $arr[$key]);
  349. $arr[$key] = preg_replace("#^([^\\*].(.*))<br />#sUi", "</ul>\\1", $arr[$key], -1, $count);
  350. if ($count) $UlOpen = 0;
  351. } else {
  352. $arr[$key] = preg_replace("#^\\* (.*)<br />#sUi", "<ul><li>\\1</li>", $arr[$key], -1, $count);
  353. if ($count) $UlOpen = 1;
  354. }
  355. if ($OlOpen) {
  356. $arr[$key] = preg_replace("|^\\# (.*)<br />|sUi", "<li>\\1</li>", $arr[$key]);
  357. $arr[$key] = preg_replace("|^([^\\#].(.*))<br />|sUi", "</ol>\\1", $arr[$key], -1, $count);
  358. if ($count) $OlOpen = 0;
  359. } else {
  360. $arr[$key] = preg_replace("|^\\# (.*)<br />|sUi", "<ol><li>\\1</li>", $arr[$key], -1, $count);
  361. if ($count) $OlOpen = 1;
  362. }
  363. if ($COpen) {
  364. $arr[$key] = preg_replace("#^([^ ].)#sUi", "[/c]\\1", $arr[$key], -1, $count);
  365. if ($count) $COpen = 0;
  366. $arr[$key] = preg_replace("#^ #sUi", "", $arr[$key]);
  367. $arr[$key] = preg_replace("#<br />$#sUi", "", $arr[$key]);
  368. } else {
  369. $arr[$key] = preg_replace("#^ #sUi", "[c]", $arr[$key], -1, $count);
  370. if ($count) {
  371. $COpen = 1;
  372. $arr[$key] = preg_replace("#<br />$#sUi", "", $arr[$key]);
  373. }
  374. }
  375. }
  376. $string = implode("\n", $arr);
  377. if ($UlOpen) $string .= '</ul>';
  378. if ($OlOpen) $string .= '</ol>';
  379. if ($COpen) $string .= '[/c]';
  380. return $this->Text2HTML($string, 2);
  381. }
  382. function Entity2Uml($string) {
  383. $string = str_replace('&uuml;', 'ό', $string);
  384. $string = str_replace('&Uuml;', 'ά', $string);
  385. $string = str_replace('&auml;', 'δ', $string);
  386. $string = str_replace('&Auml;', 'Δ', $string);
  387. $string = str_replace('&ouml;', 'φ', $string);
  388. $string = str_replace('&Ouml;', 'Φ', $string);
  389. $string = str_replace('&szlig;', 'ί', $string);
  390. $string = str_replace('&nbsp;', '', $string);
  391. $string = str_replace('&quot;', '"', $string);
  392. return $string;
  393. }
  394. #### Check Var Content ####
  395. function check_var($var, $type, $min_length, $max_length) {
  396. if (($type == "integer" OR $type == "double" OR $type == "string" OR $type == "boolean" OR $type == "object" OR $type == "array") AND (isset($min_length) == FALSE OR gettype($min_length) == "integer") AND (isset($max_length) == FALSE OR gettype($max_length) == "integer") AND (isset($var) == TRUE))
  397. {
  398. if((gettype($var) == $type) AND (strlen($var) >= $min_length) AND (strlen($var) <= $max_length)) return TRUE;
  399. else return FALSE;
  400. } else echo "Error: Function check_var needs defined: var, datatype (may be integer, double, string, boolean, object or array), [optionally: min_length], [optionally: max_length] <br/> For more information please visit the lansuite programmers docu";
  401. }
  402. function checkIP($ip) {
  403. if (strlen($ip) < 5 OR strlen($ip) > 15) return 0;
  404. $IPParts = explode(".", $ip);
  405. if (count($IPParts) != 4) return 0;
  406. if ($IPParts[0] == 0 ) return 0;
  407. for ($i=0; $i<=3; $i++) {
  408. if (ereg("[^0-9]", $IPParts[$i])) return 0;
  409. if ($IPParts[$i] > 255 or $IPParts[$i] < 0) return 0;
  410. }
  411. return 1;
  412. }
  413. #### Misc ####
  414. // Types: 1 = Info, 2 = Warning, 3 = Error (be careful with 3)
  415. function log_event($message, $type = 1, $sort_tag = '', $target_id = '') {
  416. global $db, $auth;
  417. if ($message == '') echo("Function log_event needs message defined! - Invalid arguments supplied!");
  418. else {
  419. if ($sort_tag == '') $sort_tag = $_GET['mod'];
  420. $entry = $db->qry("INSERT INTO %prefix%log SET
  421. userid = %int%,
  422. description=%string%,
  423. type=%string%,
  424. sort_tag = %string%,
  425. target_id = %int%,
  426. script = %string%,
  427. referer = %string%,
  428. ip = INET_ATON(%string%)
  429. ", $auth['userid'], $message, $type, $sort_tag, $target_id, $_SERVER["REQUEST_URI"], $this->internal_referer, $_SERVER['REMOTE_ADDR']);
  430. if ($entry == 1) return 1;
  431. }
  432. return 0;
  433. }
  434. // Better use MasterSearch..
  435. function page_split($current_page, $max_entries_per_page, $overall_entries, $working_link, $var_page_name) {
  436. if ($max_entries_per_page > 0 and $overall_entries >= 0 and $working_link != "" and $var_page_name != "") {
  437. if($current_page == "") {
  438. $page_sql = "LIMIT 0," . $max_entries_per_page;
  439. $page_a = 0;
  440. $page_b = $max_entries_per_page;
  441. }
  442. if($current_page == "all") {
  443. $page_sql = "";
  444. $page_a = 0;
  445. $page_b = $overall_entries;
  446. } else {
  447. $page_sql = ("LIMIT " . ($current_page * $max_entries_per_page) . ", " . ($max_entries_per_page));
  448. $page_a = ($current_page * $max_entries_per_page);
  449. $page_b = ($max_entries_per_page);
  450. }
  451. if($overall_entries > $max_entries_per_page) {
  452. $page_output = ("Seiten: ");
  453. if( $current_page != "all" && ($current_page + 1) > 1 ) {
  454. $page_output .= ("&nbsp; " . "<a class=\"menu\" href=\"" . $working_link . "&" . $var_page_name . "=" . ($current_page - 1) . "&orderby=" . $orderby . "\">" ."<b>" . "<" . "</b>" . "</a>");
  455. }
  456. $i = 0;
  457. while($i < ($overall_entries / $max_entries_per_page)) {
  458. if($current_page == $i && $current_page != "all") {
  459. $page_output .= (" " . ($i + 1));
  460. } else {
  461. $page_output .= ("&nbsp; " . "<a class=\"menu\" href=\"" . $working_link . "&" . $var_page_name . "=" . $i . "\">" ."<b>" . ($i + 1) . "</b>" . "</a>");
  462. }
  463. $i++;
  464. }
  465. if($current_page != "all" && ($current_page + 1) < ($overall_entries/$max_entries_per_page)) {
  466. $page_output .= ("&nbsp; " . "<a class=\"menu\" href=\"" . $working_link ."&" . $var_page_name . "=" . ($current_page + 1) . "\">" ."<b>" . ">" . "</b>" . "</a>");
  467. }
  468. if($current_page != "all") {
  469. $page_output .= ("&nbsp; " . "<a class=\"menu\" href=\"" . $working_link ."&" . $var_page_name . "=all" . "\">" ."<b>" . "Alle" . "</b>" . "</a>");
  470. }
  471. if ($current_page == "all") {
  472. $page_output .= " Alle";
  473. }
  474. }
  475. $output["html"] = $page_output;
  476. $output["sql"] = $page_sql;
  477. $output["a"] = $page_a;
  478. $output["b"] = $page_b;
  479. return($output);
  480. // ?!?! unset($output); unset($working_link); unset($page_sql); unset($page_output);
  481. } else echo ("Error: Function page_split needs defined: current_page, max_entries_per_page,working_link, page_varname For more information please visit the lansuite programmers docu");
  482. }
  483. function FileUpload($source_var, $path, $name = NULL) {
  484. global $config;
  485. switch ($_FILES[$source_var]['error']) {
  486. case 1:
  487. echo "Fehler: Die hochgeladene Datei όberschreitet die in der Anweisung upload_max_filesize in php.ini festgelegte Grφίe";
  488. return 0;
  489. break;
  490. case 2:
  491. echo "Fehler: Die hochgeladene Datei όberschreitet die in dem HTML Formular mittels der Anweisung MAX_FILE_SIZE angegebene maximale Dateigrφίe";
  492. return 0;
  493. break;
  494. case 3:
  495. echo "Fehler: Die Datei wurde nur teilweise hochgeladen";
  496. return 0;
  497. break;
  498. case 4:
  499. #echo "Fehler: Es wurde keine Datei hochgeladen";
  500. return 0;
  501. break;
  502. default:
  503. if ($_FILES[$source_var]['tmp_name'] == '') return false;
  504. if (strrpos($path, '/') + 1 != strlen($path)) $path .= "/";
  505. if (!file_exists($path)) mkdir($path);
  506. if ($name) {
  507. // Auto-Add File-Extension
  508. if (!strpos($name, ".")) $name .= substr($_FILES[$source_var]['name'], strrpos($_FILES[$source_var]['name'], "."), 5);
  509. $target = $path . $name;
  510. } else $target = $path . $_FILES[$source_var]['name'];
  511. // Change .php to .php.txt
  512. switch (substr($target, strrpos($target, "."), strlen($target))) {
  513. // Script extentions
  514. case '.php':
  515. case '.php2':
  516. case '.php3':
  517. case '.php4':
  518. case '.php5':
  519. case '.phtml':
  520. case '.pwml':
  521. case '.inc':
  522. case '.asp':
  523. case '.aspx':
  524. case '.ascx':
  525. case '.jsp':
  526. case '.cfm':
  527. case '.cfc':
  528. case '.pl':
  529. case '.bat':
  530. case '.vbs':
  531. case '.reg':
  532. case '.cgi':
  533. case '.shtml':
  534. // Harmless extentions, but better to view with .txt
  535. case '.html':
  536. case '.htm':
  537. case '.js':
  538. case '.css':
  539. $target .= '.txt';
  540. break;
  541. }
  542. $i = '';
  543. do {
  544. $targetStart = substr($target, 0, strrpos($target, "."));
  545. $targetEnd = substr($target, strrpos($target, "."), strlen($target));
  546. $targetUniq = $targetStart . $i . $targetEnd;
  547. $i++;
  548. } while (file_exists($targetUniq));
  549. if (move_uploaded_file($_FILES[$source_var]['tmp_name'], $targetUniq)) {
  550. chmod ($targetUniq, octdec($config["lansuite"]["chmod_file"]));
  551. return $targetUniq;
  552. } else {
  553. echo "Fehler: Datei konnte nicht hochgeladen werden." . HTML_NEWLINE;
  554. print_r($_FILES);
  555. return 0;
  556. }
  557. break;
  558. }
  559. }
  560. function CreateDir($dir) {
  561. global $config;
  562. if (!is_dir($dir)) {
  563. mkdir($dir, octdec($config["lansuite"]["chmod_dir"]));
  564. #chmod($dir, octdec($config["lansuite"]["chmod_dir"]));
  565. }
  566. }
  567. function ping($host, $timeout = 200000){
  568. // Φffne Socket zum Server
  569. $handle=fsockopen('udp://'.$host, 7, $errno, $errstr);
  570. if (!$handle){
  571. return false;
  572. }else{
  573. //Set read timeout
  574. socket_set_timeout($handle, 0 , $timeout);
  575. //Time the responce
  576. list($usec, $sec) = explode(" ", microtime(true));
  577. $start=(float)$usec + (float)$sec;
  578. //send somthing
  579. $write=fwrite($handle,"echo this\n");
  580. if(!$write){
  581. fclose($handle);
  582. return false;
  583. }
  584. //Try to read. the server will most likely respond with a "ICMP Destination Unreachable" and end the read. But that is a responce!
  585. fread($handle,1024);
  586. //Work out if we got a responce and time it
  587. list($usec, $sec) = explode(" ", microtime(true));
  588. $laptime=((float)$usec + (float)$sec)-$start;
  589. if(($laptime*1000000)>($timeout*0.9)){
  590. fclose($handle);
  591. return false;
  592. }else{
  593. fclose($handle);
  594. return true;
  595. }
  596. }
  597. }
  598. function FormatFileSize($size){
  599. $i = 0;
  600. $iec = array("Byte", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  601. while (($size / 1024) > 1) {
  602. $size = $size / 1024;
  603. $i++;
  604. }
  605. return round($size, 2) .' '. $iec[$i];
  606. }
  607. function GetDirList($dir) {
  608. if (!is_dir($dir)) return false;
  609. $ret = array();
  610. $handle = opendir($dir);
  611. while ($file = readdir ($handle)) {
  612. if ((substr($file, 0, 1) != '.') and ($file != '.svn')) $ret[] = strtolower($file);
  613. }
  614. closedir($handle);
  615. sort($ret);
  616. return $ret;
  617. }
  618. /**
  619. * func::chk_img_path() Check for a valid Picturepath
  620. *
  621. * @param mixed Path to test for validity
  622. * @return boolean Path OK an Picture exists
  623. * @static
  624. */
  625. function chk_img_path($imgpath) {
  626. if ($imgpath != '' and $imgpath != 'none' and $imgpath != '0') {
  627. if (is_file($imgpath)) {
  628. return 1;
  629. } else {
  630. return 0;
  631. }
  632. }
  633. }
  634. /**
  635. * Read DB and shows if a Superadmin exists
  636. *
  637. * @return boolean
  638. * @static
  639. */
  640. function admin_exists(){
  641. global $db;
  642. if (is_object($db) AND $db->success==1) {
  643. $res = $db->qry("SELECT userid FROM %prefix%user WHERE type = 3 LIMIT 1");
  644. if ($db->num_rows($res) > 0) $found = 1; else $found = 0;
  645. $db->free_result($res);
  646. return $found;
  647. } else return 0;
  648. }
  649. function CutString($str, $SoftLimit, $HardLimit = false) {
  650. if ($HardLimit === false) $HardLimit = $SoftLimit + 6;
  651. if ($HardLimit and strlen($str) > $HardLimit) return substr($str, 0, $HardLimit - 2) . '...';
  652. elseif (strlen($str) > $SoftLimit) {
  653. preg_match('/[^a-zA-Z0-9]/', substr($str, $SoftLimit, strlen($str)), $ret, PREG_OFFSET_CAPTURE);
  654. return substr($str, 0, $SoftLimit + $ret[0][1]) . '...';
  655. } else return $str;
  656. }
  657. function CheckNewPosts($last_change, $table, $entryid, $userid = 0) {
  658. global $db, $auth;
  659. // Older, than one week
  660. if ($last_change < (time() - 60 * 60 * 24 * 7)) return 0;
  661. // If logged out, everyting in the last week is considered new
  662. if (!$userid) $userid = $auth['userid'];
  663. if (!$userid) return 1;
  664. // If logged in
  665. else {
  666. $last_read = $db->qry_first('SELECT UNIX_TIMESTAMP(date) AS date FROM %prefix%lastread
  667. WHERE userid = %int% AND tab = %string% AND entryid = %int%', $userid, $table, $entryid);
  668. // Older, than one week
  669. if ($last_change < (time() - 60 * 60 * 24 * 7)) return 0;
  670. // No entry -> Thread completely new
  671. elseif (!$last_read['date']) return 1;
  672. // Entry exists
  673. else {
  674. // The posts date is newer than the mark -> New
  675. if ($last_read['date'] < $last_change) return 1;
  676. // The posts date is older than the mark -> Old
  677. else return 0;
  678. }
  679. }
  680. }
  681. function SetRead($table, $entryid, $userid = 0) {
  682. global $db, $auth;
  683. if (!$userid) $userid = $auth['userid'];
  684. $search_read = $db->qry_first("SELECT 1 AS found FROM %prefix%lastread WHERE tab = %string% AND entryid = %int% AND userid = %int%", $table, $entryid, $userid);
  685. if ($search_read["found"]) $db->qry_first("UPDATE %prefix%lastread SET date = NOW() WHERE tab = %string% AND entryid = %int% AND userid = %int%", $table, $entryid, $userid);
  686. else $db->qry_first("INSERT INTO %prefix%lastread SET date = NOW(), tab = %string%, entryid = %int%, userid = %int%", $table, $entryid, $userid);
  687. }
  688. function CreateSignonBar($guests, $paid_guests, $max_guests) {
  689. $max_bars = 100;
  690. // Angemeldet lδnge ausrechnen.
  691. if ($max_guests * $guests) $curuser = round($max_bars / $max_guests * $guests);
  692. if ($curuser > $max_bars) $curuser = $max_bars;
  693. // Bezahlt lδnge ausrechnen.
  694. if ($max_guests * $paid_guests) $gesamtpaid = round($max_bars / $max_guests * $paid_guests);
  695. if ($gesamtpaid > $max_bars) $gesamtpaid = $max_bars;
  696. // Wirkliche Bildanzahl ausrechenn
  697. $pixelges = $max_bars - $curuser;
  698. $pixelcuruser = $curuser - $gesamtpaid;
  699. $pixelpaid = $gesamtpaid;
  700. // Bar erzeugen
  701. if ($pixelpaid > 0) $bar = '<ul class="BarOccupied infolink" style="width:'. $pixelpaid .'px;">&nbsp;<span class="infobox">'. t('Angemeldet und Bezahlt') .': '. $paid_guests .'</span></ul>';
  702. if ($pixelcuruser > 0) $bar .= '<ul class="BarMarked infolink" style="width:'. $pixelcuruser .'px;">&nbsp;<span class="infobox">'. t('Nur Angemeldet') .': '. ($guests - $paid_guests) .'</span></ul>';
  703. if ($pixelges > 0) $bar .= '<ul class="BarFree infolink" style="width:'. $pixelges .'px;">&nbsp;<span class="infobox">'. t('Frei') .': '. ($max_guests - $paid_guests) .'</span></ul>';
  704. $bar .= '<ul class="BarClear">&nbsp;</ul>';
  705. return $bar;
  706. }
  707. function getActiveModules() {
  708. global $db;
  709. $this->ActiveModules = array();
  710. $res = $db->qry('SELECT name, caption FROM %prefix%modules WHERE active = 1');
  711. while($row = $db->fetch_array($res)) $this->ActiveModules[$row['name']] = $row['caption'];
  712. $db->free_result($res);
  713. $this->ActiveModules['helplet'] = 'Helplets';
  714. $this->ActiveModules['popups'] = 'Popups';
  715. $this->ActiveModules['auth'] = 'Auth';
  716. }
  717. function isModActive($mod, &$caption = '') {
  718. $caption = $this->ActiveModules[$mod];
  719. return array_key_exists($mod, $this->ActiveModules);
  720. }
  721. }
  722. ?>