Skip to content

Commit

Permalink
fix obsolete char indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
bob committed Nov 5, 2020
1 parent ac30447 commit 74e5563
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions std/os/h2o/h2o/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ class CoreFilters extends FilterCollection {
static function first($value) {
return $value[0];
}

static function last($value) {
return $value[count($value) - 1];
}

static function join($value, $delimiter = ', ') {
return join($delimiter, $value);
}

static function length($value) {
return count($value);
}

static function urlencode($data) {
if (is_array($data)) {
$result;
Expand All @@ -32,13 +32,13 @@ static function urlencode($data) {
return urlencode($data);
}
}

static function hyphenize ($string) {
$rules = array('/[^\w\s-]+/'=>'','/\s+/'=>'-', '/-{2,}/'=>'-');
$string = preg_replace(array_keys($rules), $rules, trim($string));
return $string = trim(strtolower($string));
}

static function urlize( $string, $truncate = false ) {
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $string, $matches);
Expand Down Expand Up @@ -73,24 +73,25 @@ static function humanize($string) {
$string = preg_replace('/\s+/', ' ', trim(preg_replace('/[^A-Za-z0-9()!,?$]+/', ' ', $string)));
return capfirst($string);
}

static function capitalize($string) {
return ucwords(strtolower($string)) ;
}

static function titlize($string) {
return self::capitalize($string);
}

static function capfirst($string) {
$string = strtolower($string);
return strtoupper($string{0}). substr($string, 1, strlen($string));
// Bob return strtoupper($string{0}). substr($string, 1, strlen($string));
return strtoupper($string[0]). substr($string, 1, strlen($string));
}

static function tighten_space($value) {
return preg_replace("/\s{2,}/", ' ', $value);
}

static function escape($value, $attribute = false) {
return htmlspecialchars($value, $attribute ? ENT_QUOTES : ENT_NOQUOTES);
}
Expand All @@ -100,23 +101,23 @@ static function escapejson($value) {
// This function encodes the entire data structure, and strings get quotes around them.
return json_encode($value);
}

static function force_escape($value, $attribute = false) {
return self::escape($value, $attribute);
}

static function e($value, $attribute = false) {
return self::escape($value, $attribute);
}

static function safe($value) {
return $value;
}

static function truncate ($string, $max = 50, $ends = '...') {
return (strlen($string) > $max ? substr($string, 0, $max).$ends : $string);
}

static function limitwords($text, $limit = 50, $ends = '...') {
if (strlen($text) > $limit) {
$words = str_word_count($text, 2);
Expand All @@ -136,7 +137,7 @@ static function filesize ($bytes, $round = 1) {
return '0 bytes';
elseif ($bytes === 1)
return '1 byte';

$units = array(
'bytes' => pow(2, 0), 'kB' => pow(2, 10),
'BM' => pow(2, 20), 'GB' => pow(2, 30),
Expand All @@ -157,14 +158,14 @@ static function filesize ($bytes, $round = 1) {

static function currency($amount, $currency = 'USD', $precision = 2, $negateWithParentheses = false) {
$definition = array(
'EUR' => array('','.',','), 'GBP' => '', 'JPY' => '',
'EUR' => array('','.',','), 'GBP' => '', 'JPY' => '',
'USD'=>'$', 'AU' => '$', 'CAN' => '$'
);
$negative = false;
$separator = ',';
$decimals = '.';
$currency = strtoupper($currency);

// Is negative
if (strpos('-', $amount) !== false) {
$negative = true;
Expand All @@ -184,7 +185,7 @@ static function currency($amount, $currency = 'USD', $precision = 2, $negateWith
if (round($amount, $precision) === $zero) {
$amount = $zero;
}

if (isset($definition[$currency])) {
$symbol = $definition[$currency];
if (is_array($symbol))
Expand All @@ -202,11 +203,11 @@ class HtmlFilters extends FilterCollection {
static function base_url($url, $options = array()) {
return $url;
}

static function asset_url($url, $options = array()) {
return self::base_url($url, $options);
}

static function image_tag($url, $options = array()) {
$attr = self::htmlAttribute(array('alt','width','height','border'), $options);
return sprintf('<img src="%s" %s/>', $url, $attr);
Expand All @@ -220,17 +221,17 @@ static function css_tag($url, $options = array()) {
static function script_tag($url, $options = array()) {
return sprintf('<script src="%s" type="text/javascript"></script>', $url);
}

static function links_to($text, $url, $options = array()) {
$attrs = self::htmlAttribute(array('ref'), $options);
$url = self::base_url($url, $options);
return sprintf('<a href="%s" %s>%s</a>', $url, $attrs, $text);
}

static function links_with ($url, $text, $options = array()) {
return self::links_to($text, $url, $options);
}

static function strip_tags($text) {
$text = preg_replace(array('/</', '/>/'), array(' <', '> '),$text);
return strip_tags($text);
Expand All @@ -241,11 +242,11 @@ static function linebreaks($value, $format = 'p') {
return HtmlFilters::nl2br($value);
return HtmlFilters::nl2pbr($value);
}

static function nl2br($value) {
return str_replace("\n", "<br />\n", $value);
}

static function nl2pbr($value) {
$result = array();
$parts = preg_split('/(\r?\n){2,}/m', $value);
Expand All @@ -257,7 +258,7 @@ static function nl2pbr($value) {

protected static function htmlAttribute($attrs = array(), $data = array()) {
$attrs = self::extract(array_merge(array('id', 'class', 'title', "style"), $attrs), $data);

$result = array();
foreach ($attrs as $name => $value) {
$result[] = "{$name}=\"{$value}\"";
Expand All @@ -277,24 +278,24 @@ protected static function extract($attrs = array(), $data=array()) {

class DatetimeFilters extends FilterCollection {
static function date($time, $format = 'jS F Y H:i') {
if ($time instanceof DateTime)
if ($time instanceof DateTime)
$time = (int) $time->format('U');
if (!is_numeric($time))
if (!is_numeric($time))
$time = strtotime($time);

return date($format, $time);
}

static function relative_time($timestamp, $format = 'g:iA') {
if ($timestamp instanceof DateTime)
if ($timestamp instanceof DateTime)
$timestamp = (int) $timestamp->format('U');

$timestamp = is_numeric($timestamp) ? $timestamp: strtotime($timestamp);

$time = mktime(0, 0, 0);
$delta = time() - $timestamp;
$string = '';

if ($timestamp < $time - 86400) {
return date("F j, Y, g:i a", $timestamp);
}
Expand All @@ -309,7 +310,7 @@ static function relative_time($timestamp, $format = 'g:iA') {
else if ($delta >= 3600)
$string .= "1 hour ";
$delta %= 3600;

if ($delta > 60)
$string .= floor($delta / 60) . " minutes ";
else
Expand All @@ -318,13 +319,13 @@ static function relative_time($timestamp, $format = 'g:iA') {
}

static function relative_date($time) {
if ($time instanceof DateTime)
if ($time instanceof DateTime)
$time = (int) $time->format('U');

$time = is_numeric($time) ? $time: strtotime($time);
$today = strtotime(date('M j, Y'));
$reldays = ($time - $today)/86400;

if ($reldays >= 0 && $reldays < 1)
return 'today';
else if ($reldays >= 1 && $reldays < 2)
Expand All @@ -346,13 +347,13 @@ static function relative_date($time) {
else
return date('l, F j, Y',$time ? $time : time());
}

static function relative_datetime($time) {
$date = self::relative_date($time);

if ($date === 'today')
return self::relative_time($time);

return $date;
}
}
Expand Down

0 comments on commit 74e5563

Please sign in to comment.