You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB
PHP

<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// Run this script via cron every 15 minutes to send reminder notifications.
require __DIR__ . '/settings.php';
require __DIR__ . '/vendor/autoload.php';
$libs = glob(__DIR__ . "/lib/*.lib.php");
foreach ($libs as $lib) {
require_once $lib;
}
$Strings = new Strings(LANGUAGE);
date_default_timezone_set(TIMEZONE);
use Medoo\Medoo;
$database;
try {
$database = new Medoo([
'database_type' => DB_TYPE,
'database_name' => DB_NAME,
'server' => DB_SERVER,
'username' => DB_USER,
'password' => DB_PASS,
'charset' => DB_CHARSET
]);
} catch (Exception $ex) {
echo "Database error: $ex\n";
exit(1);
}
$reminders = $database->select('reminders', [
'reminderid (id)',
'noteid',
'when'
], [
'when[<]' => date("Y-m-d H:i:s", strtotime("now + 15 minutes"))
]);
echo "Start sending " . count($reminders) . " reminder notifications...\n";
$deleteids = [];
$linelimit = 3;
foreach ($reminders as $r) {
$note = Note::loadNote($r['noteid']);
$lines = explode("\n", $note->getText(), $linelimit);
if (count($lines) == $linelimit) {
array_pop($lines);
$lines[] = $Strings->build("Open {app} to read more", ["app" => SITE_TITLE], false);
}
$content = implode("\n", $lines);
Notifications::add($note->getOwner(), $Strings->get("Note Reminder", false), $content);
$deleteids[] = $r['id'];
}
echo "Sent " . count($reminders) . " notifications.\n";
$deleted = $database->delete('reminders', ['reminderid' => $deleteids]);
echo "Outdated reminder cleanup complete: " . $deleted->rowCount() . " records removed.\n";