Example #1
0
 /**
  * 
  * Returns an escaped href or src attribute value for a generic URI.
  * 
  * @param Solar_Uri|string $spec The href or src specification.
  * 
  * @return string
  * 
  */
 public function href($spec)
 {
     if ($spec instanceof Solar_Uri) {
         // fetch the full href, not just the path/query/fragment
         $href = $spec->get(true);
     } else {
         $href = $spec;
     }
     return $this->_view->escape($href);
 }
 protected function _generateBaseUri()
 {
     $uri = new Solar_Uri();
     $uri->setPath($this->_config['dbname']);
     $uri->host = $this->_config['host'];
     $uri->port = $this->_config['port'];
     if ($this->_config['encrypted'] == false) {
         $uri->scheme = 'http';
     } else {
         $uri->scheme = 'https';
     }
     if (isset($this->_config['user']) and isset($this->_config['password'])) {
         $uri->user = $this->_config['user'];
         $uri->pass = $this->_config['password'];
     }
     return $uri;
 }
Example #3
0
 /**
  * 
  * Checks the server variables to see if we have a SOLAR_URI_ACTION_PATH
  * value set from Apache; also pre-sets $this->_request.
  * 
  * In a standard solar system, when mod_rewrite is turned on, it
  * may "SetEnv SOLAR_URI_ACTION_PATH /" as a hint for the default
  * action path. This lets you go from no-rewriting to rewriting in
  * one easy step, rather than having to remember to change the action
  * path in the Solar.config.php file as well.
  * 
  * @return void
  * 
  */
 protected function _preConfig()
 {
     parent::_preConfig();
     $this->_request = Solar_Registry::get('request');
     $this->_Solar_Uri_Action['path'] = $this->_request->server('SOLAR_URI_ACTION_PATH', '/index.php');
 }
Example #4
0
 /**
  * 
  * Checks the URI to see what page name and controller class we should
  * route to.
  * 
  * @param Solar_Uri $uri The URI to route on.
  * 
  * @return void
  * 
  */
 protected function _routing($uri)
 {
     // first path-element is the page-name.
     $page = strtolower(reset($uri->path));
     if (empty($page)) {
         // page-name is blank. get the default class.
         // remove the empty element from the path.
         $class = $this->_getPageClass($this->_default);
         array_shift($uri->path);
         $this->_explain['routing_page'] = "empty, using default page '{$this->_default}'";
     } elseif (in_array($page, $this->_disable)) {
         // page-name is disabled. get the default class.
         // leave existing elements in the path.
         $class = $this->_getPageClass($this->_default);
         $this->_explain['routing_page'] = "'{$page}' disabled, using default page '{$this->_default}'";
     } else {
         // look for a controller for the requested page.
         $class = $this->_getPageClass($page);
         if (!$class) {
             // no class for the page-name. get the default class.
             // leave existing elements in the path.
             $class = $this->_getPageClass($this->_default);
             $this->_explain['routing_page'] = "no class for page '{$page}', using default page '{$this->_default}'";
         } else {
             // found a class. don't need the page-name any more, so
             // remove it from the path.
             array_shift($uri->path);
             $this->_explain['routing_page'] = $page;
         }
     }
     $this->_explain['routing_class'] = $class;
     $this->_explain['routing_uri'] = $uri->getFrontPath();
     return array($page, $class);
 }
Example #5
0
 /**
  * 
  * Given a URI path, matches it against a rewrite rule and returns the
  * rewritten path.
  * 
  * @param string|Solar_Uri $spec The original URI path.
  * 
  * @return string The rewritten path.
  * 
  */
 public function match($spec)
 {
     $this->_explain = null;
     // pre-empt if no rules
     if (!$this->_rewrite) {
         $this->_explain = 'no rules';
         return;
     }
     // convert spec to a path
     if ($spec instanceof Solar_Uri_Action) {
         $oldpath = trim($spec->getFrontPath(), '/');
     } elseif ($spec instanceof Solar_Uri) {
         $oldpath = trim($spec->getPath());
     } else {
         $oldpath = $spec;
     }
     // go through each of the rules to find a match
     foreach ($this->_rewrite as $name => $info) {
         if (!is_array($info)) {
             // shorthand format
             $info = array('pattern' => $name, 'rewrite' => $info, 'replace' => array(), 'default' => array());
         } else {
             // longhand format
             $info += $this->_default;
         }
         // consolidate replacements
         $replace = $info['replace'] + $this->_replace;
         // convert replacements in the pattern
         $pattern = str_replace(array_keys($replace), array_values($replace), $info['pattern']);
         // trim slashes and wrap as a full regex
         $pattern = '#^' . trim($pattern, '/') . '$#';
         // is it a match?
         if (preg_match($pattern, $oldpath)) {
             // rewrite to new path and trim slashes
             $rewrite = trim($info['rewrite'], '/');
             $newpath = preg_replace($pattern, $rewrite, $oldpath);
             $this->_explain = "matched rule '{$name}'";
             return trim($newpath, '/');
         }
     }
     $this->_explain = 'no matches';
 }
