entry.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * scientia
  4. *
  5. * Copyright 2023 - 2024 Johannes Keßler
  6. *
  7. * https://www.bananas-playground.net/projekt/scientia/
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  21. */
  22. /**
  23. * Class Entry
  24. *
  25. * Entry loading and creation
  26. */
  27. class Entry {
  28. /**
  29. * the global DB object
  30. *
  31. * @var mysqli
  32. */
  33. private mysqli $_DB;
  34. /**
  35. * Entry constructor.
  36. *
  37. * @param mysqli $db
  38. */
  39. public function __construct(mysqli $db) {
  40. $this->_DB = $db;
  41. }
  42. /**
  43. * Create a new entry with given data
  44. * Data is not validated anymore
  45. *
  46. * @param string $data
  47. * @return string
  48. */
  49. public function create(string $data): string {
  50. $ret = '';
  51. $_words = implode(' ', $this->_words($data));
  52. $_ident = Summoner::b64sl_pack_id(rand(111111, 999999));
  53. $queryStr = "INSERT INTO `".DB_PREFIX."_entry` SET
  54. `created` = NOW(),
  55. `date` = CURRENT_DATE(),
  56. `ident` = '".$this->_DB->real_escape_string($_ident)."',
  57. `body` = '".$this->_DB->real_escape_string($data)."',
  58. `words` = '".$this->_DB->real_escape_string($_words)."'";
  59. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  60. try {
  61. $this->_DB->query($queryStr);
  62. $ret = $_ident;
  63. }
  64. catch(Exception $e) {
  65. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  66. }
  67. return $ret;
  68. }
  69. /**
  70. * Load an entry by given $id. Use date info to make sure that the context is correct
  71. *
  72. * @param string $y Year Y
  73. * @param string $m Month m
  74. * @param string $d Day d
  75. * @param string $id Id of the entry
  76. * @return array
  77. */
  78. public function load(string $y, string $m, string $d, string $id): array {
  79. $ret = array();
  80. if(!empty($id) && !empty($y) && !empty($m) && !empty($d)) {
  81. $queryStr = "SELECT `created`,`modified`,`body`
  82. FROM `".DB_PREFIX."_entry`
  83. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'
  84. AND `date` = '".$this->_DB->real_escape_string($y.'-'.$m.'-'.$d)."'";
  85. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  86. try {
  87. $query = $this->_DB->query($queryStr);
  88. if($query !== false && $query->num_rows > 0) {
  89. $ret = $query->fetch_assoc();
  90. }
  91. }
  92. catch(Exception $e) {
  93. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  94. }
  95. }
  96. return $ret;
  97. }
  98. /**
  99. * Load an entry by given $id.
  100. * Used by get api
  101. *
  102. * @param string $id Id of the entry
  103. * @return array
  104. */
  105. public function loadById(string $id): array {
  106. $ret = array();
  107. if(!empty($id)) {
  108. $queryStr = "SELECT `ident`,`date`,`body`
  109. FROM `".DB_PREFIX."_entry`
  110. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
  111. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  112. try {
  113. $query = $this->_DB->query($queryStr);
  114. if($query !== false && $query->num_rows > 0) {
  115. $ret = $query->fetch_assoc();
  116. }
  117. }
  118. catch(Exception $e) {
  119. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  120. }
  121. }
  122. return $ret;
  123. }
  124. /**
  125. * Update an entry by given $id and $data
  126. *
  127. * @param string $data
  128. * @param string $id
  129. * @return string
  130. */
  131. public function update(string $data, string $id): string {
  132. $ret = '';
  133. if(!empty($data) && !empty($id)) {
  134. $_words = implode(' ', $this->_words($data));
  135. $queryStr = "UPDATE `".DB_PREFIX."_entry` SET
  136. `body` = '".$this->_DB->real_escape_string($data)."',
  137. `words` = '".$this->_DB->real_escape_string($_words)."'
  138. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
  139. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  140. try {
  141. $this->_DB->query($queryStr);
  142. $ret = $id;
  143. }
  144. catch(Exception $e) {
  145. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  146. }
  147. }
  148. return $ret;
  149. }
  150. /**
  151. * Delete given id from _entry table
  152. *
  153. * @param string $id
  154. * @return bool
  155. */
  156. public function delete(string $id): bool {
  157. $ret = false;
  158. if(!empty($id)) {
  159. $queryStr = "DELETE FROM `".DB_PREFIX."_entry`
  160. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
  161. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  162. try {
  163. $this->_DB->query($queryStr);
  164. $ret = true;
  165. }
  166. catch(Exception $e) {
  167. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  168. }
  169. }
  170. return $ret;
  171. }
  172. /**
  173. * Get all entries which match the specified options
  174. * Body is trimmed to the first 100 chars
  175. *
  176. * @param String $searchTerm
  177. * @param String $intervalStart
  178. * @param String $intervalEnd
  179. * @param int $limit
  180. * @return array
  181. */
  182. public function list(string $searchTerm='', string $intervalStart='', string $intervalEnd='', int $limit=100): array {
  183. $ret = array();
  184. $queryStr = "SELECT e.ident, e.date, SUBSTRING(e.body,1,100) AS body
  185. FROM `".DB_PREFIX."_entry` AS e";
  186. if(!empty($intervalStart) && !empty($intervalEnd)) {
  187. $queryStr .= " WHERE e.date >= '".$intervalStart."' AND e.date <= '".$intervalEnd."'";
  188. }
  189. if(!empty($searchTerm)) {
  190. $queryStr .= " AND MATCH(e.words) AGAINST('".$this->_DB->real_escape_string($searchTerm)."' IN BOOLEAN MODE)";
  191. }
  192. $queryStr .= " ORDER BY `created` DESC";
  193. $queryStr .= " LIMIT $limit";
  194. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  195. try {
  196. $query = $this->_DB->query($queryStr);
  197. $ret = $query->fetch_all(MYSQLI_ASSOC);
  198. }
  199. catch(Exception $e) {
  200. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  201. }
  202. return $ret;
  203. }
  204. /**
  205. * Create unique words from the given data
  206. *
  207. * @param $data string
  208. * @return array
  209. * @todo ignores
  210. *
  211. */
  212. private function _words(string $data): array {
  213. preg_match_all('/\w{3,}+/u',$data,$matches);
  214. return array_unique($matches[0]);
  215. }
  216. }