/** * ========================================================== * DATE AGO CALCULATOR * ========================================================== * * -- CODE: ------------------------------------------------- * * $input = '2014-05-30 09:22:42'; * * var_dump(Date::ago($input)); * * ---------------------------------------------------------- * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Parameter | Type | Description * --------- | ------- | ----------------------------------- * $input | mixed | The date input * $output | string | Optional to output single data * $compact | boolean | Remove empty leading offset(s)? * --------- | ------- | ----------------------------------- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * */ public static function ago($input, $output = null, $compact = true) { $speak = Config::speak(); $date = new DateTime(); $date->setTimestamp((int) self::format($input, 'U')); $interval = $date->diff(new DateTime('now')); $time = $interval->format('%y.%m.%d.%h.%i.%s'); $time = explode('.', $time); $time = Converter::strEval($time); $data = array('year' => $time[0], 'month' => $time[1], 'day' => $time[2], 'hour' => $time[3], 'minute' => $time[4], 'second' => $time[5]); if ($compact) { foreach ($data as $k => $v) { if ($v === 0) { unset($data[$k]); } else { break; } } } $results = array(); foreach ($data as $k => $v) { $text = array($speak->{$k}, $speak->{$k . 's'}); $results[$k] = $v . ' ' . ($v === 1 ? $text[0] : $text[1]); } unset($data); return !is_null($output) ? $results[$output] : $results; }
/** * ========================================================== * DATE AGO CALCULATOR * ========================================================== * * -- CODE: ------------------------------------------------- * * $input = '2014-05-30 09:22:42'; * * var_dump(Date::ago($input)); * * ---------------------------------------------------------- * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Parameter | Type | Description * --------- | ------- | ----------------------------------- * $input | mixed | The date input * $output | string | Optional to output single data * $compact | boolean | Remove empty leading offset(s)? * --------- | ------- | ----------------------------------- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * */ public static function ago($input, $output = null, $compact = true) { $speak = Config::speak(); $date = new DateTime(); $date->setTimestamp((int) self::format($input, 'U')); $interval = $date->diff(new DateTime('now')); $time = $interval->format('%y.%m.%d.%h.%i.%s'); $time = explode('.', $time); $time = Converter::strEval($time); $data = array($speak->year . '|' . $speak->years => $time[0], $speak->month . '|' . $speak->months => $time[1], $speak->day . '|' . $speak->days => $time[2], $speak->hour . '|' . $speak->hours => $time[3], $speak->minute . '|' . $speak->minutes => $time[4], $speak->second . '|' . $speak->seconds => $time[5]); if ($compact) { foreach ($data as $k => $v) { if ($offset === 0) { unset($data[$k]); } else { break; } } } $results = array(); foreach ($data as $k => $v) { $text = explode('|', $k); $results[strtolower($text[0])] = $v . ' ' . ($v === 1 ? $text[0] : $text[1]); } unset($data); return !is_null($output) ? $results[$output] : $results; }
public static function get($param = null, $fallback = false, $str_eval = true) { if (is_null($param)) { return $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET) && !empty($_GET) ? $str_eval ? Converter::strEval($_GET, false) : $_GET : $fallback; } $output = Mecha::GVR($_GET, $param, $fallback); return $output === '0' || !empty($output) ? $str_eval ? Converter::strEval($output, false) : $output : $fallback; }
/** * ============================================================ * GET ACCEPTED USER(S) DATA * ============================================================ * * -- CODE: --------------------------------------------------- * * echo Guardian::ally('mecha'); * * ------------------------------------------------------------ * * var_dump(Guardian::ally()); * * ------------------------------------------------------------ * */ public static function ally($user = null, $fallback = false) { if ($file = File::exist(LOG . DS . 'users.txt')) { $ally = array(); foreach (explode("\n", file_get_contents($file)) as $str) { $s = trim($str); // serialized array if (strpos($s, 'a:') === 0) { $s = unserialize($s); // encoded JSON array } else { if (strpos($s, '{"') === 0) { $s = json_decode($s, true); // Pattern 1: `user: pass (Author Name: status) email@domain.com` // Pattern 2: `user: pass (Author Name $status) email@domain.com` } else { if (preg_match('#^(.*?)\\:\\s*(.*?)\\s+\\((.*?)(?:\\s*\\$|\\:\\s*)(pilot|[a-z0-9_.]+)\\)(?:\\s+(.*?))?$#', $s, $matches)) { $s = array(); $s['user'] = $matches[1]; $s['pass'] = $matches[2]; $s['name'] = $matches[3]; $s['status'] = $matches[4]; $s['email'] = isset($matches[5]) && !empty($matches[5]) ? $matches[5] : false; } else { self::abort('Broken <code>' . LOG . DS . 'users.txt</code> format.'); } } } foreach ($s as $k => $v) { $v = Converter::strEval($v); $s[$k . '_raw'] = Filter::colon('user:'******'_raw', $v, $s); $s[$k] = Filter::colon('user:'******'user']] = Filter::apply('user', $s, $s['user']); } $ally = Filter::apply('users', $ally); if (is_null($user)) { return $ally; } return isset($ally[$user]) ? $ally[$user] : $fallback; } else { self::abort('Missing <code>' . LOG . DS . 'users.txt</code> file.'); } }
/** * =========================================================================== * CHECK FOR ROUTE PATTERN MATCH * =========================================================================== * * -- CODE: ------------------------------------------------------------------ * * if(Route::is('foo/bar')) { ... } * * --------------------------------------------------------------------------- * */ public static function is($pattern, $fallback = false) { $pattern = self::path($pattern); $path = Config::get('url_path'); if (strpos($pattern, '(') === false) { return $path === $pattern ? array('pattern' => $pattern, 'path' => $path, 'lot' => array()) : $fallback; } if (preg_match('#^' . self::fix($pattern) . '$#', $path, $matches)) { array_shift($matches); return array('pattern' => $pattern, 'path' => $path, 'lot' => Converter::strEval($matches)); } return $fallback; }
/** * ========================================================================== * GET LIST OF PAGE DETAIL(S) * ========================================================================== * * -- CODE: ----------------------------------------------------------------- * * var_dump(Get::pageExtract($input)); * * -------------------------------------------------------------------------- * */ public static function pageExtract($input, $FP = 'page:') { if (!$input) { return false; } $extension = File::E($input); $update = File::T($input); $update_date = !is_null($update) ? date('Y-m-d H:i:s', $update) : null; list($time, $kind, $slug) = explode('_', File::N($input), 3); $kind = explode(',', $kind); return array('path' => self::AMF($input, $FP, 'path'), 'id' => self::AMF((int) Date::format($time, 'U'), $FP, 'id'), 'time' => self::AMF(Date::format($time), $FP, 'time'), 'update_raw' => self::AMF($update, $FP, 'update_raw'), 'update' => self::AMF($update_date, $FP, 'update'), 'kind' => self::AMF(Converter::strEval($kind), $FP, 'kind'), 'slug' => self::AMF($slug, $FP, 'slug'), 'state' => self::AMF($extension !== 'draft' ? 'published' : 'draft', $FP, 'state')); }
public static function kill($session = null) { if (is_null($session)) { session_destroy(); } else { if ($session === 'cookies') { $_COOKIE = array(); if (isset($_SERVER['HTTP_COOKIE'])) { $cookies = explode(';', $_SERVER['HTTP_COOKIE']); foreach ($cookies as $cookie) { $parts = explode('=', $cookie, 2); $name = trim($parts[0]); setcookie($name, null, -1); setcookie($name, null, -1, '/'); } } } else { if (strpos($session, 'cookie:') === 0) { $name = substr($session, 7); if (strpos($session, '.') !== false) { $name_a = explode('.', $name); if (isset($_COOKIE[$name_a[0]])) { $old = Converter::strEval($_COOKIE[$name_a[0]]); Mecha::UVR($old, $name); foreach ($old as $key => $value) { $_COOKIE[$name_a[0]][$key] = is_array($value) ? base64_encode(json_encode($value, true)) : $value; } $c = Converter::strEval($_COOKIE['__' . $name_a[0]]); setcookie($name_a[0], base64_encode(json_encode($old, true)), $c[0], $c[1], $c[2], $c[3], $c[4]); } } else { unset($_COOKIE[$name]); self::set($session, null, -1); } } else { Mecha::UVR($_SESSION, $session); } } } }
/** * ========================================================================== * GET LIST OF POST DETAIL(S) * ========================================================================== * * -- CODE: ----------------------------------------------------------------- * * var_dump(Get::postExtract($input)); * * -------------------------------------------------------------------------- * */ public static function postExtract($input, $FP = 'post:') { if (!$input) { return false; } $e = File::E($input); $update = File::T($input); $update_date = !is_null($update) ? date('Y-m-d H:i:s', $update) : null; list($time, $kind, $slug) = explode('_', File::N($input), 3); $kind = $kind !== "" ? explode(',', $kind) : array(); return array('path' => Filter::colon($FP . 'path', $input, $input), 'id' => Filter::colon($FP . 'id', (int) Date::format($time, 'U'), $input), 'time' => Filter::colon($FP . 'time', Date::format($time), $input), 'update_raw' => Filter::colon($FP . 'update_raw', $update, $input), 'update' => Filter::colon($FP . 'update', $update_date, $input), 'kind' => Filter::colon($FP . 'kind', Converter::strEval($kind), $input), 'slug' => Filter::colon($FP . 'slug', $slug, $input), 'state' => Filter::colon($FP . 'state', Mecha::alter($e, array('txt' => 'published', 'draft' => 'drafted', 'archive' => 'archived')), $input)); }
/** * ========================================================== * DATE AGO CALCULATOR * ========================================================== * * -- CODE: ------------------------------------------------- * * $input = '2014-05-30 09:22:42'; * * var_dump(Date::ago($input)); * * ---------------------------------------------------------- * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Parameter | Type | Description * --------- | ------- | ----------------------------------- * $date | mixed | The date input * $compact | boolean | Remove empty leading offset(s)? * --------- | ------- | ----------------------------------- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * */ public static function ago($input, $compact = true) { $speak = Config::speak(); $date = new DateTime(); $date->setTimestamp((int) self::format($input, 'U')); $interval = $date->diff(new DateTime('now')); $time = $interval->format('%y.%m.%d.%h.%i.%s'); $time = explode('.', $time); $time = Converter::strEval($time); $data = array($speak->year . '/' . $speak->year_p => $time[0], $speak->month . '/' . $speak->month_p => $time[1], $speak->day . '/' . $speak->day_p => $time[2], $speak->hour . '/' . $speak->hour_p => $time[3], $speak->minute . '/' . $speak->minute_p => $time[4], $speak->second . '/' . $speak->second_p => $time[5]); if ($compact) { foreach ($data as $name => $offset) { if ($offset === 0) { unset($data[$name]); } else { break; } } } $output = array(); foreach ($data as $name => $offset) { $name = explode('/', $name); $output[strtolower($name[0])] = $offset . ' ' . ($offset === 1 ? $name[0] : $name[1]); } return $output; }
protected static function __doParseHeaders($text, $FP, $data) { $results = array(); $headers = explode("\n", trim($text)); foreach ($headers as $header) { $field = explode(S, $header, 2); if (!isset($field[1])) { $field[1] = 'false'; } $key = Text::parse(trim($field[0]), '->array_key', true); $value = Converter::DS(trim($field[1])); $value = Filter::colon($FP . $key . '_raw', Converter::strEval($value), $data); $results[$key . '_raw'] = $value; $value = Filter::colon($FP . $key, $value, $data); $results[$key] = $value; } return $results; }
return call_user_func('Get::' . $segment . 'Header', $v); }); } else { if ($offset !== 1) { Shield::abort(); } } Config::set(array('page_title' => $speak->{$segment . 's'} . $config->title_separator . $config->manager->title, 'pages' => $posts, 'offset' => $offset, 'pagination' => Navigator::extract($s, $offset, $config->manager->per_page, $config->manager->slug . '/' . $segment), 'cargo' => 'cargo.post.php')); Shield::lot(array('segment' => $segment))->attach('manager'); }); /** * Post Repairer/Igniter * --------------------- */ Route::accept(array($config->manager->slug . '/(' . $post . ')/ignite', $config->manager->slug . '/(' . $post . ')/repair/id:(:num)'), function ($segment = "", $id = false) use($config, $speak, $response) { $id = Converter::strEval($id); $units = array('title', 'slug', 'link', 'content', 'content_type', 'description', 'post' . DS . 'author'); foreach ($units as $k => $v) { Weapon::add('tab_content_1_before', function ($page, $segment) use($config, $speak, $v) { include __DIR__ . DS . 'unit' . DS . 'form' . DS . $v . '.php'; }, $k + 1); } $units = array('css', 'js'); foreach ($units as $k => $v) { Weapon::add('tab_content_2_before', function ($page, $segment) use($config, $speak, $v) { include __DIR__ . DS . 'unit' . DS . 'form' . DS . $v . '.php'; }, $k + 1); } Weapon::add('tab_content_3_before', function ($page, $segment) use($config, $speak) { include __DIR__ . DS . 'unit' . DS . 'form' . DS . 'fields[].php'; }, 1);
/** * ===================================================================== * READ A TEXT FILE AS AN ASSOCIATIVE ARRAY * ===================================================================== * * -- CODE: ------------------------------------------------------------ * * var_dump(Text::toArray("key 1: value 1\nkey 2: value 2")); * * --------------------------------------------------------------------- * */ public static function toArray($text, $s = S, $indent = ' ') { if (is_array($text)) { return $text; } if (is_object($text)) { return Mecha::A($text); } $results = array(); $data = array(); $indent_length = strlen($indent); // Remove comment(s) and empty line break(s) $text = preg_replace(array('#\\r#', '#(^|\\n)\\s*\\#[^\\n]*#', '#\\n+#', '#^\\n+|\\n+$#'), array("", "\n", "\n", ""), $text); foreach (explode("\n", $text) as $line) { $depth = 0; // No `:` ... fix it! if (strpos($line, $s) === false) { $line .= $s . $line; } while (substr($line, 0, $indent_length) === $indent) { $depth += 1; $line = rtrim(substr($line, $indent_length)); } while ($depth < count($data)) { array_pop($data); } $parts = explode($s, $line, 2); $data[$depth] = rtrim($parts[0]); $parent =& $results; foreach ($data as $depth => $key) { if (!isset($parent[$key])) { $value = isset($parts[1]) && !empty($parts[1]) ? preg_replace('#^`|`$#', "", trim($parts[1])) : array(); $parent[rtrim($parts[0])] = Converter::strEval($value); break; } $parent =& $parent[$key]; } } return $results; }
function do_snippet($content) { global $config, $speak; if (strpos($content, '{{') === false) { return $content; } $content = function_exists('do_shortcode_x') ? do_shortcode_x($content) : $content; // plain text: `{{print:foo}}` if (strpos($content, '{{print:') !== false || strpos($content, '{{print=') !== false) { $content = preg_replace_callback('#\\{\\{print[:=](.*?)\\}\\}#', function ($matches) { $content = $matches[0]; $e = File::E($matches[1], false); if ($e !== 'txt' && $e !== 'php') { $e = 'txt'; $matches[1] .= '.txt'; } if ($snippet = File::exist(ASSET . DS . '__snippet' . DS . $e . DS . $matches[1])) { return File::open($snippet)->read(); } return $content; }, $content); } // plain text with wildcard(s): `{{print path="foo" lot="bar,baz,qux"}}` if (strpos($content, '{{print ') !== false) { $content = preg_replace_callback('#\\{\\{print\\s+(.*?)\\}\\}#', function ($matches) { $content = $matches[0]; $data = Converter::attr($content, array('{{', '}}', ' '), array('"', '"', '=')); $attr = (array) $data['attributes']; if (!isset($attr['path'])) { return $matches[0]; } $e = File::E($attr['path'], false); if ($e !== 'txt' && $e !== 'php') { $e = 'txt'; $attr['path'] .= '.txt'; } if (!($snippet = File::exist(ASSET . DS . '__snippet' . DS . $e . DS . $attr['path']))) { return $matches[0]; } $content = File::open($snippet)->read(); if (isset($attr['lot']) && strpos($content, '%') !== false) { // `http://stackoverflow.com/a/2053931` if (preg_match_all('#%(?:(\\d+)[$])?[-+]?(?:[ 0]|[\'].)?(?:[-]?\\d+)?(?:[.]\\d+)?[%bcdeEfFgGosuxX]#', $content, $matches)) { $lot = Mecha::walk(explode(',', $attr['lot']), function ($v) { return str_replace(',', ',', $v); }); if (count($lot) >= count(array_unique($matches[1]))) { $content = vsprintf($content, $lot); } } } return $content; }, $content); } // executable code: `{{include:foo}}` if (strpos($content, '{{include:') !== false || strpos($content, '{{include=') !== false) { $content = preg_replace_callback('#\\{\\{include[:=](.*?)\\}\\}#', function ($matches) { $content = $matches[0]; $e = File::E($matches[1], false); if ($e !== 'php') { $e = 'php'; $matches[1] .= '.php'; } if ($snippet = File::exist(ASSET . DS . '__snippet' . DS . $e . DS . $matches[1])) { ob_start(); include $snippet; $content = ob_get_clean(); } return $content; }, $content); } // executable code with variable(s): `{{include path="foo" lot="bar,baz,qux" another_var="1"}}` if (strpos($content, '{{include ') !== false) { $content = preg_replace_callback('#\\{\\{include\\s+(.*?)\\}\\}#', function ($matches) { $content = $matches[0]; $data = Converter::attr($content, array('{{', '}}', ' '), array('"', '"', '=')); $attr = (array) $data['attributes']; if (!isset($attr['path'])) { return $matches[0]; } $e = File::E($attr['path'], false); if ($e !== 'php') { $e = 'php'; $attr['path'] .= '.php'; } if ($snippet = File::exist(ASSET . DS . '__snippet' . DS . $e . DS . $attr['path'])) { ob_start(); if (isset($attr['lot'])) { $lot = Mecha::walk(explode(',', str_replace('\\,', ',', $attr['lot'])), function ($v) { return Converter::strEval(str_replace(',', ',', $v)); }); } else { $lot = array(); } unset($attr['path'], $attr['lot']); extract($attr); include $snippet; $content = ob_get_clean(); } return $content; }, $content); } return $content; }