2016-07-22 20:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
require_once(__DIR__.'/util.php');
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
use Vn\Lib;
|
2016-09-06 14:25:02 +00:00
|
|
|
use Vn\Lib\UserException;
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a file creating its corresponding sizes.
|
2017-05-08 15:54:35 +00:00
|
|
|
*/
|
2018-05-23 10:14:20 +00:00
|
|
|
class Upload extends Vn\Web\JsonRequest {
|
2017-05-08 15:54:35 +00:00
|
|
|
const PARAMS = [
|
|
|
|
'name',
|
2019-02-08 06:57:07 +00:00
|
|
|
'schema'
|
2017-05-08 15:54:35 +00:00
|
|
|
];
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function run($db) {
|
|
|
|
$util = new Util($this->app);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2019-02-07 13:09:56 +00:00
|
|
|
$collection = $_REQUEST['schema'];
|
2017-05-08 15:54:35 +00:00
|
|
|
$name = $_REQUEST['name'];
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2017-05-08 15:54:35 +00:00
|
|
|
// Checks schema
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2019-02-07 13:09:56 +00:00
|
|
|
$info = $util->loadInfo($collection);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if (!$info)
|
2019-02-07 13:09:56 +00:00
|
|
|
throw new UserException(s('Collection not exists'));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2017-05-08 15:54:35 +00:00
|
|
|
// Checks file name
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2019-02-07 13:09:56 +00:00
|
|
|
if (preg_match('/[^a-z0-9_]/', $name) !== 0)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new UserException(s('Bad file name'));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2017-05-08 15:54:35 +00:00
|
|
|
// Checks for file errors
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (empty($_FILES['image']['name']))
|
|
|
|
throw new UserException(s('File not choosed'));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($_FILES['image']['error'] != 0) {
|
|
|
|
switch ($_FILES['image']['error']) {
|
2016-07-22 20:00:27 +00:00
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
|
|
|
$message = 'ErrIniSize';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
|
|
|
$message = 'ErrFormSize';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_PARTIAL:
|
|
|
|
$message = 'ErrPartial';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_NO_FILE:
|
|
|
|
$message = 'ErrNoFile';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
$message = 'ErrNoTmpDir';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
$message = 'ErrCantWrite';
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
$message = 'ErrExtension';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$message = 'ErrDefault';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Lib\Exception(s($message));
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$maxSize = $db->getValue('SELECT maxSize FROM imageConfig');
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if ($_FILES['image']['size'] > $maxSize * 1048576)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new UserException(sprintf(s('File size error'), $maxSize));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2019-02-07 13:09:56 +00:00
|
|
|
// Updates items with matching id, when option is checked
|
|
|
|
|
|
|
|
if ($_REQUEST['updateMatching'] === 'true') {
|
|
|
|
$schema = $db->quote($info['schema']);
|
|
|
|
$table = $db->quote($info['table']);
|
|
|
|
$column = $db->quote($info['column']);
|
2019-02-08 06:57:07 +00:00
|
|
|
|
|
|
|
if (!($schema && $table && column))
|
|
|
|
throw new UserException(s('Cannot update matching id'));
|
2019-02-07 13:09:56 +00:00
|
|
|
|
|
|
|
$pk = $db->getRow ("SHOW INDEX FROM $schema.$table WHERE Key_name = 'PRIMARY'");
|
|
|
|
if (!pk) throw new UserException(s('Cannot update matching id'));
|
|
|
|
|
|
|
|
$pkColumn = $db->quote($pk['Column_name']);
|
|
|
|
|
|
|
|
$query = "UPDATE $schema.$table SET $column = #name WHERE $pkColumn = #name LIMIT 1";
|
|
|
|
$db->query ($query, ['name' => $name]);
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:54:35 +00:00
|
|
|
// Resizes and saves the image
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2017-05-08 15:54:35 +00:00
|
|
|
$tmpName = $_FILES['image']['tmp_name'];
|
|
|
|
$fileName = "{$name}.png";
|
2019-02-07 13:09:56 +00:00
|
|
|
$collectionPath = "{$util->dataDir}/$collection";
|
|
|
|
$fullFile = "$collectionPath/full/$fileName";
|
2016-07-22 20:00:27 +00:00
|
|
|
$symbolicSrc = "../full/$fileName";
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$image = Image::create($tmpName);
|
|
|
|
Image::resizeSave($image, $fullFile, $info['maxHeight'], $info['maxWidth']);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($info['sizes'] as $size => $i) {
|
2019-02-07 13:09:56 +00:00
|
|
|
$dstFile = "$collectionPath/$size/$fileName";
|
2018-05-23 10:14:20 +00:00
|
|
|
Image::resizeSave($image, $dstFile, $i['height'], $i['width'], $i['crop'], $symbolicSrc);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
imagedestroy($image);
|
|
|
|
unlink($tmpName);
|
2016-07-22 20:00:27 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|