Файл: adultscript-2.0.3-pro/files/admin/modules/photo/components/upload.php
Строк: 123
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_photo_upload extends VModule_Admin_photo
{
public function __construct()
{
parent::__construct();
}
public function render()
{
$pcfg = VF::cfg('module.photo');
$errors = array();
$messages = array();
$album = array(
'username' => '', 'title' => '', 'description' => '',
'category' => array(), 'tags' => '', 'type' => 'public',
'status' => 1
);
$categories = $this->get_photo_categories();
if (isset($_POST['submit_upload'])) {
$filter = VF::factory('filter');
$username = $filter->get('username');
$title = $filter->get('title');
$description = $filter->get('description');
$tags = $filter->get('tags');
$category = (isset($_POST['categories'])) ? (array) $_POST['categories'] : array();
$status = (int) trim($_POST['status']);
$type = $filter->get('type');
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
if ($username == '') {
$errors[] = 'Please enter a username for this album';
} else {
$this->db->query("SELECT user_id FROM #__user WHERE username = '".$this->db->escape($username)."' LIMIT 1");
if ($this->db->affected_rows()) {
$user_id = (int) $this->db->fetch_field('user_id');
$album['username'] = $username;
} else {
$errors[] = 'Invalid username! Are you sure this username exists!?';
}
}
if ($title == '') {
$errors[] = 'Title field cannot be left blank!';
} elseif (!VValid::length($title, $pcfg['title_min_length'], $pcfg['title_max_length'])) {
$errors[] = 'Title field must contain at least '.$pcfg['title_min_length'].' and no more than '.$pcfg['title_max_length'].' characters!';
} else {
$album['title'] = $title;
}
if ($description != '') {
$album['description'] = $description;
}
if (!$category) {
$errors[] = 'Please select at least one category for this album!';
} else {
$cats = array();
foreach ($categories as $cat) {
$cats[$cat['cat_id']] = 1;
}
foreach ($category as $cat) {
if (!isset($cats[$cat])) {
$error = TRUE;
break;
}
}
if (isset($error)) {
$errors[] = 'Invalid category checked! What exactly did you click!?';
} else {
$album['category'] = $category;
}
}
if ($tags == '') {
$errors[] = 'Please enter tags for this album!';
} elseif (!VValid::length($tags, $pcfg['tags_min_length'], $pcfg['tags_max_length'])) {
$errors[] = 'Tags field must contain at least '.$pcfg['tags_min_length'].' and no more than '.$pcfg['tags_max_length'].' characters!';
} else {
$tags = prepare_tags($tags);
if ($tags == '') {
$errors[] = 'Tags can contain only letters, numbers, spaces and must be separated by commas!';
} else {
$album['tags'] = $tags;
}
}
if (!isset($_FILES['image_1']) OR $_FILES['image_1']['tmp_name'] == '') {
$errors[] = 'Please upload at least one image for this album!';
}
if (!$errors) {
if (!$file = VUpload::process('image_1', $pcfg['photo_max_size'], $pcfg['photo_allowed_ext'])) {
$errors = array_merge($errors, VUpload::error());
}
}
if (!$errors) {
$slug = prepare_string($title, TRUE);
$mobile = ($pcfg['mobile']) ? 1 : 0;
$pass = ($password) ? VHash::encrypt($password) : '';
$this->db->query("INSERT INTO #__photo_albums
SET user_id = ".$user_id.",
title = '".$this->db->escape($title)."',
slug = '".$this->db->escape($slug)."',
description = '".$this->db->escape($description)."',
password = '".$this->db->escape($pass)."',
type = '".$this->db->escape($type)."',
mobile = '".$mobile."',
add_date = '".date('Y-m-d h:i:s')."',
add_time = ".time().",
status = '3'");
if ($this->db->affected_rows()) {
$album_id = $this->db->get_last_insert_id('#__photo_albums');
$image = VF::factory('image');
$images = 0;
$photos = array();
foreach ($_FILES as $key => $value) {
if ($value['tmp_name'] != '') {
if (!$file = VUpload::process($key, $pcfg['photo_max_size'], $pcfg['photo_allowed_ext'])) {
continue;
}
if (!$image->load($file['path'])) {
continue;
}
$arr = explode('_', $key);
$id = $arr['1'];
$caption = $filter->get('caption_'.$id);
$desc = $filter->get('description_'.$id);
$this->db->query("INSERT INTO #__photo
SET album_id = ".$album_id.",
caption = '".$this->db->escape($caption)."',
description = '".$this->db->escape($description)."',
ext = '".$this->db->escape($image->src['ext'])."',
size = ".$file['size'].",
add_date = '".date('Y-m-d h:i:s')."',
status = '0'");
if (!$this->db->affected_rows()) {
continue;
}
$photo_id = $this->db->get_last_insert_id('#__photo');
$photos[$photo_id] = $image->src['ext'];
$dst_orig = MEDIA_DIR.'/photos/orig/'.$photo_id.'.'.$image->src['ext'];
if (!copy($file['path'], $dst_orig)) {
continue;
}
$dst = MEDIA_DIR.'/photos/'.$photo_id.'.'.$image->src['ext'];
$dst_thumb = MEDIA_DIR.'/photos/thumbs/'.$photo_id.'.jpg';
$dst_thumb_tmp = TMP_DIR.'/images/'.$photo_id.'.thumb.jpg';
if ($image->src['width'] < $pcfg['photo_width']) {
copy($file['path'], $dst);
} else {
if (!$image->resize($pcfg['photo_width'], $pcfg['photo_height'], 'MAX_WIDTH', $dst)) {
continue;
}
}
if ($mobile) {
$dst_mobile = MEDIA_DIR.'/photos/mobile/'.$photo_id.'.'.$image->src['ext'];
if ($image->src['width'] < $pcfg['mobile_width']) {
copy($file['path'], $dst_mobile);
} else {
if (!$image->resize($pcfg['mobile_width'], $pcfg['mobile_height'], 'MAX_WIDTH', $dst_mobile)) {
continue;
}
}
}
$thumb_width = $pcfg['thumb_width']+30;
$thumb_height = $pcfg['thumb_height']+100;
$image->set_option('jpeg_quality', 100);
if (!$image->resize($thumb_width, $thumb_height, 'MAX_HEIGHT', $dst_thumb_tmp)) {
continue;
}
$image->clear();
$image->load($dst_thumb_tmp);
if (!$image->crop_from_center($pcfg['thumb_width'], $pcfg['thumb_height'], $dst_thumb)) {
continue;
}
$image->clear();
$image->load($dst_thumb);
if (!$image->resize($pcfg['thumb_width'], $pcfg['thumb_height'], 'EXACT', $dst_thumb_tmp)) {
continue;
}
rename($dst_thumb_tmp, $dst_thumb);
++$images;
if ($images === 1) {
$cover_id = $photo_id;
$cover_ext = $image->src['ext'];
}
$image->clear();
VFile::delete($dst_thumb_tmp);
}
}
$src = MEDIA_DIR.'/photos/thumbs/'.$cover_id.'.jpg';
$dst = MEDIA_DIR.'/photos/covers/'.$album_id.'.jpg';
$dst_tmp = TMP_DIR.'/images/'.$cover_id.'.jpg';
$image->clear();
$image->load($src);
if ($image->src['height'] < $pcfg['cover_height']) {
$src = MEDIA_DIR.'/photos/orig/'.$cover_id.'.'.$cover_ext;
$image->clear();
$image->load($src);
$cover_max_width = ($pcfg['cover_width'] + 30);
$cover_max_height = ($pcfg['cover_height'] + 50);
if ($image->src['width'] > $cover_max_width && $image->src['height'] > $cover_max_height) {
$image->set_option('jpeg_quality', 100);
$image->resize($cover_max_width, $cover_max_height, 'MAX_HEIGHT', $dst_tmp);
$src = $dst_tmp;
}
}
$image->clear();
$image->load($src);
$image->crop_from_center($pcfg['cover_width'], $pcfg['cover_height'], $dst);
$image->clear();
$image->load($dst);
$image->set_option('jpeg_quality', 100);
if (!$image->resize($pcfg['cover_width'], $pcfg['cover_height'], 'EXACT', $dst_tmp)) {
continue;
}
rename($dst_tmp, $dst);
$server_id = 0;
if ($pcfg['multi_server']) {
VHelper::load('module.photo.server');
if ($server = VHelper_photo_server::get()) {
$server_id = $server['server_id'];
foreach ($photos as $photo_id => $ext) {
if (!VHelper_photo_server::upload_photo($server, $photo_id, $ext)) {
$server_id = 0;
}
}
if ($server_id) {
VHelper_photo_server::update($server_id);
foreach ($photos as $photo_id => $ext) {
@VFile::delete(MEDIA_DIR.'/photos/'.$photo_id.'.'.$ext);
@VFile::delete(MEDIA_DIR.'/photos/orig/'.$photo_id.'.'.$ext);
@VFile::delete(MEDIA_DIR.'/photos/mobile/'.$photo_id.'.'.$ext);
}
}
}
}
if ($pcfg['thumb_server']) {
VHelper::load('module.photo.thumb');
VHelper_photo_thumb::upload($album_id);
}
$status = ($pcfg['approve']) ? 2 : 1;
$pstatus = ($status === 1) ? 1 : 0;
$this->db->query("UPDATE #__photo_albums
SET total_photos = ".$images.",
cover = ".$cover_id.",
status = '".$status."'
WHERE album_id = ".$album_id."
LIMIT 1");
$this->db->query("UPDATE #__user_activity
SET total_albums = total_albums+1,
total_photos = total_photos+".$images."
WHERE user_id = ".$user_id."
LIMIT 1");
$this->db->query("UPDATE #__photo
SET status = '".$pstatus."',
server = ".$server_id."
WHERE album_id = ".$album_id);
foreach ($category as $cat) {
$cat = (int) $cat;
$this->db->query("INSERT INTO #__photo_category SET cat_id = ".$cat.", album_id = ".$album_id);
$this->db->query("UPDATE #__photo_categories SET total_albums = total_albums+1 WHERE cat_id = ".$cat." LIMIT 1");
}
$tags = explode(',', $tags);
foreach ($tags as $tag) {
$tag = trim($tag);
$this->db->query("INSERT INTO #__photo_tags SET album_id = ".$album_id.", name = '".$this->db->escape($tag)."'");
}
if (isset($dst_tmp)) {
VFile::delete($dst_tmp);
}
$messages[] = 'Album uploaded!';
} else {
throw new VException('Failed to add database entry!');
}
}
}
$tpl = VF::factory('template');
$tpl->menu = 'photo';
$tpl->submenu = 'photo_upload';
$tpl->extramenu = 'photo_upload';
$tpl->meta_title = 'Upload Photo Albums';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->css = array(TPL_REL.'/css/photo.css');
$tpl->album = $album;
$tpl->categories = $categories;
$tpl->load(array('header', 'photo_upload', 'footer'));
$tpl->display();
}
}