Файл: adultscript-2.0.3-pro/files/modules/user/components/avatar.php.backup.php
Строк: 60
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_user_avatar extends VModule_user
{
public function __construct()
{
parent::__construct();
}
public function render()
{
VAuth::check('Registered');
$user_id = (int) $_SESSION['user_id'];
$unique = time().'0'.mt_rand();
$locked = $this->is_locked($user_id);
$errors = array();
$messages = array();
$uploaded = false;
if ($locked) {
$errors[] = __('account-locked');
}
if (isset($_POST['submit-upload-avatar']) && !$locked) {
$filter = VF::factory('filter');
$upload_id = $filter->get('unique_id');
if (!ctype_digit($upload_id)) {
$errors[] = 'Invalid upload identifier!';
}
if (!$errors) {
if (!$file = $this->process_file($upload_id, VCfg::get('user.avatar_max_size'], VCfg::get('user.avatar_allowed_ext'))) {
$errors = array_merge($errors, $this->errors);
}
}
if (!$errors) {
$photo = $user_id.'.'.$file['ext'];
$dst_orig = MEDIA_DIR.'/users/orig/'.$photo;
$dwidth = $this->ucfg['avatar_width'];
$dheight = $this->ucfg['avatar_height'];
if (move_uploaded_file($avatar['path'], $dst_orig)) {
$dst_tmp = TMP_DIR.'/uploads/'.$photo;
$dst_thumb = MEDIA_DIR.'/users/'.$photo;
$image = VF::factory('image');
if (!$image->load($dst_orig)) {
$errors[] = $image->get_error();
}
if (!$errors) {
$img = getimagesize($dst_orig);
$width = $img['0'];
$height = $img['1'];
if ($this->ucfg['avatar_method'] == 'canvas' OR
$width < $dwidth OR $height < $dheight) {
if ($image->canvas($dwidth, $dheight, '000000') &&
$image->resize($dwidth, $dheight, 'ASPECT_RATIO', $dst_tmp)) {
if ($image->load($dst_tmp) &&
$image->crop_from_center($dwidth, $dheight, $dst_thumb)) {
$processed = TRUE;
} else {
$errors[] = $image->get_error();
}
} else {
$errors[] = $image->get_error();
}
} else {
$twidth = $dwidth*3;
$theight = $dheight*3;
if ($width > $twidth OR $height > $height) {
if (!$image->resize($twidth, $theight, 'TO_ASPECT_RATIO', $dst_tmp)) {
$errors[] = $image->get_error();
}
} else {
copy($dst_orig, $dst_tmp);
}
if (!$errors) {
$uploaded = $photo;
$_SESSION['uploaded'] = $uploaded;
$this->tpl->width = $dwidth;
$this->tpl->height = $dheight;
$this->tpl->twidth = $twidth;
$this->tpl->theight = $theight;
}
}
if (isset($processed) && $processed === TRUE) {
$this->db->query("UPDATE #__user SET avatar = '".$this->db->escape($avatar['ext'])."'
WHERE user_id = ".$user_id." LIMIT 1");
$_SESSION['avatar'] = $avatar['ext'];
$messages[] = __('avatar-success');
}
}
} else {
$errors[] = __('avatar-failed');
}
}
}
if (isset($_POST['submit-crop-avatar']) && !$locked) {
$x = (int) $_POST['x'];
$y = (int) $_POST['y'];
$x2 = (int) $_POST['x2'];
$y2 = (int) $_POST['y2'];
$w = (int) $_POST['w'];
$h = (int) $_POST['h'];
$avatar = $_SESSION['uploaded'];
$src = TMP_DIR.'/uploads/'.$avatar;
$dst = MEDIA_DIR.'/users/'.$avatar;
$image = &VF::factory('image');
if ($image->load($src) &&
$image->crop($x, $y, $w, $h, $dst)) {
$this->db = VF::factory('database');
$ext = VFile::ext($avatar);
$this->db->query("UPDATE #__user SET avatar = '".$this->db->escape($ext)."'
WHERE user_id = ".$user_id." LIMIT 1");
$_SESSION['avatar'] = $ext;
$messages[] = __('avatar-success');
} else {
$errors[] = $image->get_error();
}
}
$this->tpl->menu = 'home';
$this->tpl->colmenu = 'account';
$this->tpl->submenu = 'user-avatar';
$this->tpl->title = __('avatar-meta-title');
$this->tpl->meta_title = __('avatar-meta-title');
$this->tpl->css = array(TPL_REL.'/css/jquery.Jcrop.css');
$this->tpl->errors = $errors;
$this->tpl->messages = $messages;
$this->tpl->uploaded = $uploaded;
$this->tpl->unique = $unique;
$this->tpl->load(array('header', 'user_avatar', 'footer'));
$this->tpl->display();
}
private function is_locked($user_id)
{
$this->db->query("SELECT locked FROM #__user WHERE user_id = ".$user_id." LIMIT 1");
if ($this->db->affected_rows()) {
return (bool) $this->db->fetch_field('locked');
}
VModule::load('error', true);
}
private function process_file($upload_id, $max_size, $allowed_ext)
{
VLanguage::load('frontend.upload');
$sec = substr(md5(VF::cfg_item('secret')), -5);
$info = TMP_DIR.'/uploads/'.$upload_id.'_'.$sec;
if (file_exists($info) && is_file($info)) {
$info = file($info);
$name = trim($info['0']);
$ext = trim($info['1']);
$path = TMP_DIR.'/uploads/'.$upload_id.'_'.$sec.'.'.$ext;
if (file_exists($path) && is_file($path)) {
$size = filesize($path);
if ($max_size !== 0 && $size > ($max_size*1024*1024)) {
$this->errors[] = __('file-limit', array($max_size));
} else {
if (in_array($ext, $allowed_ext)) {
@unlink($info);
return array(
'path' => $path,
'name' => $name,
'size' => $size,
'ext' => $ext
);
} else {
$this->errors[] = __('file-invalid', array(implode(', ', $allowed_ext)));
}
}
} else {
$this->errors[] = __('file-select');
}
} else {
$this->errors[] = __('file-select').'*';
}
return FALSE;
}
}