/** * Count existing links - public links hidden */ public function testCountHiddenPublic() { $linkDB = new LinkDB(self::$testDatastore, false, true); $this->assertEquals(0, $linkDB->count()); $this->assertEquals(0, $linkDB->count()); }
function importFile() { if (!isLoggedIn()) { die('Not allowed.'); } $LINKSDB = new LinkDB($GLOBALS['config']['DATASTORE'], isLoggedIn(), $GLOBALS['config']['HIDE_PUBLIC_LINKS'], $GLOBALS['redirector']); $filename = $_FILES['filetoupload']['name']; $filesize = $_FILES['filetoupload']['size']; $data = file_get_contents($_FILES['filetoupload']['tmp_name']); $private = empty($_POST['private']) ? 0 : 1; // Should the links be imported as private? $overwrite = !empty($_POST['overwrite']); // Should the imported links overwrite existing ones? $import_count = 0; // Sniff file type: $type = 'unknown'; if (startsWith($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>')) { $type = 'netscape'; } // Netscape bookmark file (aka Firefox). // Then import the bookmarks. if ($type == 'netscape') { // This is a standard Netscape-style bookmark file. // This format is supported by all browsers (except IE, of course), also Delicious, Diigo and others. foreach (explode('<DT>', $data) as $html) { $link = array('linkdate' => '', 'title' => '', 'url' => '', 'description' => '', 'tags' => '', 'private' => 0); $d = explode('<DD>', $html); if (startswith($d[0], '<A ')) { $link['description'] = isset($d[1]) ? html_entity_decode(trim($d[1]), ENT_QUOTES, 'UTF-8') : ''; // Get description (optional) preg_match('!<A .*?>(.*?)</A>!i', $d[0], $matches); $link['title'] = isset($matches[1]) ? trim($matches[1]) : ''; // Get title $link['title'] = html_entity_decode($link['title'], ENT_QUOTES, 'UTF-8'); preg_match_all('! ([A-Z_]+)=\\"(.*?)"!i', $html, $matches, PREG_SET_ORDER); // Get all other attributes $raw_add_date = 0; foreach ($matches as $m) { $attr = $m[1]; $value = $m[2]; if ($attr == 'HREF') { $link['url'] = html_entity_decode($value, ENT_QUOTES, 'UTF-8'); } elseif ($attr == 'ADD_DATE') { $raw_add_date = intval($value); if ($raw_add_date > 30000000000) { $raw_add_date /= 1000; } //If larger than year 2920, then was likely stored in milliseconds instead of seconds } elseif ($attr == 'PRIVATE') { $link['private'] = $value == '0' ? 0 : 1; } elseif ($attr == 'TAGS') { $link['tags'] = html_entity_decode(str_replace(',', ' ', $value), ENT_QUOTES, 'UTF-8'); } } if ($link['url'] != '') { if ($private == 1) { $link['private'] = 1; } $dblink = $LINKSDB->getLinkFromUrl($link['url']); // See if the link is already in database. if ($dblink == false) { // Link not in database, let's import it... if (empty($raw_add_date)) { $raw_add_date = time(); } // In case of shitty bookmark file with no ADD_DATE // Make sure date/time is not already used by another link. // (Some bookmark files have several different links with the same ADD_DATE) // We increment date by 1 second until we find a date which is not used in DB. // (so that links that have the same date/time are more or less kept grouped by date, but do not conflict.) while (!empty($LINKSDB[date('Ymd_His', $raw_add_date)])) { $raw_add_date++; } // Yes, I know it's ugly. $link['linkdate'] = date('Ymd_His', $raw_add_date); $LINKSDB[$link['linkdate']] = $link; $import_count++; } else { if ($overwrite) { // If overwrite is required, we import link data, except date/time. $link['linkdate'] = $dblink['linkdate']; $LINKSDB[$link['linkdate']] = $link; $import_count++; } } } } } $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); echo '<script>alert("File ' . json_encode($filename) . ' (' . $filesize . ' bytes) was successfully processed: ' . $import_count . ' links imported.");document.location=\'?\';</script>'; } else { echo '<script>alert("File ' . json_encode($filename) . ' (' . $filesize . ' bytes) has an unknown file format. Nothing was imported.");document.location=\'?\';</script>'; } }
/** * Template for the list of links (<div id="linklist">) * This function fills all the necessary fields in the $PAGE for the template 'linklist.html' * * @param pageBuilder $PAGE pageBuilder instance. * @param LinkDB $LINKSDB LinkDB instance. */ function buildLinkList($PAGE, $LINKSDB) { // Used in templates $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; // Smallhash filter if (!empty($_SERVER['QUERY_STRING']) && preg_match('/^[a-zA-Z0-9-_@]{6}($|&|#)/', $_SERVER['QUERY_STRING'])) { try { $linksToDisplay = $LINKSDB->filterHash($_SERVER['QUERY_STRING']); } catch (LinkNotFoundException $e) { $PAGE->render404($e->getMessage()); exit; } } else { // Filter links according search parameters. $privateonly = !empty($_SESSION['privateonly']); $linksToDisplay = $LINKSDB->filterSearch($_GET, false, $privateonly); } // ---- Handle paging. $keys = array(); foreach ($linksToDisplay as $key => $value) { $keys[] = $key; } // 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; // Start index. $i = ($page - 1) * $_SESSION['LINKS_PER_PAGE']; $end = $i + $_SESSION['LINKS_PER_PAGE']; $linkDisp = array(); while ($i < $end && $i < count($keys)) { $link = $linksToDisplay[$keys[$i]]; $link['description'] = format_description($link['description'], $GLOBALS['redirector']); $classLi = $i % 2 != 0 ? '' : 'publicLinkHightLight'; $link['class'] = $link['private'] == 0 ? $classLi : 'private'; $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $link['timestamp'] = $date->getTimestamp(); $taglist = explode(' ', $link['tags']); uasort($taglist, 'strcasecmp'); $link['taglist'] = $taglist; $link['shorturl'] = smallHash($link['linkdate']); // Check for both signs of a note: starting with ? and 7 chars long. if ($link['url'][0] === '?' && strlen($link['url']) === 7) { $link['url'] = index_url($_SERVER) . $link['url']; } $linkDisp[$keys[$i]] = $link; $i++; } // Compute paging navigation $searchtagsUrl = empty($searchtags) ? '' : '&searchtags=' . urlencode($searchtags); $searchtermUrl = empty($searchterm) ? '' : '&searchterm=' . urlencode($searchterm); $previous_page_url = ''; if ($i != count($keys)) { $previous_page_url = '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl; } $next_page_url = ''; if ($page > 1) { $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl; } $token = isLoggedIn() ? getToken() : ''; // Fill all template fields. $data = array('previous_page_url' => $previous_page_url, 'next_page_url' => $next_page_url, 'page_current' => $page, 'page_max' => $pagecount, 'result_count' => count($linksToDisplay), 'search_term' => $searchterm, 'search_tags' => $searchtags, 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], 'token' => $token, 'links' => $linkDisp, 'tags' => $LINKSDB->allTags()); // FIXME! temporary fix - see #399. if (!empty($GLOBALS['pagetitle']) && count($linkDisp) == 1) { $data['pagetitle'] = $GLOBALS['pagetitle']; } $pluginManager = PluginManager::getInstance(); $pluginManager->executeHooks('render_linklist', $data, array('loggedin' => isLoggedIn())); foreach ($data as $key => $value) { $PAGE->assign($key, $value); } return; }