entry.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * scientia
  4. *
  5. * Copyright 2022 - 2024 Johannes Keßler
  6. *
  7. * https://www.bananas-playground.net/projekt/scientia/
  8. *
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  12. *
  13. * You should have received a copy of the
  14. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  15. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  16. */
  17. /**
  18. * Class Entry
  19. *
  20. * Entry loading and creation
  21. */
  22. class Entry {
  23. /**
  24. * the global DB object
  25. *
  26. * @var mysqli
  27. */
  28. private mysqli $_DB;
  29. /**
  30. * Entry constructor.
  31. *
  32. * @param mysqli $db
  33. */
  34. public function __construct(mysqli $db) {
  35. $this->_DB = $db;
  36. }
  37. /**
  38. * Create a new entry with given data
  39. * Data is not validated anymore
  40. *
  41. * @param string $data
  42. * @return string
  43. */
  44. public function create(string $data): string {
  45. $ret = '';
  46. $_words = implode(' ', $this->_words($data));
  47. $_ident = Summoner::b64sl_pack_id(rand(111111, 999999));
  48. $queryStr = "INSERT INTO `".DB_PREFIX."_entry` SET
  49. `created` = NOW(),
  50. `date` = CURRENT_DATE(),
  51. `ident` = '".$this->_DB->real_escape_string($_ident)."',
  52. `body` = '".$this->_DB->real_escape_string($data)."',
  53. `words` = '".$this->_DB->real_escape_string($_words)."'";
  54. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  55. try {
  56. $this->_DB->query($queryStr);
  57. $ret = $_ident;
  58. }
  59. catch(Exception $e) {
  60. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  61. }
  62. return $ret;
  63. }
  64. /**
  65. * Load an entry by given $id. Use date info to make sure that the context is correct
  66. *
  67. * @param string $y Year Y
  68. * @param string $m Month m
  69. * @param string $d Day d
  70. * @param string $id Id of the entry
  71. * @return array
  72. */
  73. public function load(string $y, string $m, string $d, string $id): array {
  74. $ret = array();
  75. if(!empty($id) && !empty($y) && !empty($m) && !empty($d)) {
  76. $queryStr = "SELECT `created`,`modified`,`body`
  77. FROM `".DB_PREFIX."_entry`
  78. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'
  79. AND `date` = '".$this->_DB->real_escape_string($y.'-'.$m.'-'.$d)."'";
  80. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  81. try {
  82. $query = $this->_DB->query($queryStr);
  83. if($query !== false && $query->num_rows > 0) {
  84. $ret = $query->fetch_assoc();
  85. }
  86. }
  87. catch(Exception $e) {
  88. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  89. }
  90. }
  91. return $ret;
  92. }
  93. /**
  94. * Update an entry by given $id and $data
  95. *
  96. * @param string $data
  97. * @param string $id
  98. * @return string
  99. */
  100. public function update(string $data, string $id): string {
  101. $ret = '';
  102. if(!empty($data) && !empty($id)) {
  103. $_words = implode(' ', $this->_words($data));
  104. $queryStr = "UPDATE `".DB_PREFIX."_entry` SET
  105. `body` = '".$this->_DB->real_escape_string($data)."',
  106. `words` = '".$this->_DB->real_escape_string($_words)."'
  107. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
  108. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  109. try {
  110. $this->_DB->query($queryStr);
  111. $ret = $id;
  112. }
  113. catch(Exception $e) {
  114. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  115. }
  116. }
  117. return $ret;
  118. }
  119. /**
  120. * Delete given id from _entry table
  121. *
  122. * @param string $id
  123. * @return bool
  124. */
  125. public function delete(string $id): bool {
  126. $ret = false;
  127. if(!empty($id)) {
  128. $queryStr = "DELETE FROM `".DB_PREFIX."_entry`
  129. WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
  130. if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
  131. try {
  132. $this->_DB->query($queryStr);
  133. $ret = true;
  134. }
  135. catch(Exception $e) {
  136. error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
  137. }
  138. }
  139. return $ret;
  140. }
  141. /**
  142. * Create unique words from the given data
  143. *
  144. * @param $data string
  145. * @return array
  146. * @todo ignores
  147. *
  148. */
  149. private function _words(string $data): array {
  150. preg_match_all('/\w{3,}+/u',$data,$matches);
  151. return array_unique($matches[0]);
  152. }
  153. }