Generates a well-formed querystring from $q
public static queryString ( string | array $q, array $extra = [], boolean $escape = false ) : array | ||
$q | string | array | Query string Either a string of already compiled query string arguments or an array of arguments to convert into a query string. |
$extra | array | Extra querystring parameters. |
$escape | boolean | Whether or not to use escaped & |
return | array |
protected function _buildQuery($query, $escape = false) { if (is_array($query)) { $query = substr(Router::queryString($query, array(), $escape), '1'); } return $query; }
/** * load Anywhere script * * @param string $dataSource * @param string $apiKey * @param string $apiVersion */ public function loadScript($dataSource = 'twitter', $apiKey = null, $apiVersion = 1) { if (empty($apiKey)) { /* @var $ds TwitterSource */ $ds = ConnectionManager::getDataSource($dataSource); if (!empty($ds->config['api_key'])) { $apiKey = $ds->config['api_key']; } } $params = array('id' => $apiKey, 'v' => $apiVersion); return $this->Html->script(self::$anywhereUri . Router::queryString($params, array(), true)); }
/** * load Anywhere script * * @param array $options * @return string */ public function loadScript($options = array()) { $dataSource = $apiKey = $apiVersion = null; $defaults = array('dataSource' => 'twitter', 'apiKey' => null, 'apiVersion' => 1, 'inline' => false); $options = am($defaults, $options); extract($options, EXTR_OVERWRITE); unset($options['dataSource']); unset($options['apiKey']); unset($options['apiVersion']); if (empty($apiKey)) { /* @var $ds TwitterSource */ $ds = ConnectionManager::getDataSource($dataSource); if (!empty($ds->config['api_key'])) { $apiKey = $ds->config['api_key']; } } $params = array('id' => $apiKey, 'v' => $apiVersion); return $this->Html->script(self::$anywhereUri . Router::queryString($params, array(), true), $options); }
/** * Main execution method. Handles redirecting of invalid users, and saving * of request url as Sanction.referer * * @param object $controller A reference to the instantiating controller object * @return boolean * @access public */ function startup(&$controller) { foreach ($this->routes as $route) { if ($this->_parse($controller, $route['route'])) { if ($this->_execute($route)) { if (isset($controller->params['url']['url'])) { $url = $controller->params['url']['url']; } $url = Router::normalize($url); if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) { $query = $controller->params['url']; unset($query['url'], $query['ext']); $url .= Router::queryString($query, array()); } $this->Session->write('Sanction.referer', $url); $this->redirect($controller, $route); } break; } } }
/** * Main execution method. Handles redirecting of invalid users, and processing * of login form data. * * @param object $controller A reference to the instantiating controller object * @return boolean * @access public */ function startup(&$controller) { $this->__checkCookie(); // see if user has the RememberMe cookie $isErrorOrTests = strtolower($controller->name) == 'cakeerror' || strtolower($controller->name) == 'tests' && Configure::read() > 0; if ($isErrorOrTests) { return true; } $methods = array_flip($controller->methods); $action = strtolower($controller->params['action']); $isMissingAction = $controller->scaffold === false && !isset($methods[$action]); if ($isMissingAction) { return true; } if (!$this->__setDefaults()) { return false; } $this->data = $controller->data = $this->hashPasswords($controller->data); $url = array(); if (!empty($controller->name) && !empty($controller->action)) { $url = array('controller' => strtolower($controller->name), 'action' => $controller->action); } $url = Router::normalize($url); $loginAction = Router::normalize($this->loginAction); $allowedActions = array_map('strtolower', $this->allowedActions); $isAllowed = $this->allowedActions == array('*') || in_array($action, $allowedActions); // row-level acl begin if ($this->user()) { $aros = Classregistry::init('Aro')->find('all', array('conditions' => array('Aro.model' => $this->userModel, 'Aro.foreign_key' => $this->user('id')), 'fields' => array('Aro.id', 'Aro.model', 'Aro.foreign_key'))); $this->userAros = Set::extract('/Aro/id', $aros); } // row-level acl end if ($loginAction != $url && $isAllowed) { return true; } if ($loginAction == $url) { $model =& $this->getModel(); if (empty($controller->data) || !isset($controller->data[$model->alias])) { if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) { $this->Session->write('Auth.redirect', $controller->referer(null, true)); } return false; } $isValid = !empty($controller->data[$model->alias][$this->fields['username']]) && !empty($controller->data[$model->alias][$this->fields['password']]); if ($isValid) { $username = $controller->data[$model->alias][$this->fields['username']]; $password = $controller->data[$model->alias][$this->fields['password']]; $data = array($model->alias . '.' . $this->fields['username'] => $username, $model->alias . '.' . $this->fields['password'] => $password); if ($this->login($data)) { if ($this->autoRedirect) { $controller->redirect($this->redirect(), null, true); } return true; } } $this->Session->setFlash($this->loginError, $this->flashElement, array(), 'auth'); $controller->data[$model->alias][$this->fields['password']] = null; return false; } else { if (!$this->user()) { if (!$this->RequestHandler->isAjax()) { $this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth'); if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) { $query = $controller->params['url']; unset($query['url'], $query['ext']); $url .= Router::queryString($query, array()); } $this->Session->write('Auth.redirect', $url); $controller->redirect($loginAction); return false; } elseif (!empty($this->ajaxLogin)) { $controller->viewPath = 'elements'; echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout); $this->_stop(); return false; } else { $controller->redirect(null, 403); } } } if (!$this->authorize) { return true; } extract($this->__authType()); switch ($type) { case 'controller': $this->object =& $controller; break; case 'crud': case 'actions': if (isset($controller->Acl)) { $this->Acl =& $controller->Acl; } else { trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING); } break; case 'acl': if (isset($controller->Acl)) { $this->Acl =& $controller->Acl; } else { trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING); } case 'model': $object = null; if (!isset($object)) { $hasModel = isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass}); $isUses = !empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]}); if ($hasModel) { $object = $controller->modelClass; } elseif ($isUses) { $object = $controller->uses[0]; } } $type = array('model' => $object); if (isset($this->Acl)) { $type = array('acl' => $object); } break; } if ($this->isAuthorized($type)) { return true; } $this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth'); $controller->redirect($controller->referer(), null, true); return false; }
/** * Main execution method. Handles redirecting of invalid users, and processing * of login form data. * * @param object $controller A reference to the instantiating controller object * @return boolean * @access public */ function startup(&$controller) { $methods = array_flip($controller->methods); $action = strtolower($controller->params['action']); $allowedActions = array_map('strtolower', $this->allowedActions); $isErrorOrTests = strtolower($controller->name) == 'cakeerror' || strtolower($controller->name) == 'tests' && Configure::read() > 0; if ($isErrorOrTests) { return true; } $isMissingAction = $controller->scaffold === false && !isset($methods[$action]); if ($isMissingAction) { return true; } if (!$this->__setDefaults()) { return false; } $this->data = $controller->data = $this->hashPasswords($controller->data); $url = ''; if (isset($controller->params['url']['url'])) { $url = $controller->params['url']['url']; } $url = Router::normalize($url); $loginAction = Router::normalize($this->loginAction); $isAllowed = $this->allowedActions == array('*') || in_array($action, $allowedActions); if ($loginAction != $url && $isAllowed) { return true; } if ($loginAction == $url) { if (empty($controller->data) || !isset($controller->data[$this->userModel])) { if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) { $this->Session->write('Auth.redirect', $controller->referer(null, true)); } return false; } $isValid = !empty($controller->data[$this->userModel][$this->fields['username']]) && !empty($controller->data[$this->userModel][$this->fields['password']]); if ($isValid) { $username = $controller->data[$this->userModel][$this->fields['username']]; $password = $controller->data[$this->userModel][$this->fields['password']]; $data = array($this->userModel . '.' . $this->fields['username'] => $username, $this->userModel . '.' . $this->fields['password'] => $password); if ($this->login($data)) { if ($this->autoRedirect) { $controller->redirect($this->redirect(), null, true); } return true; } } $this->Session->setFlash($this->loginError, 'default', array(), 'auth'); $controller->data[$this->userModel][$this->fields['password']] = null; return false; } else { if (!$this->user()) { if (!$this->RequestHandler->isAjax()) { $this->Session->setFlash($this->authError, 'default', array(), 'auth'); if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) { $query = $controller->params['url']; unset($query['url'], $query['ext']); $url .= Router::queryString($query, array()); } $this->Session->write('Auth.redirect', $url); $controller->redirect($loginAction); return false; } elseif (!empty($this->ajaxLogin)) { $controller->viewPath = 'elements'; echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout); $this->_stop(); return false; } else { $controller->redirect(null, 403); } } } if (!$this->authorize) { return true; } extract($this->__authType()); switch ($type) { case 'controller': $this->object =& $controller; break; case 'crud': case 'actions': if (isset($controller->Acl)) { $this->Acl =& $controller->Acl; } else { $err = 'Could not find AclComponent. Please include Acl in '; $err .= 'Controller::$components.'; trigger_error(__($err, true), E_USER_WARNING); } break; case 'model': if (!isset($object)) { $hasModel = isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass}); $isUses = !empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]}); if ($hasModel) { $object = $controller->modelClass; } elseif ($isUses) { $object = $controller->uses[0]; } } $type = array('model' => $object); break; } if ($this->isAuthorized($type)) { return true; } $this->Session->setFlash($this->authError, 'default', array(), 'auth'); $controller->redirect($controller->referer(), null, true); return false; }
/** * * @param string $apiName * @param string $requestToken * @param array $extra extra parameters for the querystring */ public function authorize($apiName, $requestToken, $extra = array()) { $query = Router::queryString(array('oauth_token' => $requestToken), $extra); $uri = $this->getOauthUri($apiName, 'authorize'); return $uri . $query; }
/** * Tests generating well-formed querystrings * * @return void */ public function testQueryString() { $result = Router::queryString(array('var' => 'foo bar')); $expected = '?var=foo+bar'; $this->assertEquals($expected, $result); $result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar')); $expected = '?some=param&foo=bar'; $this->assertEquals($expected, $result); $existing = array('apple' => 'red', 'pear' => 'green'); $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); $expected = '?apple=red&pear=green&some=param&foo=bar'; $this->assertEquals($expected, $result); $existing = 'apple=red&pear=green'; $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); $expected = '?apple=red&pear=green&some=param&foo=bar'; $this->assertEquals($expected, $result); $existing = '?apple=red&pear=green'; $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); $expected = '?apple=red&pear=green&some=param&foo=bar'; $this->assertEquals($expected, $result); $result = Router::queryString('apple=red&pear=green'); $expected = '?apple=red&pear=green'; $this->assertEquals($expected, $result); $result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true); $expected = '?foo=bar&php=nut&jose=zap'; $this->assertEquals($expected, $result); $result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'), true); $expected = '?foo=bar&php=nut&jose=zap'; $this->assertEquals($expected, $result); $result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap')); $expected = '?foo=bar&php=nut&jose=zap'; $this->assertEquals($expected, $result); }
/** * Serializes an array for transport. * * @param array $data Data to serialize * @return string Serialized variable * @access protected */ function _httpSerialize($data = array()) { if (is_string($data)) { return $data; } if (empty($data) || !is_array($data)) { return false; } return substr(Router::queryString($data), 1); }
/** * A special fallback method that handles url arrays that cannot match * any defined routes. * * @param array $url A url that didn't match any routes * @return string A generated url for the array * @see Router::url() */ protected static function _handleNoRoute($url) { $named = $args = $query = array(); $skip = array_merge(array('bare', 'action', 'controller', 'plugin', 'prefix'), self::$_prefixes); $keys = array_values(array_diff(array_keys($url), $skip)); $count = count($keys); // Remove this once parsed URL parameters can be inserted into 'pass' for ($i = 0; $i < $count; $i++) { $key = $keys[$i]; if (is_numeric($keys[$i])) { $args[] = $url[$key]; } else { $named[$key] = $url[$key]; } } list($args, $named) = array(Set::filter($args, true), Set::filter($named, true)); foreach (self::$_prefixes as $prefix) { if (!empty($url[$prefix])) { $url['action'] = str_replace($prefix . '_', '', $url['action']); break; } } if (empty($named) && empty($args) && empty($query) && (!isset($url['action']) || $url['action'] === 'index')) { $url['action'] = null; } $urlOut = array_filter(array($url['controller'], $url['action'])); if (isset($url['plugin'])) { array_unshift($urlOut, $url['plugin']); } foreach (self::$_prefixes as $prefix) { if (isset($url[$prefix])) { array_unshift($urlOut, $prefix); break; } } $output = implode('/', $urlOut); if (!empty($args)) { $output .= '/' . implode('/', $args); } if (!empty($named)) { foreach ($named as $name => $value) { if (is_array($value)) { $flattend = Set::flatten($value, ']['); foreach ($flattend as $namedKey => $namedValue) { $output .= '/' . $name . "[{$namedKey}]" . self::$_namedConfig['separator'] . $namedValue; } } else { $output .= '/' . $name . self::$_namedConfig['separator'] . $value; } } } if (!empty($query)) { $output .= Router::queryString($query); } return $output; }
/** * @param float progress * @param array options: * - min, max * - steps * - decimals (how precise should the result be displayed) * @return string HTML * @deprecated Try to use font icons or move to own helper */ public function progressBar($progress, $options = array(), $htmlOptions = array()) { $defaults = array('min' => 0, 'max' => 100, 'steps' => 15, 'decimals' => 1); $options += $defaults; $current = (double) $progress / $options['max'] - $options['min']; $percent = $current * 100; $current *= $options['steps']; $options['progress'] = number_format($current, $options['decimals'], null, ''); $params = Router::queryString($options, array(), true); $htmlDefaults = array('title' => $this->Numeric->format($percent, $options['decimals']) . ' ' . __('Percent'), 'class' => 'help'); $htmlDefaults['alt'] = $htmlDefaults['title']; $htmlOptions += $htmlDefaults; //return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions); return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' . $htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />'; }
/** * Startup * * @param object Controller instance * @return void */ public function startUp(Controller $Controller) { if (in_array($Controller->action, $this->actions)) { if (empty($Controller->request->data) && $Controller->Session->check($this->sessionPath)) { if ($this->directPost == true) { $Controller->request->data = $Controller->Session->read($this->sessionPath); $Controller->Session->delete($this->sessionPath); } } elseif (!empty($Controller->request->data) && !$Controller->Auth->user()) { $this->preserve($Controller->request->data); if (empty($this->loginAction) && !empty($Controller->Auth->loginAction)) { $this->loginAction = $Controller->Auth->loginAction; if (!empty($this->redirectMessage)) { $Controller->Session->setFlash($this->redirectMessage); } // Code from AuthComponent to store the redirect url so the user get redirected // to the correct location after a successful login if (isset($Controller->Auth)) { $url = ''; if (isset($Controller->params['url']['url'])) { $url = $Controller->params['url']['url']; } $url = Router::normalize($url); if (!empty($Controller->params['url']) && count($Controller->params['url']) >= 2) { $query = $Controller->params['url']; unset($query['url'], $query['ext']); $url .= Router::queryString($query, array()); } $this->Session->write('Auth.redirect', $url); } $Controller->redirect($this->loginAction); } } } }
/** * @param float progress * @param array options: * - min, max * - steps * - decimals (how precise should the result be displayed) * * 2010-01-10 ms */ public function progressBar($progress, $options = array(), $htmlOptions = array()) { $defaults = array('min' => 0, 'max' => 100, 'steps' => 15, 'decimals' => 1); $options = array_merge($defaults, $options); $current = (double) $progress / $options['max'] - $options['min']; $percent = $current * 100; $current *= $options['steps']; $options['progress'] = number_format($current, $options['decimals'], null, ''); $params = Router::queryString($options, array(), true); App::import('Helper', 'Tools.Numeric'); $this->Numeric = new NumericHelper(new View(null)); $htmlDefaults = array('title' => $this->Numeric->format($percent, $options['decimals']) . ' ' . __('Percent'), 'class' => 'help'); $htmlDefaults['alt'] = $htmlDefaults['title']; $htmlOptions = array_merge($htmlDefaults, $htmlOptions); //return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions); # bug in Html::webroot() ??? ommits ?... return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' . $htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />'; }
/** * create tweet button * * @see http://dev.twitter.com/pages/tweet_button * @param string $label * @param array $options * @param boolean $dataAttribute * @param boolean $scriptInline * @return string */ public function tweetButton($label = null, $options = array(), $dataAttribute = false, $scriptInline = false) { $attributes = array(); $defaults = array('class' => 'twitter-share-button', 'url' => '', 'via' => '', 'text' => '', 'related' => '', 'count' => 'horizontal', 'lang' => 'en', 'counturl' => ''); if (empty($label)) { $label = 'Tweet'; } $options = am($defaults, $options); $attributes['class'] = $options['class']; unset($options['class']); $options['count'] = strtolower($options['count']); if (!in_array($options['count'], array('none', 'horizontal', 'vertical'))) { $options['count'] = 'none'; } $options = Set::filter($options); if ($dataAttribute) { foreach ($options as $key => $val) { $attributes['data-' . $key] = $val; } $options = array(); } $out = $this->Html->link($label, 'http://twitter.com/share' . Router::queryString($options), $attributes); $out .= $this->Html->script('http://platform.twitter.com/widgets.js', array('inline' => $scriptInline)); return $this->output($out); }
/** * Normalizes a URL for purposes of comparison. Will strip the base path off * and replace any double /'s. It will not unify the casing and underscoring * of the input value. * * @param mixed $url URL to normalize Either an array or a string url. * @return string Normalized URL * @access public * @static */ function normalize($url = '/', $querystring = null) { if (is_array($url)) { $url = Router::url($url); } elseif (preg_match('/^[a-z\\-]+:\\/\\//', $url)) { return $url; } $paths = Router::getPaths(); if (!empty($paths['base']) && stristr($url, $paths['base'])) { $url = preg_replace('/^' . preg_quote($paths['base'], '/') . '/', '', $url, 1); } $url = '/' . $url; while (strpos($url, '//') !== false) { $url = str_replace('//', '/', $url); } $url = preg_replace('/(?:(\\/$))/', '', $url); if (empty($url)) { $url = '/'; } if (!is_null($querystring)) { $url .= Router::queryString($querystring); } return $url; }