From 62729088c5551521ac00299e56a0037a977927d3 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Tue, 2 Jun 2020 22:25:06 +0200 Subject: [PATCH] Add max upload size to config --- .tests/fixtures/fixture_non_admin.xml | 1 + .tests/tests/MigrateTest.php | 3 +- helpers/config.php | 78 ++++++++++++++++----------- helpers/upload.php | 2 +- index.php | 2 +- js/src/ajax.js | 13 +++-- js/src/config.js | 40 +++++++------- js/src/configdialogmodel.js | 43 ++++++++------- js/test/config.test.js | 1 + js/test/configdialogmodel.test.js | 2 +- lang/en.php | 3 +- scripts/migrate_to_1_x.mysql | 3 +- scripts/migrate_to_1_x.php | 9 ++-- scripts/setup.php | 9 ++-- scripts/ulogger.mysql | 3 +- scripts/ulogger.pgsql | 3 +- scripts/ulogger.sqlite | 3 +- utils/getinit.php | 20 ++++--- utils/import.php | 9 ---- utils/saveconfig.php | 3 +- 20 files changed, 142 insertions(+), 108 deletions(-) diff --git a/.tests/fixtures/fixture_non_admin.xml b/.tests/fixtures/fixture_non_admin.xml index 9ace6e0..c4345cd 100644 --- a/.tests/fixtures/fixture_non_admin.xml +++ b/.tests/fixtures/fixture_non_admin.xml @@ -22,5 +22,6 @@ + diff --git a/.tests/tests/MigrateTest.php b/.tests/tests/MigrateTest.php index 10d52a1..daaa549 100644 --- a/.tests/tests/MigrateTest.php +++ b/.tests/tests/MigrateTest.php @@ -80,7 +80,8 @@ class MigrateTest extends UloggerDatabaseTestCase { ["name" => "stroke_color", "value" => "s:7:\"#abcdef\";"], ["name" => "stroke_opacity", "value" => "i:1;"], ["name" => "stroke_weight", "value" => "i:22;"], - ["name" => "units", "value" => "s:8:\"imperial\";"] + ["name" => "units", "value" => "s:8:\"imperial\";"], + ["name" => "upload_maxsize", "value" => "i:0;"] ]]; $actual = $this->getConnection()->createQueryTable( "config", diff --git a/helpers/config.php b/helpers/config.php index a76b413..d13c2c8 100644 --- a/helpers/config.php +++ b/helpers/config.php @@ -131,8 +131,14 @@ class uConfig { * @var string Stroke color */ public $colorHilite = "#feff6a"; + /** + * @var int Maximum size of uploaded files in bytes. + * Will be adjusted to system maximum upload size + */ + public $uploadMaxSize; public function __construct($useDatabase = true) { + $this->uploadMaxSize = uUtils::getUploadMaxSize(); if ($useDatabase) { $this->setFromDatabase(); } @@ -215,47 +221,49 @@ class uConfig { $placeholder = self::db()->lobPlaceholder(); $query = "UPDATE " . self::db()->table('config') . " SET value = CASE name - WHEN 'map_api' THEN $placeholder - WHEN 'latitude' THEN $placeholder - WHEN 'longitude' THEN $placeholder - WHEN 'google_key' THEN $placeholder - WHEN 'require_auth' THEN $placeholder - WHEN 'public_tracks' THEN $placeholder - WHEN 'pass_lenmin' THEN $placeholder - WHEN 'pass_strength' THEN $placeholder - WHEN 'interval_seconds' THEN $placeholder - WHEN 'lang' THEN $placeholder - WHEN 'units' THEN $placeholder - WHEN 'stroke_weight' THEN $placeholder - WHEN 'stroke_color' THEN $placeholder - WHEN 'stroke_opacity' THEN $placeholder + WHEN 'color_extra' THEN $placeholder + WHEN 'color_hilite' THEN $placeholder WHEN 'color_normal' THEN $placeholder WHEN 'color_start' THEN $placeholder WHEN 'color_stop' THEN $placeholder - WHEN 'color_extra' THEN $placeholder - WHEN 'color_hilite' THEN $placeholder + WHEN 'google_key' THEN $placeholder + WHEN 'interval_seconds' THEN $placeholder + WHEN 'lang' THEN $placeholder + WHEN 'latitude' THEN $placeholder + WHEN 'longitude' THEN $placeholder + WHEN 'map_api' THEN $placeholder + WHEN 'pass_lenmin' THEN $placeholder + WHEN 'pass_strength' THEN $placeholder + WHEN 'public_tracks' THEN $placeholder + WHEN 'require_auth' THEN $placeholder + WHEN 'stroke_color' THEN $placeholder + WHEN 'stroke_opacity' THEN $placeholder + WHEN 'stroke_weight' THEN $placeholder + WHEN 'units' THEN $placeholder + WHEN 'upload_maxsize' THEN $placeholder END"; $stmt = self::db()->prepare($query); $params = [ - $this->mapApi, - $this->initLatitude, - $this->initLongitude, - $this->googleKey, - $this->requireAuthentication, - $this->publicTracks, - $this->passLenMin, - $this->passStrength, - $this->interval, - $this->lang, - $this->units, - $this->strokeWeight, - $this->strokeColor, - $this->strokeOpacity, + $this->colorExtra, + $this->colorHilite, $this->colorNormal, $this->colorStart, $this->colorStop, - $this->colorExtra, - $this->colorHilite + $this->googleKey, + $this->initLatitude, + $this->initLongitude, + $this->interval, + $this->lang, + $this->mapApi, + $this->passLenMin, + $this->passStrength, + $this->publicTracks, + $this->requireAuthentication, + $this->strokeColor, + $this->strokeOpacity, + $this->strokeWeight, + $this->units, + $this->uploadMaxSize ]; $stmt->execute(array_map('serialize', $params)); @@ -419,6 +427,12 @@ class uConfig { if (isset($arr['color_hilite']) && !empty($arr['color_hilite'])) { $this->colorHilite = $arr['color_hilite']; } + if (isset($arr['upload_maxsize']) && is_numeric($arr['upload_maxsize'])) { + $this->uploadMaxSize = (int) $arr['upload_maxsize']; + if ($this->uploadMaxSize === 0 || $this->uploadMaxSize > uUtils::getUploadMaxSize()) { + $this->uploadMaxSize = uUtils::getUploadMaxSize(); + } + } } } diff --git a/helpers/upload.php b/helpers/upload.php index 15a4380..2fdfbf0 100644 --- a/helpers/upload.php +++ b/helpers/upload.php @@ -32,7 +32,7 @@ class uUpload { const META_ERROR = "error"; const META_SIZE = "size"; public static $uploadDir = ROOT_DIR . "/uploads/"; - private static $filePattern = "[a-z0-9_.]{20,}"; + private static $filePattern = "/[a-z0-9_.]{20,}/"; private static $mimeMap = []; /** diff --git a/index.php b/index.php index bf1217e..6812991 100644 --- a/index.php +++ b/index.php @@ -125,7 +125,7 @@
- +
gpx diff --git a/js/src/ajax.js b/js/src/ajax.js index ce5bd4b..9551ca9 100644 --- a/js/src/ajax.js +++ b/js/src/ajax.js @@ -42,7 +42,7 @@ export default class uAjax { /** * Perform ajax HTTP request * @param {string} url Request URL - * @param {Object|HTMLFormElement} [data] Optional request parameters: key/value pairs or form element + * @param {Object|HTMLFormElement|FormData} [data] Optional request parameters: key/value pairs or form element * @param {Object} [options] Optional options * @param {string} [options.method='GET'] Optional query method, default 'GET' * @return {Promise} @@ -81,12 +81,15 @@ export default class uAjax { reject(new Error(message)); } }; - let body; if (data instanceof HTMLFormElement) { + data = new FormData(data); + } + let body; + if (data instanceof FormData) { if (method === 'POST') { - body = new FormData(data); + body = data; } else { - body = new URLSearchParams(new FormData(data)).toString(); + body = new URLSearchParams(data).toString(); } } else { for (const key in data) { @@ -108,7 +111,7 @@ export default class uAjax { body = null; } xhr.open(method, url, true); - if (method === 'POST' && !(data instanceof HTMLFormElement)) { + if (method === 'POST' && !(data instanceof FormData)) { xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); } xhr.send(body); diff --git a/js/src/config.js b/js/src/config.js index 4980ad3..60d36de 100644 --- a/js/src/config.js +++ b/js/src/config.js @@ -23,28 +23,29 @@ import uObserve from './observe.js'; /** * @class uConfig - * @property {number} interval; - * @property {string} units - * @property {string} lang - * @property {string} mapApi - * @property {string} googleKey - * @property {uLayerCollection} olLayers - * @property {number} initLatitude - * @property {number} initLongitude - * @property {number} initLongitude - * @property {boolean} requireAuth - * @property {boolean} publicTracks - * @property {number} passStrength - * @property {number} passLenMin - * @property {number} strokeWeight - * @property {string} strokeColor - * @property {number} strokeOpacity - * @property {boolean} showLatest + * @property {string} colorExtra + * @property {string} colorHilite * @property {string} colorNormal * @property {string} colorStart * @property {string} colorStop - * @property {string} colorExtra - * @property {string} colorHilite + * @property {string} googleKey + * @property {number} initLatitude + * @property {number} initLongitude + * @property {number} initLongitude + * @property {number} interval; + * @property {string} lang + * @property {string} mapApi + * @property {uLayerCollection} olLayers + * @property {number} passLenMin + * @property {number} passStrength + * @property {boolean} publicTracks + * @property {boolean} requireAuth + * @property {boolean} showLatest + * @property {string} strokeColor + * @property {number} strokeOpacity + * @property {number} strokeWeight + * @property {string} units + * @property {number} uploadMaxSize */ export default class uConfig { @@ -74,6 +75,7 @@ export default class uConfig { this.colorStop = '#ff6a00'; this.colorExtra = '#cccccc'; this.colorHilite = '#feff6a'; + this.uploadMaxSize = 0; this.initUnits(); } diff --git a/js/src/configdialogmodel.js b/js/src/configdialogmodel.js index 1404ba8..aa67b8d 100644 --- a/js/src/configdialogmodel.js +++ b/js/src/configdialogmodel.js @@ -32,29 +32,30 @@ export default class ConfigDialogModel extends ViewModel { constructor() { super({ - interval: config.interval, - units: config.units, - lang: config.lang, - mapApi: config.mapApi, - googleKey: config.googleKey, - layerId: config.olLayers.getPriorityLayer().toString(), - layers: new uLayerCollection(new uLayer(0, 'OpenStreetMap', '', 0), ...config.olLayers), - layerName: null, - layerUrl: null, - initLatitude: config.initLatitude, - initLongitude: config.initLongitude, - requireAuth: config.requireAuth, - publicTracks: config.publicTracks, - passStrength: config.passStrength, - passLenMin: config.passLenMin, - strokeWeight: config.strokeWeight, - strokeColor: config.strokeColor, - strokeOpacity: config.strokeOpacity, + colorExtra: config.colorExtra, + colorHilite: config.colorHilite, colorNormal: config.colorNormal, colorStart: config.colorStart, colorStop: config.colorStop, - colorExtra: config.colorExtra, - colorHilite: config.colorHilite + googleKey: config.googleKey, + initLatitude: config.initLatitude, + initLongitude: config.initLongitude, + interval: config.interval, + lang: config.lang, + layerId: config.olLayers.getPriorityLayer().toString(), + layerName: null, + layers: new uLayerCollection(new uLayer(0, 'OpenStreetMap', '', 0), ...config.olLayers), + layerUrl: null, + mapApi: config.mapApi, + passLenMin: config.passLenMin, + passStrength: config.passStrength, + publicTracks: config.publicTracks, + requireAuth: config.requireAuth, + strokeColor: config.strokeColor, + strokeOpacity: config.strokeOpacity, + strokeWeight: config.strokeWeight, + units: config.units, + uploadMaxSize: config.uploadMaxSize }); this.model.onCancel = () => this.onCancel(); this.model.onSave = () => this.onSave(); @@ -242,6 +243,8 @@ export default class ConfigDialogModel extends ViewModel { +