zira/zira/helper.php

206 lines
7.3 KiB
PHP
Raw Normal View History

2016-06-02 14:42:43 +02:00
<?php
/**
* Zira project
* helper.php
2019-10-09 15:27:31 +02:00
* (c)2015 https://github.com/ziracms/zira
2016-06-02 14:42:43 +02:00
*/
namespace Zira;
class Helper {
protected static $_add_language_to_url = true;
public static function setAddingLanguageToUrl($add_language_to_url) {
self::$_add_language_to_url = $add_language_to_url;
}
public static function html($str) {
return htmlspecialchars($str, ENT_QUOTES);
}
public static function url($path,$absolute=false,$detect_protocol=false) {
$url = '';
if ($absolute && $detect_protocol) {
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
$protocol = $port == 443 ? 'https:' : 'http:';
2019-01-05 22:19:02 +01:00
if ($port!=443 && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') $protocol = 'https:';
2016-06-02 14:42:43 +02:00
$url .= $protocol;
}
if ($absolute) $url .= '//' . $_SERVER['HTTP_HOST'];
$base_url = trim(BASE_URL,'/');
if (!empty($base_url)) $url .= '/'.$base_url;
if (!Config::get('clean_url')) {
$url .= '/index.php';
}
$path=trim($path,'/');
if (self::$_add_language_to_url &&
2017-11-15 17:02:47 +01:00
!Router::isAllLanguagesRoute($path) &&
2016-06-02 14:42:43 +02:00
Locale::getLanguage() &&
Config::get('languages') &&
count(Config::get('languages'))>1 &&
(Locale::getLanguage()!=Config::get('language') || !empty($path))
) {
$url .= '/'.Locale::getLanguage();
}
$url .= '/'.$path;
if ($url != '/') $url = rtrim($url,'/');
return $url;
}
public static function baseUrl($url,$absolute=false,$detect_protocol=false) {
$prefix = '';
if ($absolute && $detect_protocol) {
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
$protocol = $port == 443 ? 'https:' : 'http:';
2019-01-05 22:19:02 +01:00
if ($port!=443 && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') $protocol = 'https:';
2016-06-02 14:42:43 +02:00
$prefix .= $protocol;
}
if ($absolute) $prefix .= '//' . $_SERVER['HTTP_HOST'];
return $prefix . rtrim(BASE_URL,'/') . '/' .ltrim($url,'/');
}
public static function assetUrl($url) {
return rtrim(BASE_URL,'/') . '/' . ASSETS_DIR . '/' .$url;
}
public static function assetThemeUrl($url) {
return rtrim(BASE_URL,'/') . '/' . THEMES_DIR . '/' . View::getTheme() . '/' . ASSETS_DIR . '/' .$url;
}
public static function cssUrl($url) {
return rtrim(BASE_URL,'/') . '/' . ASSETS_DIR . '/' . CSS_DIR . '/' .$url;
}
public static function cssThemeUrl($url) {
return rtrim(BASE_URL,'/') . '/' . THEMES_DIR . '/' . View::getTheme() . '/' . ASSETS_DIR . '/' . CSS_DIR . '/' .$url;
}
public static function jsUrl($url) {
return rtrim(BASE_URL,'/') . '/' . ASSETS_DIR . '/' . JS_DIR . '/' .$url;
}
public static function jsThemeUrl($url) {
return rtrim(BASE_URL,'/') . '/' . THEMES_DIR . '/' . View::getTheme() . '/' . ASSETS_DIR . '/' . JS_DIR . '/' .$url;
}
public static function imgUrl($url) {
return rtrim(BASE_URL,'/') . '/' . ASSETS_DIR . '/' . IMAGES_DIR . '/' .$url;
}
public static function imgThemeUrl($url) {
return rtrim(BASE_URL,'/') . '/' . THEMES_DIR . '/' . View::getTheme() . '/' . ASSETS_DIR . '/' . IMAGES_DIR . '/' .$url;
}
2017-08-26 00:43:04 +02:00
public static function fileUrl($path) {
if (is_numeric($path)) return self::url('file/'.$path);
else return self::baseUrl($path);
}
2016-06-02 14:42:43 +02:00
public static function tag_short($name, array $attributes = null) {
if (!$attributes) $attributes = array();
$html = '<'.self::html($name);
foreach($attributes as $k=>$v) {
$html .= ' '.self::html($k) . '="' . self::html($v) . '"';
}
$html .= ' />';
return $html;
}
public static function tag_open($name, array $attributes = null) {
if (!$attributes) $attributes = array();
$html = '<'.self::html($name);
foreach($attributes as $k=>$v) {
$html .= ' '.self::html($k) . '="' . self::html($v) . '"';
}
$html .= '>';
return $html;
}
public static function tag_close($name) {
return '</'.self::html($name).'>';
}
public static function tag($name, $value = null, array $attributes = null) {
$html = self::tag_open($name, $attributes);
if ($value!==null) $html .= self::html($value);
$html .= self::tag_close($name);
return $html;
}
public static function nl2br($str) {
return nl2br(trim($str));
}
public static function utf8BadMatch($value) {
return preg_match('/[\x{10000}-\x{10FFFF}]/u', $value);
}
public static function utf8Clean($content) {
return preg_replace('/[\x{10000}-\x{10FFFF}]/u', ' ', $content);
}
public static function utf8Entity($content) {
if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $content, $m)) {
foreach($m[0] as $c) {
$h = dechex(self::utf8Ord($c));
$e = '&#x' . $h . ';';
$content = preg_replace('/[\x{'.$h.'}]/u', $e, $content);
}
}
return $content;
}
public static function utf8Ord($c) {
2021-11-23 11:44:40 +01:00
if (ord($c[0]) >=0 && ord($c[0]) <= 127)
return ord($c[0]);
if (ord($c[0]) >= 192 && ord($c[0]) <= 223)
return (ord($c[0])-192)*64 + (ord($c[1])-128);
if (ord($c[0]) >= 224 && ord($c[0]) <= 239)
return (ord($c[0])-224)*4096 + (ord($c[1])-128)*64 + (ord($c[2])-128);
if (ord($c[0]) >= 240 && ord($c[0]) <= 247)
return (ord($c[0])-240)*262144 + (ord($c[1])-128)*4096 + (ord($c[2])-128)*64 + (ord($c[3])-128);
if (ord($c[0]) >= 248 && ord($c[0]) <= 251)
return (ord($c[0])-248)*16777216 + (ord($c[1])-128)*262144 + (ord($c[2])-128)*4096 + (ord($c[3])-128)*64 + (ord($c[4])-128);
if (ord($c[0]) >= 252 && ord($c[0]) <= 253)
return (ord($c[0])-252)*1073741824 + (ord($c[1])-128)*16777216 + (ord($c[2])-128)*262144 + (ord($c[3])-128)*4096 + (ord($c[4])-128)*64 + (ord($c[5])-128);
if (ord($c[0]) >= 254 && ord($c[0]) <= 255) // error
return FALSE;
return 0;
}
2017-11-10 11:44:12 +01:00
2017-11-13 17:02:54 +01:00
public static function basename($path) {
if (preg_match('#^.*[\\\\/]([^\\\\/]+)$#s', $path, $m)) {
return $m[1];
}
return $path;
}
2019-12-01 13:04:02 +01:00
public static function urlencode($url, $isAbsolute = false) {
2017-11-13 21:36:35 +01:00
$url = rawurlencode($url);
2017-11-14 10:53:26 +01:00
$url = str_ireplace('%2F', '/', $url);
2019-12-01 13:04:02 +01:00
if ($isAbsolute) $url = str_ireplace('%3A', ':', $url);
2017-11-13 21:36:35 +01:00
return $url;
}
2019-07-12 19:21:00 +02:00
public static function isCurrentDay($time) {
$day_str = date(Config::get('date_format'), $time);
$current_str = date(Config::get('date_format'), time());
return $day_str == $current_str;
}
2019-07-13 13:40:04 +02:00
public static function datetime($time) {
return (self::isCurrentDay($time) ? date('H:i', $time) : date(Config::get('date_format'), $time));
}
2017-11-13 21:36:35 +01:00
2017-12-04 12:01:55 +01:00
public static function backtrace($return=false) {
2017-11-10 11:44:12 +01:00
$backtrace = debug_backtrace();
2017-12-04 12:01:55 +01:00
if (!$return) {
echo self::tag_open('pre').print_r(array_shift($backtrace), true).self::tag_close('pre');
} else {
return print_r(array_shift($backtrace), true);
}
2017-11-10 11:44:12 +01:00
}
2016-06-02 14:42:43 +02:00
}