View source
<?php
function filelog_ui_admin_settings() {
$form['filelog_maxage'] = array(
'#type' => 'select',
'#title' => t('Discard log entries older than the following maximum age'),
'#default_value' => variable_get('filelog_maxage', 2592000),
'#options' => array(
0 => t('Forever'),
604800 => t('1 week'),
1209600 => t('2 weeks'),
2592000 => t('1 month'),
5184000 => t('2 months'),
7776000 => t('3 months'),
),
'#description' => t('This option does not affect the log file contents. The maximum age for rows to keep in the database. Older entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array(
'@cron' => url('admin/reports/status'),
)),
);
$last = variable_get('filelog_last_import', 0);
$form['filelog_import_frequency'] = array(
'#type' => 'select',
'#title' => t('Import new entries at the following frequency'),
'#default_value' => variable_get('filelog_import_frequency', 604800),
'#options' => array(
0 => t('Never'),
604800 => t('1 week'),
1209600 => t('2 weeks'),
2592000 => t('1 month'),
5184000 => t('2 months'),
7776000 => t('3 months'),
),
'#description' => t('Last import was on <strong>@date</strong>. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array(
'@date' => $last ? format_date($last, 'medium') : t('Never'),
'@cron' => url('admin/reports/status'),
)),
);
$form['filelog_check_base_url'] = array(
'#type' => 'checkbox',
'#title' => t('Check base URL on file import'),
'#default_value' => variable_get('filelog_check_base_url', 1),
'#description' => t('When enabled, only entries belonging to the current base URL will be imported.'),
);
$form['import'] = array(
'#type' => 'fieldset',
'#title' => t('Import / Re-import'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['import']['import_new'] = array(
'#type' => 'submit',
'#value' => t('Import new entries'),
'#submit' => array(
'filelog_ui_submit_import_new',
),
);
$form['import']['import_all'] = array(
'#type' => 'submit',
'#value' => t('Re-import all entries'),
'#submit' => array(
'filelog_ui_submit_import_all',
),
);
return system_settings_form($form);
}
function filelog_ui_submit_import_new($form, &$form_state) {
batch_set(array(
'title' => t('Importing new log entries'),
'operations' => array(
array(
'filelog_ui_import_entries',
array(),
),
array(
'cache_clear_all',
array(
'filelog_type_registry',
'cache',
FALSE,
),
),
),
'file' => drupal_get_path('module', 'filelog_ui') . '/filelog_ui.admin.inc',
));
}
function filelog_ui_submit_import_all($form, &$form_state) {
cache_clear_all('filelog_path_registry', 'cache');
db_query('DELETE FROM {filelog}');
batch_set(array(
'title' => t('Re-importing all log entries'),
'operations' => array(
array(
'filelog_ui_import_entries',
array(),
),
array(
'cache_clear_all',
array(
'filelog_type_registry',
'cache',
FALSE,
),
),
),
'file' => drupal_get_path('module', 'filelog_ui') . '/filelog_ui.admin.inc',
));
}
function filelog_ui_import_entries() {
if (!($dir = filelog_directory(FALSE))) {
return FALSE;
}
$cbr = variable_get('filelog_check_base_url', 1);
if (!($reg = cache_get('filelog_path_registry', 'cache'))) {
$reg = array();
}
else {
$reg = $reg->data;
}
foreach ($reg as $path => $info) {
if (!file_exists($path)) {
unset($reg[$path]);
}
}
if ($files = file_scan_directory($dir, '.+\\.ui-log(.*)?')) {
foreach ($files as $path => $info) {
$size = filesize($path);
$mtime = filemtime($path);
if (isset($reg[$path]) && $size == $reg[$path]['size'] && $mtime == $reg[$path]['mtime']) {
continue;
}
if (strrpos($path, '.gz', strlen($path) - 3) === FALSE) {
$open = 'fopen';
$get = 'fgets';
$eof = 'feof';
$close = 'fclose';
}
else {
$open = 'gzopen';
$get = 'gzgets';
$eof = 'gzeof';
$close = 'gzclose';
}
if ($size && ($fh = $open($path, 'r'))) {
try {
$idx = 0;
while (!$eof($fh)) {
if ($line = $get($fh, 4096)) {
_filelog_ui_import_entry($line, $cbr, $path, $idx);
}
$idx++;
}
} catch (Exception $e) {
$close($fh);
}
}
$reg[$path] = array(
'size' => $size,
'mtime' => $mtime,
);
}
}
cache_set('filelog_path_registry', $reg, 'cache');
variable_set('filelog_last_import', time());
}
function _filelog_ui_import_entry($line, $cbr, $path, $idx) {
global $base_url;
$line = trim($line, "\r\n\t");
$entry = explode('|', $line);
if (!is_numeric($entry[0])) {
$entry_base_url = array_shift($entry);
if ($cbr && $entry_base_url != $base_url) {
return FALSE;
}
}
$entry[5] = base64_decode($entry[5]);
$entry[6] = base64_decode($entry[6]);
$entry[7] = base64_decode($entry[7]);
$entry[8] = base64_decode($entry[8]);
if (isset($entry[9])) {
$vars = unserialize($entry[9]);
foreach ($vars as $key => $value) {
$vars[$key] = base64_decode($value);
}
$entry[9] = serialize($vars);
}
$wid = md5($path . (string) $idx . $line);
array_unshift($entry, $wid);
@db_query("INSERT INTO {filelog}\n (wid, timestamp, severity, type, uid, hostname, location, referer, link, message" . (isset($entry[10]) ? ", variables" : '') . ")\n VALUES\n ('%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s'" . (isset($entry[10]) ? ", '%s'" : '') . ")", $entry);
return TRUE;
}