forked from 4ducky/4ducky
The PHP sources, templates and old jQuery-based scripts move unchanged into legacy/ for reference. Shared assets (media/, images/, favicon.ico, the default avatar) stay at the repository root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
325 lines
8.3 KiB
PHP
325 lines
8.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
The ImageUploader class handles the insertion of images into the system -
|
|
whether they were uploaded or should be copied from a remote URL
|
|
*/
|
|
|
|
require_once( 'lib/config.php' );
|
|
require_once( 'lib/db.php' );
|
|
require_once( 'lib/filesystem.php' );
|
|
require_once( 'lib/images.php' );
|
|
require_once( 'lib/users.php' );
|
|
require_once( 'lib/keyword.php' );
|
|
|
|
class ImageUploader {
|
|
|
|
|
|
public static function writeError($error) {
|
|
|
|
$handle = fopen('./import/log.txt', 'a');
|
|
fwrite($handle, $error);
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
public static function copyFromUrl( $url, $tags, $referer, &$errors,$commentUpload=false,$cid=0) {
|
|
|
|
$upl = DB::getRow("SELECT * FROM pl_settings");
|
|
if ($upl["upload"] != 1){
|
|
$errors['notLoggedIn'] = 'Upload disabled.';
|
|
return false;
|
|
}
|
|
|
|
$url = str_replace("https", "http", $url);
|
|
|
|
if( stripos( $url, 'http://' ) !== 0 ) {
|
|
$url = 'http://'.$url;
|
|
}
|
|
// no recursion, no non-images
|
|
if(
|
|
stripos( $url, Config::$frontendPath ) !== false ||
|
|
!preg_match( '/([^\/]*?)\.(gif|jpg|jpeg|png)$/i', $url, $fileMatch )
|
|
) {
|
|
$errors['downloadFailed'] = 'The Image could not be downloaded!';
|
|
return false;
|
|
}
|
|
|
|
|
|
$fileName = $fileMatch[1];
|
|
// use the domain name as part of the keyword
|
|
$urlParts = parse_url( $url );
|
|
if( preg_match( '/(^|\.)([\w\-]+)\.\w+$/', $urlParts['host'], $m ) ) {
|
|
$fileName = strtolower($m[2]).'-'.$fileName;
|
|
}
|
|
$fileName = Keyword::get( $fileName ).'.'.$fileMatch[2];
|
|
$targetPath = './import/'.$fileName;
|
|
|
|
if( !Filesystem::download( $url, $targetPath, Config::$images['maxDownload'], $referer ) ) {
|
|
$errors['downloadFailed'] = 'The Image could not be downloaded!';
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
if( self::process( $fileName, $targetPath, $tags, false, $errors, $url,$commentUpload,$cid) ) {
|
|
return true;
|
|
} else {
|
|
@unlink( $targetPath );
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function process( $name, $localPath, $tags, $uploaded, &$errors, $source, $commentUpload=false,$cid=0) {
|
|
|
|
$upl = DB::getRow("SELECT * FROM pl_settings");
|
|
if ($upl["upload"] != 1){
|
|
$errors['downloadFailed'] = 'Upload disabled.';
|
|
return false;
|
|
}
|
|
|
|
self::writeError("test");
|
|
|
|
$user = new User();
|
|
$user->login();
|
|
|
|
if( !$user->id ) {
|
|
$errors['notLoggedIn'] = 'You are not logged in!';
|
|
return false;
|
|
}
|
|
|
|
// no non-images
|
|
if(
|
|
!preg_match( '/([^\/]*?)\.(gif|jpg|jpeg|png)$/i', $name, $fileMatch )
|
|
) {
|
|
$errors['wrongExtension'] = 'Your file has not the right extension (gif, jpg or png)!';
|
|
self::writeError("1");
|
|
return false;
|
|
}
|
|
|
|
$time = time();
|
|
$keyword = Keyword::get( $fileMatch[1] );
|
|
|
|
// Iterate over all keywords, if this one is allready taken
|
|
$taken = array();
|
|
$r = DB::query( 'SELECT keyword FROM '.TABLE_IMAGES.' WHERE keyword LIKE :1', date('Y/m/',$time).$keyword.'%' );
|
|
foreach( $r as $v ) {
|
|
$taken[] = substr($v['keyword'],8); // Keyword without date!
|
|
}
|
|
$keyword = Keyword::iterate( $keyword, $taken );
|
|
|
|
list( $srcWidth, $srcHeight, $type, $attr ) = getimagesize($localPath);
|
|
|
|
if( $type == IMAGETYPE_JPEG ) {
|
|
$extension = 'jpg';
|
|
} else if( $type == IMAGETYPE_GIF ) {
|
|
$extension = 'gif';
|
|
} else if( $type == IMAGETYPE_PNG ) {
|
|
$extension = 'png';
|
|
} else if($uploaded == false) {
|
|
if(preg_match( '/([^\/]*?)\.(jpg|jpeg)$/i', $localPath, $fileMatch )) {
|
|
$extension = 'jpg';
|
|
}
|
|
else if(preg_match( '/([^\/]*?)\.(png)$/i', $localPath, $fileMatch )) {
|
|
$extension = 'png';
|
|
}
|
|
else if(preg_match( '/([^\/]*?)\.(gif)$/i', $localPath, $fileMatch )) {
|
|
$extension = 'gif';
|
|
}
|
|
|
|
} else {
|
|
self::writeError("2");
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
$thumbName = $keyword.".jpg"; // all thumbs are jpegs!
|
|
$name = $keyword.".".$extension;
|
|
$imageDir = Config::$images['imagePath'] . date('Y/m', $time);
|
|
$thumbDir = Config::$images['thumbPath'] . date('Y/m', $time);
|
|
$imagePath = $imageDir.'/'.$name;
|
|
|
|
if( !Filesystem::mkdirr($imageDir) || !Filesystem::mkdirr($thumbDir) ) {
|
|
$errors['downloadFailed'] = 'folder failed';
|
|
self::writeError("3");
|
|
return false;
|
|
}
|
|
|
|
if( $uploaded ) {
|
|
if( !@move_uploaded_file( $localPath, $imagePath ) ) {
|
|
$errors['downloadFailed'] = 'move failed';
|
|
return false;
|
|
}
|
|
} else {
|
|
if( !@rename( $localPath, $imagePath ) ) {
|
|
$errors['downloadFailed'] = 'rename failed';
|
|
self::writeError($extension);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
self::writeError($imagePath);
|
|
|
|
if($commentUpload == false && $cid == 0){
|
|
$hash = md5_file( $imagePath );
|
|
$c = DB::getRow('SELECT id,keyword FROM '.TABLE_IMAGES.' WHERE hash = :1', $hash);
|
|
if( !empty( $c ) ) {
|
|
|
|
unlink( $imagePath );
|
|
$errors['duplicate'] =
|
|
'This image is already in our database!'."<br /><a href='http://www.4ducky.com/all/".'view/'.end($c)."'>Click <u>here</u> to comment it.</a>";
|
|
self::writeError("5");
|
|
return false;
|
|
|
|
}else{
|
|
$c = DB::query('SELECT id FROM pl_bannedimages WHERE hash = :1', $hash);
|
|
if( !empty( $c ) ) {
|
|
unlink( $imagePath );
|
|
$errors['duplicate'] = 'This image is already in our database!';
|
|
self::writeError("6");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// make thumbs
|
|
foreach( Config::$gridView['classes'] as $gc ) {
|
|
$thumbPath = $thumbDir.'/'.$gc['dir'].'/'.$thumbName;
|
|
Filesystem::mkdirr($thumbDir.'/'.$gc['dir']);
|
|
if( !Image::createThumb( $imagePath, $thumbPath,
|
|
$gc['width'] * Config::$gridView['gridSize'] - Config::$gridView['borderWidth'],
|
|
$gc['height'] * Config::$gridView['gridSize'] - Config::$gridView['borderWidth']
|
|
)) {
|
|
unlink( $imagePath );
|
|
$errors['internalError'] = 'Error generating thumbnails!';
|
|
return false;
|
|
}
|
|
}
|
|
|
|
///watermark
|
|
watermark($imagePath,"media/watermark.png",$imagePath);
|
|
///watermark
|
|
|
|
$user->increaseScore( Config::$postScore );
|
|
|
|
|
|
|
|
|
|
if (($commentUpload) && ($cid != 0)){
|
|
|
|
DB::query('INSERT INTO pl_commentuploads (c_id,image_link,thumb_link) VALUES (:1,:2,:3)',$cid,$imagePath,"data/thumbs/".date('Y/m/', $time)."64x64/".$thumbName);
|
|
|
|
|
|
|
|
|
|
/*DB::insertRow( "pl_commentuploads",array('c_id' => $cid,"image_link" =>$imagePath,"thumb_link" => "data/thumbs/".date('Y/m/', $time)."/128x128/".$name));*/
|
|
|
|
}else{
|
|
$iCount = DB::getRow("SELECT COUNT(*) as c FROM pl_images WHERE user=:1",$user->id);
|
|
DB::query( 'UPDATE '.TABLE_USERS.' SET images =:2 WHERE id = :1', $user->id,(int)$iCount["c"]);
|
|
|
|
DB::insertRow( TABLE_IMAGES,
|
|
array(
|
|
'logged' => date('Y-m-d H:i:s', $time),
|
|
'user' => $user->id,
|
|
'score' => 1,
|
|
'votes' => 0,
|
|
'keyword' => date('Y/m/', $time).$keyword,
|
|
'tags' => $tags,
|
|
'image' => $name,
|
|
'thumb' => $thumbName,
|
|
'hash' => $hash,
|
|
'source' => $source
|
|
)
|
|
);
|
|
}
|
|
|
|
$imageId = DB::insertId();
|
|
|
|
/*$colors = Image::getColors( $imagePath, 4 );
|
|
foreach( $colors as $c ) {
|
|
DB::insertRow( TABLE_IMAGECOLORS,
|
|
array(
|
|
'imageId' => $imageId,
|
|
'r' => $c['r'],
|
|
'g' => $c['g'],
|
|
'b' => $c['b']
|
|
)
|
|
);
|
|
}*/
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
function watermark($SourceFile, $WatermarkFile, $SaveToFile = NULL)
|
|
|
|
{
|
|
|
|
list( $srcWidth, $srcHeight, $type ) = getimagesize( $SourceFile );
|
|
if( $type != IMAGETYPE_GIF ) {
|
|
|
|
$watermark = @imagecreatefrompng($WatermarkFile)
|
|
|
|
|
|
or exit('Cannot open the watermark file.');
|
|
|
|
imageAlphaBlending($watermark, false);
|
|
|
|
imageSaveAlpha($watermark, true);
|
|
|
|
$image_string = @file_get_contents($SourceFile)
|
|
|
|
|
|
or exit('Cannot open image file.');
|
|
|
|
$image = @imagecreatefromstring($image_string)
|
|
|
|
|
|
or exit('Not a valid image format.');
|
|
|
|
/*$tmp = imagecreatetruecolor($srcWidth,($srcHeight + 30));*/
|
|
|
|
/*$back = imagecolorallocatealpha($image, 0, 0, 0,60);
|
|
imagefilledrectangle($image,0, $srcHeight - 20,$srcWidth,$srcHeight ,$back); */
|
|
|
|
/*imagecopy($tmp,$image,0,0,0,0,$srcWidth,$srcHeight);
|
|
|
|
$image = $tmp;*/
|
|
|
|
|
|
$imageWidth=imageSX($image);
|
|
$imageHeight=imageSY($image);
|
|
|
|
$watermarkWidth=imageSX($watermark);
|
|
$watermarkHeight=imageSY($watermark);
|
|
|
|
$coordinate_X = ( $imageWidth) - ( $watermarkWidth);
|
|
$coordinate_Y = ( $imageHeight) - ( $watermarkHeight);
|
|
|
|
imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 0, 0, $watermarkWidth, $watermarkHeight);
|
|
|
|
if(!($SaveToFile)) header('Content-Type: image/jpeg');
|
|
|
|
imagejpeg ($image, $SaveToFile, 100);
|
|
|
|
|
|
imagedestroy($image);
|
|
|
|
imagedestroy($watermark);
|
|
|
|
if(!($SaveToFile)) exit;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
?>
|