aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'index.php')
-rw-r--r--index.php104
1 files changed, 58 insertions, 46 deletions
diff --git a/index.php b/index.php
index 5366cb0e..fdbdfaa2 100644
--- a/index.php
+++ b/index.php
@@ -564,24 +564,19 @@ function showDailyRSS($conf) {
564 ); 564 );
565 565
566 /* Some Shaarlies may have very few links, so we need to look 566 /* Some Shaarlies may have very few links, so we need to look
567 back in time (rsort()) until we have enough days ($nb_of_days). 567 back in time until we have enough days ($nb_of_days).
568 */ 568 */
569 $linkdates = array();
570 foreach ($LINKSDB as $linkdate => $value) {
571 $linkdates[] = $linkdate;
572 }
573 rsort($linkdates);
574 $nb_of_days = 7; // We take 7 days. 569 $nb_of_days = 7; // We take 7 days.
575 $today = date('Ymd'); 570 $today = date('Ymd');
576 $days = array(); 571 $days = array();
577 572
578 foreach ($linkdates as $linkdate) { 573 foreach ($LINKSDB as $link) {
579 $day = substr($linkdate, 0, 8); // Extract day (without time) 574 $day = $link['created']->format('Ymd'); // Extract day (without time)
580 if (strcmp($day,$today) < 0) { 575 if (strcmp($day, $today) < 0) {
581 if (empty($days[$day])) { 576 if (empty($days[$day])) {
582 $days[$day] = array(); 577 $days[$day] = array();
583 } 578 }
584 $days[$day][] = $linkdate; 579 $days[$day][] = $link;
585 } 580 }
586 581
587 if (count($days) > $nb_of_days) { 582 if (count($days) > $nb_of_days) {
@@ -601,24 +596,18 @@ function showDailyRSS($conf) {
601 echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL; 596 echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
602 597
603 // For each day. 598 // For each day.
604 foreach ($days as $day => $linkdates) { 599 foreach ($days as $day => $links) {
605 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000'); 600 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
606 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. 601 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
607 602
608 // Build the HTML body of this RSS entry.
609 $links = array();
610
611 // We pre-format some fields for proper output. 603 // We pre-format some fields for proper output.
612 foreach ($linkdates as $linkdate) { 604 foreach ($links as &$link) {
613 $l = $LINKSDB[$linkdate]; 605 $link['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
614 $l['formatedDescription'] = format_description($l['description'], $conf->get('redirector.url')); 606 $link['thumbnail'] = thumbnail($conf, $link['url']);
615 $l['thumbnail'] = thumbnail($conf, $l['url']); 607 $link['timestamp'] = $link['created']->getTimestamp();
616 $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']); 608 if (startsWith($link['url'], '?')) {
617 $l['timestamp'] = $l_date->getTimestamp(); 609 $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute
618 if (startsWith($l['url'], '?')) {
619 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute
620 } 610 }
621 $links[$linkdate] = $l;
622 } 611 }
623 612
624 // Then build the HTML for this day: 613 // Then build the HTML for this day:
@@ -680,8 +669,7 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager)
680 $linksToDisplay[$key]['taglist']=$taglist; 669 $linksToDisplay[$key]['taglist']=$taglist;
681 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); 670 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
682 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); 671 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']);
683 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); 672 $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
684 $linksToDisplay[$key]['timestamp'] = $date->getTimestamp();
685 } 673 }
686 674
687 /* We need to spread the articles on 3 columns. 675 /* We need to spread the articles on 3 columns.
@@ -831,7 +819,7 @@ function renderPage($conf, $pluginManager)
831 // Get only links which have a thumbnail. 819 // Get only links which have a thumbnail.
832 foreach($links as $link) 820 foreach($links as $link)
833 { 821 {
834 $permalink='?'.escape(smallHash($link['linkdate'])); 822 $permalink='?'.$link['shorturl'];
835 $thumb=lazyThumbnail($conf, $link['url'],$permalink); 823 $thumb=lazyThumbnail($conf, $link['url'],$permalink);
836 if ($thumb!='') // Only output links which have a thumbnail. 824 if ($thumb!='') // Only output links which have a thumbnail.
837 { 825 {
@@ -1245,13 +1233,28 @@ function renderPage($conf, $pluginManager)
1245 // -------- User clicked the "Save" button when editing a link: Save link to database. 1233 // -------- User clicked the "Save" button when editing a link: Save link to database.
1246 if (isset($_POST['save_edit'])) 1234 if (isset($_POST['save_edit']))
1247 { 1235 {
1248 $linkdate = $_POST['lf_linkdate'];
1249 $updated = isset($LINKSDB[$linkdate]) ? strval(date('Ymd_His')) : false;
1250
1251 // Go away! 1236 // Go away!
1252 if (! tokenOk($_POST['token'])) { 1237 if (! tokenOk($_POST['token'])) {
1253 die('Wrong token.'); 1238 die('Wrong token.');
1254 } 1239 }
1240
1241 // lf_id should only be present if the link exists.
1242 $id = !empty($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : $LINKSDB->getNextId();
1243 // Linkdate is kept here to:
1244 // - use the same permalink for notes as they're displayed when creating them
1245 // - let users hack creation date of their posts
1246 // See: https://github.com/shaarli/Shaarli/wiki/Datastore-hacks#changing-the-timestamp-for-a-link
1247 $linkdate = escape($_POST['lf_linkdate']);
1248 if (isset($LINKSDB[$id])) {
1249 // Edit
1250 $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
1251 $updated = new DateTime();
1252 } else {
1253 // New link
1254 $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
1255 $updated = null;
1256 }
1257
1255 // Remove multiple spaces. 1258 // Remove multiple spaces.
1256 $tags = trim(preg_replace('/\s\s+/', ' ', $_POST['lf_tags'])); 1259 $tags = trim(preg_replace('/\s\s+/', ' ', $_POST['lf_tags']));
1257 // Remove first '-' char in tags. 1260 // Remove first '-' char in tags.
@@ -1268,14 +1271,17 @@ function renderPage($conf, $pluginManager)
1268 } 1271 }
1269 1272
1270 $link = array( 1273 $link = array(
1274 'id' => $id,
1271 'title' => trim($_POST['lf_title']), 1275 'title' => trim($_POST['lf_title']),
1272 'url' => $url, 1276 'url' => $url,
1273 'description' => $_POST['lf_description'], 1277 'description' => $_POST['lf_description'],
1274 'private' => (isset($_POST['lf_private']) ? 1 : 0), 1278 'private' => (isset($_POST['lf_private']) ? 1 : 0),
1275 'linkdate' => $linkdate, 1279 'created' => $created,
1276 'updated' => $updated, 1280 'updated' => $updated,
1277 'tags' => str_replace(',', ' ', $tags) 1281 'tags' => str_replace(',', ' ', $tags),
1282 'shorturl' => link_small_hash($created, $id),
1278 ); 1283 );
1284
1279 // If title is empty, use the URL as title. 1285 // If title is empty, use the URL as title.
1280 if ($link['title'] == '') { 1286 if ($link['title'] == '') {
1281 $link['title'] = $link['url']; 1287 $link['title'] = $link['url'];
@@ -1283,7 +1289,7 @@ function renderPage($conf, $pluginManager)
1283 1289
1284 $pluginManager->executeHooks('save_link', $link); 1290 $pluginManager->executeHooks('save_link', $link);
1285 1291
1286 $LINKSDB[$linkdate] = $link; 1292 $LINKSDB[$id] = $link;
1287 $LINKSDB->save($conf->get('resource.page_cache')); 1293 $LINKSDB->save($conf->get('resource.page_cache'));
1288 pubsubhub($conf); 1294 pubsubhub($conf);
1289 1295
@@ -1296,7 +1302,7 @@ function renderPage($conf, $pluginManager)
1296 $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?'; 1302 $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?';
1297 $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); 1303 $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
1298 // Scroll to the link which has been edited. 1304 // Scroll to the link which has been edited.
1299 $location .= '#' . smallHash($_POST['lf_linkdate']); 1305 $location .= '#' . $link['shorturl'];
1300 // After saving the link, redirect to the page the user was on. 1306 // After saving the link, redirect to the page the user was on.
1301 header('Location: '. $location); 1307 header('Location: '. $location);
1302 exit; 1308 exit;
@@ -1307,8 +1313,10 @@ function renderPage($conf, $pluginManager)
1307 { 1313 {
1308 // If we are called from the bookmarklet, we must close the popup: 1314 // If we are called from the bookmarklet, we must close the popup:
1309 if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; } 1315 if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; }
1316 $link = $LINKSDB[(int) escape($_POST['lf_id'])];
1310 $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' ); 1317 $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' );
1311 $returnurl .= '#'.smallHash($_POST['lf_linkdate']); // Scroll to the link which has been edited. 1318 // Scroll to the link which has been edited.
1319 $returnurl .= '#'. $link['shorturl'];
1312 $returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); 1320 $returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
1313 header('Location: '.$returnurl); // After canceling, redirect to the page the user was on. 1321 header('Location: '.$returnurl); // After canceling, redirect to the page the user was on.
1314 exit; 1322 exit;
@@ -1318,14 +1326,17 @@ function renderPage($conf, $pluginManager)
1318 if (isset($_POST['delete_link'])) 1326 if (isset($_POST['delete_link']))
1319 { 1327 {
1320 if (!tokenOk($_POST['token'])) die('Wrong token.'); 1328 if (!tokenOk($_POST['token'])) die('Wrong token.');
1329
1321 // We do not need to ask for confirmation: 1330 // We do not need to ask for confirmation:
1322 // - confirmation is handled by JavaScript 1331 // - confirmation is handled by JavaScript
1323 // - we are protected from XSRF by the token. 1332 // - we are protected from XSRF by the token.
1324 $linkdate=$_POST['lf_linkdate'];
1325 1333
1326 $pluginManager->executeHooks('delete_link', $LINKSDB[$linkdate]); 1334 // FIXME! We keep `lf_linkdate` for consistency before a proper API. To be removed.
1335 $id = isset($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : intval(escape($_POST['lf_linkdate']));
1336
1337 $pluginManager->executeHooks('delete_link', $LINKSDB[$id]);
1327 1338
1328 unset($LINKSDB[$linkdate]); 1339 unset($LINKSDB[$id]);
1329 $LINKSDB->save('resource.page_cache'); // save to disk 1340 $LINKSDB->save('resource.page_cache'); // save to disk
1330 1341
1331 // If we are called from the bookmarklet, we must close the popup: 1342 // If we are called from the bookmarklet, we must close the popup:
@@ -1364,8 +1375,10 @@ function renderPage($conf, $pluginManager)
1364 // -------- User clicked the "EDIT" button on a link: Display link edit form. 1375 // -------- User clicked the "EDIT" button on a link: Display link edit form.
1365 if (isset($_GET['edit_link'])) 1376 if (isset($_GET['edit_link']))
1366 { 1377 {
1367 $link = $LINKSDB[$_GET['edit_link']]; // Read database 1378 $id = (int) escape($_GET['edit_link']);
1379 $link = $LINKSDB[$id]; // Read database
1368 if (!$link) { header('Location: ?'); exit; } // Link not found in database. 1380 if (!$link) { header('Location: ?'); exit; } // Link not found in database.
1381 $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT);
1369 $data = array( 1382 $data = array(
1370 'link' => $link, 1383 'link' => $link,
1371 'link_is_new' => false, 1384 'link_is_new' => false,
@@ -1389,10 +1402,10 @@ function renderPage($conf, $pluginManager)
1389 $link_is_new = false; 1402 $link_is_new = false;
1390 // Check if URL is not already in database (in this case, we will edit the existing link) 1403 // Check if URL is not already in database (in this case, we will edit the existing link)
1391 $link = $LINKSDB->getLinkFromUrl($url); 1404 $link = $LINKSDB->getLinkFromUrl($url);
1392 if (!$link) 1405 if (! $link)
1393 { 1406 {
1394 $link_is_new = true; 1407 $link_is_new = true;
1395 $linkdate = strval(date('Ymd_His')); 1408 $linkdate = strval(date(LinkDB::LINK_DATE_FORMAT));
1396 // Get title if it was provided in URL (by the bookmarklet). 1409 // Get title if it was provided in URL (by the bookmarklet).
1397 $title = empty($_GET['title']) ? '' : escape($_GET['title']); 1410 $title = empty($_GET['title']) ? '' : escape($_GET['title']);
1398 // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] 1411 // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
@@ -1416,7 +1429,7 @@ function renderPage($conf, $pluginManager)
1416 } 1429 }
1417 1430
1418 if ($url == '') { 1431 if ($url == '') {
1419 $url = '?' . smallHash($linkdate); 1432 $url = '?' . smallHash($linkdate . $LINKSDB->getNextId());
1420 $title = 'Note: '; 1433 $title = 'Note: ';
1421 } 1434 }
1422 $url = escape($url); 1435 $url = escape($url);
@@ -1430,6 +1443,8 @@ function renderPage($conf, $pluginManager)
1430 'tags' => $tags, 1443 'tags' => $tags,
1431 'private' => $private 1444 'private' => $private
1432 ); 1445 );
1446 } else {
1447 $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT);
1433 } 1448 }
1434 1449
1435 $data = array( 1450 $data = array(
@@ -1635,18 +1650,15 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
1635 $link['description'] = format_description($link['description'], $conf->get('redirector.url')); 1650 $link['description'] = format_description($link['description'], $conf->get('redirector.url'));
1636 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; 1651 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
1637 $link['class'] = $link['private'] == 0 ? $classLi : 'private'; 1652 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
1638 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); 1653 $link['timestamp'] = $link['created']->getTimestamp();
1639 $link['timestamp'] = $date->getTimestamp();
1640 if (! empty($link['updated'])) { 1654 if (! empty($link['updated'])) {
1641 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']); 1655 $link['updated_timestamp'] = $link['updated']->getTimestamp();
1642 $link['updated_timestamp'] = $date->getTimestamp();
1643 } else { 1656 } else {
1644 $link['updated_timestamp'] = ''; 1657 $link['updated_timestamp'] = '';
1645 } 1658 }
1646 $taglist = explode(' ', $link['tags']); 1659 $taglist = explode(' ', $link['tags']);
1647 uasort($taglist, 'strcasecmp'); 1660 uasort($taglist, 'strcasecmp');
1648 $link['taglist'] = $taglist; 1661 $link['taglist'] = $taglist;
1649 $link['shorturl'] = smallHash($link['linkdate']);
1650 // Check for both signs of a note: starting with ? and 7 chars long. 1662 // Check for both signs of a note: starting with ? and 7 chars long.
1651 if ($link['url'][0] === '?' && 1663 if ($link['url'][0] === '?' &&
1652 strlen($link['url']) === 7) { 1664 strlen($link['url']) === 7) {