Exemple #1
0
 /**
  * @return bool
  */
 public function readElementContent()
 {
     if ($this->elementContent !== null) {
         $this->elementContent = null;
     }
     if (!$this->readToBufferIfEmpty()) {
         return false;
     }
     do {
         $pos = stripos($this->buffer, 'BEGIN:VCARD', 0);
     } while ($pos === false && $this->readToBuffer());
     if ($pos === false) {
         return false;
     }
     $beginPos = $pos;
     do {
         $pos = stripos($this->buffer, 'END:VCARD', $beginPos + 11);
         // 11 is strlen('BEGIN:VCARD')
     } while ($pos === false && $this->readToBuffer());
     if ($pos === false) {
         return false;
     }
     $endPos = $pos;
     $borderPos = $endPos + 9;
     // 9 is strlen('END:VCARD')
     $this->elementContent = substr($this->buffer, $beginPos, $borderPos - $beginPos);
     $this->buffer = substr($this->buffer, $borderPos);
     $this->elementBorderPosition = $this->filePosition - Main\Text\String::getBinaryLength($this->buffer);
     return $this->elementContent !== '';
 }
 /**
  * Set new secret
  *
  * @param string $secret Secret (binary).
  * @return $this
  */
 public function setSecret($secret)
 {
     $this->secret = $secret;
     // Backward compatibility. Use sha256 for eToken with 256bits key
     if (\Bitrix\Main\Text\String::getBinaryLength($this->secret) > 25) {
         $this->digest = 'sha256';
     }
     return $this;
 }
Exemple #3
0
 /**
  * Returns pseudo random block
  *
  * @return string
  */
 protected function getPseudoRandomBlock()
 {
     global $APPLICATION;
     if (function_exists('openssl_random_pseudo_bytes')) {
         $bytes = openssl_random_pseudo_bytes(64);
         if ($bytes && String::getBinaryLength($bytes) >= 64) {
             return String::getBinarySubstring($bytes, 0, 64);
         }
     }
     $bytes = '';
     for ($i = 0; $i < 64; $i++) {
         $bytes .= pack('S', mt_rand(0, 0xffff));
     }
     $bytes .= $APPLICATION->getServerUniqID();
     return hash('sha512', $bytes, true);
 }
 /**
  * Returns pseudo random block
  *
  * @return string
  */
 protected function getPseudoRandomBlock()
 {
     global $APPLICATION;
     if (static::isOpensslSkipped()) {
         $bytes = openssl_random_pseudo_bytes(static::RANDOM_BLOCK_LENGTH);
         if ($bytes && String::getBinaryLength($bytes) >= static::RANDOM_BLOCK_LENGTH) {
             return String::getBinarySubstring($bytes, 0, static::RANDOM_BLOCK_LENGTH);
         }
     }
     $bytes = '';
     for ($i = 0; $i < static::RANDOM_BLOCK_LENGTH; $i++) {
         $bytes .= pack('S', mt_rand(0, 0xffff));
     }
     $bytes .= $APPLICATION->getServerUniqID();
     return hash('sha512', $bytes, true);
 }
