2020-01-14 22:25:43 +01:00
< ? php
2018-02-03 15:44:40 +01:00
2020-01-19 13:16:40 +01:00
class UpdatesLog implements IWebsiteModule
2018-02-03 15:44:40 +01:00
{
2020-01-15 00:45:30 +01:00
/** @var Website */
private $site ;
/** @var array */
private $staticData ;
public function __construct ( Website $site )
{
$this -> site = $site ;
$this -> load ();
}
private function load ()
{
2020-01-18 12:50:46 +01:00
$all = require ( __DIR__ . '/../../statics/updates/__all.php' );
2020-01-15 00:45:30 +01:00
$this -> staticData = array_map ( function ( $a ){ return self :: readSingle ( $a );}, $all );
}
private static function readSingle ( $d )
{
return $d ;
}
public function listUpdateData ()
{
return $this -> staticData ;
}
public function insert ( $name , $version )
2018-02-03 15:44:40 +01:00
{
$ip = get_client_ip ();
2020-01-15 02:50:23 +01:00
$ippath = ( __DIR__ . '/../../dynamic/self_ip_address.auto.cfg' );
2018-02-03 15:44:40 +01:00
$self_ip = file_exists ( $ippath ) ? file_get_contents ( $ippath ) : 'N/A' ;
if ( $self_ip === $ip ) $ip = " self " ;
2020-01-15 02:50:23 +01:00
$this -> site -> modules -> Database () -> sql_exec_prep ( " INSERT INTO updateslog (programname, ip, version, date) VALUES (:pn, :ip, :vn, NOW()) " ,
2018-02-03 15:44:40 +01:00
[
[ ':pn' , $name , PDO :: PARAM_STR ],
[ ':ip' , $ip , PDO :: PARAM_STR ],
[ ':vn' , $version , PDO :: PARAM_STR ],
]);
}
2020-01-15 00:45:30 +01:00
public function listProgramsInformation ()
2018-02-03 15:44:40 +01:00
{
2020-01-15 02:50:23 +01:00
return $this -> site -> modules -> Database () -> sql_query_assoc ( 'SELECT programname AS name, Count(*) as count_total, MAX(date) AS last_query, (SELECT COUNT(*) FROM updateslog AS u1 WHERE u1.programname=u0.programname AND NOW() - INTERVAL 7 DAY < u1.date) AS count_week FROM updateslog AS u0 GROUP BY programname' );
2018-02-03 15:44:40 +01:00
}
2020-01-15 00:45:30 +01:00
public function getEntries ( $name , $limit )
2018-02-03 15:44:40 +01:00
{
2020-01-15 02:50:23 +01:00
return $this -> site -> modules -> Database () -> sql_query_assoc_prep ( 'SELECT * FROM updateslog WHERE programname = :pn ORDER BY date DESC LIMIT :lt' ,
2018-02-03 15:44:40 +01:00
[
[ ':pn' , $name , PDO :: PARAM_STR ],
[ ':lt' , $limit , PDO :: PARAM_INT ],
]);
}
2020-01-19 13:16:40 +01:00
public function checkConsistency ()
{
$warn = null ;
$this -> load ();
foreach ( $this -> staticData as $name => $data )
{
if ( ! key_exists ( 'version' , $data )) return [ 'result' => 'err' , 'message' => 'Missing value [version]' ];
if ( ! key_exists ( 'url' , $data )) return [ 'result' => 'err' , 'message' => 'Missing value [url]' ];
}
if ( $warn != null ) return $warn ;
return [ 'result' => 'ok' , 'message' => '' ];
}
2018-02-03 15:44:40 +01:00
}