PHP Classes
elePHPant
Icontem

File: src/CommandLine/package_metadata.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  CMS Airship  >  src/CommandLine/package_metadata.php  >  Download  
File: src/CommandLine/package_metadata.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change: Nits, Psalm errors.
Date: 10 months ago
Size: 2,317 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Airship\Alerts\Hail\NoAPIResponse;
use
Airship\Engine\{
   
Database,
   
Hail,
   
State
};
use
ParagonIE\Halite\Asymmetric\SignaturePublicKey;
use
ParagonIE\Halite\HiddenString;
use
ParagonIE\ConstantTime\Hex;

require_once \
dirname(__DIR__).'/bootstrap.php';

/**
 * Package metadata updates.
 *
 * @param Database $db
 * @param array $updates
 * @return bool
 * @throws TypeError
 */
function processUpdates(Database $db, array $updates = []): bool
{
   
$db->beginTransaction();
    foreach (
$updates as $update) {
       
$db->update(
           
'airship_package_cache',
            [
               
'skyport_metadata' => \json_encode($update['metadata'])
            ],
            [
               
'packagetype' => $update['package']['type'],
               
'supplier' => $update['package']['supplier'],
               
'name' => $update['package']['name']
            ]
        );
    }
    return
$db->commit();
}

$channels = \Airship\loadJSON(
   
ROOT . '/config/channels.json'
);
$state = State::instance();

$lastScan = \file_get_contents(
   
ROOT . '/tmp/last_metadata_update.txt'
);
if (
$lastScan === false) {
   
$lastScan = '1970-01-01\T00:00:00';
}
$db = \Airship\get_database();

if (
$state->hail instanceof Hail) {
    foreach (
$channels as $identifier => $channel) {
       
$publicKey = new SignaturePublicKey(
            new
HiddenString(
               
Hex::decode($channel['public_key'])
            )
        );

        foreach (
$channel['urls'] as $url) {
            try {
               
$updates = $state->hail->postSignedJSON(
                   
$url . '/packageMeta',
                   
$publicKey,
                    [
                       
'since' => $lastScan
                   
]
                );
                if (
$updates['status'] === 'success') {
                    if (\
processUpdates($db, ['packageMetadata'])) {
                       
file_put_contents(
                           
ROOT . '/tmp/last_metadata_update.txt',
                            (new \
DateTime())
                                ->
format(\AIRSHIP_DATE_FORMAT)
                        );
                        exit(
0);
                    }
                }
            } catch (
NoAPIResponse $ex) {
            }
        }
    }
}
exit(
255);