- md5
- d34679266c6ada7edb49ca0ed071a7f7
- sha1
- dbbc316e2383c1d01608bf288a351bea95d0b6a8
- sha256
- 5f3c711ffbb5972c39912a4e34acb10df00c81be7e2e445810a9d6a0246a73bf
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
<?php
/**
* @package Joomla.Administrator
* @subpackage com_joomlaupdate
*
* @copyright (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Joomlaupdate\Administrator\Model;
use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Mail\MailHelper;
use Joomla\CMS\Mail\MailTemplate;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Table\Asset;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Version;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Joomla! Notification Model
*
* @internal
* @since 5.4.0
*/
final class NotificationModel extends BaseDatabaseModel
{
/**
* Sends the update notification to the specifically configured emails and superusers
*
* @param string $type The type of notification to send. This is the last key for the mail template
* @param string $oldVersion The old version from before the update
*
* @return void
*
* @since 5.4.0
*/
public function sendNotification($type, $oldVersion): void
{
$params = ComponentHelper::getParams('com_joomlaupdate');
// Send a notification to all super users
$superUsers = $this->getSuperUsers();
if (empty($superUsers)) {
throw new \RuntimeException();
}
$app = Factory::getApplication();
$jLanguage = $app->getLanguage();
$sitename = $app->get('sitename');
$newVersion = (new Version())->getShortVersion();
$substitutions = [
'oldversion' => $oldVersion,
'newversion' => $newVersion,
'sitename' => $sitename,
'url' => Uri::root(),
];
// Send the emails to the Super Users
foreach ($superUsers as $superUser) {
$params = new Registry($superUser->params);
$jLanguage->load('com_joomlaupdate', JPATH_ADMINISTRATOR, 'en-GB', true, true);
$jLanguage->load('com_joomlaupdate', JPATH_ADMINISTRATOR, $params->get('admin_language', null), true, true);
$mailer = new MailTemplate('com_joomlaupdate.update.' . $type, $jLanguage->getTag());
$mailer->addRecipient($superUser->email);
$mailer->addTemplateData($substitutions);
$mailer->send();
}
}
/**
* Returns the Super Users email information. If you provide a comma separated $email list
* we will check that these emails do belong to Super Users and that they have not blocked
* system emails.
*
* @param null|string $email A list of Super Users to email
*
* @return array The list of Super User emails
*
* @since 5.4.0
*/
private function getSuperUsers($email = null): array
{
$db = $this->getDatabase();
$emails = [];
// Convert the email list to an array
if (!empty($email)) {
$temp = explode(',', $email);
foreach ($temp as $entry) {
if (!MailHelper::isEmailAddress(trim($entry))) {
continue;
}
$emails[] = trim($entry);
}
$emails = array_unique($emails);
}
// Get a list of groups which have Super User privileges
$ret = [];
try {
$rootId = (new Asset($db))->getRootId();
$rules = Access::getAssetRules($rootId)->getData();
$rawGroups = $rules['core.admin']->getData();
$groups = [];
if (empty($rawGroups)) {
return $ret;
}
foreach ($rawGroups as $g => $enabled) {
if ($enabled) {
$groups[] = $g;
}
}
if (empty($groups)) {
return $ret;
}
} catch (\Exception $exc) {
return $ret;
}
// Get the user IDs of users belonging to the SA groups
try {
$query = $db->getQuery(true)
->select($db->quoteName('user_id'))
->from($db->quoteName('#__user_usergroup_map'))
->whereIn($db->quoteName('group_id'), $groups);
$db->setQuery($query);
$userIDs = $db->loadColumn(0);
if (empty($userIDs)) {
return $ret;
}
} catch (\Exception $exc) {
return $ret;
}
// Get the user information for the Super Administrator users
try {
$query = $db->getQuery(true)
->select($db->quoteName(['id', 'username', 'email', 'params']))
->from($db->quoteName('#__users'))
->whereIn($db->quoteName('id'), $userIDs)
->where($db->quoteName('block') . ' = 0')
->where($db->quoteName('sendEmail') . ' = 1');
if (!empty($emails)) {
$lowerCaseEmails = array_map('strtolower', $emails);
$query->whereIn('LOWER(' . $db->quoteName('email') . ')', $lowerCaseEmails, ParameterType::STRING);
}
$ret = $db->setQuery($query)->loadObjectList();
} catch (\Exception) {
return $ret;
}
return $ret;
}
}
The same content is in 4 releases, Joomla 5.4.0-alpha3 to 6.0.0-beta1. See them all, or every version of this file.