/** * Return current URL with get query. * * @param null|string $slug * @param array $query * @return string */ function url($slug = null, array $query = []) { static $url = null; if ($url === null) { $url = strval(Url::current('/')); } return $url . ltrim($slug, '/') . (($q = http_build_query($query, '', '&', PHP_QUERY_RFC3986)) ? '?' . $q : ''); }
static function init() { self::$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === true ? 'https' : 'http'; self::$host = self::$protocol . '://' . $_SERVER['HTTP_HOST']; self::$current = urldecode(self::$host . $_SERVER['REQUEST_URI']); self::$application = self::$host . str_replace('index.php', '', $_SERVER['SCRIPT_NAME']); define('URL_PROTOCOL', self::$protocol); define('URL_HOST', self::$host); define('URL_CURRENT', self::$current); define('URL_APPLICATION', self::$application); }
protected static function boot() { parent::boot(); self::saving(function ($channel) { if ($channel->code == null) { $channel->code = \Str::slug($channel->name); } }); if (strstr(\Url::current(), \Backend::baseUrl())) { self::backendBoot(); } }
/** * Creates an HTML anchor tag. If the given url matches the first part of current url, a class of "active" will automatically * be added to the attributes. * * Basic Example: * Html::a('My Text', 'some/path'); // Short hand * Html::a()->get('My Text', 'some/path'); * * Attributes Example: * Html::a()->get('My Text', 'some/path', array('title' => 'My Text', 'class' => 'active')); * * @param string $title The text to go between the tags * @param string $url The relative or full url * @param array $attributes An optional array of attributes where the key is the attribute name * @return string An HTML anchor tag */ public function get($title, $url, $attributes = array()) { $attr = ''; if ($url != '/' && strstr(Url::current(true), $url)) { if (isset($attributes['class'])) { $attributes['class'] = 'active ' . $attributes['class']; } else { $attributes['class'] = 'active'; } } elseif ($url == '/' && !strlen(Url::current(true))) { $attributes['class'] = 'active'; } if ($attributes) { foreach ($attributes as $k => $v) { $attr .= sprintf(' %s="%s"', $k, $v); } } $url = is_null($url) ? 'javascript:null' : Url::to($url); return sprintf('<a href="%s"%s>%s</a>', $url, $attr, $title); }
/** * Create the column sorter. * * @return string|void */ public function sorter() { if (!$this->sortable) { return; } $icon = 'fa-sort'; $type = 'desc'; if ($this->isSorted()) { $type = $this->sort['type'] == 'desc' ? 'asc' : 'desc'; $icon .= "-amount-{$this->sort['type']}"; } $query = app('request')->all(); $query = array_merge($query, ['_sort' => ['column' => $this->name, 'type' => $type]]); $url = Url::current() . '?' . http_build_query($query); return "<a class=\"fa fa-fw {$icon}\" href=\"{$url}\"></a>"; }
<h1><?php echo __('users.log_in', 'Log in'); ?> </h1> <?php echo Notifications::read(); ?> <section class="content"> <form method="post" action="<?php echo Url::current(); ?> "> <input name="token" type="hidden" value="<?php echo Csrf::token(); ?> "> <fieldset> <p> <label for="user"><?php echo __('users.username', 'Username'); ?> :</label> <input autocapitalize="off" name="user" id="user" value="<?php echo filter_var(Input::post('user'), FILTER_SANITIZE_STRING); ?>
<?php $canonical_url = Url::current(); switch (PAGE_ACTION) { case 'index': $title = 'Encuestas | ' . SITE_NAME; $description = ''; break; case 'view': $title = $poll->question . ' | ' . SITE_NAME; $description = $poll->description; break; case 'vote': $title = 'Vota: "' . $poll->question . '" | ' . SITE_NAME; $description = 'Vota a la encuesta número ' . $poll->id . ' en ' . SITE_NAME; break; } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title><?php echo $title; ?> </title> <meta name="description" content="<?php echo $description; ?> ">
<img src="<?php echo theme_url('assets/img/logo.png'); ?> " alt="Anchor CMS"> </a> <?php if (($user = Users::authed()) !== false) { ?> <nav> <ul> <?php foreach (admin_menu() as $title => $url) { ?> <li <?php if (strpos(Url::current(), $url) !== false) { echo 'class="active"'; } ?> > <a href="<?php echo Url::make($url); ?> "><?php echo __('common.' . $title, $title); ?> </a> </li> <?php } ?>
public static function translate(&$route_vars = array()) { $route_vars = array('extension' => '', 'controller' => '', 'action' => ''); $route_comps = parse_url(str_replace(Url::root(), '', Url::current())); if (empty($route_comps['path']) and empty($route_comps['query'])) { //show the home page $route_vars = self::page(); return; } $path = !empty($route_comps['path']) ? $route_comps['path'] : ''; $path_chunks = array_filter(explode('/', $path)); //remove indeix.php from the path if (!empty($path_chunks) and strtolower($path_chunks[0]) == 'index.php') { array_shift($path_chunks); } //if there is no path, show the home page (index.php) if (empty($path_chunks)) { //no path provided, try to find the extension data $route_vars = array('extension' => Request::data('ext', ''), 'controller' => Request::data('cont', ''), 'action' => Request::data('act', '')); if (empty($route_vars['extension'])) { if (empty($route_vars['controller'])) { //no ext/cont provided, load default home page $route_vars = self::page(); } elseif ($route_vars['controller'] == 'pages') { $route_vars = self::page(Request::get('_Route.index', null)); } } return; } if ((bool) Base::getConfig('sef_urls', 0) === false) { //SEF disabled, no matching path found, will display 404 error return; } //we have a path, it may be a page alias OR an extension name $branches = $path_chunks; $alias = array_shift($path_chunks); //check a page with a matching alias if (!empty($alias)) { //check pages aliases $tests = array(); while (!empty($branches)) { $tests[] = implode('/', $branches); array_pop($branches); } $route_vars = self::page($tests); $path_chunks = array_diff($path_chunks, explode('/', Request::data('_Route.page.alias', ''))); if (!empty($route_vars)) { //matching page alias found //check if the page belongs to an extension and if the extension has a router ? if (!empty($route_vars['extension'])) { $route_vars = array_merge($route_vars, self::set_params($route_vars['extension'], $path_chunks)); } elseif (empty($route_vars['extension']) and !empty($route_vars['controller'])) { $route_vars = array_merge($route_vars, self::set_params($route_vars['controller'], $path_chunks, 'controller')); } return; } } //check if this is a valid extension name if (class_exists('\\GCore\\Extensions\\' . Str::camilize($alias) . '\\' . Str::camilize($alias))) { $route_vars['extension'] = $alias; Request::set('_Route.index', $alias); $route_vars['controller'] = Request::data('cont', ''); $route_vars['action'] = Request::data('act', ''); //check if the extension has a router ? $route_vars = array_merge($route_vars, self::set_params($alias, $path_chunks)); return; } //check if this is a valid core controller name if (class_exists('\\GCore\\Controllers\\' . Str::camilize($alias))) { $route_vars['controller'] = $alias; Request::set('_Route.index', $alias); $route_vars['action'] = Request::data('act', ''); //check if the extension has a router ? $route_vars = array_merge($route_vars, self::set_params($alias, $path_chunks, 'controller')); return; } }
/** * 生成outh URL * * @param string $to * @param string $scope * @param string $state * * @return string */ public function url($to = null, $scope = 'snsapi_base', $state = 'STATE') { $to !== null || ($to = Url::current()); $params = array('appid' => $this->appId, 'redirect_uri' => $to, 'response_type' => 'code', 'scope' => $scope, 'state' => $state); return self::API_URL . '?' . http_build_query($params) . '#wechat_redirect'; }
/** * Returns the route data associated with the current route. * * The route data is set in a modules setup.php file. If no data is * found for the current route, boolean false is returned. * * @return mixed Array of data if route exists, boolean false otherwise * * TODO Check for invalid characters in URL */ public static function getRouteData() { $data = false; /* $defaultRoute = Config::get('router.default_route'); $currentRoute = $defaultRoute; if(isset($_GET['r']) && strlen($_GET['r'])) $currentRoute = $_GET['r']; $currentRoute = rtrim($currentRoute, '/'); // Look for language code, and modify route if need be if(Multilanguage::routeHasLangCode($currentRoute)) { $currentRoute = ltrim(substr($currentRoute, 3), '/'); // If current route is empty now (due to being at base url with language code (ex: /<code>), set to default if(!strlen($currentRoute)) $currentRoute = $defaultRoute; } */ $currentRoute = Url::current(); if ($currentRoute == '/') { $currentRoute = Config::get('router.default_route'); } else { $currentRoute = ltrim($currentRoute, '/'); } // We dont want leading slash of the current url for routes while (true) { // First search routes with exact match if (isset(self::$_routes[$currentRoute])) { $data = self::$_routes[$currentRoute]; } // If no exact match, check matches with regex foreach (self::$_routes as $route => $routeData) { $regexRoute = String::regify($route); if (preg_match('@^' . $regexRoute . '$@', $currentRoute, $matches)) { self::$_params = array_slice($matches, 1); $data = $routeData; } } // If no route data was found, must be 404 if (!$data) { break; } elseif (isset($data['redirect'])) { $currentRoute = $data['redirect']; } elseif ($data) { // First check permissions, if any, for current user self::$_currentRoute = array('route' => $currentRoute, 'data' => $data); break; } } if ($data && !isset($data['permissions'])) { $data['permissions'] = array(); } // Let other areas of the application make use of route data Event::trigger('router.data', array($currentRoute, $data)); return array($currentRoute, $data); }
public static function action_edit($id = null) { if (!IS_ADMIN) { Redirect::to(Url::get('admin@login', null, 'redirect-to=' . urlencode(Url::current()))); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($id === 'delete-answer') { if (($answer_id = Param::post('answer_id')) && is_numeric($answer_id)) { $answer = Answer::get((int) $answer_id); Answer::find((int) $answer_id)->delete(); $votes = Vote::where('answer_id', '=', $answer_id)->count(); if ($votes) { Poll::find($answer->poll_id)->set(array('nofilter:total_votes' => "`total_votes` - {$votes}")); } return Response::json(array('status' => 200, 'deleted' => true)); } else { return Response::json(array('status' => 0, 'deleted' => false)); } } elseif ($id) { return Response::error(404); } else { $id = Param::post('id'); if ($answer_id = Param::post('remove_answer')) { Answer::find((int) $answer_id)->and_where('poll_id', '=', $id)->delete(); $votes = Vote::where('answer_id', '=', $answer_id)->count(); if ($votes) { Poll::find($id)->set(array('nofilter:total_votes' => "`total_votes` - {$votes}")); } Redirect::to(Url::get('admin@edit', $id, 'answer_deleted=true')); } if (Param::post('remove_poll')) { Poll::find($id)->delete(); Redirect::to(Url::get('admin', null, 'poll_deleted=true')); } if (is_numeric($id) && ($poll = Poll::get((int) $id))) { foreach ($_POST as $key => $value) { if (isset($poll->{$key}) && (!empty($_POST[$key]) || $_POST[$key] === "0")) { $poll->{$key} = is_numeric($_POST[$key]) ? intval($_POST[$key], 10) : $_POST[$key]; } elseif (false !== strpos($key, 'answer-')) { $answer_id = explode('-', $key); $answer_id = $answer_id[1]; if (is_numeric($answer_id)) { Answer::find((int) $answer_id)->set(array('text' => $value)); } } elseif ($key === 'new_answers') { foreach ($value as $new_answer) { if (!empty($new_answer)) { Answer::create(array('poll_id' => (int) $poll->id, 'text' => $new_answer)); } } } } Poll::save($poll); Redirect::to(Url::get('admin', null, 'success=' . $_POST['id'] . '&updated=true')); } else { return Response::error(500); } } } if (!$id || !is_numeric($id) || !($poll = Poll::get((int) $id))) { return Response::error(404); } else { $answers = Answer::where('poll_id', '=', $poll->id)->get(); return View::make('admin.edit')->add_var('answers', $answers)->add_var('poll', $poll); } }
public static function current() { return Url::current()->uri(); }
function dispatch($content_only = false, $check_perm = true) { Event::trigger('on_before_dispatch', $this); $session = Base::getSession(); reset: //if no action set, set it to index if (strlen(trim($this->action)) == 0) { $this->action = 'index'; } //set admin path $site = ''; if ($this->site == 'admin') { $site = '\\Admin'; } //load the extension class $controller = !empty($this->controller) ? '\\Controllers\\' . Str::camilize($this->controller) : '\\' . Str::camilize($this->extension); $extension = !empty($this->extension) ? '\\Extensions\\' . Str::camilize($this->extension) : ''; $classname = '\\GCore' . $site . $extension . $controller; $this->tvout = strlen(Request::data('tvout', null)) > 0 ? Request::data('tvout') : $this->tvout; //set referer if (!$content_only) { if (!($this->controller == 'users' and ($this->action == 'login' or $this->action == 'logout' or $this->action == 'register')) and (!empty($this->extension) or !empty($this->controller)) and $this->tvout == 'index') { $session->set('_referer', Url::current()); } else { //$session->set('_referer', 'index.php'); } } //check permissions if ($check_perm and !Authorize::authorized($classname, $this->action)) { if ($content_only) { return; } $this->redirect(r_('index.php?cont=users&act=login')); } //if the extension class not found or the action function not found then load an error if (!class_exists($classname) or !in_array($this->action, get_class_methods($classname)) and !in_array('__call', get_class_methods($classname)) or substr($this->action, 0, 1) == '_') { $this->controller = 'errors'; $this->action = 'e404'; //reset the controller $classname = '\\GCore\\Controllers\\Errors'; \GCore\Libs\Env::e404(); //we need the rendered content only if ($content_only) { return; } } //load language file if (!empty($extension)) { Lang::load($site . $extension); } //set theme $doc = Document::getInstance($this->site, $this->thread); $doc->theme = 'bootstrap3'; //'gcoreui';//'semantic1'; $theme = \GCore\Helpers\Theme::getInstance(); // in gcore app, bootstrap should be always loaded first with jquery //load class and run the action ${$classname} = new $classname($this->site, $this->thread); ob_start(); $continue = ${$classname}->_initialize(); //check and read cache if (!empty(${$classname}->cache)) { if (!is_array(${$classname}->cache)) { ${$classname}->cache = array(); } if (empty(${$classname}->cache['time'])) { ${$classname}->cache['time'] = Base::getConfig('app_cache_expiry', 900); } if (empty(${$classname}->cache['title'])) { ${$classname}->cache['title'] = File::makeSafe($classname . '_' . $this->action); } else { ${$classname}->cache['title'] = File::makeSafe(${$classname}->cache['title']); } if (empty(${$classname}->cache['key'])) { ${$classname}->cache['key'] = 'cached_view'; } else { ${$classname}->cache['key'] = 'cached_view_' . ${$classname}->cache['key']; } $cache = Cache::getInstance(${$classname}->cache['title'], array('expiration' => ${$classname}->cache['time'])); $cached_view = $cache->get(${$classname}->cache['key']); $cached = false; if (!empty($cached_view)) { $cached = true; $continue = false; echo $cached_view; } } if ($continue !== false) { ${$classname}->{$this->action}(); if ($this->reset === true) { $this->reset = false; goto reset; } //initialize and render view $view = new View(); $view->initialize(${$classname}); $view->renderView($this->action); } //get the action output buffer $this->buffer = ob_get_clean(); //check and save cache if (!empty(${$classname}->cache) and !$cached) { $cache = Cache::getInstance(${$classname}->cache['title'], array('expiration' => ${$classname}->cache['time'])); $cache->set(${$classname}->cache['key'], $this->buffer); } //finalize ob_start(); ${$classname}->_finalize(); $this->buffer .= ob_get_clean(); //now load the theme files //$theme = \GCore\Helpers\Theme::getInstance(); if ($this->tvout != 'ajax' and $doc->theme == 'bootstrap3') { $this->buffer = '<div class="gbs3">' . $this->buffer . '</div>'; } Event::trigger('on_after_dispatch'); }