PageRenderTime 72ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete5/concrete/libraries/log.php

https://bitbucket.org/jmhmd/hrfreeclinic
PHP | 227 lines | 131 code | 36 blank | 60 comment | 16 complexity | 4e6e148f117fc15ccdb7798d9a3b0279 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  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() {return $this->timestamp;}
  26. /**
  27. * Returns a log entry by ID
  28. */
  29. public static function getByID($logID) {
  30. $db = Loader::db();
  31. $r = $db->Execute("select * from Logs where logID = ?", array($logID));
  32. if ($r) {
  33. $row = $r->FetchRow();
  34. $obj = new LogEntry();
  35. $obj->setPropertiesFromArray($row);
  36. return $obj;
  37. }
  38. }
  39. }
  40. /**
  41. * An object that represents a log entry dealing specifically with a database query. This item is populated by ADODB.
  42. * @package Utilities
  43. * @author Andrew Embler <andrew@concrete5.org>
  44. * @category Concrete
  45. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  46. * @license http://www.concrete5.org/license/ MIT License
  47. *
  48. */
  49. class DatabaseLogEntry extends LogEntry {
  50. public function getQuery() {return $this->query;}
  51. public function getParameters() {return $this->params;}
  52. public function getTrace() {return $this->tracer;}
  53. public static function getTotal() {
  54. $db = Loader::db();
  55. return $db->GetOne("select count(created) from adodb_logsql");
  56. }
  57. public static function clear() {
  58. $db = Loader::db();
  59. $db->Execute("delete from adodb_logsql");
  60. }
  61. public function getTimestamp() { return $this->created; }
  62. public static function getList($limit) {
  63. $entries = array();
  64. $db = Loader::db();
  65. $r = $db->GetAll("select sql1 as query, created, params, tracer from adodb_logsql order by created desc limit " . $limit);
  66. foreach($r as $row) {
  67. $dle = new DatabaseLogEntry();
  68. $dle->setPropertiesFromArray($row);
  69. $entries[] = $dle;
  70. }
  71. return $entries;
  72. }
  73. }
  74. /**
  75. * A library for dealing with searchable logs.
  76. * @package Utilities
  77. * @author Andrew Embler <andrew@concrete5.org>
  78. * @category Concrete
  79. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  80. * @license http://www.concrete5.org/license/ MIT License
  81. *
  82. */
  83. class Log {
  84. private $log;
  85. private $logfile;
  86. private $name;
  87. private $session = false;
  88. private $sessionText = null;
  89. private $isInternal = false;
  90. public function __construct($log = null, $session = false, $internal = false) {
  91. $th = Loader::helper('text');
  92. if ($log == null) {
  93. $log = '';
  94. }
  95. $this->log = $log;
  96. $this->name = $th->unhandle($log);
  97. $this->session = $session;
  98. $this->isInternal = $internal;
  99. }
  100. public function write($message) {
  101. $this->sessionText .= $message . "\n";
  102. if (!$this->session) {
  103. $this->close();
  104. }
  105. }
  106. /**
  107. * Removes all "custom" log entries - these are entries that an app owner has written and don't have a builtin C5 type
  108. */
  109. public function clearCustom() {
  110. $db = Loader::db();
  111. $db->Execute("delete from Logs where logIsInternal = 0");
  112. }
  113. public function clearInternal() {
  114. $db = Loader::db();
  115. $db->Execute("delete from Logs where logIsInternal = 1");
  116. }
  117. /**
  118. * Removes all log entries
  119. */
  120. public function clearAll() {
  121. $db = Loader::db();
  122. $db->Execute("delete from Logs");
  123. }
  124. public function close() {
  125. $v = array($this->log, $this->sessionText, $this->isInternal);
  126. $db = Loader::db();
  127. $db->Execute("insert into Logs (logType, logText, logIsInternal) values (?, ?, ?)", $v);
  128. $this->sessionText = '';
  129. }
  130. /**
  131. * Renames a log file and moves it to the log archive.
  132. */
  133. public function archive() {
  134. }
  135. /**
  136. * Returns the total number of entries matching this type
  137. */
  138. public static function getTotal($keywords, $type) {
  139. $db = Loader::db();
  140. if ($keywords != '') {
  141. $kw = 'and logText like ' . $db->quote('%' . $keywords . '%');
  142. }
  143. if ($type != false) {
  144. $v = array($type);
  145. $r = $db->GetOne('select count(logID) from Logs where logType = ? ' . $kw, $v);
  146. } else {
  147. $r = $db->GetOne('select count(logID) from Logs where 1=1 ' . $kw);
  148. }
  149. return $r;
  150. }
  151. /**
  152. * Returns a list of log entries
  153. */
  154. public static function getList($keywords, $type, $limit) {
  155. $db = Loader::db();
  156. if ($keywords != '') {
  157. $kw = 'and logText like ' . $db->quote('%' . $keywords . '%');
  158. }
  159. if ($type != false) {
  160. $v = array($type);
  161. $r = $db->Execute('select logID from Logs where logType = ? ' . $kw . ' order by timestamp desc limit ' . $limit, $v);
  162. } else {
  163. $r = $db->Execute('select logID from Logs where 1=1 ' . $kw . ' order by timestamp desc limit ' . $limit);
  164. }
  165. $entries = array();
  166. while ($row = $r->FetchRow()) {
  167. $entries[] = LogEntry::getByID($row['logID']);
  168. }
  169. return $entries;
  170. }
  171. /**
  172. * Returns an array of distinct log types
  173. */
  174. public static function getTypeList() {
  175. $db = Loader::db();
  176. $lt = $db->GetCol("select distinct logType from Logs");
  177. if (!is_array($lt)) {
  178. $lt = array();
  179. }
  180. return $lt;
  181. }
  182. public function getName() { return $this->name;}
  183. /**
  184. * Returns all the log files in the directory
  185. */
  186. public static function getLogs() {
  187. $db = Loader::db();
  188. $r = $db->GetCol('select distinct logType from Logs order by logType asc');
  189. return $r;
  190. }
  191. }