/**
  * @return array
  */
 public static function fileFilter()
 {
     $files = AuditEntry::getDb()->cache(function () {
         return AuditError::find()->distinct(true)->select('file')->where(['is not', 'file', null])->groupBy('file')->orderBy('file ASC')->column();
     }, 30);
     return array_combine($files, $files);
 }
 /**
  * Displays a single AuditError model.
  * @param integer $id
  * @return mixed
  * @throws \HttpInvalidParamException
  */
 public function actionView($id)
 {
     $model = AuditError::findOne($id);
     if (!$model) {
         throw new \HttpInvalidParamException('Invalid request number specified');
     }
     return $this->render('view', ['model' => $model]);
 }
 /**
  * Displays a single AuditError model.
  * @param integer $id
  * @return mixed
  * @throws NotFoundHttpException
  */
 public function actionView($id)
 {
     $model = AuditError::findOne($id);
     if (!$model) {
         throw new NotFoundHttpException('The requested error does not exist.');
     }
     return $this->render('view', ['model' => $model]);
 }
Exemple #4
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->module->registerFunction('exception', function (\Exception $e) {
         $entry = $this->module->getEntry(true);
         return $entry ? AuditError::log($entry, $e) : false;
     });
     $this->module->registerFunction('errorMessage', function ($message, $code = 0, $file = '', $line = 0, $trace = []) {
         $entry = $this->module->getEntry(true);
         return $entry ? AuditError::logMessage($entry, $message, $code, $file, $line, $trace) : false;
     });
 }
 public function search($params)
 {
     $query = AuditError::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     // load the search form data and validate
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     // adjust the query by adding the filters
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'file', $this->file]);
     $query->andFilterWhere(['line' => $this->line]);
     return $dataProvider;
 }
 public function logException($exception)
 {
     try {
         $auditing = Auditing::current();
         // Make sure not to interfere with out of memory errors, there's not enough spare room to load everything
         if ($auditing && strncmp($exception->getMessage(), 'Allowed memory size of', 22)) {
             $entry = $auditing->getEntry(true);
             if ($entry) {
                 AuditError::log($entry, $exception);
                 $entry->finalize();
             }
         }
     } catch (\Exception $e) {
     }
     parent::logException($exception);
 }
 /**
  * @param Exception $exception
  */
 public function logException($exception)
 {
     try {
         // Make sure not to interfere with out of memory errors, there's not enough spare room to load everything
         if (strncmp($exception->getMessage(), 'Allowed memory size of', 22) == 0) {
             return;
         }
         /** @var Audit $audit */
         $audit = Yii::$app->getModule('audit');
         if ($audit) {
             $entry = $audit->getEntry(true);
             if ($entry) {
                 AuditError::log($entry, $exception);
                 $entry->finalize();
             }
         }
     } catch (\Exception $e) {
     }
     parent::logException($exception);
 }
 /**
  * Email errors to support email.
  */
 public function actionErrorEmail()
 {
     $email = Yii::$app->params['supportEmail'];
     // find all errors to email
     $batch = AuditError::find()->where(['emailed' => 0])->batch();
     foreach ($batch as $auditErrors) {
         /** @var AuditError $model */
         foreach ($auditErrors as $model) {
             // define params and message
             $url = ['audit/default/view', 'id' => $model->entry_id];
             $params = ['entry_id' => $model->entry_id, 'message' => $model->message, 'file' => $model->file, 'line' => $model->line, 'url' => Url::to($url), 'link' => Html::a(Yii::t('audit', 'view audit entry'), $url)];
             $message = ['subject' => Yii::t('audit', 'Audit Error in Audit Entry #{entry_id}', $params), 'text' => Yii::t('audit', '{message}' . "\n" . 'in {file} on line {line}.' . "\n" . '-- {url}', $params), 'html' => Yii::t('audit', '<b>{message}</b><br />in <i>{file}</i> on line <i>{line}</i>.<br/>-- {link}', $params)];
             // send email
             Yii::$app->mailer->compose()->setFrom([$email => 'Audit :: ' . Yii::$app->name])->setTo($email)->setSubject($message['subject'])->setTextBody($message['text'])->setHtmlBody($message['html'])->send();
             // mark as emailed
             $model->emailed = 1;
             $model->save(false, ['emailed']);
         }
     }
 }
 /**
  * @param Exception $exception
  */
 public function logException($exception)
 {
     try {
         $isMemoryError = strncmp($exception->getMessage(), 'Allowed memory size of', 22) === 0;
         $module = Audit::getInstance();
         if (!$module && !$isMemoryError) {
             // Only attempt to load the module if this isn't an out of memory error, not enough room otherwise
             $module = \Yii::$app->getModule(Audit::findModuleIdentifier());
         }
         if (!$module) {
             throw new \Exception('Audit module cannot be loaded');
         }
         $entry = $module->getEntry(!$isMemoryError);
         if ($entry) {
             AuditError::log($entry, $exception);
             $entry->finalize();
         }
     } catch (\Exception $e) {
         // if we catch an exception here, let it slide, we don't want recursive errors killing the script
     }
     parent::logException($exception);
 }
