[], 'groups' => [], ]; if (empty($id_message)) { return $targets; } $ret = db_get_all_rows_sql( sprintf( 'SELECT id_user FROM tnotification_user nu WHERE nu.id_mensaje = %d', $id_message ) ); if (is_array($ret)) { foreach ($ret as $row) { array_push($targets['users'], get_user_fullname($row['id_user'])); } } $ret = db_get_all_rows_sql( sprintf( 'SELECT COALESCE(tg.nombre,ng.id_group) as "id_group" FROM tnotification_group ng LEFT JOIN tgrupo tg ON tg.id_grupo=ng.id_group WHERE ng.id_mensaje = %d', $id_message ) ); if (is_array($ret)) { foreach ($ret as $row) { if ($row['id_group'] == '0') { $row['id_group'] = ''.__('All').''; } array_push($targets['groups'], $row['id_group']); } } return $targets; } /** * Return subtypes. * * @param string|null $source Source filter or all. * * @return array */ function notifications_get_subtypes(?string $source=null) { $subtypes = [ 'System status' => [ 'NOTIF.LICENSE.LIMITED', 'NOTIF.FILES.ATTACHMENT', 'NOTIF.FILES.DATAIN', 'NOTIF.FILES.DATAIN.BADXML', 'NOTIF.PHP.SAFE_MODE', 'NOTIF.PHP.INPUT_TIME', 'NOTIF.PHP.EXECUTION_TIME', 'NOTIF.PHP.UPLOAD_MAX_FILESIZE', 'NOTIF.PHP.MEMORY_LIMIT', 'NOTIF.PHP.DISABLE_FUNCTIONS', 'NOTIF.PHP.CHROMIUM', 'NOTIF.PHP.VERSION', 'NOTIF.HISTORYDB', 'NOTIF.PANDORADB', 'NOTIF.PANDORADB.HISTORICAL', 'NOTIF.HISTORYDB.MR', 'NOTIF.EXT.ELASTICSEARCH', 'NOTIF.EXT.LOGSTASH', 'NOTIF.METACONSOLE.DB_CONNECTION', 'NOTIF.DOWNTIME', 'NOTIF.UPDATEMANAGER.REGISTRATION', 'NOTIF.MISC.EVENTSTORMPROTECTION', 'NOTIF.MISC.DEVELOPBYPASS', 'NOTIF.MISC.FONTPATH', 'NOTIF.SECURITY.DEFAULT_PASSWORD', 'NOTIF.UPDATEMANAGER.OPENSETUP', 'NOTIF.UPDATEMANAGER.UPDATE', 'NOTIF.UPDATEMANAGER.MINOR', 'NOTIF.UPDATEMANAGER.MESSAGES', 'NOTIF.CRON.CONFIGURED', 'NOTIF.ALLOWOVERRIDE.MESSAGE', 'NOTIF.HAMASTER.MESSAGE', 'NOTIF.SERVER.STATUS', 'NOTIF.SERVER.QUEUE', 'NOTIF.SERVER.MASTER', 'NOTIF.ACCESSSTASTICS.PERFORMANCE', 'NOTIF.VARIABLES.PERFORMANCE', ], ]; if ($source === null) { return $subtypes; } if (isset($subtypes[$source]) === true) { return $subtypes[$source]; } return []; } /** * Check if current user has grants to read this notification * * @param integer $id_message Target message. * * @return boolean true, read available. False if not. */ function check_notification_readable(int $id_message) { global $config; if (empty($id_message)) { return false; } // Using distinct to avoid double response on group messages read by user. $sql = sprintf( 'SELECT DISTINCT tm.*, utimestamp_read > 0 as "read" FROM tmensajes tm LEFT JOIN tnotification_user nu ON tm.id_mensaje=nu.id_mensaje AND nu.id_user="%s" AND tm.id_mensaje=%d LEFT JOIN (tnotification_group ng INNER JOIN tusuario_perfil up ON ng.id_group=up.id_grupo AND up.id_grupo=ng.id_group ) ON tm.id_mensaje=ng.id_mensaje WHERE utimestamp_erased is null AND (nu.id_user="%s" OR up.id_usuario="%s" OR ng.id_group=0)', $config['id_user'], $id_message, $config['id_user'], $config['id_user'] ); return (bool) db_get_value_sql($sql); } /** * Returns the target users and groups assigned to be notified on * desired source. * * @param integer $id_source Source identificator. * @param string|null $subtype Subtype identification. * * @return array [users] and [groups] with the targets. */ function get_notification_source_targets( int $id_source, ?string $subtype=null ) { $ret = []; $filter = ''; if ($subtype !== null) { $matches = []; if (preg_match('/(.*)\.\d+$/', $subtype, $matches) > 0) { $subtype = $matches[1]; } $filter = sprintf( ' AND (ns.`subtype_blacklist` IS NULL OR ns.`subtype_blacklist` NOT LIKE "%%%s%%") ', $subtype ); } $users = db_get_all_rows_sql( sprintf( 'SELECT id_user, IF(ns.user_editable = 1,nsu.also_mail,ns.also_mail) AS also_mail FROM tnotification_source_user nsu INNER JOIN tnotification_source ns ON ns.id=nsu.id_source %s WHERE ns.id = %d AND ((ns.enabled is NULL OR ns.enabled != 0) OR (nsu.enabled is NULL OR nsu.enabled != 0))', $filter, $id_source ) ); if ($users !== false) { $i = 0; foreach ($users as $user) { $ret['users'][$i]['id_user'] = $user['id_user']; $ret['users'][$i++]['also_mail'] = $user['also_mail']; } } $groups = db_get_all_rows_sql( sprintf( 'SELECT id_group,ns.also_mail FROM tnotification_source_group nsg INNER JOIN tnotification_source ns ON ns.id=nsg.id_source %s WHERE ns.id = %d AND (ns.enabled is NULL OR ns.enabled != 0)', $filter, $id_source ) ); if ($groups !== false) { $i = 0; foreach ($groups as $group) { $ret['groups'][$i]['id_group'] = $group['id_group']; $ret['groups'][$i++]['also_mail'] = $group['also_mail']; } } return $ret; } /** * Return all info from tnotification_source * * @param array $filter Filter to table tnotification_source. * * @return array with sources info */ function notifications_get_all_sources($filter=[]) { return db_get_all_rows_filter('tnotification_source', $filter); } /** * Return the user sources to be inserted into a select * * @param integer $source_id Source database identificator. * * @return array with the user id in keys and user id in value too */ function notifications_get_user_sources_for_select($source_id) { $users = notifications_get_user_sources( ['id_source' => $source_id], ['id_user'] ); return index_array($users, 'id_user', 'id_user'); } /** * Get the user sources * * @param array $filter Filter of sql query. * @param array $fields Fields to get of query. * * @return array Array with user sources data. */ function notifications_get_user_sources($filter=[], $fields=[]) { $users = db_get_all_rows_filter( 'tnotification_source_user', $filter, $fields ); // If fails or no one is selected, return empty array. if ($users === false) { return []; } return $users; } /** * Return the groups sources to be inserted into a select * * @param integer $source_id Source database identificator. * * @return array with the group id in keys and group name in value */ function notifications_get_group_sources_for_select($source_id) { $groups = notifications_get_group_sources( ['id_source' => $source_id], ['id_group'] ); return index_array($groups, 'id_group', 'name'); } /** * Get the group sources * * @param array $filter Filter of sql query. * @param array $fields Fields retrieved. * * @return array With the group info */ function notifications_get_group_sources($filter=[], $fields=[]) { // Get only the tnotifications_source_group fields in addition to group name. if (empty($fields)) { $fields[] = 'tnsg.*'; } $fields = array_map( function ($field) { if (!preg_match('/^tnsg./', $field)) { $field = 'tnsg.'.$field; } return $field; }, $fields ); // Get groups. $groups = db_get_all_rows_filter( 'tnotification_source_group tnsg LEFT JOIN tgrupo tg ON tnsg.id_group = tg.id_grupo', $filter, array_merge($fields, ['IFNULL(tg.nombre, "All") AS name']) ); // If fails or no one is selected, return empty array. if ($groups === false) { return []; } return $groups; } /** * Delete a set of groups from notification source * * @param integer $source_id Source id. * @param array $groups Id of groups to be deleted. * * @return boolean True if success. False otherwise. */ function notifications_remove_group_from_source($source_id, $groups) { // Source id is mandatory. if (!isset($source_id)) { return false; } // Delete from database. return db_process_sql_delete( 'tnotification_source_group', [ 'id_group' => $groups, 'id_source' => $source_id, ] ) !== false; } /** * Delete a set of users from notification source * * @param integer $source_id Source id. * @param array $users Id of users to be deleted. * * @return boolean True if success. False otherwise. */ function notifications_remove_users_from_source($source_id, $users) { // Source id is mandatory. if (!isset($source_id)) { return false; } // Delete from database. return db_process_sql_delete( 'tnotification_source_user', [ 'id_user' => $users, 'id_source' => $source_id, ] ) !== false; } /** * Insert a set of groups to notification source * * @param integer $source_id Source id. * @param array $groups Id of groups to be deleted. * * @return boolean True if success. False otherwise. */ function notifications_add_group_to_source($source_id, $groups) { // Source id is mandatory. if (!isset($source_id)) { return false; } // Insert into database all groups passed. $res = true; foreach ($groups as $group) { if (!isset($group)) { continue; } $res = $res && db_process_sql_insert( 'tnotification_source_group', [ 'id_group' => $group, 'id_source' => $source_id, ] ) !== false; } return $res; } /** * Insert a set of users to notification source * * @param integer $source_id Source id. * @param array $users Id of users to be deleted. * * @return boolean True if success. False otherwise. */ function notifications_add_users_to_source($source_id, $users) { // Source id is mandatory. if (!isset($source_id)) { return false; } // Insert into database all groups passed. $res = true; $also_mail = db_get_value( 'also_mail', 'tnotification_source', 'id', $source_id ); foreach ($users as $user) { if (empty($user)) { continue; } $res = $res && db_process_sql_insert( 'tnotification_source_user', [ 'id_user' => $user, 'id_source' => $source_id, 'enabled' => 1, 'also_mail' => (int) $also_mail, ] ) !== false; } return $res; } /** * Get the groups that not own to a source and, for that reason, they can be * added to the source. * * @param integer $source_id Source id. * * @return array Indexed by id group all selectable groups. */ function notifications_get_group_source_not_configured($source_id) { $groups_selected = notifications_get_group_sources_for_select($source_id); $all_groups = users_get_groups_for_select( false, 'AR', false, true, $groups_selected ); return array_diff($all_groups, $groups_selected); } /** * Get the users that not own to a source and, for that reason, they can be * added to the source. * * @param integer $source_id Source id. * * @return array Indexed by id user, all selectable users. */ function notifications_get_user_source_not_configured($source_id) { $users_selected = array_keys( notifications_get_user_sources_for_select($source_id) ); $users = get_users('id_user', ['!id_user' => $users_selected], ['id_user']); return index_array($users, 'id_user', 'id_user'); } /** * Build a data struct to handle the value of a label * * @param mixed $status Status value. * @param mixed $enabled Enabled value. * * @return array with status (1|0) and enabled (1|0) */ function notifications_build_user_enable_return($status, $enabled) { return [ 'status' => (bool) $status === true ? 1 : 0, 'enabled' => (bool) $enabled === true ? 1 : 0, ]; } /** * Get user label (enabled, also_mail...) status. * * @param integer $source Id of notification source. * @param string $user User id. * @param string $label Label id (enabled, also_email...). * * @return array Return of notifications_build_user_enable_return. */ function notifications_get_user_label_status($source, $user, $label) { // If not enabled, it cannot be modificable. if (!$source['enabled']) { return notifications_build_user_enable_return(false, false); } // See at first for direct reference. $user_source = notifications_get_user_sources( [ 'id_source' => $source['id'], 'id_user' => $user, ] ); if (!empty($user_source)) { return notifications_build_user_enable_return( isset($user_source[0][$label]) ? $user_source[0][$label] : false, $source['user_editable'] ); } $common_groups = array_intersect( array_keys(users_get_groups($user)), array_keys(notifications_get_group_sources_for_select($source['id'])) ); // No group found, return no permissions. $value = empty($common_groups) ? false : $source[$label]; return notifications_build_user_enable_return($value, false); } /** * Set the status to a single label on config of users notifications. * * @param integer $source Id of notification source. * @param string $user User id. * @param string $label Label id (enabled, also_email...). * @param mixed $value Numeric value: 1 or 0. * * @return boolean True if success. */ function notifications_set_user_label_status($source, $user, $label, $value) { $source_info = notifications_get_all_sources(['id' => $source]); if (!isset($source_info[0]) || !$source_info[0]['enabled'] || !$source_info[0]['user_editable'] ) { return false; } return (bool) db_process_sql_update( 'tnotification_source_user', [$label => $value], [ 'id_user' => $user, 'id_source' => $source, ] ); } /** * Get the counters of notification. Usefull to print the header notification * ball. * * @return array With the fields: * 'notifications' => Total new notifications, * 'last_id' => Id of last read value. Usefull to make comparisons. */ function notifications_get_counters() { $num_notifications = 0; $last_id = 0; $last_message = messages_get_overview('timestamp', 'DESC', false, false, 1); if (!empty($last_message)) { $num_notifications = messages_get_count(); $last_id = $last_message[0]['id_mensaje']; } return [ 'notifications' => $num_notifications, 'last_id' => $last_id, ]; } // ///////////////////////////////////////////////////////////////////////////// // UI FUNCTIONS // ///////////////////////////////////////////////////////////////////////////// /** * Print the notification ball to see unread messages. * * @param integer $num_notifications Number of messages shown * in notification ball. * @param integer $last_id Id of last message shown * in the notification ball. * * @return string with HTML code of notification ball. */ function notifications_print_ball($num_notifications, $last_id) { $no_notifications = (int) $num_notifications === 0; $class_status = $no_notifications ? 'notification-ball-no-messages' : 'notification-ball-new-messages'; return sprintf( '
' ); break; } $split_subject = explode(' ', io_safe_output($message_info['subject'])); $is_image = false; $img = ''; foreach ($split_subject as $item) { if ($is_image) { if (preg_match('/src/', $item)) { $img .= $item.' >'; $is_image = false; } } if (preg_match('/img/', $item)) { $img = ' %s", $action.';click_on_notification_toast(event)', $message_info['id_mensaje'], messages_get_url($message_info['id_mensaje']), $target, html_print_image('images/info.svg', true, ['style' => 'height: 40px;margin-left: -20px;margin-top: -40px;']), io_safe_output($message_info['subject']), $body_preview ); }%s
%s