Exemplo n.º 1
0
/**
 * Helper function for sending notification mails
 *
 * @param string $message The message
 * @param int $level Notification level
 * @return
 */
function local_sandbox_inform_admin($message, $level = SANDBOX_LEVEL_NOTICE)
{
    // Get recipients
    $recipients = get_users_from_config(get_config('local_sandbox', 'notifyonerrors'), 'moodle/site:config');
    // If there are no recipients, don't execute.
    if (!is_array($recipients) || count($recipients) <= 0) {
        return false;
    }
    // If message level is below configured notice level, don't execute
    if ($level < get_config('local_sandbox', 'notifylevel')) {
        return false;
    }
    // Get subject
    if ($level > SANDBOX_LEVEL_WARNING) {
        $subject = get_string('emailsubjecterror', 'local_sandbox');
    } else {
        if ($level > SANDBOX_LEVEL_NOTICE) {
            $subject = get_string('emailsubjectwarning', 'local_sandbox');
        } else {
            $subject = get_string('emailsubjectnotice', 'local_sandbox');
        }
    }
    // Send mail
    foreach ($recipients as $r) {
        // Email the admin directly rather than putting these through the messaging system
        email_to_user($r, core_user::get_support_user(), $subject, $message);
    }
}
Exemplo n.º 2
0
/**
 * Notify admin users or admin user of any failed logins (since last notification).
 *
 * Note that this function must be only executed from the cron script
 * It uses the cache_flags system to store temporary records, deleting them
 * by name before finishing
 *
 * @return bool True if executed, false if not
 */
