// this script relies on the superglobal arrays, fake it here for old PHP versions if (phpversion() < '4.1.0') { $_SERVER = $HTTP_SERVER_VARS; $_GET = $HTTP_GET_VARS; } // instantiate a new phpThumb() object ob_start(); if (!(include_once dirname(__FILE__) . '/phpthumb.class.php')) { ob_end_flush(); die('failed to include_once("' . realpath(dirname(__FILE__) . '/phpthumb.class.php') . '")'); } ob_end_clean(); $phpThumb = new phpThumb(); $phpThumb->DebugTimingMessage('phpThumb.php start', __FILE__, __LINE__, $starttime); $phpThumb->SetParameter('config_error_die_on_error', true); if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { set_time_limit(60); // shouldn't take nearly this long in most cases, but with many filters and/or a slow server... } // phpThumbDebug[0] used to be here, but may reveal too much // info when high_security_mode should be enabled (not set yet) if (file_exists(dirname(__FILE__) . '/phpThumb.config.php')) { ob_start(); if (include_once dirname(__FILE__) . '/phpThumb.config.php') { // great } else { ob_end_flush(); $phpThumb->ErrorImage('failed to include_once(' . dirname(__FILE__) . '/phpThumb.config.php) - realpath="' . realpath(dirname(__FILE__) . '/phpThumb.config.php') . '"'); } ob_end_clean(); } elseif (file_exists(dirname(__FILE__) . '/phpThumb.config.php.default')) {
function SafeURLread($url, &$error, $timeout = 10, $followredirects = true) { $error = ''; $parsed_url = phpthumb_functions::ParseURLbetter($url); $alreadyLookedAtURLs[trim($url)] = true; while (true) { $tryagain = false; $rawData = phpthumb_functions::URLreadFsock(@$parsed_url['host'], @$parsed_url['path'] . '?' . @$parsed_url['query'], $errstr, true, @$parsed_url['port'] ? @$parsed_url['port'] : 80, $timeout); if (eregi('302 [a-z ]+; Location\\: (http.*)', $errstr, $matches)) { $matches[1] = trim(@$matches[1]); if (!@$alreadyLookedAtURLs[$matches[1]]) { // loop through and examine new URL $error .= 'URL "' . $url . '" redirected to "' . $matches[1] . '"'; $tryagain = true; $alreadyLookedAtURLs[$matches[1]] = true; $parsed_url = phpthumb_functions::ParseURLbetter($matches[1]); } } if (!$tryagain) { break; } } if ($rawData === false) { $error .= 'Error opening "' . $url . '":' . "\n\n" . $errstr; return false; } elseif ($rawData === null) { // fall through $error .= 'Error opening "' . $url . '":' . "\n\n" . $errstr; } else { return $rawData; } if (function_exists('curl_version') && !phpthumb_functions::FunctionIsDisabled('curl_exec')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $rawData = curl_exec($ch); curl_close($ch); if (strlen($rawData) > 0) { $error .= 'CURL succeeded (' . strlen($rawData) . ' bytes); '; return $rawData; } $error .= 'CURL available but returned no data; '; } else { $error .= 'CURL unavailable; '; } $BrokenURLfopenPHPversions = array('4.4.2'); if (in_array(phpversion(), $BrokenURLfopenPHPversions)) { $error .= 'fopen(URL) broken in PHP v' . phpversion() . '; '; } elseif (@ini_get('allow_url_fopen')) { $rawData = ''; $error_fopen = ''; ob_start(); if ($fp = fopen($url, 'rb')) { do { $buffer = fread($fp, 8192); $rawData .= $buffer; } while (strlen($buffer) > 0); fclose($fp); } else { $error_fopen .= trim(strip_tags(ob_get_contents())); } ob_end_clean(); $error .= $error_fopen; if (!$error_fopen) { $error .= '; "allow_url_fopen" succeeded (' . strlen($rawData) . ' bytes); '; return $rawData; } $error .= '; "allow_url_fopen" enabled but returned no data (' . $error_fopen . '); '; } else { $error .= '"allow_url_fopen" disabled; '; } return false; }
function PlotBMP(&$BMPinfo) { $starttime = time(); if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) { echo 'ERROR: no pixel data<BR>'; return false; } if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000))); } $im = $this->PlotPixelsGD($BMPinfo['bmp']); if (headers_sent()) { echo 'plotted ' . $BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] . ' pixels in ' . (time() - $starttime) . ' seconds<BR>'; ImageDestroy($im); exit; } else { header('Content-Type: image/png'); ImagePNG($im); ImageDestroy($im); return true; } return false; }
function getGD_PixelPlotterVersion() { if (!$this->m_bLoaded) { return false; } // PREPARE COLOR TABLE (RGBQUADs) if ($this->m_img->m_gih->m_bLocalClr) { $pal = $this->m_img->m_gih->m_colorTable->toString(); } elseif ($this->m_gfh->m_bGlobalClr) { $pal = $this->m_gfh->m_colorTable->toString(); } else { die('No color table available in getGD_PixelPlotterVersion()'); } $PlottingIMG = ImageCreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight); $NumColorsInPal = floor(strlen($pal) / 3); for ($i = 0; $i < $NumColorsInPal; $i++) { $ThisImageColor[$i] = ImageColorAllocate($PlottingIMG, ord($pal[$i * 3 + 0]), ord($pal[$i * 3 + 1]), ord($pal[$i * 3 + 2])); } // PREPARE BITMAP BITS $data = $this->m_img->m_data; $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { set_time_limit(30); } for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { if ($x >= $this->m_img->m_gih->m_nLeft && $y >= $this->m_img->m_gih->m_nTop && $x < $this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth && $y < $this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight) { // PART OF IMAGE if (@$this->m_img->m_bTrans && ord($data[$nPxl]) == $this->m_img->m_nTrans) { ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); } else { ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data[$nPxl])]); } } else { // BACKGROUND ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); } } $nPxl -= $this->m_gfh->m_nWidth << 1; } return $PlottingIMG; }
function SafeURLread($url, &$error) { $error = ''; $parsed_url = @parse_url($url); $rawData = phpthumb_functions::URLreadFsock(@$parsed_url['host'], @$parsed_url['path'], $errstr, true, @$parsed_url['port'] ? @$parsed_url['port'] : 80); $error .= 'Error: ' . $errstr . "\n" . $url; if ($rawData === false) { return false; } elseif ($rawData === null) { // fall through } else { return $rawData; } $BrokenURLfopenPHPversions = array('4.4.2'); if (in_array(phpversion(), $BrokenURLfopenPHPversions)) { $error .= 'fopen(URL) broken in PHP v' . phpversion() . '; '; } elseif (@ini_get('allow_url_fopen')) { $rawData = ''; ob_start(); if ($fp = fopen($url, 'rb')) { do { $buffer = fread($fp, 8192); $rawData .= $buffer; } while (strlen($buffer) > 0); fclose($fp); } else { $error .= trim(strip_tags(ob_get_contents())); } ob_end_clean(); if (!$error) { return $rawData; } $error .= '; "allow_url_fopen" enabled but returned no data; '; } else { $error .= '"allow_url_fopen" disabled; '; } if (function_exists('curl_version') && !phpthumb_functions::FunctionIsDisabled('curl_exec')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); $rawData = curl_exec($ch); curl_close($ch); if (strlen($rawData) > 0) { return $rawData; } $error .= 'CURL available but returned no data; '; } else { $error .= 'CURL unavailable; '; } return false; }
static function URLreadFsock($host, $file, &$errstr, $successonly = true, $port = 80, $timeout = 10) { if (!function_exists('fsockopen') || phpthumb_functions::FunctionIsDisabled('fsockopen')) { $errstr = 'fsockopen() unavailable'; return false; } if ($fp = @fsockopen($host, $port, $errno, $errstr, $timeout)) { $out = 'GET ' . $file . ' HTTP/1.0' . "\r\n"; $out .= 'Host: ' . $host . "\r\n"; $out .= 'Connection: Close' . "\r\n\r\n"; fwrite($fp, $out); $isHeader = true; $Data_header = ''; $Data_body = ''; $header_newlocation = ''; while (!feof($fp)) { $line = fgets($fp, 1024); if ($isHeader) { $Data_header .= $line; } else { $Data_body .= $line; } if (preg_match('#^HTTP/[\\.0-9]+ ([0-9]+) (.+)$#i', rtrim($line), $matches)) { list($dummy, $errno, $errstr) = $matches; $errno = intval($errno); } elseif (preg_match('#^Location: (.*)$#i', rtrim($line), $matches)) { $header_newlocation = $matches[1]; } if ($isHeader && $line == "\r\n") { $isHeader = false; if ($successonly) { switch ($errno) { case 200: // great, continue break; default: $errstr = $errno . ' ' . $errstr . ($header_newlocation ? '; Location: ' . $header_newlocation : ''); fclose($fp); return false; break; } } } } fclose($fp); return $Data_body; } return null; }