Ejemplo n.º 1
0
 public function emailErrors()
 {
     if ($this->settings->get('send_errors_to_email') == '') {
         trigger_error('Can\'t send errors to email - no email address is specified', E_USER_WARNING);
         return;
     }
     $log = Elm_PhpErrorLog::autodetect();
     if (is_wp_error($log)) {
         trigger_error('Error log not detected', E_USER_WARNING);
         return;
     }
     $lines = $log->readLastLines($this->settings->get('email_line_count'), true);
     if (is_wp_error($lines)) {
         trigger_error('Error log is not accessible', E_USER_WARNING);
         return;
     }
     //Only include messages logged since the previous email.
     $logEntries = array();
     $foundNewMessages = false;
     $lastEmailTimestamp = $this->settings->get('email_last_line_timestamp');
     foreach ($lines as $line) {
         $foundNewMessages = $foundNewMessages || $line['timestamp'] > $lastEmailTimestamp;
         if ($foundNewMessages) {
             $logEntries[] = $line;
         }
     }
     if (!empty($logEntries)) {
         $subject = sprintf('PHP errors logged on %s', site_url());
         $body = sprintf("New PHP errors have been logged on %s\nHere are the last %d lines from %s:\n\n", site_url(), count($logEntries), $log->getFilename());
         $stripWordPressPath = $this->settings->get('strip_wordpress_path');
         $lastEntryTimestamp = time();
         //Fall-back value in case none of the new entries have a timestamp.
         foreach ($logEntries as $logEntry) {
             if ($stripWordPressPath) {
                 $logEntry['message'] = $this->stripWpPath($logEntry['message']);
             }
             if (!empty($logEntry['timestamp'])) {
                 $body .= '[' . $this->formatTimestamp($logEntry['timestamp']) . '] ';
                 $lastEntryTimestamp = $logEntry['timestamp'];
             }
             $body .= $logEntry['message'] . "\n";
         }
         if (wp_mail($this->settings->get('send_errors_to_email'), $subject, $body)) {
             $this->settings->set('email_last_line_timestamp', $lastEntryTimestamp);
         } else {
             trigger_error('Failed to send an email, wp_mail() returned FALSE', E_USER_WARNING);
         }
     }
 }
    public function handleSettingsForm()
    {
        if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget_id']) && is_array($_POST[$this->widgetId])) {
            $formInputs = $_POST[$this->widgetId];
            $this->settings->set('widget_line_count', intval($formInputs['widget_line_count']));
            if ($this->settings->get('widget_line_count') <= 0) {
                $this->settings->set('widget_line_count', $this->settings->get_defaults('widget_line_count'));
            }
            $this->settings->set('strip_wordpress_path', isset($formInputs['strip_wordpress_path']));
            $this->settings->set('send_errors_to_email', trim(strval($formInputs['send_errors_to_email'])));
            $this->settings->set('email_interval', intval($formInputs['email_interval']));
            if ($this->settings->get('email_interval') <= 60) {
                $this->settings->set('email_interval', $this->settings->get_defaults('email_interval'));
            }
            do_action('elm_settings_changed', $this->settings);
        }
        printf('<p><label>%s <input type="text" name="%s[widget_line_count]" value="%s" size="5"></label></p>', 'Number of lines to show:', esc_attr($this->widgetId), esc_attr($this->settings->get('widget_line_count')));
        printf('<p><label><input type="checkbox" name="%s[strip_wordpress_path]"%s> %s</label></p>', esc_attr($this->widgetId), $this->settings->get('strip_wordpress_path') ? ' checked="checked"' : '', 'Strip WordPress root directory from log messages');
        printf('<p>
				<label for="%1$s-send_errors_to_email">%2$s</label>
				<input type="text" class="widefat" name="%1$s[send_errors_to_email]" id="%1$s-send_errors_to_email" value="%3$s">
			</p>', esc_attr($this->widgetId), 'Periodically email logged errors to:', $this->settings->get('send_errors_to_email'));
        printf('<p><label>%s <select name="%s[email_interval]">', 'How often to send email (max):', esc_attr($this->widgetId));
        $intervals = array('Every 10 minutes' => 10 * 60, 'Every 15 minutes' => 15 * 60, 'Every 30 minutes' => 30 * 60, 'Hourly' => 60 * 60, 'Daily' => 24 * 60 * 60, 'Weekly' => 7 * 24 * 60 * 60);
        foreach ($intervals as $name => $interval) {
            printf('<option value="%d"%s>%s</option>', $interval, $interval == $this->settings->get('email_interval') ? ' selected="selected"' : '', $name);
        }
        echo '</select></label></p>';
    }
