aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'index.php')
-rw-r--r--index.php151
1 files changed, 34 insertions, 117 deletions
diff --git a/index.php b/index.php
index 55b12adc..1f148d78 100644
--- a/index.php
+++ b/index.php
@@ -44,6 +44,10 @@ error_reporting(E_ALL^E_WARNING);
44//error_reporting(-1); 44//error_reporting(-1);
45 45
46 46
47// 3rd-party libraries
48require_once 'inc/rain.tpl.class.php';
49require_once __DIR__ . '/vendor/autoload.php';
50
47// Shaarli library 51// Shaarli library
48require_once 'application/ApplicationUtils.php'; 52require_once 'application/ApplicationUtils.php';
49require_once 'application/Cache.php'; 53require_once 'application/Cache.php';
@@ -53,6 +57,7 @@ require_once 'application/config/ConfigPlugin.php';
53require_once 'application/FeedBuilder.php'; 57require_once 'application/FeedBuilder.php';
54require_once 'application/FileUtils.php'; 58require_once 'application/FileUtils.php';
55require_once 'application/HttpUtils.php'; 59require_once 'application/HttpUtils.php';
60require_once 'application/Languages.php';
56require_once 'application/LinkDB.php'; 61require_once 'application/LinkDB.php';
57require_once 'application/LinkFilter.php'; 62require_once 'application/LinkFilter.php';
58require_once 'application/LinkUtils.php'; 63require_once 'application/LinkUtils.php';
@@ -64,7 +69,6 @@ require_once 'application/Utils.php';
64require_once 'application/PluginManager.php'; 69require_once 'application/PluginManager.php';
65require_once 'application/Router.php'; 70require_once 'application/Router.php';
66require_once 'application/Updater.php'; 71require_once 'application/Updater.php';
67require_once 'inc/rain.tpl.class.php';
68 72
69// Ensure the PHP version is supported 73// Ensure the PHP version is supported
70try { 74try {
@@ -783,8 +787,6 @@ function renderPage($conf, $pluginManager)
783 if ($targetPage == Router::$PAGE_LOGIN) 787 if ($targetPage == Router::$PAGE_LOGIN)
784 { 788 {
785 if ($conf->get('security.open_shaarli')) { header('Location: ?'); exit; } // No need to login for open Shaarli 789 if ($conf->get('security.open_shaarli')) { header('Location: ?'); exit; } // No need to login for open Shaarli
786 $token=''; if (ban_canLogin($conf)) $token=getToken($conf); // Do not waste token generation if not useful.
787 $PAGE->assign('token',$token);
788 if (isset($_GET['username'])) { 790 if (isset($_GET['username'])) {
789 $PAGE->assign('username', escape($_GET['username'])); 791 $PAGE->assign('username', escape($_GET['username']));
790 } 792 }
@@ -1105,7 +1107,6 @@ function renderPage($conf, $pluginManager)
1105 } 1107 }
1106 else // show the change password form. 1108 else // show the change password form.
1107 { 1109 {
1108 $PAGE->assign('token',getToken($conf));
1109 $PAGE->renderPage('changepassword'); 1110 $PAGE->renderPage('changepassword');
1110 exit; 1111 exit;
1111 } 1112 }
@@ -1152,7 +1153,6 @@ function renderPage($conf, $pluginManager)
1152 } 1153 }
1153 else // Show the configuration form. 1154 else // Show the configuration form.
1154 { 1155 {
1155 $PAGE->assign('token',getToken($conf));
1156 $PAGE->assign('title', $conf->get('general.title')); 1156 $PAGE->assign('title', $conf->get('general.title'));
1157 $PAGE->assign('redirector', $conf->get('redirector.url')); 1157 $PAGE->assign('redirector', $conf->get('redirector.url'));
1158 list($timezone_form, $timezone_js) = generateTimeZoneForm($conf->get('general.timezone')); 1158 list($timezone_form, $timezone_js) = generateTimeZoneForm($conf->get('general.timezone'));
@@ -1172,7 +1172,6 @@ function renderPage($conf, $pluginManager)
1172 if ($targetPage == Router::$PAGE_CHANGETAG) 1172 if ($targetPage == Router::$PAGE_CHANGETAG)
1173 { 1173 {
1174 if (empty($_POST['fromtag']) || (empty($_POST['totag']) && isset($_POST['renametag']))) { 1174 if (empty($_POST['fromtag']) || (empty($_POST['totag']) && isset($_POST['renametag']))) {
1175 $PAGE->assign('token', getToken($conf));
1176 $PAGE->assign('tags', $LINKSDB->allTags()); 1175 $PAGE->assign('tags', $LINKSDB->allTags());
1177 $PAGE->renderPage('changetag'); 1176 $PAGE->renderPage('changetag');
1178 exit; 1177 exit;
@@ -1347,7 +1346,6 @@ function renderPage($conf, $pluginManager)
1347 $data = array( 1346 $data = array(
1348 'link' => $link, 1347 'link' => $link,
1349 'link_is_new' => false, 1348 'link_is_new' => false,
1350 'token' => getToken($conf),
1351 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), 1349 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''),
1352 'tags' => $LINKSDB->allTags(), 1350 'tags' => $LINKSDB->allTags(),
1353 ); 1351 );
@@ -1414,11 +1412,10 @@ function renderPage($conf, $pluginManager)
1414 $data = array( 1412 $data = array(
1415 'link' => $link, 1413 'link' => $link,
1416 'link_is_new' => $link_is_new, 1414 'link_is_new' => $link_is_new,
1417 'token' => getToken($conf), // XSRF protection.
1418 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), 1415 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''),
1419 'source' => (isset($_GET['source']) ? $_GET['source'] : ''), 1416 'source' => (isset($_GET['source']) ? $_GET['source'] : ''),
1420 'tags' => $LINKSDB->allTags(), 1417 'tags' => $LINKSDB->allTags(),
1421 'default_private_links' => $conf->get('default_private_links', false), 1418 'default_private_links' => $conf->get('privacy.default_private_links', false),
1422 ); 1419 );
1423 $pluginManager->executeHooks('render_editlink', $data); 1420 $pluginManager->executeHooks('render_editlink', $data);
1424 1421
@@ -1474,27 +1471,37 @@ function renderPage($conf, $pluginManager)
1474 exit; 1471 exit;
1475 } 1472 }
1476 1473
1477 // -------- User is uploading a file for import 1474 if ($targetPage == Router::$PAGE_IMPORT) {
1478 if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=upload')) 1475 // Upload a Netscape bookmark dump to import its contents
1479 { 1476
1480 // If file is too big, some form field may be missing. 1477 if (! isset($_POST['token']) || ! isset($_FILES['filetoupload'])) {
1481 if (!isset($_POST['token']) || (!isset($_FILES)) || (isset($_FILES['filetoupload']['size']) && $_FILES['filetoupload']['size']==0)) 1478 // Show import dialog
1482 { 1479 $PAGE->assign('maxfilesize', getMaxFileSize());
1483 $returnurl = ( empty($_SERVER['HTTP_REFERER']) ? '?' : $_SERVER['HTTP_REFERER'] ); 1480 $PAGE->renderPage('import');
1484 echo '<script>alert("The file you are trying to upload is probably bigger than what this webserver can accept ('.getMaxFileSize().' bytes). Please upload in smaller chunks.");document.location=\''.escape($returnurl).'\';</script>';
1485 exit; 1481 exit;
1486 } 1482 }
1487 if (!tokenOk($_POST['token'])) die('Wrong token.');
1488 importFile($LINKSDB);
1489 exit;
1490 }
1491 1483
1492 // -------- Show upload/import dialog: 1484 // Import bookmarks from an uploaded file
1493 if ($targetPage == Router::$PAGE_IMPORT) 1485 if (isset($_FILES['filetoupload']['size']) && $_FILES['filetoupload']['size'] == 0) {
1494 { 1486 // The file is too big or some form field may be missing.
1495 $PAGE->assign('token',getToken($conf)); 1487 echo '<script>alert("The file you are trying to upload is probably'
1496 $PAGE->assign('maxfilesize',getMaxFileSize()); 1488 .' bigger than what this webserver can accept ('
1497 $PAGE->renderPage('import'); 1489 .getMaxFileSize().' bytes).'
1490 .' Please upload in smaller chunks.");document.location=\'?do='
1491 .Router::$PAGE_IMPORT .'\';</script>';
1492 exit;
1493 }
1494 if (! tokenOk($_POST['token'])) {
1495 die('Wrong token.');
1496 }
1497 $status = NetscapeBookmarkUtils::import(
1498 $_POST,
1499 $_FILES,
1500 $LINKSDB,
1501 $conf->get('resource.page_cache')
1502 );
1503 echo '<script>alert("'.$status.'");document.location=\'?do='
1504 .Router::$PAGE_IMPORT .'\';</script>';
1498 exit; 1505 exit;
1499 } 1506 }
1500 1507
@@ -1552,95 +1559,6 @@ function renderPage($conf, $pluginManager)
1552} 1559}
1553 1560
1554/** 1561/**
1555 * Process the import file form.
1556 *
1557 * @param LinkDB $LINKSDB Loaded LinkDB instance.
1558 * @param ConfigManager $conf Configuration Manager instance.
1559 */
1560function importFile($LINKSDB, $conf)
1561{
1562 if (!isLoggedIn()) { die('Not allowed.'); }
1563
1564 $filename=$_FILES['filetoupload']['name'];
1565 $filesize=$_FILES['filetoupload']['size'];
1566 $data=file_get_contents($_FILES['filetoupload']['tmp_name']);
1567 $private = (empty($_POST['private']) ? 0 : 1); // Should the links be imported as private?
1568 $overwrite = !empty($_POST['overwrite']) ; // Should the imported links overwrite existing ones?
1569 $import_count=0;
1570
1571 // Sniff file type:
1572 $type='unknown';
1573 if (startsWith($data,'<!DOCTYPE NETSCAPE-Bookmark-file-1>')) $type='netscape'; // Netscape bookmark file (aka Firefox).
1574
1575 // Then import the bookmarks.
1576 if ($type=='netscape')
1577 {
1578 // This is a standard Netscape-style bookmark file.
1579 // This format is supported by all browsers (except IE, of course), also Delicious, Diigo and others.
1580 foreach(explode('<DT>',$data) as $html) // explode is very fast
1581 {
1582 $link = array('linkdate'=>'','title'=>'','url'=>'','description'=>'','tags'=>'','private'=>0);
1583 $d = explode('<DD>',$html);
1584 if (startsWith($d[0], '<A '))
1585 {
1586 $link['description'] = (isset($d[1]) ? html_entity_decode(trim($d[1]),ENT_QUOTES,'UTF-8') : ''); // Get description (optional)
1587 preg_match('!<A .*?>(.*?)</A>!i',$d[0],$matches); $link['title'] = (isset($matches[1]) ? trim($matches[1]) : ''); // Get title
1588 $link['title'] = html_entity_decode($link['title'],ENT_QUOTES,'UTF-8');
1589 preg_match_all('! ([A-Z_]+)=\"(.*?)"!i',$html,$matches,PREG_SET_ORDER); // Get all other attributes
1590 $raw_add_date=0;
1591 foreach($matches as $m)
1592 {
1593 $attr=$m[1]; $value=$m[2];
1594 if ($attr=='HREF') $link['url']=html_entity_decode($value,ENT_QUOTES,'UTF-8');
1595 elseif ($attr=='ADD_DATE')
1596 {
1597 $raw_add_date=intval($value);
1598 if ($raw_add_date>30000000000) $raw_add_date/=1000; //If larger than year 2920, then was likely stored in milliseconds instead of seconds
1599 }
1600 elseif ($attr=='PRIVATE') $link['private']=($value=='0'?0:1);
1601 elseif ($attr=='TAGS') $link['tags']=html_entity_decode(str_replace(',',' ',$value),ENT_QUOTES,'UTF-8');
1602 }
1603 if ($link['url']!='')
1604 {
1605 if ($private==1) $link['private']=1;
1606 $dblink = $LINKSDB->getLinkFromUrl($link['url']); // See if the link is already in database.
1607 if ($dblink==false)
1608 { // Link not in database, let's import it...
1609 if (empty($raw_add_date)) $raw_add_date=time(); // In case of shitty bookmark file with no ADD_DATE
1610
1611 // Make sure date/time is not already used by another link.
1612 // (Some bookmark files have several different links with the same ADD_DATE)
1613 // We increment date by 1 second until we find a date which is not used in DB.
1614 // (so that links that have the same date/time are more or less kept grouped by date, but do not conflict.)
1615 while (!empty($LINKSDB[date('Ymd_His',$raw_add_date)])) { $raw_add_date++; }// Yes, I know it's ugly.
1616 $link['linkdate']=date('Ymd_His',$raw_add_date);
1617 $LINKSDB[$link['linkdate']] = $link;
1618 $import_count++;
1619 }
1620 else // Link already present in database.
1621 {
1622 if ($overwrite)
1623 { // If overwrite is required, we import link data, except date/time.
1624 $link['linkdate']=$dblink['linkdate'];
1625 $LINKSDB[$link['linkdate']] = $link;
1626 $import_count++;
1627 }
1628 }
1629
1630 }
1631 }
1632 }
1633 $LINKSDB->savedb($conf->get('resource.page_cache'));
1634
1635 echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) was successfully processed: '.$import_count.' links imported.");document.location=\'?\';</script>';
1636 }
1637 else
1638 {
1639 echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) has an unknown file format. Nothing was imported.");document.location=\'?\';</script>';
1640 }
1641}
1642
1643/**
1644 * Template for the list of links (<div id="linklist">) 1562 * Template for the list of links (<div id="linklist">)
1645 * This function fills all the necessary fields in the $PAGE for the template 'linklist.html' 1563 * This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
1646 * 1564 *
@@ -1734,7 +1652,6 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
1734 'search_term' => $searchterm, 1652 'search_term' => $searchterm,
1735 'search_tags' => $searchtags, 1653 'search_tags' => $searchtags,
1736 'redirector' => $conf->get('redirector.url'), // Optional redirector URL. 1654 'redirector' => $conf->get('redirector.url'), // Optional redirector URL.
1737 'token' => $token,
1738 'links' => $linkDisp, 1655 'links' => $linkDisp,
1739 'tags' => $LINKSDB->allTags(), 1656 'tags' => $LINKSDB->allTags(),
1740 ); 1657 );