function __autoload($class) { // a more traditional autoloader - used for loading in controllers and models $maverick = \maverick\maverick::getInstance(); $class_found = false; foreach ($maverick->get_config('config.paths') as $path) { if (file_exists("{$path}/{$class}.php")) { require_once "{$path}/{$class}.php"; $class_found = true; break; } } // add the maverick directory in as the namespace if it doesn't exist if (strpos($class, '\\') === false) { $class = 'maverick/' . $class; } // PSR-0 autoloader - sourced from http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ if (!$class_found) { $className = ltrim($class, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strripos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } //$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; // this is breaking cases where a function name is using underscores instead of camel case $fileName .= $className . '.php'; set_include_path(MAVERICK_BASEDIR . 'vendor'); require_once $fileName; } }
/** * static method used to log an error and optionally show it also * @param string $message the error message to show * @param bool $show whether or not to also show the error on screen * @param int $http_code the HTTP status code to respond with */ static function log($message, $show = false, $http_code = 500) { $maverick = \maverick\maverick::getInstance(); //$caller = debug_backtrace(); if ($maverick->get_config('config.error_detail')) { $message .= error::generate_call_trace(); } if ($maverick->get_config('config.log_errors')) { $log_date = date("y-m-d"); error_log("\n\n{$message}", 3, MAVERICK_LOGSDIR . "error-{$log_date}.log"); } if ($show) { error::show(nl2br($message), $http_code); } }
/** * clear all caches held for the cache type specified in the cache config file */ static function clear() { $app = \maverick\maverick::getInstance(); // if apc is selected as the mechanism, but not available on the system, switch to file-based caching if ($app->get_config('cache.type') == 'apc' && function_exists('apc_fetch')) { apc_clear_cache('user'); } else { $cache_dir = MAVERICK_BASEDIR . "cache"; if ($dh = opendir($cache_dir)) { while (false !== ($file = readdir($dh))) { if (substr($file, 0, 1) != '.') { unlink("{$cache_dir}/{$file}"); } } } } }
/** * match the requested route with one of the routes added to the routes array on the main maverick class * @param string $protocol the protocol specified in the routes, either post, get, or any * @param string $route the route string * @param string $action the action to take, usually in the form of controller->method * @param array $args an array of matched parameters to pass to the routing controller class * @return boolean */ private static function match_route($protocol, $route, $action, $args) { $maverick = \maverick\maverick::getInstance(); // return if a route has already been matched if (isset($maverick->route)) { return false; } // return if the protocols don't match if (strtolower($_SERVER['REQUEST_METHOD']) == $protocol || $protocol == 'any') { preg_match("~^{$route}\$~", $maverick->requested_route_string, $matches); // route match - assign the information back to the main maverick object if (!empty($matches)) { $maverick->controller = route::get_full_action($action, $args, $matches); } } else { return false; } }
public static function get($var) { $app = \maverick\maverick::getInstance(); if (strpos($var, '.') !== false) { $matches = explode('.', $var); if (count($matches) > 1) { $v = $app->view->get_data($matches[0]); array_shift($matches); foreach ($matches as $item) { if (isset($v[$item])) { $var = $v[$item]; $v = $v[$item]; } else { break; } } } } else { $var = $app->view->get_data($var); } return $var; }
<?php use maverick\route; $app = \maverick\maverick::getInstance(); // admin routes route::any("{$app->get_config('tweed.admin_path')}(/[^/]+)?(/.+)?", 'admin_controller->admin', array('$1', '$2')); route::any("{$app->get_config('tweed.admin_path')}/login", 'admin_controller->login'); route::any("{$app->get_config('tweed.admin_path')}/logout", 'admin_controller->logout'); route::post("{$app->get_config('tweed.admin_path')}/tweets/(approve|unapprove)/([\\d]+)", 'admin_controller->update_tweet_status', array('$1', '$2')); route::get('campaigns', 'main_controller->get_campaigns'); // reserved for the cron to call //route::any('update/([a-z0-9]+)', 'main_controller->update', '$1'); // reserved for the cron to call route::any('get/([a-z0-9]+)', 'main_controller->get_tweets', '$1'); // the route for fetching tweets from the service by campaign route::any('', 'main_controller->home'); route::error('404', 'error'); route::error('500', 'error');
/** * builds a query, executes it, and returns the results of the query * @param string $type the type of query being executed * @return boolean */ private function result($type) { $maverick = \maverick\maverick::getInstance(); $q = query::getInstance(); if (!strlen($q->from)) { return false; } $from = "FROM {$q->from}"; list($join_string, $join_params) = $q->compile_joins($q->joins); list($where_string, $where_params) = $q->compile_wheres($q->wheres); list($group_by_string, $group_by_params) = $q->compile_group_bys($q->group_bys); list($order_by_string, $order_by_params) = $q->compile_order_bys($q->order_bys); switch ($type) { case 'select': $select_string = implode(',', $q->gets); $params = array_merge($join_params, $where_params, $group_by_params, $order_by_params); $limit_string = $q->limit ? " LIMIT {$q->limit['offset']}, {$q->limit['results']}" : ''; $stmt = $maverick->db->pdo->prepare("SELECT {$select_string} {$from} {$join_string} {$where_string} {$group_by_string} {$order_by_string} {$limit_string}"); break; case 'insert': list($insert_string, $params) = $q->compile_inserts($q->data); $stmt = $maverick->db->pdo->prepare("INSERT INTO {$q->from} {$insert_string}"); break; case 'delete': $params = $where_params; $stmt = $maverick->db->pdo->prepare("DELETE FROM {$q->from} {$where_string}"); break; case 'update': list($update_string, $update_params) = $q->compile_updates($q->data); $params = array_merge($update_params, $where_params); $stmt = $maverick->db->pdo->prepare("UPDATE {$q->from} SET {$update_string} {$where_string}"); break; case 'insert_on_duplicate_key_update': list($insert_string, $insert_params) = $q->compile_inserts($q->data_ins); list($update_string, $update_params) = $q->compile_updates($q->data_up); $params = array_merge($insert_params, $update_params); //$update_str = "INSERT INTO {$q->from} $insert_string ON DUPLICATE KEY UPDATE {$q->gets[0]}=LAST_INSERT_ID({$q->gets[0]}), $update_string"; $stmt = $maverick->db->pdo->prepare("INSERT INTO {$q->from} {$insert_string} ON DUPLICATE KEY UPDATE {$q->gets[0]}=LAST_INSERT_ID({$q->gets[0]}), {$update_string}"); break; } if ($stmt->execute($params)) { switch ($type) { case 'select': $results = array(); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $results[] = $row; } break; case 'insert': case 'insert_on_duplicate_key_update': $results = $maverick->db->pdo->lastInsertId(); break; default: $results = true; } } else { $results = false; } $q->numrows = count($results); $q->queries[] = array('query' => $stmt->queryString, 'params' => $params); $q->results = $results; }
/** * run the rules attached to this instance and apply them to the submitted data * populating the internal errors array with any errors found * @return bool */ public static function run() { $v = validator::getInstance(); $app = \maverick\maverick::getInstance(); // run through the rules and apply them to any data that exists in the $_REQUEST array foreach ($v->rules as $field => $rules) { if (!is_array($rules)) { $rules = (array) $rules; } // cast the single string rules to an array to make them the same to work with as groups of rules foreach ($rules as $rule) { $params = explode(':', $rule); $rule_method = "rule_{$params[0]}"; $rule = $params[0]; if (method_exists($v, $rule_method)) { $params = array_slice($params, 1); if (!$v->{$rule_method}($field, $params)) { // look up an error message for this and push it into the errors array $error_string = vsprintf($app->get_config("validator.{$rule}"), array_merge((array) $field, $params)); if (!isset($v->errors[$field])) { $v->errors[$field] = array(); } $v->errors[$field][] = $error_string; } } else { error::show("the validation rule {$params[0]} does not exist"); } } } return !count($v->errors); }
function login() { $app = \maverick\maverick::getInstance(); if (isset($_POST['username']) && isset($_POST['password'])) { $login = content::check_login($_POST['username'], $_POST['password']); if ($login) { $_SESSION['maverick_login'] = true; $_SESSION['maverick_id'] = $login; $app = \maverick\maverick::getInstance(); view::redirect('/' . $app->get_config('tweed.admin_path') . '/'); } } $elements = '{ "username":{"type":"text","label":"Username","validation":["required"]}, "password":{"type":"password","label":"Password","validation":["required"]}, "submit":{"type":"submit","value":"Login"} }'; $form = new \helpers\html\form('login', $elements); $form->method = 'post'; $view = view::make('includes/template_basic')->with('page', 'login')->with('login_form', $form->render())->render(); }
/** * parse the rendered view for {{placeholders}} and replace them * @param string $view the rendered view (typically html) possibly containing {{placeholders}} * @return string */ private function parse_view($view) { $v = view::getInstance(); $app = \maverick\maverick::getInstance(); // check for the use of multi-lingual gettext stuff if ($app->get_config('lang.active') !== false) { // match the format of {{_('string to translate')}} - the quote style can be either single or double, // and the text to translate must start with a letter \p{L} if (preg_match_all('/\\{\\{_\\(([\'"]\\p{L}[^\'"]+[\'"])\\)\\}\\}/', $view, $matches) && !empty($matches[1])) { $find = $replace = array(); foreach ($matches[1] as $match) { $find[] = "{{_({$match})}}"; $replace[] = _(trim($match, "\"'")); } $view = str_replace($find, $replace, $view); } } // run any custom handlers that have been added - the callback will pass up to three matched parameters in the form // {{handler_namespacee:param1:param2:param3}} with each matched parameter being in an array with // param1 being array index 1, param2 being array index 3, and param3 being array index5 // the callback method must be a static method, and the return must be a string if (count($v->parse_handlers)) { foreach ($v->parse_handlers as $parse_handler) { list($controller, $method) = explode('->', $parse_handler[1]); $view = preg_replace_callback("/\\{\\{{$parse_handler[0]}:([\\p{L}\\p{N}_]+)(:([\\p{L}\\p{N}_]+))?(:([\\p{L}\\p{N}_]+))?\\}\\}/", array($controller, $method), $view); } } // match simple placeholder formats - this check should always be last if (preg_match_all('/\\{\\{(\\p{L}[\\p{L}\\p{N}_\\.]+)/', $view, $matches) && !empty($matches[1])) { $find = $replace = array(); foreach ($matches[1] as $match) { $find[] = "{{{$match}}}"; $r = \data::get($match); if (is_array($r)) { $r = implode($r); } $replace[] = $r; } $view = str_replace($find, $replace, $view); } return $view; }
/** * deals with the creation of action buttons (links) used throughout the CMS to do something * @param string $section the section, as all links will contain this in their URL * @param int $id the ID of the object being worked on * @param array $actions a basic single dimensional array of single-word actions, that go into the URL and the text of the link * @param string $extra_classes a string of extra classes that should be added to each button * @param string $type the type of element to use, either a button or a link * @return string */ static function generate_actions($section, $id, $actions = array(), $extra_classes = '', $type = 'link') { if (empty($actions) || empty($section)) { return ''; } $app = \maverick\maverick::getInstance(); $type = in_array($type, array('link', 'button')) ? $type : 'link'; $actions_html = ''; foreach ($actions as $action) { $replacements = array('href' => str_replace(' ', '_', "/{$app->get_config('tweed.admin_path')}/{$section}/{$action}/{$id}"), 'action' => $action, 'id' => $id, 'section' => $section, 'class' => str_replace(' ', '_', $action) . " {$extra_classes}"); $actions_html .= \helpers\html\html::load_snippet(MAVERICK_VIEWSDIR . "includes/snippets/action_{$type}.php", $replacements); } return $actions_html; }