diff --git a/class-EnfatizeSubstr.php b/class-EnfatizeSubstr.php
deleted file mode 100644
index 72e08b4..0000000
--- a/class-EnfatizeSubstr.php
+++ /dev/null
@@ -1,65 +0,0 @@
-.
-
-/**
- * Enfatize a substring
- */
-class EnfatizeSubstr {
-
- /**
- * Enfatize a substring
- *
- * @param $s string Heystack
- * @param $q string Needle
- * @param $pre string HTML before query (bold tag as default)
- * @param $post string HTML after query (bold tag as default)
- * @return string Enfatized string
- * @todo Move in it's own class
- */
- static function get( $s, $q, $pre = "", $post = "" ) {
-
- // no needle? that's quick
- if( empty( $q ) ) {
- return $s;
- }
-
- $s_length = strlen( $s );
- $q_length = strlen( $q );
- $offset = 0;
- do {
- // find occurrence
- $pos = stripos( $s, $q, $offset );
- if($pos === false) {
- break;
- }
-
- // enfatize query
- $enfatized = $pre . substr( $s, $pos, $q_length ) . $post;
- $enfatized_length = strlen( $enfatized );
-
- // pre-query and post-query strings
- $s_pre = substr( $s, 0, $pos );
- $s_post = substr( $s, $pos + $q_length );
-
- // save
- $s = $s_pre . $enfatized . $s_post;
-
- $offset = $pos + $enfatized_length;
- } while( $offset < $s_length );
-
- return $s;
- }
-}
diff --git a/class-Slug.php b/class-StringStuff.php
similarity index 63%
rename from class-Slug.php
rename to class-StringStuff.php
index a90ef09..e5d6c16 100644
--- a/class-Slug.php
+++ b/class-StringStuff.php
@@ -1,55 +1,105 @@
.
-class Slug {
+/**
+ * Collection of string utilities
+ */
+class StringStuff {
+
/**
- * Generate a slug.
+ * Generate a slug
*
- * @param string $s Input
+ * @param string $s Input string e.g. 'what the hell'
* @param int $max_length Max. length limit
* @param string $glue Slug glue
* @param bool $truncated Flag to indicate if the string is truncated by the $max_length limit
+ * @return 'something-as-this'
*/
- static function get($s, $max_length = -1, $glue = '-', & $truncated) {
+ public static function slug( $s, $max_length = -1, $glue = '-', & $truncated ) {
$truncated = false;
$s = strtolower( self::stripAccents($s) );
if( $glue !== '_' ) {
- $s = str_replace('_',' ', $s);
+ $s = str_replace( '_',' ', $s );
}
- $s = preg_replace("/[^a-z0-9\s\\$glue]/", '', $s);
- $s = preg_replace("/[\s\\$glue]+/", ' ', $s);
- $s = trim($s, ' ');
- if($max_length !== -1) {
- $len = strlen($s);
- $s = substr($s, 0, $max_length);
- $truncated = $len !== strlen($s);
+ $s = preg_replace( "/[^a-z0-9\s\\$glue]/", '', $s );
+ $s = preg_replace( "/[\s\\$glue]+/", ' ', $s );
+ $s = trim( $s, ' ' );
+ if( $max_length !== -1 ) {
+ $len = strlen( $s );
+ $s = substr( $s, 0, $max_length );
+ $truncated = $len !== strlen( $s );
}
- $s = preg_replace('/\s/', $glue, $s);
- return rtrim($s, $glue);
+ $s = preg_replace( '/\s/', $glue, $s );
+ return rtrim( $s, $glue );
+ }
+
+ /**
+ * Enfatize a substring
+ *
+ * @param $s string Heystack
+ * @param $q string Needle
+ * @param $pre string HTML before query (bold tag as default)
+ * @param $post string HTML after query (bold tag as default)
+ * @return string Enfatized string
+ * @todo Move in it's own class
+ */
+ public static function enfatizeSubstring( $s, $q, $pre = "", $post = "" ) {
+
+ // no needle? that's quick
+ if( empty( $q ) ) {
+ return $s;
+ }
+
+ $s_length = strlen( $s );
+ $q_length = strlen( $q );
+ $offset = 0;
+ do {
+ // find occurrence
+ $pos = stripos( $s, $q, $offset );
+ if($pos === false) {
+ break;
+ }
+
+ // enfatize query
+ $enfatized = $pre . substr( $s, $pos, $q_length ) . $post;
+ $enfatized_length = strlen( $enfatized );
+
+ // pre-query and post-query strings
+ $s_pre = substr( $s, 0, $pos );
+ $s_post = substr( $s, $pos + $q_length );
+
+ // save
+ $s = $s_pre . $enfatized . $s_post;
+
+ $offset = $pos + $enfatized_length;
+ } while( $offset < $s_length );
+
+ return $s;
}
/**
* Strip accents.
*
- * @param string $s
+ * @param $s string
* @return string
*/
- static function stripAccents($s) {
+ public static function stripAccents( $s ) {
$a = ['À','Á','Â','Ã','Ä','Å','Æ', 'Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ', 'ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','Ā','ā','Ă','ă','Ą','ą','Ć','ć','Ĉ','ĉ','Ċ','ċ','Č','č','Ď','ď','Đ','đ','Ē','ē','Ĕ','ĕ','Ė','ė','Ę','ę','Ě','ě','Ĝ','ĝ','Ğ','ğ','Ġ','ġ','Ģ','ģ','Ĥ','ĥ','Ħ','ħ','Ĩ','ĩ','Ī','ī','Ĭ','ĭ','Į','į','İ','ı','IJ','ij','Ĵ','ĵ','Ķ','ķ','Ĺ','ĺ','Ļ','ļ','Ľ','ľ','Ŀ','ŀ','Ł','ł','Ń','ń','Ņ','ņ','Ň','ň','ʼn','Ō','ō','Ŏ','ŏ','Ő','ő','Œ','œ','Ŕ','ŕ','Ŗ','ŗ','Ř','ř','Ś','ś','Ŝ','ŝ','Ş','ş','Š','š','Ţ','ţ','Ť','ť','Ŧ','ŧ','Ũ','ũ','Ū','ū','Ŭ','ŭ','Ů','ů','Ű','ű','Ų','ų','Ŵ','ŵ','Ŷ','ŷ','Ÿ','Ź','ź','Ż','ż','Ž','ž','ſ','ƒ','Ơ','ơ','Ư','ư','Ǎ','ǎ','Ǐ','ǐ','Ǒ','ǒ','Ǔ','ǔ','Ǖ','ǖ','Ǘ','ǘ','Ǚ','ǚ','Ǜ','ǜ','Ǻ','ǻ','Ǽ','ǽ','Ǿ','ǿ','Ά','ά','Έ','έ','Ό','ό','Ώ','ώ','Ί','ί','ϊ','ΐ','Ύ','ύ','ϋ','ΰ','Ή','ή'];
$b = ['A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o','Α','α','Ε','ε','Ο','ο','Ω','ω','Ι','ι','ι','ι','Υ','υ','υ','υ','Η','η'];
- return str_replace($a, $b, $s);
+ return str_replace( $a, $b, $s );
}
-}
+
+}
\ No newline at end of file
diff --git a/functions.php b/functions.php
index eba3d74..b4327a0 100644
--- a/functions.php
+++ b/functions.php
@@ -1,774 +1,773 @@
.
// unuseful shortcuts when you want a SELECT * query
define('DOT', '.');
define('STAR', '*');
function define_default( $constant_name, $default_value ) {
defined( $constant_name ) or define( $constant_name, $default_value );
}
function selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
return html_property_when_matching( 'selected', 'selected', $helper, $current, $force);
}
function checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
return html_property_when_matching( 'checked', 'checked', $helper, $current, $force);
}
function disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
return html_property_when_matching( 'disabled', 'disabled', $helper, $current, $force );
}
function _checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
echo checked( $helper, $current, $force );
}
function _selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
echo selected( $helper, $current, $force );
}
function _disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
echo disabled( $helper, $current, $force );
}
function html_property_when_matching( $property, $value, $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) {
if( $helper === $current || $helper && PHP_INT_MAX === $current || $force ) {
return HTML::property( $property, $value );
}
return '';
}
function _value( $v ) {
echo HTML::property( 'value', $v );
}
/**
* Force something to be an array.
*
* @return mixed|array
* @return array
*/
function force_array( & $a ) {
if( ! is_array( $a ) ) {
$a = [ $a ];
}
}
/**
- * Enfatize a substring.
- * @see EnfatizeSubstr::get()
+ * Enfatize a substring
*/
function enfatize_substr( $heystack, $needle, $pre = '', $post = '' ) {
- return EnfatizeSubstr::get( $heystack, $needle, $pre, $post );
+ return StringStuff::enfatizeSubstr( $heystack, $needle, $pre, $post );
}
/**
* SQL query escape string
* @see DB#escapeString()
*/
function esc_sql($s) {
return DB::instance()->escapeString($s);
}
/**
* Same as esc_sql() but also avoid '%'s.
*
* @param string $s
* @return string
*/
function esc_sql_like($s) {
$s = str_replace('%', '\%', $s);
return esc_sql($s);
}
/**
* HTML escape
*
* @param string $s
* @return string
*/
function esc_html($s) {
return htmlspecialchars($s);
}
/**
* HTML escape print
*
* @param string
* @return void
*/
function _esc_html($s) {
echo htmlentities($s);
}
/**
* Execute a simple query.
* @see DB#getResults()
*/
function query($query) {
return DB::instance()->query($query);
}
/**
* Execute a query and return an array of objects.
*
* @param string $query SQL query
* @param string $class Class name to encapsulate the result set
* @return array
* @see DB#getResults()
*/
function query_results($query, $class = null, $args = [] ) {
return DB::instance()->getResults($query, $class, $args);
}
/**
* Execute a query and return a Generator
*
* @param string $query SQL query
* @param string $class Class name to encapsulate the result set
* @generator
* @see DB#getGenerator()
*/
function query_generator( $query, $class = null, $args = [] ) {
return DB::instance()->getGenerator( $query, $class, $args );
}
/**
* Execute a query and return an object.
*
* @param string $query SQL query
* @param string $class Class name to encapsulate the result set
* @return null|Object
* @see DB#getRow()
*/
function query_row($query, $class = null, $args = [] ) {
return DB::instance()->getRow($query, $class, $args);
}
/**
* Execute a query and return a single value.
* @see DB#getValue()
*/
function query_value($query, $value, $class = null) {
return DB::instance()->getValue($query, $value, $class);
}
/**
* Executes one or multiple queries which are concatenated by a semicolon
* @see DB#multiQuery()
*/
function multiquery( $queries ) {
return DB::instance()->multiQuery( $queries );
}
/**
* Database table full with prefix.
*
* @param string $t Table name (as 'test')
* @return string Table name with prefix (as '`site01_test`')
* @see DB#getTable()
*/
function T($t, $as = false) {
return DB::instance()->getTable($t, $as);
}
define('T', 'T');
define('JOIN', 'JOIN');
// Stupid shurtcut for string context
$GLOBALS[T] = function($t, $as = false) {
return T($t, $as = false);
};
// Stupid shortcut for string context for listing tables
$GLOBALS[JOIN] = function($t) {
return DB::instance()->getTables( func_get_args() );
};
/**
* Insert a row in the specified database table.
* @param string $table
* @param DBCols[]
* @param $args array
* @see DB#insertRow()
*/
function insert_row( $table, $cols, $args = [] ) {
return DB::instance()->insertRow( $table, $cols, $args );
}
/**
* If the table has an AUTOINCREMENT you can get the last inserted index
* after an insert_row().
* @return int
* @see DB#getLastInsertedID()
*/
function last_inserted_ID() {
if( ! DB::instanced() ) {
error_die( 'cannot obtain last inserted ID without database connection' );
}
return DB::instance()->getLastInsertedID();
}
/**
* Insert multiple values in the specified database table
* @param string $table
* @param array $cols
* @param array $values
* @see DB#insert()
*/
function insert_values($table, $cols, $values) {
return DB::instance()->insert($table, $cols, $values);
}
/**
* Update rows in the specified database table
* @param string $table
* @param DBCol[] $cols
* @param string $condition
* @see DB#update()
*/
function query_update($table, $cols, $condition, $after = '') {
DB::instance()->update($table, $cols, $condition, $after);
}
/**
* Alias for htmlspecialchars().
* @param string $s
* @return string
*/
function esc_attr($s) {
return htmlspecialchars($s);
}
/**
* @param string
* @return void
*/
function _esc_attr($s) {
echo htmlspecialchars($s);
}
/*
* Friendly symlinks
*/
function register_mimetypes($category, $mimetypes) {
MimeTypes::instance()->registerMimetypes($category, $mimetypes);
}
function get_mimetypes($category = null) {
return MimeTypes::instance()->getMimetypes($category);
}
/**
* @param string $role User role
* @param string|string[] $permissions Permissions
*/
function register_permissions($role, $permissions) {
Permissions::instance()->registerPermissions($role, $permissions);
}
/**
* @param string $role_to New role
* @param string $role_from Existing role
*/
function inherit_permissions($role_to, $role_from, $other_permissions = []) {
Permissions::instance()->inheritPermissions($role_to, $role_from, $other_permissions);
}
function register_js($javascript_uid, $url, $position = null) {
return RegisterJS::instance()->register( $javascript_uid, $url, $position );
}
function enqueue_js($javascript_uid, $position = null) {
return RegisterJS::instance()->enqueue( $javascript_uid, $position );
}
function register_css($css_uid, $url) {
return RegisterCSS::instance()->register($css_uid, $url);
}
function enqueue_css($css_uid) {
return RegisterCSS::instance()->enqueue($css_uid);
}
function add_menu_entries($menuEntries) {
Menu::instance()->add($menuEntries);
}
function get_menu_entry($uid) {
return Menu::instance()->getMenuEntry($uid);
}
function get_children_menu_entries($parentUid) {
return Menu::instance()->getChildrenMenuEntries($parentUid);
}
function register_module($module_uid) {
return RegisterModule::instance()->register($module_uid);
}
function inject_in_module($module_uid, $callback) {
return RegisterModule::instance()->injectFunction($module_uid, $callback);
}
function load_module($module_uid) {
return RegisterModule::instance()->loadModule($module_uid);
}
function get_table_prefix() {
return DB::instance()->getPrefix();
}
function register_option($option_name) {
return DB::instance()->registerOption($option_name);
}
function get_option( $name, $default = '' ) {
return Options::instance()->get( $name, $default );
}
function set_option( $name, $value, $autoload = true ) {
return Options::instance()->set( $name, $value, $autoload );
}
function remove_option( $name ) {
return Options::instance()->remove( $name );
}
/**
* Get the current logged user.
*
* @param null|string $property Property name
* @return mixed|Sessionuser Property, or entire Sessionuser object.
*/
function get_user( $property = null ) {
$user = Session::instance()->getUser();
if( null === $property ) {
return $user;
}
return $user ? $user->get( $property ) : null;
}
/**
* Try to login using $_POST['user_uid'] and $_POST['user_password'].
*
* @param int $status
* @see Session::login()
*/
function login(& $status = null, $user_uid = null, $user_password = null) {
return Session::instance()->login($status, $user_uid, $user_password);
}
function logout() {
return Session::instance()->destroy();
}
function register_language($code, $aliases = [], $encode = null, $iso = null, $human = null) {
return RegisterLanguage::instance()->registerLanguage($code, $aliases, $encode, $iso, $human);
}
function register_default_language($default) {
return RegisterLanguage::instance()->setDefaultLanguage($default);
}
function find_language($language_alias) {
return RegisterLanguage::instance()->getLanguage($language_alias);
}
function apply_language($language_alias = null) {
return RegisterLanguage::instance()->applyLanguage($language_alias);
}
function latest_language() {
return RegisterLanguage::instance()->getLatestLanguageApplied();
}
function all_languages() {
return RegisterLanguage::instance()->getAll();
}
function get_num_queries() {
if( DB::instanced() ) {
return DB::instance()->getNumQueries();
}
return 0;
}
function is_logged() {
return Session::instance()->isLogged();
}
/**
* @param string $permission Permission uid
* @param User|null $user Specified user
* @return bool
*/
function has_permission($permission, $user = null) {
if( ! $user ) {
$user = Session::instance()->getUser();
}
$role = $user
? $user->get( 'user_role' )
: false;
if( ! $role ) {
$role = DEFAULT_USER_ROLE;
}
return Permissions::instance()->hasPermission($role, $permission);
}
/**
* Add a directory to a base URL or a pathname.
* If the base URL it is not defined, a slash ('/') is appended to the URL.
* The base URL could end with a slash ('/') or not.
*
* @param string $base_URL Base URL with/without any slash at start
* @param string $dir Directory without any slash
* @return string URL / Pathname
*/
function append_dir( $base_URL, $dir = '/' ) {
$base_URL = rtrim( $base_URL, '/' );
$dir = ltrim( $dir, '/' );
return $base_URL . _ . $dir;
}
/**
* Normalize a site page
*
* @param $page string Whatever, a full URL, a relative pathname e.g. 'page', an absolute one, etc.
* @param $full_url boolean As default it try to avoid full URLs
*/
function site_page( $page, $full_url = false ) {
$first = @$page[ 0 ];
if( $first === '#' ) {
return $page; // '#anchor'
}
if( $first === '/' ) {
if( @$page[ 1 ] === '/' ) {
return $page; // "//example.org"
}
return append_dir( $full_url ? BASE_URL : '', $page );
} elseif( preg_match( '#^[a-z]+://#', $page ) === 1 ) {
return $page; // "ftp://example.org"
}
return append_dir( $full_url ? URL : ROOT, $page );
}
function single_quotes($s) {
return "'$s'";
}
function double_quotes($s) {
return '"' . $s . '"';
}
function this_folder() {
return dirname( $_SERVER['PHP_SELF'] );
}
/**
* Truncate a string if it's over a specific length.
* You can specify the end of the string if it's truncated.
*
* @param string %s Input string
* @param int $max_length Max string length
* @param string $blabla Optional. If string length is over $max_length, $blabla it's appended after $string
* @deprecated Use mb_strimwidth
*/
function str_truncate($s, $max_length, $blabla = '', $encoding = null ) {
if( ! $encoding ) {
$encoding = mb_internal_encoding();
}
return mb_strimwidth($s, 0, $max_length, $blabla, $encoding);
}
/**
* Choose the appropriate string.
* '%' will be replaced with the input number.
*
* @param int $n Input number.
* @param string $text_multi Text displayed if n > 1
* @param string $text_one Text displayed if n == 1
* @param string $text_no Text displayed if n < 1
*/
function multi_text($n, $text_multi, $text_one, $text_no = '') {
if($n > 1) {
return str_replace('%', $n, $text_multi);
} elseif($n == 1) {
return $text_one;
}
return $text_no;
}
/**
* Simple HTTP redirects.
*/
function http_redirect($url, $http_response_code = 0) {
header("Location: $url", true, $http_response_code);
exit;
}
/**
* Check if the request is under HTTPS
*/
function is_https() {
return ! empty( $_SERVER['HTTPS'] );
}
/**
* Get the protocol of the request
* @see use PROTOCOL
*/
function URL_protocol() {
return is_https() ? 'https://' : 'http://';
}
/**
* Get the domain of the request
* @see DOMAIN
*/
function URL_domain() {
return isset( $_SERVER[ 'SERVER_NAME' ] )
? $_SERVER[ 'SERVER_NAME' ]
: php_uname( 'n' );
}
/**
* Get the explicit port of the request
* @see PORT
* @return string
*/
function URL_port() {
if( isset( $_SERVER[ 'SERVER_PORT' ] ) ) {
$p = (int) $_SERVER[ 'SERVER_PORT' ];
if( 80 !== $p && 443 !== $p ) {
return ":$p";
}
}
return '';
}
function URL_root() {
$root = this_folder();
if( $root === _ ) {
return '';
}
return $root;
}
/**
* Get a secured version of a string
*/
function generate_slug($s, $max_length = -1, $glue = '-', & $truncated = false) {
- return Slug::get($s, $max_length, $glue, $truncated);
+ return StringStuff::slug($s, $max_length, $glue, $truncated);
}
function http_build_get_query( $url, $data ) {
$data = http_build_query( $data );
return $data ? "$url?$data" : $url;
}
/**
* HTTP 503 headers
*/
function http_503() {
return Shit::header503();
}
/**
* It scares the user with an error message (and dies).
*/
function error_die( $msg ) {
Shit::WSOD( $msg );
}
/**
* It logs an error message and eventually prints it when DEBUG
*
* @return void
*/
function error( $msg ) {
Shit::error( $msg );
}
/**
* Translates a string
*
* @param string $msgid String to be translated
* @return string translated string (or original, if not found)
*/
function __( $msgid ) {
// does the user want a native GNU GETTEXT implementation? cache it.
static $native = null;
if( null === $native ) {
$native = RegisterLanguage::instance()->isNative();
}
if( $native ) {
// low-level GNU Gettext call (quicker but the locales must be installed by a sysadmin, and other cache problems)
return _( $msgid );
}
// high-level GNU Gettext (simpler but slower)
return MoLoader::instance()->getTranslator()->gettext( $msgid );
}
/**
* Shortcut for echoing a translated string
*/
function _e( $s ) {
echo __( $s );
}
function http_json_header($charset = null) {
if( null === $charset ) {
$charset = CHARSET;
}
header( "Content-Type: application/json; charset=$charset" );
}
/**
* Unset the empty values in an array or an object as well recursively
*
* @param $data mixed
* @return array
*/
function array_unset_empty( $data ) {
$is_array = is_array( $data );
if( $is_array || is_object( $data ) ) {
foreach( $data as $k => $v ) {
if( is_array( $v ) || is_object( $v ) ) {
if( $is_array ) {
$data [ $k ] = array_unset_empty( $v );
} else {
$data->{ $k } = array_unset_empty( $v );
}
} elseif( empty( $v ) && ! is_int( $v ) ) {
if( $is_array ) {
unset( $data [ $k ] );
} else {
unset( $data->{ $k } );
}
}
}
}
return $data;
}
/**
* Send a JSON (stripping out unuseful values) and quit
*
* Falsy elements are not returned
*
* @param $data mixed
*/
function json( $data ) {
http_json_header();
echo json_encode( array_unset_empty( $data ) );
exit;
}
/**
* Send a JSON error and quit
*
* @param $http_code int HTTP response code
* @param $code string Error code
* @param $msg string Error human message
*/
function json_error( $http_code, $code, $msg ) {
http_response_code( $http_code );
json( [ 'error' => [
'code' => $code,
'message' => $msg,
] ] );
}
/**
* Get the MIME type of a file.
* @see MimeTypes::fileMimetype()
*/
function get_mimetype($filepath, $pure = false) {
return MimeTypes::fileMimetype( $filepath, $pure = false );
}
/**
* Know if a file belongs to a certain category
* @see MimeTypes::isMimetypeInCategory()
*/
function is_file_in_category($filepath, $category) {
$mime = get_mimetype($filepath);
return MimeTypes::instance()->isMimetypeInCategory($mime , $category);
}
/**
* Get the file extension
*/
function get_file_extension_from_expectations($filename, $category) {
return MimeTypes::instance()->getFileExtensionFromExpectations($filename, $category);
}
function is_image($filepath) {
return is_file_in_category($filepath, 'image');
}
function is_audio($filepath) {
return is_file_in_category($filepath, 'audio');
}
function is_video($filepath) {
return is_file_in_category($filepath, 'video');
}
function is_document($filepath) {
return is_file_in_category($filepath, 'document');
}
function is_closure($t) {
return is_object($t) && ($t instanceof Closure);
}
/*
* From http://php.net/manual/en/features.file-upload.php#88591
*/
function human_filesize($filesize, $separator = ' '){
if( ! is_numeric($filesize) ) {
return __("NaN");
}
$decr = 1024;
$step = 0;
$prefix = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB'];
while(($filesize / $decr) > 0.9) {
$filesize /= $decr;
$step++;
}
return round($filesize, 2) . $separator . $prefix[$step];
}
/*
* Create a pathname in the filesystem
*/
function create_path($path, $chmod = CHMOD_WRITABLE_DIRECTORY) {
if( file_exists($path) || mkdir($path, $chmod, true) ) {
return true;
}
error( "unable to create path $path" );
return false;
}
/**
* @see FileUploader::searchFreeFilename()
*/
function search_free_filename( $filepath, $filename, $ext, $args, $build_filename = null ) {
return FileUploader::searchFreeFilename( $filepath, $filename, $ext, $args, $build_filename = null );
}
/**
* I use this to clean user input before DB#insert()
*
* @param string $s Input string
* @param int $max Max length
*/
function luser_input($s, $max) {
return mb_strimwidth( trim($s), 0, $max, '');
}
/**
* Used to know much is the page load
*
* @return mixed Execution time
* @deprecated
*/
function get_page_load($decimals = 6) {
static $start_microtime = 0; // Please let me take advantage of PHP features
if($start_microtime == 0) {
$start_microtime = microtime(true);
}
return substr(microtime(true) - $start_microtime, 0, 2 + $decimals);
}