Exemple #5
0
 protected function readResponse()
 {
     $headers = "";
     while (!feof($this->resource)) {
         $line = fgets($this->resource, self::BUF_READ_LEN);
         if ($line == "\r\n" || $line === false) {
             break;
         }
         $headers .= $line;
         if ($this->streamTimeout > 0) {
             $info = stream_get_meta_data($this->resource);
             if ($info['timed_out']) {
                 break;
             }
         }
     }
     $this->parseHeaders($headers);
     if ($this->redirect && ($location = $this->responseHeaders->get("Location")) !== null && $location != '') {
         //do we need entity body on redirect?
         return;
     }
     if ($this->responseHeaders->get("Transfer-Encoding") == "chunked") {
         while (!feof($this->resource)) {
             /*
             chunk = chunk-size [ chunk-extension ] CRLF
             		chunk-data CRLF
             chunk-size = 1*HEX
             chunk-extension = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
             */
             $line = fgets($this->resource, self::BUF_READ_LEN);
             if ($line == "\r\n") {
                 continue;
             }
             if (($pos = strpos($line, ";")) !== false) {
                 $line = substr($line, 0, $pos);
             }
             $length = hexdec($line);
             while ($length > 0) {
                 $buf = $this->receive($length);
                 if ($buf === false) {
                     break 2;
                 }
                 $length -= String::getBinaryLength($buf);
                 if ($this->streamTimeout > 0) {
                     $info = stream_get_meta_data($this->resource);
                     if ($info['timed_out']) {
                         break 2;
                     }
                 }
             }
         }
     } else {
         while (!feof($this->resource)) {
             $buf = $this->receive();
             if ($buf === false) {
                 break;
             }
             if ($this->streamTimeout > 0) {
                 $info = stream_get_meta_data($this->resource);
                 if ($info['timed_out']) {
                     break;
                 }
             }
         }
     }
     if ($this->responseHeaders->get("Content-Encoding") == "gzip") {
         $this->decompress();
     }
 }
 /**
  * Writes cache into the file.
  *
  * @param mixed $arAllVars Cached result.
  * @param string $baseDir Base cache directory (usually /bitrix/cache).
  * @param string $initDir Directory within base.
  * @param string $filename File name.
  * @param integer $TTL Expiration period in seconds.
  *
  * @return void
  */
 public function write($arAllVars, $baseDir, $initDir, $filename, $TTL)
 {
     static $search = array("\\", "'", "");
     static $replace = array("\\\\", "\\'", "'.chr(0).'");
     $documentRoot = Main\Loader::getDocumentRoot();
     $folder = $documentRoot . "/" . ltrim($baseDir . $initDir, "/");
     $fn = $folder . $filename;
     $fnTmp = $folder . md5(mt_rand()) . ".tmp";
     if (!CheckDirPath($fn)) {
         return;
     }
     if ($handle = fopen($fnTmp, "wb+")) {
         if (is_array($arAllVars)) {
             $contents = "<?";
             $contents .= "\nif(\$INCLUDE_FROM_CACHE!='Y')return false;";
             $contents .= "\n\$datecreate = '" . str_pad(mktime(), 12, "0", STR_PAD_LEFT) . "';";
             $contents .= "\n\$dateexpire = '" . str_pad(mktime() + intval($TTL), 12, "0", STR_PAD_LEFT) . "';";
             $contents .= "\n\$ser_content = '" . str_replace($search, $replace, serialize($arAllVars)) . "';";
             $contents .= "\nreturn true;";
             $contents .= "\n?>";
         } else {
             $contents = "BX" . str_pad(mktime(), 12, "0", STR_PAD_LEFT) . str_pad(mktime() + intval($this->TTL), 12, "0", STR_PAD_LEFT);
             $contents .= $arAllVars;
         }
         $this->written = fwrite($handle, $contents);
         $this->path = $fn;
         $len = Main\Text\String::getBinaryLength($contents);
         fclose($handle);
         static::unlink($fn);
         if ($this->written === $len) {
             rename($fnTmp, $fn);
         }
         static::unlink($fnTmp);
     }
 }
Exemple #7
0
 /**
  * Write optimized css, js files or info file
  *
  * @param string $filePath - Path for optimized css, js or info file
  * @param string $content - File contents
  * @param bool $gzip - For disabled gzip
  * @return bool - TRUE or FALSE result
  */
 function write($filePath, $content, $gzip = true)
 {
     $result = false;
     $fnTmp = $filePath . '.tmp';
     if (!CheckDirPath($filePath) || !($fh = fopen($fnTmp, "wb"))) {
         return $result;
     }
     $written = fwrite($fh, $content);
     $len = Main\Text\String::getBinaryLength($content);
     fclose($fh);
     self::unlink($filePath);
     if ($written === $len) {
         $result = true;
         rename($fnTmp, $filePath);
         if ($gzip && self::gzipEnabled()) {
             $fnTmpGz = $filePath . '.tmp.gz';
             $fnGz = $filePath . '.gz';
             if ($gz = gzopen($fnTmpGz, 'wb9f')) {
                 $writtenGz = @gzwrite($gz, $content);
                 gzclose($gz);
                 self::unlink($fnGz);
                 if ($writtenGz === $len) {
                     rename($fnTmpGz, $fnGz);
                 }
                 self::unlink($fnTmpGz);
             }
         }
     }
     self::unlink($fnTmp);
     return $result;
 }
