helper_log.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: helper_log.php 28822 2012-03-14 06:35:55Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class helper_log {
  12. public static function runlog($file, $message, $halt=0) {
  13. global $_G;
  14. $nowurl = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
  15. $log = dgmdate($_G['timestamp'], 'Y-m-d H:i:s')."\t".$_G['clientip']."\t$_G[uid]\t{$nowurl}\t".str_replace(array("\r", "\n"), array(' ', ' '), trim($message))."\n";
  16. helper_log::writelog($file, $log);
  17. if($halt) {
  18. exit();
  19. }
  20. }
  21. public static function writelog($file, $log) {
  22. global $_G;
  23. $yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G['setting']['timeoffset']);
  24. $logdir = DISCUZ_ROOT.'./data/log/';
  25. $logfile = $logdir.$yearmonth.'_'.$file.'.php';
  26. if(@filesize($logfile) > 2048000) {
  27. $dir = opendir($logdir);
  28. $length = strlen($file);
  29. $maxid = $id = 0;
  30. while($entry = readdir($dir)) {
  31. if(strpos($entry, $yearmonth.'_'.$file) !== false) {
  32. $id = intval(substr($entry, $length + 8, -4));
  33. $id > $maxid && $maxid = $id;
  34. }
  35. }
  36. closedir($dir);
  37. $logfilebak = $logdir.$yearmonth.'_'.$file.'_'.($maxid + 1).'.php';
  38. @rename($logfile, $logfilebak);
  39. }
  40. if($fp = @fopen($logfile, 'a')) {
  41. @flock($fp, 2);
  42. if(!is_array($log)) {
  43. $log = array($log);
  44. }
  45. foreach($log as $tmp) {
  46. fwrite($fp, "<?PHP exit;?>\t".str_replace(array('<?', '?>'), '', $tmp)."\n");
  47. }
  48. fclose($fp);
  49. }
  50. }
  51. public static function useractionlog($uid, $action) {
  52. $uid = intval($uid);
  53. if(empty($uid) || empty($action)) {
  54. return false;
  55. }
  56. $action = getuseraction($action);
  57. C::t('common_member_action_log')->insert(array('uid' => $uid, 'action' => $action, 'dateline' => TIMESTAMP));
  58. return true;
  59. }
  60. public static function getuseraction($var) {
  61. $value = false;
  62. $ops = array('tid', 'pid', 'blogid', 'picid', 'doid', 'sid', 'aid', 'uid_cid', 'blogid_cid', 'sid_cid', 'picid_cid', 'aid_cid', 'topicid_cid', 'pmid');
  63. if(is_numeric($var)) {
  64. $value = isset($ops[$var]) ? $ops[$var] : false;
  65. } else {
  66. $value = array_search($var, $ops);
  67. }
  68. return $value;
  69. }
  70. }
  71. ?>