/**
  * 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);
 }
Beispiel #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, "/");
 }
 /**
  * Sets header key.
  *
  * @param string $name  Header key
  * @param string $value Value
  *
  * @return void
  */
 public function setHeaderKey($name, $value)
 {
     // fixes headers by replacing '\n' by '\r\n'
     $header = $this->getOption("header");
     $header = str_replace(array("\r", "\n"), array("", "\r\n"), $header);
     // replaces entry
     $count = 0;
     $regexp = '/^(\\s*' . preg_quote($name) . '\\s*)\\:(.*)/mi';
     $header = preg_replace($regexp, "\$1: {$value}", $header, -1, $count);
     // ... or appends entry
     if ($count == 0) {
         $header = Text::concat("\r\n", $header, "{$name}: {$value}");
     }
     $this->setOption("header", $header);
 }
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * Gets a string representation of the node.
  *
  * @return string
  */
 public function __toString()
 {
     $ret = "";
     foreach ($this->elements as $element) {
         $ret = Text::concat("\n", $ret, Dom::dom2str($element));
     }
     return $ret;
 }
Beispiel #6
0
 /**
  * Removes left spaces from a multiline string.
  *
  * <p>This function removes left spaces from a multiline string, so the first line
  * starts at the first column. It would be the equivalent to 'align to left' in
  * a text editor.</p>
  *
  * @param string $str Multiline string
  *
  * @return string
  */
 public static function trimText($str)
 {
     $ret = "";
     $str = preg_replace("/\t/", "    ", $str);
     $lines = explode("\n", $str);
     $len = count($lines);
     // start index.
     // ignores initial empty lines.
     $i0 = 0;
     for ($i = 0; $i < $len; $i++) {
         $line = $lines[$i];
         $trimLine = trim($line);
         if (strlen($trimLine) > 0) {
             $i0 = $i;
             break;
         }
     }
     // final index.
     // ignores final empty lines.
     $i1 = count($lines) - 1;
     for ($i = $len - 1; $i >= 0; $i--) {
         $line = $lines[$i];
         $trimLine = trim($line);
         if (strlen($trimLine) > 0) {
             $i1 = $i;
             break;
         }
     }
     // calculates spaces to remove
     $spaces = PHP_INT_MAX;
     for ($i = $i0; $i <= $i1; $i++) {
         $line = $lines[$i];
         $leftTrimLine = ltrim($line);
         $spaces = min($spaces, strlen($line) - strlen($leftTrimLine));
     }
     // removes left spaces
     for ($i = $i0; $i <= $i1; $i++) {
         $line = $lines[$i];
         $ret = Text::concat("\n", $ret, substr($line, $spaces));
     }
     return $ret;
 }
 /**
  * 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;
 }
Beispiel #9
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);
     }
 }
Beispiel #10
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));
 }