-
Notifications
You must be signed in to change notification settings - Fork 5
/
sqlite.php
222 lines (214 loc) · 10.2 KB
/
sqlite.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use SQLite3;
use ReflectionProperty;
class SqlitePlugin extends Plugin
{
protected $sqlite;
const ERROR = 2; // binary flags for each log type
const SELECT = 4;
const INSERT = 8;
const UPDATE = 16;
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$route = $this->config->get('plugins.sqlite.database_route');
$dbname = $this->config->get('plugins.sqlite.database_name');
$path = $this->grav['locator']->findResource("user://$route", true);
// path is also used for error logging, so there must be a valid route in case user supplied route fails.
$this->sqlite['path'] = $path?: $this->grav['locator']->findResource("user://data/sqlite", true);
$this->sqlite['logging'] = $this->config->get('plugins.sqlite.logging') * // is either 0 or 1
( $this->config->get('plugins.sqlite.all_logging') ? (self::ERROR+self::SELECT+self::INSERT+self::UPDATE)
: ( $this->config->get('plugins.sqlite.error_logging') * self::ERROR
+ $this->config->get('plugins.sqlite.select_logging') * self::SELECT
+ $this->config->get('plugins.sqlite.insert_logging') * self::INSERT
+ $this->config->get('plugins.sqlite.update_logging') * self::UPDATE
)
);
$this->sqlite['extraSecurity'] = $this->config->get('plugins.sqlite.extra_security');
$dbloc = $path . DS . $dbname;
if ( file_exists($dbloc) ) {
$this->sqlite['db'] = new SQLite3($dbloc);
$this->sqlite['db']->enableExceptions(true);
} else {
$this->sqlite['error'] = "No database found at --user://$route/$dbname--";
}
$this->grav['sqlite'] = $this->sqlite;
$this->enable([
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths',0],
'onFormProcessed' => ['onFormProcessed', 0],
'onPageContentRaw' => ['onPageContentRaw', 0]
]);
}
public function onPageContentRaw() {
// Not called if page cached.
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
$this->active = false;
return;
}
if ( ! $this->sqlite['extraSecurity'] ) return; // only continue if extraSecurity is enabled
$page = $this->grav['page'];
// is there explicit permission for this page?
$frontmatter = $page->header();
if ( property_exists($frontmatter, 'sqliteSelect') AND $frontmatter->sqliteSelect !== 'allow' ) {
return;
}
// extra security is on, so change every occurence of '[sql' to '[sql-sec'
$raw = $page->getRawContent();
$processed = str_replace( [ '[sql' , '[/sql' ], [ '[sqlSEC' , '[/sqlSEC' ], $raw );
$page->setRawContent( $processed );
return;
}
public function onFormProcessed(Event $event)
{
if ( isset($this->grav['sqlite']['error']) && $this->grav['sqlite']['error'] ) {
$this->log(self::ERROR,$this->grav['sqlite']['error']);
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.DATABASE_ERROR']), $this->grav['sqlite']['error'])
]));
$event->stopPropagation();
return;
}
$action = $event['action'];
$params = $event['params'];
$form = $event['form'];
switch ($action) {
case 'sql-insert':
$data = $form->value()->toArray();
if ( isset($params['ignore'])) {
foreach ( $params['ignore'] as $k ) unset( $data[$k] );
}
$fields = '';
$values = '';
$nxt = false;
foreach ( $data as $field => $value ) {
// remove fields associated with Form plugins
if ( preg_match('/^\\_|form\\-nonce/', $field ) ) continue; // next iteration if error (false) in match, or match succeeds.
$fields .= ( $nxt ? ',' : '') . $field;
$values .= ( $nxt ? ',' : '' ) . '"' . $value . '"' ;
$nxt = true;
}
if (isset($data['where'])) {
unset($data['where']); // dont want it polluting UPDATE as a field. Should be ignored
}
$set = 'SET ';
$nxt = false;
foreach ( $data as $field => $value ) {
$set .= ( $nxt ? ', ' : '') ;
$set .= $field . '="' . $value . '"' ;
$nxt = true;
}
$sql ="INSERT INTO {$params['table']} ( $fields ) VALUES ( $values )";
$this->log(self::INSERT,$sql);
$db = $this->grav['sqlite']['db'];
try {
$db->exec($sql) ;
} catch ( \Exception $e ) {
$msg = $e->getMessage();
if ( stripos($msg, 'unique') !== false ) {
$msg .= $this->grav['language']->translate(['PLUGIN_SQLITE.UNIQUE_FIELD_ERROR']);
} else {
$msg .= $this->grav['language']->translate(['PLUGIN_SQLITE.OTHER_SQL_ERROR']) . "<BR>$sql";
}
$this->log(self::ERROR,$msg);
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => $msg
]));
$event->stopPropagation();
}
break;
case 'sql-update':
$data = $form->value()->toArray();
if ( isset($params['ignore']) ) {
foreach ( $params['ignore'] as $k ) unset( $data[$k] );
}
if ( ! isset( $params['where'] ) and ! isset($data['where'])) {
// where expression is mandatory, so fail if not set
$this->log(self::ERROR,$this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_WHERE']));
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => $this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_WHERE'])
]));
$event->stopPropagation();
break;
}
if (isset($data['where'])) {
// priority to where in form
$where = $data['where'];
unset($data['where']); // dont want it polluting UPDATE as a field
} else {
$where = $params['where'];
}
// allows for use of inpage twig
$where = $this->grav['twig']->processString($where);
$set = 'SET ';
$nxt = false;
foreach ( $data as $field => $value ) {
$set .= ( $nxt ? ', ' : '') ;
$set .= $field . '="' . $value . '"' ;
$nxt = true;
}
$sql ="UPDATE {$params['table']} $set WHERE $where";
$this->log(self::UPDATE,$sql);
$db = $this->grav['sqlite']['db'];
try {
$db->exec($sql) ;
} catch ( \Exception $e ) {
$this->log(self::ERROR,sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_ERROR']),$e->getMessage()));
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_ERROR']),$e->getMessage())
]));
$event->stopPropagation();
}
break;
}
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Initialize configuration
* @param Event $e
*/
public function onShortcodeHandlers(Event $e)
{
$this->grav['shortcode']->registerAllShortcodes(__DIR__.'/shortcodes');
}
public function log($type, $msg) {
$log_val =$this->grav['sqlite']['logging'];
if ( $log_val == 0 ) return;
$path = $this->grav['sqlite']['path'] . DS . 'sqlite.html';
$datafh = File::instance($path);
if ( ($log_val & self::ERROR) && ($type & self::ERROR)
|| ($log_val & self::INSERT) && ($type & self::INSERT)
|| ($log_val & self::UPDATE) && ($type & self::UPDATE)
) {
if ( file_exists($path) ) {
$datafh->save($datafh->content() . '<br><span style="color:blue">' . date('Y-m-d:H:i') . '</span>: ' . $msg);
} else {
$datafh->save('<span style="color:blue">' . date('Y-m-d:H:i') . '</span>: ' . $msg);
chmod($path, 0664);
}
}
}
}