Example #1
0
 /**
  * Turn a string into a in-memory file resource.
  * Remember to close it when done to free up the used memory.
  *
  * @param string $data
  *
  * @return resource the in-memory file handle.
  */
 public static function str2resource($data)
 {
     $length = BinStringStatic::_strlen($data);
     $handle = fopen("php://temp/maxmemory:{$length}", 'r+');
     fputs($handle, $data);
     rewind($handle);
     return $handle;
 }
Example #2
0
 public function getComment()
 {
     $pos = 0;
     $len = BinStringStatic::_strlen($this->dataSubBlocks);
     $comment = "";
     while ($pos < $len && ord($this->dataSubBlocks[$pos]) > 0) {
         $comment .= BinStringStatic::_substr($this->dataSubBlocks, $pos + 1, ord($this->dataSubBlocks[$pos]));
         $pos += 1 + ord($this->dataSubBlocks[$pos]);
     }
     return $comment;
 }
 public function __toString()
 {
     $this->zipCommentLength = BinStringStatic::_strlen($this->zipComment);
     $eocd = AbstractZipHeader::ZIP_END_OF_CENTRAL_DIRECTORY;
     $eocd .= pack("vvvv", $this->thisDisk, $this->firstCDDisk, $this->cdrCount1, $this->cdrCount2);
     $eocd .= pack("VVv", $this->cdrLength, $this->cdrStart, $this->zipCommentLength);
     if ($this->zipCommentLength > 0) {
         $eocd .= $this->zipComment;
     } else {
         $eocd .= AbstractZipHeader::NULL_WORD;
     }
     return $eocd;
 }
Example #4
0
 /**
  * @return string
  */
 public function encode()
 {
     $ctSize = BinStringStatic::_strlen($this->colorTable);
     if ($ctSize > 0) {
         $this->colorTableFlag = true;
         $this->colorTableSize = $ctSize / 3;
     } else {
         $this->colorTableFlag = false;
         $this->colorTableSize = 2;
     }
     $packedFields = 0;
     $packedFields |= $this->colorTableFlag ? 0x80 : 0x0;
     $packedFields |= $this->interlaceFlag ? 0x40 : 0x0;
     $packedFields |= $this->sortFlag ? 0x20 : 0x0;
     $packedFields |= $this->reserved << 3 & 0x18;
     $packedFields |= ceil(log($this->colorTableSize) / log(2)) - 1;
     //$packedFields |= $this->ctSizeList[$this->colorTableSize];
     // The lookup table is faster, but I wanted the Pedantic version.
     $r = DataHandler::packUint16($this->screenLeftPos) . DataHandler::packUint16($this->screenTopPos) . DataHandler::packUint16($this->screenWidth) . DataHandler::packUint16($this->screenHeight) . chr($packedFields & 0xff);
     if ($this->colorTableFlag && $this->colorTableSize > 0) {
         $r .= $this->colorTable;
     }
     return "," . $r . $this->lzwMinCodeSize . $this->dataSubBlocks . "";
 }
 /**
  * @return string
  */
 public function encode()
 {
     $ctSize = BinStringStatic::_strlen($this->colorTable);
     if ($ctSize > 0) {
         $this->colorTableFlag = true;
         $this->colorTableSize = $ctSize / 3;
     } else {
         $this->colorTableFlag = false;
         $this->colorTableSize = 2;
     }
     $packedFields = 0;
     $packedFields |= $this->colorTableFlag ? 0x80 : 0x0;
     $packedFields |= $this->colorResolution << 4 & 0x70;
     $packedFields |= $this->sortFlag ? 0x8 : 0x0;
     // For just 8 possible values, the lookup table is faster.
     // the commented line below is the pedantic way of doing it.
     //$packedFields |= ceil(log($this->colorTableSize) / log(2)) - 1;
     $packedFields |= $this->ctSizeList[$this->colorTableSize];
     $r = DataHandler::packUint16($this->screenWidth) . DataHandler::packUint16($this->screenHeight) . chr($packedFields & 0xff) . chr($this->bgColorIndex) . chr($this->pixelAspectRatio == 0 ? 0 : (int) ($this->pixelAspectRatio * 64 / 15));
     if ($this->colorTableFlag && $this->colorTableSize > 0) {
         $r .= $this->colorTable;
     }
     return $r;
 }
 /**
  * @return string The version of the field for the Local Header.
  */
 public function getLocalField()
 {
     return parent::HEADER_UNICODE_PATH . pack('vV', BinStringStatic::_strlen($this->utf8Data) + 5, $this->CRC32) . $this->version . $this->utf8Data;
 }
