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 displayWidgetContents()
 {
     $log = Elm_PhpErrorLog::autodetect();
     if (is_wp_error($log)) {
         $this->displayConfigurationHelp($log->get_error_message());
         return;
     }
     $doClearLog = isset($_GET['elm-action']) && ($_GET['elm-action'] = 'clear-log') && check_admin_referer('clear-log') && current_user_can($this->requiredCapability);
     if ($doClearLog) {
         $log->clear();
         echo '<p><strong>Log cleared.</strong></p>';
     }
     $lines = $log->readLastLines($this->settings->get('widget_line_count'), true);
     if (is_wp_error($lines)) {
         printf('<p>%s</p>', $lines->get_error_message());
     } else {
         if (empty($lines)) {
             echo '<p>The log file is empty.</p>';
         } else {
             echo '<table class="widefat"><tbody>';
             $isOddRow = false;
             foreach ($lines as $line) {
                 $isOddRow = !$isOddRow;
                 if ($this->settings->get('strip_wordpress_path')) {
                     $line['message'] = $this->plugin->stripWpPath($line['message']);
                 }
                 printf('<tr%s><td style="white-space:nowrap;">%s</td><td>%s</td></tr>', $isOddRow ? ' class="alternate"' : '', !empty($line['timestamp']) ? $this->plugin->formatTimestamp($line['timestamp']) : '', esc_html($line['message']));
             }
             echo '</tbody></table>';
             echo '<p>';
             printf('Log file: %s (%s) ', esc_html($log->getFilename()), $this->formatByteCount($log->getFileSize(), 2));
             printf('<a href="%s" class="button" onclick="return confirm(\'%s\');">%s</a>', wp_nonce_url(admin_url('/index.php?elm-action=clear-log'), 'clear-log'), 'Are you sure you want to clear the error log?', 'Clear Log');
             echo '</p>';
         }
     }
 }
Example #3
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();
 }
Example #4
0
 public function handleLogClearing()
 {
     $doClearLog = isset($_GET['elm-action']) && $_GET['elm-action'] === 'clear-log' && check_admin_referer('clear-log') && current_user_can($this->requiredCapability);
     if ($doClearLog) {
         $log = Elm_PhpErrorLog::autodetect();
         if (is_wp_error($log)) {
             return;
         }
         $log->clear();
         wp_redirect(admin_url('index.php?elm-log-cleared=1'));
         exit;
     }
 }