<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_DataManager_Userpic_Filesystem extends vB_DataManager_Userpic { public function fetch_path($userid, $revision, $thumb = false, $ext = 'gif') { $table = $this->fetchTableBase($this->table); $path = $this->filepath . DIRECTORY_SEPARATOR . ($thumb ? 'thumbs' . DIRECTORY_SEPARATOR : '') . preg_replace("#^custom#si", '', $table) . $userid . "_" . $revision . ".$ext"; return $path; } function pre_save($doquery = true) { if ($this->presave_called !== null) { return $this->presave_called; } if ($file = $this->fetch_field('filedata')) { $this->setr_info('filedata', $file); $this->do_unset('filedata'); $this->set('filesize', strlen($this->info['filedata'])); chdir(DIR); if (!is_writable($this->filepath)) { $this->error('upload_invalid_imagepath'); return false; } if ($thumb = $this->fetch_field('filedata_thumb')) { $this->setr_info('filedata_thumb', $thumb); $this->do_unset('filedata_thumb'); } } return parent::pre_save($doquery); } public function getAvatarFilenames($userid, $revision, $extension) { return [ 'full' => $this->fetch_path($userid, $revision, false, $extension), 'thumb' => $this->fetch_path($userid, $revision, true, $extension), ]; } function post_save_each($doquery = true) { if (isset($this->info["{$this->revision}"])) { $revision = $this->info["{$this->revision}"]; } else if ($this->fetch_field($this->revision) !== null) { $revision = $this->fetch_field($this->revision); } if (!empty($this->info['filedata']) AND isset($revision)) { $ext = empty($this->existing['aextension']) ? 'gif' : $this->existing['aextension']; $userid = $this->fetch_field('userid'); ['full' => $oldfilename, 'thumb' => $oldthumbfilename] = $this->getAvatarFilenames($userid, $revision, $ext); ['full' => $newfilename, 'thumb' => $thumbfilename] = $this->getAvatarFilenames($userid, $revision + 1, $ext); if ($filenum = fopen($newfilename, 'wb')) { $table = $this->fetchTableBase($this->table); if ($revision) { $this->deleteFile($oldfilename); if ($table == 'customavatar') { $this->deleteFile($oldthumbfilename); } } @fwrite($filenum, $this->info['filedata']); @fclose($filenum); $userdata = new vB_DataManager_User($this->registry, vB_DataManager_Constants::ERRTYPE_SILENT); $userdata->setr('userid', $this->fetch_field('userid')); $userdata->condition = ['userid' => $this->fetch_field('userid')]; $userdata->set($this->revision, $revision + 1); $userdata->save(); unset($userdata); if ($table == 'customavatar') { if (!empty($this->info['filedata_thumb'])) { $thumbnail['filedata'] =& $this->info['filedata_thumb']; } else { $thumbnail = $this->fetch_thumbnail($newfilename, true); } $writeSuccess = false; require_once(DIR . '/includes/functions_file.php'); if ($thumbnail['filedata'] AND vbmkdir(dirname($thumbfilename)) AND $filenum = @fopen($thumbfilename, 'wb')) { @fwrite($filenum, $thumbnail['filedata']); @fclose($filenum); $writeSuccess = true; } $newfiledetails = pathinfo($newfilename); $fields = " filename = '" . $this->registry->db->escape_string($newfiledetails['basename']) . "'"; if ($writeSuccess AND $thumbnail['height'] AND $thumbnail['width']) { $fields .= ", width_thumb = " . intval($thumbnail['width']) . ", height_thumb = " . intval($thumbnail['height']); } unset($thumbnail); $this->registry->db->query_write("
						UPDATE " . TABLE_PREFIX . "customavatar
						SET $fields
						WHERE userid = " . $this->fetch_field('userid') ); } return true; } else { $this->error('upload_invalid_imagepath'); return false; } } else { return true; } } function post_delete($doquery = true) { $users = $this->registry->db->query_read_slave("
			SELECT
				userid, {$this->revision} AS revision
			FROM " . TABLE_PREFIX . "user
			WHERE " . $this->condition ); while ($user = $this->registry->db->fetch_array($users)) { $this->deleteFile($this->fetch_path($user['userid'], $user['revision'])); $this->deleteFile($this->fetch_path($user['userid'], $user['revision'], true)); } } } 