Файл: adultscript-2.0.3-pro/files/mobile/components/user_avatar.php
Строк: 46
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_mobile_user_avatar extends VComponent_mobile_user
{
public function __construct()
{
parent::__construct();
}
public function render()
{
VAuth::check('Registered', MOBILE_URL.'/user/login/');
$errors = array();
$messages = array();
$step = 'upload';
if (isset($_POST['submit-avatar'])) {
$filter = VF::factory('filter');
$unique = $filter->get('unique');
$mime = $filter->get('mime');
if ($unique == '') {
$errors[] = 'Unique upload identifier not set!';
} elseif (!ctype_digit($unique)) {
$errors[] = 'Invalid unique upload identifier!';
}
if (!$ext = $this->mime_to_ext($mime)) {
$errors[] = 'Invalid image format!';
}
if (!$errors) {
$file = TMP_DIR.'/uploads/'.$unique.'.'.$ext;
if (file_exists($file) && is_file($file)) {
$size = filesize($file);
$image = VF::factory('image');
if (!$image->load($file)) {
$errors[] = 'Invalid image file!';
}
} else {
$errors[] = 'Failed to find file!';
}
}
if (!$errors) {
$dst_tmp = BASE_DIR.'/mobile/tmp/'.$unique.'.tmp.'.$ext;
$width = $image->src['width'];
$height = $image->src['height'];
$awidth = VF::cfg_item('module.user.avatar_width');
$aheight = VF::cfg_item('module.user.avatar_height');
if ($width > 300) {
if (!$image->resize(300, 0, 'TO_WIDTH', $dst_tmp)) {
$errors[] = $image->get_error();
}
} else {
copy($file, $dst_tmp);
}
$step = 'crop';
$this->tpl->url = MOBILE_URL.'/tmp/'.$unique.'.tmp.'.$ext;
$this->tpl->width = $awidth;
$this->tpl->height = $aheight;
}
}
if (isset($_POST['submit-crop'])) {
$user_id = $_SESSION['user_id'];
$img = trim($_POST['crop']);
$dst = MEDIA_DIR.'/users/'.$user_id.'.png';
$fp = fopen($dst, "wb");
$data = explode(',', $img);
fwrite($fp, base64_decode($data['1']));
fclose($fp);
$_SESSION['avatar'] = 'png';
$this->db->query("UPDATE #__user SET avatar = 'png' WHERE user_id = ".$user_id." LIMIT 1");
$step = 'upload';
$messages[] = 'Avatar added!';
}
$this->tpl->menu = 'community';
$this->tpl->submenu = 'avatar';
$this->tpl->meta_title = __('avatar');
$this->tpl->messages = $messages;
$this->tpl->errors = $errors;
$this->tpl->unique = mt_rand().time();
$this->tpl->step = $step;
$this->tpl->load(array('header', 'user_avatar', 'footer'));
$this->tpl->display();
}
private function mime_to_ext($mime)
{
$mimes = array(
'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png'
);
return (isset($mimes[$mime])) ? $mimes[$mime] : false;
}
}