nativegallery/app/Controllers/Api/Images/Upload.php

191 lines
7.9 KiB
PHP
Raw Permalink Normal View History

2024-07-05 07:26:39 +03:00
<?php
namespace App\Controllers\Api\Images;
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
use App\Services\Upload as UploadPhoto;
use \Aws\S3\MultipartUploader;
use \Aws\Exception\MultipartUploadException;
class Upload
{
static $continue;
static $photourl;
2024-07-21 02:22:08 +03:00
static $vidpreview;
static $videourl;
2024-10-09 21:45:05 +03:00
static $comments = 'allowed';
static $rating = 'allowed';
static $showtop = 'allowed';
static $subsnotify = 'allowed';
2024-10-12 13:40:58 +03:00
static $exif = 'exif';
2024-10-16 17:16:21 +03:00
static $entitydata_id = 0;
static $entityroute = NULL;
static $entitycomment = NULL;
2024-07-05 07:26:39 +03:00
public static function create($postbody, $content, $exif)
{
$user = new \App\Models\User(Auth::userid());
if (NGALLERY['root']['photo']['upload']['premoderation'] === true) {
2024-07-20 22:09:13 +03:00
if ($user->content('premoderation') === "true" || $user->i('admin') > 0) {
$moderated = 1;
} else {
$moderated = 0;
}
} else {
$moderated = 1;
}
2024-10-16 17:16:21 +03:00
DB::query('INSERT INTO photos VALUES (\'0\', :userid, :postbody, :photourl, :time, :timeup, :exif, 0, :moderated, :place, 0, :gallery, :entityid, :content)', array(':postbody' => $postbody, ':userid' => Auth::userid(), ':time' => mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']), ':content' => $content, ':photourl' => self::$photourl, ':exif' => $exif, ':place' => $_POST['place'], ':timeup' => time(), ':moderated' => $moderated, ':gallery'=>$_POST['gallery'], ':entityid'=>self::$entitydata_id));
2024-10-09 21:45:05 +03:00
if (($moderated === 1) && (self::$subsnotify != 'disabled')) {
2024-07-21 02:22:08 +03:00
$followers = DB::query('SELECT * FROM followers WHERE user_id=:uid', array(':uid' => Auth::userid()));
2024-07-20 23:40:47 +03:00
foreach ($followers as $f) {
2024-07-21 02:22:08 +03:00
DB::query('INSERT INTO followers_notifications VALUES (\'0\', :uid, :fid, :pid, 0)', array(':uid' => Auth::userid(), ':fid' => $f['follower_id'], ':pid' => DB::query('SELECT * FROM photos ORDER BY id DESC LIMIT 1')[0]['id']));
2024-07-20 23:40:47 +03:00
}
}
2024-07-05 07:26:39 +03:00
echo json_encode(
array(
2024-07-05 12:44:31 +03:00
'id' => DB::query('SELECT id FROM photos ORDER BY id DESC LIMIT 1')[0]['id'],
2024-07-05 07:26:39 +03:00
'errorcode' => 0,
'error' => 0
)
);
}
public function __construct()
2024-07-21 02:22:08 +03:00
{
2024-07-05 07:26:39 +03:00
2024-07-05 09:49:49 +03:00
if ($_FILES['image']['error'] != 4) {
2024-07-17 03:58:19 +03:00
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $_FILES['image']['tmp_name']);
if ($type === 'image/gif') {
if (NGALLERY['root']['photo']['upload']['allowgif'] === false) {
echo json_encode(
array(
'errorcode' => 'FILE_NOTSUPPORTED',
'error' => 1
)
);
die();
}
}
2024-07-21 02:22:08 +03:00
if (explode('/', $type)[0] === 'video') {
$newname = GenerateRandomStr::init(64);
$ffmpegPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'E:\Maksim\kandle\app\Controllers\Video\Exec\ffmpeg.exe' : 'ffmpeg';
$tempDir = $_SERVER['DOCUMENT_ROOT'] . '/cdn/temp/';
$mp4File = $tempDir . $newname . '.mp4';
$ffmpegCommand = "$ffmpegPath -i " . $_FILES['image']['tmp_name'] . " -c:v libx264 -crf 18 -fpsmax 60 -preset fast -c:a aac -ac 2 -codec:v copy -codec:a copy $mp4File";
exec($ffmpegCommand);
$thumbnailFile = $tempDir . $newname . '.jpg';
$ffmpegCommand = "$ffmpegPath -i " . $_FILES['image']['tmp_name'] . " -ss 00:00:00 -frames:v 1 -q:v 2 $thumbnailFile";
exec($ffmpegCommand);
$backgroundImagePath = $thumbnailFile;
$overlayImagePath = $_SERVER['DOCUMENT_ROOT'] . '/static/img/playic.png';
$background = imagecreatefromjpeg($backgroundImagePath);
$overlay = imagecreatefrompng($overlayImagePath);
$backgroundWidth = imagesx($background);
$backgroundHeight = imagesy($background);
$overlayWidth = imagesx($overlay);
$overlayHeight = imagesy($overlay);
$destX = ($backgroundWidth - $overlayWidth) / 2;
$destY = ($backgroundHeight - $overlayHeight) / 2;
imagecopy($background, $overlay, $destX, $destY, 0, 0, $overlayWidth, $overlayHeight);
$outputImagePath = $_SERVER['DOCUMENT_ROOT'] . '/cdn/temp/VIDPRV_' . $newname . '.jpg';
imagejpeg($background, $outputImagePath, 90);
imagedestroy($background);
imagedestroy($overlay);
2024-07-21 02:22:08 +03:00
$upload = new UploadPhoto($outputImagePath, 'cdn/img/');
self::$vidpreview = $upload->getSrc();
$upload = new UploadPhoto($mp4File, 'cdn/video/');
2024-07-21 02:59:40 +03:00
echo explode($mp4File, '.')[1];
2024-07-21 02:22:08 +03:00
self::$videourl = $upload->getSrc();
2024-07-21 02:59:40 +03:00
$exif = Json::return(
array(
'type' => 'none',
)
);
} else if (explode('/', $type)[0] === 'image') {
2024-07-21 02:22:08 +03:00
$exif = new EXIF($_FILES['image']['tmp_name']);
$exif = $exif->getData();
$upload = new UploadPhoto($_FILES['image'], 'cdn/img/');
if ($exif === null) {
$exif = Json::return(
array(
'type' => 'none',
)
);
}
} else {
echo json_encode(
2024-07-05 12:44:31 +03:00
array(
2024-07-21 02:22:08 +03:00
'errorcode' => 'FILE_NOTSELECTED',
'error' => 1
2024-07-05 12:44:31 +03:00
)
);
}
2024-07-06 08:05:10 +03:00
if (isset($_POST['nomap'])) {
$_POST['lat'] = null;
$_POST['lng'] = null;
}
2024-10-09 21:59:15 +03:00
if ((int)$_POST['disablecomments'] === 1) {
2024-10-09 21:45:05 +03:00
self::$comments = 'disabled';
}
2024-10-09 21:59:15 +03:00
if ((int)$_POST['disablerating'] === 1) {
2024-10-09 21:45:05 +03:00
self::$rating = 'disabled';
}
2024-10-09 21:59:15 +03:00
if ((int)$_POST['disableshowtop'] === 1) {
2024-10-09 21:45:05 +03:00
self::$showtop = 'disabled';
}
2024-10-09 21:59:15 +03:00
if ((int)$_POST['disablesubsnotify'] === 1) {
2024-10-09 21:45:05 +03:00
self::$subsnotify = 'disabled';
}
2024-10-12 13:40:58 +03:00
if ((int)$_POST['disableexif'] === 1) {
self::$exif = 'disabled';
}
2024-10-16 17:16:21 +03:00
if ((int)$_POST['nid'] >= 1) {
if (DB::query('SELECT id FROM entities_data WHERE id=:id', array(':id'=>$_POST['nid']))[0]['id']) {
self::$entitydata_id = $_POST['nid'];
self::$entityroute = $_POST["route[".$_POST['nid']."]"];
self::$entitycomment = $_POST["notes[".$_POST['nid']."]"];
} else {
return;
}
}
2024-07-05 09:49:49 +03:00
if ($upload->getType() !== null) {
2024-07-05 07:26:39 +03:00
$content = Json::return(
array(
2024-07-21 02:22:08 +03:00
'type' => explode('/', $type)[0],
'videourl' => self::$videourl,
2024-07-05 09:49:49 +03:00
'copyright' => $_POST['license'],
2024-07-05 12:58:12 +03:00
'comment' => $_POST['descr'],
2024-07-06 08:05:10 +03:00
'lat' => $_POST['lat'],
2024-10-09 21:45:05 +03:00
'lng' => $_POST['lng'],
'comments' => self::$comments,
'rating' => self::$rating,
'showtop' => self::$showtop,
2024-10-16 17:16:21 +03:00
'entityroute' => self::$entityroute,
'entitycomment' => self::$entitycomment
2024-07-05 07:26:39 +03:00
)
);
2024-07-21 02:22:08 +03:00
if (explode('/', $type)[0] === 'video') {
self::$photourl = self::$vidpreview;
} else {
self::$photourl = $upload->getSrc();
}
2024-07-05 12:44:31 +03:00
self::create($_POST['descr'], $content, $exif);
2024-07-05 07:26:39 +03:00
}
}
}
2024-07-21 02:22:08 +03:00
}