/web/concrete/libraries/log.php

http://github.com/concrete5/concrete5 · PHP · 210 lines · 125 code · 30 blank · 55 comment · 21 complexity · 81dfae63ba7533bf5c012080e80b637c MD5 · raw file

  1. <?
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. * A library for dealing with searchable logs.
  5. * @package Utilities
  6. * @author Andrew Embler <andrew@concrete5.org>
  7. * @category Concrete
  8. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  9. * @license http://www.concrete5.org/license/ MIT License
  10. *
  11. */
  12. /**
  13. * An object that represents a log entry.
  14. * @package Utilities
  15. * @author Andrew Embler <andrew@concrete5.org>
  16. * @category Concrete
  17. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  18. * @license http://www.concrete5.org/license/ MIT License
  19. *
  20. */
  21. class LogEntry extends Object {
  22. public function getType() {return $this->logType;}
  23. public function getText() {return $this->logText;}
  24. public function getID() {return $this->logID;}
  25. public function getTimestamp($type = 'system') {
  26. if(ENABLE_USER_TIMEZONES && $type == 'user') {
  27. $dh = Loader::helper('date');
  28. $timestamp = $dh->getLocalDateTime($this->timestamp);
  29. } else {
  30. $timestamp = $this->timestamp;
  31. }
  32. return $timestamp;
  33. }
  34. /**
  35. * Returns a log entry by ID
  36. */
  37. public static function getByID($logID) {
  38. $db = Loader::db();
  39. $r = $db->Execute("select * from Logs where logID = ?", array($logID));
  40. if ($r) {
  41. $row = $r->FetchRow();
  42. $obj = new LogEntry();
  43. $obj->setPropertiesFromArray($row);
  44. return $obj;
  45. }
  46. }
  47. }
  48. /**
  49. * A library for dealing with searchable logs.
  50. * @package Utilities
  51. * @author Andrew Embler <andrew@concrete5.org>
  52. * @category Concrete
  53. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  54. * @license http://www.concrete5.org/license/ MIT License
  55. *
  56. */
  57. class Log {
  58. private $log;
  59. private $logfile;
  60. private $name;
  61. private $session = false;
  62. private $sessionText = null;
  63. private $isInternal = false;
  64. public function __construct($log = null, $session = true, $internal = false) {
  65. $th = Loader::helper('text');
  66. if ($log == null) {
  67. $log = '';
  68. }
  69. $this->log = $log;
  70. $this->name = $th->unhandle($log);
  71. $this->session = $session;
  72. $this->isInternal = $internal;
  73. }
  74. public function write($message) {
  75. $this->sessionText .= $message . "\n";
  76. if (!$this->session) {
  77. $this->close();
  78. }
  79. }
  80. public static function addEntry($message, $namespace = false) {
  81. if (!$namespace) {
  82. $namespace = t('debug');
  83. }
  84. $l = new Log($namespace, false);
  85. $l->write($message);
  86. }
  87. /**
  88. * Removes all "custom" log entries - these are entries that an app owner has written and don't have a builtin C5 type
  89. */
  90. public function clearCustom() {
  91. $db = Loader::db();
  92. $db->Execute("delete from Logs where logIsInternal = 0");
  93. }
  94. /**
  95. * Removes log entries by type- these are entries that an app owner has written and don't have a builtin C5 type
  96. * @param string $type Is a lowercase string that uses underscores instead of spaces, e.g. sent_emails
  97. */
  98. public function clearByType($type) {
  99. $db = Loader::db();
  100. $db->Execute("delete from Logs where logType = ?", array($type));
  101. }
  102. public function clearInternal() {
  103. $db = Loader::db();
  104. $db->Execute("delete from Logs where logIsInternal = 1");
  105. }
  106. /**
  107. * Removes all log entries
  108. */
  109. public function clearAll() {
  110. $db = Loader::db();
  111. $db->Execute("delete from Logs");
  112. }
  113. public function close() {
  114. $v = array($this->log, htmlentities($this->sessionText, ENT_COMPAT, APP_CHARSET), $this->isInternal);
  115. $db = Loader::db();
  116. $db->Execute("insert into Logs (logType, logText, logIsInternal) values (?, ?, ?)", $v);
  117. $this->sessionText = '';
  118. }
  119. /**
  120. * Renames a log file and moves it to the log archive.
  121. */
  122. public function archive() {
  123. }
  124. /**
  125. * Returns the total number of entries matching this type
  126. */
  127. public static function getTotal($keywords, $type) {
  128. $db = Loader::db();
  129. if ($keywords != '') {
  130. $kw = 'and logText like ' . $db->quote('%' . $keywords . '%');
  131. }
  132. if ($type != false) {
  133. $v = array($type);
  134. $r = $db->GetOne('select count(logID) from Logs where logType = ? ' . $kw, $v);
  135. } else {
  136. $r = $db->GetOne('select count(logID) from Logs where 1=1 ' . $kw);
  137. }
  138. return $r;
  139. }
  140. /**
  141. * Returns a list of log entries
  142. */
  143. public static function getList($keywords, $type, $limit) {
  144. $db = Loader::db();
  145. if ($keywords != '') {
  146. $kw = 'and logText like ' . $db->quote('%' . $keywords . '%');
  147. }
  148. if ($type != false) {
  149. $v = array($type);
  150. $r = $db->Execute('select logID from Logs where logType = ? ' . $kw . ' order by timestamp desc limit ' . $limit, $v);
  151. } else {
  152. $r = $db->Execute('select logID from Logs where 1=1 ' . $kw . ' order by timestamp desc limit ' . $limit);
  153. }
  154. $entries = array();
  155. while ($row = $r->FetchRow()) {
  156. $entries[] = LogEntry::getByID($row['logID']);
  157. }
  158. return $entries;
  159. }
  160. /**
  161. * Returns an array of distinct log types
  162. */
  163. public static function getTypeList() {
  164. $db = Loader::db();
  165. $lt = $db->GetCol("select distinct logType from Logs");
  166. if (!is_array($lt)) {
  167. $lt = array();
  168. }
  169. return $lt;
  170. }
  171. public function getName() { return $this->name;}
  172. /**
  173. * Returns all the log files in the directory
  174. */
  175. public static function getLogs() {
  176. $db = Loader::db();
  177. $r = $db->GetCol('select distinct logType from Logs order by logType asc');
  178. return $r;
  179. }
  180. }