Example #6
0
 /**
  * 
  * Prepares $this->_headers, $this->_cookies, and $this->content for the
  * request.
  * 
  * @return array A sequential array where element 0 is a URI object,
  * element 1 is string of headers (including cookies), and element 2 is a 
  * string of content.
  * 
  * @todo Only generate $content on POST and PUT?
  * 
  */
 protected function _prepareRequest()
 {
     // get the URI
     if (!$this->_uri) {
         throw $this->_exception('ERR_NO_URI');
     } else {
         $uri = clone $this->_uri;
     }
     // what kind of request is this?
     $is_get = $this->_method == Solar_Http_Request::METHOD_GET;
     $is_post = $this->_method == Solar_Http_Request::METHOD_POST;
     $is_put = $this->_method == Solar_Http_Request::METHOD_PUT;
     // do we have any body content?
     if (is_array($this->content) && ($is_post || $is_put)) {
         // is a POST or PUT with a data array.
         // convert from array and force the content-type.
         $content = http_build_query($this->content);
         $content_type = 'application/x-www-form-urlencoded';
     } elseif (is_array($this->content) && $is_get) {
         // is a GET with a data array.
         // merge the content array to the cloned uri query params.
         $uri->query = array_merge($uri->query, $this->content);
         // now clear out the content
         $content = null;
         $content_type = null;
     } elseif (is_string($this->content)) {
         // honor as set by the user
         $content = $this->content;
         $content_type = $this->_content_type;
     } else {
         // no recognizable content
         $content = null;
         $content_type = null;
     }
     // get a list of the headers as they are now
     $list = $this->_headers;
     // force the content-type header if needed
     if ($content_type) {
         if ($this->_charset) {
             $content_type .= "; charset={$this->_charset}";
         }
         $list['Content-Type'] = $content_type;
     }
     // auto-set the content-length
     if ($this->_config['auto_set_length']) {
         if ($content) {
             $list['Content-Length'] = strlen($content);
         } else {
             unset($list['Content-Length']);
         }
     }
     // force the user-agent header if needed
     if ($this->_user_agent) {
         $list['User-Agent'] = $this->_user_agent;
     }
     // force the referer if needed
     if ($this->_referer) {
         $list['Referer'] = $this->_referer->get(true);
     }
     // convert the list of all header values to a sequential array
     $headers = array();
     foreach ($list as $key => $set) {
         settype($set, 'array');
         foreach ($set as $val) {
             $headers[] = Solar_Mime::headerLine($key, $val);
         }
     }
     // create additional cookies in the headers array
     if ($this->_cookies) {
         $val = array();
         foreach ($this->_cookies as $name => $data) {
             $val[] = "{$name}=" . urlencode($data);
         }
         $headers[] = Solar_Mime::headerLine('Cookie', implode(';', $val));
     }
     // done!
     return array($uri, $headers, $content);
 }
Example #7
0
 /**
  * Queries the couchdb database directly
  * 
  * @param Solar_Uri          $uri    The uri object to use
  * @param Solar_Http_Request $request The request object to use
  * 
  * @return array the result
  * @author Bahtiar Gadimov <*****@*****.**>
  */
 public function query(Solar_Uri $uri, Solar_Http_Request_Adapter $request = null)
 {
     if ($request == null) {
         $request = $this->getHttpRequest();
     }
     //print($uri->get(true)."\n\n");
     //print("\n");
     //Solar::dump($request);
     //print("\n");
     $request->setUri($uri->get(true));
     $json = $request->fetch()->content;
     $result = json_decode($json, true);
     if (isset($result['error']) && isset($result['reason'])) {
         $this->_createException($result);
     }
     return $result;
 }