link.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2019 Johannes Keßler
  7. *
  8. * Development starting from 2011: Johannes Keßler
  9. * https://www.bananas-playground.net/projekt/insipid/
  10. *
  11. * creator:
  12. * Luke Reeves <luke@neuro-tech.net>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  26. *
  27. */
  28. class Link {
  29. /**
  30. * the database object
  31. * @var object
  32. */
  33. private $DB;
  34. /**
  35. * the current loaded link data
  36. * @var array
  37. */
  38. private $_data;
  39. public function __construct($databaseConnectionObject) {
  40. $this->DB = $databaseConnectionObject;
  41. }
  42. /**
  43. * load all the info we have about a link by given hash
  44. * @param string $hash
  45. * @return mixed
  46. */
  47. public function load($hash) {
  48. $this->_data = array();
  49. if (!empty($hash)) {
  50. $queryStr = "SELECT
  51. any_value(`id`) as id,
  52. any_value(`link`) as link,
  53. any_value(`created`) as created,
  54. any_value(`updated`) as `updated`,
  55. any_value(`status`) as `status`,
  56. any_value(`description`) as description,
  57. any_value(`title`) as title,
  58. any_value(`image`) as image,
  59. any_value(`hash`) as hash
  60. FROM `" . DB_PREFIX . "_link`
  61. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  62. $query = $this->DB->query($queryStr);
  63. if (!empty($query) && $query->num_rows == 1) {
  64. $this->_data = $query->fetch_assoc();
  65. # add stuff
  66. $this->_tags();
  67. $this->_categories();
  68. $this->_image();
  69. $this->_private();
  70. }
  71. }
  72. return $this->_data;
  73. }
  74. /**
  75. * loads only the info needed to display the link
  76. * for edit use $this->load
  77. * @param $hash
  78. * @return array
  79. */
  80. public function loadShortInfo($hash) {
  81. $this->_data = array();
  82. if (!empty($hash)) {
  83. $queryStr = "SELECT
  84. any_value(`id`) as id,
  85. any_value(`link`) as link,
  86. any_value(`description`) as description,
  87. any_value(`title`) as title,
  88. any_value(`image`) as image,
  89. any_value(`hash`) as hash
  90. FROM `" . DB_PREFIX . "_link`
  91. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  92. $query = $this->DB->query($queryStr);
  93. if (!empty($query) && $query->num_rows == 1) {
  94. $this->_data = $query->fetch_assoc();
  95. # add stuff
  96. $this->_image();
  97. }
  98. }
  99. return $this->_data;
  100. }
  101. /**
  102. * return all or data for given key on the current loaded link
  103. * @param bool $key
  104. * @return array|mixed
  105. */
  106. public function getData($key = false) {
  107. $ret = $this->_data;
  108. if (!empty($key) && isset($this->_data[$key])) {
  109. $ret = $this->_data[$key];
  110. }
  111. return $ret;
  112. }
  113. /**
  114. * reload the current id from DB
  115. */
  116. public function reload() {
  117. $this->load($this->_data['hash']);
  118. }
  119. /**
  120. * create a new link with the given data
  121. * @param array $data
  122. * @return boolean|int
  123. */
  124. public function create($data, $returnId = false) {
  125. $ret = false;
  126. if (!isset($data['link']) || empty($data['link'])) return false;
  127. if (!isset($data['hash']) || empty($data['hash'])) return false;
  128. if (!isset($data['title']) || empty($data['title'])) return false;
  129. $queryStr = "INSERT INTO `" . DB_PREFIX . "_link` SET
  130. `link` = '" . $this->DB->real_escape_string($data['link']) . "',
  131. `created` = NOW(),
  132. `status` = '" . $this->DB->real_escape_string($data['status']) . "',
  133. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  134. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  135. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  136. `hash` = '" . $this->DB->real_escape_string($data['hash']) . "',
  137. `search` = '" . $this->DB->real_escape_string($data['search']) . "'";
  138. $this->DB->query($queryStr);
  139. if ($returnId === true) {
  140. $ret = $this->DB->insert_id;
  141. }
  142. return $ret;
  143. }
  144. /**
  145. * update the current loaded link with the given data
  146. * @param array $data
  147. * @return boolean|int
  148. */
  149. public function update($data) {
  150. $ret = false;
  151. if (isset($data['title']) && !empty($data['title'])) {
  152. # categories and tag stuff
  153. $catArr = Summoner::prepareTagOrCategoryStr($data['category']);
  154. $tagArr = Summoner::prepareTagOrCategoryStr($data['tag']);
  155. $search = $data['title'];
  156. $search .= ' ' . $data['description'];
  157. $search .= ' ' . implode(" ", $tagArr);
  158. $search .= ' ' . implode(" ", $catArr);
  159. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  160. # did the image url change?
  161. $_imageUrlChanged = false;
  162. if ($this->_data['image'] != $data['image']) {
  163. $_imageUrlChanged = true;
  164. }
  165. $queryStr = "UPDATE `" . DB_PREFIX . "_link` SET
  166. `status` = '" . $this->DB->real_escape_string($data['private']) . "',
  167. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  168. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  169. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  170. `search` = '" . $this->DB->real_escape_string($search) . "'
  171. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  172. $query = $this->DB->query($queryStr);
  173. if ($query !== false) {
  174. $catObj = new Category($this->DB);
  175. $tagObj = new Tag($this->DB);
  176. // clean the relations first
  177. $this->_removeTagRelation(false);
  178. $this->_removeCategoryRelation(false);
  179. if (!empty($catArr)) {
  180. foreach ($catArr as $c) {
  181. $catObj->initbystring($c);
  182. $catObj->setRelation($this->_data['id']);
  183. }
  184. }
  185. if (!empty($tagArr)) {
  186. foreach ($tagArr as $t) {
  187. $tagObj->initbystring($t);
  188. $tagObj->setRelation($this->_data['id']);
  189. }
  190. }
  191. # decide to store or remove the image
  192. if (isset($data['localImage'])) {
  193. $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'];
  194. if ($data['localImage'] === true) {
  195. if (!file_exists($image) || $_imageUrlChanged === true) {
  196. Summoner::downloadFile($data['image'], $image);
  197. }
  198. } elseif ($data['localImage'] === false) {
  199. if (file_exists($image)) {
  200. unlink($image);
  201. }
  202. }
  203. }
  204. $this->DB->commit();
  205. $ret = true;
  206. } else {
  207. $this->DB->rollback();
  208. }
  209. }
  210. return $ret;
  211. }
  212. /**
  213. * call this to delete all the relations to this link.
  214. * To completely remove the link use Management->deleteLink()
  215. */
  216. public function deleteRelations() {
  217. $this->_removeTagRelation(false);
  218. $this->_removeCategoryRelation(false);
  219. $this->_deleteImage();
  220. }
  221. /**
  222. * load all the tags we have to the already loaded link
  223. * needs $this->load called first
  224. */
  225. private function _tags() {
  226. $ret = array();
  227. if (!empty($this->_data['hash'])) {
  228. $queryStr = "SELECT
  229. DISTINCT tag, tagId
  230. FROM `" . DB_PREFIX . "_combined`
  231. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  232. $query = $this->DB->query($queryStr);
  233. if (!empty($query) && $query->num_rows > 0) {
  234. while ($result = $query->fetch_assoc()) {
  235. if ($result['tag'] !== NULL) {
  236. $ret[$result['tagId']] = $result['tag'];
  237. }
  238. }
  239. }
  240. }
  241. $this->_data['tags'] = $ret;
  242. }
  243. /**
  244. * load all the categories we have to the already loaded link
  245. * needs $this->load called first
  246. */
  247. private function _categories() {
  248. $ret = array();
  249. if (!empty($this->_data['hash'])) {
  250. $queryStr = "SELECT
  251. DISTINCT category, categoryId
  252. FROM `" . DB_PREFIX . "_combined`
  253. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  254. $query = $this->DB->query($queryStr);
  255. if (!empty($query) && $query->num_rows > 0) {
  256. while ($result = $query->fetch_assoc()) {
  257. if ($result['category'] !== NULL) {
  258. $ret[$result['categoryId']] = $result['category'];
  259. }
  260. }
  261. }
  262. }
  263. $this->_data['categories'] = $ret;
  264. }
  265. /**
  266. * remove all or given tag relation to the current loaded link
  267. * @param mixed $tagid
  268. */
  269. private function _removeTagRelation($tagid) {
  270. if (!empty($this->_data['id'])) {
  271. $queryStr = false;
  272. if ($tagid === false) {
  273. $queryStr = "DELETE
  274. FROM `" . DB_PREFIX . "_tagrelation`
  275. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  276. } elseif (is_numeric($tagid)) {
  277. $queryStr = "DELETE
  278. FROM `" . DB_PREFIX . "_tagrelation`
  279. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  280. AND `tagid` = '" . $this->DB->real_escape_string($tagid) . "'";
  281. }
  282. if (!empty($queryStr)) {
  283. $this->DB->query($queryStr);
  284. }
  285. }
  286. }
  287. /**
  288. * remove all or given category relation to the current loaded link
  289. * @param mixed $categoryid
  290. */
  291. private function _removeCategoryRelation($categoryid) {
  292. if (!empty($this->_data['id'])) {
  293. $queryStr = false;
  294. if ($categoryid === false) {
  295. $queryStr = "DELETE
  296. FROM `" . DB_PREFIX . "_categoryrelation`
  297. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  298. } elseif (is_numeric($categoryid)) {
  299. $queryStr = "DELETE
  300. FROM `" . DB_PREFIX . "_categoryrelation`
  301. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  302. AND `categoryid` = '" . $this->DB->real_escape_string($categoryid) . "'";
  303. }
  304. if (!empty($queryStr)) {
  305. $this->DB->query($queryStr);
  306. }
  307. }
  308. }
  309. /**
  310. * determine of we have a local stored image
  311. * if so populate the localImage attribute
  312. */
  313. private function _image() {
  314. if (!empty($this->_data['hash'])) {
  315. $this->_data['imageToShow'] = $this->_data['image'];
  316. $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'];
  317. if (file_exists($image)) {
  318. $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'];
  319. $this->_data['localImage'] = true;
  320. }
  321. }
  322. }
  323. /**
  324. * remove the local stored image
  325. */
  326. private function _deleteImage() {
  327. if (!empty($this->_data['hash']) && !empty($this->_data['imageToShow'])) {
  328. $image = ABSOLUTE_PATH.'/'.$this->_data['imageToShow'];
  329. if (file_exists($image)) {
  330. unlink($image);
  331. }
  332. }
  333. }
  334. /**
  335. * check if the status is private and set the info
  336. */
  337. private function _private() {
  338. if (!empty($this->_data['status']) && $this->_data['status'] == "1") {
  339. $this->_data['private'] = "1";
  340. }
  341. }
  342. }