Файл: adultscript-2.0.3-pro/files/modules/photo/helpers/thumb.php
Строк: 105
<?php
defined('_VALID') or die('Restricted Access!');
class VHelper_photo_thumb
{
public static function upload($album_id)
{
$db = VF::factory('database');
$db->query("SELECT photo_id
FROM #__photo
WHERE album_id = ".$album_id);
if (!$db->affected_rows()) {
return false;
}
$photos = $db->fetch_rows();
$conn_id = self::connect();
if ($conn_id) {
$base = ftp_pwd($conn_id);
if (!ftp_chdir($conn_id, 'covers')) {
self::log('Failed to change directory to covers!');
ftp_close($conn_id);
return false;
}
$dst = $album_id.'.jpg';
@ftp_delete($conn_id, $dst);
if (!ftp_put($conn_id, $dst, MEDIA_DIR.'/photos/covers/'.$dst, FTP_BINARY)) {
self::log('Failed to upload album cover!');
ftp_close($conn_id);
return false;
}
if (!ftp_site($conn_id, sprintf('CHMOD %u %s', 777, $dst))) {
self::log('Failed to change album cover permissions!');
ftp_close($conn_id);
return false;
}
ftp_chdir($conn_id, $base);
if (!ftp_chdir($conn_id, 'thumbs')) {
self::log('Failed to change directory to thumbs!');
ftp_close($conn_id);
return false;
}
foreach ($photos as $photo) {
$photo_id = $photo['photo_id'];
$src = MEDIA_DIR.'/photos/thumbs/'.$photo_id.'.jpg';
$dst = $photo_id.'.jpg';
if (VFile::exists($src)) {
if (!ftp_put($conn_id, $dst, $src, FTP_BINARY)) {
self::log('Failed to upload: '.$src.' to '.$dst.'!');
ftp_close($conn_id);
return false;
}
if (!ftp_site($conn_id, sprintf('CHMOD %u %s', 777, $dst))) {
self::log('Failed to change permissions for '.$dst.'!');
return false;
}
}
}
}
return true;
}
public static function delete($album_id)
{
$db = VF::factory('database');
$db->query("SELECT photo_id
FROM #__photo
WHERE album_id = ".$album_id);
if (!$db->affected_rows()) {
return false;
}
$photos = $db->fetch_rows();
$conn_id = self::connect();
if ($conn_id) {
@ftp_delete($conn_id, 'covers/'.$album_id.'.jpg');
foreach ($photos as $photo) {
@ftp_delete($conn_id, 'thumbs/'.$photo['photo_id'].'.jpg');
}
ftp_close($conn_id);
}
}
private static function connect()
{
self::log('Initializing thumb server ...');
$host = VF::cfg_item('module.photo.thumb_host');
$port = VF::cfg_item('module.photo.thumb_port');
$username = VF::cfg_item('module.photo.thumb_user');
$password = VF::cfg_item('module.photo.thumb_pass');
$root = VF::cfg_item('module.photo.thumb_root');
$conn_id = ftp_connect($host, $port);
if (!$conn_id) {
self::log('Failed to connect to FTP server!');
return FALSE;
}
if (!ftp_login($conn_id, $username, $password)) {
self::log('Failed to login to FTP server!');
return FALSE;
}
// lets make sure base root does not contain a /
if ($root == '') {
$root = 'media/photos';
} else {
if (substr($root, -1) == '/') {
$root = substr($root, 0, -1);
}
if ($root{0} == '/') {
$root = substr($root, 1);
}
$root = $root.'/media/photos';
}
if (!ftp_chdir($conn_id, $root)) {
self::log('Failed to chdir to '.$root);
return FALSE;
}
return $conn_id;
}
private static function log($data)
{
file_put_contents(TMP_DIR.'/logs/photos-'.md5(VF::cfg_item('secret')).'.log', $data."n", FILE_APPEND);
}
}