PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/log.plugin.php

http://github.com/haugstrup/filethingie
PHP | 455 lines | 321 code | 44 blank | 90 comment | 56 complexity | 6a1a17573b27e469e0ec14b3caeaa12a MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Log plugin for File Thingie.
  5. * Author: Andreas Haugstrup Pedersen, Copyright 2009, All Rights Reserved
  6. *
  7. * Must be loaded after the db plugin.
  8. */
  9. define('FT_LOG_EMERG', 0);
  10. define('FT_LOG_ALERT', 1);
  11. define('FT_LOG_CRITICAL', 2);
  12. define('FT_LOG_ERROR', 3);
  13. define('FT_LOG_WARNING', 4);
  14. define('FT_LOG_NOTICE', 5);
  15. define('FT_LOG_INFO', 5);
  16. define('FT_LOG_DEBUG', 7);
  17. /**
  18. * Implementation of hook_info.
  19. */
  20. function ft_log_info() {
  21. return array(
  22. 'name' => 'Log: Enable logging functionality',
  23. 'settings' => array(
  24. 'viewlogs' => array(
  25. 'default' => FALSE,
  26. 'description' => t('Can user view and prune logs?'),
  27. ),
  28. ),
  29. );
  30. }
  31. function ft_log_severity_levels() {
  32. return array(
  33. FT_LOG_EMERG => t('emergency'),
  34. FT_LOG_ALERT => t('alert'),
  35. FT_LOG_CRITICAL => t('critical'),
  36. FT_LOG_ERROR => t('error'),
  37. FT_LOG_WARNING => t('warning'),
  38. FT_LOG_NOTICE => t('notice'),
  39. FT_LOG_INFO => t('info'),
  40. FT_LOG_DEBUG => t('debug'),
  41. );
  42. }
  43. /**
  44. * Implementation of hook_secondary_menu.
  45. */
  46. function ft_log_secondary_menu() {
  47. global $ft;
  48. if (isset($ft['plugins']['log']['settings']['viewlogs']) && $ft['plugins']['log']['settings']['viewlogs'] === TRUE) {
  49. return ft_make_link(t('[logs]'), "act=log", t("View logs"));
  50. }
  51. return '';
  52. }
  53. /**
  54. * Implementation of hook_init.
  55. */
  56. function ft_log_init() {
  57. global $ft;
  58. // Check if DB plugin is loaded.
  59. if (ft_plugin_exists('db')) {
  60. // Check if we need to create new table.
  61. $sql = "CREATE TABLE log (
  62. id INTEGER PRIMARY KEY,
  63. type TEXT NOT NULL,
  64. message TEXT NOT NULL,
  65. location TEXT NOL NULL,
  66. timestamp DATE NOT NULL,
  67. user TEXT NOT NULL,
  68. hostname TEXT NOT NULL,
  69. severity INTEGER NOT NULL
  70. )";
  71. ft_db_install_table('log', $sql);
  72. }
  73. }
  74. /**
  75. * Implementation of hook_download.
  76. */
  77. function ft_log_download($dir, $filename) {
  78. ft_log_insert('download', t('File downloaded.'), FT_LOG_INFO, $dir . '/' . $filename);
  79. }
  80. /**
  81. * Implementation of hook_upload.
  82. */
  83. function ft_log_upload($dir, $filename) {
  84. ft_log_insert('upload', t('File uploaded.'), FT_LOG_INFO, $dir . '/' . $filename);
  85. }
  86. /**
  87. * Implementation of hook_loginfail.
  88. */
  89. function ft_log_loginfail($username) {
  90. ft_log_insert('user', t('Failed login attempt for @user.', array('@user' => $username)), FT_LOG_NOTICE);
  91. }
  92. /**
  93. * Implementation of hook_loginsuccess.
  94. */
  95. function ft_log_loginsuccess($username) {
  96. ft_log_insert('user', t('@user logged in.', array('@user' => $username)), FT_LOG_INFO);
  97. }
  98. /**
  99. * Implementation of hook_logout.
  100. */
  101. function ft_log_logout($username) {
  102. ft_log_insert('user', t('@user logged out.', array('@user' => $username)), FT_LOG_INFO);
  103. }
  104. /**
  105. * Get list of types in the DB.
  106. */
  107. function ft_log_get_types() {
  108. global $ft;
  109. static $types;
  110. if (!is_array($types)) {
  111. $sql = "SELECT DISTINCT(type) as type FROM log";
  112. $result = $ft['db']['link']->query($sql);
  113. $types = array();
  114. if ($result) {
  115. foreach($result as $row) {
  116. $types[] = $row['type'];
  117. }
  118. }
  119. }
  120. return $types;
  121. }
  122. /**
  123. * Get oldest and newest log item.
  124. */
  125. function ft_log_get_boundaries() {
  126. global $ft;
  127. $sql = "SELECT MIN(timestamp) as oldest, MAX(timestamp) as newest FROM log";
  128. $result = $ft['db']['link']->query($sql);
  129. if ($result) {
  130. foreach ($result as $row) {
  131. // $ft['db']['link']->closeCursor();
  132. return $row;
  133. }
  134. }
  135. return FALSE;
  136. }
  137. /**
  138. * Get a list of date <option> tags.
  139. */
  140. function ft_log_get_date_options($type) {
  141. for ($i=1;$i<=31;$i++) {
  142. $str .= '<option ' . ($i == $_GET[$type] ? 'selected="selected"' : '') . '>'.$i.'</option>';
  143. }
  144. return $str;
  145. }
  146. /**
  147. * Format a month string for the filters.
  148. */
  149. function ft_log_format_month_option($year, $month) {
  150. return date('F Y', mktime(0, 0, 0, $month, 1, $year));
  151. }
  152. /**
  153. * Get a list of date <option> tags.
  154. */
  155. function ft_log_get_month_options($month_min, $month_max, $year_min, $year_max, $type) {
  156. for($i=$year_min;$i<=$year_max;$i++) {
  157. if ($i == $year_min) {
  158. if ($year_min != $year_max) {
  159. for($j=$month_min;$j<=12;$j++) {
  160. $str .= '<option value="'.$i.'-'.$j.'" ' . ($i.'-'.$j == $_GET[$type] ? 'selected="selected"' : '') . '>'.ft_log_format_month_option($i, $j).'</option>';
  161. }
  162. }
  163. else {
  164. for($j=$month_min;$j<=$month_max;$j++) {
  165. $str .= '<option value="'.$i.'-'.$j.'"' . ($i.'-'.$j == $_GET[$type] ? 'selected="selected"' : '') . '>'.ft_log_format_month_option($i, $j).'</option>';
  166. }
  167. }
  168. }
  169. elseif ($i == $year_max) {
  170. for($j=1;$j<=$month_max;$j++) {
  171. $str .= '<option value="'.$i.'-'.$j.'"' . ($i.'-'.$j == $_GET[$type] ? 'selected="selected"' : '') . '>'.ft_log_format_month_option($i, $j).'</option>';
  172. }
  173. }
  174. else {
  175. // Intermediate year. Loop through all months.
  176. for($j=1;$j<=12;$j++) {
  177. $str .= '<option value="'.$i.'-'.$j.'"' . ($i.'-'.$j == $_GET[$type] ? 'selected="selected"' : '') . '>'.ft_log_format_month_option($i, $j).'</option>';
  178. }
  179. }
  180. }
  181. return $str;
  182. }
  183. /**
  184. * Do log query.
  185. */
  186. function ft_log_do_query($type = '', $from_day = '', $from = '', $to_day = '', $to = '') {
  187. global $ft;
  188. $items = array();
  189. $types = ft_log_get_types();
  190. // Type filter
  191. $wheres = array();
  192. if (!empty($type) && in_array($type, $types)) {
  193. $wheres[] = 'type = '.$ft['db']['link']->quote($type);
  194. }
  195. // From date filter
  196. if (!empty($from)) {
  197. $from = explode('-', $from);
  198. $day = '01';
  199. if (is_numeric($from_day)) {
  200. $day = $from_day;
  201. $day = str_pad($day, 2, '0', STR_PAD_LEFT);
  202. }
  203. $from[1] = str_pad($from[1], 2, '0', STR_PAD_LEFT);
  204. $wheres[] = 'timestamp >= ' . $from[0] . '-' . $from[1] . '-' . $day;
  205. }
  206. // To date filter
  207. if (!empty($to)) {
  208. $to = explode('-', $to);
  209. $to_day = '31';
  210. if (is_numeric($to_day)) {
  211. $to_day = $to_day;
  212. $to_day = str_pad($to_day, 2, '0', STR_PAD_LEFT);
  213. }
  214. $to[1] = str_pad($to[1], 2, '0', STR_PAD_LEFT);
  215. $wheres[] = 'timestamp <= ' . $to[0] . '-' . $to[1] . '-' . $to_day;
  216. }
  217. if (count($wheres) > 0) {
  218. $sql = "SELECT * FROM log WHERE ".implode(' AND ', $wheres)." ORDER BY timestamp DESC";
  219. // print $sql;
  220. }
  221. else {
  222. // No filters, load everything.
  223. $sql = "SELECT * FROM log ORDER BY timestamp DESC";
  224. }
  225. $result = $ft['db']['link']->query($sql);
  226. if ($result) {
  227. foreach($result as $row) {
  228. $items[] = array(
  229. 'type' => $row['type'],
  230. 'message' => $row['message'],
  231. 'location' => $row['location'],
  232. 'timestamp' => $row['timestamp'],
  233. 'user' => $row['user'],
  234. );
  235. }
  236. }
  237. return $items;
  238. }
  239. /**
  240. * Implementation of hook_page.
  241. */
  242. function ft_log_page($act) {
  243. global $ft;
  244. $str = '';
  245. if ($act == 'log' && isset($ft['plugins']['log']['settings']['viewlogs']) && $ft['plugins']['log']['settings']['viewlogs'] === TRUE) {
  246. $types = ft_log_get_types();
  247. $boundaries = ft_log_get_boundaries();
  248. $month_min = date('m', strtotime($boundaries['oldest']));
  249. $month_max = date('m', strtotime($boundaries['newest']));
  250. $year_min = date('Y', strtotime($boundaries['oldest']));
  251. $year_max = date('Y', strtotime($boundaries['newest']));
  252. $str = "<h2>".t('Log')."</h2>";
  253. $str .= '<div>';
  254. // Filters.
  255. $str .= '<form action="'.ft_get_self().'?act=log" method="get">';
  256. $str .= '<div>';
  257. $str .= ' <label for="type">'.t('Show').' </label>';
  258. $str .= '<select id="type" name="type">';
  259. $str .= '<option value="">All types</option>';
  260. foreach ($types as $type) {
  261. $str .= '<option value="' . $type . '" ' . ($type == $_GET['type'] ? 'selected="selected"' : '') . '>' . $type . '</option>';
  262. }
  263. $str .= '</select> ';
  264. $str .= '<label for="from">'.t('from:').' </label>';
  265. $str .= '<select id="from_day" name="from_day">';
  266. $str .= '<option value="">--</option>';
  267. $str .= ft_log_get_date_options('from_day');
  268. $str .= '</select> ';
  269. $str .= '<select id="from" name="from">';
  270. $str .= '<option value="">Any month</option>';
  271. $str .= ft_log_get_month_options($month_min, $month_max, $year_min, $year_max, 'from');
  272. $str .= '</select>';
  273. $str .= ' <label for="to">'.t('to:').' </label>';
  274. $str .= '<select id="to_day" name="to_day">';
  275. $str .= '<option value="">--</option>';
  276. $str .= ft_log_get_date_options('to_day');
  277. $str .= '</select> ';
  278. $str .= '<select id="to" name="to">';
  279. $str .= '<option value="">Any month</option>';
  280. $str .= ft_log_get_month_options($month_min, $month_max, $year_min, $year_max, 'to');
  281. $str .= '</select> ';
  282. $str .= '<input type="submit" name="ft_submit" value="'.t('Show').'">';
  283. $str .= '<input type="hidden" name="act" value="log">';
  284. $str .= '</div>';
  285. $str .= '</form>';
  286. $str .= '<table id="log_table" class="tablesorter"><thead><tr><th>'.t('Type').'</th><th>'.t('Message').'</th><th>'.t('Location').'<th>'.t('Timestamp').'</th></tr></thead><tbody>';
  287. $items = ft_log_do_query($_GET['type'], $_GET['from_day'], $_GET['from'], $_GET['to_day'], $_GET['to']);
  288. foreach ($items as $row) {
  289. $str .= '<tr><td>' . $row['type'] . '</td><td>' . htmlentities($row['message']) . '</td><td>' . htmlentities($row['location']) . '</td><td>' . $row['timestamp'] . '</td></tr>';
  290. }
  291. $str .= '</tbody></table>';
  292. $str .= '<form action="'.ft_get_self().'" method="post" accept-charset="utf-8" style="margin-left:300px;"><div><input type="submit" value="'.t('Delete records older than 7 months').'"><input type="hidden" name="act" value="log_prune" /> ';
  293. $query = 'act=log_csv&type='.$_GET['type'].'&from_day='.$_GET['from_day'].'&from='.$_GET['from'].'&to_day='.$_GET['to_day'].'&to='.$_GET['to'];
  294. $str .= ft_make_link(t('Download CSV report'), $query, t("Download CSV report of current view"));
  295. $str .= '</div></form>';
  296. $str .= '</div>';
  297. }
  298. return $str;
  299. }
  300. /**
  301. * Implementation of hook_add_css.
  302. */
  303. function ft_log_add_css() {
  304. return ".headerSortUp, .headerSortDown {background:#c30}
  305. #log_table {width:660px}";
  306. }
  307. /**
  308. * Implementation of hook_action.
  309. */
  310. function ft_log_action($act) {
  311. global $ft;
  312. // Prune old records.
  313. if ($act == 'log_prune' && isset($ft['plugins']['log']['settings']['viewlogs']) && $ft['plugins']['log']['settings']['viewlogs'] === TRUE) {
  314. $offset = date('Y-m-d', strtotime('-7 months', gmmktime()));
  315. $sql = "DELETE FROM log WHERE timestamp <= '".$ft['db']['link']->quote($offset)."'";
  316. $result = $ft['db']['link']->query($sql);
  317. ft_set_message(t('Log pruned'));
  318. // Redirect.
  319. ft_redirect("act=log");
  320. }
  321. // View CSV log.
  322. if ($act == 'log_csv' && isset($ft['plugins']['log']['settings']['viewlogs']) && $ft['plugins']['log']['settings']['viewlogs'] === TRUE) {
  323. header('Content-type: text/plain;charset=UTF-8');
  324. $headers = array(t('Type'), t('Message'), t('Location'), t('Timestamp'), t('User'));
  325. $items = ft_log_do_query($_GET['type'], $_GET['from_day'], $_GET['from'], $_GET['to_day'], $_GET['to']);
  326. print str_putcsv($headers, ';') . "\n";
  327. foreach ($items as $row) {
  328. print str_putcsv($row, ';') . "\n";
  329. }
  330. exit;
  331. }
  332. }
  333. if(!function_exists('str_putcsv')) {
  334. function str_putcsv($input, $delimiter = ',', $enclosure = '"') {
  335. // Open a memory "file" for read/write...
  336. $fp = fopen('php://temp', 'r+');
  337. // ... write the $input array to the "file" using fputcsv()...
  338. fputcsv($fp, $input, $delimiter, $enclosure);
  339. // ... rewind the "file" so we can read what we just wrote...
  340. rewind($fp);
  341. // ... read the entire line into a variable...
  342. $data = fgets($fp);
  343. // ... close the "file"...
  344. fclose($fp);
  345. // ... and return the $data to the caller, with the trailing newline from fgets() removed.
  346. return rtrim( $data, "\n" );
  347. }
  348. }
  349. /**
  350. * Implementation of hook_add_js_file.
  351. */
  352. function ft_log_add_js_file() {
  353. $return = array();
  354. // Only add JS when we are on the log page.
  355. if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'log') {
  356. $return[] = PLUGINDIR . '/tablesorter/jquery.tablesorter.min.js';
  357. }
  358. return $return;
  359. }
  360. /**
  361. * Implementation of hook_add_js_call.
  362. */
  363. function ft_log_add_js_call() {
  364. global $ft;
  365. $return = '';
  366. // Only add JS when we're on the log page.
  367. if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'log') {
  368. // Unbind save action and rebind with a tinymce specific version.
  369. $return .= '$("#log_table").tablesorter();';
  370. }
  371. return $return;
  372. }
  373. /**
  374. * Insert a log item.
  375. */
  376. function ft_log_insert($type, $message, $severity = FT_LOG_NOTICE, $location = '', $user = '') {
  377. global $ft;
  378. if ($user == '') {
  379. $user = $_SESSION['ft_user_'.MUTEX];
  380. }
  381. $datetime = date('Y-m-d H:i:s', gmmktime());
  382. $query = $ft['db']['link']->prepare("INSERT INTO log (
  383. type,
  384. message,
  385. location,
  386. timestamp,
  387. user,
  388. hostname,
  389. severity
  390. ) VALUES (
  391. :type,
  392. :message,
  393. :location,
  394. :timestamp,
  395. :user,
  396. :hostname,
  397. :severity
  398. )");
  399. if (!$query) {
  400. print_r($ft['db']['link']->errorInfo());
  401. }
  402. $query->execute(array(
  403. ':type' => $type,
  404. ':message' => $message,
  405. ':location' => $location,
  406. ':timestamp' => $datetime,
  407. ':user' => $user,
  408. ':hostname' => $_SERVER['REMOTE_ADDR'],
  409. ':severity' => $severity
  410. ));
  411. }