PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/v2/mod_goodrelations/rdfa/h2o/h2o/filters.php

https://code.google.com/p/goodrelations-for-joomla/
PHP | 342 lines | 273 code | 64 blank | 5 comment | 46 complexity | 1da5c639d709fe8ddd94bee15ee460b4 MD5 | raw file
  1. <?php
  2. class FilterCollection {};
  3. class CoreFilters extends FilterCollection {
  4. static function first($value) {
  5. return $value[0];
  6. }
  7. static function last($value) {
  8. return $value[count($value) - 1];
  9. }
  10. static function join($value, $delimiter = ', ') {
  11. return join($delimiter, $value);
  12. }
  13. static function urlencode($data) {
  14. if (is_array($data)) {
  15. $result;
  16. foreach ($data as $name => $value) {
  17. $result .= $name.'='.urlencode($value).'&'.$querystring;
  18. }
  19. $querystring = substr($result, 0, strlen($result)-1);
  20. return htmlspecialchars($result);
  21. } else {
  22. return urlencode($data);
  23. }
  24. }
  25. static function hyphenize ($string) {
  26. $rules = array('/[^\w\s-]+/'=>'','/\s+/'=>'-', '/-{2,}/'=>'-');
  27. $string = preg_replace(array_keys($rules), $rules, trim($string));
  28. return $string = trim(strtolower($string));
  29. }
  30. static function urlize($url, $truncate = false) {
  31. if (preg_match('/^(http|https|ftp:\/\/([^\s"\']+))/i', $url, $match))
  32. $url = "<a href='{$url}'>". ($truncate ? truncate($url,$truncate): $url).'</a>';
  33. return $url;
  34. }
  35. static function set_default($object, $default) {
  36. return !$object ? $default : $object;
  37. }
  38. }
  39. class StringFilters extends FilterCollection {
  40. static function humanize($string) {
  41. $string = preg_replace('/\s+/', ' ', trim(preg_replace('/[^A-Za-z0-9()!,?$]+/', ' ', $string)));
  42. return capfirst($string);
  43. }
  44. static function capitalize($string) {
  45. return ucwords(strtolower($string)) ;
  46. }
  47. static function titlize($string) {
  48. return self::capitalize($string);
  49. }
  50. static function capfirst($string) {
  51. $string = strtolower($string);
  52. return strtoupper($string{0}). substr($string, 1, strlen($string));
  53. }
  54. static function tighten_space($value) {
  55. return preg_replace("/\s{2,}/", ' ', $value);
  56. }
  57. static function escape($value, $attribute = false) {
  58. return htmlspecialchars($value, $attribute ? ENT_QUOTES : ENT_NOQUOTES);
  59. }
  60. static function force_escape($value, $attribute = false) {
  61. return self::escape($value, $attribute);
  62. }
  63. static function e($value, $attribute = false) {
  64. return self::escape($value, $attribute);
  65. }
  66. static function safe($value) {
  67. return $value;
  68. }
  69. static function truncate ($string, $max = 50, $ends = '...') {
  70. return (strlen($string) > $max ? substr($string, 0, $max).$ends : $string);
  71. }
  72. static function limitwords($text, $limit = 50, $ends = '...') {
  73. if (strlen($text) > $limit) {
  74. $words = str_word_count($text, 2);
  75. $pos = array_keys($words);
  76. if (isset($pos[$limit])) {
  77. $text = substr($text, 0, $pos[$limit]) . $ends;
  78. }
  79. }
  80. return $text;
  81. }
  82. }
  83. class NumberFilters extends FilterCollection {
  84. static function filesize ($bytes, $round = 1) {
  85. if ($bytes === 0)
  86. return '0 bytes';
  87. elseif ($bytes === 1)
  88. return '1 byte';
  89. $units = array(
  90. 'bytes' => pow(2, 0), 'kB' => pow(2, 10),
  91. 'BM' => pow(2, 20), 'GB' => pow(2, 30),
  92. 'TB' => pow(2, 40), 'PB' => pow(2, 50),
  93. 'EB' => pow(2, 60), 'ZB' => pow(2, 70)
  94. );
  95. $lastUnit = 'bytes';
  96. foreach ($units as $unitName => $unitFactor) {
  97. if ($bytes >= $unitFactor) {
  98. $lastUnit = $unitName;
  99. } else {
  100. $number = round( $bytes / $units[$lastUnit], $round );
  101. return number_format($number) . ' ' . $lastUnit;
  102. }
  103. }
  104. }
  105. static function currency($amount, $currency = 'USD', $precision = 2, $negateWithParentheses = false) {
  106. $definition = array(
  107. 'EUR' => array('?','.',','), 'GBP' => '?', 'JPY' => '?',
  108. 'USD'=>'$', 'AU' => '$', 'CAN' => '$'
  109. );
  110. $negative = false;
  111. $separator = ',';
  112. $decimals = '.';
  113. $currency = strtoupper($currency);
  114. // Is negative
  115. if (strpos('-', $amount) !== false) {
  116. $negative = true;
  117. $amount = str_replace("-","",$amount);
  118. }
  119. $amount = (float) $amount;
  120. if (!$negative) {
  121. $negative = $amount < 0;
  122. }
  123. if ($negateWithParentheses) {
  124. $amount = abs($amount);
  125. }
  126. // Get rid of negative zero
  127. $zero = round(0, $precision);
  128. if (round($amount, $precision) === $zero) {
  129. $amount = $zero;
  130. }
  131. if (isset($definition[$currency])) {
  132. $symbol = $definition[$currency];
  133. if (is_array($symbol))
  134. @list($symbol, $separator, $decimals) = $symbol;
  135. } else {
  136. $symbol = $currency;
  137. }
  138. $amount = number_format($amount, $precision, $decimals, $separator);
  139. return $negateWithParentheses ? "({$symbol}{$amount})" : "{$symbol}{$amount}";
  140. }
  141. }
  142. class HtmlFilters extends FilterCollection {
  143. static function base_url($url, $options = array()) {
  144. return $url;
  145. }
  146. static function asset_url($url, $options = array()) {
  147. return self::base_url($url, $options);
  148. }
  149. static function image_tag($url, $options = array()) {
  150. $attr = self::htmlAttribute(array('alt','width','height','border'), $options);
  151. return sprintf('<img src="%s" %s/>', $url, $attr);
  152. }
  153. static function css_tag($url, $options = array()) {
  154. $attr = self::htmlAttribute(array('media'), $options);
  155. return sprintf('<link rel="stylesheet" href="%s" type="text/css" %s />', $url, $attr);
  156. }
  157. static function script_tag($url, $options = array()) {
  158. return sprintf('<script src="%s" type="text/javascript"></script>', $url);
  159. }
  160. static function links_to($text, $url, $options = array()) {
  161. $attrs = self::htmlAttribute(array('ref'), $options);
  162. $url = self::base_url($url, $options);
  163. return sprintf('<a href="%s" %s>%s</a>', $url, $attrs, $text);
  164. }
  165. static function links_with ($url, $text, $options = array()) {
  166. return self::links_to($text, $url, $options);
  167. }
  168. static function strip_tags($text) {
  169. $text = preg_replace(array('/</', '/>/'), array(' <', '> '),$text);
  170. return strip_tags($text);
  171. }
  172. static function linebreaks($value, $format = 'p') {
  173. if ($format === 'br')
  174. return HtmlFilters::nl2br($value);
  175. return HtmlFilters::nl2pbr($value);
  176. }
  177. static function nl2br($value) {
  178. return str_replace("\n", "<br />\n", $value);
  179. }
  180. static function nl2pbr($value) {
  181. $result = array();
  182. $parts = preg_split('/(\r?\n){2,}/m', $value);
  183. foreach ($parts as $part) {
  184. array_push($result, '<p>' . HtmlFilters::nl2br($part) . '</p>');
  185. }
  186. return implode("\n", $result);
  187. }
  188. protected static function htmlAttribute($attrs = array(), $data = array()) {
  189. $attrs = self::extract(array_merge(array('id', 'class', 'title', "style"), $attrs), $data);
  190. $result = array();
  191. foreach ($attrs as $name => $value) {
  192. $result[] = "{$name}=\"{$value}\"";
  193. }
  194. return join(' ', $result);
  195. }
  196. protected static function extract($attrs = array(), $data=array()) {
  197. $result = array();
  198. if (empty($data)) return array();
  199. foreach($data as $k => $e) {
  200. if (in_array($k, $attrs)) $result[$k] = $e;
  201. }
  202. return $result;
  203. }
  204. }
  205. class DatetimeFilters extends FilterCollection {
  206. static function date($time, $format = 'jS F Y H:i') {
  207. if ($time instanceof DateTime)
  208. $time = (int) $time->format('U');
  209. if (!is_numeric($time))
  210. $time = strtotime($time);
  211. return date($format, $time);
  212. }
  213. static function relative_time($timestamp, $format = 'g:iA') {
  214. if ($timestamp instanceof DateTime)
  215. $timestamp = (int) $timestamp->format('U');
  216. $timestamp = is_numeric($timestamp) ? $timestamp: strtotime($timestamp);
  217. $time = mktime(0, 0, 0);
  218. $delta = time() - $timestamp;
  219. $string = '';
  220. if ($timestamp < $time - 86400) {
  221. return date("F j, Y, g:i a", $timestamp);
  222. }
  223. if ($delta > 86400 && $timestamp < $time) {
  224. return "Yesterday at " . date("g:i a", $timestamp);
  225. }
  226. if ($delta > 7200)
  227. $string .= floor($delta / 3600) . " hours, ";
  228. else if ($delta > 3660)
  229. $string .= "1 hour, ";
  230. else if ($delta >= 3600)
  231. $string .= "1 hour ";
  232. $delta %= 3600;
  233. if ($delta > 60)
  234. $string .= floor($delta / 60) . " minutes ";
  235. else
  236. $string .= $delta . " seconds ";
  237. return "$string ago";
  238. }
  239. static function relative_date($time) {
  240. if ($time instanceof DateTime)
  241. $time = (int) $time->format('U');
  242. $time = is_numeric($time) ? $time: strtotime($time);
  243. $today = strtotime(date('M j, Y'));
  244. $reldays = ($time - $today)/86400;
  245. if ($reldays >= 0 && $reldays < 1)
  246. return 'today';
  247. else if ($reldays >= 1 && $reldays < 2)
  248. return 'tomorrow';
  249. else if ($reldays >= -1 && $reldays < 0)
  250. return 'yesterday';
  251. if (abs($reldays) < 7) {
  252. if ($reldays > 0) {
  253. $reldays = floor($reldays);
  254. return 'in ' . $reldays . ' day' . ($reldays != 1 ? 's' : '');
  255. } else {
  256. $reldays = abs(floor($reldays));
  257. return $reldays . ' day' . ($reldays != 1 ? 's' : '') . ' ago';
  258. }
  259. }
  260. if (abs($reldays) < 182)
  261. return date('l, F j',$time ? $time : time());
  262. else
  263. return date('l, F j, Y',$time ? $time : time());
  264. }
  265. static function relative_datetime($time) {
  266. $date = self::relative_date($time);
  267. if ($date === 'today')
  268. return self::relative_time($time);
  269. return $date;
  270. }
  271. }
  272. /* Ultizie php funciton as Filters */
  273. h2o::addFilter(array('md5', 'sha1', 'numberformat'=>'number_format', 'wordwrap', 'trim', 'upper' => 'strtoupper', 'lower' => 'strtolower'));
  274. /* Add filter collections */
  275. h2o::addFilter(array('CoreFilters', 'StringFilters', 'NumberFilters', 'DatetimeFilters', 'HtmlFilters'));
  276. /* Alias default to set_default */
  277. h2o::addFilter('default', array('CoreFilters', 'set_default'));
  278. ?>