147 lines
4.5 KiB
PHP
147 lines
4.5 KiB
PHP
<?php
|
|
/*
|
|
* Plugin Name: Versions Checker
|
|
* Description: Vérifie les versions des plugins et de wordpress
|
|
* Version: 1.0
|
|
* Author: Thibault Richy
|
|
* Author URI:
|
|
*/
|
|
|
|
include("logger.class.php");
|
|
|
|
// ---- create a scheduled event
|
|
// function cronstarter_activation() {
|
|
// if( !wp_next_scheduled( 'check_version_cronjob' ) ) {
|
|
// wp_schedule_event( time(), 'hourly', 'check_version_cronjob' );
|
|
// }
|
|
// }
|
|
// add_action('wp', 'cronstarter_activation');
|
|
|
|
function cronstarter_activate() {
|
|
if (! wp_next_scheduled ( 'check_version_cronjob' )) {
|
|
wp_schedule_event(time(), 'hourly', 'check_version_cronjob');
|
|
}
|
|
}
|
|
register_activation_hook(__FILE__, 'cronstarter_activate');
|
|
|
|
|
|
// ---- unschedule event upon plugin deactivation
|
|
function cronstarter_deactivate() {
|
|
$timestamp = wp_next_scheduled ('check_version_cronjob');
|
|
wp_unschedule_event ($timestamp, 'check_version_cronjob');
|
|
}
|
|
register_deactivation_hook (__FILE__, 'cronstarter_deactivate');
|
|
|
|
|
|
function checkingVersion(){
|
|
|
|
$logFile = ABSPATH ."log.txt";
|
|
$logger = new logger($logFile);
|
|
$logger->writeLog("DEBUT DE RECUP VERSIONS : ");
|
|
//Check if we have the necessary functions
|
|
if ( ! function_exists( 'get_plugins' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
|
}
|
|
if ( ! function_exists( 'plugins_api' ) ) {
|
|
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
|
|
}
|
|
if ( ! function_exists( 'get_plugin_updates' ) ) {
|
|
require_once( ABSPATH . 'wp-admin/includes/update.php' );
|
|
}
|
|
if ( ! function_exists( 'get_home_path' ) ) {
|
|
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
|
}
|
|
$data = [];
|
|
|
|
// get all plugins
|
|
$allPlugin = get_plugins();
|
|
//get all updatable plugins
|
|
$plugin_to_update = get_plugin_updates();
|
|
|
|
//date update
|
|
$date = new DateTime();
|
|
$date = $date->setTimezone(new DateTimeZone('Europe/Paris'));
|
|
$date = $date->format('d/m/Y H:i:s');
|
|
$data['updateDate'] = $date;
|
|
|
|
//get active plugins
|
|
$active_plugins = wp_get_active_and_valid_plugins();
|
|
foreach ($active_plugins as $key => $path) {
|
|
$plugin_data = get_plugin_data($path);
|
|
$logger->writeLog("Look plugin : ".$plugin_data['Name']);
|
|
$data['plugins'][$plugin_data['Name']]['Version'] = $plugin_data['Version'];
|
|
// check if one of the active plugins is updatable
|
|
if (array_key_exists (plugin_basename( $path ), $plugin_to_update)) {
|
|
$data['plugins'][$plugin_data['Name']]['Maj'] = $plugin_to_update[plugin_basename( $path )]->update->new_version;
|
|
} else {
|
|
$data['plugins'][$plugin_data['Name']]['Maj'] = false;
|
|
}
|
|
}
|
|
|
|
//get non active plugins
|
|
foreach ($allPlugin as $key => $value) {
|
|
if (is_plugin_inactive($key)) {
|
|
$data['inactiveplugins'][$value['Name']] = true;
|
|
}
|
|
}
|
|
//get installed wordpress version
|
|
$currentversion = get_bloginfo('version');
|
|
$data['Wordpress']['Version'] = $currentversion;
|
|
|
|
//call wordpress api to check if our version is updatable
|
|
$url = 'https://api.wordpress.org/core/version-check/1.7/';
|
|
$response = wp_remote_get($url);
|
|
$json = $response['body'];
|
|
$obj = json_decode($json);
|
|
|
|
|
|
if (($obj->offers[0]->current) > $currentversion) {
|
|
$data['Wordpress']['Maj'] = $obj->offers[0]->current;
|
|
} else {
|
|
$data['Wordpress']['Maj'] = false;
|
|
}
|
|
|
|
|
|
//get mu plugins / check security plugin
|
|
$muPlugins = get_mu_plugins();
|
|
if (isset($muPlugins['admin-useraccount-security.php'])) {
|
|
$data['SecurityPlugin'] = true;
|
|
} else {
|
|
$data['SecurityPlugin'] = false;
|
|
}
|
|
$logger->writeLog("Check Security END");
|
|
//check if .git folder is present
|
|
$pathgit = get_home_path().'.git';
|
|
if (is_dir($pathgit)) {
|
|
$data['Git'] = true;
|
|
} else {
|
|
$data['Git'] = false;
|
|
}
|
|
$logger->writeLog("GIT End");
|
|
//path to all the files and creates if necessary
|
|
$pathfolder = get_home_path().'version-checker';
|
|
$filepath = get_home_path().'version-checker/versions.json';
|
|
$htaccesspath = get_home_path().'version-checker/.htaccess';
|
|
$htpasswdpath = get_home_path().'version-checker/.htpasswd';
|
|
$logger->writeLog("START DIR");
|
|
if (!file_exists($pathfolder)) {
|
|
mkdir($pathfolder, 0777, true);
|
|
$logger->writeLog("Global dir");
|
|
}
|
|
if (!file_exists($htaccesspath)) {
|
|
$htaccess = 'AuthName "Protected Version Checker"
|
|
AuthType Basic
|
|
AuthUserFile '.$htpasswdpath.'
|
|
Require valid-user';
|
|
file_put_contents($htaccesspath, $htaccess);
|
|
|
|
$htpasswd = 'ldc-web:$1$LvylAO22$R72WuB1w8OpNdcISgXBZU.';
|
|
file_put_contents($htpasswdpath, $htpasswd);
|
|
}
|
|
file_put_contents($filepath, json_encode($data));
|
|
$logger->writeLog("End dir");
|
|
}
|
|
|
|
add_action ('check_version_cronjob', 'checkingVersion');
|
|
|
|
?>
|