link.class.php 15 KB

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