/**
  * 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);
 }
 /**
  * Gets 'form data'.
  *
  * This function is used to set the 'content' option. For example:
  * ```php
  * $config = new HttpRequestConfig();
  * $config->setOption('content', $this->getFormData());
  * ```
  *
  * @return string
  */
 public function getFormData()
 {
     $ret = "";
     if (count($this->formParams) > 0) {
         foreach ($this->formParams as $name => $param) {
             $data = $param->getData();
             $isArray = is_array($data);
             $items = $isArray ? $data : array($data);
             $suffix = count($items) > 1 ? "[]" : "";
             foreach ($items as $key => $item) {
                 $str = "--" . $this->_formBoundary;
                 $suffix = $isArray ? "[{$key}]" : "";
                 $contentDisposition = "Content-Disposition: form-data; name=\"" . str_replace("\"", '\\"', $name) . "{$suffix}\"";
                 $filename = $param->getFilename();
                 if (!Text::isEmpty($filename)) {
                     $contentDisposition .= "; filename=" . urlencode($filename);
                 }
                 $contentType = "";
                 $mimeType = $param->getMimeType();
                 if (!Text::isEmpty($mimeType)) {
                     $contentType = "Content-Type: {$mimeType}";
                 }
                 $content = "\n{$item}";
                 $str = Text::concat("\n", $str, $contentDisposition, $contentType, $content);
                 $ret = Text::concat("\n", $ret, $str);
             }
         }
         $ret = Text::concat("\n", $ret, "--" . $this->_formBoundary . "--\r\n");
     }
     return $ret;
 }
Example #3
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;
 }
Example #4
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;
 }
 /**
  * Gets inner HTML code.
  *
  * @return string
  */
 private function _getInnerHtml()
 {
     $ret = "";
     foreach ($this->elements() as $element) {
         $childNodes = $element->childNodes;
         $str = "";
         foreach ($childNodes as $node) {
             $str .= Dom::dom2str($node);
         }
         $ret = Text::concat("\n", $ret, $str);
     }
     return $ret;
 }
Example #6
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));
 }