Example #7
0
 /**
  * Get an image from a file or url, return it resized if the image exceeds the $maxImageWidth or $maxImageHeight directives.
  *
  * The return value is an array.
  * ['width'] is the width of the image.
  * ['height'] is the height of the image.
  * ['mime'] is the mime type of the image. Resized images are always in jpeg format.
  * ['image'] is the image data.
  * ['ext'] is the extension of the image file.
  *
  * @param string $source path or url to file.
  *
  * @return array|bool
  */
 function getImage($source)
 {
     $width = -1;
     $height = -1;
     $mime = "application/octet-stream";
     $ext = "";
     $image = $this->getFileContents($source);
     $ratio = 1;
     if ($image !== false && strlen($image) > 0) {
         if (BinStringStatic::startsWith(trim($image), '<svg') || (BinStringStatic::startsWith(trim($image), '<?xml') || strpos($image, '<svg') > 0)) {
             // SVG image.
             $xml = simplexml_load_string($image);
             $attr = $xml->attributes();
             $meta = $this->handleSVGAttribs($xml);
             $mime = "image/svg+xml";
             $ext = "svg";
             $width = $meta['width'];
             $height = $meta['height'];
             $ratio = $this->getImageScale($width, $height);
             if ($ratio < 1) {
                 $attr->width = $width * $ratio;
                 $attr->height = $height * $ratio;
             }
             $image = $xml->asXML();
         } else {
             $imageFile = imagecreatefromstring($image);
             if ($imageFile !== false) {
                 $width = ImageSX($imageFile);
                 $height = ImageSY($imageFile);
             }
             if ($this->isExifInstalled) {
                 @($type = exif_imagetype($source));
                 $mime = image_type_to_mime_type($type);
             }
             if ($mime === "application/octet-stream") {
                 $mime = $this->image_file_type_from_binary($image);
             }
             if ($mime === "application/octet-stream") {
                 $mime = $this->getMimeTypeFromUrl($source);
             }
         }
     } else {
         return false;
     }
     if ($width <= 0 || $height <= 0) {
         return false;
     }
     if ($mime !== "image/svg+xml" && $this->isGdInstalled) {
         $ratio = $this->getImageScale($width, $height);
         //echo "<pre>\$source: $source\n\$ratio.: $ratio\n\$mime..: $mime\n\$width.: $width\n\$height: $height</pre>\n";
         if ($ratio < 1 || empty($mime)) {
             if ($mime == "image/png" || $this->isGifImagesEnabled === false && $mime == "image/gif") {
                 $image_o = imagecreatefromstring($image);
                 $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                 imagealphablending($image_p, false);
                 imagesavealpha($image_p, true);
                 imagealphablending($image_o, true);
                 imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                 ob_start();
                 imagepng($image_p, null, 9);
                 $image = ob_get_contents();
                 ob_end_clean();
                 imagedestroy($image_o);
                 imagedestroy($image_p);
                 $ext = "png";
             } else {
                 if ($this->isGifImagesEnabled !== false && $mime == "image/gif") {
                     /*
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     
                     imagealphablending($image_p, false);
                     imagesavealpha($image_p, true);
                     imagealphablending($image_o, true);
                     
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width * $ratio), ($height * $ratio), $width, $height);
                     ob_start();
                     imagegif($image_p, null);
                     $image = ob_get_contents();
                     ob_end_clean();
                     
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     
                     $ext = "gif";
                     */
                     $tFileD = tempnam("BewareOfGeeksBearingGifs", "grD");
                     ResizeGif::ResizeByRatio($source, $tFileD, $ratio);
                     $image = file_get_contents($tFileD);
                     unlink($tFileD);
                 } else {
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                     ob_start();
                     imagejpeg($image_p, null, 80);
                     $image = ob_get_contents();
                     ob_end_clean();
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     $mime = "image/jpeg";
                     $ext = "jpg";
                 }
             }
         }
     }
     if ($ext === "") {
         static $mimeToExt = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png', 'image/svg+xml' => "svg");
         if (isset($mimeToExt[$mime])) {
             $ext = $mimeToExt[$mime];
         }
     }
     $rv = array();
     $rv['width'] = $width * $ratio;
     $rv['height'] = $height * $ratio;
     $rv['mime'] = $mime;
     $rv['image'] = $image;
     $rv['ext'] = $ext;
     return $rv;
 }