Exemple #8
0
 public function SetPos($iCurPos = 0)
 {
     $iCurPos = intval($iCurPos);
     if ($iCurPos <= $this->iFileLength) {
         $this->iCurPos = $iCurPos;
     } else {
         $this->iCurPos = $this->iFileLength;
     }
     $pos = $this->iCurPos;
     if ($this->__hasBOM) {
         $pos += 3;
     }
     fseek($this->__file, $pos);
     if (feof($this->__file)) {
         $this->__buffer = "";
     } else {
         $this->__buffer = fread($this->__file, 1024 * 1024);
     }
     $this->__buffer_size = String::getBinaryLength($this->__buffer);
     $this->__buffer_pos = 0;
 }
Exemple #9
0
 /**
  * @param int $iblockId This variable is the id iblock.
  * @return string
  * @throws Main\ArgumentNullException
  * @throws Main\ArgumentOutOfRangeException
  */
 public static function export($iblockId)
 {
     $iblockId = intval($iblockId);
     if ($iblockId <= 0) {
         throw new Main\ArgumentNullException("iblockId");
     }
     $db = \CIBlock::GetList(array(), array("ID" => $iblockId, "CHECK_PERMISSIONS" => "N"));
     $iblock = $db->Fetch();
     if (!$iblock) {
         throw new Main\ArgumentOutOfRangeException("iblockId");
     }
     if (!$iblock["CODE"]) {
         throw new Main\ArgumentException("Parameter 'CODE' is required.", "matches");
     }
     $list = new \CList($iblockId);
     $fields = $list->getFields();
     foreach ($fields as $fieldId => $field) {
         if ($field["TYPE"] == "NAME") {
             $iblock["~NAME_FIELD"] = array("NAME" => $field["NAME"], "SETTINGS" => $field["SETTINGS"], "DEFAULT_VALUE" => $field["DEFAULT_VALUE"], "SORT" => $field["SORT"]);
             break;
         }
     }
     $iblockUtf8 = Main\Text\Encoding::convertEncodingArray($iblock, LANG_CHARSET, "UTF-8");
     $iblockUtf8 = serialize($iblockUtf8);
     $iblockUtf8Length = Main\Text\String::getBinaryLength($iblockUtf8);
     $datum = str_pad($iblockUtf8Length, 10, "0", STR_PAD_LEFT) . $iblockUtf8;
     if (intval($iblock["PICTURE"]) > 0) {
         $picture = \CFile::MakeFileArray($iblock["PICTURE"]);
         if (isset($picture["tmp_name"]) && !empty($picture["tmp_name"])) {
             $f = fopen($picture["tmp_name"], "rb");
             $pictureData = fread($f, filesize($picture["tmp_name"]));
             fclose($f);
             $pictureTypeLength = Main\Text\String::getBinaryLength($picture["type"]);
             $pictureLength = Main\Text\String::getBinaryLength($pictureData);
             $datum .= "P" . str_pad($pictureTypeLength, 10, "0", STR_PAD_LEFT) . $picture["type"] . str_pad($pictureLength, 10, "0", STR_PAD_LEFT) . $pictureData;
         }
     }
     $documentType = self::getDocumentType($iblock["IBLOCK_TYPE_ID"], $iblockId);
     $templatesList = \CBPWorkflowTemplateLoader::GetList(array(), array("DOCUMENT_TYPE" => $documentType), false, false, array("ID", "AUTO_EXECUTE", "NAME", "DESCRIPTION", "SYSTEM_CODE"));
     while ($templatesListItem = $templatesList->Fetch()) {
         $bpDescrUtf8 = Main\Text\Encoding::convertEncodingArray($templatesListItem, LANG_CHARSET, "UTF-8");
         $bpDescrUtf8 = serialize($bpDescrUtf8);
         $bpDescrUtf8Length = Main\Text\String::getBinaryLength($bpDescrUtf8);
         $datum .= "B" . str_pad($bpDescrUtf8Length, 10, "0", STR_PAD_LEFT) . $bpDescrUtf8;
         $bp = \CBPWorkflowTemplateLoader::ExportTemplate($templatesListItem["ID"], false);
         $bpLength = Main\Text\String::getBinaryLength($bp);
         $datum .= str_pad($bpLength, 10, "0", STR_PAD_LEFT) . $bp;
     }
     if (function_exists("gzcompress")) {
         $datum = "compressed" . gzcompress($datum, 9);
     }
     return $datum;
 }