[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/classes/task/ -> send_failed_login_notifications_task.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Scheduled task class.
  19   *
  20   * @package    core
  21   * @copyright  2013 onwards Martin Dougiamas  http://dougiamas.com
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\task;
  25  
  26  /**
  27   * Simple task to send notifications about failed login attempts.
  28   */
  29  class send_failed_login_notifications_task extends scheduled_task {
  30  
  31      /**
  32       * Get a descriptive name for this task (shown to admins).
  33       *
  34       * @return string
  35       */
  36      public function get_name() {
  37          return get_string('tasksendfailedloginnotifications', 'admin');
  38      }
  39  
  40      /**
  41       * Do the job.
  42       * Throw exceptions on errors (the job will be retried).
  43       */
  44      public function execute() {
  45          global $CFG, $DB;
  46  
  47          if (empty($CFG->notifyloginfailures)) {
  48              return;
  49          }
  50  
  51          $recip = get_users_from_config($CFG->notifyloginfailures, 'moodle/site:config');
  52  
  53          if (empty($CFG->lastnotifyfailure)) {
  54              $CFG->lastnotifyfailure = 0;
  55          }
  56  
  57          // If it has been less than an hour, or if there are no recipients, don't execute.
  58          if (((time() - HOURSECS) < $CFG->lastnotifyfailure) || !is_array($recip) || count($recip) <= 0) {
  59              return;
  60          }
  61  
  62          // We need to deal with the threshold stuff first.
  63          if (empty($CFG->notifyloginthreshold)) {
  64              $CFG->notifyloginthreshold = 10; // Default to something sensible.
  65          }
  66  
  67          // Get all the IPs with more than notifyloginthreshold failures since lastnotifyfailure
  68          // and insert them into the cache_flags temp table.
  69          $logmang = get_log_manager();
  70          $readers = $logmang->get_readers('\core\log\sql_internal_reader');
  71          $reader = reset($readers);
  72          $readername = key($readers);
  73          if (empty($reader) || empty($readername)) {
  74              // No readers, no processing.
  75              return true;
  76          }
  77          $logtable = $reader->get_internal_log_table_name();
  78  
  79          $sql = "SELECT ip, COUNT(*)
  80                    FROM {" . $logtable . "}
  81                   WHERE eventname = ?
  82                         AND timecreated > ?
  83                 GROUP BY ip
  84                   HAVING COUNT(*) >= ?";
  85          $params = array('\core\event\user_login_failed', $CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
  86          $rs = $DB->get_recordset_sql($sql, $params);
  87          foreach ($rs as $iprec) {
  88              if (!empty($iprec->ip)) {
  89                  set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0);
  90              }
  91          }
  92          $rs->close();
  93  
  94          // Get all the INFOs with more than notifyloginthreshold failures since lastnotifyfailure
  95          // and insert them into the cache_flags temp table.
  96          $sql = "SELECT userid, count(*)
  97                    FROM {" . $logtable . "}
  98                   WHERE eventname = ?
  99                         AND timecreated > ?
 100                GROUP BY userid
 101                  HAVING count(*) >= ?";
 102          $params = array('\core\event\user_login_failed', $CFG->lastnotifyfailure, $CFG->notifyloginthreshold);
 103          $rs = $DB->get_recordset_sql($sql, $params);
 104          foreach ($rs as $inforec) {
 105              if (!empty($inforec->info)) {
 106                  set_cache_flag('login_failure_by_id', $inforec->userid, '1', 0);
 107              }
 108          }
 109          $rs->close();
 110  
 111          // Now, select all the login error logged records belonging to the ips and infos
 112          // since lastnotifyfailure, that we have stored in the cache_flags table.
 113          $sql = "SELECT * FROM (
 114                          SELECT l.*, u.username
 115                            FROM {" . $logtable . "} l
 116                            JOIN {cache_flags} cf ON l.ip = cf.name
 117                       LEFT JOIN {user} u         ON l.userid = u.id
 118                           WHERE l.eventname = ?
 119                                 AND l.timecreated > ?
 120                                 AND cf.flagtype = 'login_failure_by_ip'
 121                      UNION ALL
 122                          SELECT l.*, u.username
 123                            FROM {" . $logtable . "} l
 124                            JOIN {cache_flags} cf ON l.userid = " . $DB->sql_cast_char2int('cf.name') . "
 125                       LEFT JOIN {user} u         ON l.userid = u.id
 126                           WHERE l.eventname = ?
 127                                 AND l.timecreated > ?
 128                                 AND cf.flagtype = 'login_failure_by_info') t
 129               ORDER BY t.timecreated DESC";
 130          $params = array('\core\event\user_login_failed', $CFG->lastnotifyfailure, '\core\event\user_login_failed', $CFG->lastnotifyfailure);
 131  
 132          // Init some variables.
 133          $count = 0;
 134          $messages = '';
 135          // Iterate over the logs recordset.
 136          $rs = $DB->get_recordset_sql($sql, $params);
 137          foreach ($rs as $log) {
 138              $a = new \stdClass();
 139              $a->time = userdate($log->timecreated);
 140              if (empty($log->username)) {
 141                  // Entries with no valid username. We get attempted username from the event's other field.
 142                  $other = unserialize($log->other);
 143                  $a->info = empty($other['username']) ? '' : $other['username'];
 144              } else {
 145                  $a->info = $log->username;
 146              }
 147              $a->ip = $log->ip;
 148              $messages .= get_string('notifyloginfailuresmessage', '', $a)."\n";
 149              $count++;
 150          }
 151          $rs->close();
 152  
 153          // If we have something useful to report.
 154          if ($count > 0) {
 155              $site = get_site();
 156              $subject = get_string('notifyloginfailuressubject', '', format_string($site->fullname));
 157              // Calculate the complete body of notification (start + messages + end).
 158              $params = array('id' => 0, 'modid' => 'site_errors', 'chooselog' => '1', 'logreader' => $readername);
 159              $url = new \moodle_url('/report/log/index.php', $params);
 160              $body = get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot) .
 161                      (($CFG->lastnotifyfailure != 0) ? '('.userdate($CFG->lastnotifyfailure).')' : '')."\n\n" .
 162                      $messages .
 163                      "\n\n".get_string('notifyloginfailuresmessageend', '',  $url->out(false).' ')."\n\n";
 164  
 165              // For each destination, send mail.
 166              mtrace('Emailing admins about '. $count .' failed login attempts');
 167              foreach ($recip as $admin) {
 168                  // Emailing the admins directly rather than putting these through the messaging system.
 169                  email_to_user($admin, \core_user::get_support_user(), $subject, $body);
 170              }
 171          }
 172  
 173          // Update lastnotifyfailure with current time.
 174          set_config('lastnotifyfailure', time());
 175  
 176          // Finally, delete all the temp records we have created in cache_flags.
 177          $DB->delete_records_select('cache_flags', "flagtype IN ('login_failure_by_ip', 'login_failure_by_info')");
 178  
 179      }
 180  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1