update.php 18.1 KB
Newer Older
MatrixCrawler's avatar
MatrixCrawler committed
1
<?php
2 3 4 5 6 7

/**
 * Pull pod info.
 */

declare(strict_types=1);
8

dmorley's avatar
dmorley committed
9 10 11
require_once __DIR__ . '/../boot.php';

if (!is_cli()) {
dmorley's avatar
dmorley committed
12 13 14 15 16
    $referer = ($_SERVER['HTTP_REFERER'] ? parse_url($_SERVER['HTTP_REFERER'])['host'] : '');
    if ($referer !== $_SERVER['SERVER_NAME']) {
        header('HTTP/1.0 403 Forbidden');
        exit;
    }
17 18
}

19
use Carbon\Carbon;
dmorley's avatar
dmorley committed
20
use GeoIp2\Database\Reader;
dmorley's avatar
dmorley committed
21
use Longman\IPTools\Ip;
22 23
use Poduptime\PodStatus;
use RedBeanPHP\R;
noplanman's avatar
noplanman committed
24

dmorley's avatar
dmorley committed
25 26 27 28 29 30
if (!is_connected()) {
    die('no internet');
}

$time_start = microtime(true);

31 32
$debug    = isset($_GET['debug']) || (isset($argv) && in_array('debug', $argv, true));
$sqldebug = isset($_GET['sqldebug']) || (isset($argv) && in_array('sqldebug', $argv, true));
dmorley's avatar
dmorley committed
33
$develop  = isset($_GET['develop']) || (isset($argv) && in_array('develop', $argv, true));
34
$write    = !(isset($_GET['nowrite']) || (isset($argv) && in_array('nowrite', $argv, true)));
dmorley's avatar
dmorley committed
35
$newline  = is_cli() ? "\n\n" : '<br><br>';
David Morley's avatar
David Morley committed
36

noplanman's avatar
noplanman committed
37 38 39
$_domain = $_GET['domain'] ?? null;

// Must have a domain, except if called from CLI.
dmorley's avatar
dmorley committed
40
$_domain || is_cli() || die('No valid input');
noplanman's avatar
noplanman committed
41

noplanman's avatar
noplanman committed
42
$sqldebug && R::fancyDebug(true);
noplanman's avatar
noplanman committed
43 44

try {
dmorley's avatar
dmorley committed
45
    // Setup GeoIP Database
noplanman's avatar
noplanman committed
46
    $reader = new Reader(c('geoip2db'));
dmorley's avatar
dmorley committed
47

48
    $sql = '
dmorley's avatar
dmorley committed
49
        SELECT domain, score, date_created, weight, podmin_notify, podmin_notify_level, email, masterversion, shortversion, status, detectedlanguage
50 51 52 53
        FROM pods
    ';

    $pods = [];
dmorley's avatar
dmorley committed
54

55 56 57
    if ($_domain) {
        $sql  .= ' WHERE domain = ?';
        $pods = R::getAll($sql, [$_domain]);
dmorley's avatar
dmorley committed
58
    } elseif (is_cli()) {
dmorley's avatar
dmorley committed
59 60 61
        if (getMeta('pods_updating')) {
            die('already running');
        }
dmorley's avatar
dmorley committed
62
        updateMeta('pods_updating', true);
dmorley's avatar
dmorley committed
63 64 65 66 67 68 69 70

        if (isset($argv) && in_array('Check_System_Deleted', $argv, true)) {
            $sql  .= ' WHERE status = ? ORDER BY id';
            $pods = R::getAll($sql, [PodStatus::SYSTEM_DELETED]);
        } else {
            $sql  .= ' WHERE status < ? ORDER BY id';
            $pods = R::getAll($sql, [PodStatus::PAUSED]);
        }
71
    }
noplanman's avatar
noplanman committed
72
} catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
73
    debug('Error in SQL query: ' . sprintf($e->getMessage()));
dmorley's avatar
dmorley committed
74
} catch (\MaxMind\Db\Reader\InvalidDatabaseException $e) {
dmorley's avatar
dmorley committed
75
    debug('Invalid GeoIP database: ' . sprintf($e->getMessage()));
dmorley's avatar
cleanup  
dmorley committed
76
}
noplanman's avatar
noplanman committed
77 78

