management.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 Management
  30. */
  31. class Management {
  32. const LINK_QUERY_STATUS = 2;
  33. /**
  34. * the database object
  35. *
  36. * @var object
  37. */
  38. private $DB;
  39. /**
  40. * Type of links based on status to show
  41. *
  42. * @var bool
  43. */
  44. private $_queryStatus = self::LINK_QUERY_STATUS;
  45. /**
  46. * Management constructor.
  47. *
  48. * @param Object $databaseConnectionObject
  49. * @return void
  50. */
  51. public function __construct($databaseConnectionObject) {
  52. $this->DB = $databaseConnectionObject;
  53. }
  54. /**
  55. * Show private links or not
  56. *
  57. * @param boolean $bool
  58. * @return void
  59. */
  60. public function setShowPrivate(bool $bool) {
  61. $this->_queryStatus = self::LINK_QUERY_STATUS;
  62. if($bool === true) {
  63. $this->_queryStatus = 1;
  64. }
  65. }
  66. /**
  67. * Show awaiting moderation links or not
  68. *
  69. * @param boolean $bool
  70. * @return void
  71. */
  72. public function setShowAwm(bool $bool) {
  73. $this->_queryStatus = self::LINK_QUERY_STATUS;
  74. if($bool === true) {
  75. $this->_queryStatus = 3;
  76. }
  77. }
  78. /**
  79. * get all the available categories from the DB.
  80. * optional limit
  81. * optional stats
  82. *
  83. * @param bool|int $limit
  84. * @param bool $stats
  85. * @return array
  86. */
  87. public function categories($limit=false, $stats=false): array {
  88. $ret = array();
  89. $statsInfo = array();
  90. if($stats === true) {
  91. $queryStr = "SELECT
  92. COUNT(*) AS amount,
  93. cr.categoryid AS categoryId
  94. FROM `".DB_PREFIX."_categoryrelation` AS cr, `".DB_PREFIX."_link` AS t
  95. WHERE cr.linkid = t.id";
  96. $queryStr .= " AND ".$this->_decideLinkTypeForQuery();
  97. $queryStr .= " GROUP BY categoryid";
  98. $query = $this->DB->query($queryStr);
  99. if(!empty($query)) {
  100. while($result = $query->fetch_assoc()) {
  101. $statsInfo[$result['categoryId']] = $result['amount'];
  102. }
  103. }
  104. }
  105. $queryStr = "SELECT `id`, `name`
  106. FROM `".DB_PREFIX."_category`
  107. ORDER BY `name` ASC";
  108. if(!empty($limit)) {
  109. $queryStr .= " LIMIT $limit";
  110. }
  111. $query = $this->DB->query($queryStr);
  112. if(!empty($query)) {
  113. while($result = $query->fetch_assoc()) {
  114. if($stats === true && isset($statsInfo[$result['id']])) {
  115. $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]);
  116. }
  117. else {
  118. $ret[$result['id']] = array('name' => $result['name'], 'amount' => 0);
  119. }
  120. }
  121. }
  122. return $ret;
  123. }
  124. /**
  125. * get all the available tags from the DB.
  126. * optional limit
  127. * optional stats
  128. *
  129. * @param bool|int $limit
  130. * @param bool $stats
  131. * @return array
  132. */
  133. public function tags($limit=false, $stats=false): array {
  134. $ret = array();
  135. $statsInfo = array();
  136. if($stats === true) {
  137. $queryStr = "SELECT COUNT(*) AS amount,
  138. tr.tagid AS tagId
  139. FROM `".DB_PREFIX."_tagrelation` AS tr, `".DB_PREFIX."_link` AS t
  140. WHERE tr.linkid = t.id";
  141. $queryStr .= " AND ".$this->_decideLinkTypeForQuery();
  142. $queryStr .= " GROUP BY tagId";
  143. $query = $this->DB->query($queryStr);
  144. if(!empty($query)) {
  145. while($result = $query->fetch_assoc()) {
  146. $statsInfo[$result['tagId']] = $result['amount'];
  147. }
  148. }
  149. }
  150. $queryStr = "SELECT `id`, `name`
  151. FROM `".DB_PREFIX."_tag`
  152. ORDER BY `name` ASC";
  153. if(!empty($limit)) {
  154. $queryStr .= " LIMIT $limit";
  155. }
  156. $query = $this->DB->query($queryStr);
  157. if(!empty($query)) {
  158. while($result = $query->fetch_assoc()) {
  159. if($stats === true && isset($statsInfo[$result['id']])) {
  160. $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]);
  161. }
  162. else {
  163. $ret[$result['id']] = array('name' => $result['name'], 'amount' => 0);
  164. }
  165. }
  166. }
  167. return $ret;
  168. }
  169. /**
  170. * return the latest added links
  171. *
  172. * @param int $limit
  173. * @return array
  174. */
  175. public function latestLinks($limit=5): array {
  176. $ret = array();
  177. $queryStr = "SELECT `title`, `link` FROM `".DB_PREFIX."_link` AS t";
  178. $queryStr .= " WHERE ".$this->_decideLinkTypeForQuery();
  179. $queryStr .= " ORDER BY `created` DESC";
  180. if(!empty($limit)) {
  181. $queryStr .= " LIMIT $limit";
  182. }
  183. $query = $this->DB->query($queryStr);
  184. if(!empty($query) && $query->num_rows > 0) {
  185. $ret = $query->fetch_all(MYSQLI_ASSOC);
  186. }
  187. return $ret;
  188. }
  189. /**
  190. * Return a random entry from link table.
  191. * Slow but does the trick for now. If there is way more entries
  192. * re-think this solution
  193. *
  194. * @param int $limit
  195. * @return array
  196. */
  197. public function randomLink($limit=1): array {
  198. $ret = array();
  199. $queryStr = "SELECT `title`, `link`, `hash` FROM `".DB_PREFIX."_link` AS t";
  200. $queryStr .= " WHERE ".$this->_decideLinkTypeForQuery();
  201. $queryStr .= " ORDER BY RAND()";
  202. if(!empty($limit)) {
  203. $queryStr .= " LIMIT $limit";
  204. }
  205. $query = $this->DB->query($queryStr);
  206. if(!empty($query) && $query->num_rows > 0) {
  207. $ret = $query->fetch_all(MYSQLI_ASSOC);
  208. }
  209. return $ret;
  210. }
  211. public function randomCategory($limit=1): array {
  212. $ret = array();
  213. $queryStr = "SELECT `id`, `name` FROM `".DB_PREFIX."_category`";
  214. $queryStr .= " ORDER BY RAND()";
  215. if(!empty($limit)) {
  216. $queryStr .= " LIMIT $limit";
  217. }
  218. $query = $this->DB->query($queryStr);
  219. if(!empty($query) && $query->num_rows > 0) {
  220. $ret = $query->fetch_all(MYSQLI_ASSOC);
  221. }
  222. return $ret;
  223. }
  224. public function randomTag($limit=1): array {
  225. $ret = array();
  226. $queryStr = "SELECT `id`, `name` FROM `".DB_PREFIX."_tag`";
  227. $queryStr .= " ORDER BY RAND()";
  228. if(!empty($limit)) {
  229. $queryStr .= " LIMIT $limit";
  230. }
  231. $query = $this->DB->query($queryStr);
  232. if(!empty($query) && $query->num_rows > 0) {
  233. $ret = $query->fetch_all(MYSQLI_ASSOC);
  234. }
  235. return $ret;
  236. }
  237. /**
  238. * get all the categories ordered by link added date
  239. *
  240. * @return array
  241. */
  242. public function categoriesByDateAdded(): array {
  243. $ret = array();
  244. $categories = $this->categories();
  245. foreach($categories as $k=>$v) {
  246. $latestLink = $this->latestLinkForCategory($k);
  247. if(!empty($latestLink)) {
  248. array_push($ret, array('created' => $latestLink[0]['created'], 'id' => $k, 'name' => $v['name']));
  249. }
  250. }
  251. $_created = array_column($ret, 'created');
  252. array_multisort($_created, SORT_DESC, $ret);
  253. return $ret;
  254. }
  255. /**
  256. * find all links by given category string or id.
  257. * Return array sorted by creation date DESC
  258. *
  259. * @param int $id Category ID
  260. * @param array $options Array with limit|offset|sort|sortDirection
  261. * @return array
  262. */
  263. public function linksByCategory(int $id, $options=array()): array {
  264. $ret = array();
  265. if(!isset($options['limit'])) $options['limit'] = 5;
  266. if(!isset($options['offset'])) $options['offset'] = false;
  267. if(!isset($options['sort'])) $options['sort'] = false;
  268. if(!isset($options['sortDirection'])) $options['sortDirection'] = false;
  269. $querySelect = "SELECT `id`, `link`, `created`, `status`, `title`, `hash`, `description`, `image`";
  270. $queryFrom = " FROM `".DB_PREFIX."_link` AS t
  271. LEFT JOIN insipid_categoryrelation AS cr ON cr.linkid = t.id";
  272. $queryWhere = " WHERE ".$this->_decideLinkTypeForQuery();
  273. if(!empty($id) && is_numeric($id)) {
  274. $queryWhere .= " AND cr.categoryId = '" . $this->DB->real_escape_string($id) . "'";
  275. }
  276. else {
  277. return $ret;
  278. }
  279. $queryOrder = " ORDER BY";
  280. if(!empty($options['sort'])) {
  281. $queryOrder .= ' t.'.$options['sort'];
  282. }
  283. else {
  284. $queryOrder .= " t.created";
  285. }
  286. if(!empty($options['sortDirection'])) {
  287. $queryOrder .= ' '.$options['sortDirection'];
  288. }
  289. else {
  290. $queryOrder .= " DESC";
  291. }
  292. $queryLimit = '';
  293. # this allows the set the limit to false
  294. if(!empty($options['limit'])) {
  295. $queryLimit .= " LIMIT ".$options['limit'];
  296. # offset can be 0
  297. if($options['offset'] !== false) {
  298. $queryLimit .= " OFFSET ".$options['offset'];
  299. }
  300. }
  301. $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
  302. if(!empty($query) && $query->num_rows > 0) {
  303. while($result = $query->fetch_assoc()) {
  304. $linkObj = new Link($this->DB);
  305. $ret['results'][] = $linkObj->loadFromDataShortInfo($result);
  306. unset($linkObj);
  307. }
  308. $query = $this->DB->query("SELECT COUNT(DISTINCT(t.hash)) AS amount ".$queryFrom.$queryWhere);
  309. $result = $query->fetch_assoc();
  310. $ret['amount'] = $result['amount'];
  311. }
  312. return $ret;
  313. }
  314. /**
  315. * find all links by given tag string or id.
  316. * Return array sorted by creation date DESC
  317. *
  318. * @param int $id Tag id
  319. * @param array $options Array with limit|offset|sort|sortDirection
  320. * @return array
  321. */
  322. public function linksByTag(int $id, $options=array()): array {
  323. $ret = array();
  324. if(!isset($options['limit'])) $options['limit'] = 5;
  325. if(!isset($options['offset'])) $options['offset'] = false;
  326. if(!isset($options['sort'])) $options['sort'] = false;
  327. if(!isset($options['sortDirection'])) $options['sortDirection'] = false;
  328. $querySelect = "SELECT `id`, `link`, `created`, `status`, `title`, `hash`, `description`, `image`";
  329. $queryFrom = " FROM `".DB_PREFIX."_link` AS t
  330. LEFT JOIN insipid_tagrelation AS tr ON tr.linkid = t.id";
  331. $queryWhere = " WHERE ".$this->_decideLinkTypeForQuery();
  332. if(!empty($id) && is_numeric($id)) {
  333. $queryWhere .= " AND tr.tagId = '".$this->DB->real_escape_string($id)."'";
  334. }
  335. else {
  336. return $ret;
  337. }
  338. $queryOrder = " ORDER BY";
  339. if(!empty($options['sort'])) {
  340. $queryOrder .= ' t.'.$options['sort'];
  341. }
  342. else {
  343. $queryOrder .= " t.created";
  344. }
  345. if(!empty($options['sortDirection'])) {
  346. $queryOrder .= ' '.$options['sortDirection'];
  347. }
  348. else {
  349. $queryOrder .= " DESC";
  350. }
  351. $queryLimit = '';
  352. # this allows the set the limit to false
  353. if(!empty($options['limit'])) {
  354. $queryLimit .= " LIMIT ".$options['limit'];
  355. # offset can be 0
  356. if($options['offset'] !== false) {
  357. $queryLimit .= " OFFSET ".$options['offset'];
  358. }
  359. }
  360. $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
  361. if(!empty($query) && $query->num_rows > 0) {
  362. while($result = $query->fetch_assoc()) {
  363. $linkObj = new Link($this->DB);
  364. $ret['results'][] = $linkObj->loadFromDataShortInfo($result);
  365. unset($linkObj);
  366. }
  367. $query = $this->DB->query("SELECT COUNT(DISTINCT(t.hash)) AS amount ".$queryFrom.$queryWhere);
  368. $result = $query->fetch_assoc();
  369. $ret['amount'] = $result['amount'];
  370. }
  371. return $ret;
  372. }
  373. /**
  374. * return all links and Info we have from the combined view
  375. *
  376. * @param bool|int $limit
  377. * @param bool $offset
  378. * @return array
  379. */
  380. public function links($limit=10,$offset=false): array {
  381. $ret = array();
  382. $querySelect = "SELECT `hash`";
  383. $queryFrom = " FROM `".DB_PREFIX."_link` AS t";
  384. $queryWhere = " WHERE ".$this->_decideLinkTypeForQuery();
  385. $queryOrder = " ORDER BY `created` DESC";
  386. $queryLimit = "";
  387. if(!empty($limit)) {
  388. $queryLimit = " LIMIT $limit";
  389. if($offset !== false) {
  390. $queryLimit .= " OFFSET $offset";
  391. }
  392. }
  393. $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
  394. if(!empty($query) && $query->num_rows > 0) {
  395. while($result = $query->fetch_assoc()) {
  396. $linkObj = new Link($this->DB);
  397. $ret['results'][] = $linkObj->loadShortInfo($result['hash']);
  398. unset($linkObj);
  399. }
  400. $query = $this->DB->query("SELECT COUNT(t.hash) AS amount ".$queryFrom.$queryWhere);
  401. $result = $query->fetch_assoc();
  402. $ret['amount'] = $result['amount'];
  403. }
  404. return $ret;
  405. }
  406. /**
  407. * return the latest added link for given category id
  408. *
  409. * @param int $categoryid
  410. * @return array
  411. */
  412. public function latestLinkForCategory(int $categoryid): array {
  413. $ret = array();
  414. if(!empty($categoryid) && is_numeric($categoryid)) {
  415. $queryStr = "SELECT `id`, `link`, `created`, `status`, `description`, `title`, `image`, `hash`,
  416. `tag`, `category`, `categoryId`, `tagId`
  417. FROM `".DB_PREFIX."_combined` AS t";
  418. $queryStr .= " WHERE ".$this->_decideLinkTypeForQuery();
  419. $queryStr .= " AND t.categoryId = '" . $this->DB->real_escape_string($categoryid) . "'
  420. ORDER BY t.created DESC
  421. LIMIT 1";
  422. $query = $this->DB->query($queryStr);
  423. if(!empty($query) && $query->num_rows > 0) {
  424. $ret = $query->fetch_all(MYSQLI_ASSOC);
  425. }
  426. }
  427. return $ret;
  428. }
  429. /**
  430. * Search for the given url in the links table
  431. *
  432. * @param string $url
  433. * @return array
  434. */
  435. public function searchForLinkByURL(string $url): array {
  436. $ret = array();
  437. if(!empty($url)) {
  438. $queryStr = "SELECT * FROM `".DB_PREFIX."_link` AS t";
  439. $queryStr .= " WHERE ".$this->_decideLinkTypeForQuery();
  440. $queryStr .= " AND t.link = '".$this->DB->real_escape_string($url)."'";
  441. $query = $this->DB->query($queryStr);
  442. if(!empty($query) && $query->num_rows > 0) {
  443. $ret = $query->fetch_all(MYSQLI_ASSOC);
  444. }
  445. }
  446. return $ret;
  447. }
  448. /**
  449. * search for given searchstring in the search data of the links
  450. *
  451. * @param string $searchStr
  452. * @return array
  453. */
  454. public function searchForLinkBySearchData(string $searchStr): array {
  455. $ret = array();
  456. if(!empty($searchStr)) {
  457. $queryStr = "SELECT *,
  458. MATCH (`search`) AGAINST ('".$this->DB->real_escape_string($searchStr)."' IN BOOLEAN MODE) AS score
  459. FROM `".DB_PREFIX."_link` AS t
  460. WHERE MATCH (`search`) AGAINST ('".$this->DB->real_escape_string($searchStr)."' IN BOOLEAN MODE)";
  461. $queryStr .= " AND ".$this->_decideLinkTypeForQuery();
  462. $queryStr .= " ORDER BY score DESC";
  463. $query = $this->DB->query($queryStr);
  464. if(!empty($query) && $query->num_rows > 0) {
  465. $ret = $query->fetch_all(MYSQLI_ASSOC);
  466. }
  467. }
  468. return $ret;
  469. }
  470. /**
  471. * amount of links in the DB. Status 1 and 2 only
  472. *
  473. * @return int
  474. */
  475. public function linkAmount(): int {
  476. $ret = 0;
  477. $queryStr = "SELECT COUNT(*) AS amount
  478. FROM `".DB_PREFIX."_link` AS t";
  479. $queryStr .= " WHERE ".$this->_decideLinkTypeForQuery();
  480. $query = $this->DB->query($queryStr);
  481. if(!empty($query) && $query->num_rows > 0) {
  482. $result = $query->fetch_assoc();
  483. $ret = $result['amount'];
  484. }
  485. return $ret;
  486. }
  487. /**
  488. * amount of tags
  489. *
  490. * @return int
  491. */
  492. public function tagAmount(): int {
  493. $ret = 0;
  494. $queryStr = "SELECT COUNT(*) AS amount FROM `".DB_PREFIX."_tag`";
  495. $query = $this->DB->query($queryStr);
  496. if(!empty($query) && $query->num_rows > 0) {
  497. $result = $query->fetch_assoc();
  498. $ret = $result['amount'];
  499. }
  500. return $ret;
  501. }
  502. /**
  503. * amount of categories
  504. *
  505. * @return int
  506. */
  507. public function categoryAmount(): int {
  508. $ret = 0;
  509. $queryStr = "SELECT COUNT(*) AS amount FROM `".DB_PREFIX."_category`";
  510. $query = $this->DB->query($queryStr);
  511. if(!empty($query) && $query->num_rows > 0) {
  512. $result = $query->fetch_assoc();
  513. $ret = $result['amount'];
  514. }
  515. return $ret;
  516. }
  517. /**
  518. * Amount of links need moderation
  519. *
  520. * @return int
  521. */
  522. public function moderationAmount(): int {
  523. $ret = 0;
  524. $queryStr = "SELECT COUNT(*) AS amount FROM `".DB_PREFIX."_link`";
  525. $queryStr .= " WHERE `status` = 3";
  526. $query = $this->DB->query($queryStr);
  527. if(!empty($query) && $query->num_rows > 0) {
  528. $result = $query->fetch_assoc();
  529. $ret = $result['amount'];
  530. }
  531. return $ret;
  532. }
  533. /**
  534. * get the used disk space for local image storage
  535. *
  536. * @return int
  537. */
  538. public function storageAmount(): int {
  539. $ret = 0;
  540. $_storageFolder = ABSOLUTE_PATH.'/'.LOCAL_STORAGE;
  541. if(file_exists($_storageFolder) && is_readable($_storageFolder)) {
  542. $ret = Summoner::folderSize($_storageFolder);
  543. }
  544. return $ret;
  545. }
  546. /**
  547. * empties the local storage directory
  548. *
  549. * @return bool
  550. */
  551. public function clearLocalStorage(): bool {
  552. $ret = false;
  553. $_storageFolder = ABSOLUTE_PATH.'/'.LOCAL_STORAGE;
  554. if(file_exists($_storageFolder) && is_writable($_storageFolder)) {
  555. $ret = Summoner::recursive_remove_directory($_storageFolder,true);
  556. }
  557. return $ret;
  558. }
  559. /**
  560. * Load link by given hash. Do not use Link class directly.
  561. * Otherwise the authentication will be ignored.
  562. *
  563. * @param String $hash Link hash
  564. * @param bool $fullInfo Load all the info we have
  565. * @param bool $withObject An array with data and the link obj itself
  566. * @return array
  567. */
  568. public function loadLink(string $hash, $fullInfo=true, $withObject=false): array {
  569. $ret = array();
  570. if (!empty($hash)) {
  571. $querySelect = "SELECT `hash`";
  572. $queryFrom = " FROM `".DB_PREFIX."_link` AS t";
  573. $queryWhere = " WHERE ".$this->_decideLinkTypeForQuery();
  574. $queryWhere .= " AND t.hash = '".$this->DB->real_escape_string($hash)."'";
  575. $query = $this->DB->query($querySelect.$queryFrom.$queryWhere);
  576. if (!empty($query) && $query->num_rows == 1) {
  577. $linkObj = new Link($this->DB);
  578. if($fullInfo === true) {
  579. $ret = $linkObj->load($hash);
  580. }
  581. else {
  582. $ret = $linkObj->loadShortInfo($hash);
  583. }
  584. if($withObject === true) {
  585. $ret = array(
  586. 'data' => $ret,
  587. 'obj' => $linkObj
  588. );
  589. }
  590. }
  591. }
  592. return $ret;
  593. }
  594. /**
  595. * Delete link by given hash
  596. *
  597. * @param string $hash
  598. * @return bool
  599. */
  600. public function deleteLink(string $hash): bool {
  601. $ret = false;
  602. if (!empty($hash)) {
  603. $linkData = $this->loadLink($hash,false,true);
  604. if(!empty($linkData)) {
  605. $linkData['obj']->deleteRelations();
  606. $queryStr = "DELETE FROM `" . DB_PREFIX . "_link`
  607. WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'";
  608. $query = $this->DB->query($queryStr);
  609. if (!empty($query)) {
  610. $ret = true;
  611. }
  612. }
  613. }
  614. return $ret;
  615. }
  616. /**
  617. * Export given link for download as a xml file
  618. *
  619. * @param string $hash
  620. * @param bool|Link $linkObj Use already existing link obj
  621. * @return bool
  622. */
  623. public function exportLinkData(string $hash, $linkObj=false): bool {
  624. $ret = false;
  625. if (!empty($hash)) {
  626. $linkData = $this->loadLink($hash, true, true);
  627. if (!empty($linkData)) {
  628. $data = $linkData;
  629. }
  630. }
  631. elseif(!empty($linkObj) && is_a($linkObj,'Link')) {
  632. $data = $linkObj->getData();
  633. }
  634. if(!empty($data) && isset($data['link'])) {
  635. require_once 'lib/import-export.class.php';
  636. $ImEx = new ImportExport();
  637. $ret = $ImEx->createSingleLinkExportXML($data);
  638. }
  639. return $ret;
  640. }
  641. /**
  642. * for simpler management we have the search data in a separate column
  643. * it is not fancy or even technical nice but it damn works
  644. *
  645. * @return boolean
  646. */
  647. public function updateSearchIndex(): bool {
  648. $ret = false;
  649. $allLinks = array();
  650. $queryStr = "SELECT hash FROM `".DB_PREFIX."_link`";
  651. $query = $this->DB->query($queryStr);
  652. if(!empty($query) && $query->num_rows > 0) {
  653. $allLinks = $query->fetch_all(MYSQLI_ASSOC);
  654. }
  655. if(!empty($allLinks)) {
  656. foreach($allLinks as $link) {
  657. $LinkObj = new Link($this->DB);
  658. $l = $LinkObj->load($link['hash']);
  659. $_t = parse_url($l['link']);
  660. $searchStr = $l['title'];
  661. $searchStr .= ' '.$l['description'];
  662. $searchStr .= ' '.implode(' ',$l['tags']);
  663. $searchStr .= ' '.implode(' ',$l['categories']);
  664. $searchStr .= ' '.$_t['host'];
  665. $searchStr .= ' '.implode(' ',explode('/',$_t['path']));
  666. $searchStr = trim($searchStr);
  667. $searchStr = strtolower($searchStr);
  668. # now update the search string
  669. $queryStr = "UPDATE `".DB_PREFIX."_link`
  670. SET `search` = '".$this->DB->real_escape_string($searchStr)."'
  671. WHERE `hash` = '".$this->DB->real_escape_string($link['hash'])."'";
  672. $this->DB->query($queryStr);
  673. unset($LinkObj,$l,$searchStr,$t,$c,$queryStr);
  674. }
  675. $ret = true;
  676. }
  677. return $ret;
  678. }
  679. /**
  680. * process the given xml file. Based on the export file
  681. * options are overwrite => true|false
  682. *
  683. * @param string $file
  684. * @param array $options
  685. * @return array
  686. */
  687. public function processImportFile(string $file, array $options): array {
  688. $ret = array(
  689. 'status' => 'error',
  690. 'message' => 'Processing error'
  691. );
  692. $links = array();
  693. require_once 'lib/import-export.class.php';
  694. $ImEx = new ImportExport();
  695. try {
  696. $ImEx->loadImportFile($file);
  697. $links = $ImEx->parseImportFile();
  698. }
  699. catch (Exception $e) {
  700. $ret['message'] = $e->getMessage();
  701. }
  702. $_existing = 0;
  703. $_new = 0;
  704. if(!empty($links)) {
  705. $_amount = count($links);
  706. foreach($links as $linkToImport) {
  707. $do = false;
  708. if($this->_linkExistsById($linkToImport['id'])) {
  709. if(isset($options['overwrite']) && $options['overwrite'] === true) {
  710. $linkObj = new Link($this->DB);
  711. $linkObj->load($linkToImport['hash']);
  712. $do = $linkObj->update($linkToImport);
  713. }
  714. $_existing++;
  715. }
  716. else {
  717. $linkObj = new Link($this->DB);
  718. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  719. try{
  720. $do = $linkObj->create(array(
  721. 'hash' => $linkToImport['hash'],
  722. 'link' => $linkToImport['link'],
  723. 'status' => $linkToImport['private'],
  724. 'description' => $linkToImport['description'],
  725. 'title' => $linkToImport['title'],
  726. 'image' => $linkToImport['image']
  727. ), true);
  728. } catch (Exception $e) {
  729. continue;
  730. }
  731. if(!empty($do)) {
  732. $linkToImport['catArr'] = Summoner::prepareTagOrCategoryStr($linkToImport['category']);
  733. $linkToImport['tagArr'] = Summoner::prepareTagOrCategoryStr($linkToImport['tag']);
  734. if(!empty($linkToImport['catArr'])) {
  735. foreach($linkToImport['catArr'] as $c) {
  736. $catObj = new Category($this->DB);
  737. $catObj->initbystring($c);
  738. $catObj->setRelation($do);
  739. unset($catObj);
  740. }
  741. }
  742. if(!empty($linkToImport['tagArr'])) {
  743. foreach($linkToImport['tagArr'] as $t) {
  744. $tagObj = new Tag($this->DB);
  745. $tagObj->initbystring($t);
  746. $tagObj->setRelation($do);
  747. unset($tagObj);
  748. }
  749. }
  750. $this->DB->commit();
  751. $this->updateSearchIndex();
  752. }
  753. else {
  754. $this->DB->rollback();
  755. }
  756. $_new++;
  757. }
  758. }
  759. if(isset($options['overwrite']) && $options['overwrite'] === true) {
  760. $_msg = "Found $_amount link(s) to import. Overwritten $_existing existing and imported $_new new one(s).";
  761. }
  762. else {
  763. $_msg = "Found $_amount link(s) to import. Skipped $_existing existing and imported $_new new one(s).";
  764. }
  765. $ret = array(
  766. 'status' => 'success',
  767. 'message' => $_msg
  768. );
  769. }
  770. return $ret;
  771. }
  772. /**
  773. * Return the query string for the correct status type
  774. *
  775. * @return string
  776. */
  777. private function _decideLinkTypeForQuery(): string {
  778. switch ($this->_queryStatus) {
  779. case 1:
  780. $ret = "t.status IN (2,1)";
  781. break;
  782. case 3:
  783. $ret = "t.status = 3";
  784. break;
  785. default:
  786. $ret = "t.status = 2";
  787. }
  788. return $ret;
  789. }
  790. /**
  791. * Check if given id (not hash) exists in link database
  792. *
  793. * @param integer $id
  794. * @return bool
  795. */
  796. private function _linkExistsById($id): bool {
  797. $ret = false;
  798. if(!empty($id)) {
  799. $queryStr = "SELECT `id`
  800. FROM `" . DB_PREFIX . "_link`
  801. WHERE `id` = '" . $this->DB->real_escape_string($id) . "'";
  802. $query = $this->DB->query($queryStr);
  803. if(!empty($query) && $query->num_rows > 0) {
  804. $ret = true;
  805. }
  806. }
  807. return $ret;
  808. }
  809. }