From c061d367e4e4e2b1b5b1517ade90b3b2dec4df05 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 9 Jul 2023 02:32:01 +0000 Subject: [PATCH] backup --- Controller/ApiController.php | 47 +++++- Theme/Backend/Lang/en.lang.php | 1 + .../resource-comparison-inline.tpl.php | 134 ++++++++++++++++++ Theme/Backend/resource-single.tpl.php | 59 +------- tests/Bootstrap.php | 6 +- 5 files changed, 179 insertions(+), 68 deletions(-) create mode 100644 Theme/Backend/resource-comparison-inline.tpl.php diff --git a/Controller/ApiController.php b/Controller/ApiController.php index e929c58..3dd8fbf 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -34,7 +34,9 @@ use phpOMS\Message\NotificationLevel; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; +use phpOMS\System\File\FileUtils; use phpOMS\System\File\Local\Directory; +use phpOMS\System\MimeType; use phpOMS\System\OperatingSystem; use phpOMS\System\SystemType; use phpOMS\System\SystemUtils; @@ -74,6 +76,12 @@ final class ApiController extends Controller ->where('id', (int) $request->getData('id')) ->execute(); + if ($request->hasData('path')) { + // @todo: check if user has permission? + $this->app->moduleManager->get('Media', 'Api')->apiMediaExport($request, $response, ['guard' => __DIR__ . '/../Files']); + return; + } + $path = ''; $basePath = __DIR__ . '/../Files/' . $resource->path . '/' . $resource->lastVersionPath; @@ -124,7 +132,9 @@ final class ApiController extends Controller { $val = []; if (($val['title'] = !$request->hasData('title')) - || ($val['uri'] = (!$request->hasData('uri') || \filter_var($request->getDataString('uri'), \FILTER_VALIDATE_URL) === false)) + || ($val['uri'] = (!$request->hasData('uri') + || (\filter_var($request->getDataString('uri'), \FILTER_VALIDATE_URL) === false && \stripos($request->getDataString('uri'), 'www.') !== 0)) + ) ) { return $val; } @@ -330,7 +340,7 @@ final class ApiController extends Controller $totalCount = \count($toCheck); $maxLoops = (int) \min(60 * 10, $totalCount * 10 / 4); // At most run 600 times or 2.5 times the resource count $startTime = \time(); - $minTime = $startTime + ((int) \max(10 * $totalCount, 60 * 5)); // At least run 10 seconds per element or 5 minutes + $minTime = $startTime + ((int) \max(10 * $totalCount, 60)); // At least run 10 seconds per element or 5 minutes $maxTime = $startTime + ((int) \min(60 * $totalCount, 60 * 60 * 3)); // At most run 60 seconds per element or 3 hours while (!empty($toCheck)) { @@ -404,7 +414,20 @@ final class ApiController extends Controller continue; } - $extension = ($pos = \strrpos($file, '.')) !== false ? \substr($file, $pos + 1) : ''; + $extension = ($pos = \strrpos($file, '.')) !== false ? \substr($file, $pos + 1) : ''; + $possibleExtension = MimeType::mimeToExtension(\mime_content_type($path . '/' . $file)); + + $newFileName = FileUtils::makeSafeFileName($file); + if ($possibleExtension !== null && $possibleExtension !== $extension) { + $extension = $possibleExtension; + $newFileName .= '.' . $extension; + } + + if ($file !== $newFileName) { + \rename($path . '/' . $file, $path . '/' . $newFileName); + $file = $newFileName; + } + if (StringUtils::endsWith($file, '.' . $extension)) { $fileName = $file; $toCheck[$index]['handled'] = true; @@ -666,10 +689,20 @@ final class ApiController extends Controller } // Make sure that no wkhtmltoimage processes are running - if (OperatingSystem::getSystem() === SystemType::LINUX) { - SystemUtils::runProc('pkill', '-f wkhtmltoimage', true); - } else { - SystemUtils::runProc('taskkill', '/F /IM wkhtmltoimage.exe', true); + $time = \time(); + if ($time - $minTime < 3) { + // The shortest time interval we give the script to download the images + \sleep(3); + } + + if ($time > $minTime) { + // @todo: create a separate function which is called async minTime - time seconds after + // This solution is just a workaround for small lists which would otherwise be forced to wait at least 60 seconds. + if (OperatingSystem::getSystem() === SystemType::LINUX) { + SystemUtils::runProc('pkill', '-f wkhtmltoimage', true); + } else { + SystemUtils::runProc('taskkill', '/F /IM wkhtmltoimage.exe', true); + } } Directory::delete($basePath . '/temp'); diff --git a/Theme/Backend/Lang/en.lang.php b/Theme/Backend/Lang/en.lang.php index 228677e..483eccb 100755 --- a/Theme/Backend/Lang/en.lang.php +++ b/Theme/Backend/Lang/en.lang.php @@ -53,6 +53,7 @@ return ['OnlineResourceWatcher' => [ 'SignIn' => 'Sign In', 'Status' => 'Status', 'Submit' => 'Submit', + 'Title' => 'Title', 'Terms' => 'Terms', 'Trigger' => 'Trigger', 'Type' => 'Type', diff --git a/Theme/Backend/resource-comparison-inline.tpl.php b/Theme/Backend/resource-comparison-inline.tpl.php new file mode 100644 index 0000000..d89ef2c --- /dev/null +++ b/Theme/Backend/resource-comparison-inline.tpl.php @@ -0,0 +1,134 @@ + + +
+ status === ReportStatus::DOWNLOAD_ERROR) { + continue; + } + + $old = $new; + $new = $report; + + if ($old === null) { + $old = $report; + } + } + + if ($resource->checkedAt !== null) : + $type = ''; + + if ($new !== null) { + $newBasePath = __DIR__ . '/../../Files/' . $resource->path . '/' . $new->versionPath; + $newWebPath = 'Modules/OnlineResourceWatcher/Files/' . $resource->path . '/' . $new->versionPath; + + if (\is_file($newBasePath . '/index.jpg')) { + $type = 'img'; + $newWebPath .= '/index.jpg'; + } else { + $files = \scandir($newBasePath); + if ($files !== false) { + foreach ($files as $file) { + if ($file === '.' || $file === '..') { + continue; + } + + $newWebPath .= '/' . $file; + + if (\stripos($file, '.jpg') !== false + || \stripos($file, '.jpeg') !== false + || \stripos($file, '.png') !== false + || \stripos($file, '.gif') !== false + ) { + $type = 'img'; + break; + } elseif (\stripos($file, '.pdf') !== false) { + $type = 'pdf'; + break; + } + } + } + } + } + + if ($old !== null) { + $oldBasePath = __DIR__ . '/../../Files/' . $resource->path . '/' . $old->versionPath; + $oldWebPath = 'Modules/OnlineResourceWatcher/Files/' . $resource->path . '/' . $old->versionPath; + + if (\is_file($oldBasePath . '/index.jpg')) { + $type = 'img'; + $oldWebPath .= '/index.jpg'; + } else { + $files = \scandir($oldBasePath); + if ($files !== false) { + foreach ($files as $file) { + if ($file === '.' || $file === '..') { + continue; + } + + $oldWebPath .= '/' . $file; + + if (\stripos($file, '.jpg') !== false + || \stripos($file, '.jpeg') !== false + || \stripos($file, '.png') !== false + || \stripos($file, '.gif') !== false + ) { + $type = 'img'; + break; + } elseif (\stripos($file, '.pdf') !== false) { + $type = 'pdf'; + break; + } + } + } + } + } + ?> + + +
+
+
+ +
+
+ <?= $this->printHtml($resource->title); ?> +
+ <?= $this->printHtml($resource->title); ?> +
+ +
+
+
+ + +
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+ + +
\ No newline at end of file diff --git a/Theme/Backend/resource-single.tpl.php b/Theme/Backend/resource-single.tpl.php index c009cda..ca845d8 100755 --- a/Theme/Backend/resource-single.tpl.php +++ b/Theme/Backend/resource-single.tpl.php @@ -78,61 +78,4 @@ $reports = $resource->reports; -
-
- checkedAt !== null) : ?> -
-
- path . '/' . $resource->lastVersionPath; - $path = ''; - $webPath = 'Modules/OnlineResourceWatcher/Files/' . $resource->path . '/' . $resource->lastVersionPath; - - if (\is_file($basePath . '/index.jpg')) { - $type = 'img'; - $path = $basePath . '/index.jpg'; - $webPath .= '/index.jpg'; - } else { - $files = \scandir($basePath); - - if ($files !== false) { - foreach ($files as $file) { - if ($file === '.' || $files === '..') { - continue; - } - - $path = $basePath . '/' . $file; - $webPath .= '/' . $file; - - if (\stripos($file, '.jpg') !== false - || \stripos($file, '.jpeg') !== false - || \stripos($file, '.png') !== false - || \stripos($file, '.gif') !== false - ) { - $type = 'img'; - break; - } elseif (\stripos($file, '.pdf') !== false) { - $type = 'pdf'; - break; - } - } - } - } - ?> - - status !== ReportStatus::DOWNLOAD_ERROR) : ?> - - <?= $this->printHtml($resource->title); ?> - - - - - - -
-
- -
-
+exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']); $db->exec('CREATE DATABASE IF NOT EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']); $db = null; - } catch (\Throwable $t) { + } catch (\Throwable $_) { echo "\nCouldn't connect to MYSQL DB\n"; } } @@ -317,7 +317,7 @@ if (\defined('RESET') && RESET === '1') { $db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['postgresql']['admin']['database']); $db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['postgresql']['admin']['database']); $db = null; - } catch (\Throwable $t) { + } catch (\Throwable $_) { echo "\nCouldn't connect to POSTGRESQL DB\n"; } } @@ -332,7 +332,7 @@ if (\defined('RESET') && RESET === '1') { $db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['mssql']['admin']['database']); $db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['mssql']['admin']['database']); $db = null; - } catch (\Throwable $t) { + } catch (\Throwable $_) { echo "\nCouldn't connect to MSSQL DB\n"; } }