' . print_r($variable, true) . ''; } if($echo === true) echo $output; return $output; } /** * Generates a single attribute or a list of attributes * * @see html::attr(); * @param string $name mixed string: a single attribute with that name will be generated. array: a list of attributes will be generated. Don't pass a second argument in that case. * @param string $value if used for a single attribute, pass the content for the attribute here * @return string the generated html */ function attr($name, $value = null) { return html::attr($name, $value); } /** * Creates safe html by encoding special characters * * @param string $text unencoded text * @param bool $keepTags * @return string */ function html($text, $keepTags = true) { return html::encode($text, $keepTags); } /** * Shortcut for html() * * @see html() * @param $text * @param bool $keepTags * @return string */ function h($text, $keepTags = true) { return html::encode($text, $keepTags); } /** * Shortcut for xml::encode() * * @param $text * @return string */ function xml($text) { return xml::encode($text); } /** * Escape context specific output * * @param string $string Untrusted data * @param string $context Location of output * @param boolean $strict Whether to escape an extended set of characters (HTML attributes only) * @return string Escaped data */ function esc($string, $context = 'html', $strict = false) { if (method_exists('escape', $context)) { return escape::$context($string, $strict); } } /** * The widont function makes sure that there are no * typographical widows at the end of a paragraph – * that's a single word in the last line * * @param string $string * @return string */ function widont($string = '') { return str::widont($string); } /** * Convert a text to multiline text * * @param string $text * @return string */ function multiline($text) { return nl2br(html($text)); } /** * Returns the memory usage in a readable format * * @return string */ function memory() { return f::niceSize(memory_get_usage()); } /** * Determines the size/length of numbers, strings, arrays and files * * @param mixed $value * @return int */ function size($value) { if(is_numeric($value)) return $value; if(is_string($value)) return str::length(trim($value)); if(is_array($value)) return count($value); if(f::exists($value)) return f::size($value) / 1024; } /** * Generates a gravatar image link * * @param string $email * @param int $size * @param string $default * @return string */ function gravatar($email, $size = 256, $default = 'mm') { return 'https://gravatar.com/avatar/' . md5(strtolower(trim($email))) . '?d=' . urlencode($default) . '&s=' . $size; } /** * Checks / returns a csrf token * * @param string $check Pass a token here to compare it to the one in the session * @return mixed Either the token or a boolean check result */ function csrf($check = null) { // make sure a session is started s::start(); if(is_null($check)) { $token = str::random(64); s::set('csrf', $token); return $token; } return ($check === s::get('csrf')) ? true : false; } /** * Facepalm typo alias * @see csrf() */ function csfr($check = null) { return csrf($check); } /** * Shortcut for call_user_func_array with a better handling of arguments * * @param mixed $function * @param mixed $arguments * @return mixed */ function call($function, $arguments = array()) { if(!is_callable($function)) return false; if(!is_array($arguments)) $arguments = array($arguments); return call_user_func_array($function, $arguments); } /** * Parses yaml structured text * * @param $string * @return array */ function yaml($string) { return yaml::decode($string); } /** * Simple email sender helper * * @param array $params * @return Email */ function email($params = array()) { return new Email($params); } /** * Shortcut for the upload class * * @param $to * @param array $params * @return Upload */ function upload($to, $params = array()) { return new Upload($to, $params); } /** * Checks for invalid data * * @param array $data * @param array $rules * @param array $messages * @return mixed */ function invalid($data, $rules, $messages = array()) { $errors = array(); foreach($rules as $field => $validations) { foreach($validations as $method => $options) { if(is_numeric($method)) $method = $options; if($method == 'required') { if(!isset($data[$field]) || (empty($data[$field]) && $data[$field] !== 0)) { $errors[$field] = a::get($messages, $field, $field); } } else if(!empty($data[$field]) || $data[$field] === 0) { if(!is_array($options)) $options = array($options); array_unshift($options, a::get($data, $field)); if(!call(array('v', $method), $options)) { $errors[$field] = a::get($messages, $field, $field); } } } } return array_unique($errors); } /** * Shortcut for the language variable getter * * @param string $key * @param mixed $default * @return string */ function l($key, $default = null) { return l::get($key, $default); } /** * @param $tag * @param bool $html * @param array $attr * @return Brick */ function brick($tag, $html = false, $attr = array()) { return new Brick($tag, $html, $attr); }