Пример #1
0
 public function cropImg($imgInfo, $maxwidth = 100, $maxheight = 100, $forceResize = false)
 {
     $thumbnail = array('filedata' => '', 'filesize' => 0, 'dateline' => 0, 'imageerror' => '');
     $filename = $imgInfo['filename'];
     $imgInfo['extension'] = strtoupper($imgInfo['extension']);
     if ($imgInfo['extension'] == 'JPG') {
         $imgInfo['extension'] = 'JPEG';
     }
     if ($validfile = $this->isValidThumbnailExtension($imgInfo['extension'])) {
         $thumbnail['source_width'] = $new_width = $width = $imgInfo['width'];
         $thumbnail['source_height'] = $new_height = $height = $imgInfo['height'];
         if ($forceResize or $width >= $maxwidth or $height >= $maxheight) {
             $memoryok = true;
             $checkmem = false;
             if (function_exists('memory_get_usage') and $memory_limit = @ini_get('memory_limit') and $memory_limit != -1) {
                 $memorylimit = vb_number_format($memory_limit, 0, false, null, '');
                 $memoryusage = memory_get_usage();
                 $freemem = $memorylimit - $memoryusage;
                 $checkmem = true;
                 $tmemory = $width * $height * ($imgInfo['extension'] == 'JPEG' ? 5 : 2) + 7372.8 + sqrt(sqrt($width * $height));
                 $tmemory += 166000;
                 // fudge factor, object overhead, etc
                 if ($freemem > 0 and $tmemory > $freemem and $tmemory <= $memorylimit * 3) {
                     // attempt to increase memory within reason, no more than triple
                     if (($current_memory_limit = vB_Utilities::ini_size_to_bytes(@ini_get('memory_limit'))) < $memorylimit + $tmemory and $current_memory_limit > 0) {
                         @ini_set('memory_limit', $memorylimit + $tmemory);
                     }
                     $memory_limit = @ini_get('memory_limit');
                     $memorylimit = vb_number_format($memory_limit, 0, false, null, '');
                     $memoryusage = memory_get_usage();
                     $freemem = $memorylimit - $memoryusage;
                 }
             }
             $fh = fopen($filename, 'w');
             fwrite($fh, $imgInfo['filedata']);
             fclose($fh);
             switch ($imgInfo['extension']) {
                 case 'GIF':
                     if (function_exists('imagecreatefromgif')) {
                         if ($checkmem) {
                             if ($freemem > 0 and $tmemory > $freemem) {
                                 throw new vB_Exception_Api('thumbnail_notenoughmemory');
                             }
                         }
                         if ($memoryok and !($image = @imagecreatefromgif($filename))) {
                             throw new vB_Exception_Api('thumbnail_nocreateimage_gif');
                         }
                     } else {
                         throw new vB_Exception_Api('thumbnail_nosupport');
                     }
                     break;
                 case 'JPEG':
                     if (function_exists('imagecreatefromjpeg')) {
                         if ($checkmem) {
                             if ($freemem > 0 and $tmemory > $freemem) {
                                 throw new vB_Exception_Api('thumbnail_notenoughmemory');
                             }
                         }
                         if ($memoryok and !($image = @imagecreatefromjpeg($filename))) {
                             throw new vB_Exception_Api('thumbnail_nocreateimage_jpeg');
                         }
                     } else {
                         throw new vB_Exception_Api('thumbnail_nosupport');
                     }
                     break;
                 case 'PNG':
                     if (function_exists('imagecreatefrompng')) {
                         if ($checkmem) {
                             if ($freemem > 0 and $tmemory > $freemem) {
                                 throw new vB_Exception_Api('thumbnail_notenoughmemory');
                             }
                         }
                         if ($memoryok and !($image = @imagecreatefrompng($filename))) {
                             throw new vB_Exception_Api('thumbnail_nocreateimage_png');
                         }
                     } else {
                         throw new vB_Exception_Api('thumbnail_nosupport');
                     }
                     break;
             }
             if ($image) {
                 $xratio = $maxwidth == 0 ? 1 : $width / $maxwidth;
                 $yratio = $maxheight == 0 ? 1 : $height / $maxheight;
                 if ($xratio > $yratio) {
                     $new_width = round($width / $xratio);
                     $new_height = round($height / $xratio);
                 } else {
                     $new_width = round($width / $yratio);
                     $new_height = round($height / $yratio);
                 }
             }
             if (!($finalimage = @imagecreatetruecolor($new_width, $new_height))) {
                 imagedestroy($image);
                 throw new vB_Exception_Api('thumbnail_nocreateimage_truecolor');
             }
             $bgcolor = imagecolorallocate($finalimage, 255, 255, 255);
             imagefill($finalimage, 0, 0, $bgcolor);
             imagecopyresampled($finalimage, $image, 0, 0, $imgInfo['x1'], $imgInfo['y1'], $new_width, $new_height, $imgInfo['width'], $imgInfo['height']);
             imagedestroy($image);
             if ($imgInfo['extension'] != 'GIF') {
                 $this->unsharpmask($finalimage);
             }
             ob_start();
             $new_extension = $this->printImage($finalimage, $imgInfo['extension'], false, 75);
             $thumbnail['filedata'] = ob_get_contents();
             ob_end_clean();
             $thumbnail['width'] = $new_width;
             $thumbnail['height'] = $new_height;
             $extension = $imgInfo['extension'];
             if ($new_extension != $extension) {
                 $thumbnail['filename'] = preg_replace('#' . preg_quote($extension, '#') . '$#', $new_extension, $filename);
             }
         } else {
             // image is a thumbnail size already
             if ($imgInfo['width'] > 0 and $imgInfo['height'] > 0) {
                 $thumbnail['filedata'] = @file_get_contents($filename);
                 $thumbnail['width'] = $imgInfo['width'];
                 $thumbnail['height'] = $imgInfo['height'];
             } else {
                 throw new vB_Exception_Api('thumbnail_nogetimagesize');
             }
         }
     } else {
         if (!$validfile) {
             throw new vB_Exception_Api('thumbnail_nosupport');
         }
     }
     if (!empty($thumbnail['filedata'])) {
         $thumbnail['filesize'] = strlen($thumbnail['filedata']);
         $thumbnail['dateline'] = vB::getRequest()->getTimeNow();
     }
     @unlink($filename);
     return $thumbnail;
 }
