PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/func.php

http://snowcms.googlecode.com/
PHP | 272 lines | 141 code | 21 blank | 110 comment | 17 complexity | 8d3bf1b668e203e9c94df79bb946dd44 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////
  3. // SnowCMS v2.0 //
  4. // By the SnowCMS Team //
  5. // www.snowcms.com //
  6. // Released under the Microsoft Reciprocal License //
  7. // www.opensource.org/licenses/ms-rl.html //
  8. ////////////////////////////////////////////////////////////////////////////
  9. // //
  10. // SnowCMS originally pawned by soren121 started in early 2008 //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////
  13. // //
  14. // SnowCMS v2.0 began in November 2009 //
  15. // //
  16. ////////////////////////////////////////////////////////////////////////////
  17. // File version: SnowCMS 2.0 //
  18. ////////////////////////////////////////////////////////////////////////////
  19. if(!defined('INSNOW'))
  20. {
  21. die('Nice try...');
  22. }
  23. /*
  24. Title: Variable functions
  25. Function: init_func
  26. init_func initializes the the $func array which contains variable
  27. functions for handling strings to allow better language support.
  28. Parameters:
  29. none
  30. Returns:
  31. void - Nothing is returned by this function.
  32. */
  33. function init_func()
  34. {
  35. global $func;
  36. $func = array(
  37. // Add a couple aliases.
  38. 'htmlchars' => 'htmlchars',
  39. 'htmlspecialchars' => 'htmlchars',
  40. 'htmlchars_decode' => 'htmlchars_decode',
  41. 'htmlspecialchars_decode' => 'htmlchars_decode',
  42. );
  43. // Enable multibyte strings (which we set to use UTF-8).
  44. if(settings()->get('enable_utf8', 'bool', false) && function_exists('mb_internal_encoding'))
  45. {
  46. // Set the internal encoding to UTF-8.
  47. mb_internal_encoding('UTF-8');
  48. mb_http_output(mb_internal_encoding());
  49. // Handle the output buffer correctly.
  50. api()->add_filter('output_callback', create_function('$value', '
  51. return \'mb_output_handler\';'), 1);
  52. // Setup the variable functions for use!
  53. $func += array(
  54. 'parse_str' => 'mb_parse_str',
  55. 'mail' => 'mb_send_mail',
  56. 'stripos' => create_function('$haystack, $needle, $offset = 0', '
  57. // This function doesn\'t exist until PHP 5.2.0 >=
  58. if(function_exists(\'mb_stripos\'))
  59. return mb_stripos($haystack, $needle, $offset);
  60. else
  61. // Simple to emulate, really.
  62. return mb_strpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);'),
  63. 'stristr' => create_function('$haystack, $needle, $part = false', '
  64. // Same as mb_stripos, this doesn\'t exist until 5.2.0 as well.
  65. if(function_exists(\'mb_stristr\'))
  66. return mb_stristr($haystack, $needle, $part);
  67. else
  68. // Pretty easy to emulate too.
  69. return mb_strstr(mb_strtolower($haystack), mb_strtolower($needle), $part);'),
  70. 'strlen' => 'mb_strlen',
  71. 'strpos' => 'mb_strpos',
  72. 'strrchr' => 'mb_strrchr',
  73. 'strrichr' => create_function('$haystack, $needle, $part = false', '
  74. if(function_exists(\'mb_strrichr\'))
  75. return mb_strrichr($haystack, $needle, $part);
  76. else
  77. return mb_strrchr(mb_strtolower($haystack), mb_strtolower($needle), $part);'),
  78. 'strripos' => create_function('$haystack, $needle, $offset = 0', '
  79. if(function_exists(\'mb_strripos\'))
  80. return mb_strripos($haystack, $needle, $offset);
  81. else
  82. return mb_strrpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);'),
  83. 'strrpos' => 'mb_strrpos',
  84. 'strstr' => 'mb_strstr',
  85. 'strtolower' => 'mb_strtolower',
  86. 'strtoupper' => 'mb_strtoupper',
  87. 'ucwords' => create_function('$str', '
  88. // It may not have its own dedicated function, but this is good enough :P
  89. return mb_convert_case($str, MB_CASE_TITLE);'),
  90. 'substr_count' => 'mb_substr_count',
  91. 'substr' => 'mb_substr',
  92. );
  93. }
  94. else
  95. {
  96. // Define all the same variable functions, just without mb_ in front, really.
  97. $func += array(
  98. 'parse_str' => 'parse_str',
  99. 'mail' => 'mail',
  100. 'stripos' => 'stripos',
  101. 'stristr' => 'stristr',
  102. 'strlen' => 'strlen',
  103. 'strpos' => 'strpos',
  104. 'strrchr' => 'strrchr',
  105. 'strrichr' => create_function('$haystack, $needle, $part = false', '
  106. return strrchar(strtolower($haystack), strtolower($needle));'),
  107. 'strripos' => 'strripos',
  108. 'strrpos' => 'strrpos',
  109. 'strstr' => 'strstr',
  110. 'strtolower' => 'strtolower',
  111. 'strtoupper' => 'strtoupper',
  112. 'ucwords' => 'ucwords',
  113. 'substr_count' => 'substr_count',
  114. 'substr' => 'substr',
  115. );
  116. }
  117. api()->run_hooks('post_init_func', array(&$func));
  118. }
  119. /*
  120. Function: htmlchars
  121. Encodes the supplied string with htmlspecialchars with ENT_QUOTES and UTF-8
  122. as parameters. This function is here to simplify coding so you don't have to
  123. repeatedly to ENT_QUOTES, 'UTF-8' over and over again! ;)
  124. Parameters:
  125. string $str - The string to encode.
  126. Returns:
  127. string - Returns the encoded string.
  128. */
  129. function htmlchars($str)
  130. {
  131. return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  132. }
  133. /*
  134. Function: htmlchars_decode
  135. Decodes the supplied string with htmlspecialchars_decode with ENT_QUOTES
  136. as parameters.
  137. Parameters:
  138. string $str - The string to decode.
  139. Returns:
  140. string - Returns the decoded string.
  141. */
  142. function htmlchars_decode($str)
  143. {
  144. return htmlspecialchars_decode($str, ENT_QUOTES);
  145. }
  146. if(!function_exists('create_pagination'))
  147. {
  148. /*
  149. Function: create_pagination
  150. Creates a pagination... You know, those things that allow you to go to
  151. the next page and what not ;-).
  152. Parameters:
  153. string $tpl_url - The URL which will have &page={NUM} appended to.
  154. int &$start - The starting page, this is to be obtained from $_GET['page'].
  155. This is a reference parameter, after the pagination is generated
  156. this is meant to be put into a query in the LIMIT $start,PER_PAGE
  157. clause.
  158. int $num_items - The total number of items.
  159. int $per_page - The number of items to display per page.
  160. Returns:
  161. string - Returns the string containing the generated pagination.
  162. Note:
  163. This function is overloadable.
  164. */
  165. function create_pagination($tpl_url, &$start, $num_items, $per_page = 10)
  166. {
  167. // So how many pages total..?
  168. $total_pages = ceil((int)($num_items == 0 ? 1 : $num_items) / (int)$per_page);
  169. // Make sure start is an integer... At least make it one.
  170. $start = (int)$start;
  171. // We can't have a page less then one,
  172. // or greater then total_pages ;)
  173. if($start < 1)
  174. {
  175. $start = 1;
  176. }
  177. elseif($start > $total_pages)
  178. {
  179. $start = $total_pages;
  180. }
  181. // So start... Make an array holding all our stuffs.
  182. $index = array();
  183. // So the << First :) Though we may not link it
  184. // if we are on the first page.
  185. $index[] = '<span class="pagination_first">'. ($start != 1 ? '<a href="'. $tpl_url. '">' : ''). l('&laquo;&laquo; First'). ($start != 1 ? '</a>' : ''). '</span>';
  186. // Now the < which is the previous one... Don't link
  187. // it if thats where we are :P
  188. $index[] = '<span class="pagination_prev">'. ($start != 1 ? '<a href="'. (($start - 1) > 1 ? $tpl_url. '&page='. ($start - 1) : $tpl_url). '">' : ''). l('&laquo; Previous'). ($start != 1 ? '</a>' : ''). '</span>';
  189. // So now the page numbers...
  190. if($total_pages < 6)
  191. {
  192. // Hmm... Less then 5 :P
  193. $page_start = 1;
  194. $page_end = $total_pages;
  195. }
  196. elseif($start - 2 < 1)
  197. {
  198. // We are gonna go from 1 to 5 ;)
  199. $page_start = 1;
  200. $page_end = 5;
  201. }
  202. elseif($start + 2 <= $total_pages)
  203. {
  204. // Somewhere in between...
  205. $page_start = $start - 2;
  206. $page_end = $start + 2;
  207. }
  208. else
  209. {
  210. // The end of the line...
  211. // Some weird buggy that needs fixing...
  212. $page_start = ($start == ($total_pages - 1) ? $start - 3 : $start - 4);
  213. $page_end = $total_pages;
  214. }
  215. // So now that we have our numbers, for loop :D
  216. for($page = $page_start; $page < ($page_end + 1); $page++)
  217. {
  218. // So add the page number... Also, don't link the page number
  219. // if thats where we are at ;) oh, ya and, don't add &page=
  220. // to the end of our template url if its page one :)
  221. $index[] = '<span class="pagination_page'. ($page == $start ? ' pagination_current' : ''). '">'. ($page != $start ? '<a href="'. ($page != 1 ? $tpl_url. '&page='. $page : $tpl_url). '">' : ''). $page. ($page != $start ? '</a>' : ''). '</span>';
  222. }
  223. // Almost done :D!
  224. // So add the > which is the next one ;)
  225. // Don't link it if thats our current page...
  226. $index[] = '<span class="pagination_next">'. ($start < $total_pages ? '<a href="'. $tpl_url. '&page='. ($start + 1). '">' : ''). l('Next &raquo;'). ($start < $total_pages ? '</a>' : ''). '</span>';
  227. // Now the Last >> Of course, don't link it if thats where we are.
  228. $index[] = '<span class="pagination_last">'. ($start < $total_pages ? '<a href="'. $tpl_url. '&page='. $total_pages. '">' : ''). l('Last &raquo;&raquo;'). ($start < $total_pages ? '</a>' : ''). '</span>';
  229. // And we are done with the hard stuffs, yay.
  230. // So before we implode the stuff, take away 1 from
  231. // start, then multiply it by per_page. What for? For LIMIT clauses :D
  232. $start = ($start - 1) * $per_page;
  233. // Return it imploded...
  234. return '<span class="pagination">'. implode(' ', $index). '</span>';
  235. }
  236. }
  237. ?>