link.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2023 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. /**
  29. * Class Link
  30. */
  31. class Link {
  32. /**
  33. * the database object
  34. *
  35. * @var mysqli
  36. */
  37. private mysqli $DB;
  38. /**
  39. * the current loaded link data
  40. *
  41. * @var array
  42. */
  43. private array $_data;
  44. /**
  45. * Link constructor.
  46. *
  47. * @param mysqli $databaseConnectionObject
  48. */
  49. public function __construct(mysqli $databaseConnectionObject) {
  50. $this->DB = $databaseConnectionObject;
  51. }
  52. /**
  53. * load all the info we have about a link by given hash
  54. *
  55. * @param string $hash
  56. * @return array
  57. */
  58. public function load(string $hash): array {
  59. $this->_data = array();
  60. if (!empty($hash)) {
  61. $queryStr = "SELECT
  62. `id`,
  63. `link`,
  64. `created`,
  65. `updated`,
  66. `status`,
  67. `description`,
  68. `title`,
  69. `image`,
  70. `hash`
  71. FROM `".DB_PREFIX."_link`
  72. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  73. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  74. try {
  75. $query = $this->DB->query($queryStr);
  76. if (!empty($query) && $query->num_rows == 1) {
  77. $this->_data = $query->fetch_assoc();
  78. # add stuff
  79. $this->_tags();
  80. $this->_categories();
  81. $this->_image();
  82. $this->_private();
  83. $this->_snapshot();
  84. $this->_pageScreenshot();
  85. }
  86. } catch (Exception $e) {
  87. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  88. }
  89. }
  90. return $this->_data;
  91. }
  92. /**
  93. * loads only the info needed to display the link
  94. * for edit use $this->load
  95. *
  96. * @param string $hash
  97. * @return array
  98. */
  99. public function loadShortInfo(string $hash): array {
  100. $this->_data = array();
  101. if (!empty($hash)) {
  102. $queryStr = "SELECT `id`,`link`,`description`,`title`,`image`,`hash`, `created`
  103. FROM `".DB_PREFIX."_link`
  104. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  105. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  106. try {
  107. $query = $this->DB->query($queryStr);
  108. if (!empty($query) && $query->num_rows == 1) {
  109. $this->_data = $query->fetch_assoc();
  110. # add stuff
  111. $this->_image();
  112. $this->_tags();
  113. $this->_categories();
  114. }
  115. } catch (Exception $e) {
  116. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  117. }
  118. }
  119. return $this->_data;
  120. }
  121. /**
  122. * Get shortinfo from given data array
  123. *
  124. * @param array $data
  125. * @return array
  126. */
  127. public function loadFromDataShortInfo(array $data): array {
  128. $this->_data = array();
  129. if(isset($data['id']) && isset($data['link']) && isset($data['created']) && isset($data['status'])
  130. && isset($data['title']) && isset($data['hash']) && isset($data['description']) && isset($data['image'])) {
  131. $this->_data = $data;
  132. $this->_image();
  133. $this->_tags();
  134. $this->_categories();
  135. }
  136. return $this->_data;
  137. }
  138. /**
  139. * return all or data for given key on the current loaded link
  140. *
  141. * @param string $key
  142. * @return string|array
  143. */
  144. public function getData(string $key = ''): string|array {
  145. $ret = $this->_data;
  146. if (!empty($key) && isset($this->_data[$key])) {
  147. $ret = $this->_data[$key];
  148. }
  149. return $ret;
  150. }
  151. /**
  152. * reload the current id from DB
  153. *
  154. * @return void
  155. */
  156. public function reload(): void {
  157. $this->load($this->_data['hash']);
  158. }
  159. /**
  160. * create a new link with the given data
  161. *
  162. * @param array $data
  163. * @param bool $returnId
  164. * @return string
  165. */
  166. public function create(array $data, bool $returnId = false): string {
  167. $ret = '';
  168. if (!isset($data['link']) || empty($data['link'])) return $ret;
  169. if (!isset($data['hash']) || empty($data['hash'])) return $ret;
  170. if (!isset($data['title']) || empty($data['title'])) return $ret;
  171. $_t = parse_url($data['link']);
  172. $data['search'] = $data['title'];
  173. $data['search'] .= ' '.$data['description'];
  174. $data['search'] .= ' '.implode(" ",$data['tagArr']);
  175. $data['search'] .= ' '.implode(" ",$data['catArr']);
  176. $data['search'] .= ' '.$_t['host'];
  177. $data['search'] .= ' '.implode(' ',explode('/',$_t['path']));
  178. $data['search'] = trim($data['search']);
  179. $data['search'] = strtolower($data['search']);
  180. $queryStr = "INSERT INTO `" . DB_PREFIX . "_link` SET
  181. `link` = '" . $this->DB->real_escape_string($data['link']) . "',
  182. `created` = NOW(),
  183. `status` = '" . $this->DB->real_escape_string($data['status']) . "',
  184. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  185. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  186. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  187. `hash` = '" . $this->DB->real_escape_string($data['hash']) . "',
  188. `search` = '" . $this->DB->real_escape_string($data['search']) . "'";
  189. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  190. try {
  191. $this->DB->query($queryStr);
  192. if ($returnId === true) {
  193. $ret = $this->DB->insert_id;
  194. }
  195. else {
  196. Summoner::sysLog('ERROR Failed to create link: '.Summoner::cleanForLog($data));
  197. }
  198. } catch (Exception $e) {
  199. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  200. }
  201. return $ret;
  202. }
  203. /**
  204. * update the current loaded link with the given data
  205. *
  206. * @param array $data
  207. * @return boolean
  208. */
  209. public function update(array $data): bool {
  210. $ret = false;
  211. if (isset($data['title']) && !empty($data['title']) && !empty($this->_data)) {
  212. # categories and tag stuff
  213. $catArr = Summoner::prepareTagOrCategoryStr($data['category']);
  214. $tagArr = Summoner::prepareTagOrCategoryStr($data['tag']);
  215. $_t = parse_url($this->_data['link']);
  216. $search = $data['title'];
  217. $search .= ' '.$data['description'];
  218. $search .= ' '.implode(" ", $tagArr);
  219. $search .= ' '.implode(" ", $catArr);
  220. $search .= ' '.$_t['host'];
  221. if(isset($_t['path'])) {
  222. $search .= ' '.implode(' ',explode('/',$_t['path']));
  223. }
  224. $search = trim($search);
  225. $search = strtolower($search);
  226. # did the image url change?
  227. $_imageUrlChanged = false;
  228. if ($this->_data['image'] != $data['image']) {
  229. $_imageUrlChanged = true;
  230. }
  231. $queryStr = "UPDATE `" . DB_PREFIX . "_link` SET
  232. `status` = '" . $this->DB->real_escape_string($data['private']) . "',
  233. `description` = '" . $this->DB->real_escape_string($data['description']) . "',
  234. `title` = '" . $this->DB->real_escape_string($data['title']) . "',
  235. `image` = '" . $this->DB->real_escape_string($data['image']) . "',
  236. `search` = '" . $this->DB->real_escape_string($search) . "'
  237. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  238. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  239. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  240. try {
  241. $query = $this->DB->query($queryStr);
  242. } catch (Exception $e) {
  243. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  244. }
  245. if ($query !== false) {
  246. $catObj = new Category($this->DB);
  247. $tagObj = new Tag($this->DB);
  248. // clean the relations first
  249. $this->_removeTagRelation();
  250. $this->_removeCategoryRelation();
  251. if (!empty($catArr)) {
  252. foreach ($catArr as $c) {
  253. $catObj->initbystring($c);
  254. $catObj->setRelation($this->_data['id']);
  255. }
  256. }
  257. if (!empty($tagArr)) {
  258. foreach ($tagArr as $t) {
  259. $tagObj->initbystring($t);
  260. $tagObj->setRelation($this->_data['id']);
  261. }
  262. }
  263. $this->DB->commit();
  264. # decide to store or remove the image
  265. if (isset($data['localImage'])) {
  266. $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg';
  267. if ($data['localImage'] === true) {
  268. if(DEBUG) Summoner::sysLog("DEBUG want to save local image to: $image");
  269. if (!file_exists($image) || $_imageUrlChanged === true) {
  270. if(DEBUG) Summoner::sysLog("DEBUG Image new or not there yet: $image");
  271. Summoner::downloadFile($data['image'], $image);
  272. }
  273. } elseif ($data['localImage'] === false) {
  274. if(DEBUG) Summoner::sysLog("DEBUG want to remove local image: $image");
  275. if (file_exists($image)) {
  276. if(DEBUG) Summoner::sysLog("DEBUG Image to be removed: $image");
  277. unlink($image);
  278. }
  279. }
  280. }
  281. # decide if we want to make a local snapshot
  282. if(isset($data['snapshot'])) {
  283. $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'].'.webp';
  284. if ($data['snapshot'] === true) {
  285. if (!file_exists($snapshot) || $_imageUrlChanged === true) {
  286. require_once 'lib/snapshot.class.php';
  287. $snap = new Snapshot();
  288. $do = $snap->doSnapshot($this->_data['link'], $snapshot);
  289. if(empty($do)) {
  290. Summoner::sysLog('ERROR Failed to create snapshot: '.Summoner::cleanForLog($data));
  291. }
  292. }
  293. } elseif ($data['snapshot'] === false) {
  294. if(DEBUG) Summoner::sysLog("DEBUG want to remove local snapshot: $snapshot");
  295. if (file_exists($snapshot)) {
  296. if(DEBUG) Summoner::sysLog("DEBUG remove local snapshot: $snapshot");
  297. unlink($snapshot);
  298. }
  299. }
  300. }
  301. # decide if we want to make a local full page screenshot
  302. if(isset($data['pagescreenshot'])) {
  303. $pagescreenshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/pagescreenshot-' . $this->_data['hash'].'.jpeg';
  304. if ($data['pagescreenshot'] === true) {
  305. if (!file_exists($pagescreenshot) || $_imageUrlChanged === true) {
  306. require_once 'lib/snapshot.class.php';
  307. $snap = new Snapshot();
  308. $do = $snap->wholePageSnapshot($this->_data['link'], $pagescreenshot);
  309. if(!$do) {
  310. Summoner::sysLog('ERROR Failed to create pagescreenshot: '.Summoner::cleanForLog($data));
  311. }
  312. }
  313. } elseif ($data['pagescreenshot'] === false) {
  314. if(DEBUG) Summoner::sysLog("DEBUG want to remove local pagescreenshot: $pagescreenshot");
  315. if (file_exists($pagescreenshot)) {
  316. if(DEBUG) Summoner::sysLog("DEBUG remove local pagescreenshot: $pagescreenshot");
  317. unlink($pagescreenshot);
  318. }
  319. }
  320. }
  321. $ret = true;
  322. } else {
  323. $this->DB->rollback();
  324. Summoner::sysLog('ERROR Failed to update link: '.Summoner::cleanForLog($data));
  325. }
  326. }
  327. return $ret;
  328. }
  329. /**
  330. * call this to delete all the relations to this link.
  331. * To completely remove the link use Management->deleteLink()
  332. *
  333. * @return void
  334. */
  335. public function deleteRelations(): void {
  336. $this->_removeTagRelation();
  337. $this->_removeCategoryRelation();
  338. $this->_deleteImage();
  339. $this->_deleteSnapshot();
  340. $this->_deletePageScreenshot();
  341. }
  342. /**
  343. * load all the tags we have to the already loaded link
  344. * needs $this->load called first
  345. *
  346. * @return void
  347. */
  348. private function _tags(): void {
  349. $ret = array();
  350. if (!empty($this->_data['hash'])) {
  351. $queryStr = "SELECT
  352. DISTINCT tag, tagId
  353. FROM `" . DB_PREFIX . "_combined`
  354. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  355. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  356. try {
  357. $query = $this->DB->query($queryStr);
  358. if (!empty($query) && $query->num_rows > 0) {
  359. while ($result = $query->fetch_assoc()) {
  360. if ($result['tag'] !== NULL) {
  361. $ret[$result['tagId']] = $result['tag'];
  362. }
  363. }
  364. }
  365. } catch (Exception $e) {
  366. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  367. }
  368. }
  369. $this->_data['tags'] = $ret;
  370. }
  371. /**
  372. * load all the categories we have to the already loaded link
  373. * needs $this->load called first
  374. *
  375. * @return void
  376. */
  377. private function _categories(): void {
  378. $ret = array();
  379. if (!empty($this->_data['hash'])) {
  380. $queryStr = "SELECT
  381. DISTINCT category, categoryId
  382. FROM `" . DB_PREFIX . "_combined`
  383. WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'";
  384. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  385. try {
  386. $query = $this->DB->query($queryStr);
  387. if (!empty($query) && $query->num_rows > 0) {
  388. while ($result = $query->fetch_assoc()) {
  389. if ($result['category'] !== NULL) {
  390. $ret[$result['categoryId']] = $result['category'];
  391. }
  392. }
  393. }
  394. } catch (Exception $e) {
  395. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  396. }
  397. }
  398. $this->_data['categories'] = $ret;
  399. }
  400. /**
  401. * remove all or given tag relation to the current loaded link
  402. *
  403. * @param string $tagid
  404. * @return void
  405. */
  406. private function _removeTagRelation(string $tagid = ''): void {
  407. if (!empty($this->_data['id'])) {
  408. $queryStr = '';
  409. if (is_numeric($tagid)) {
  410. $queryStr = "DELETE
  411. FROM `" . DB_PREFIX . "_tagrelation`
  412. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  413. AND `tagid` = '" . $this->DB->real_escape_string($tagid) . "'";
  414. } else {
  415. $queryStr = "DELETE
  416. FROM `" . DB_PREFIX . "_tagrelation`
  417. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  418. }
  419. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  420. if (!empty($queryStr)) {
  421. try {
  422. $this->DB->query($queryStr);
  423. } catch (Exception $e) {
  424. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  425. }
  426. }
  427. }
  428. }
  429. /**
  430. * remove all or given category relation to the current loaded link
  431. *
  432. * @param string $categoryid
  433. * @return void
  434. */
  435. private function _removeCategoryRelation(string $categoryid=''): void {
  436. if (!empty($this->_data['id'])) {
  437. $queryStr = '';
  438. if (is_numeric($categoryid)) {
  439. $queryStr = "DELETE
  440. FROM `" . DB_PREFIX . "_categoryrelation`
  441. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'
  442. AND `categoryid` = '" . $this->DB->real_escape_string($categoryid) . "'";
  443. } else {
  444. $queryStr = "DELETE
  445. FROM `" . DB_PREFIX . "_categoryrelation`
  446. WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'";
  447. }
  448. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  449. if (!empty($queryStr)) {
  450. try {
  451. $this->DB->query($queryStr);
  452. } catch (Exception $e) {
  453. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  454. }
  455. }
  456. }
  457. }
  458. /**
  459. * determine of we have a local stored image
  460. * if so populate the localImage attribute
  461. *
  462. * @return void
  463. */
  464. private function _image(): void {
  465. if (!empty($this->_data['hash'])) {
  466. $this->_data['imageToShow'] = $this->_data['image'];
  467. $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
  468. if (file_exists($image)) {
  469. $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
  470. $this->_data['localImage'] = true;
  471. }
  472. }
  473. }
  474. /**
  475. * determine if we have a local stored snapshot
  476. * if so populate the snapshotLink attribute
  477. *
  478. * @return void
  479. */
  480. private function _snapshot(): void {
  481. if (!empty($this->_data['hash'])) {
  482. $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.webp';
  483. if (file_exists($snapshot)) {
  484. $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.webp';
  485. $this->_data['snapshot'] = true;
  486. }
  487. }
  488. }
  489. /**
  490. * determine if we have a local full page screenshot
  491. * if so populate the pagescreenshotLink attribute
  492. *
  493. * @return void
  494. */
  495. private function _pageScreenshot(): void {
  496. if (!empty($this->_data['hash'])) {
  497. $pagescreenshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpeg';
  498. if (file_exists($pagescreenshot)) {
  499. $this->_data['pagescreenshotLink'] = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpeg';
  500. $this->_data['pagescreenshot'] = true;
  501. }
  502. }
  503. }
  504. /**
  505. * remove the local stored image
  506. *
  507. * @return void
  508. */
  509. private function _deleteImage(): void {
  510. if (!empty($this->_data['hash']) && !empty($this->_data['imageToShow'])) {
  511. $image = ABSOLUTE_PATH.'/'.$this->_data['imageToShow'];
  512. if (file_exists($image)) {
  513. unlink($image);
  514. }
  515. }
  516. }
  517. /**
  518. * remove the local stored snapshot
  519. *
  520. * @return void
  521. */
  522. private function _deleteSnapshot(): void {
  523. if (!empty($this->_data['hash']) && !empty($this->_data['snapshotLink'])) {
  524. $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.webp';
  525. if (file_exists($snapshot)) {
  526. unlink($snapshot);
  527. }
  528. }
  529. }
  530. /**
  531. * remove the local stored pagescreenshot
  532. *
  533. * @return void
  534. */
  535. private function _deletePageScreenshot(): void {
  536. if (!empty($this->_data['hash']) && !empty($this->_data['pagescreenshotLink'])) {
  537. $pagescreenshot = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpeg';
  538. if (file_exists($pagescreenshot)) {
  539. unlink($pagescreenshot);
  540. }
  541. }
  542. }
  543. /**
  544. * check if the status is private and set the info
  545. *
  546. * @return void
  547. */
  548. private function _private(): void {
  549. if (!empty($this->_data['status']) && $this->_data['status'] == "1") {
  550. $this->_data['private'] = "1";
  551. }
  552. }
  553. }