-
Notifications
You must be signed in to change notification settings - Fork 4
/
extension.driver.php
executable file
·76 lines (53 loc) · 2.01 KB
/
extension.driver.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
<?php
Class extension_db_sync extends Extension {
public static $meta_written = FALSE;
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'PostQueryExecution',
'callback' => 'log'
)
);
}
public function install() {
Symphony::Configuration()->set('enabled', 'yes', 'db_sync');
Symphony::Configuration()->write();
return TRUE;
}
public function uninstall() {
if (file_exists(MANIFEST . '/db_sync.sql')) unlink(MANIFEST . '/db_sync.sql');
Symphony::Configuration()->remove('db_sync');
Symphony::Configuration()->write();
}
public static function log($context) {
if(Symphony::Configuration()->get('enabled', 'db_sync') == 'no') return;
$query = $context['query'];
$tbl_prefix = Symphony::Configuration()->get('tbl_prefix', 'database');
/* FILTERS */
// only structural changes, no SELECT statements
if (!preg_match('/^(insert|update|delete|create|drop|alter|rename)/i', $query)) return;
// un-tracked tables (sessions, cache, authors)
if (preg_match("/{$tbl_prefix}(authors|cache|forgotpass|sessions)/i", $query)) return;
// content updates in tbl_entries (includes tbl_entries_fields_*)
if (preg_match('/^(insert|delete|update)/i', $query) && preg_match("/({$config->tbl_prefix}entries)/i", $query)) return;
$line = '';
if(self::$meta_written == FALSE) {
$line .= "\n" . '-- ' . date('Y-m-d H:i:s', time());
$author = Administration::instance()->Author;
if (isset($author)) $line .= ', ' . $author->getFullName();
$url = Administration::instance()->getCurrentPageURL();
if (!is_null($url)) $line .= ', ' . $url;
$line .= "\n";
self::$meta_written = TRUE;
}
$query = trim($query);
// append query delimeter if it doesn't exist
if (!preg_match('/;$/', $query)) $query .= ";";
$line .= $query . "\n";
$logfile = MANIFEST . '/db_sync.sql';
$handle = @fopen($logfile, 'a');
fwrite($handle, $line);
fclose($handle);
}
}