/**
  * Constructor.
  *
  * @param string $path     Path to file
  * @param string $mimeType MIME type (not required)
  * @param string $filename Filename (not required)
  */
 public function __construct($path, $mimeType = "", $filename = "")
 {
     $contents = file_get_contents($path);
     parent::__construct($contents);
     // sets mimetype
     if (Text::isEmpty($mimeType)) {
         $result = new Finfo(FILEINFO_MIME);
         $mimeType = $result->buffer($contents);
         if ($mimeType === false) {
             throw new HttpRequestException("Error detecting MIME type");
         }
     }
     $this->setMimeType($mimeType);
     // sets filename
     if (Text::isEmpty($filename)) {
         $filename = basename($path);
     }
     $this->setFilename($filename);
 }
Пример #2
0
 /**
  * Gets the current URI path.
  * 
  * This method gets the current URI excluding the query and the fragment parts.
  * 
  * For example,
  * http://hostname.com/path/to/resource?param1=1&param2=2 --> http://hostname.com/path/to/resource
  * 
  * @return string
  */
 private function _getUriPath()
 {
     // user info part
     $userInfo = $this->_username;
     if (!Text::isEmpty($this->_password)) {
         $userInfo .= ":" . $this->_password;
     }
     if (!Text::isEmpty($userInfo)) {
         $userInfo = $userInfo . "@";
     }
     $scheme = Text::isEmpty($this->_scheme) ? "" : $this->_scheme . ":";
     $port = Text::isEmpty($this->_port) ? "" : ":" . $this->_port;
     $authority = Text::isEmpty($this->_host) ? "" : "//" . $userInfo . $this->_host . $port;
     return $scheme . $authority . rtrim($this->_path, "/");
 }
 /**
  * Sends a HTTP request and returns contents.
  *
  * @param string $url URL
  *
  * @return string
  */
 public function send($url)
 {
     $config = clone $this->config;
     $this->prepare($config);
     if (Text::isEmpty($config->getOption("content"))) {
         $config->setOption("content", $this->getFormData());
     }
     $context = stream_context_create(array("http" => $config->getOptions()));
     $contents = @file_get_contents(Http::addParams($url, $this->params), false, $context);
     // Checks for erros
     // The function file_get_contents populates the local variable $http_response_header
     // This is not a great idea, but it is what it is.
     // For more info: http://php.net/manual/es/reserved.variables.httpresponseheader.php
     if ($contents === false) {
         $headers = implode("\n", $http_response_header);
         $error = "Failed to open {$url}";
         if (preg_match('/^\\s*HTTP(\\/[\\d\\.]+)?\\s+([45]\\d{2})\\s+(.*)/mi', $headers, $matches)) {
             $statusMessage = trim($matches[0]);
             $errorCode = $matches[2];
             $errorMessage = $matches[3];
             $error = $errorCode == 404 ? "Url not found: {$url}" : "Failed to open {$url}:\n" . $statusMessage;
         }
         throw new HttpRequestException($error);
     }
     return $contents;
 }
Пример #4
0
 /**
  * Creates an instance from a given string.
  *
  * @param string $str         Well formed document
  * @param string $contentType Content Type (default is "text/xml")
  *
  * @return DomNode
  */
 public static function createFromString($str, $contentType = "text/xml")
 {
     $doc = new DOMDocument("1.0");
     $doc->preserveWhiteSpace = false;
     $doc->formatOutput = true;
     // use internal errors
     $useInternalErrors = libxml_use_internal_errors(true);
     if ($contentType == "text/html") {
         $doc->loadHTML($str);
     } else {
         $doc->loadXML($str);
     }
     // retrieves the errors
     $text = "";
     $errors = libxml_get_errors();
     foreach ($errors as $error) {
         $message = trim($error->message);
         $text = Text::concat("\n", $text, "{$message} on line {$error->line}, column {$error->column}");
     }
     libxml_clear_errors();
     // restores internal errors status
     libxml_use_internal_errors($useInternalErrors);
     if (!Text::isEmpty($text)) {
         throw new DomNodeException($text);
     }
     $node = new static();
     $node->document = $doc;
     $node->elements = array($doc->documentElement);
     return $node;
 }
Пример #5
0
 /**
  * Returns $def if $str is empty.
  *
  * @param string $str A string
  * @param mixed  $def Default value
  *
  * @return mixed
  */
 public static function ifEmpty($str, $def)
 {
     return Text::isEmpty($str) ? $def : $str;
 }
Пример #6
0
 /**
  * Constructor.
  * 
  * Examples:
  * ```php
  * // the following instance represents a NEW record
  * $r = new DbRecord($db, "my_table");
  * 
  * // the following instance represents an EXISTING record
  * $r = new DbRecord($db, "my_table", $id);
  * 
  * // By default the primary key name is "id".
  * // But you can change it in the constructor:
  * $r = new DbRecord($db, "my_table", ["pk" => ""]);  // new record
  * $r = new DbRecord($db, "my_table", ["pk" => $id]); // existing record
  * ```
  * 
  * @param DbConnector $db        Database connector
  * @param string      $tableName Table name
  * @param mixed|array $pk        Primary key (not required)
  */
 public function __construct($db, $tableName, $pk = ["id" => ""])
 {
     // gets the primary key and value
     $pkName = "";
     $pkValue = "";
     if (!is_array($pk)) {
         $pk = ["id" => "{$pk}"];
     }
     foreach ($pk as $key => $value) {
         $pkName = "{$key}";
         $pkValue = "{$value}";
         break;
     }
     $this->_db = $db;
     $this->_tableName = $tableName;
     $this->_primaryKey = new DbRecordColumn($this, $pkName);
     if (!Text::isEmpty($pkValue)) {
         $this->_primaryKey->setValue($pkValue);
     }
 }
 /**
  * Sets inner HTML code.
  *
  * @param string $value Inner HTML code
  *
  * @return DomNode
  */
 private function _setInnerHtml($value)
 {
     $this->clear();
     if (!Text::isEmpty($value)) {
         foreach ($this->elements() as $element) {
             $doc = $element->ownerDocument;
             $fragment = $doc->createDocumentFragment();
             @$fragment->appendXML($value);
             $node = @$element->appendChild($fragment);
             if ($node === false) {
                 throw new DomNodeException("Invalid XML fragment");
             }
         }
     }
     return $this;
 }
Пример #8
0
 /**
  * Adds a debug listener.
  * 
  * @param Callable $listener Listener
  * @param string   $type     Filter type ('exec' or 'query'. Not required)
  * 
  * @return void
  */
 public function addDebugListener($listener, $type = "")
 {
     if (!Text::isEmpty($type) && !in_array($type, ["exec", "query"])) {
         throw new DbException("Invalid filter type. Valid filter types are 'exec' and 'query'");
     }
     $types = Text::isEmpty($type) ? ["exec", "query"] : [$type];
     foreach ($types as $type) {
         $this->_debugger->on($type, $listener);
     }
 }
Пример #9
0
 /**
  * Appends parameters to a given url.
  *
  * For example:
  * ```php
  * echo Http::addParams("http://www.mysite.php", array("username" => "John", "id" => 101));
  * ```
  *
  * @param string $url    URL
  * @param array  $params Associative array of parameters
  *
  * @return strings.
  */
 public static function addParams($url, $params)
 {
     $query = parse_url($url, PHP_URL_QUERY);
     $separator = Text::isEmpty($query) ? "?" : "&";
     return Text::concat($separator, $url, http_build_query($params));
 }