public function onAfterRender()
    {
        // Check for PHP4
        if (defined('PHP_VERSION')) {
            $version = PHP_VERSION;
        } elseif (function_exists('phpversion')) {
            $version = phpversion();
        } else {
            // No version info. I'll lie and hope for the best.
            $version = '5.0.0';
        }
        // Old PHP version detected. EJECT! EJECT! EJECT!
        if (!version_compare($version, '5.2.7', '>=')) {
            return;
        }
        // Make sure Akeeba Backup is installed
        if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_akeeba')) {
            return;
        }
        JLoader::import('joomla.filesystem.file');
        $db = JFactory::getDBO();
        // If another extension using Live Update is already loaded, or if
        // another update check plugin has already run, bail out.
        if (class_exists('LiveUpdate')) {
            return;
        }
        // Is Akeeba Backup with Live Update installed?
        $liveUpdateFile = JPATH_ADMINISTRATOR . '/components/com_akeeba/liveupdate/liveupdate.php';
        if (!JFile::exists($liveUpdateFile)) {
            return;
        }
        // Is Akeeba Backup enabled?
        $query = $db->getQuery(true)->select($db->qn('enabled'))->from($db->qn('#__extensions'))->where($db->qn('element') . ' = ' . $db->q('com_akeeba'))->where($db->qn('type') . ' = ' . $db->q('component'));
        $db->setQuery($query);
        $enabled = $db->loadResult();
        if (!$enabled) {
            return;
        }
        // Is the One Click Action plugin enabled?
        $app = JFactory::getApplication();
        $jResponse = $app->triggerEvent('onOneClickActionEnabled');
        if (empty($jResponse)) {
            return;
        }
        $status = false;
        foreach ($jResponse as $response) {
            $status = $status || $response;
        }
        if (!$status) {
            return;
        }
        // Do we have to run (at most once per 3 hours)?
        JLoader::import('joomla.html.parameter');
        JLoader::import('joomla.application.component.helper');
        $component = JComponentHelper::getComponent('com_akeeba');
        if (!$component->params instanceof JRegistry) {
            $params = new JParameter($component->params);
        } else {
            $params = $component->params;
        }
        if (version_compare(JVERSION, '3.0', 'ge')) {
            $last = $params->get('plg_akeebaupdatecheck', 0);
        } else {
            $last = $params->getValue('plg_akeebaupdatecheck', 0);
        }
        $now = time();
        if (abs($now - $last) < 86400) {
            return;
        }
        // Use a 20% chance of running; this allows multiple concurrent page
        // requests to not cause double update emails being sent out.
        $random = rand(1, 5);
        if ($random != 3) {
            return;
        }
        // Update last run status
        if (version_compare(JVERSION, '3.0', 'ge')) {
            $params->set('plg_akeebaupdatecheck', $now);
        } else {
            $params->setValue('plg_akeebaupdatecheck', $now);
        }
        $db = JFactory::getDBO();
        $data = $params->toString('JSON');
        $query = $db->getQuery(true)->update($db->qn('#__extensions'))->set($db->qn('params') . ' = ' . $db->q($data))->where($db->qn('element') . ' = ' . $db->q('com_akeeba'))->where($db->qn('type') . ' = ' . $db->q('component'));
        $db->setQuery($query);
        // If a DB error occurs, return null
        $db->execute();
        if ($db->getErrorNum()) {
            return;
        }
        if (!defined('AKEEBAENGINE')) {
            define('AKEEBAENGINE', 1);
            // Required for accessing Akeeba Engine's factory class
        }
        include_once JPATH_ADMINISTRATOR . '/components/com_akeeba/version.php';
        require_once JPATH_ADMINISTRATOR . '/components/com_akeeba/akeeba/factory.php';
        // If this is not the Professional release, bail out. So far I have only
        // received complaints about this feature from users of the Core release
        // who never bothered to read the documentation. FINE! If you are bitching
        // about it, you don't get this feature (unless you are a developer who can
        // come here and edit the code). Fair enough.
        if (!defined('AKEEBA_PRO')) {
            return;
        }
        if (!AKEEBA_PRO) {
            return;
        }
        // OK, cool. Let's run Live Update fetch, OK?
        require_once $liveUpdateFile;
        $updateInfo = LiveUpdate::getUpdateInformation();
        if (!$updateInfo->hasUpdates) {
            return;
        }
        // No point continuing if there are no udpates available
        if (!$updateInfo->supported) {
            return;
        }
        // No point continuing if Live Update is not supported
        if ($updateInfo->stuck) {
            return;
        }
        // No point continuing if Live Update is stuck
        if (empty($updateInfo->version)) {
            return;
        }
        // No point continuing if the version is empty (something went wrong)
        if (empty($updateInfo->stability)) {
            return;
        }
        // Ditto
        // If we're here, we have updates. Let's create an OTP.
        $uri = JURI::base();
        $uri = rtrim($uri, '/');
        $uri .= substr($uri, -13) != 'administrator' ? '/administrator/' : '/';
        $link = 'index.php?option=com_akeeba&view=liveupdate&task=startupdate';
        $superAdmins = array();
        $superAdminEmail = $this->params->get('email', '');
        if (!empty($superAdminEmail)) {
            $superAdmins = $this->_getSuperAdministrators($superAdminEmail);
        }
        if (empty($superAdmins)) {
            $superAdmins = $this->_getSuperAdministrators();
        }
        if (empty($superAdmins)) {
            return;
        }
        $this->_loadLanguage();
        $email_subject = <<<ENDSUBJECT
