home.inc.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $searchValue = false;
  29. $isUrl = false;
  30. $submitFeedback = array();
  31. $queryStr = false;
  32. $searchResult = false;
  33. $showAddForm = false;
  34. $honeypotCheck = false;
  35. $formData = array();
  36. $_requestMode = '';
  37. if(isset($_GET['m']) && !empty($_GET['m'])) {
  38. $_requestMode = trim($_GET['m']);
  39. $_requestMode = Summoner::validate($_requestMode,'nospace') ? $_requestMode : "all";
  40. }
  41. if($_requestMode === "auth") {
  42. # very simple security check.
  43. # can/should be extended in the future.
  44. Summoner::simpleAuth();
  45. }
  46. if((isset($_POST['password']) && !empty($_POST['password'])) || (isset($_POST['username']) && !empty($_POST['username']))) {
  47. # those are hidden fields. A robot may input these. A valid user does not.
  48. $honeypotCheck = true;
  49. }
  50. # search or new one.
  51. if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['submitsearch']) && $honeypotCheck === false) {
  52. $searchValue = trim($_POST['data']['searchfield']);
  53. $isUrl = Summoner::validate($searchValue,'url');
  54. if($isUrl === true) {
  55. # search for URL
  56. $searchResult = $Management->searchForLinkByURL($searchValue);
  57. }
  58. elseif(Summoner::validate($searchValue,'text')) {
  59. $searchResult = $Management->searchForLinkBySearchData(strtolower($searchValue));
  60. }
  61. else {
  62. $submitFeedback['message'] = $T->t('home.input.invalid');
  63. $submitFeedback['status'] = 'error';
  64. }
  65. # new one?
  66. if(empty($searchResult) && $isUrl === true && Summoner::simpleAuthCheck() === true) {
  67. # try to gather some information automatically
  68. $linkInfo = Summoner::gatherInfoFromURL($searchValue);
  69. if(!empty($linkInfo)) {
  70. if(isset($linkInfo['description'])) {
  71. $formData['description'] = $linkInfo['description'];
  72. }
  73. if(isset($linkInfo['title'])) {
  74. $formData['title'] = $linkInfo['title'];
  75. }
  76. if(isset($linkInfo['image'])) {
  77. $formData['image'] = $linkInfo['image'];
  78. }
  79. }
  80. # show the add form
  81. $showAddForm = true;
  82. $formData['url'] = $searchValue;
  83. $formData['categories'] = array();
  84. $formData['tags'] = array();
  85. }
  86. elseif(!empty($searchResult)) {
  87. # something has been found
  88. }
  89. else {
  90. # nothing found
  91. $submitFeedback['message'] = $T->t('home.input.search.not.found');
  92. $submitFeedback['status'] = 'error';
  93. }
  94. }
  95. # add a new one
  96. if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['addnewone']) && $honeypotCheck === false
  97. && Summoner::simpleAuthCheck() === true
  98. ) {
  99. $fData = $_POST['data'];
  100. $formData['private'] = 2;
  101. if(isset($fData['private'])) {
  102. $formData['private'] = 1;
  103. }
  104. $formData['url'] = trim($fData['url']);
  105. $formData['description'] = trim($fData['description']);
  106. $formData['title'] = trim($fData['title']);
  107. $formData['image'] = trim($fData['image']);
  108. $formData['category'] = trim($fData['category']);
  109. $formData['tag'] = trim($fData['tag']);
  110. # categories and tag stuff
  111. $catArr = Summoner::prepareTagOrCategoryStr($formData['category']);
  112. $tagArr = Summoner::prepareTagOrCategoryStr($formData['tag']);
  113. $formData['categories'] = $catArr;
  114. $formData['tags'] = $tagArr;
  115. $isUrl = Summoner::validate($formData['url'],'url');
  116. if($isUrl === true && !empty($formData['title'])) {
  117. $hash = md5($formData['url']);
  118. $DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  119. $linkObj = new Link($DB);
  120. $linkID = $linkObj->create(array(
  121. 'hash' => $hash,
  122. 'link' => $formData['url'],
  123. 'status' => $formData['private'],
  124. 'description' => $formData['description'],
  125. 'title' => $formData['title'],
  126. 'image' => $formData['image'],
  127. 'tagArr' => $tagArr,
  128. 'catArr' => $catArr
  129. ),true);
  130. if(!empty($linkID)) {
  131. if(!empty($catArr)) {
  132. foreach($catArr as $c) {
  133. $catObj = new Category($DB);
  134. $catObj->initbystring($c);
  135. $catObj->setRelation($linkID);
  136. unset($catObj);
  137. }
  138. }
  139. if(!empty($tagArr)) {
  140. foreach($tagArr as $t) {
  141. $tagObj = new Tag($DB);
  142. $tagObj->initbystring($t);
  143. $tagObj->setRelation($linkID);
  144. unset($tagObj);
  145. }
  146. }
  147. $DB->commit();
  148. $submitFeedback['message'] = $T->t('home.input.added');
  149. $submitFeedback['status'] = 'success';
  150. $TemplateData['refresh'] = 'index.php?p=linkinfo&id='.$hash;
  151. }
  152. else {
  153. $DB->rollback();
  154. $submitFeedback['message'] = $T->t('home.input.add.fail');
  155. $submitFeedback['status'] = 'error';
  156. $showAddForm = true;
  157. }
  158. }
  159. else {
  160. $submitFeedback['message'] = $T->t('home.input.invalid.data');
  161. $submitFeedback['status'] = 'error';
  162. $showAddForm = true;
  163. }
  164. }
  165. if($showAddForm === true) {
  166. $existingCategories = $Management->categories();
  167. $existingTags = $Management->tags();
  168. }
  169. $latestLinks = $Management->latestLinks(50);