Пример #2
0
|| ###################################################################### ||
\*========================================================================*/
// Attempt to load XML extension if we don't have the XML functions
// already loaded.
if (!function_exists('xml_set_element_handler')) {
    $extension_dir = ini_get('extension_dir');
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
        $extension_file = 'php_xml.dll';
    } else {
        $extension_file = 'xml.so';
    }
    if ($extension_dir and file_exists($extension_dir . '/' . $extension_file)) {
        dl($extension_file);
    }
}
if (($current_memory_limit = vB_Utilities::ini_size_to_bytes(@ini_get('memory_limit'))) < 256 * 1024 * 1024 and $current_memory_limit > 0) {
    @ini_set('memory_limit', 256 * 1024 * 1024);
}
/**
* vBulletin XML Parsing Object
*
* This class allows the parsing of an XML document to an array
*
* @package 		vBulletin
* @author		Scott MacVicar
* @version		$Revision: 84278 $
* @date 		$Date: 2015-03-13 10:45:26 -0700 (Fri, 13 Mar 2015) $
* @copyright 	http://www.vbulletin.com/license.html
*
*/
class vB_XML_Parser
Пример #3
0
 /**
  * Constructor
  * Don't allow direct construction of this abstract class
  * Sets registry
  *
  * @return	void
  */
 public function __construct($options)
 {
     if (!defined('ATTACH_AS_DB')) {
         define('ATTACH_AS_DB', 0);
     }
     if (!defined('ATTACH_AS_FILES_OLD')) {
         define('ATTACH_AS_FILES_OLD', 1);
     }
     if (!defined('ATTACH_AS_FILES_NEW')) {
         define('ATTACH_AS_FILES_NEW', 2);
     }
     if (!defined('IMAGEGIF')) {
         if (function_exists('imagegif')) {
             define('IMAGEGIF', true);
         } else {
             define('IMAGEGIF', false);
         }
     }
     if (!defined('IMAGEJPEG')) {
         if (function_exists('imagejpeg')) {
             define('IMAGEJPEG', true);
         } else {
             define('IMAGEJPEG', false);
         }
     }
     if (!defined('IMAGEPNG')) {
         if (function_exists('imagepng')) {
             define('IMAGEPNG', true);
         } else {
             define('IMAGEPNG', false);
         }
     }
     if (($current_memory_limit = vB_Utilities::ini_size_to_bytes(@ini_get('memory_limit'))) < 256 * 1024 * 1024 and $current_memory_limit > 0) {
         try {
             @ini_set('memory_limit', 256 * 1024 * 1024);
         } catch (Exception $e) {
             // just ignore
         }
     }
     $this->options = $options;
     $this->regimageoption['randomfont'] = $this->options['regimageoption'] & self::ALLOW_RANDOM_FONT;
     $this->regimageoption['randomsize'] = $this->options['regimageoption'] & self::ALLOW_RANDOM_SIZE;
     $this->regimageoption['randomslant'] = $this->options['regimageoption'] & self::ALLOW_RANDOM_SLANT;
     $this->regimageoption['randomcolor'] = $this->options['regimageoption'] & self::ALLOW_RANDOM_COLOR;
     $this->regimageoption['randomshape'] = $this->options['regimageoption'] & self::ALLOW_RANDOM_SHAPE;
 }
