Exemplo n.º 1
0
function read_tgz_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
{
    // Make sure we have this loaded.
    loadLanguage('Packages');
    // This function sorta needs gzinflate!
    if (!function_exists('gzinflate')) {
        fatal_lang_error('package_no_zlib', 'critical');
    }
    umask(0);
    if (!$single_file && $destination !== null && !file_exists($destination)) {
        mktree($destination, 0777);
    }
    // No signature?
    if (strlen($data) < 2) {
        return false;
    }
    $id = unpack('H2a/H2b', substr($data, 0, 2));
    if (strtolower($id['a'] . $id['b']) != '1f8b') {
        // Okay, this ain't no tar.gz, but maybe it's a zip file.
        if (substr($data, 0, 2) == 'PK') {
            return read_zip_data($data, $destination, $single_file, $overwrite, $files_to_extract);
        } else {
            return false;
        }
    }
    $flags = unpack('Ct/Cf', substr($data, 2, 2));
    // Not deflate!
    if ($flags['t'] != 8) {
        return false;
    }
    $flags = $flags['f'];
    $offset = 10;
    $octdec = array('mode', 'uid', 'gid', 'size', 'mtime', 'checksum', 'type');
    // "Read" the filename and comment. // !!! Might be mussed.
    if ($flags & 12) {
        while ($flags & 8 && $data[$offset++] != "") {
            continue;
        }
        while ($flags & 4 && $data[$offset++] != "") {
            continue;
        }
    }
    $crc = unpack('Vcrc32/Visize', substr($data, strlen($data) - 8, 8));
    $data = @gzinflate(substr($data, $offset, strlen($data) - 8 - $offset));
    // smf_crc32 and crc32 may not return the same results, so we accept either.
    if ($crc['crc32'] != smf_crc32($data) && $crc['crc32'] != crc32($data)) {
        return false;
    }
    $blocks = strlen($data) / 512 - 1;
    $offset = 0;
    $return = array();
    while ($offset < $blocks) {
        $header = substr($data, $offset << 9, 512);
        $current = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100linkname/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155path', $header);
        // Blank record?  This is probably at the end of the file.
        if (empty($current['filename'])) {
            $offset += 512;
            continue;
        }
        if ($current['type'] == 5 && substr($current['filename'], -1) != '/') {
            $current['filename'] .= '/';
        }
        foreach ($current as $k => $v) {
            if (in_array($k, $octdec)) {
                $current[$k] = octdec(trim($v));
            } else {
                $current[$k] = trim($v);
            }
        }
        $checksum = 256;
        for ($i = 0; $i < 148; $i++) {
            $checksum += ord($header[$i]);
        }
        for ($i = 156; $i < 512; $i++) {
            $checksum += ord($header[$i]);
        }
        if ($current['checksum'] != $checksum) {
            break;
        }
        $size = ceil($current['size'] / 512);
        $current['data'] = substr($data, ++$offset << 9, $current['size']);
        $offset += $size;
        // Not a directory and doesn't exist already...
        if (substr($current['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $current['filename'])) {
            $write_this = true;
        } elseif (substr($current['filename'], -1, 1) != '/') {
            $write_this = $overwrite || filemtime($destination . '/' . $current['filename']) < $current['mtime'];
        } elseif ($destination !== null && !$single_file) {
            // Protect from accidental parent directory writing...
            $current['filename'] = strtr($current['filename'], array('../' => '', '/..' => ''));
            if (!file_exists($destination . '/' . $current['filename'])) {
                mktree($destination . '/' . $current['filename'], 0777);
            }
            $write_this = false;
        } else {
            $write_this = false;
        }
        if ($write_this && $destination !== null) {
            if (strpos($current['filename'], '/') !== false && !$single_file) {
                mktree($destination . '/' . dirname($current['filename']), 0777);
            }
            // Is this the file we're looking for?
            if ($single_file && ($destination == $current['filename'] || $destination == '*/' . basename($current['filename']))) {
                return $current['data'];
            } elseif ($single_file) {
                continue;
            } elseif ($files_to_extract !== null && !in_array($current['filename'], $files_to_extract)) {
                continue;
            }
            package_put_contents($destination . '/' . $current['filename'], $current['data']);
        }
        if (substr($current['filename'], -1, 1) != '/') {
            $return[] = array('filename' => $current['filename'], 'md5' => md5($current['data']), 'preview' => substr($current['data'], 0, 100), 'size' => $current['size'], 'skipped' => false);
        }
    }
    if ($destination !== null && !$single_file) {
        package_flush_cache();
    }
    if ($single_file) {
        return false;
    } else {
        return $return;
    }
}
Exemplo n.º 2
0
 public function get_png_data($background_color)
 {
     if (!$this->loaded) {
         return false;
     }
     // Prepare the color table.
     if ($this->image->m_gih->m_bLocalClr) {
         $colors = $this->image->m_gih->m_nTableSize;
         $pal = $this->image->m_gih->m_colorTable->toString();
         if ($background_color != -1) {
             $background_color = $this->image->m_gih->m_colorTable->colorIndex($background_color);
         }
     } elseif ($this->header->m_bGlobalClr) {
         $colors = $this->header->m_nTableSize;
         $pal = $this->header->m_colorTable->toString();
         if ($background_color != -1) {
             $background_color = $this->header->m_colorTable->colorIndex($background_color);
         }
     } else {
         $colors = 0;
         $background_color = -1;
     }
     if ($background_color == -1) {
         $background_color = $this->header->m_nBgColor;
     }
     $data =& $this->image->m_data;
     $header =& $this->image->m_gih;
     $i = 0;
     $bmp = '';
     // Prepare the bitmap itself.
     for ($y = 0; $y < $this->header->m_nHeight; $y++) {
         $bmp .= "";
         for ($x = 0; $x < $this->header->m_nWidth; $x++, $i++) {
             // Is this in the proper range?  If so, get the specific pixel data...
             if ($x >= $header->m_nLeft && $y >= $header->m_nTop && $x < $header->m_nLeft + $header->m_nWidth && $y < $header->m_nTop + $header->m_nHeight) {
                 $bmp .= $data[$i];
             } else {
                 $bmp .= chr($background_color);
             }
         }
     }
     $bmp = gzcompress($bmp, 9);
     // Output the basic signature first of all.
     $out = "‰PNG\r\n\n";
     // Now, we want the header...
     $out .= "\r";
     $tmp = 'IHDR' . pack('N', (int) $this->header->m_nWidth) . pack('N', (int) $this->header->m_nHeight) . "";
     $out .= $tmp . pack('N', smf_crc32($tmp));
     // The palette, assuming we have one to speak of...
     if ($colors > 0) {
         $out .= pack('N', (int) $colors * 3);
         $tmp = 'PLTE' . $pal;
         $out .= $tmp . pack('N', smf_crc32($tmp));
     }
     // Do we have any transparency we want to make available?
     if ($this->image->m_bTrans && $colors > 0) {
         $out .= pack('N', (int) $colors);
         $tmp = 'tRNS';
         // Stick each color on - full transparency or none.
         for ($i = 0; $i < $colors; $i++) {
             $tmp .= $i == $this->image->m_nTrans ? "" : "ÿ";
         }
         $out .= $tmp . pack('N', smf_crc32($tmp));
     }
     // Here's the data itself!
     $out .= pack('N', strlen($bmp));
     $tmp = 'IDAT' . $bmp;
     $out .= $tmp . pack('N', smf_crc32($tmp));
     // EOF marker...
     $out .= "" . 'IEND' . "®B`‚";
     return $out;
 }