Last active
August 18, 2017 15:40
-
-
Save matdave/3b2420a8459a3baad61e4d6e7dc50414 to your computer and use it in GitHub Desktop.
MODX Beacon (Notify of Package and Version Info)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: matdave | |
* Date: 8/17/17 | |
* Time: 3:27 PM | |
* Description: Paths designed for core directly in front of the user's home directory. | |
*/ | |
define('MODX_CORE_PATH', getenv('HOME').'/core/'); | |
require_once MODX_CORE_PATH . 'model/modx/modx.class.php'; | |
$modx= new modX(); | |
$modx->initialize('mgr'); | |
$vers = $modx->getVersionData(); | |
if(!function_exists('checkForUpdates')){ | |
function checkForUpdates($package, $modx, $providerCache = array()){ | |
$updates = array('count' => 0); | |
if ($package->get('provider') > 0 && $modx->getOption('auto_check_pkg_updates',null,false)) { | |
$updateCacheKey = 'mgr/providers/updates/'.$package->get('provider').'/'.$package->get('signature'); | |
$updateCacheOptions = array( | |
xPDO::OPT_CACHE_KEY => $modx->cacheManager->getOption('cache_packages_key', null, 'packages'), | |
xPDO::OPT_CACHE_HANDLER => $modx->cacheManager->getOption('cache_packages_handler', null, $modx->cacheManager->getOption(xPDO::OPT_CACHE_HANDLER)), | |
); | |
$updates = $modx->cacheManager->get($updateCacheKey, $updateCacheOptions); | |
if (empty($updates)) { | |
/* cache providers to speed up load time */ | |
/** @var modTransportProvider $provider */ | |
if (!empty($providerCache[$package->get('provider')])) { | |
$provider =& $providerCache[$package->get('provider')]; | |
} else { | |
$provider = $package->getOne('Provider'); | |
if ($provider) { | |
$providerCache[$provider->get('id')] = $provider; | |
} | |
} | |
if ($provider) { | |
$updates = $provider->latest($package->get('signature')); | |
$updates = array('count' => count($updates)); | |
$modx->cacheManager->set($updateCacheKey, $updates, 300, $updateCacheOptions); | |
} | |
} | |
} | |
return (int)$updates['count'] >= 1 ? true : false; | |
} | |
} | |
//get Transport Packages | |
$packs = array(); | |
$providerCache = array(); | |
$c = $modx->newQuery('transport.modTransportPackage'); | |
$c->sortby('package_name', 'ASC'); | |
$c->sortby('installed', 'ASC'); | |
$packages = $modx->getCollection('transport.modTransportPackage', $c); | |
if(!empty($packages)){ | |
foreach($packages as $p){ | |
$update = checkForUpdates($p, $modx, $providerCache); | |
$packs[$p->get('package_name')] = array( | |
'row' => '<td>'.$p->get('package_name'). ' '. ($update ? '<a href="'.$modx->getOption('site_url').'/manager/?a=workspaces">(update!)</a>' : null) .'</td><td>' . $p->get('version_major'). '.' . $p->get('version_minor'). '.' . $p->get('version_patch'). ' ' . $p->get('version_release'). '</td><td>' . $p->get('installed').'</td>' | |
,'update' => $update | |
); } | |
} | |
$message = "<h2>".$modx->getOption('site_name'). " " .$modx->getOption('site_url') . " " .$vers['full_version'] ." </h2>"; | |
$packs = array_values($packs); | |
$updates = 0; | |
$message .= "<h3>Installed Packages</h3>"; | |
$message .= "<table><thead><th>Package</th><th>Version</th><th>Installed</th></thead><tbody>"; | |
foreach($packs as $m){ | |
$message .= "<tr>".$m['row']."</tr>"; | |
if($m['update']){ | |
$updates++; | |
} | |
} | |
$message .="</tbody></table>"; | |
$message .= ($updates > 0)? "<h3><a href='".$modx->getOption('site_url')."/manager/?a=workspaces'>". $updates." Update(s) Available</a></h3>":null; | |
$modx->getService('mail', 'mail.modPHPMailer'); | |
$modx->mail->set(modMail::MAIL_BODY,$message); | |
$modx->mail->set(modMail::MAIL_FROM,'[email protected]'); | |
$modx->mail->set(modMail::MAIL_FROM_NAME,'MODX Beacon'); | |
$modx->mail->set(modMail::MAIL_SUBJECT,$modx->getOption('site_name'). " " .$modx->getOption('site_url') ." Version Check"); | |
$modx->mail->address('to','[email protected]'); | |
$modx->mail->setHTML(true); | |
if (!$modx->mail->send()) { | |
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo); | |
} | |
$modx->mail->reset(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment