PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/functions.php

http://section-cms.googlecode.com/
PHP | 292 lines | 228 code | 51 blank | 13 comment | 43 complexity | 5fc179619c251637af82d051a634374b MD5 | raw file
  1. <?php
  2. // Buffer Function
  3. function buffer($f)
  4. {
  5. global $mod, $table, $sid, $id, $sPage, $sId, $block, $_GET, $_POST, $_COOKIE, $_FILES, $_SESSION;
  6. ob_start(); # Start Buffer
  7. include($f); # Include File
  8. $obj = ob_get_contents(); # Store Contents
  9. ob_end_clean(); # Clear Buffer
  10. return $obj; # Return Contents
  11. }
  12. function block($title, $content, $block, $get_id='', $details='')
  13. {
  14. $getBlock = buffer('./blocks/'.$block.'.tpl');
  15. if(is_array($content))
  16. {
  17. $setContent = $content[$aId];
  18. }
  19. else
  20. {
  21. $setContent = $content;
  22. }
  23. $replaceContent = array(
  24. 'id' => $get_id,
  25. 'title' => $title,
  26. 'content' => $content,
  27. 'details' => $details,
  28. );
  29. echo replaceTag($getBlock, $replaceContent);
  30. }
  31. function query($q)
  32. {
  33. $f = mysql_query($q)or die(mysql_error());
  34. return $f;
  35. }
  36. function between($string, $start, $eind)
  37. {
  38. $string = " ".$string;
  39. $ini = strpos($string,$start);
  40. if ($ini == 0) return "";
  41. $ini += strlen($start);
  42. $len = strpos($string,$eind,$ini) - $ini;
  43. return substr($string,$ini,$len);
  44. }
  45. function replaceTag($str, $replace)
  46. {
  47. preg_match_all("/\{(.[^\{\}]*)\}/", $str, $out);
  48. foreach($out[1] as $key => $value)
  49. {
  50. if(isset($replace[$value]))
  51. {
  52. $str = str_replace("{".$value."}", $replace[$value], $str);
  53. }
  54. }
  55. return $str;
  56. }
  57. function getSections($theme)
  58. {
  59. $var = buffer($theme);
  60. $pattern = '/\{section:(.*?)\}/si';
  61. preg_match_all($pattern, substr($var,3), $sections);
  62. return $sections[1];
  63. }
  64. function debugmode($i)
  65. {
  66. echo '<pre>';
  67. print_r($i);
  68. echo '</pre>';
  69. }
  70. function v($i)
  71. {
  72. return htmlspecialchars($i);
  73. }
  74. function cutoffMax($i,$max)
  75. {
  76. // Check lenght of string
  77. $len = strlen($i);
  78. if($len > $max)
  79. {
  80. $cutoffAmount = $len - $max;
  81. $final = substr($i, 0, -$cutoffAmount).'..';
  82. }
  83. else
  84. {
  85. $final = $i;
  86. }
  87. // 12 x 18 (18/12=6 characters too much, remove last 6 characters)
  88. return $final;
  89. }
  90. function sql($i, $t=0)
  91. {
  92. if($t == 0)
  93. {
  94. $final = mysql_real_escape_string($i);
  95. }
  96. else
  97. {
  98. if($t == 1){ $type = $_GET; }
  99. elseif($t == 2){ $type = $_POST; }
  100. elseif($t == 3){ $type = $_COOKIE; }
  101. elseif($t == 4){ $type = $_SESSION; }
  102. $val = isset($type[$i]) ? $type[$i] : 0;
  103. $final = mysql_real_escape_string($val);
  104. }
  105. return $final;
  106. }
  107. // String Generation
  108. function strGen($amount, $type = 'alphanum')
  109. {
  110. // Define Vars so PHP Would not Print Errors
  111. $build = '';
  112. $r = '';
  113. // Set of characters to choose from
  114. $chars['alpha'] = 'abcdefghijklmnopqrstuvwxyz';
  115. $chars['alphaC'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  116. $chars['num'] = '0123456789';
  117. // Alphanumeric if you want all
  118. $chars['alphanum'] = $chars['alpha'] . $chars['alphaC'] . $chars['num'];
  119. // Print out each character, I want 100 characters
  120. for($i = 1; $i <= $amount; $i++)
  121. {
  122. // Random Number Between 1 and amount of characters in alpha
  123. $r = mt_rand(0, strlen($chars[$type])-1);
  124. // Echo the random selected Character
  125. $build .= $chars[$type][$r];
  126. }
  127. // Print Build
  128. return $build;
  129. }
  130. // Easy Cookie Function
  131. function cookie($name, $value, $time=31536000)
  132. {
  133. setcookie($name, $value, time()+$time, "/");
  134. }
  135. // Easy Redirect Function
  136. function redirect($location, $time=0)
  137. {
  138. echo '<META HTTP-EQUIV="REFRESH" CONTENT="'.$time.'; URL='.$location.'">';
  139. }
  140. function templateFiles($ads=null)
  141. {
  142. if($handle = opendir('../templates'))
  143. {
  144. $trap = ($ads == null) ? array() : $ads;
  145. while (false !== ($file = readdir($handle)))
  146. {
  147. if($file != '.' && $file != '..')
  148. {
  149. $tmp = explode('.', $file);
  150. $trap[] .= $tmp[0];
  151. }
  152. }
  153. closedir($handle);
  154. }
  155. return $trap;
  156. }
  157. function blockFiles($ads=null)
  158. {
  159. if($handle = opendir('../blocks'))
  160. {
  161. $trap = ($ads == null) ? array() : $ads;
  162. while (false !== ($file = readdir($handle)))
  163. {
  164. if($file != '.' && $file != '..')
  165. {
  166. $tmp = explode('.', $file);
  167. $trap[] .= $tmp[0];
  168. }
  169. }
  170. closedir($handle);
  171. }
  172. return $trap;
  173. }
  174. function getDir($dir)
  175. {
  176. if($handle = opendir($dir))
  177. {
  178. $trap = array();
  179. while (false !== ($file = readdir($handle)))
  180. {
  181. if($file != '.' && $file != '..')
  182. {
  183. $tmp = explode('.', $file);
  184. $trap[] .= $tmp[0];
  185. }
  186. }
  187. closedir($handle);
  188. }
  189. return $trap;
  190. }
  191. function moduleFiles($ads=null)
  192. {
  193. if($handle = opendir('../modules'))
  194. {
  195. $trap = ($ads == null) ? array() : $ads;
  196. while (false !== ($file = readdir($handle)))
  197. {
  198. $pos = strpos($file, 'Admin');
  199. if($file != '.' && $file != '..' && $file != 'modinit.php' && $pos === false)
  200. {
  201. $tmp = explode('.', $file);
  202. $trap[] = array(
  203. 0 => 'modules/'.$tmp[0].'.php',
  204. 1 => $tmp[0]
  205. );
  206. }
  207. }
  208. closedir($handle);
  209. }
  210. return $trap;
  211. }
  212. function getAdmins()
  213. {
  214. $trap = array();
  215. $q = mysql_query("SELECT * FROM ".tAdmins)or die(mysql_error());
  216. while($d = mysql_fetch_object($q))
  217. {
  218. $trap[] = array(
  219. 0 => $d->id,
  220. 1 => $d->username
  221. );
  222. }
  223. return $trap;
  224. }
  225. function getPages($ads=null)
  226. {
  227. $trap = array();
  228. $trap[] = array(
  229. 0 => 0,
  230. 1 => $ads[0]
  231. );
  232. $q = mysql_query("SELECT * FROM ".tPages)or die(mysql_error());
  233. while($d = mysql_fetch_object($q))
  234. {
  235. $trap[] = array(
  236. 0 => $d->id,
  237. 1 => $d->title
  238. );
  239. }
  240. return $trap;
  241. }