2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2013-05-18 12:52:11 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-01-19 18:57:02 +00:00
|
|
|
use function MicroHTML\{rawHTML};
|
|
|
|
|
2019-09-29 16:37:03 +00:00
|
|
|
class SourceHistory extends Extension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var SourceHistoryTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-02-04 00:46:36 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// in before source are actually set, so that "get current source" works
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 40;
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_int("history_limit", -1);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
if ($event->page_matches("source_history/revert", method: "POST", permission: Permissions::EDIT_IMAGE_TAG)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
// this is a request to revert to a previous version of the source
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->process_revert_request((int)$event->req_POST('revert'));
|
|
|
|
} elseif ($event->page_matches("source_history/bulk_revert", method: "POST", permission: Permissions::BULK_EDIT_IMAGE_TAG)) {
|
|
|
|
$this->process_bulk_revert_request();
|
2024-02-11 11:34:09 +00:00
|
|
|
} elseif ($event->page_matches("source_history/all/{page}")) {
|
|
|
|
$page_id = $event->get_iarg('page');
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_global_page($page, $this->get_global_source_history($page_id), $page_id);
|
2024-02-11 11:34:09 +00:00
|
|
|
} elseif ($event->page_matches("source_history/{image_id}")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
// must be an attempt to view a source history
|
2024-02-11 11:34:09 +00:00
|
|
|
$image_id = $event->get_iarg('image_id');
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_history_page($page, $image_id, $this->get_source_history_from_id($image_id));
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onRobotsBuilding(RobotsBuildingEvent $event): void
|
2023-03-30 19:43:14 +00:00
|
|
|
{
|
|
|
|
$event->add_disallow("source_history");
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-02-10 18:35:55 +00:00
|
|
|
$event->add_button("View Source History", "source_history/{$event->image->id}", 20);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// disk space is cheaper than manually rebuilding history,
|
|
|
|
// so let's default to -1 and the user can go advanced if
|
|
|
|
// they /really/ want to
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Source History");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("Limit to ");
|
|
|
|
$sb->add_int_option("history_limit");
|
|
|
|
$sb->add_label(" entires per image");
|
|
|
|
$sb->add_label("<br>(-1 for unlimited)");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSourceSet(SourceSetEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->add_source_history($event->image, $event->source);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent === "system") {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::BULK_EDIT_IMAGE_TAG)) {
|
|
|
|
$event->add_nav_link("source_history", new Link('source_history/all/1'), "Source Changes", NavLink::is_active(["source_history"]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BULK_EDIT_IMAGE_TAG)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Source Changes", make_link("source_history/all/1"));
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_source_history_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("source_histories", "
|
2013-05-18 12:52:11 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
image_id INTEGER NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
|
|
|
user_ip SCORE_INET NOT NULL,
|
|
|
|
source TEXT NOT NULL,
|
2019-11-03 19:25:51 +00:00
|
|
|
date_set TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2013-05-18 12:52:11 +00:00
|
|
|
FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX source_histories_image_id_idx ON source_histories(image_id)", []);
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_source_history_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_source_history_version") == 1) {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("ALTER TABLE source_histories ADD COLUMN user_id INTEGER NOT NULL");
|
|
|
|
$database->execute("ALTER TABLE source_histories ADD COLUMN date_set DATETIME NOT NULL");
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_source_history_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_source_history_version") == 2) {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("ALTER TABLE source_histories ADD COLUMN user_ip CHAR(15) NOT NULL");
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_source_history_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when a revert request is received.
|
|
|
|
*/
|
2024-01-20 14:10:59 +00:00
|
|
|
private function process_revert_request(int $revert_id): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
|
|
|
|
// check for the nothing case
|
|
|
|
if ($revert_id < 1) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link());
|
|
|
|
return;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// lets get this revert id assuming it exists
|
|
|
|
$result = $this->get_source_history_from_revert($revert_id);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($result)) {
|
|
|
|
// there is no history entry with that id so either the image was deleted
|
|
|
|
// while the user was viewing the history, someone is playing with form
|
|
|
|
// variables or we have messed up in code somewhere.
|
|
|
|
/* calling die() is probably not a good idea, we should throw an Exception */
|
|
|
|
die("Error: No source history with specified id was found.");
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// lets get the values out of the result
|
|
|
|
//$stored_result_id = $result['id'];
|
2020-04-12 11:42:37 +00:00
|
|
|
$stored_image_id = (int)$result['image_id'];
|
2019-05-28 16:59:38 +00:00
|
|
|
$stored_source = $result['source'];
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2020-10-09 12:47:42 +00:00
|
|
|
log_debug("source_history", 'Reverting source of >>'.$stored_image_id.' to ['.$stored_source.']');
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$image = Image::by_id($stored_image_id);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (is_null($image)) {
|
|
|
|
die('Error: No image with the id ('.$stored_image_id.') was found. Perhaps the image was deleted while processing this request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// all should be ok so we can revert by firing the SetUserSources event.
|
|
|
|
send_event(new SourceSetEvent($image, $stored_source));
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// all should be done now so redirect the user back to the image
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link('post/view/'.$stored_image_id));
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
protected function process_bulk_revert_request(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) {
|
|
|
|
$revert_name = $_POST['revert_name'];
|
|
|
|
} else {
|
|
|
|
$revert_name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_POST['revert_ip']) && !empty($_POST['revert_ip'])) {
|
2024-01-20 20:48:47 +00:00
|
|
|
$revert_ip = filter_var_ex($_POST['revert_ip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($revert_ip === false) {
|
|
|
|
// invalid ip given.
|
|
|
|
$this->theme->display_admin_block('Invalid IP');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$revert_ip = null;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (isset($_POST['revert_date']) && !empty($_POST['revert_date'])) {
|
|
|
|
if (isValidDate($_POST['revert_date'])) {
|
|
|
|
$revert_date = addslashes($_POST['revert_date']); // addslashes is really unnecessary since we just checked if valid, but better safe.
|
|
|
|
} else {
|
|
|
|
$this->theme->display_admin_block('Invalid Date');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$revert_date = null;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-06-25 13:19:02 +00:00
|
|
|
shm_set_timeout(null); // reverting changes can take a long time, disable php's timelimit if possible.
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Call the revert function.
|
|
|
|
$this->process_revert_all_changes($revert_name, $revert_ip, $revert_date);
|
|
|
|
// output results
|
|
|
|
$this->theme->display_revert_ip_results();
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>|null
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_source_history_from_revert(int $revert_id): ?array
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
$row = $database->get_row("
|
2013-05-18 12:52:11 +00:00
|
|
|
SELECT source_histories.*, users.name
|
|
|
|
FROM source_histories
|
|
|
|
JOIN users ON source_histories.user_id = users.id
|
2023-11-11 21:49:12 +00:00
|
|
|
WHERE source_histories.id = :id", ["id" => $revert_id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
return ($row ? $row : null);
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_source_history_from_id(int $image_id): array
|
|
|
|
{
|
|
|
|
global $database;
|
2019-11-27 11:22:46 +00:00
|
|
|
return $database->get_all(
|
2019-05-28 16:59:38 +00:00
|
|
|
"
|
2013-05-18 12:52:11 +00:00
|
|
|
SELECT source_histories.*, users.name
|
|
|
|
FROM source_histories
|
|
|
|
JOIN users ON source_histories.user_id = users.id
|
2019-11-27 11:22:46 +00:00
|
|
|
WHERE image_id = :image_id
|
2013-05-18 12:52:11 +00:00
|
|
|
ORDER BY source_histories.id DESC",
|
2023-11-11 21:49:12 +00:00
|
|
|
["image_id" => $image_id]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_global_source_history(int $page_id): array
|
|
|
|
{
|
|
|
|
global $database;
|
2019-11-27 11:22:46 +00:00
|
|
|
return $database->get_all("
|
2013-05-18 12:52:11 +00:00
|
|
|
SELECT source_histories.*, users.name
|
|
|
|
FROM source_histories
|
|
|
|
JOIN users ON source_histories.user_id = users.id
|
|
|
|
ORDER BY source_histories.id DESC
|
|
|
|
LIMIT 100 OFFSET :offset
|
2023-11-11 21:49:12 +00:00
|
|
|
", ["offset" => ($page_id - 1) * 100]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function attempts to revert all changes by a given IP within an (optional) timeframe.
|
|
|
|
*/
|
2024-01-20 14:10:59 +00:00
|
|
|
public function process_revert_all_changes(?string $name, ?string $ip, ?string $date): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$select_code = [];
|
|
|
|
$select_args = [];
|
|
|
|
|
|
|
|
if (!is_null($name)) {
|
|
|
|
$duser = User::by_name($name);
|
2024-08-31 19:35:13 +00:00
|
|
|
$select_code[] = 'user_id = :user_id';
|
|
|
|
$select_args['user_id'] = $duser->id;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($ip)) {
|
2019-11-27 11:22:46 +00:00
|
|
|
$select_code[] = 'user_ip = :user_ip';
|
|
|
|
$select_args['user_ip'] = $ip;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:32:01 +00:00
|
|
|
if (!is_null($date)) {
|
2019-11-27 11:22:46 +00:00
|
|
|
$select_code[] = 'date_set >= :date_set';
|
|
|
|
$select_args['date_set'] = $date;
|
2019-06-09 07:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (count($select_code) == 0) {
|
|
|
|
log_error("source_history", "Tried to mass revert without any conditions");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("source_history", 'Attempting to revert edits where '.implode(" and ", $select_code)." (".implode(" / ", $select_args).")");
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Get all the images that the given IP has changed source on (within the timeframe) that were last editied by the given IP
|
|
|
|
$result = $database->get_col('
|
2013-05-18 12:52:11 +00:00
|
|
|
SELECT t1.image_id
|
|
|
|
FROM source_histories t1
|
|
|
|
LEFT JOIN source_histories t2 ON (t1.image_id = t2.image_id AND t1.date_set < t2.date_set)
|
|
|
|
WHERE t2.image_id IS NULL
|
2020-01-26 13:19:35 +00:00
|
|
|
AND t1.image_id IN ( select image_id from source_histories where '.implode(" AND ", $select_code).')
|
2013-05-18 12:52:11 +00:00
|
|
|
ORDER BY t1.image_id
|
|
|
|
', $select_args);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
foreach ($result as $image_id) {
|
|
|
|
// Get the first source history that was done before the given IP edit
|
|
|
|
$row = $database->get_row('
|
2013-05-18 12:52:11 +00:00
|
|
|
SELECT id, source
|
|
|
|
FROM source_histories
|
|
|
|
WHERE image_id='.$image_id.'
|
|
|
|
AND NOT ('.implode(" AND ", $select_code).')
|
|
|
|
ORDER BY date_set DESC LIMIT 1
|
|
|
|
', $select_args);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2020-01-26 16:38:13 +00:00
|
|
|
if (!empty($row)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$revert_id = $row['id'];
|
|
|
|
$result = $this->get_source_history_from_revert($revert_id);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($result)) {
|
|
|
|
// there is no history entry with that id so either the image was deleted
|
|
|
|
// while the user was viewing the history, or something messed up
|
|
|
|
/* calling die() is probably not a good idea, we should throw an Exception */
|
|
|
|
die('Error: No source history with specified id ('.$revert_id.') was found in the database.'."\n\n".
|
|
|
|
'Perhaps the image was deleted while processing this request.');
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// lets get the values out of the result
|
|
|
|
$stored_result_id = $result['id'];
|
|
|
|
$stored_image_id = $result['image_id'];
|
|
|
|
$stored_source = $result['source'];
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2020-10-09 12:47:42 +00:00
|
|
|
log_debug("source_history", 'Reverting source of >>'.$stored_image_id.' to ['.$stored_source.']');
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$image = Image::by_id($stored_image_id);
|
|
|
|
|
|
|
|
if (is_null($image)) {
|
|
|
|
die('Error: No image with the id ('.$stored_image_id.') was found. Perhaps the image was deleted while processing this request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// all should be ok so we can revert by firing the SetSources event.
|
|
|
|
send_event(new SourceSetEvent($image, $stored_source));
|
2020-10-09 12:47:42 +00:00
|
|
|
$this->theme->add_status('Reverted Change', 'Reverted >>'.$image_id.' to Source History #'.$stored_result_id.' ('.$row['source'].')');
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("source_history", 'Reverted '.count($result).' edits.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called just before an images source is changed.
|
|
|
|
*/
|
2024-01-20 14:10:59 +00:00
|
|
|
private function add_source_history(Image $image, string $source): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $config, $user;
|
|
|
|
|
|
|
|
$new_source = $source;
|
|
|
|
$old_source = $image->source;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($new_source == $old_source) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($old_source)) {
|
|
|
|
/* no old source, so we are probably adding the image for the first time */
|
|
|
|
log_debug("source_history", "adding new source history: [$new_source]");
|
|
|
|
} else {
|
|
|
|
log_debug("source_history", "adding source history: [$old_source] -> [$new_source]");
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$allowed = $config->get_int("history_limit");
|
|
|
|
if ($allowed == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// if the image has no history, make one with the old source
|
2023-11-11 21:49:12 +00:00
|
|
|
$entries = $database->get_one("SELECT COUNT(*) FROM source_histories WHERE image_id = :image_id", ['image_id' => $image->id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($entries == 0 && !empty($old_source)) {
|
|
|
|
$database->execute(
|
|
|
|
"
|
2013-05-18 12:52:11 +00:00
|
|
|
INSERT INTO source_histories(image_id, source, user_id, user_ip, date_set)
|
2019-11-27 11:22:46 +00:00
|
|
|
VALUES (:image_id, :source, :user_id, :user_ip, now())",
|
2023-11-11 21:49:12 +00:00
|
|
|
["image_id" => $image->id, "source" => $old_source, "user_id" => $config->get_int('anon_id'), "user_ip" => '127.0.0.1']
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
$entries++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a history entry
|
|
|
|
$database->execute(
|
|
|
|
"
|
2013-05-18 12:52:11 +00:00
|
|
|
INSERT INTO source_histories(image_id, source, user_id, user_ip, date_set)
|
2019-11-27 11:22:46 +00:00
|
|
|
VALUES (:image_id, :source, :user_id, :user_ip, now())",
|
2023-11-11 21:49:12 +00:00
|
|
|
["image_id" => $image->id, "source" => $new_source, "user_id" => $user->id, "user_ip" => get_real_ip()]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
$entries++;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// if needed remove oldest one
|
|
|
|
if ($allowed == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($entries > $allowed) {
|
|
|
|
// TODO: Make these queries better
|
|
|
|
/*
|
|
|
|
MySQL does NOT allow you to modify the same table which you use in the SELECT part.
|
|
|
|
Which means that these will probably have to stay as TWO separate queries...
|
|
|
|
|
2020-03-25 11:47:00 +00:00
|
|
|
https://dev.mysql.com/doc/refman/5.1/en/subquery-restrictions.html
|
|
|
|
https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
$min_id = $database->get_one("SELECT MIN(id) FROM source_histories WHERE image_id = :image_id", ["image_id" => $image->id]);
|
|
|
|
$database->execute("DELETE FROM source_histories WHERE id = :id", ["id" => $min_id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-18 12:52:11 +00:00
|
|
|
}
|