summaryrefslogtreecommitdiff
path: root/includes/jobs/UpdateContactMethods.php
blob: 2e9d27ff1187e8c572069fd8944f6100d04735aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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));