function buildLinkList($PAGE, $LINKSDB) { // ---- Filter link database according to parameters $linksToDisplay = array(); $search_type = ''; $search_crits = ''; if (isset($_GET['searchterm'])) { $linksToDisplay = $LINKSDB->filterFulltext(trim($_GET['searchterm'])); $search_crits = escape(trim($_GET['searchterm'])); $search_type = 'fulltext'; } elseif (isset($_GET['searchtags'])) { $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); $search_crits = explode(' ', escape(trim($_GET['searchtags']))); $search_type = 'tags'; } elseif (isset($_SERVER['QUERY_STRING']) && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])) { $linksToDisplay = $LINKSDB->filterSmallHash(substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6)); if (count($linksToDisplay) == 0) { header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); echo '<h1>404 Not found.</h1>Oh crap. The link you are trying to reach does not exist or has been deleted.'; echo '<br>Would you mind <a href="?">clicking here</a>?'; exit; } $search_type = 'permalink'; } else { $linksToDisplay = $LINKSDB; } // Otherwise, display without filtering. // Option: Show only private links if (!empty($_SESSION['privateonly'])) { $tmp = array(); foreach ($linksToDisplay as $linkdate => $link) { if ($link['private'] != 0) { $tmp[$linkdate] = $link; } } $linksToDisplay = $tmp; } // ---- Handle paging. /* Can someone explain to me why you get the following error when using array_keys() on an object which implements the interface ArrayAccess??? "Warning: array_keys() expects parameter 1 to be array, object given in ... " If my class implements ArrayAccess, why won't array_keys() accept it ? ( $keys=array_keys($linksToDisplay); ) */ $keys = array(); foreach ($linksToDisplay as $key => $value) { $keys[] = $key; } // Stupid and ugly. Thanks PHP. // If there is only a single link, we change on-the-fly the title of the page. if (count($linksToDisplay) == 1) { $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'] . ' - ' . $GLOBALS['title']; } // Select articles according to paging. $pagecount = ceil(count($keys) / $_SESSION['LINKS_PER_PAGE']); $pagecount = $pagecount == 0 ? 1 : $pagecount; $page = empty($_GET['page']) ? 1 : intval($_GET['page']); $page = $page < 1 ? 1 : $page; $page = $page > $pagecount ? $pagecount : $page; $i = ($page - 1) * $_SESSION['LINKS_PER_PAGE']; // Start index. $end = $i + $_SESSION['LINKS_PER_PAGE']; $linkDisp = array(); // Links to display while ($i < $end && $i < count($keys)) { $link = $linksToDisplay[$keys[$i]]; $link['description'] = nl2br(keepMultipleSpaces(text2clickable($link['description']))); $title = $link['title']; $classLi = $i % 2 != 0 ? '' : 'publicLinkHightLight'; $link['class'] = $link['private'] == 0 ? $classLi : 'private'; $link['timestamp'] = linkdate2timestamp($link['linkdate']); $taglist = explode(' ', $link['tags']); uasort($taglist, 'strcasecmp'); $link['taglist'] = $taglist; if ($link["url"][0] === '?' && strlen($link["url"]) === 7) { $link["url"] = indexUrl() . $link["url"]; } $linkDisp[$keys[$i]] = $link; $i++; } // Compute paging navigation $searchterm = empty($_GET['searchterm']) ? '' : '&searchterm=' . $_GET['searchterm']; $searchtags = empty($_GET['searchtags']) ? '' : '&searchtags=' . $_GET['searchtags']; $paging = ''; $previous_page_url = ''; if ($i != count($keys)) { $previous_page_url = '?page=' . ($page + 1) . $searchterm . $searchtags; } $next_page_url = ''; if ($page > 1) { $next_page_url = '?page=' . ($page - 1) . $searchterm . $searchtags; } $token = ''; if (isLoggedIn()) { $token = getToken(); } // Fill all template fields. $PAGE->assign('linkcount', count($LINKSDB)); $PAGE->assign('previous_page_url', $previous_page_url); $PAGE->assign('next_page_url', $next_page_url); $PAGE->assign('page_current', $page); $PAGE->assign('page_max', $pagecount); $PAGE->assign('result_count', count($linksToDisplay)); $PAGE->assign('search_type', $search_type); $PAGE->assign('search_crits', $search_crits); $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector']); // Optional redirector URL. $PAGE->assign('token', $token); $PAGE->assign('links', $linkDisp); $PAGE->assign('tags', $LINKSDB->allTags()); return; }
/** * Format Shaarli's description * TODO: Move me to ApplicationUtils when it's ready. * * @param string $description shaare's description. * @param string $redirector if a redirector is set, use it to gerenate links. * * @return string formatted description. */ function format_description($description, $redirector) { return nl2br(space2nbsp(text2clickable($description, $redirector))); }
/** * Test text2clickable a redirector set. */ public function testText2clickableWithRedirector() { $text = 'stuff http://hello.there/is=someone#here otherstuff'; $redirector = 'http://redirector.to'; $expectedText = 'stuff <a href="' . $redirector . urlencode('http://hello.there/is=someone#here') . '">http://hello.there/is=someone#here</a> otherstuff'; $processedText = text2clickable($text, $redirector); $this->assertEquals($expectedText, $processedText); }