diff options
Diffstat (limited to 'includes/jobs')
-rw-r--r-- | includes/jobs/UpdateContactMethods.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/includes/jobs/UpdateContactMethods.php b/includes/jobs/UpdateContactMethods.php new file mode 100644 index 0000000..2e9d27f --- /dev/null +++ b/includes/jobs/UpdateContactMethods.php @@ -0,0 +1,61 @@ +<?php + +$options = json_decode($argv[1], true); +$_SERVER['DOCUMENT_ROOT'] = "/_ch"; +require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/composer/vendor/autoload.php'; +use ColorThief\ColorThief; + +echo("Loading...\n"); + +$app = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true); +$id = $options["contact"]; + +$methods = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/contactmethods.json"), true); +$contacts = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/addressbook/contacts.json"), true); +$cache = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/addressbook/saved.json"), true); + +echo("Processing for contact $id\n"); + +if (!isset($contacts[$id])) { + echo("Contact was deleted since then, aborting\n"); + die(); +} + +$contact = $contacts[$id]; + +foreach ($methods as $name => $method) { + if (isset($contacts[$id][$name])) { + echo("Contact makes use of $name, gathering information\n"); + + if (!isset($cache[$name])) $cache[$name] = []; + if (!isset($cache[$name][$contacts[$id][$name]])) $cache[$name][$contacts[$id][$name]] = [ + "data" => null, + "update" => 0 + ]; + + if (time() - $cache[$name][$contacts[$id][$name]]["update"] > 86400) { + echo(" Information is out of date, updating it\n"); + + $return = []; + $cmd = "cd \"$_SERVER[DOCUMENT_ROOT]/includes/external/addressbook\" && node \"$_SERVER[DOCUMENT_ROOT]/includes/external/addressbook/$name.js\" \"" . str_replace('"', "''", $contacts[$id][$name]) . "\""; + + exec($cmd, $return); + $json = trim(implode("\n", $return)); + + $cache[$name][$contacts[$id][$name]] = [ + "data" => json_decode($json), + "update" => time() + ]; + + echo(" Information updated\n"); + } else { + echo(" Information is up to date, not updating it\n"); + } + } else { + echo("Contact does not makes use of $name\n"); + } +} + +echo("Done!\n"); + +file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/addressbook/saved.json", json_encode($cache));
\ No newline at end of file |