Added lock file usage to cron uploader to prevent concurrent runs.

Changed extension manager to allow author to be a comma-separated list.
This commit is contained in:
Matthew Barbour 2019-06-15 11:01:13 -05:00 committed by matthew
parent e940d87c22
commit 0202597f88
3 changed files with 162 additions and 120 deletions

View File

@ -1,12 +1,14 @@
<?php
/*
* Name: Cron Uploader
* Author: YaoiFox <admin@yaoifox.com>
* Authors: YaoiFox <admin@yaoifox.com>, Matthew Barbour <matthew@darkholme.net>
* Link: http://www.yaoifox.com/
* License: GPLv2
* Description: Uploads images automatically using Cron Jobs
* Documentation: Installation guide: activate this extension and navigate to www.yoursite.com/cron_upload
*/
class CronUploader extends Extension
{
// TODO: Checkbox option to only allow localhost + a list of additional IP adresses that can be set in /cron_upload
@ -51,7 +53,18 @@ class CronUploader extends Extension
// If the key is in the url, upload
if ($this->upload_key != "" && $event->get_arg(0) == $this->upload_key) {
// log in as admin
$this->set_dir();
$lockfile = fopen($this->root_dir . "/.lock", "w");
try {
if (!flock($lockfile, LOCK_EX | LOCK_NB)) {
throw new Exception("Cron upload process is already running");
}
$this->process_upload(); // Start upload
} finally {
flock($lockfile, LOCK_UN);
fclose($lockfile);
}
} elseif ($user->is_admin()) {
$this->set_dir();
$this->display_documentation();
@ -131,6 +144,8 @@ class CronUploader extends Extension
<br />This link can be found under 'Cron Command' in the board config, just remove the 'wget ' part and only the url remains.
<br />(<b>$cron_url</b>)";
$page->set_title("Cron Uploader");
$page->set_heading("Cron Uploader");
$block = new Block("Cron Uploader", $info_html, "main", 10);
$block_install = new Block("Installation Guide", $install_html, "main", 20);
@ -246,10 +261,10 @@ class CronUploader extends Extension
{
global $config, $database;
set_time_limit(0);
//set_time_limit(0);
$output_subdir = date('Ymd-His', time()) . "/";
$this->set_dir();
$this->generate_image_queue();
// Gets amount of imgs to upload
@ -310,11 +325,11 @@ class CronUploader extends Extension
$msgNumber = $this->add_upload_info("Items failed: $failed");
// Display & save upload log
$this->handle_log();
return true;
}
private function move_uploaded($path, $filename, $output_subdir, $corrupt = false)

View File

@ -22,8 +22,7 @@ class ExtensionInfo
public $ext_name;
public $name;
public $link;
public $author;
public $email;
public $authors;
public $description;
public $documentation;
public $version;
@ -39,6 +38,7 @@ class ExtensionInfo
$this->ext_name = $matches[1];
$this->name = $this->ext_name;
$this->enabled = $this->is_enabled($this->ext_name);
$this->authors = [];
for ($i = 0; $i < $number_of_lines; $i++) {
$line = $lines[$i];
@ -53,11 +53,17 @@ class ExtensionInfo
}
} elseif (preg_match("/Version: (.*)/", $line, $matches)) {
$this->version = $matches[1];
} elseif (preg_match("/Author: (.*) [<\(](.*@.*)[>\)]/", $line, $matches)) {
$this->author = $matches[1];
$this->email = $matches[2];
} elseif (preg_match("/Author: (.*)/", $line, $matches)) {
$this->author = $matches[1];
} elseif (preg_match("/Authors?: (.*)/", $line, $matches)) {
$author_list = explode(',', $matches[1]);
foreach ($author_list as $author) {
if (preg_match("/(.*) [<\(](.*@.*)[>\)]/", $author, $matches)) {
$this->authors[] = new ExtensionAuthor($matches[1], $matches[2]);
} else {
$this->authors[] = new ExtensionAuthor($author, null);
}
}
} elseif (preg_match("/(.*)Description: ?(.*)/", $line, $matches)) {
$this->description = $matches[2];
$start = $matches[1] . " ";
@ -96,6 +102,18 @@ class ExtensionInfo
}
}
class ExtensionAuthor
{
public $name;
public $email;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
}
class ExtManager extends Extension
{
public function onPageRequest(PageRequestEvent $event)

View File

@ -116,13 +116,22 @@ class ExtManagerTheme extends Themelet
public function display_doc(Page $page, ExtensionInfo $info)
{
$author = "";
if ($info->author) {
if ($info->email) {
$author = "<br><b>Author:</b> <a href=\"mailto:".html_escape($info->email)."\">".html_escape($info->author)."</a>";
if (count($info->authors) > 0) {
$author = "<br /><b>Author";
if (count($info->authors) > 1) {
$author .= "s";
}
$author .= ":</b>";
foreach ($info->authors as $auth) {
if (!empty($auth->email)) {
$author .= "<a href=\"mailto:" . html_escape($auth->email) . "\">" . html_escape($auth->name) . "</a>";
} else {
$author = "<br><b>Author:</b> ".html_escape($info->author);
$author .= html_escape($auth->name);
}
}
}
$version = ($info->version) ? "<br><b>Version:</b> " . html_escape($info->version) : "";
$link = ($info->link) ? "<br><b>Home Page:</b> <a href=\"" . html_escape($info->link) . "\">Link</a>" : "";
$doc = $info->documentation;