link.class.php 15 KB

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