PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htroot/assets/snippets/ajaxSearch/classes/ajaxSearchLog.class.inc.php

https://github.com/gunf/novo-isaak.local
PHP | 174 lines | 112 code | 15 blank | 47 comment | 17 complexity | 7a5b6c60cec767fe269dcf8e1815b565 MD5 | raw file
  1. <?php
  2. /* -----------------------------------------------------------------------------
  3. * Snippet: AjaxSearch
  4. * -----------------------------------------------------------------------------
  5. * @package AjaxSearchLog
  6. *
  7. * @author Coroico - www.modx.wangba.fr
  8. * @version 1.9.2
  9. * @date 05/12/2010
  10. *
  11. * Purpose:
  12. * The AjaxSearchLog class contains all functions used to Log AjaxSearch requests
  13. *
  14. */
  15. define('CMT_MAX_LENGTH', 100);
  16. define('CMT_MAX_LINKS', 3);
  17. define('LOG_TABLE_NAME', 'ajaxsearch_log');
  18. define('PURGE', 200);
  19. define('COMMENT_JSDIR', 'js/comment');
  20. class AjaxSearchLog {
  21. // public variables
  22. var $log = '0:0';
  23. var $logcmt;
  24. // private variables
  25. var $_tbName;
  26. var $_purge;
  27. /*
  28. * Constructs the ajaxSearchLog object
  29. *
  30. * @access public
  31. * @param string $log log parameter
  32. */
  33. function AjaxSearchLog($log='0:0') {
  34. global $modx;
  35. $this->_tbName = $modx->getFullTableName(LOG_TABLE_NAME);
  36. $asLog_array = explode(':', $log);
  37. $this->log = (int)$asLog_array[0];
  38. if ($this->log > 0 && $this->log < 3) {
  39. $this->_purge = isset($asLog_array[2]) ? (int)$asLog_array[2] : PURGE;
  40. if ($this->_purge < 0) $this->_purge = PURGE;
  41. $this->_initLogTable();
  42. $this->logcmt = isset($asLog_array[1]) ? (int)$asLog_array[1] : 0;
  43. if ($this->logcmt) {
  44. $jsInclude = AS_SPATH . COMMENT_JSDIR . '/ajaxSearchCmt.js';
  45. $modx->regClientStartupScript($jsInclude);
  46. }
  47. } else {
  48. $this->log = 0;
  49. }
  50. }
  51. /*
  52. * Create the ajaxSearch log table if needed
  53. */
  54. function _initLogTable() {
  55. global $modx;
  56. $db = $modx->db->config['dbase'];
  57. $tbn = $modx->db->config['table_prefix'] . LOG_TABLE_NAME;
  58. if (!$this->_existLogTable($db, $tbn)) {
  59. $SQL_CREATE_TABLE = "CREATE TABLE " . $this->_tbName . " (
  60. `id` smallint(5) NOT NULL auto_increment,
  61. `searchstring` varchar(128) NOT NULL,
  62. `nb_results` smallint(5) NOT NULL,
  63. `results` mediumtext,
  64. `comment` mediumtext,
  65. `as_call` mediumtext,
  66. `as_select` mediumtext,
  67. `date` timestamp(12) NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  68. `ip` varchar(255) NOT NULL,
  69. PRIMARY KEY (`id`)
  70. ) ENGINE=MyISAM;";
  71. if (!$modx->db->query($SQL_CREATE_TABLE)) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. }
  77. /*
  78. * Check if the table exists or not
  79. */
  80. function _existLogTable($db, $tbName) {
  81. global $modx;
  82. $SHOW_TABLES = "SHOW TABLES FROM $db LIKE '$tbName';";
  83. $exec = $modx->db->query($SHOW_TABLES);
  84. return $modx->db->getRecordCount($exec);
  85. }
  86. /*
  87. * Write a log record in database
  88. *
  89. * @access public
  90. * @param array $rs record set
  91. * return the id of the record logged
  92. */
  93. function setLogRecord($rs) {
  94. global $modx;
  95. if ($this->_purge) $this->_purgeLogs();
  96. $asString = mysql_real_escape_string($rs['searchString']);
  97. $asNbResults = $rs['nbResults'];
  98. $asResults = trim($rs['results']);
  99. $asCmt = '';
  100. $asCall = $rs['asCall'];
  101. $asSelect = $rs['asSelect'];
  102. $asIp = $_SERVER['REMOTE_ADDR'];
  103. $INSERT_RECORD = "INSERT INTO " . $this->_tbName . " (
  104. searchstring, nb_results, results, comment, as_call, as_select, ip
  105. ) VALUES ('$asString','$asNbResults','$asResults','$asCmt','$asCall','$asSelect','$asIp')";
  106. $modx->db->query($INSERT_RECORD);
  107. $lastid = $modx->db->getInsertId();
  108. return $lastid;
  109. }
  110. /*
  111. * Purge the log table
  112. */
  113. function _purgeLogs() {
  114. global $modx;
  115. $sql = "SELECT COUNT(*) AS count FROM " . $this->_tbName;
  116. $rs = $modx->db->query($sql);
  117. $row = $modx->db->getRow($rs);
  118. $nbLogs = $row['count'];
  119. if ($nbLogs + 1 > $this->_purge) {
  120. $sql = "DELETE LOW_PRIORITY FROM " . $this->_tbName;
  121. $rs = $modx->db->query($sql);
  122. }
  123. }
  124. /*
  125. * Update a comment of a search record in database
  126. *
  127. * @access public
  128. * @param int $logid id of the log
  129. * @param string $ascmt comment
  130. */
  131. function updateComment($logid, $ascmt) {
  132. global $modx;
  133. $fields['comment'] = $ascmt;
  134. $where = "id='" . $logid . "'";
  135. $modx->db->update($fields, $this->_tbName, $where);
  136. return true;
  137. }
  138. }
  139. //==============================================================================
  140. /* The code below handles comment sent if the $_POST variables are set.
  141. Used when the user post comment from the ajaxSearch results window */
  142. if ($_POST['logid'] && $_POST['ascmt']) {
  143. $ascmt = strip_tags($_POST['ascmt']);
  144. $logid = intval($_POST['logid']);
  145. $safeCmt = (strlen($ascmt) < CMT_MAX_LENGTH) && (substr_count($ascmt, 'http') < CMT_MAX_LINKS);
  146. if (($ascmt != '') && ($logid > 0) && $safeCmt) {
  147. define('MODX_API_MODE', true);
  148. include_once (MODX_MANAGER_PATH . '/includes/document.parser.class.inc.php');
  149. $modx = new DocumentParser;
  150. $modx->db->connect();
  151. $modx->getSettings();
  152. $asLog = new AjaxSearchLog();
  153. $asLog->updateComment($logid, $ascmt);
  154. echo "comment about record " . $logid . " registered";
  155. } else {
  156. echo "ERROR: comment rejected";
  157. }
  158. }
  159. ?>