Example #8
0
 /**
  * Compare $byteSequence with the current pointer location without moving the pointer.
  *
  * @param string $byteSequence
  *
  * @return boolean
  */
 public function compareBytes($byteSequence)
 {
     if (isset($this->handle)) {
         $length = BinStringStatic::_strlen($byteSequence);
         $data = $this->readData($length);
         $this->seekReverse($length);
         return $data === $byteSequence;
     }
     return false;
 }
 /**
  *
  * @param LogicalScreenDescriptor $lsd
  *
  * @return array|bool array of colors or FALSE
  */
 public function getTransparancyColor($lsd = null)
 {
     if (!$this->transparentColorFlag) {
         return false;
     }
     if ($this->imageDescriptor->colorTableFlag && $this->imageDescriptor->colorTableSize > 0 && BinStringStatic::_strlen($this->imageDescriptor->colorTable) > $this->transparentColorIndex * 3) {
         $ct = $this->imageDescriptor->colorTable;
     } else {
         $ct = $lsd->colorTable;
     }
     if (BinStringStatic::_strlen($this->imageDescriptor->colorTable) > $this->transparentColorIndex * 3) {
         $color = array();
         $color['red'] = $ct[$this->transparentColorIndex * 3];
         $color['green'] = $ct[$this->transparentColorIndex * 3 + 1];
         $color['blue'] = $ct[$this->transparentColorIndex * 3 + 2];
         return $color;
     }
     return false;
 }
Example #10
0
 /**
  * @param $doc
  *
  * @return string
  */
 public static function removeComments($doc)
 {
     $doc = preg_replace('~--\\s+>~', '-->', $doc);
     $doc = preg_replace('~<\\s*!\\s*--~', '<!--', $doc);
     $cPos = BinStringStatic::_strpos($doc, "<!--");
     if ($cPos !== false) {
         $startCount = substr_count($doc, "<!--");
         $endCount = substr_count($doc, "-->");
         $lastCPos = -1;
         while ($cPos !== false && $lastCPos != $cPos) {
             $lastCPos = $cPos;
             $lastEPos = $cPos;
             $ePos = $cPos;
             do {
                 $ePos = BinStringStatic::_strpos($doc, "-->", $ePos + 1);
                 if ($ePos !== false) {
                     $lastEPos = $ePos;
                     $comment = BinStringStatic::_substr($doc, $cPos, $lastEPos + 3 - $cPos);
                     $startCount = substr_count($comment, "<!--");
                     $endCount = substr_count($comment, "-->");
                 } elseif ($lastEPos == $cPos) {
                     $lastEPos = BinStringStatic::_strlen($doc) - 3;
                 }
             } while ($startCount != $endCount && $ePos !== false);
             $doc = substr_replace($doc, "", $cPos, $lastEPos + 3 - $cPos);
             $cPos = BinStringStatic::_strpos($doc, "<!--");
         }
     }
     // print "<pre>\n" . htmlentities($doc) . "\n</pre>\n";
     return $doc;
 }
Example #11
0
 /**
  * Send the book as a zip download
  *
  * Sending will fail if the output buffer is in use. You can override this limit by
  *  calling setIgnoreEmptyBuffer(TRUE), though the function will still fail if that
  *  buffer is not empty.
  *
  * @param string $fileName The name of the book without the .epub at the end.
  *
  * @return string|bool The sent file name if successful, FALSE if it failed.
  */
 function sendBook($fileName)
 {
     if (!$this->isFinalized) {
         $this->finalize();
     }
     if (!BinStringStatic::endsWith($fileName, ".epub")) {
         $fileName .= ".epub";
     }
     if (true === $this->zip->sendZip($fileName, "application/epub+zip")) {
         return $fileName;
     }
     return false;
 }
Example #12
0
 public static function encodeField($header, $data)
 {
     if ($header != null) {
         return $header . pack('v', BinStringStatic::_strlen($data)) . $data;
     }
     return '';
 }
Example #13
0
    /**
     *
     * @author A. Grandt <*****@*****.**>
     * @author Greg Kappatos
     *
     * @return string The full path to a temporary file.
     */
    public static function getTemporaryFile() {
        if (is_callable(self::$temp)) {
            $file = @call_user_func(self::$temp);

            if (is_string($file) && BinStringStatic::_strlen($file) && is_writable($file)) {
                return $file;
            }
        }

        $dir = (is_string(self::$temp) && BinStringStatic::_strlen(self::$temp)) ? self::$temp : sys_get_temp_dir();

        return tempnam($dir, __NAMESPACE__);
    }
