Файл: adultscript-2.0.3-pro/files/admin/modules/photo/components/upload_ftp.php
Строк: 150
<?php
defined('_VALID') or die('Restricted Access!');
set_time_limit(0);
class VComponent_Admin_photo_upload_ftp extends VModule_Admin_photo
{
private $pcfg;
public function __construct()
{
parent::__construct();
$this->pcfg = VF::cfg('module.photo');
}
public function render()
{
$unique = time().'_'.mt_rand();
$statuses = array(
0 => 'Suspended',
1 => 'Active',
2 => 'Approve Required',
3 => 'Processing'
);
$dir = BASE_DIR.'/tmp/uploads';
$albums = array();
$loaded = FALSE;
$methods = array(
'folder' => 'Load All Folders (Multiple Albums Possible)',
'archive' => 'Load All Archives (ZIPs) (Multiple Albums Possible)'
);
$method = 'folder';
$errors = array();
$messages = array();
$warnings = array();
$import = array(
'username' => '',
'type' => 'public',
'mobile' => (VModule::enabled('mobile')) ? 1 : 0,
'status' => 1,
'password' => '',
'category' => array(),
'min_width' => 250,
'min_height' => 150
);
if (isset($_POST['submit-load'])) {
$filter = VF::factory('filter');
$dir = $filter->get('dir');
$method = $filter->get('method');
if ($dir == '') {
$errors[] = 'Directory (folder) field cannot be left blank!';
} elseif (!file_exists($dir) OR !is_dir($dir)) {
$errors[] = 'Specified directory (folder) not found or not a valid folder!';
}
if (!$errors) {
if ($method == 'archive') {
$found = VFolder::files($dir, TRUE, FALSE, 'zip');
} else {
$found = VFolder::folders($dir, TRUE, FALSE);
}
if ($found) {
$albums = array();
foreach ($found as $item) {
$key = md5($item);
$title = ($method == 'archive') ? basename(VFile::strip_ext($item)) : basename($item);
$title = str_replace('_', ' ', $title);
$description = '';
$tags = 'abc';
$albums[$key] = array(
'key' => $key,
'process' => 1,
'path' => $item,
'title' => $title,
'description' => $description,
'tags' => $tags
);
}
$loaded = TRUE;
} else {
$errors[] = 'Failed to load any albums (folders '.$method.'s found)!';
}
}
}
if (isset($_POST['submit-process'])) {
$filter = VF::factory('filter');
$method = $filter->get('method');
$status = (int) trim($_POST['status']);
$username = $filter->get('username');
$type = $filter->get('type');
$mobile = (int) trim($_POST['mobile']);
$category = (isset($_POST['category'])) ? (array) $_POST['category'] : array();
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
$min_width = (int) trim($_POST['min_width']);
$min_height = (int) trim($_POST['min_height']);
if ($username == '') {
$errors[] = 'Useranme field cannot be left blank!';
} 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');
$import['username'] = $username;
} else {
$errors[] = 'Invalid username! Are you sure this username exists!?';
}
}
if (!$category) {
$errors[] = 'Please check at least one category for your album(s)!';
} else {
$import['category'] = $category;
}
$import['mobile'] = $mobile;
$import['status'] = $status;
$import['type'] = $type;
$import['min_width'] = $min_width;
$import['min_height'] = $min_height;
foreach ($_POST as $key => $values) {
if (is_array($_POST[$key]) && strlen($key) == 32 && is_array($_POST[$key])) {
$albums[$key] = $values;
}
}
if (!$albums) {
$errors[] = 'Failed to find any albums! Application error!?';
}
if (!$errors) {
$etitle = FALSE;
$etags = FALSE;
$etagsf = FALSE;
foreach ($albums as $album) {
if ($album['title'] == '') {
$etitle = TRUE;
}
if ($album['tags'] == '') {
$etags = TRUE;
}
}
if ($etitle) {
$errors[] = 'One or more albums do not have any title set!';
}
if ($etags) {
$errors[] = 'One or more albums do not have any tags set!';
}
if (!$errors) {
session_write_close();
$added = 0;
$failed = 0;
foreach ($albums as $key => $album) {
if ($this->process_album($key, $album, $user_id, $category, $type, $mobile, $status, $min_width, $min_height, $method, $password)) {
++$added;
} else {
++$failed;
}
}
if ($added > 0) {
$messages[] = 'Sucessfuly created '.$added.' albums!';
}
if ($failed > 0) {
$errors[] = 'Failed to create '.$failed.' albums!';
}
}
}
$loaded = TRUE;
}
$tpl = VF::factory('template');
$tpl->menu = 'photo';
$tpl->submenu = 'photo_upload';
$tpl->extramenu = 'photo_upload_ftp';
$tpl->meta_title = 'Admin::Photo::Upload FTP';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->warnings = $warnings;
$tpl->dir = $dir;
$tpl->method = $method;
$tpl->methods = $methods;
$tpl->albums = $albums;
$tpl->loaded = $loaded;
$tpl->import = $import;
$tpl->statuses = $statuses;
$tpl->categories = $this->get_photo_categories();
$tpl->load(array('header', 'photo_upload_ftp', 'footer'));
$tpl->display();
}
private function process_album($key, $album, $user_id, $categories, $type, $mobile, $status, $min_width, $min_height, $method, $password)
{
VF::factory_remove('database');
$this->db = VF::factory('database');
if ($method == 'archive') {
$folder = TMP_DIR.'/uploads/'.$key;
if (!$this->extract_archive($album['path'], $folder)) {
return FALSE;
}
$album['path'] = $folder;
}
if (!file_exists($album['path']) OR !is_dir($album['path'])) {
return FALSE;
}
$files = VFolder::files($album['path'], TRUE, TRUE, $this->pcfg['photo_allowed_ext']);
if (!$files) {
return FALSE;
}
$slug = prepare_string($album['title'], TRUE);
$pass = ($password) ? VHash::encrypted($password) : '';
$this->db->query("INSERT INTO #__photo_albums
SET user_id = ".$user_id.",
title = '".$this->db->escape($album['title'])."',
slug = '".$this->db->escape($slug)."',
description = '".$this->db->escape($album['description'])."',
password = '".$this->db->escape($password)."',
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();
$max_size = $this->pcfg['photo_max_size']*1024*1024;
foreach ($files as $file) {
if (!$image->load($file)) {
continue;
}
$ext = $image->src['ext'];
if (!in_array($ext, $this->pcfg['photo_allowed_ext'])) {
continue;
}
$width = $image->src['width'];
$height = $image->src['height'];
if ($width < $min_width OR $height < $min_height) {
continue;
}
$size = filesize($album['path']);
if ($size > $max_size) {
echo VF::debug($file);
continue;
}
$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($file, $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 < $this->pcfg['photo_width']) {
copy($file, $dst);
} else {
if (!$image->resize($this->pcfg['photo_width'], $this->pcfg['photo_height'], 'MAX_WIDTH', $dst)) {
continue;
}
}
if ($mobile) {
$dst_mobile = MEDIA_DIR.'/photos/mobile/'.$photo_id.'.'.$ext;
if ($width < $this->pcfg['mobile_width']) {
copy($file, $dst_mobile);
} else {
if (!$image->resize($this->pcfg['mobile_width'], $this->pcfg['mobile_height'], 'MAX_WIDTH', $dst_mobile)) {
continue;
}
}
}
$thumb_width = $this->pcfg['thumb_width']+30;
$thumb_height = $this->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($this->pcfg['thumb_width'], $this->pcfg['thumb_height'], $dst_thumb)) {
continue;
}
$image->clear();
$image->load($dst_thumb);
if (!$image->resize($this->pcfg['thumb_width'], $this->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(',', $album['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();
$image->load($src);
if ($image->src['height'] < $this->pcfg['cover_height']) {
$src = MEDIA_DIR.'/photos/orig/'.$cover_id.'.'.$cover_ext;
$image->clear();
$image->load($src);
$cover_max_width = ($this->pcfg['cover_width'] + 30);
$cover_max_height = ($this->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($this->pcfg['cover_width'], $this->pcfg['cover_height'], $dst);
$image->clear();
$image->load($dst);
if (!$image->resize($this->pcfg['cover_width'], $this->pcfg['cover_height'], 'EXACT', $dst_tmp)) {
continue;
}
rename($dst_tmp, $dst);
$server_id = 0;
if ($this->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 ($this->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");
$status = ($status === 1) ? 1 : 0;
$this->db->query("UPDATE #__photo
SET status = '".$status."',
server = ".$server_id."
WHERE album_id = ".$album_id);
if (isset($dst_tmp)) {
VFile::delete($dst_tmp);
}
if ($images > 0) {
return TRUE;
}
}
}
return FALSE;
}
private function extract_archive($file, $folder)
{
if (!file_exists($file) OR
!is_file($file) OR
!is_readable($file)) {
return false;
}
if (!VFolder::create($folder)) {
return false;
}
VF::load('pclzip.pclzip');
$archive = new PclZip($file);
if ($list = $archive->extract(PCLZIP_OPT_PATH, $folder)) {
return true;
} else {
echo VF::debug('Unrecoverable error: '.$archive->errorName(true));
}
return false;
}
}