Пример #4
0
 /**
  * This function accepts a file via URL or from $_FILES, verifies it, and places it in a temporary location for processing
  *
  * @param	mixed	Valid options are: (a) a URL to a file to retrieve or (b) a pointer to a file in the $_FILES array
  */
 function accept_upload(&$upload)
 {
     $this->error = '';
     if (!is_array($upload) and strval($upload) != '') {
         $this->upload['extension'] = strtolower(file_extension($upload));
         // Check extension here so we can save grabbing a large file that we aren't going to use
         if (!$this->is_valid_extension($this->upload['extension'])) {
             $this->set_error('upload_invalid_file');
             return false;
         }
         // Admins can upload any size file
         if ($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel']) {
             $this->maxuploadsize = 0;
         } else {
             $this->maxuploadsize = $this->fetch_max_uploadsize($this->upload['extension']);
             if (!$this->maxuploadsize) {
                 $newmem = 20971520;
             }
         }
         if (!preg_match('#^((http|ftp)s?):\\/\\/#i', $upload)) {
             $upload = 'http://' . $upload;
         }
         if (ini_get('allow_url_fopen') == 0 and !function_exists('curl_init')) {
             $this->set_error('upload_fopen_disabled');
             return false;
         } else {
             if ($filesize = $this->fetch_remote_filesize($upload)) {
                 if ($this->maxuploadsize and $filesize > $this->maxuploadsize) {
                     $this->set_error('upload_remoteimage_toolarge');
                     return false;
                 } else {
                     if (function_exists('memory_get_usage') and $memory_limit = @ini_get('memory_limit') and $memory_limit != -1) {
                         // Make sure we have enough memory to process this file
                         $memorylimit = vb_number_format($memory_limit, 0, false, null, '');
                         $memoryusage = memory_get_usage();
                         $freemem = $memorylimit - $memoryusage;
                         $newmemlimit = !empty($newmem) ? $freemem + $newmem : $freemem + $filesize;
                         if (($current_memory_limit = vB_Utilities::ini_size_to_bytes(@ini_get('memory_limit'))) < $newmemlimit and $current_memory_limit > 0) {
                             @ini_set('memory_limit', $newmemlimit);
                         }
                     }
                     require_once DIR . '/includes/class_vurl.php';
                     $vurl = new vB_vURL($this->registry);
                     $vurl->set_option(VURL_URL, $upload);
                     $vurl->set_option(VURL_FOLLOWLOCATION, 1);
                     $vurl->set_option(VURL_HEADER, true);
                     $vurl->set_option(VURL_MAXSIZE, $this->maxuploadsize);
                     $vurl->set_option(VURL_RETURNTRANSFER, true);
                     if ($result = $vurl->exec2()) {
                     } else {
                         switch ($vurl->fetch_error()) {
                             case VURL_ERROR_MAXSIZE:
                                 $this->set_error('upload_remoteimage_toolarge');
                                 break;
                             case VURL_ERROR_NOLIB:
                                 // this condition isn't reachable
                                 $this->set_error('upload_fopen_disabled');
                                 break;
                             case VURL_ERROR_SSL:
                             case VURL_URL_URL:
                             default:
                                 $this->set_error('retrieval_of_remote_file_failed');
                         }
                         return false;
                     }
                     unset($vurl);
                 }
             } else {
                 $this->set_error('upload_invalid_url');
                 return false;
             }
         }
         // write file to temporary directory...
         $this->upload['location'] = vB_Utilities::getTmpFileName('', 'vbupload');
         $attachment_write_failed = true;
         if (!empty($result['body'])) {
             $fp = $this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel'] ? fopen($this->upload['location'], 'wb') : @fopen($this->upload['location'], 'wb');
             if ($fp and $this->upload['location']) {
                 @fwrite($fp, $result['body']);
                 @fclose($fp);
                 $attachment_write_failed = false;
             }
         } else {
             if (file_exists($result['body_file'])) {
                 if (@rename($result['body_file'], $this->upload['location']) or copy($result['body_file'], $this->upload['location']) and unlink($result['body_file'])) {
                     $mask = 0777 & ~umask();
                     @chmod($this->upload['location'], $mask);
                     $attachment_write_failed = false;
                 }
             }
         }
         if ($attachment_write_failed) {
             $this->set_error('upload_writefile_failed');
             return false;
         }
         $this->upload['filesize'] = @filesize($this->upload['location']);
         $this->upload['filename'] = basename($upload);
         $this->upload['extension'] = strtolower(file_extension($this->upload['filename']));
         $this->upload['thumbnail'] = '';
         $this->upload['filestuff'] = '';
         $this->upload['url'] = true;
     } else {
         $this->upload['filename'] = trim($upload['name']);
         $this->upload['filesize'] = intval($upload['size']);
         $this->upload['location'] = trim($upload['tmp_name']);
         $this->upload['extension'] = strtolower(file_extension($this->upload['filename']));
         $this->upload['thumbnail'] = '';
         $this->upload['filestuff'] = '';
         if ($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel'] and $this->upload['error']) {
             // Encountered PHP upload error
             if (!($maxupload = @ini_get('upload_max_filesize'))) {
                 $maxupload = 10485760;
             }
             $maxattachsize = vb_number_format($maxupload, 1, true);
             switch ($this->upload['error']) {
                 case '1':
                     // UPLOAD_ERR_INI_SIZE
                 // UPLOAD_ERR_INI_SIZE
                 case '2':
                     // UPLOAD_ERR_FORM_SIZE
                     $this->set_error('upload_file_exceeds_php_limit', $maxattachsize);
                     break;
                 case '3':
                     // UPLOAD_ERR_PARTIAL
                     $this->set_error('upload_file_partially_uploaded');
                     break;
                 case '4':
                     $this->set_error('upload_file_failed');
                     break;
                 case '6':
                     $this->set_error('missing_temporary_folder');
                     break;
                 case '7':
                     $this->set_error('upload_writefile_failed');
                     break;
                 case '8':
                     $this->set_error('upload_stopped_by_extension');
                     break;
                 default:
                     $this->set_error('upload_invalid_file');
             }
             return false;
         } else {
             if ($this->upload['error'] or $this->upload['location'] == 'none' or $this->upload['location'] == '' or $this->upload['filename'] == '' or !$this->upload['filesize'] or !is_uploaded_file($this->upload['location'])) {
                 if ($this->emptyfile or $this->upload['filename'] != '') {
                     $this->set_error('upload_file_failed');
                 }
                 return false;
             }
         }
         if ($this->registry->options['safeupload']) {
             $temppath = $this->registry->options['tmppath'] . '/' . $this->registry->session->fetch_sessionhash();
             $moveresult = $this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel'] ? move_uploaded_file($this->upload['location'], $temppath) : @move_uploaded_file($this->upload['location'], $temppath);
             if (!$moveresult) {
                 $this->set_error('upload_unable_move');
                 return false;
             }
             $this->upload['location'] = $temppath;
         }
     }
     // Check if the filename is utf8
     $this->upload['utf8_name'] = isset($upload['utf8_name']) and $upload['utf8_name'];
     $return_value = true;
     // Legacy Hook 'upload_accept' Removed //
     return $return_value;
 }