Example #14
0
 /**
  * @param LogicalScreenDescriptor $lsd
  */
 public static function dumpLogicalScreenDescriptor($lsd)
 {
     echo "lsdScreenWidth......................: " . $lsd->screenWidth . "\n";
     echo "lsdScreenHeight.....................: " . $lsd->screenHeight . "\n";
     echo "lsdPackedFields.....................: \n";
     echo "  - lsdGlobalColorTable.............: " . self::echoYN($lsd->colorTableFlag) . "\n";
     echo "  - lsdColorResolution..............: " . $lsd->colorResolution . "\n";
     echo "  - lsdSortFlag.....................: " . self::echoYN($lsd->sortFlag) . "\n";
     echo "  - lsdGlobalColorTableSize.........: " . $lsd->colorTableSize . "\n";
     echo "lsdBGColorIndex.....................: " . $lsd->bgColorIndex;
     echo " (0x";
     echo bin2hex(BinStringStatic::_substr($lsd->colorTable, $lsd->bgColorIndex * 3, 3));
     echo " (rrggbb))\n";
     echo "lsdPixelAspectRatio.................: " . $lsd->pixelAspectRatio . "\n";
     echo "\n";
     echo "Global Color Table..................: ";
     if ($lsd->colorTableFlag && $lsd->colorTableSize > 0) {
         echo "GCT present.\n";
         if ($lsd->colorTableSize < 16) {
             echo "Global Color Table content..........: ";
             echo bin2hex($lsd->colorTable) . "\n";
         }
     } else {
         echo "No GCT.\n";
     }
 }
Example #15
0
 /**
  * Build the base standard response headers, and ensure the content can be streamed.
  *
  * @author A. Grandt <*****@*****.**>
  *
  * @param String $fileName The name of the Zip archive, in ISO-8859-1 (or ASCII) encoding, ie. "archive.zip". Optional, defaults to null, which means that no ISO-8859-1 encoded file name will be specified.
  * @param String $contentType Content mime type. Optional, defaults to "application/zip".
  * @param String $utf8FileName The name of the Zip archive, in UTF-8 encoding. Optional, defaults to null, which means that no UTF-8 encoded file name will be specified.
  * @param bool   $inline Use Content-Disposition with "inline" instead of "attached". Optional, defaults to false.
  *
  * @return bool Always returns true (for backward compatibility).
  * 
  * @throws \ZipMerge\Zip\Exception\BufferNotEmpty, HeadersSent In case of errors
  */
 protected function buildResponseHeader($fileName = null, $contentType = self::CONTENT_TYPE, $utf8FileName = null, $inline = false)
 {
     $ob = null;
     $headerFile = null;
     $headerLine = null;
     $zlibConfig = 'zlib.output_compression';
     $ob = ob_get_contents();
     if ($ob !== false && BinStringStatic::_strlen($ob)) {
         $this->_throwException(new BufferNotEmpty(array('outputBuffer' => $ob, 'fileName' => $fileName)));
     }
     if (headers_sent($headerFile, $headerLine)) {
         $this->_throwException(new HeadersSent(array('headerFile' => $headerFile, 'headerLine' => $headerLine, 'fileName' => $fileName)));
     }
     if (@ini_get($zlibConfig)) {
         @ini_set($zlibConfig, 'Off');
     }
     $cd = 'Content-Disposition: ' . ($inline ? 'inline' : 'attached');
     if ($fileName) {
         $cd .= '; filename="' . $fileName . '"';
     }
     if ($utf8FileName) {
         $cd .= "; filename*=UTF-8''" . rawurlencode($utf8FileName);
     }
     header('Pragma: public');
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s T'));
     header('Expires: 0');
     header('Accept-Ranges: bytes');
     header('Connection: close');
     header('Content-Type: ' . $contentType);
     header($cd);
     return true;
 }
Example #16
0
 /**
  * @param string $href
  *
  * @param bool $startsWith
  * @return bool|array|Item Item if the href is found, else FALSE. If $startsWith is true, the returned object will be an array if any are found.
  */
 function getItemByHref($href, $startsWith = false)
 {
     $rv = array();
     /** @var Item $item */
     foreach ($this->manifest->getItems() as $item) {
         if (!$startsWith && $item->getHref() == $href) {
             return $item;
         } elseif ($startsWith && BinStringStatic::startsWith($item->getHref(), $href)) {
             $rv[] = $item;
         }
     }
     if (sizeof($rv) > 0) {
         return $rv;
     }
     return false;
 }
 /**
  * @return string The version of the field for the Central Header.
  */
 public function getCentralField()
 {
     $ts = $this->isModTimeSet ? pack('V', $this->modTime) : '';
     $flags = 0;
     ZipUtils::setBit($flags, 0, $this->isModTimeSet);
     ZipUtils::setBit($flags, 1, $this->isAcTimeSet);
     ZipUtils::setBit($flags, 2, $this->isCrTimeSet);
     return parent::HEADER_EXTENDED_TIMESTAMP . pack('vc', 1 + BinStringStatic::_strlen($ts), $flags) . $ts;
 }
Example #18
0
    /**
     * Return the current size of the archive
     *
     * @author A. Grandt <*****@*****.**>
     *
     * @return int Size of the archive
     */
    public function getArchiveSize() {
        if (!is_resource($this->_zipFile)) {
            return BinStringStatic::_strlen($this->_zipData);
        }

        $stat = fstat($this->_zipFile);
        return $stat['size'];
    }