link.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 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. $this->_snapshot();
  71. $this->_pageScreenshot();
  72. }
  73. }
  74. return $this->_data;
  75. }
  76. /**
  77. * loads only the info needed to display the link
  78. * for edit use $this->load
  79. * @param $hash
  80. * @return array
  81. */
  82. public function loadShortInfo($hash) {
  83. $this->_data = array();
  84. if (!empty($hash)) {
  85. $queryStr = "SELECT
  86. any_value(`id`) as id,
  87. any_value(`link`) as link,
  88. any_value(`description`) as description,
  89. any_value(`title`) as title,
  90. any_value(`image`) as image,
  91. any_value(`hash`) as hash
  92. FROM `" . DB_PREFIX . "_link`
  93. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  94. $query = $this->DB->query($queryStr);
  95. if (!empty($query) && $query->num_rows == 1) {
  96. $this->_data = $query->fetch_assoc();
  97. # add stuff
  98. $this->_image();
  99. }
  100. }
  101. return $this->_data;
  102. }
  103. /**
  104. * return all or data for given key on the current loaded link
  105. * @param bool $key
  106. * @return array|mixed
  107. */
  108. public function getData($key = false) {
  109. $ret = $this->_data;
  110. if (!empty($key) && isset($this->_data[$key])) {
  111. $ret = $this->_data[$key];
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * reload the current id from DB
  117. */
  118. public function reload() {
  119. $this->load($this->_data['hash']);
  120. }
  121. /**
  122. * create a new link with the given data
  123. * @param array $data
  124. * @param bool $returnId
  125. * @return boolean|int
  126. */
  127. public function create($data, $returnId = false) {
  128. $ret = false;
  129. if (!isset($data['link']) || empty($data['link'])) return false;
  130. if (!isset($data['hash']) || empty($data['hash'])) return false;
  131. if (!isset($data['title']) || empty($data['title'])) return false;
  132. $queryStr = "INSERT INTO `" . DB_PREFIX . "_link` SET
  133. `link` = '" . $this->DB->real_escape_string($data['link']) . "',
  134. `created` = NOW(),
  135. `status` = '" . $this->DB->real_escape_string($data['status']) . "',
  136. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  137. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  138. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  139. `hash` = '" . $this->DB->real_escape_string($data['hash']) . "',
  140. `search` = '" . $this->DB->real_escape_string($data['search']) . "'";
  141. $this->DB->query($queryStr);
  142. if ($returnId === true) {
  143. $ret = $this->DB->insert_id;
  144. }
  145. else {
  146. error_log('ERROR Failed to rcreate link: '.var_export($data,true));
  147. }
  148. return $ret;
  149. }
  150. /**
  151. * update the current loaded link with the given data
  152. * @param array $data
  153. * @return boolean|int
  154. */
  155. public function update($data) {
  156. $ret = false;
  157. if (isset($data['title']) && !empty($data['title']) && !empty($this->_data)) {
  158. # categories and tag stuff
  159. $catArr = Summoner::prepareTagOrCategoryStr($data['category']);
  160. $tagArr = Summoner::prepareTagOrCategoryStr($data['tag']);
  161. $search = $data['title'];
  162. $search .= ' '.$data['description'];
  163. $search .= ' '.implode(" ", $tagArr);
  164. $search .= ' '.implode(" ", $catArr);
  165. $search = trim($search);
  166. $search = strtolower($search);
  167. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  168. # did the image url change?
  169. $_imageUrlChanged = false;
  170. if ($this->_data['image'] != $data['image']) {
  171. $_imageUrlChanged = true;
  172. }
  173. $queryStr = "UPDATE `" . DB_PREFIX . "_link` SET
  174. `status` = '" . $this->DB->real_escape_string($data['private']) . "',
  175. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  176. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  177. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  178. `search` = '" . $this->DB->real_escape_string($search) . "'
  179. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  180. $query = $this->DB->query($queryStr);
  181. if ($query !== false) {
  182. $catObj = new Category($this->DB);
  183. $tagObj = new Tag($this->DB);
  184. // clean the relations first
  185. $this->_removeTagRelation(false);
  186. $this->_removeCategoryRelation(false);
  187. if (!empty($catArr)) {
  188. foreach ($catArr as $c) {
  189. $catObj->initbystring($c);
  190. $catObj->setRelation($this->_data['id']);
  191. }
  192. }
  193. if (!empty($tagArr)) {
  194. foreach ($tagArr as $t) {
  195. $tagObj->initbystring($t);
  196. $tagObj->setRelation($this->_data['id']);
  197. }
  198. }
  199. $this->DB->commit();
  200. # decide to store or remove the image
  201. if (isset($data['localImage'])) {
  202. $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg';
  203. if ($data['localImage'] === true) {
  204. if (!file_exists($image) || $_imageUrlChanged === true) {
  205. Summoner::downloadFile($data['image'], $image);
  206. }
  207. } elseif ($data['localImage'] === false) {
  208. if (file_exists($image)) {
  209. unlink($image);
  210. }
  211. }
  212. }
  213. # decide if we want to make a local snapshot
  214. if(isset($data['snapshot'])) {
  215. $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'].'.jpg';
  216. if ($data['snapshot'] === true) {
  217. if (!file_exists($snapshot) || $_imageUrlChanged === true) {
  218. require_once 'lib/snapshot.class.php';
  219. $snap = new Snapshot();
  220. $do = $snap->doSnapshot($this->_data['link'], $snapshot);
  221. if(empty($do)) {
  222. error_log('ERROR Failed to create snapshot: '.var_export($data,true));
  223. }
  224. }
  225. } elseif ($data['snapshot'] === false) {
  226. if (file_exists($snapshot)) {
  227. unlink($snapshot);
  228. }
  229. }
  230. }
  231. # decide if we want to make a local full page scrrenshot
  232. if(isset($data['pagescreenshot'])) {
  233. $pagescreenshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/pagescreenshot-' . $this->_data['hash'].'.jpg';
  234. if ($data['pagescreenshot'] === true) {
  235. if (!file_exists($pagescreenshot) || $_imageUrlChanged === true) {
  236. require_once 'lib/snapshot.class.php';
  237. $snap = new Snapshot();
  238. $do = $snap->wholePageSnpashot($this->_data['link'], $pagescreenshot);
  239. if(!empty($do)) {
  240. error_log('ERROR Failed to create snapshot: '.var_export($data,true));
  241. }
  242. }
  243. } elseif ($data['pagescreenshot'] === false) {
  244. if (file_exists($pagescreenshot)) {
  245. unlink($pagescreenshot);
  246. }
  247. }
  248. }
  249. $ret = true;
  250. } else {
  251. $this->DB->rollback();
  252. error_log('ERROR Failed to update link: '.var_export($data,true));
  253. }
  254. }
  255. return $ret;
  256. }
  257. /**
  258. * call this to delete all the relations to this link.
  259. * To completely remove the link use Management->deleteLink()
  260. */
  261. public function deleteRelations() {
  262. $this->_removeTagRelation(false);
  263. $this->_removeCategoryRelation(false);
  264. $this->_deleteImage();
  265. $this->_deleteSnapshot();
  266. $this->_deletePageScreenshot();
  267. }
  268. /**
  269. * load all the tags we have to the already loaded link
  270. * needs $this->load called first
  271. */
  272. private function _tags() {
  273. $ret = array();
  274. if (!empty($this->_data['hash'])) {
  275. $queryStr = "SELECT
  276. DISTINCT tag, tagId
  277. FROM `" . DB_PREFIX . "_combined`
  278. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  279. $query = $this->DB->query($queryStr);
  280. if (!empty($query) && $query->num_rows > 0) {
  281. while ($result = $query->fetch_assoc()) {
  282. if ($result['tag'] !== NULL) {
  283. $ret[$result['tagId']] = $result['tag'];
  284. }
  285. }
  286. }
  287. }
  288. $this->_data['tags'] = $ret;
  289. }
  290. /**
  291. * load all the categories we have to the already loaded link
  292. * needs $this->load called first
  293. */
  294. private function _categories() {
  295. $ret = array();
  296. if (!empty($this->_data['hash'])) {
  297. $queryStr = "SELECT
  298. DISTINCT category, categoryId
  299. FROM `" . DB_PREFIX . "_combined`
  300. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  301. $query = $this->DB->query($queryStr);
  302. if (!empty($query) && $query->num_rows > 0) {
  303. while ($result = $query->fetch_assoc()) {
  304. if ($result['category'] !== NULL) {
  305. $ret[$result['categoryId']] = $result['category'];
  306. }
  307. }
  308. }
  309. }
  310. $this->_data['categories'] = $ret;
  311. }
  312. /**
  313. * remove all or given tag relation to the current loaded link
  314. * @param mixed $tagid
  315. */
  316. private function _removeTagRelation($tagid) {
  317. if (!empty($this->_data['id'])) {
  318. $queryStr = false;
  319. if ($tagid === false) {
  320. $queryStr = "DELETE
  321. FROM `" . DB_PREFIX . "_tagrelation`
  322. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  323. } elseif (is_numeric($tagid)) {
  324. $queryStr = "DELETE
  325. FROM `" . DB_PREFIX . "_tagrelation`
  326. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  327. AND `tagid` = '" . $this->DB->real_escape_string($tagid) . "'";
  328. }
  329. if (!empty($queryStr)) {
  330. $this->DB->query($queryStr);
  331. }
  332. }
  333. }
  334. /**
  335. * remove all or given category relation to the current loaded link
  336. * @param mixed $categoryid
  337. */
  338. private function _removeCategoryRelation($categoryid) {
  339. if (!empty($this->_data['id'])) {
  340. $queryStr = false;
  341. if ($categoryid === false) {
  342. $queryStr = "DELETE
  343. FROM `" . DB_PREFIX . "_categoryrelation`
  344. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  345. } elseif (is_numeric($categoryid)) {
  346. $queryStr = "DELETE
  347. FROM `" . DB_PREFIX . "_categoryrelation`
  348. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  349. AND `categoryid` = '" . $this->DB->real_escape_string($categoryid) . "'";
  350. }
  351. if (!empty($queryStr)) {
  352. $this->DB->query($queryStr);
  353. }
  354. }
  355. }
  356. /**
  357. * determine of we have a local stored image
  358. * if so populate the localImage attribute
  359. */
  360. private function _image() {
  361. if (!empty($this->_data['hash'])) {
  362. $this->_data['imageToShow'] = $this->_data['image'];
  363. $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
  364. if (file_exists($image)) {
  365. $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
  366. $this->_data['localImage'] = true;
  367. }
  368. }
  369. }
  370. /**
  371. * determine if we have a local stored snapshot
  372. * if so populate the snapshotLink attribute
  373. */
  374. private function _snapshot() {
  375. if (!empty($this->_data['hash'])) {
  376. $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
  377. if (file_exists($snapshot)) {
  378. $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
  379. $this->_data['snapshot'] = true;
  380. }
  381. }
  382. }
  383. /**
  384. * determine if we have a local full page screenshot
  385. * if so populate the pagescreenshotLink attribute
  386. */
  387. private function _pageScreenshot() {
  388. if (!empty($this->_data['hash'])) {
  389. $pagescreenshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
  390. if (file_exists($pagescreenshot)) {
  391. $this->_data['pagescreenshotLink'] = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
  392. $this->_data['pagescreenshot'] = true;
  393. }
  394. }
  395. }
  396. /**
  397. * remove the local stored image
  398. */
  399. private function _deleteImage() {
  400. if (!empty($this->_data['hash']) && !empty($this->_data['imageToShow'])) {
  401. $image = ABSOLUTE_PATH.'/'.$this->_data['imageToShow'];
  402. if (file_exists($image)) {
  403. unlink($image);
  404. }
  405. }
  406. }
  407. /**
  408. * remove the local stored snapshot
  409. */
  410. private function _deleteSnapshot() {
  411. if (!empty($this->_data['hash']) && !empty($this->_data['snapshotLink'])) {
  412. $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
  413. if (file_exists($snapshot)) {
  414. unlink($snapshot);
  415. }
  416. }
  417. }
  418. /**
  419. * remove the local stored pagescreenshot
  420. */
  421. private function _deletePageScreenshot() {
  422. if (!empty($this->_data['hash']) && !empty($this->_data['pagescreenshotLink'])) {
  423. $pagescreenshot = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
  424. if (file_exists($pagescreenshot)) {
  425. unlink($pagescreenshot);
  426. }
  427. }
  428. }
  429. /**
  430. * check if the status is private and set the info
  431. */
  432. private function _private() {
  433. if (!empty($this->_data['status']) && $this->_data['status'] == "1") {
  434. $this->_data['private'] = "1";
  435. }
  436. }
  437. }