function notify_login_failures()
{
    global $CFG, $DB, $OUTPUT;
    if (empty($CFG->notifyloginfailures)) {
        return false;
    }
    $recip = get_users_from_config($CFG->notifyloginfailures, 'moodle/site:config');
    if (empty($CFG->lastnotifyfailure)) {
        $CFG->lastnotifyfailure = 0;
    }
    // If it has been less than an hour, or if there are no recipients, don't execute.
    if (time() - HOURSECS < $CFG->lastnotifyfailure || !is_array($recip) || count($recip) <= 0) {
        return false;
    }
    // we need to deal with the threshold stuff first.
    if (empty($CFG->notifyloginthreshold)) {
        $CFG->notifyloginthreshold = 10;
        // default to something sensible.
    }
    // Get all the IPs with more than notifyloginthreshold failures since lastnotifyfailure
    // and insert them into the cache_flags temp table
    $sql = "SELECT ip, COUNT(*)\n              FROM {log}\n             WHERE module = 'login' AND action = 'error'\n                   AND time > ?\n          GROUP BY ip\n            HAVING COUNT(*) >= ?";
    $params = array($CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
    $rs = $DB->get_recordset_sql($sql, $params);
    foreach ($rs as $iprec) {
        if (!empty($iprec->ip)) {
            set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0);
        }
    }
    $rs->close();
    // Get all the INFOs with more than notifyloginthreshold failures since lastnotifyfailure
    // and insert them into the cache_flags temp table
    $sql = "SELECT info, count(*)\n              FROM {log}\n             WHERE module = 'login' AND action = 'error'\n                   AND time > ?\n          GROUP BY info\n            HAVING count(*) >= ?";
    $params = array($CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
    $rs = $DB->get_recordset_sql($sql, $params);
    foreach ($rs as $inforec) {
        if (!empty($inforec->info)) {
            set_cache_flag('login_failure_by_info', $inforec->info, '1', 0);
        }
    }
    $rs->close();
    // Now, select all the login error logged records belonging to the ips and infos
    // since lastnotifyfailure, that we have stored in the cache_flags table
    $sql = "SELECT * FROM (\n        SELECT l.*, u.firstname, u.lastname\n              FROM {log} l\n              JOIN {cache_flags} cf ON l.ip = cf.name\n         LEFT JOIN {user} u         ON l.userid = u.id\n             WHERE l.module = 'login' AND l.action = 'error'\n                   AND l.time > ?\n                   AND cf.flagtype = 'login_failure_by_ip'\n        UNION ALL\n            SELECT l.*, u.firstname, u.lastname\n              FROM {log} l\n              JOIN {cache_flags} cf ON l.info = cf.name\n         LEFT JOIN {user} u         ON l.userid = u.id\n             WHERE l.module = 'login' AND l.action = 'error'\n                   AND l.time > ?\n                   AND cf.flagtype = 'login_failure_by_info') t\n        ORDER BY t.time DESC";
    $params = array($CFG->lastnotifyfailure, $CFG->lastnotifyfailure);
    // Init some variables
    $count = 0;
    $messages = '';
    // Iterate over the logs recordset
    $rs = $DB->get_recordset_sql($sql, $params);
    foreach ($rs as $log) {
        $log->time = userdate($log->time);
        $messages .= get_string('notifyloginfailuresmessage', '', $log) . "\n";
        $count++;
    }
    $rs->close();
    // If we have something useful to report.
    if ($count > 0) {
        $site = get_site();
        $subject = get_string('notifyloginfailuressubject', '', format_string($site->fullname));
        // Calculate the complete body of notification (start + messages + end)
        $body = get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot) . ($CFG->lastnotifyfailure != 0 ? '(' . userdate($CFG->lastnotifyfailure) . ')' : '') . "\n\n" . $messages . "\n\n" . get_string('notifyloginfailuresmessageend', '', $CFG->wwwroot) . "\n\n";
        // For each destination, send mail
        mtrace('Emailing admins about ' . $count . ' failed login attempts');
        foreach ($recip as $admin) {
            //emailing the admins directly rather than putting these through the messaging system
            email_to_user($admin, core_user::get_support_user(), $subject, $body);
        }
    }
    // Update lastnotifyfailure with current time
    set_config('lastnotifyfailure', time());
    // Finally, delete all the temp records we have created in cache_flags
    $DB->delete_records_select('cache_flags', "flagtype IN ('login_failure_by_ip', 'login_failure_by_info')");
    return true;
}
Exemplo n.º 3
0
 /**
  * Static function to create a new course request when passed an array of properties
  * for it.
  *
  * This function also handles saving any files that may have been used in the editor
  *
  * @static
  * @param stdClass $data
  * @return course_request The newly created course request
  */
 public static function create($data)
 {
     global $USER, $DB, $CFG;
     $data->requester = $USER->id;
     // Summary is a required field so copy the text over
     $data->summary = $data->summary_editor['text'];
     $data->summaryformat = $data->summary_editor['format'];
     $data->id = $DB->insert_record('course_request', $data);
     // Create a new course_request object and return it
     $request = new course_request($data);
     // Notify the admin if required.
     if ($users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse')) {
         $a = new stdClass();
         $a->link = "{$CFG->wwwroot}/course/pending.php";
         $a->user = fullname($USER);
         $subject = get_string('courserequest');
         $message = get_string('courserequestnotifyemail', 'admin', $a);
         foreach ($users as $user) {
             $request->notify($user, $USER, 'courserequested', $subject, $message);
         }
     }
     return $request;
 }
 /**
  * Do the job.
  * Throw exceptions on errors (the job will be retried).
  */
 public function execute()
 {
     global $CFG, $DB;
     if (empty($CFG->notifyloginfailures)) {
         return;
     }
     $recip = get_users_from_config($CFG->notifyloginfailures, 'moodle/site:config');
     if (empty($CFG->lastnotifyfailure)) {
         $CFG->lastnotifyfailure = 0;
     }
     // If it has been less than an hour, or if there are no recipients, don't execute.
     if (time() - HOURSECS < $CFG->lastnotifyfailure || !is_array($recip) || count($recip) <= 0) {
         return;
     }
     // We need to deal with the threshold stuff first.
     if (empty($CFG->notifyloginthreshold)) {
         $CFG->notifyloginthreshold = 10;
         // Default to something sensible.
     }
     // Get all the IPs with more than notifyloginthreshold failures since lastnotifyfailure
     // and insert them into the cache_flags temp table.
     $logmang = get_log_manager();
     $readers = $logmang->get_readers('\\core\\log\\sql_internal_reader');
     $reader = reset($readers);
     $readername = key($readers);
     if (empty($reader) || empty($readername)) {
         // No readers, no processing.
         return true;
     }
     $logtable = $reader->get_internal_log_table_name();
     $sql = "SELECT ip, COUNT(*)\n                  FROM {" . $logtable . "}\n                 WHERE eventname = ?\n                       AND timecreated > ?\n               GROUP BY ip\n                 HAVING COUNT(*) >= ?";
     $params = array('\\core\\event\\user_login_failed', $CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $iprec) {
         if (!empty($iprec->ip)) {
             set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0);
         }
     }
     $rs->close();
     // Get all the INFOs with more than notifyloginthreshold failures since lastnotifyfailure
     // and insert them into the cache_flags temp table.
     $sql = "SELECT userid, count(*)\n                  FROM {" . $logtable . "}\n                 WHERE eventname = ?\n                       AND timecreated > ?\n              GROUP BY userid\n                HAVING count(*) >= ?";
     $params = array('\\core\\event\\user_login_failed', $CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $inforec) {
         if (!empty($inforec->info)) {
             set_cache_flag('login_failure_by_id', $inforec->userid, '1', 0);
         }
     }
     $rs->close();
     // Now, select all the login error logged records belonging to the ips and infos
     // since lastnotifyfailure, that we have stored in the cache_flags table.
     $sql = "SELECT * FROM (\n                        SELECT l.*, u.username\n                          FROM {" . $logtable . "} l\n                          JOIN {cache_flags} cf ON l.ip = cf.name\n                     LEFT JOIN {user} u         ON l.userid = u.id\n                         WHERE l.eventname = ?\n                               AND l.timecreated > ?\n                               AND cf.flagtype = 'login_failure_by_ip'\n                    UNION ALL\n                        SELECT l.*, u.username\n                          FROM {" . $logtable . "} l\n                          JOIN {cache_flags} cf ON l.userid = " . $DB->sql_cast_char2int('cf.name') . "\n                     LEFT JOIN {user} u         ON l.userid = u.id\n                         WHERE l.eventname = ?\n                               AND l.timecreated > ?\n                               AND cf.flagtype = 'login_failure_by_info') t\n             ORDER BY t.timecreated DESC";
     $params = array('\\core\\event\\user_login_failed', $CFG->lastnotifyfailure, '\\core\\event\\user_login_failed', $CFG->lastnotifyfailure);
     // Init some variables.
     $count = 0;
     $messages = '';
     // Iterate over the logs recordset.
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $log) {
         $a = new \stdClass();
         $a->time = userdate($log->timecreated);
         if (empty($log->username)) {
             // Entries with no valid username. We get attempted username from the event's other field.
             $other = unserialize($log->other);
             $a->info = empty($other['username']) ? '' : $other['username'];
         } else {
             $a->info = $log->username;
         }
         $a->ip = $log->ip;
         $messages .= get_string('notifyloginfailuresmessage', '', $a) . "\n";
         $count++;
     }
     $rs->close();
     // If we have something useful to report.
     if ($count > 0) {
         $site = get_site();
         $subject = get_string('notifyloginfailuressubject', '', format_string($site->fullname));
         // Calculate the complete body of notification (start + messages + end).
         $params = array('id' => 0, 'modid' => 'site_errors', 'chooselog' => '1', 'logreader' => $readername);
         $url = new \moodle_url('/report/log/index.php', $params);
         $body = get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot) . ($CFG->lastnotifyfailure != 0 ? '(' . userdate($CFG->lastnotifyfailure) . ')' : '') . "\n\n" . $messages . "\n\n" . get_string('notifyloginfailuresmessageend', '', $url->out(false) . ' ') . "\n\n";
         // For each destination, send mail.
         mtrace('Emailing admins about ' . $count . ' failed login attempts');
         foreach ($recip as $admin) {
             // Emailing the admins directly rather than putting these through the messaging system.
             email_to_user($admin, \core_user::get_support_user(), $subject, $body);
         }
     }
     // Update lastnotifyfailure with current time.
     set_config('lastnotifyfailure', time());
     // Finally, delete all the temp records we have created in cache_flags.
     $DB->delete_records_select('cache_flags', "flagtype IN ('login_failure_by_ip', 'login_failure_by_info')");
 }