foreach ($pods as $pod) {
dmorley's avatar
dmorley committed
79 80 81 82 83 84 85 86 87 88 89 90
    $domain          = $pod['domain'];
    $score           = (int) $pod['score'];
    $dbscore         = $score;
    $dateadded       = $pod['date_created'];
    $weight          = $pod['weight'];
    $email           = $pod['email'];
    $notify          = $pod['podmin_notify'];
    $notify_level    = $pod['podmin_notify_level'];
    $masterv         = $pod['masterversion'];
    $shortv          = $pod['shortversion'];
    $dbstatus        = $pod['status'];
    $language        = $pod['detectedlanguage'];
91 92 93

    try {
        $ratings = R::getAll('
dmorley's avatar
dmorley committed
94 95
            SELECT rating
            FROM ratingcomments
96 97 98
            WHERE domain = ?
        ', [$domain]);
    } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
99
        debug('Error in SQL query: ' . sprintf($e->getMessage()));
100
    }
101

dmorley's avatar
dmorley committed
102
    debug('Domain', $domain);
noplanman's avatar
noplanman committed
103

dmorley's avatar
dmorley committed
104
    $user_rating = 0;
dmorley's avatar
dmorley committed
105

dmorley's avatar
dmorley committed
106 107
    if ($user_ratings = array_column($ratings, 'rating')) {
        $user_rating = round(array_sum($user_ratings) / count($user_ratings), 2);
108
    }
109

dmorley's avatar
dmorley committed
110
    $nodeinfo_meta = curl("https://{$domain}/.well-known/nodeinfo");
noplanman's avatar
noplanman committed
111

112 113
    // Default link to fetch node info.
    $nodeinfo_url = "https://{$domain}/nodeinfo/1.0";
114

dmorley's avatar
dmorley committed
115
    if ($info = json_decode($nodeinfo_meta['body'] ?: '', true)) {
dmorley's avatar
dmorley committed
116 117 118 119 120
        if (count($info['links'], COUNT_RECURSIVE) === 2) {
            $nodeinfo_url = $info['links']['href'];
        } else {
            $nodeinfo_url = max($info['links'])['href'];
        }
121 122
    }

dmorley's avatar
dmorley committed
123
    debug('Nodeinfo link', $nodeinfo_url);
dmorley's avatar
dmorley committed
124

dmorley's avatar
dmorley committed
125
    $nodeinfo       = curl($nodeinfo_url);
126 127 128
    $outputssl      = $nodeinfo['body'];
    $outputsslerror = $nodeinfo['error'];
    $info           = $nodeinfo['info'];
dmorley's avatar
dmorley committed
129
    $httpcode       = $nodeinfo['code'];
130 131
    $conntime       = $nodeinfo['conntime'];
    $nstime         = $nodeinfo['nstime'];
132 133 134
    $latency        = $conntime - $nstime;
    $sslexpire      = $info[0]['Expire date'] ?? null;

dmorley's avatar
dmorley committed
135 136 137 138 139 140 141
    debug('Nodeinfo output', $outputssl, true);
    debug('Nodeinfo output error', $outputsslerror, true);
    debug('Nodeinfo service http response code', $httpcode);
    debug('Cert expire date', $sslexpire);
    debug('Conntime', $conntime);
    debug('NStime', $nstime);
    debug('Latency', $latency);
142

dmorley's avatar
dmorley committed
143
    $jsonssl = json_decode($outputssl ?: '');
144 145

    if ($jsonssl !== null) {
dmorley's avatar
dmorley committed
146
        $version               = $jsonssl->software->version ?? 0;
dmorley's avatar
dmorley committed
147 148
        preg_match_all('((?:\d(.|-)?)+(\.)\d+\.*)', $version, $sversion);
        $shortversion          = $sversion[0][0] ?? '0.0.0.0';
dmorley's avatar
dmorley committed
149 150 151 152 153 154 155 156 157 158 159 160
        $signup                = ($jsonssl->openRegistrations ?? false) === true;
        $softwarename          = $jsonssl->software->name ?? 'unknown';
        $name                  = $jsonssl->metadata->nodeName ?? $softwarename;
        $total_users           = $jsonssl->usage->users->total ?? 0;
        $active_users_halfyear = $jsonssl->usage->users->activeHalfyear ?? 0;
        $active_users_monthly  = $jsonssl->usage->users->activeMonth ?? 0;
        $local_posts           = $jsonssl->usage->localPosts ?? 0;
        $comment_counts        = $jsonssl->usage->localComments ?? 0;
        $service_xmpp          = ($jsonssl->metadata->xmppChat ?? false) === true;
        if (json_last_error() === 0) {
            (!$jsonssl->software->version) || $score += 1;
            if (is_array($jsonssl->services->outbound)) {
161
                $services = json_encode($jsonssl->services->outbound);
dmorley's avatar
dmorley committed
162 163
            }
        }
164 165 166 167 168 169 170 171 172 173

        try {
            $c                   = R::dispense('checks');
            $c['domain']         = $domain;
            $c['online']         = true;
            $c['latency']        = $latency;
            $c['total_users']    = $total_users;
            $c['local_posts']    = $local_posts;
            $c['comment_counts'] = $comment_counts;
            $c['shortversion']   = $shortversion;
dmorley's avatar
dmorley committed
174
            $c['version']        = $version;
dmorley's avatar
dmorley committed
175

176 177 178 179 180 181
            if ($write) {
                R::store($c);
            } else {
                echo $c;
            }
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
182
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
183 184
        }

dmorley's avatar
dmorley committed
185
        debug('Version code', $shortversion);
dmorley's avatar
dmorley committed
186 187 188

        try {
            $masterdata = R::getRow('
dmorley's avatar
dmorley committed
189
                SELECT version, devlastcommit, releasedate, date_checked
dmorley's avatar
dmorley committed
190 191 192 193 194 195
                FROM masterversions
                WHERE software = ?
                ORDER BY id
                DESC LIMIT 1
            ', [$softwarename]);
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
196
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
dmorley's avatar
dmorley committed
197 198 199 200 201
        }

        $masterversion = ($masterdata['version'] ?? '0.0.0.0');
        $devlastcommit = ($masterdata['devlastcommit'] ?? date('Y-m-d H:i:s'));
        $releasedate   = ($masterdata['releasedate'] ?? date('Y-m-d H:i:s'));
dmorley's avatar
dmorley committed
202

dmorley's avatar
dmorley committed
203
        debug('Masterversion', $masterversion);
dmorley's avatar
dmorley committed
204

dmorley's avatar
dmorley committed
205 206 207 208
        $masterversioncheck = explode('.', $masterversion);
        $shortversioncheck  = (strpos($shortversion, '.') ? explode('.', $shortversion) : implode('.', ['0', preg_replace('/\D/', '', $shortversion), '0']));
        //this is still off with a pod with v1 as total version. cant explode that, won't have a [0] or [1] later to use either

dmorley's avatar
dmorley committed
209
        debug('Days since master code release', date_diff(new DateTime($releasedate), new DateTime())->format('%d'));
dmorley's avatar
dmorley committed
210 211 212

        try {
            $lastpodupdates = R::getRow('
dmorley's avatar
dmorley committed
213
                SELECT DISTINCT ON (version, date_checked) version, date_checked
dmorley's avatar
dmorley committed
214 215
                FROM checks
                WHERE domain = ?
dmorley's avatar
dmorley committed
216 217
                    AND version IS NOT NULL
                ORDER BY version DESC, date_checked
dmorley's avatar
dmorley committed
218 219 220
                LIMIT 1
            ', [$domain]);
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
221
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
dmorley's avatar
dmorley committed
222 223
        }

dmorley's avatar
dmorley committed
224
        $lastdatechecked = ($lastpodupdates['date_checked'] ?? date('Y-m-d H:i:s.u'));
dmorley's avatar
dmorley committed
225 226
        $devlastdays     = $devlastcommit ? date_diff(new DateTime($devlastcommit), new DateTime())->format('%a') : 30;//tmp//if no dev branch then what?

dmorley's avatar
dmorley committed
227
        debug('Dev last commit was  ', $devlastdays);
dmorley's avatar
dmorley committed
228

dmorley's avatar
dmorley committed
229 230 231 232 233
        $updategap = date_diff(new DateTime($lastdatechecked), new DateTime($devlastcommit))->format('%a');

        if (strpos($version, 'dev') !== false || strpos($version, 'rc') !== false || $shortversioncheck > $masterversioncheck) {
            //tmp//if pod is on the development branch - see when you last updated your pod and when the last commit was made to dev branch - if the repo is active and your not updating every 120 days why are you on dev branch?

dmorley's avatar
dmorley committed
234 235
            if ($updategap + $devlastdays > 200) {
                debug('Outdated More than 200 days', 'Yes');
dmorley's avatar
dmorley committed
236

dmorley's avatar
dmorley committed
237
                $podminhelp = 'Your code base seems too out of date to be used. Last time you updated was ' . $updategap;
dmorley's avatar
dmorley committed
238 239 240 241
                $score -= 2;
            }
        } elseif (($masterversioncheck[1] - $shortversioncheck[1]) > 1) {
            ///tmp/If pod is two versions off AND it's been more than 60 days since that release came out AND your on the master production branch
dmorley's avatar
dmorley committed
242

dmorley's avatar
dmorley committed
243
            debug('Outdated second decimal > 1', 'Yes');
dmorley's avatar
dmorley committed
244

dmorley's avatar
dmorley committed
245 246
            $score     -= 2;
            $updategap = date_diff(new DateTime($lastdatechecked), new DateTime($releasedate))->format('%a');
dmorley's avatar
dmorley committed
247
            $podminhelp = 'Your code base seems too out of date to be used. Current version is ' . $masterversion . ' and you are running ' . $shortversion;
dmorley's avatar
dmorley committed
248 249
        } elseif ($updategap - date_diff(new DateTime($releasedate), new DateTime())->format('%a') > 200) {
            debug('Outdated more than 200 days since x ', 'Yes');
dmorley's avatar
dmorley committed
250

dmorley's avatar
dmorley committed
251 252
            $score     -= 2;
            $updategap = date_diff(new DateTime($lastdatechecked), new DateTime($releasedate))->format('%a');
dmorley's avatar
dmorley committed
253
            $podminhelp = 'Your code base seems too out of date to be used. Last time you updated was ' . $updategap;
dmorley's avatar
dmorley committed
254 255 256 257
        } else {
            $updategap = date_diff(new DateTime($lastdatechecked), new DateTime($releasedate))->format('%a');
        }

dmorley's avatar
dmorley committed
258
        debug('Pod code was updated after ', $updategap);
dmorley's avatar
dmorley committed
259

260 261 262
        $status = PodStatus::UP;
    }

dmorley's avatar
dmorley committed
263 264
    $langhours  = Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('languages_updated', 'date_created'))->diffInHours();

265 266
    // Default to the already saved language.
    $detectedlanguage = $language;
dmorley's avatar
dmorley committed
267
    $languagesupdated = false;
dmorley's avatar
dmorley committed
268

269 270
    $language_snippet = getWebsiteLanguageSnippetFromUrl("https://{$domain}/");
    if (!$language_snippet) {
dmorley's avatar
dmorley committed
271
        $detectedlanguage = null;
272
        --$score;
dmorley's avatar
dmorley committed
273
        $podminhelp = 'Unable to render the html on your main page https://' . $domain;
dmorley's avatar
dmorley committed
274
    } elseif ($debug || $langhours > 24) {
275
        $detectedlanguage = detectWebsiteLanguageFromSnippet($language_snippet);
dmorley's avatar
dmorley committed
276
        $languagesupdated = true;
dmorley's avatar
dmorley committed
277 278
    }

dmorley's avatar
dmorley committed
279
    debug('Detected Language', $detectedlanguage);
dmorley's avatar
dmorley committed
280

281
    if (!$jsonssl || !$language_snippet) {
dmorley's avatar
dmorley committed
282
        debug('Connection', 'Can not connect to pod');
283 284 285 286 287 288 289 290 291 292 293 294 295

        try {
            $c            = R::dispense('checks');
            $c['domain']  = $domain;
            $c['online']  = false;
            $c['error']   = $outputsslerror;
            $c['latency'] = $latency;
            if ($write) {
                R::store($c);
            } else {
                echo $c;
            }
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
296
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
297 298
        }

299
        --$score;
300 301 302
        $status = PodStatus::DOWN;
    }

dmorley's avatar
dmorley committed
303
    debug('Signup Open', $signup);
304

noplanman's avatar
noplanman committed
305
    $dnsserver = c('dnsserver') ?: '1.1.1.1';
306 307 308 309 310 311 312
    $delv      = new NPM\Xec\Command("delv @{$dnsserver} {$domain}");
    $delv->throwExceptionOnError(false);

    $ip         = '';
    $iplookupv4 = explode(PHP_EOL, trim($delv->execute([], null, 15)->stdout));
    $dnssec     = in_array('; fully validated', $iplookupv4, true) ?? false;
    $getaonly   = array_values(preg_grep('/\s+IN\s+A\s+.*/', $iplookupv4));
dmorley's avatar
dmorley committed
313

314 315 316
    if ($getaonly) {
        preg_match('/A\s(.*)/', $getaonly[0], $aversion);
        $ip = trim($aversion[1]) ?? '';
noplanman's avatar
noplanman committed
317
    }
318 319 320 321
    $ip || $score -= 2;

    $iplookupv6 = explode(PHP_EOL, trim($delv->execute(['AAAA'], null, 15)->stdout));
    $ipv6       = (bool) preg_grep('/\s+IN\s+AAAA\s+.*/', $iplookupv6);
322

dmorley's avatar
dmorley committed
323 324
    debug('IPv4', $ip);
    debug('IPv6', $ipv6);
noplanman's avatar
noplanman committed
325

dmorley's avatar
dmorley committed
326 327
    // todo: Temporary workaround (see https://github.com/akalongman/php-ip-tools/issues/8)
    if (Ip::isValid($ip) && Ip::isRemote($ip)) {
dmorley's avatar
dmorley committed
328 329 330 331 332 333 334
        $geo         = $reader->city($ip);
        $countryname = ($geo->country->name ?? null) ?: null;
        $country     = ($geo->country->isoCode ?? null) ?: null;
        $city        = ($geo->city->name ?? null) ?: null;
        $state       = ($geo->mostSpecificSubdivision->name ?? null) ?: null;
        $lat         = ($geo->location->latitude ?? null) ?: 0;
        $long        = ($geo->location->longitude ?? null) ?: 0;
335

dmorley's avatar
dmorley committed
336
        debug('Location', json_encode($geo->raw), true);
dmorley's avatar
dmorley committed
337
    }
dmorley's avatar
dmorley committed
338

339 340 341 342 343
    $statslastdate = date('Y-m-d H:i:s');

    $diff   = (new DateTime())->diff(new DateTime($dateadded));
    $months = $diff->m + ($diff->y * 12);
    $days   = $diff->days;
noplanman's avatar
noplanman committed
344 345

    try {
346 347 348 349 350 351 352 353 354 355
        $checks = R::getRow('
            SELECT
                round(avg(latency) * 1000) AS latency,
                round(avg(online::INT) * 100, 2) AS online
            FROM checks
            WHERE domain = ?
        ', [$domain]);

        $avglatency = $checks['latency'] ?? 0;
        $uptime     = $checks['online'] ?? 0;
noplanman's avatar
noplanman committed
356
    } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
357
        debug('Error in SQL query: ' . sprintf($e->getMessage()));
noplanman's avatar
noplanman committed
358 359
    }

dmorley's avatar
dmorley committed
360
    debug('Uptime', $uptime);
noplanman's avatar
noplanman committed
361

dmorley's avatar
dmorley committed
362
    if ($score == ($notify_level - 1) && $notify && !$develop && $dbscore == $notify_level) {
363
        $to      = $email;
noplanman's avatar
noplanman committed
364
        $headers = ['From: ' . c('adminemail'), 'Bcc: ' . c('adminemail')];
365
        $subject = 'Monitoring notice from poduptime';
dmorley's avatar
dmorley committed
366 367 368 369 370 371 372 373 374
        $message = 'Notice for ' . $domain . '. Your score is ' . $score . ' and your pod will fall off the list soon. HTTP response of ' . $httpcode . '. ';

        if ($outputsslerror) {
            $message .= 'SSL Error ' . $outputsslerror;
        }

        if ($podminhelp) {
            $message .= ' ' . $podminhelp;
        }
375
        @mail($to, $subject, $message, implode("\r\n", $headers));
dmorley's avatar
dmorley committed
376

dmorley's avatar
dmorley committed
377 378
        debug('Mail Message body', $message);
        debug('Mail Notice', 'sent to ' . $email);
dmorley's avatar
dmorley committed
379
    }
dmorley's avatar
dmorley committed
380

dmorley's avatar
dmorley committed
381 382
    $weightedscore = ($uptime + $score - (10 - $weight)) / 2;

383 384
    if ($score > 100) {
        $score = 100;
dmorley's avatar
dmorley committed
385 386
    } elseif ($score < 1) {
        $weightedscore = 0;
dmorley's avatar
dmorley committed
387
    } elseif ($score < -6000) {
dmorley's avatar
dmorley committed
388
        $status = PodStatus::SYSTEM_DELETED;
dmorley's avatar
dmorley committed
389
    }
dmorley's avatar
dmorley committed
390

dmorley's avatar
dmorley committed
391 392
    debug('Score', $score);
    debug('Weighted Score', $weightedscore);
noplanman's avatar
noplanman committed
393

394 395 396 397 398 399 400 401
    try {
        $p                     = R::findOne('pods', 'domain = ?', [$domain]);
        $p['ip']               = $ip;
        $p['ipv6']             = $ipv6;
        $p['daysmonitored']    = $days;
        $p['monthsmonitored']  = $months;
        $p['uptime_alltime']   = $uptime;
        $p['status']           = $status;
dmorley's avatar
dmorley committed
402 403
        $p['date_laststats']   = date('Y-m-d H:i:s.u');
        $p['date_updated']     = $lastpodupdates['date_checked'];
404 405 406 407 408 409 410 411 412 413 414 415 416 417
        $p['latency']          = $avglatency;
        $p['score']            = $score;
        $p['country']          = $country;
        $p['countryname']      = $countryname;
        $p['city']             = $city;
        $p['state']            = $state;
        $p['lat']              = $lat;
        $p['long']             = $long;
        $p['userrating']       = $user_rating;
        $p['masterversion']    = $masterversion;
        $p['weightedscore']    = $weightedscore;
        $p['sslvalid']         = $outputsslerror;
        $p['dnssec']           = $dnssec;
        $p['sslexpire']        = $sslexpire;
dmorley's avatar
dmorley committed
418
        if ($dbstatus == PodStatus::UP && $status == PodStatus::UP) {
419 420 421 422 423 424 425
            $p['shortversion']          = $shortversion;
            $p['signup']                = $signup;
            $p['total_users']           = $total_users;
            $p['active_users_halfyear'] = $active_users_halfyear;
            $p['active_users_monthly']  = $active_users_monthly;
            $p['local_posts']           = $local_posts;
            $p['name']                  = $name;
dmorley's avatar
dmorley committed
426
            $p['detectedlanguage']      = $detectedlanguage;
427 428
            $p['comment_counts']        = $comment_counts;
            $p['service_xmpp']          = $service_xmpp;
429
            $p['services']              = $services;
430 431 432 433 434 435
            $p['softwarename']          = $softwarename;
        }

        if ($write) {
            R::store($p);
        } else {
dmorley's avatar
dmorley committed
436 437
            echo 'Data not saved, testing only';
            echo $newline;
438 439
        }
    } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
440 441 442 443 444 445
        debug('Error in SQL query: ' . sprintf($e->getMessage()));
    }

    debug('Success', $domain);
}

