-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.cradle.php
113 lines (99 loc) · 3.21 KB
/
.cradle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php //-->
/**
* This file is part of a package designed for the CradlePHP Project.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
require_once __DIR__ . '/package/events.php';
require_once __DIR__ . '/package/helpers.php';
require_once __DIR__ . '/src/events.php';
require_once __DIR__ . '/src/controller.php';
use Cradle\Package\System\Schema;
use Cradle\Http\Request\RequestInterface;
use Cradle\Http\Response\ResponseInterface;
$this->addLogger(function(
$message,
$request = null,
$response = null,
$type = null,
$table = null,
$id = null
) {
//argument test
if (!is_string($message)
|| !($request instanceof RequestInterface)
|| !($response instanceof ResponseInterface)
) {
return;
}
// let's ignore CLI
if (php_sapi_name() === 'cli') {
echo $message . PHP_EOL;
return;
}
if (is_null($type)) {
switch (true) {
case strpos($message, 'created') !== FALSE:
$type = 'create';
break;
case strpos($message, 'updated') !== FALSE:
$type = 'update';
break;
case strpos($message, 'restored') !== FALSE:
$type = 'restore';
break;
case strpos($message, 'removed') !== FALSE:
$type = 'remove';
break;
case strpos($message, 'imported') !== FALSE:
$type = 'import';
break;
}
}
$payload = $this->makePayload();
//record logs
$payload['request']
->setStage('schema', 'history')
->setStage('history_remote_address', $request->getServer('REMOTE_ADDR'))
->setStage('profile_id', $request->getSession('me', 'profile_id'))
->setStage('history_page', $request->getServer('REQUEST_URI'))
->setStage('history_activity', $message)
->setStage('history_type', $type)
->setStage('history_table_name', $table)
->setStage('history_table_id', $id);
$global = $this->package('global');
//try to get the log path from settings
$logPath = $global->config('settings', 'log_path');
// if log path is not set
if (!$logPath) {
// set default log path
$logPath = $global->path('root') . '/log';
// if relative path
} else if (strpos($logPath, '/') !== 0) {
// set absolute path
$logPath = $global->path('root') . '/' . $logPath;
}
//generate uniq file name
$filename = sprintf('%s/%s.json', $logPath, md5(uniqid()));
//if its not a directory
if (!is_dir(dirname($filename))) {
// create directory
mkdir(dirname($filename), 0777);
}
//if directory is writable
if (is_writable(dirname($filename))) {
// as the name says, put contents in a file
file_put_contents($filename, json_encode([
'request' => $request->get(),
'response' => $response->get(),
]));
//record logs
$payload['request']->setStage('history_path', basename($filename));
}
$this->trigger(
'system-model-create',
$payload['request'],
$payload['response']
);
});