public function redirect() { $uri = URI::instance()->string(); $routed_uri = false; $bounces = Kohana::config('redirect_301'); if (is_null(Router::$controller) and is_array($bounces)) { foreach ($bounces as $key => $val) { // Trim slashes $key = trim($key, '/'); $val = trim($val, '/'); if (preg_match('#^' . $key . '$#u', $uri)) { if (strpos($val, '$') !== FALSE) { // Use regex routing $routed_uri = preg_replace('#^' . $key . '$#u', $val, $uri); } else { // Standard routing $routed_uri = $val; } // A valid route has been found break; } } } if ($routed_uri !== false) { url::redirect($routed_uri, '301'); } }
/** * Hook to prepare the database connection. Performs 2 tasks. * 1) Initialises the search path, unless configured not to do this (e.g. if this is set at db level). * 2) If this is a report request, sets the connection to read only. */ public static function prepare_connection() { $uri = URI::instance(); // we havent to proceed futher if a setup call was made if ($uri->segment(1) == 'setup_check') { return; } // continue to init the system // // add schema to the search path // $_schema = Kohana::config('database.default.schema'); $query = ''; if (!empty($_schema) && kohana::config('indicia.apply_schema') !== false) { $query = "SET search_path TO {$_schema}, public, pg_catalog;\n"; } // Force a read only connection for reporting. if ($uri->segment(1) == 'services' && $uri->segment(2) == 'report') { $query .= "SET default_transaction_read_only TO true;\n"; } if (!empty($query)) { $db = Database::instance(); $db->query($query); } }
/** * Loads URI, and Input into this controller. * * @return void */ public function __construct() { if (Kohana::$instance == NULL) { // Set the instance to the first controller loaded Kohana::$instance = $this; } // URI should always be available $this->uri = URI::instance(); // Input should always be available $this->input = Input::instance(); $this->db = new Database(); $this->user_model = ORM::factory('user'); $this->supplier_model = ORM::factory('supplier'); $this->category_model = ORM::factory('category'); $this->item_model = ORM::factory('item'); $this->item_stock_model = ORM::factory('item_stock'); $this->district_model = ORM::factory('district'); $this->office_model = ORM::factory('office'); $this->budget_model = ORM::factory('office_budget'); $this->purchase_model = ORM::factory('purchase'); $this->request_model = ORM::factory('request'); $this->transaction_model = ORM::factory('transaction'); $this->setting_model = ORM::factory('setting'); $this->log_model = ORM::factory('log'); $this->email_model = ORM::factory('email'); }
public function container() { $id = URI::instance()->segment(3, false); $p = ORM::factory('plugin_instance', $id); if ($p->loaded) { echo PluginManager::instance()->get_plugin($p)->render(); } }
/** * If not in the setup pages, but the indicia config file is missing, go to system setup. */ public static function _setup_check() { $uri = URI::instance(); $isOk = $uri->segment(1) == 'setup' || $uri->segment(1) == 'setup_check' || kohana::config('indicia.private_key', false, false) !== null; if (!$isOk) { url::redirect('setup_check'); } }
function error_404() { if (Kohana::$instance == NULL) { Kohana::$instance = new Errors_Controller(); } Kohana::log('error', 'Error 404 (steam.core.hooks): ' . URI::instance()->string()); Kohana::show_404(null, 'common/error_404'); }
/** * constructor, set up col data * * @author Andy Bennett */ function __construct() { $this->container_blocks = array(); $this->col_data = array('leftcolumn' => '', 'rightcolumn' => '', 'header' => '', 'footer' => '', 'history' => array(), 'show' => array(), 'extraHeadContent' => '', 'page_id' => URI::instance()->segment(1)); $this->col_data['cssfiles'] = array(); $this->col_data['jsfiles'] = array(); // Singleton instance self::$instance = $this; }
/** * return type * * @return string * @author Andy Bennett */ public static function get_type() { $types = Kohana::config('sitemap.types'); $type = URI::instance()->segment(1, 'page'); if (!in_array($type, $types)) { $type = 'page'; } return $type; }
/** * Determines the current page, and sets the SQL offsets, in exactly * the same way as the original pagination library, except that this * method can be run **before** we know the total number of pages. * * @return void */ public function determine_current_page() { if ($this->query_string !== '') { $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; } else { $this->current_page = (int) URI::instance()->segment($this->uri_segment); } $this->current_page = $current_page = max(1, $this->current_page); // Reset the sql offset based on current page $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; }
/** * Loads URI, and Input into this controller. * * @return void */ public function __construct() { if (Lemon::$instance == NULL) { // Set the instance to the first controller loaded Lemon::$instance = $this; } // URI should always be available $this->uri = URI::instance(); // Input should always be available $this->input = Input::instance(); }
public function index() { $name = URI::instance()->segment(3, ''); if (!strlen($name)) { exit('0'); } $f = Kohana::find_file('page_tpl', $name); include $f; $r = json_encode($template); echo $r; }
public function redirect() { $bounces = Kohana::config('redirect_301'); if (is_null(Router::$controller) and is_array($bounces)) { foreach ($bounces as $k => $v) { if (preg_match('#^' . $k . '$#u', URI::instance()->string())) { header('HTTP/1.1 301 Moved Permanently'); url::redirect($v); } } } }
public static function decode_url() { $uri = URI::instance(); $array = array(); $array['class'] = urldecode($uri->segment(3)); $array['where'] = urldecode($uri->segment(4)); $array['orderby'] = urldecode($uri->segment(5)); $array['limit'] = urldecode($uri->segment(6)); foreach ($array as $key => $val) { $array[$key] = arr::strtoarray($val); } return $array; }
public static function __upgrade() { $uri = URI::instance(); // we havent to proceed futher if a setup call was made if ($uri->segment(1) == 'setup_check' || $uri->segment(2) == 'upgrade') { return; } // also do not proceed when responding to a web service call // as we may not have update permission on the database if ($uri->segment(1) == 'services') { return; } // Invoke the upgrader $upgrader = new Upgrade_Model(); $upgrader->run(); }
/** * return list * * @param object $lc * @return array * @author Andy Bennett */ public function get_list() { $data = array('action' => 'list', 'name' => $this->name, 'role' => User::instance()->get_role()); Event::run('steamcore.aclcheck', $data); $tdata = array(); $limit = 10; $page_num = $this->input->get('page', 1); $offset = ($page_num - 1) * $limit; $model = ORM::factory($this->setup['model'], null); $role = inflector::singular(URI::instance()->segment(1)); $data['query'] = $model->where('role', $role)->find_all($limit, $offset); $data['controller'] = Kohana::instance()->uri->segment(1); $data['pagination'] = Pagination::factory(array('style' => 'digg', 'items_per_page' => $limit, 'query_string' => 'page', 'total_items' => $model->count_last_query())); // merge any passed data and the data returned from the model $tdata = array_merge($tdata, $data); // return the result return $tdata; }
/** * constructor * * @return void * @author Andy Bennett */ function __construct() { parent::__construct(); Event::add('gallery.show_list', array('gallery_listeners', 'gallery_list')); Event::add('gallery.save_titles_success', array('gallery_listeners', 'save_titles_success')); Event::add('gallery.upload_form', array('gallery_listeners', 'upload_form')); Event::add('gallery.post_upload_fail', array('gallery_listeners', 'post_upload_fail')); Event::add('gallery.post_upload_form', array('gallery_listeners', 'post_upload_form')); Event::clear('steamcore.item_view'); Event::clear('steamcore.item_confirm_delete'); Event::clear('xform_' . $this->name . '_edit.complete'); Event::clear('xform_' . $this->name . '_edit.show_form'); Event::add('steamcore.item_view', array('gallery_listeners', 'item_view_listener')); Event::add('steamcore.item_confirm_delete', array('gallery_listeners', 'item_confirm_delete_listener')); Event::add('xform_' . $this->name . '_edit.complete', array('gallery_listeners', 'edit_complete_listener')); Event::add('xform_' . $this->name . '_edit.show_form', array('gallery_listeners', 'edit_show_form_listener')); $this->set_gallery(URI::instance()->segment(1)); }
/** * Save current user to online cache */ public function online() { // Skip when signing in/out to avoid strange Fatal error if (strpos(URI::instance()->string(), 'sign') === 0) { return; } $user = Visitor::instance()->get_user(); $online = new Online_User_Model($_SESSION['session_id']); if (!$online->loaded()) { $online->session_id = $_SESSION['session_id']; } $online->last_activity = $_SESSION['last_activity']; $online->user_id = $user ? $user->id : null; try { $online->save(); } catch (ORM_Validation_Exception $e) { } }
/** * Loads URI, and Input into this controller. * * @return void */ public function __construct() { $session = Session::instance(); $site_config = yaml::parse($_SESSION['site_name'], 'site_config'); foreach ($site_config as $key => $value) { $this->{$key} = $value; } // --- Setup our required library instances --- // -------------------------------------------- # Auth Instance for editing site capability $this->client = new Auth($this->claimed); # Account Instance for user account tool. $this->account_user = new Account(); # assets instance to fetch datapath urls. $this->assets = Assets::instance($this->site_name, $this->theme); # URI should always be available $this->uri = URI::instance(); # Input should always be available $this->input = Input::instance(); if (Kohana::$instance == NULL) { # Set the instance to the first controller loaded Kohana::$instance = $this; } }
/** * replace {} placeholders in the form * * @return void * @author Andy Bennett */ protected function replace_placeholders() { // first convert the form object back to a string $f = $this->data->form_xml_string; // first replace placeholders matching {form_XXXX} with input post data if (preg_match_all('/{form_([a-zA-Z0-9_-]+)}/', $f, $matches)) { for ($i = 0; $i < count($matches[1]); $i++) { $m = $matches[1][$i]; $u = Kohana::instance()->input->post('form_' . $m); $v = $m !== false ? $u : ''; $f = str_replace($matches[0][$i], $v, $f); } } // replace placeholders matching {user_XXXX} with data about the current user if (preg_match_all('/{user_([a-z0-9_-]+)}/', $f, $matches)) { for ($i = 0; $i < count($matches[1]); $i++) { $m = $matches[1][$i]; $v = User::instance()->{$m}; $f = str_replace($matches[0][$i], $v, $f); } } // replace placeholders matching {user_XXXX} with data about the current user if (preg_match_all('/{session_([a-z0-9_-]+)}/', $f, $matches)) { for ($i = 0; $i < count($matches[1]); $i++) { $m = $matches[1][$i]; $v = Session::instance()->get($m); $f = str_replace($matches[0][$i], $v, $f); } } // replace placeholders matching {uri_X} with data from the URI string if (preg_match_all('/{uri_([0-9]+)}/', $f, $matches)) { for ($i = 0; $i < count($matches[1]); $i++) { $m = (int) $matches[1][$i]; $s = Kohana::instance()->uri->segment($m); $v = isset($s) ? $s : ''; $f = str_replace($matches[0][$i], $v, $f); } } if (!isset($this->form_data['controller'])) { $this->form_data['controller'] = URI::instance()->segment(1); } // finally replace any items from form_data foreach ($this->form_data as $k => $v) { if (is_string($v) || is_numeric($v)) { $f = str_replace('{' . $k . '}', $v, $f); } } // now convert the string back to a simplexml object $this->form = simplexml_load_string($f); }
public function current_page($n) { if (URI::instance()->total_segments() == 0) { return Kohana::config('routes._home'); } $p = implode('/', array_slice(URI::instance()->segment_array(), 0, $n)); if (!preg_match('/\\.html$/', $p)) { $p .= '.html'; } return $p; }
/** * get the data about the image * * @param integer $image (optional - if not passed, used uri segment 3) * @return object * @author Andy Bennett */ protected function get_image_row($image = false) { // use URI segment 3 if no id is passed if (!$image) { $image = end(URI::instance()->segment_array()); } // strip any file extensions (actually strip anything that isn't a number) $image = pathinfo(Kohana::instance()->uri->string(), PATHINFO_FILENAME); // make sure the id is numeric - if not exit if (!preg_match('/[0-9]+/i', $image)) { exit; } // get the ORM object $row = ORM::factory($this->setup['model'] . '_upload', $image); // make sure the object has been loaded properly and the parent model matches if (!$row->loaded) { Kohana::log('error', 'File model row error: ' . $image); throw new Kohana_404_Exception($image, 'common/error_404'); } if ($row->parent_model != $this->setup['model']) { Kohana::log('error', 'File model error: ' . $row->id); throw new Kohana_403_Exception($row->id, 'common/error_403'); } // make sure the object's status is true if (!$row->parent->status and !User::instance()->is_moderator()) { Kohana::log('error', 'File status: ' . $row->id); throw new Kohana_403_Exception($row->id, 'common/error_403'); } $data = array('action' => URI::instance()->segment(2), 'name' => $this->setup['name'], 'role' => User::instance()->get_role()); Event::run('acl.check', $data); // set up the image's path and error if the file doesn't exist $row->full_path = Kohana::config('upload.directory') . '/' . $row->file_name; if (!file_exists($row->full_path)) { Kohana::log('error', 'Invalid file path: ' . $row->full_path); throw new Kohana_404_Exception($row->orig_name, 'common/error_404'); } return $row; }
/** * return the id of a row from name * * @return void * @author Andy Bennett */ public function return_id_from_name() { $model = ORM::factory($this->setup['model'])->where('name', URI::instance()->segment(3))->find(); echo !$model->loaded ? 0 : $model->id; }
/** * view blog post * * @param string $post * @return string * @author Andy Bennett */ public function view($post = null) { if (is_null($post)) { $post = URI::instance()->segment(3); } if ($post !== false) { $root = ORM::factory('blogpost')->where(array('id' => $post, 'status' => 1))->find(); if (!$root->loaded) { Kohana::show_404(null, 'common/error_404'); } $blog = ORM::factory('blog', $root->blog_id); $nodes = $root->descendants(true, 'ASC')->where('status', 1)->find_all(); // Get all the user ids $user_ids = array(); foreach ($nodes as $node) { $user_ids[] = $node->user_id; } $user_ids = array_unique($user_ids); $db = Database::instance(); if (!empty($user_ids)) { $postcount_subq = "select user_id, count(*) as postcount from blogposts where lvl = 1 and user_id in (" . implode(',', $user_ids) . ") group by user_id"; $replycount_subq = "select user_id, count(*) as replycount from blogposts where lvl > 1 and user_id in (" . implode(',', $user_ids) . ") group by user_id"; $users_results = $db->query("SELECT post_q.user_id, postcount, replycount FROM ({$postcount_subq}) AS post_q, ({$replycount_subq}) AS reply_q WHERE post_q.user_id = reply_q.user_id"); } else { $users_results = array(); } $users = array(); foreach ($users_results as $user) { $users[$user->user_id] = $user; } $nodes->rewind(); if (!$nodes->count()) { Kohana::show_404(null, 'common/error_404'); } $view_options = array('blog' => $blog, 'nodes' => $nodes, 'level_column' => $root->level_column, 'post_title' => $root->title, 'users' => $users); return View::factory('blog/post', $view_options)->render(); } else { die("post id not specified"); // url::redirect('/blog'); } }
/** * create a cropped image ( do size first, then crop ) * * @param string $field * @return void * @author Andy Bennett */ public static function crop($field = false) { self::init(); $sa = array_reverse(URI::instance()->segment_array()); $image = $sa[0]; $h = $sa[1]; $w = $sa[2]; $top = Input::instance()->get('ctop', 'center'); $left = Input::instance()->get('cleft', 'center'); $row = self::returnImageRow($field, $image); if ($row === false) { self::no_image(); } $path = Kohana::config('upload.upload')->staticfiles_path . Kohana::config('upload.upload')->upload_path . $row->file_name; ini_set("memory_limit", "100M"); $cache_path = APPPATH . 'cache/images/' . dirname(Kohana::instance()->uri->string()); if (!file_exists($cache_path)) { @mkdir($cache_path, 0777, TRUE); } $cache_path .= '/' . $row->file_name; $use_cache = 0; if ($row->is_image) { if (file_exists($cache_path) && (time() - filemtime($cache_path)) / 60 > 5) { @unlink($cache_path); } if (!file_exists($cache_path) || !$use_cache) { if (!file_exists($path)) { die('invalid path:- ' . $path); } $props = getimagesize($path); $image = new Image($path); $a = $props[0]; $b = $props[1]; if ($a == $w and $b == $h) { @copy($path, $cache_path); } else { $s = $w - $a; $t = $h - $b; $r = max(($a + $s) / $a, ($b + $t) / $b); $x = $a * $r; $y = $b * $r; // echo("<br/>\n s: "); var_dump($s); // echo("<br/>\n t: "); var_dump($t); // echo("<br/>\n r: "); var_dump($r); // echo("<br/>\n x: "); var_dump($x); // echo("<br/>\n y: "); var_dump($y); // exit; $image->resize($x, $y, Image::NONE); $image->crop($w, $h, $top, $left); if (!is_numeric($w)) { die('invalid width: ' . $w); } if (!is_numeric($h)) { die('invalid height: ' . $h); } $image->save($cache_path); } } header('Content-type: ' . $row->file_type); header("Content-Length: " . filesize($cache_path)); readfile($cache_path); exit; } else { header('Content-type: ' . $row->file_type); header('Content-Disposition: attachment; filename="' . $row->orig_name . '"'); header("Content-Length: " . filesize($path)); readfile($path); exit; } }
/** * activate the user * * @return void * @author Andy Bennett */ public function activate() { try { $segs = array_reverse(URI::instance()->segment_array()); $code = $segs[0]; $id = $segs[1]; $identity = Kohana::config('steamauth.steamauth')->identity_column; // check the passed values if (!is_numeric($id) or $id <= 0) { throw new Exception("auth.invalid_user_id"); } if (!strlen($code) or !preg_match('/[a-zA-Z0-9]+/', $code)) { throw new Exception('auth.invalid_activation_code'); } $user = ORM::factory('user', $id); if (!$user->loaded) { throw new Exception("auth.invalid_user"); } if ($user->activation_code != $code) { throw new Exception('auth.activation_code_mismatch'); } $user->activated = 1; $user->activation_code = ''; $user->save(); Event::run('steamauth.activated', $user); } catch (Exception $e) { Kohana::log('error', __FILE__ . ':' . __METHOD__ . ':' . $e->getMessage()); Event::run('steamauth.activation_error', $error = $e->getMessage()); } }
<?php $uler = $o->form->return_form_data('upload_error'); $s = isset($row->controller) ? $row->controller : URI::instance()->segment(1); if (is_object($o->current) and $o->current->id > 0) { ?> <p> <?php if ($o->current->is_image) { $w = isset($row->width) ? $row->width : 20; $h = isset($row->height) ? $row->height : 20; $url = url::base() . $s . '/crop/' . $w . '/' . $h . '/' . $o->current->id . $o->current->file_ext; echo '<img src="' . $url . '" alt="Image" />'; } else { echo $o->current->orig_name; } echo form::checkbox('remove_' . $o->form_name . $o->group_id, 1); echo '<label for="remove_' . $o->form_name . $o->group_id . '">Remove file</label>'; ?> </p> <?php } ?> <?php if (!is_object($o->current) and $o->cval > 0) { ?> <p> <a href="<?php echo url::base() . $s . '/download/' . $o->cval; ?>
/** * 请求分页处理 * * 2010-04-21 */ public static function request_per_page(&$request_struct_current, &$request_data) { $preset_perpages = Kohana::config('pagination.per_page'); $uri = URI::instance(); $segment = $uri->segment(1) . '_' . $uri->segment(2) . '_' . $uri->segment(3); if (isset($request_data['per_page']) && !empty($request_data['per_page']) && in_array($request_data['per_page'], $preset_perpages)) { $request_struct_current['limit']['per_page'] = $request_data['per_page']; } elseif (!is_null(cookie::get($segment . '_per_page'))) { $request_struct_current['limit']['per_page'] = intval(cookie::get($segment . '_per_page')); } else { $request_struct_current['limit']['per_page'] = $preset_perpages[0]; } /* $request_data['per_page'] = $request_struct_current['limit']['per_page']; if(isset($request_data['page']) && !empty($request_data['page']) && is_numeric($request_data['page']) && $request_data['page']>0) { $request_struct_current['limit']['page'] = $request_data['page']; } $request_data['page'] = $request_struct_current['limit']['page']; */ }
/** * view a thread * * @return void * @author Andy Bennett */ function thread() { $thread = URI::instance()->segment(3); if ($thread === false) { url::redirect('forum'); } $thread = ORM::factory('thread', $thread); $copy = View::factory('forum/thread', array('thread' => $thread))->render(); Event::run('forum.render', $copy); }
/** * Sets config values. * * @throws Kohana_Exception * @param array configuration settings * @return void */ public function initialize($config = array()) { // Load config group if (isset($config['group'])) { // Load and validate config group if (!is_array($group_config = Kohana::config('pagination.' . $config['group']))) { throw new Kohana_Exception('pagination.undefined_group', $config['group']); } // All pagination config groups inherit default config group if ($config['group'] !== 'default') { // Load and validate default config group if (!is_array($default_config = Kohana::config('pagination.default'))) { throw new Kohana_Exception('pagination.undefined_group', 'default'); } // Merge config group with default config group $group_config += $default_config; } // Merge custom config items with config group $config += $group_config; } // Assign config values to the object foreach ($config as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; } } // Clean view directory $this->directory = trim($this->directory, '/') . '/'; // Build generic URL with page in query string if ($this->query_string !== '') { // Extract current page $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; // Insert {page} placeholder $_GET[$this->query_string] = '{page}'; // Create full URL $base_url = $this->base_url === '' ? Router::$current_uri : $this->base_url; $this->url = url::site($base_url) . '?' . str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); // Reset page number $_GET[$this->query_string] = $this->current_page; } else { // Use current URI if no base_url set $this->url = $this->base_url === '' ? Router::$segments : explode('/', trim($this->base_url, '/')); // Convert uri 'label' to corresponding integer if needed if (is_string($this->uri_segment)) { if (($key = array_search($this->uri_segment, $this->url)) === FALSE) { // If uri 'label' is not found, auto add it to base_url $this->url[] = $this->uri_segment; $this->uri_segment = count($this->url) + 1; } else { $this->uri_segment = $key + 2; } } // Insert {page} placeholder $this->url[$this->uri_segment - 1] = '{page}'; // Create full URL $this->url = url::site(implode('/', $this->url)) . Router::$query_string; // Extract current page $this->current_page = URI::instance()->segment($this->uri_segment); } // Core pagination values $this->total_items = (int) max(0, $this->total_items); $this->items_per_page = (int) max(1, $this->items_per_page); $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); $this->current_first_item = (int) min(($this->current_page - 1) * $this->items_per_page + 1, $this->total_items); $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); // If there is no first/last/previous/next page, relative to the // current page, value is set to FALSE. Valid page number otherwise. $this->first_page = $this->current_page === 1 ? FALSE : 1; $this->last_page = $this->current_page >= $this->total_pages ? FALSE : $this->total_pages; $this->previous_page = $this->current_page > 1 ? $this->current_page - 1 : FALSE; $this->next_page = $this->current_page < $this->total_pages ? $this->current_page + 1 : FALSE; // SQL values $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); }
/** * constructor * * @return void * @author Andy Bennett */ function __construct() { parent::__construct(); $this->set_gallery(URI::instance()->segment(1)); }