Ejemplo n.º 3
0
 public static function detect_version_change()
 {
     if (self::version() == self::$options->get('last_version')) {
         return;
     }
     self::$options->set('last_version', self::version());
     self::$options->set('upgrade_required', false);
     if (self::$options->get('enable_collection')) {
         Prompt_Event_Handling::record_environment();
     }
 }
Ejemplo n.º 4
0
 public function emailErrors()
 {
     if ($this->settings->get('send_errors_to_email') == '') {
         //Can't send errors to email if no email address is specified.
         return;
     }
     $lock = new Elm_ExclusiveLock('elm-email-errors');
     $lock->acquire();
     //Note: Locking failures are intentionally ignored. Most of them are likely to be caused by file permissions,
     //which are either intentional (making "/wp-content/uploads" non-writable) or not easily fixed by the user.
     //It's better to occasionally send multiple email notifications than to never send any.
     $log = Elm_PhpErrorLog::autodetect();
     if (is_wp_error($log)) {
         trigger_error('Error log not detected', E_USER_WARNING);
         $lock->release();
         return;
     }
     $lines = $log->readLastLines($this->settings->get('email_line_count'), true);
     if (is_wp_error($lines)) {
         trigger_error('Error log is not accessible', E_USER_WARNING);
         $lock->release();
         return;
     }
     //Only include messages logged since the previous email.
     $logEntries = array();
     $foundNewMessages = false;
     $lastEmailTimestamp = $this->settings->get('email_last_line_timestamp');
     $lastEntryTimestamp = time();
     //Fall-back value in case none of the new entries have a timestamp.
     foreach ($lines as $line) {
         $foundNewMessages = $foundNewMessages || $line['timestamp'] > $lastEmailTimestamp;
         if ($foundNewMessages) {
             $logEntries[] = $line;
         }
         if (!empty($line['timestamp'])) {
             $lastEntryTimestamp = $line['timestamp'];
         }
     }
     if (!empty($logEntries)) {
         $subject = sprintf('PHP errors logged on %s', site_url());
         $body = sprintf("New PHP errors have been logged on %s\nHere are the last %d lines from %s:\n\n", site_url(), count($logEntries), $log->getFilename());
         if ($this->settings->get('sort_order') === 'reverse-chronological') {
             $logEntries = array_reverse($logEntries);
         }
         $stripWordPressPath = $this->settings->get('strip_wordpress_path');
         foreach ($logEntries as $logEntry) {
             if ($stripWordPressPath) {
                 $logEntry['message'] = $this->stripWpPath($logEntry['message']);
             }
             if (!empty($logEntry['timestamp'])) {
                 $body .= '[' . $this->formatTimestamp($logEntry['timestamp']) . '] ';
             }
             $body .= $logEntry['message'] . "\n";
         }
         if (wp_mail($this->settings->get('send_errors_to_email'), $subject, $body)) {
             $this->settings->set('email_last_line_timestamp', $lastEntryTimestamp);
         } else {
             trigger_error('Failed to send an email, wp_mail() returned FALSE', E_USER_WARNING);
         }
     }
     $lock->release();
 }