Файл: adultscript-2.0.3-pro/files/admin/modules/photo/components/upload_archive.php
Строк: 137
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_photo_upload_archive extends VModule_Admin_photo
{
public function __construct()
{
parent::__construct();
}
public function render()
{
$errors = array();
$messages = array();
$pcfg = VF::cfg('module.photo');
$album = array(
'username' => '',
'title' => '',
'description' => '',
'categories' => array(),
'tags' => '',
'type' => 'public',
'status' => 1,
'method' => 'url',
'url' => '',
'min_width' => 250,
'min_height' => 150
);
if (isset($_POST['submit-archive'])) {
$filter = VF::factory('filter');
$username = $filter->get('username');
$title = $filter->get('title');
$description = $filter->get('description');
$tags = $filter->get('tags');
$categories = (isset($_POST['category'])) ? (array) $_POST['category'] : array();
$type = $filter->get('type');
$status = (int) trim($_POST['status']);
$method = $filter->get('method');
$min_width = (int) trim($_POST['min_width']);
$min_height = (int) trim($_POST['min_height']);
$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 (!$categories) {
$errors[] = 'Please select at least one category for this album!';
} else {
$album['categories'] = $categories;
}
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 ($method == 'url') {
$url = trim($_POST['url']);
if ($url == '') {
$errors[] = 'Album archive URL field cannot be left blank!';
} elseif (!VValid::url($url)) {
$errors[] = 'Album archive URL is not a valid URL Address!';
} else {
$file = tempnam(TMP_DIR.'/uploads', 'album_');
chmod($file, 0644);
session_write_close();
if (!VCurl::file($url, $file)) {
$errors[] = 'Failed to download album archive!';
}
}
} elseif ($method == 'file') {
if ($upload = VUpload::process('archive', 0, array('zip'))) {
$file = tempnam(TMP_DIR.'/uploads', 'album_');
if (!move_uploaded_file($upload['path'], $file)) {
$errors[] = 'Failed to move uploaded file!';
}
} else {
$errors[] = VUpload::error();
}
}
$album['type'] = $type;
if (!$errors) {
$slug = prepare_string($title, TRUE);
$mobile = ($pcfg['mobile'] == '1') ? 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');
$folder = TMP_DIR.'/uploads/album-'.$album_id;
VFolder::create($folder);
VF::load('pclzip.pclzip');
$archive = new PclZip($file);
$files = $archive->extract(PCLZIP_OPT_PATH,$folder);
if ($files) {
$image = VF::factory('image');
$images = 0;
$photos = array();
foreach ($files as $file) {
$path = $file['filename'];
if (!file_exists($path) OR !is_file($path)) {
continue;
}
if (!$image->load($path)) {
continue;
}
$ext = $image->src['ext'];
if (!in_array($ext, $pcfg['photo_allowed_ext'])) {
continue;
}
$width = $image->src['width'];
$height = $image->src['height'];
if ($width < $min_width OR $height < $min_height) {
continue;
}
$size = filesize($path);
$this->db->query("INSERT INTO #__photo
SET album_id = ".$album_id.",
caption = '',
ext = '".$this->db->escape($ext)."',
size = ".$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] = $ext;
$dst_orig = MEDIA_DIR.'/photos/orig/'.$photo_id.'.'.$ext;
if (!copy($path, $dst_orig)) {
continue;
}
$dst = MEDIA_DIR.'/photos/'.$photo_id.'.'.$ext;
$dst_thumb = MEDIA_DIR.'/photos/thumbs/'.$photo_id.'.jpg';
$dst_thumb_tmp = TMP_DIR.'/images/'.$photo_id.'.thumb.jpg';
if ($width < $pcfg['photo_width']) {
copy($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.'.'.$ext;
if ($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);
}
foreach ($categories as $category) {
$category = (int) $category;
$this->db->query("INSERT INTO #__photo_category
SET cat_id = ".$category.",
album_id = ".$album_id);
$this->db->query("UPDATE #__photo_categories
SET total_albums = total_albums+1
WHERE cat_id = ".$category." 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($cover_id)) {
$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();
if ($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);
if (!$image->resize($pcfg['cover_width'], $pcfg['cover_height'], 'EXACT', $dst_tmp)) {
continue;
}
rename($dst_tmp, $dst);
} else {
$errors[] = $image->get_error().' ('.$src.')';
}
$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);
}
$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");
$pstatus = ($status === 1) ? 1 : 0;
$this->db->query("UPDATE #__photo
SET status = '".$pstatus."',
server = ".$server_id."
WHERE album_id = ".$album_id);
if (isset($dst_tmp)) {
VFile::delete($dst_tmp);
}
$messages[] = 'Album uploaded!';
} else {
$errors[] = 'Failed to add at least one photo from archive! Aborting...';
}
} else {
$errors[] = 'Failed to extract album archive! Corrupt!?';
}
} else {
$errors = 'Failed to create album database entry!';
}
}
}
$tpl = VF::factory('template');
$tpl->menu = 'photo';
$tpl->submenu = 'photo_upload';
$tpl->extramenu = 'photo_upload_archive';
$tpl->meta_title = 'Photo::Upload::Archive';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->album = $album;
$tpl->categories = $this->get_photo_categories();
$tpl->load(array('header', 'photo_upload_archive', 'footer'));
$tpl->display();
}
}