Exemple #10
0
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('audit', 'Audit Module');
$this->params['breadcrumbs'][] = $this->title;
$this->registerCss('canvas {width: 100% !important;height: 400px;}');
$dataSet = ['fillColor' => "rgba(151,187,205,0.5)", 'strokeColor' => "rgba(151,187,205,1)", 'pointColor' => "rgba(151,187,205,1)", 'pointStrokeColor' => "#fff"];
$options = ['height' => 400, 'width' => 400];
$days = [];
$count = ['entry' => [], 'trail' => [], 'javascript' => [], 'error' => []];
foreach (range(-6, 0) as $day) {
    $date = strtotime($day . 'days');
    $days[] = date('D: Y-m-d', $date);
    $count['entry'][] = AuditEntry::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count();
    $count['trail'][] = AuditTrail::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count();
    $count['mail'][] = AuditMail::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count();
    $count['javascript'][] = AuditJavascript::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count();
    $count['error'][] = AuditError::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count();
}
//fake data
//foreach ($count as $type => $data) {
//    foreach ($data as $k => $v) {
//        if (!$v) {
//            $v = $type == 'entry' ? rand(100, 1000) : rand(0, 100);
//            $count[$type][$k] = $v;
//        }
//    }
//}
?>
<div class="audit-index">

    <h1><?php 
echo Html::encode($this->title);
 /**
  * @inheritdoc
  */
 public function cleanup($maxAge = null)
 {
     $maxAge = $maxAge !== null ? $maxAge : $this->maxAge;
     if ($maxAge === null) {
         return false;
     }
     return AuditError::deleteAll(['<=', 'created', date('Y-m-d 23:59:59', strtotime("-{$maxAge} days"))]);
 }
Exemple #12
0
 /**
  * Returns all linked AuditError instances
  * (Called `linkedErrors()` to avoid confusion with the `getErrors()` method)
  * @return AuditError[]
  */
 public function getLinkedErrors()
 {
     return static::hasMany(AuditError::className(), ['audit_id' => 'id']);
 }
Exemple #13
0
    /**
     * Clean up the audit data according to the settings.
     * Can be handy if you are offloading the data somewhere and want to keep only the most recent entries readily available
     */
    public function truncate()
    {
        if ($this->maxAge === null) {
            return;
        }
        $entry = models\AuditEntry::tableName();
        $errors = models\AuditError::tableName();
        $data = models\AuditData::tableName();
        $javascript = models\AuditJavascript::tableName();
        $threshold = time() - $this->maxAge * 86400;
        models\AuditEntry::getDb()->createCommand(<<<SQL
DELETE FROM {$entry}, {$errors}, {$data}, {$javascript} USING {$entry}
  INNER JOIN {$errors} ON {$errors}.audit_id = {$entry}.id
  INNER JOIN {$data} ON {$data}.audit_id = {$entry}.id
  INNER JOIN {$javascript} ON {$javascript}.audit_id = {$entry}.id
  WHERE {$entry}.created < FROM_UNIXTIME({$threshold})
SQL
)->execute();
    }
<?php

use yii\helpers\Html;
use yii\helpers\VarDumper;
use yii\helpers\Url;
$formatter = \Yii::$app->formatter;
if (isset($request['error']) && isset($request['error'][1])) {
    $error = \bedezign\yii2\audit\models\AuditError::findOne($request['error'][1]);
    $request['error'] = Html::a('[' . $request['error'][0] . '] ' . $error->message, ['error/view', 'id' => $error->id]);
}
?>
<div class="table-responsive">
    <table class="table table-condensed table-bordered table-striped table-hover request-table" style="table-layout: fixed;">
        <thead>
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
        <?php 
foreach ($request as $name => $value) {
    ?>
            <tr>
                <th><?php 
    echo Html::encode(\yii\helpers\Inflector::humanize($name));
    ?>
</th>
                <td>
<?php 
    if (is_array($value) || is_object($value)) {
 public function up()
 {
     $this->addColumn(self::TABLE, 'emailed', 'int(11) NOT NULL AFTER trace');
     AuditError::updateAll(['emailed' => 1]);
     // set to 1 so we don't email all the old errors
 }
 /**
  * @return mixed
  * @throws \Exception
  */
 protected static function filterData()
 {
     return AuditEntry::getDb()->cache(function () {
         return AuditError::find()->distinct(true)->select(['hash', 'message', 'file'])->asArray()->all();
     }, 30);
 }