Exemplo n.º 5
0
$strtitle = get_string('courserequest');
/// Standard form processing if statement.
if ($requestform->is_cancelled()) {
    redirect($returnurl);
} else {
    if ($data = $requestform->get_data()) {
        print_header($strtitle, $strtitle, build_navigation($strtitle), $requestform->focus());
        print_heading($strtitle);
        /// Record the request.
        $data->requester = $USER->id;
        if (!insert_record('course_request', $data)) {
            print_error('errorsavingrequest', '', $returnurl);
        }
        /// Notify the admin if required.
        if ($CFG->courserequestnotify) {
            $users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse');
            foreach ($users as $user) {
                $subject = get_string('courserequest');
                $a = new object();
                $a->link = "{$CFG->wwwroot}/course/pending.php";
                $a->user = fullname($USER);
                $messagetext = get_string('courserequestnotifyemail', 'admin', $a);
                email_to_user($user, $USER, $subject, $messagetext);
            }
        }
        /// and redirect back to the course listing.
        notice(get_string('courserequestsuccess'), $returnurl);
    }
}
/// Show the request form.
print_header($strtitle, $strtitle, build_navigation($strtitle), $requestform->focus());
Exemplo n.º 6
0
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die;
require_once 'judgelib.php';
mtrace('Starting online judge cron');
// Nofity admin if judged is not running
$a = new stdClass();
$a->count = $DB->count_records('onlinejudge_tasks', array('status' => ONLINEJUDGE_STATUS_PENDING));
if ($a->count > 0) {
    $oldest_unjudged = $DB->get_records('onlinejudge_tasks', array('status' => ONLINEJUDGE_STATUS_PENDING), 'submittime ASC', 'submittime', 0, 1);
    $pending_period = time() - reset($oldest_unjudged)->submittime;
    $a->period = format_time($pending_period);
    // if there is at least one task has been keeping unjudged in queue for more than 5 mins
    if ($pending_period > 5 * 60) {
        mtrace("    Found {$a->count} long time pending tasks.");
        if ($users = get_users_from_config(get_config('local_onlinejudge', 'judgedcrashnotify'), 'moodle/site:config')) {
            $admin = get_admin();
            foreach ($users as $user) {
                $eventdata = new stdClass();
                $eventdata->component = 'local_onlinejudge';
                $eventdata->name = 'judgedcrashed';
                $eventdata->userfrom = $admin;
                $eventdata->userto = $user;
                $eventdata->subject = get_string('judgednotifysubject', 'local_onlinejudge', $a);
                $eventdata->fullmessage = get_string('judgednotifybody', 'local_onlinejudge', $a);
                $eventdata->fullmessageformat = FORMAT_PLAIN;
                $eventdata->fullmessagehtml = '';
                $eventdata->smallmessage = '';
                $eventdata->notification = 1;
                message_send($eventdata);
                mtrace('    Sent notification to ' . fullname($user));