dmorley's avatar
dmorley committed
446
//if a full run
dmorley's avatar
dmorley committed
447
if (!$_domain) {
dmorley's avatar
dmorley committed
448 449 450 451 452 453 454 455
    updateMeta('pods_updated');
    $time_end = microtime(true);
    $execution_time = ($time_end - $time_start) / 60;
    updateMeta('pods_update_runtime', round($execution_time));
    if ($languagesupdated) {
        updateMeta('languages_updated');
    }

dmorley's avatar
dmorley committed
456 457 458 459 460 461 462 463 464
    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('masterversions_updated', 'date_created'))->diffInHours() > 12) {
        require __DIR__ . '/update-masterversions.php';
    }

    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('statstable_updated', 'date_created'))->diffInHours() > 23) {
        require __DIR__ . '/update-monthly-stats.php';
    }

    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('cacert_updated', 'date_created'))->diffInHours() > 40) {
dmorley's avatar
dmorley committed
465
        copy('https://curl.haxx.se/ca/cacert.pem', c('cafullpath'));
dmorley's avatar
dmorley committed
466 467 468
        updateMeta('cacert_updated');
    }

dmorley's avatar
dmorley committed
469 470 471 472
    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('sitemap_updated', 'date_created'))->diffInHours() > 2) {
        require __DIR__ . '/update-sitemap.php';
    }

dmorley's avatar
dmorley committed
473
    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('geoip_updated', 'date_created'))->diffInHours() > 45) {
dmorley's avatar
dmorley committed
474
        copy('compress.zlib://http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz', c('geoip2db'));
dmorley's avatar
dmorley committed
475 476 477 478 479
        updateMeta('geoip_updated');
    }

    if (Carbon::createFromFormat('Y-m-d H:i:s.u', getMeta('federation_updated', 'date_created'))->diffInHours() > 9) {
        require __DIR__ . '/update-remote-data.php';
480
    }
David Morley's avatar
David Morley committed
481

dmorley's avatar
dmorley committed
482
    require __DIR__ . '/backup.php';
noplanman's avatar
noplanman committed
483

dmorley's avatar
dmorley committed
484
    updateMeta('pods_updating', false);
485
}