<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_DataManager_Userpic extends vB_DataManager { var $validfields = [ 'userid' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_YES], 'filedata' => [vB_Cleaner::TYPE_BINARY, vB_DataManager_Constants::REQ_NO, vB_DataManager_Constants::VF_METHOD], 'dateline' => [vB_Cleaner::TYPE_UNIXTIME, vB_DataManager_Constants::REQ_AUTO], 'filename' => [vB_Cleaner::TYPE_STR, vB_DataManager_Constants::REQ_YES], 'visible' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'filesize' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_YES], 'width' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'height' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'filedata_thumb' => [vB_Cleaner::TYPE_BINARY, vB_DataManager_Constants::REQ_NO,], 'width_thumb' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'height_thumb' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'extension' => [vB_Cleaner::TYPE_STR, vB_DataManager_Constants::REQ_YES], ]; var $table = 'vBForum:customavatar'; protected $customavatar; var $revision = 'avatarrevision'; var $filepath = 'customavatars'; protected $keyField = ['userid']; protected $imageHandler; public static function &fetch_library(&$registry, $errtype = vB_DataManager_Constants::ERRTYPE_STANDARD, $classtype = 'userpic_avatar', $usefilesystem = null) { if (is_null($usefilesystem)) { $options = vB::getDatastore()->getValue('options'); $usefilesystem = $options['usefileavatar']; } if ($usefilesystem) { $newclass = new vB_DataManager_Userpic_Filesystem($registry, $errtype); list($prefix, $type) = explode('_', $classtype); $newclass->setStorageOptions($type); } else { $class = 'vB_DataManager_' . $classtype; $newclass = new $class($registry, $errtype); } return $newclass; } function __construct($registry = NULL, $errtype = vB_DataManager_Constants::ERRTYPE_STANDARD) { parent::__construct($registry, $errtype); $this->imageHandler = vB_Image::instance(); } function verify_filedata(&$filedata) { if (strlen($filedata) > 0) { $this->set('filesize', strlen($filedata)); } return true; } function pre_delete($doquery = true) { @ignore_user_abort(true); return true; } function pre_save($doquery = true) { if ($this->presave_called !== null) { return $this->presave_called; } if (!$this->condition) { if ($this->fetch_field('userid') AND $this->assertor->getRow($this->table, array('userid' => $this->fetch_field('userid')))) { $this->condition['userid'] = $this->fetch_field('userid'); } } $table = $this->fetchTableBase($this->table); if ($table == 'customavatar' AND $this->fetch_field('filedata') AND !$this->fetch_field('filedata_thumb') AND !$this->options['usefileavatar']) { $filename = vB_Utilities::getTmpFileName($this->fetch_field('userid'), 'vbuserpic'); $filenum = @fopen($filename, 'wb'); @fwrite($filenum, $this->fetch_field('filedata')); @fclose($filenum); $imageinfo = $this->imageHandler->fetchImageInfo($filename); if (!$this->fetch_field('width') OR !$this->fetch_field('height')) { if ($imageinfo) { $this->set('width', $imageinfo[0]); $this->set('height', $imageinfo[1]); } } $thumbnail = $this->fetch_thumbnail($filename, false, $imageinfo); $this->deleteFile($filename); if ($thumbnail['filedata']) { $this->set('width_thumb', $thumbnail['width']); $this->set('height_thumb', $thumbnail['height']); $this->set('filedata_thumb', $thumbnail['filedata']); unset($thumbnail); } else { $this->set('width_thumb', 0); $this->set('height_thumb', 0); $this->set('filedata_thumb', ''); } } $return_value = true; $this->presave_called = $return_value; return $return_value; } function post_save_each($doquery = true) { return parent::post_save_each($doquery); } function post_delete($doquery = true) { parent::post_delete($doquery); } function fetch_thumbnail($file, $forceimage = false, $imageinfo = false) { if (empty($imageinfo)) { $imageinfo = $this->imageHandler->fetchImageInfo ($file); } if ($imageinfo[0] > FIXED_SIZE_AVATAR_WIDTH OR $imageinfo[1] > FIXED_SIZE_AVATAR_HEIGHT) { $filename = 'file.' . ($imageinfo[2] == 'JPEG' ? 'jpg' : strtolower($imageinfo[2])); $thumbnail = $this->imageHandler->fetchThumbnail($filename, $file, FIXED_SIZE_AVATAR_WIDTH, FIXED_SIZE_AVATAR_HEIGHT); if ($thumbnail['filedata']) { return $thumbnail; } } return array( 'filedata' => @file_get_contents($file), 'width' => $imageinfo[0], 'height' => $imageinfo[1], ); } public function setStorageOptions($type = 'avatar') { if ($type == 'avatar') { $this->table = 'vBForum:customavatar'; $this->revision = 'avatarrevision'; $this->filepath = realpath($this->options['avatarpath']); if (!$this->filepath) { $this->filepath = realpath(DIR . DIRECTORY_SEPARATOR . $this->options['avatarpath']); } } else { $this->errors[] = 'Storage type error'; return false; } return true; } } 