home.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 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 = false;
  31. $queryStr = false;
  32. $searchResult = false;
  33. $showAddForm = false;
  34. $formData = false;
  35. $honeypotCheck = false;
  36. $_requestMode = false;
  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. $searchValue = strtolower($searchValue);
  54. $isUrl = Summoner::validate($searchValue,'url');
  55. if($isUrl === true) {
  56. # search for URL
  57. $searchValue = trim($searchValue, "/");
  58. $searchResult = $Management->searchForLinkByURL($searchValue);
  59. }
  60. elseif(Summoner::validate($searchValue,'text')) {
  61. $searchResult = $Management->searchForLinkBySearchData($searchValue);
  62. }
  63. else {
  64. $submitFeedback['message'] = 'Invalid input';
  65. $submitFeedback['status'] = 'error';
  66. }
  67. # new one?
  68. if(empty($searchResult) && $isUrl === true && Summoner::simpleAuthCheck() === true) {
  69. # try to gather some information automatically
  70. $linkInfo = Summoner::gatherInfoFromURL($searchValue);
  71. if(!empty($linkInfo)) {
  72. if(isset($linkInfo['description'])) {
  73. $formData['description'] = $linkInfo['description'];
  74. }
  75. if(isset($linkInfo['title'])) {
  76. $formData['title'] = $linkInfo['title'];
  77. }
  78. if(isset($linkInfo['image'])) {
  79. $formData['image'] = $linkInfo['image'];
  80. }
  81. }
  82. # show the add form
  83. $showAddForm = true;
  84. $formData['url'] = $searchValue;
  85. }
  86. elseif(!empty($searchResult)) {
  87. # something has been found
  88. }
  89. else {
  90. # nothing found
  91. $submitFeedback['message'] = 'Nothing 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. $isUrl = Summoner::validate($formData['url'],'url');
  111. if($isUrl === true && !empty($formData['title'])) {
  112. $hash = md5($formData['url']);
  113. # categories and tag stuff
  114. $catArr = Summoner::prepareTagOrCategoryStr($formData['category']);
  115. $tagArr = Summoner::prepareTagOrCategoryStr($formData['tag']);
  116. $search = $formData['title'];
  117. $search .= ' '.$formData['description'];
  118. $search .= ' '.implode(" ",$tagArr);
  119. $search .= ' '.implode(" ",$catArr);
  120. $search .= trim($search);
  121. $search = strtolower($search);
  122. $DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  123. $linkObj = new Link($DB);
  124. $linkID = $linkObj->create(array(
  125. 'hash' => $hash,
  126. 'search' => $search,
  127. 'link' => $formData['url'],
  128. 'status' => $formData['private'],
  129. 'description' => $formData['description'],
  130. 'title' => $formData['title'],
  131. 'image' => $formData['image']
  132. ),true);
  133. if(!empty($linkID)) {
  134. if(!empty($catArr)) {
  135. foreach($catArr as $c) {
  136. $catObj = new Category($DB);
  137. $catObj->initbystring($c);
  138. $catObj->setRelation($linkID);
  139. unset($catObj);
  140. }
  141. }
  142. if(!empty($tagArr)) {
  143. foreach($tagArr as $t) {
  144. $tagObj = new Tag($DB);
  145. $tagObj->initbystring($t);
  146. $tagObj->setRelation($linkID);
  147. unset($tagObj);
  148. }
  149. }
  150. $DB->commit();
  151. $submitFeedback['message'] = 'Link added successfully.';
  152. $submitFeedback['status'] = 'success';
  153. $TemplateData['refresh'] = 'index.php?p=linkinfo&id='.$hash;
  154. }
  155. else {
  156. $DB->rollback();
  157. $submitFeedback['message'] = 'Something went wrong...';
  158. $submitFeedback['status'] = 'error';
  159. $showAddForm = true;
  160. }
  161. }
  162. else {
  163. $submitFeedback['message'] = 'Please provide a valid URL and title.';
  164. $submitFeedback['status'] = 'error';
  165. $showAddForm = true;
  166. }
  167. }
  168. $existingCategories = $Management->categories();
  169. $existingTags = $Management->tags();
  170. $latestLinks = $Management->latestLinks(20);
  171. $orderedCategories = $Management->categoriesByDateAdded();