THIS EMAIL IS SENT FROM YOUR SITE "[SITENAME]" - Update available
ENDSUBJECT;
        $email_body = <<<ENDBODY
This email IS NOT sent by the authors of Akeeba Backup. It is sent automatically
by your own site, [SITENAME]

================================================================================
UPDATE INFORMATION
================================================================================

Your site has determined that there is an updated version of Akeeba Backup
available for download.

New version number: [VERSION]
Release date:       [DATE]
Stability level:    [STABILITY]

This email is sent to you by your site to remind you of this fact. The authors
of the software will never contact you about available updates.

================================================================================
UPDATE INSTRUCTIONS
================================================================================

To install the update on [SITENAME] please click the following link. (If the URL
is not a link, simply copy & paste it to your browser).

Update link:        [LINK]

This one-time use link, valid for 24 hours, will automatically log you in to
your site, [SITENAME], and perform the update automatically.

================================================================================
WHY AM I RECEIVING THIS EMAIL?
================================================================================

This email has been automatically sent by a plugin you, or the person who built
or manages your site, has installed and explicitly activated. This plugin looks
for updated versions of the software and sends an email notification to all
Super Users. You will receive several similar emails from your site, up to 6
times per day, until you either update the software or disable these emails.

To disable these emails, please unpublish the 'Akeeba Backup Update
Notification' plugin in the Plugin Manager on your site.

If you do not understand what this means, please do not contact the authors of
the software. They are NOT sending you this email and they cannot help you.
Instead, please contact the person who built or manages your site.

If you are the person who built or manages your website, please note that you
activated the update email notification feature during Akeeba Backup's first
run, by clicking on a check box with a clear explanation of how this feature
works printed under it.

================================================================================
WHO SENT ME THIS EMAIL?
================================================================================

This email is sent to you by your own site, [SITENAME]

