aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthur <arthur@hoa.ro>2016-01-06 19:57:42 +0100
committerArthur <arthur@hoa.ro>2016-01-06 19:57:42 +0100
commit88c15abb2a2b24f05ca926c0ddbdec18a407e47d (patch)
tree90f783c69dc51866dc01dc34e740023bd1e0c4ec /index.php
parenta327d891b3a2762bb6aabba3a6572b077f6003c0 (diff)
parenteefb636cea7acef9ddfd02a90749820f5fafc9f6 (diff)
downloadShaarli-88c15abb2a2b24f05ca926c0ddbdec18a407e47d.tar.gz
Shaarli-88c15abb2a2b24f05ca926c0ddbdec18a407e47d.tar.zst
Shaarli-88c15abb2a2b24f05ca926c0ddbdec18a407e47d.zip
Merge pull request #424 from ArthurHoaro/search
Link filter refactoring
Diffstat (limited to 'index.php')
-rw-r--r--index.php210
1 files changed, 117 insertions, 93 deletions
diff --git a/index.php b/index.php
index 1a83ca40..cd83600b 100644
--- a/index.php
+++ b/index.php
@@ -151,6 +151,7 @@ require_once 'application/CachedPage.php';
151require_once 'application/FileUtils.php'; 151require_once 'application/FileUtils.php';
152require_once 'application/HttpUtils.php'; 152require_once 'application/HttpUtils.php';
153require_once 'application/LinkDB.php'; 153require_once 'application/LinkDB.php';
154require_once 'application/LinkFilter.php';
154require_once 'application/TimeZone.php'; 155require_once 'application/TimeZone.php';
155require_once 'application/Url.php'; 156require_once 'application/Url.php';
156require_once 'application/Utils.php'; 157require_once 'application/Utils.php';
@@ -730,18 +731,23 @@ function showRSS()
730 // Read links from database (and filter private links if user it not logged in). 731 // Read links from database (and filter private links if user it not logged in).
731 732
732 // Optionally filter the results: 733 // Optionally filter the results:
733 $linksToDisplay=array(); 734 if (!empty($_GET['searchterm'])) {
734 if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); 735 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']);
735 else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); 736 }
736 else $linksToDisplay = $LINKSDB; 737 elseif (!empty($_GET['searchtags'])) {
738 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags']));
739 }
740 else {
741 $linksToDisplay = $LINKSDB;
742 }
737 743
738 $nblinksToDisplay = 50; // Number of links to display. 744 $nblinksToDisplay = 50; // Number of links to display.
739 if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. 745 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
740 { 746 if (!empty($_GET['nb'])) {
741 $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ; 747 $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1);
742 } 748 }
743 749
744 $pageaddr=escape(index_url($_SERVER)); 750 $pageaddr = escape(index_url($_SERVER));
745 echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'; 751 echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">';
746 echo '<channel><title>'.$GLOBALS['title'].'</title><link>'.$pageaddr.'</link>'; 752 echo '<channel><title>'.$GLOBALS['title'].'</title><link>'.$pageaddr.'</link>';
747 echo '<description>Shared links</description><language>en-en</language><copyright>'.$pageaddr.'</copyright>'."\n\n"; 753 echo '<description>Shared links</description><language>en-en</language><copyright>'.$pageaddr.'</copyright>'."\n\n";
@@ -821,15 +827,20 @@ function showATOM()
821 ); 827 );
822 828
823 // Optionally filter the results: 829 // Optionally filter the results:
824 $linksToDisplay=array(); 830 if (!empty($_GET['searchterm'])) {
825 if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); 831 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']);
826 else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); 832 }
827 else $linksToDisplay = $LINKSDB; 833 else if (!empty($_GET['searchtags'])) {
834 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags']));
835 }
836 else {
837 $linksToDisplay = $LINKSDB;
838 }
828 839
829 $nblinksToDisplay = 50; // Number of links to display. 840 $nblinksToDisplay = 50; // Number of links to display.
830 if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. 841 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
831 { 842 if (!empty($_GET['nb'])) {
832 $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ; 843 $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1);
833 } 844 }
834 845
835 $pageaddr=escape(index_url($_SERVER)); 846 $pageaddr=escape(index_url($_SERVER));
@@ -1024,7 +1035,7 @@ function showDaily($pageBuilder)
1024 } 1035 }
1025 1036
1026 try { 1037 try {
1027 $linksToDisplay = $LINKSDB->filterDay($day); 1038 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day);
1028 } catch (Exception $exc) { 1039 } catch (Exception $exc) {
1029 error_log($exc); 1040 error_log($exc);
1030 $linksToDisplay = array(); 1041 $linksToDisplay = array();
@@ -1149,13 +1160,17 @@ function renderPage()
1149 if ($targetPage == Router::$PAGE_PICWALL) 1160 if ($targetPage == Router::$PAGE_PICWALL)
1150 { 1161 {
1151 // Optionally filter the results: 1162 // Optionally filter the results:
1152 $links=array(); 1163 if (!empty($_GET['searchterm'])) {
1153 if (!empty($_GET['searchterm'])) $links = $LINKSDB->filterFulltext($_GET['searchterm']); 1164 $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']);
1154 elseif (!empty($_GET['searchtags'])) $links = $LINKSDB->filterTags(trim($_GET['searchtags'])); 1165 }
1155 else $links = $LINKSDB; 1166 elseif (! empty($_GET['searchtags'])) {
1167 $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags']));
1168 }
1169 else {
1170 $links = $LINKSDB;
1171 }
1156 1172
1157 $body=''; 1173 $linksToDisplay = array();
1158 $linksToDisplay=array();
1159 1174
1160 // Get only links which have a thumbnail. 1175 // Get only links which have a thumbnail.
1161 foreach($links as $link) 1176 foreach($links as $link)
@@ -1282,13 +1297,15 @@ function renderPage()
1282 } 1297 }
1283 1298
1284 if (isset($params['searchtags'])) { 1299 if (isset($params['searchtags'])) {
1285 $tags = explode(' ',$params['searchtags']); 1300 $tags = explode(' ', $params['searchtags']);
1286 $tags=array_diff($tags, array($_GET['removetag'])); // Remove value from array $tags. 1301 // Remove value from array $tags.
1287 if (count($tags)==0) { 1302 $tags = array_diff($tags, array($_GET['removetag']));
1303 $params['searchtags'] = implode(' ',$tags);
1304
1305 if (empty($params['searchtags'])) {
1288 unset($params['searchtags']); 1306 unset($params['searchtags']);
1289 } else {
1290 $params['searchtags'] = implode(' ',$tags);
1291 } 1307 }
1308
1292 unset($params['page']); // We also remove page (keeping the same page has no sense, since the results are different) 1309 unset($params['page']); // We also remove page (keeping the same page has no sense, since the results are different)
1293 } 1310 }
1294 header('Location: ?'.http_build_query($params)); 1311 header('Location: ?'.http_build_query($params));
@@ -1468,7 +1485,8 @@ function renderPage()
1468 // Delete a tag: 1485 // Delete a tag:
1469 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { 1486 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
1470 $needle=trim($_POST['fromtag']); 1487 $needle=trim($_POST['fromtag']);
1471 $linksToAlter = $LINKSDB->filterTags($needle,true); // True for case-sensitive tag search. 1488 // True for case-sensitive tag search.
1489 $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true);
1472 foreach($linksToAlter as $key=>$value) 1490 foreach($linksToAlter as $key=>$value)
1473 { 1491 {
1474 $tags = explode(' ',trim($value['tags'])); 1492 $tags = explode(' ',trim($value['tags']));
@@ -1484,7 +1502,8 @@ function renderPage()
1484 // Rename a tag: 1502 // Rename a tag:
1485 if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { 1503 if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
1486 $needle=trim($_POST['fromtag']); 1504 $needle=trim($_POST['fromtag']);
1487 $linksToAlter = $LINKSDB->filterTags($needle,true); // true for case-sensitive tag search. 1505 // True for case-sensitive tag search.
1506 $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true);
1488 foreach($linksToAlter as $key=>$value) 1507 foreach($linksToAlter as $key=>$value)
1489 { 1508 {
1490 $tags = explode(' ',trim($value['tags'])); 1509 $tags = explode(' ',trim($value['tags']));
@@ -1865,81 +1884,78 @@ function importFile()
1865function buildLinkList($PAGE,$LINKSDB) 1884function buildLinkList($PAGE,$LINKSDB)
1866{ 1885{
1867 // ---- Filter link database according to parameters 1886 // ---- Filter link database according to parameters
1868 $linksToDisplay=array(); 1887 $search_type = '';
1869 $search_type=''; 1888 $search_crits = '';
1870 $search_crits=''; 1889 $privateonly = !empty($_SESSION['privateonly']) ? true : false;
1871 if (isset($_GET['searchterm'])) // Fulltext search 1890
1872 { 1891 // Fulltext search
1873 $linksToDisplay = $LINKSDB->filterFulltext(trim($_GET['searchterm'])); 1892 if (isset($_GET['searchterm'])) {
1874 $search_crits=escape(trim($_GET['searchterm'])); 1893 $search_crits = escape(trim($_GET['searchterm']));
1875 $search_type='fulltext'; 1894 $search_type = LinkFilter::$FILTER_TEXT;
1876 } 1895 $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly);
1877 elseif (isset($_GET['searchtags'])) // Search by tag 1896 }
1878 { 1897 // Search by tag
1879 $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); 1898 elseif (isset($_GET['searchtags'])) {
1880 $search_crits=explode(' ',escape(trim($_GET['searchtags']))); 1899 $search_crits = explode(' ', escape(trim($_GET['searchtags'])));
1881 $search_type='tags'; 1900 $search_type = LinkFilter::$FILTER_TAG;
1882 } 1901 $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly);
1883 elseif (isset($_SERVER['QUERY_STRING']) && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/',$_SERVER['QUERY_STRING'])) // Detect smallHashes in URL 1902 }
1884 { 1903 // Detect smallHashes in URL.
1885 $linksToDisplay = $LINKSDB->filterSmallHash(substr(trim($_SERVER["QUERY_STRING"], '/'),0,6)); 1904 elseif (isset($_SERVER['QUERY_STRING'])
1886 if (count($linksToDisplay)==0) 1905 && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])) {
1887 { 1906 $search_type = LinkFilter::$FILTER_HASH;
1888 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 1907 $search_crits = substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6);
1889 echo '<h1>404 Not found.</h1>Oh crap. The link you are trying to reach does not exist or has been deleted.'; 1908 $linksToDisplay = $LINKSDB->filter($search_type, $search_crits);
1909
1910 if (count($linksToDisplay) == 0) {
1911 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
1912 echo '<h1>404 Not found.</h1>Oh crap.
1913 The link you are trying to reach does not exist or has been deleted.';
1890 echo '<br>Would you mind <a href="?">clicking here</a>?'; 1914 echo '<br>Would you mind <a href="?">clicking here</a>?';
1891 exit; 1915 exit;
1892 } 1916 }
1893 $search_type='permalink';
1894 } 1917 }
1895 else 1918 // Otherwise, display without filtering.
1896 $linksToDisplay = $LINKSDB; // Otherwise, display without filtering. 1919 else {
1897 1920 $linksToDisplay = $LINKSDB->filter('', '', false, $privateonly);
1898
1899 // Option: Show only private links
1900 if (!empty($_SESSION['privateonly']))
1901 {
1902 $tmp = array();
1903 foreach($linksToDisplay as $linkdate=>$link)
1904 {
1905 if ($link['private']!=0) $tmp[$linkdate]=$link;
1906 }
1907 $linksToDisplay=$tmp;
1908 } 1921 }
1909 1922
1910 // ---- Handle paging. 1923 // ---- Handle paging.
1911 /* Can someone explain to me why you get the following error when using array_keys() on an object which implements the interface ArrayAccess??? 1924 $keys = array();
1912 "Warning: array_keys() expects parameter 1 to be array, object given in ... " 1925 foreach ($linksToDisplay as $key => $value) {
1913 If my class implements ArrayAccess, why won't array_keys() accept it ? ( $keys=array_keys($linksToDisplay); ) 1926 $keys[] = $key;
1914 */ 1927 }
1915 $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // Stupid and ugly. Thanks PHP.
1916 1928
1917 // If there is only a single link, we change on-the-fly the title of the page. 1929 // If there is only a single link, we change on-the-fly the title of the page.
1918 if (count($linksToDisplay)==1) $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title']; 1930 if (count($linksToDisplay) == 1) {
1931 $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title'];
1932 }
1919 1933
1920 // Select articles according to paging. 1934 // Select articles according to paging.
1921 $pagecount = ceil(count($keys)/$_SESSION['LINKS_PER_PAGE']); 1935 $pagecount = ceil(count($keys) / $_SESSION['LINKS_PER_PAGE']);
1922 $pagecount = ($pagecount==0 ? 1 : $pagecount); 1936 $pagecount = $pagecount == 0 ? 1 : $pagecount;
1923 $page=( empty($_GET['page']) ? 1 : intval($_GET['page'])); 1937 $page= empty($_GET['page']) ? 1 : intval($_GET['page']);
1924 $page = ( $page<1 ? 1 : $page ); 1938 $page = $page < 1 ? 1 : $page;
1925 $page = ( $page>$pagecount ? $pagecount : $page ); 1939 $page = $page > $pagecount ? $pagecount : $page;
1926 $i = ($page-1)*$_SESSION['LINKS_PER_PAGE']; // Start index. 1940 // Start index.
1927 $end = $i+$_SESSION['LINKS_PER_PAGE']; 1941 $i = ($page-1) * $_SESSION['LINKS_PER_PAGE'];
1928 $linkDisp=array(); // Links to display 1942 $end = $i + $_SESSION['LINKS_PER_PAGE'];
1943 $linkDisp = array();
1929 while ($i<$end && $i<count($keys)) 1944 while ($i<$end && $i<count($keys))
1930 { 1945 {
1931 $link = $linksToDisplay[$keys[$i]]; 1946 $link = $linksToDisplay[$keys[$i]];
1932 $link['description'] = format_description($link['description'], $GLOBALS['redirector']); 1947 $link['description'] = format_description($link['description'], $GLOBALS['redirector']);
1933 $classLi = $i%2!=0 ? '' : 'publicLinkHightLight'; 1948 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
1934 $link['class'] = ($link['private']==0 ? $classLi : 'private'); 1949 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
1935 $link['timestamp']=linkdate2timestamp($link['linkdate']); 1950 $link['timestamp'] = linkdate2timestamp($link['linkdate']);
1936 $taglist = explode(' ',$link['tags']); 1951 $taglist = explode(' ', $link['tags']);
1937 uasort($taglist, 'strcasecmp'); 1952 uasort($taglist, 'strcasecmp');
1938 $link['taglist']=$taglist; 1953 $link['taglist'] = $taglist;
1939 $link['shorturl'] = smallHash($link['linkdate']); 1954 $link['shorturl'] = smallHash($link['linkdate']);
1940 if ($link["url"][0] === '?' && // Check for both signs of a note: starting with ? and 7 chars long. I doubt that you'll post any links that look like this. 1955 // Check for both signs of a note: starting with ? and 7 chars long.
1941 strlen($link["url"]) === 7) { 1956 if ($link['url'][0] === '?' &&
1942 $link["url"] = index_url($_SERVER) . $link["url"]; 1957 strlen($link['url']) === 7) {
1958 $link['url'] = index_url($_SERVER) . $link['url'];
1943 } 1959 }
1944 1960
1945 $linkDisp[$keys[$i]] = $link; 1961 $linkDisp[$keys[$i]] = $link;
@@ -1947,13 +1963,21 @@ function buildLinkList($PAGE,$LINKSDB)
1947 } 1963 }
1948 1964
1949 // Compute paging navigation 1965 // Compute paging navigation
1950 $searchterm= ( empty($_GET['searchterm']) ? '' : '&searchterm='.$_GET['searchterm'] ); 1966 $searchterm = empty($_GET['searchterm']) ? '' : '&searchterm=' . $_GET['searchterm'];
1951 $searchtags= ( empty($_GET['searchtags']) ? '' : '&searchtags='.$_GET['searchtags'] ); 1967 $searchtags = empty($_GET['searchtags']) ? '' : '&searchtags=' . $_GET['searchtags'];
1952 $paging=''; 1968 $previous_page_url = '';
1953 $previous_page_url=''; if ($i!=count($keys)) $previous_page_url='?page='.($page+1).$searchterm.$searchtags; 1969 if ($i != count($keys)) {
1954 $next_page_url='';if ($page>1) $next_page_url='?page='.($page-1).$searchterm.$searchtags; 1970 $previous_page_url = '?page=' . ($page+1) . $searchterm . $searchtags;
1971 }
1972 $next_page_url='';
1973 if ($page>1) {
1974 $next_page_url = '?page=' . ($page-1) . $searchterm . $searchtags;
1975 }
1955 1976
1956 $token = ''; if (isLoggedIn()) $token=getToken(); 1977 $token = '';
1978 if (isLoggedIn()) {
1979 $token = getToken();
1980 }
1957 1981
1958 // Fill all template fields. 1982 // Fill all template fields.
1959 $data = array( 1983 $data = array(