ENDBODY;
        $jconfig = JFactory::getConfig();
        if (version_compare(JVERSION, '3.0', 'ge')) {
            $sitename = $jconfig->get('sitename');
        } else {
            $sitename = $jconfig->getValue('config.sitename');
        }
        $substitutions = array('[VERSION]' => $updateInfo->version, '[DATE]' => $updateInfo->date, '[STABILITY]' => ucfirst($updateInfo->stability), '[SITENAME]' => $sitename);
        // If Admin Tools Professional is installed, fetch the administrator secret key as well
        $adminpw = '';
        $modelFile = JPATH_ROOT . '/administrator/components/com_admintools/models/storage.php';
        if (@file_exists($modelFile)) {
            include_once $modelFile;
            if (class_exists('AdmintoolsModelStorage')) {
                if (version_compare(JVERSION, '3.0', 'ge')) {
                    $model = JModelLegacy::getInstance('Storage', 'AdmintoolsModel');
                } else {
                    $model = JModel::getInstance('Storage', 'AdmintoolsModel');
                }
                $adminpw = $model->getValue('adminpw', '');
            }
        }
        foreach ($superAdmins as $sa) {
            $otp = plgSystemOneclickaction::addAction($sa->id, $link);
            if (is_null($otp)) {
                // If the OTP is null, a database error occurred
                return;
            } elseif (empty($otp)) {
                // If the OTP is empty, an OTP for the same action was already
                // created and it hasn't expired.
                continue;
            }
            $emaillink = $uri . 'index.php?oneclickaction=' . $otp;
            if (!empty($adminpw)) {
                $emaillink .= '&' . urlencode($adminpw);
            }
            $substitutions['[LINK]'] = $emaillink;
            foreach ($substitutions as $k => $v) {
                $email_subject = str_replace($k, $v, $email_subject);
                $email_body = str_replace($k, $v, $email_body);
            }
            $mailer = JFactory::getMailer();
            if (version_compare(JVERSION, '3.0', 'ge')) {
                $mailfrom = $jconfig->get('mailfrom');
                $fromname = $jconfig->get('fromname');
            } else {
                $mailfrom = $jconfig->getValue('config.mailfrom');
                $fromname = $jconfig->getValue('config.fromname');
            }
            $mailer->setSender(array($mailfrom, $fromname));
            $mailer->addRecipient($sa->email);
            $mailer->setSubject($email_subject);
            $mailer->setBody($email_body);
            $mailer->Send();
        }
    }
    public function onAfterRender()
    {
        // Get the singleclick preference
        $singleClick = $this->params->get('singleclick', 0);
        // Is the One Click Action plugin enabled?
        $app = JFactory::getApplication();
        $jResponse = $app->triggerEvent('onOneClickActionEnabled');
        if (empty($jResponse) && $singleClick) {
            return;
        }
        $status = false;
        foreach ($jResponse as $response) {
            $status = $status || $response;
        }
        if (!$status && $singleClick) {
            return;
        }
        // Get the timeout for Joomla! updates
        jimport('joomla.application.component.helper');
        $component = JComponentHelper::getComponent('com_installer');
        $params = $component->params;
        $cache_timeout = $params->get('cachetimeout', 6, 'int');
        $cache_timeout = 3600 * $cache_timeout;
        // Do we need to run?
        // Store the last run timestamp inside out table
        $db = JFactory::getDBO();
        $query = $db->getQuery(true)->select($db->qn('value'))->from($db->qn('#__admintools_storage'))->where($db->qn('key') . ' = ' . $db->q('atoolsjupdatecheck_lastrun'));
        $last = (int) $db->setQuery($query)->loadResult();
        $now = time();
        if (!defined('ATJUPDATEDEBUG') && abs($now - $last) < $cache_timeout) {
            return;
        }
        // Update last run status
        // If I have the time of the last run, I can update, otherwise insert
        if ($last) {
            $query = $db->getQuery(true)->update($db->qn('#__admintools_storage'))->set($db->qn('value') . ' = ' . $db->q($now))->where($db->qn('key') . ' = ' . $db->q('atoolsjupdatecheck_lastrun'));
        } else {
            $query = $db->getQuery(true)->insert($db->qn('#__admintools_storage'))->columns(array($db->qn('key'), $db->qn('value')))->values($db->q('atoolsjupdatecheck_lastrun') . ', ' . $db->q($now));
        }
        try {
            $result = $db->setQuery($query)->execute();
        } catch (Exception $exc) {
            $result = false;
        }
        if (!$result) {
            return;
        }
        // This is the extension ID for Joomla! itself
        $eid = 700;
        // Get any available updates
        $updater = JUpdater::getInstance();
        $results = $updater->findUpdates(array($eid), $cache_timeout);
        if (!$results) {
            return;
        }
        require_once JPATH_ADMINISTRATOR . '/components/com_installer/models/update.php';
        $model = JModelLegacy::getInstance('Update', 'InstallerModel');
        $model->setState('filter.extension_id', $eid);
        $updates = $model->getItems();
        if (empty($updates)) {
            return;
        }
        $update = array_pop($updates);
        // Check the version. It must be different than the current version.
        if (version_compare($update->version, JVERSION, 'eq')) {
            return;
        }
        // If we're here, we have updates. Let's create an OTP.
        $uri = JURI::base();
        $uri = rtrim($uri, '/');
        $uri .= substr($uri, -13) != 'administrator' ? '/administrator/' : '/';
        $link = 'index.php?option=com_joomlaupdate';
        $superAdmins = array();
        $superAdminEmail = $this->params->get('email', '');
        if (!empty($superAdminEmail)) {
            $superAdmins = $this->_getSuperAdministrators($superAdminEmail);
        }
        if (empty($superAdmins)) {
            $superAdmins = $this->_getSuperAdministrators();
        }
        if (empty($superAdmins)) {
            return;
        }
        $this->loadLanguage();
        $email_subject = <<<ENDSUBJECT
THIS EMAIL IS SENT FROM YOUR SITE "[SITENAME]" - Update available
ENDSUBJECT;
        if ($singleClick) {
            $autoLoginReminder = <<<ALREND
This one-time use link, valid for 24 hours, will automatically log you in to
your site, [SITENAME], and perform the update automatically.

IMPORTANT NOTE: While this kind of one-time use links are convenient, they can
pose a security risk to your site. If this or any other update notification
email with a one-time use automatic log in update URL is intercepted by a
malicious third party they can use it to gain access to your site. We strongly
advise you to avoid using this feature.

ALREND;
        } else {
            $autoLoginReminder = <<<ALREND
Visiting this link will require you to enter your login credentials (typically
your username and password) into your site's administrator login page in order
to initiate the update process. If you are not sure about the legitimacy of
this email message we strongly recommend you to visit your site's
administrator page manually, log in, and check for the availability of updates
yourself.

ALREND;
        }
        $email_body = <<<ENDBODY
This email IS NOT sent by Joomla.org or Akeeba Ltd. It is sent automatically
by your own site, [SITENAME]

================================================================================
UPDATE INFORMATION
================================================================================

Your site has determined that there is an updated version of Joomla!
available for download.

Joomla! version currently installed:        [CURVERSION]
Joomla! version available for installation: [NEWVERSION]

This email is sent to you by your site to remind you of this fact. The authors
of Joomla! (Open Source Matters) or Admin Tools (Akeeba Ltd) will not contact
you about available updates of Joomla!.

================================================================================
UPDATE INSTRUCTIONS
================================================================================

To install the update on [SITENAME] please click the following link. (If the URL
is not a link, simply copy & paste it to your browser).

Update link: [LINK]

{$autoLoginReminder}

================================================================================
WHY AM I RECEIVING THIS EMAIL?
================================================================================

This email has been automatically sent by a plugin you, or the person who built
or manages your site, has installed and explicitly activated. This plugin looks
for updated versions of Joomla! and sends an email notification to all Super
Users. You will receive several similar emails from your site, up to 6 times
per day, until you either update the software or disable these emails.

To disable these emails, please unpublish the 'System - Joomla! Update Email'
plugin in the Plugin Manager on your site.

If you do not understand what this means, please do not contact the authors of
Joomla! or Admin Tools. They are NOT sending you this email and they cannot
help you. Instead, please contact the person who built or manages your site.

If you are the person who built or manages your website, please note that you
activated the update email notification feature during Admin Tools' first run,
by clicking on a check box with a clear explanation of how this feature works
printed under it.

================================================================================
WHO SENT ME THIS EMAIL?
================================================================================

This email is sent to you by your own site, [SITENAME]

ENDBODY;
        $newVersion = $update->version;
        $jVersion = new JVersion();
        $currentVersion = $jVersion->getShortVersion();
        $jconfig = JFactory::getConfig();
        $sitename = $jconfig->get('sitename');
        $substitutions = array('[NEWVERSION]' => $newVersion, '[CURVERSION]' => $currentVersion, '[SITENAME]' => $sitename);
        // If Admin Tools Professional is installed, fetch the administrator secret key as well
        $adminpw = '';
        $modelFile = JPATH_ROOT . '/administrator/components/com_admintools/models/storage.php';
        if (@file_exists($modelFile)) {
            include_once $modelFile;
            if (class_exists('AdmintoolsModelStorage')) {
                $model = JModelLegacy::getInstance('Storage', 'AdmintoolsModel');
                $adminpw = $model->getValue('adminpw', '');
            }
        }
        foreach ($superAdmins as $sa) {
            if ($singleClick) {
                $otp = plgSystemOneclickaction::addAction($sa->id, $link);
                if (is_null($otp)) {
                    // If the OTP is null, a database error occurred
                    return;
                } elseif (empty($otp)) {
                    // If the OTP is empty, an OTP for the same action was already
                    // created and it hasn't expired.
                    continue;
                }
                $emaillink = $uri . 'index.php?oneclickaction=' . $otp;
            } else {
                $emaillink = $uri . $link;
            }
            if (!empty($adminpw)) {
                $emaillink .= '&' . urlencode($adminpw);
            }
            $substitutions['[LINK]'] = $emaillink;
            foreach ($substitutions as $k => $v) {
                $email_subject = str_replace($k, $v, $email_subject);
                $email_body = str_replace($k, $v, $email_body);
            }
            $mailer = JFactory::getMailer();
            $mailfrom = $jconfig->get('mailfrom');
            $fromname = $jconfig->get('fromname');
            $mailer->setSender(array($mailfrom, $fromname));
            $mailer->addRecipient($sa->email);
            $mailer->setSubject($email_subject);
            $mailer->setBody($email_body);
            $mailer->Send();
        }
    }