aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-11-28 16:16:44 +0100
committerArthurHoaro <arthur@hoa.ro>2016-12-12 03:03:12 +0100
commit01878a75b93b9966f7366ea2937c118bbc3e459e (patch)
tree92286b70b4dd67a1bbd8d030fac6c0a70f64ded0 /index.php
parent1dc37f9cf8397e6050c4d5d981da263e6333a849 (diff)
downloadShaarli-01878a75b93b9966f7366ea2937c118bbc3e459e.tar.gz
Shaarli-01878a75b93b9966f7366ea2937c118bbc3e459e.tar.zst
Shaarli-01878a75b93b9966f7366ea2937c118bbc3e459e.zip
Apply the new ID system accros the whole codebase
Diffstat (limited to 'index.php')
-rw-r--r--index.php92
1 files changed, 57 insertions, 35 deletions
diff --git a/index.php b/index.php
index 5366cb0e..05f06452 100644
--- a/index.php
+++ b/index.php
@@ -564,24 +564,23 @@ 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(); 569 $ids = array();
570 foreach ($LINKSDB as $linkdate => $value) { 570 foreach ($LINKSDB as $id => $value) {
571 $linkdates[] = $linkdate; 571 $ids[] = $id;
572 } 572 }
573 rsort($linkdates);
574 $nb_of_days = 7; // We take 7 days. 573 $nb_of_days = 7; // We take 7 days.
575 $today = date('Ymd'); 574 $today = date('Ymd');
576 $days = array(); 575 $days = array();
577 576
578 foreach ($linkdates as $linkdate) { 577 foreach ($ids as $id) {
579 $day = substr($linkdate, 0, 8); // Extract day (without time) 578 $day = $LINKSDB[$id]['created']->format('Ymd'); // Extract day (without time)
580 if (strcmp($day,$today) < 0) { 579 if (strcmp($day, $today) < 0) {
581 if (empty($days[$day])) { 580 if (empty($days[$day])) {
582 $days[$day] = array(); 581 $days[$day] = array();
583 } 582 }
584 $days[$day][] = $linkdate; 583 $days[$day][] = $id;
585 } 584 }
586 585
587 if (count($days) > $nb_of_days) { 586 if (count($days) > $nb_of_days) {
@@ -601,7 +600,7 @@ function showDailyRSS($conf) {
601 echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL; 600 echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
602 601
603 // For each day. 602 // For each day.
604 foreach ($days as $day => $linkdates) { 603 foreach ($days as $day => $ids) {
605 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000'); 604 $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. 605 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
607 606
@@ -609,16 +608,15 @@ function showDailyRSS($conf) {
609 $links = array(); 608 $links = array();
610 609
611 // We pre-format some fields for proper output. 610 // We pre-format some fields for proper output.
612 foreach ($linkdates as $linkdate) { 611 foreach ($ids as $id) {
613 $l = $LINKSDB[$linkdate]; 612 $l = $LINKSDB[$id];
614 $l['formatedDescription'] = format_description($l['description'], $conf->get('redirector.url')); 613 $l['formatedDescription'] = format_description($l['description'], $conf->get('redirector.url'));
615 $l['thumbnail'] = thumbnail($conf, $l['url']); 614 $l['thumbnail'] = thumbnail($conf, $l['url']);
616 $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']); 615 $l['timestamp'] = $l['created']->getTimestamp();
617 $l['timestamp'] = $l_date->getTimestamp();
618 if (startsWith($l['url'], '?')) { 616 if (startsWith($l['url'], '?')) {
619 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute 617 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute
620 } 618 }
621 $links[$linkdate] = $l; 619 $links[$id] = $l;
622 } 620 }
623 621
624 // Then build the HTML for this day: 622 // Then build the HTML for this day:
@@ -677,11 +675,11 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager)
677 675
678 $taglist = explode(' ',$link['tags']); 676 $taglist = explode(' ',$link['tags']);
679 uasort($taglist, 'strcasecmp'); 677 uasort($taglist, 'strcasecmp');
678 $linksToDisplay[$key]['shorturl'] = smallHash($link['created']->format('Ymd_His'));
680 $linksToDisplay[$key]['taglist']=$taglist; 679 $linksToDisplay[$key]['taglist']=$taglist;
681 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); 680 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
682 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); 681 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']);
683 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); 682 $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
684 $linksToDisplay[$key]['timestamp'] = $date->getTimestamp();
685 } 683 }
686 684
687 /* We need to spread the articles on 3 columns. 685 /* We need to spread the articles on 3 columns.
@@ -831,7 +829,7 @@ function renderPage($conf, $pluginManager)
831 // Get only links which have a thumbnail. 829 // Get only links which have a thumbnail.
832 foreach($links as $link) 830 foreach($links as $link)
833 { 831 {
834 $permalink='?'.escape(smallHash($link['linkdate'])); 832 $permalink='?'.escape(smallHash($link['created']->format('Ymd_His')));
835 $thumb=lazyThumbnail($conf, $link['url'],$permalink); 833 $thumb=lazyThumbnail($conf, $link['url'],$permalink);
836 if ($thumb!='') // Only output links which have a thumbnail. 834 if ($thumb!='') // Only output links which have a thumbnail.
837 { 835 {
@@ -1245,13 +1243,28 @@ function renderPage($conf, $pluginManager)
1245 // -------- User clicked the "Save" button when editing a link: Save link to database. 1243 // -------- User clicked the "Save" button when editing a link: Save link to database.
1246 if (isset($_POST['save_edit'])) 1244 if (isset($_POST['save_edit']))
1247 { 1245 {
1248 $linkdate = $_POST['lf_linkdate'];
1249 $updated = isset($LINKSDB[$linkdate]) ? strval(date('Ymd_His')) : false;
1250
1251 // Go away! 1246 // Go away!
1252 if (! tokenOk($_POST['token'])) { 1247 if (! tokenOk($_POST['token'])) {
1253 die('Wrong token.'); 1248 die('Wrong token.');
1254 } 1249 }
1250
1251 // lf_id should only be present if the link exists.
1252 $id = !empty($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : $LINKSDB->getNextId();
1253 // Linkdate is kept here to:
1254 // - use the same permalink for notes as they're displayed when creating them
1255 // - let users hack creation date of their posts
1256 // See: https://github.com/shaarli/Shaarli/wiki/Datastore-hacks#changing-the-timestamp-for-a-link
1257 $linkdate = escape($_POST['lf_linkdate']);
1258 if (isset($LINKSDB[$id])) {
1259 // Edit
1260 $created = DateTime::createFromFormat('Ymd_His', $linkdate);
1261 $updated = new DateTime();
1262 } else {
1263 // New link
1264 $created = DateTime::createFromFormat('Ymd_His', $linkdate);
1265 $updated = null;
1266 }
1267
1255 // Remove multiple spaces. 1268 // Remove multiple spaces.
1256 $tags = trim(preg_replace('/\s\s+/', ' ', $_POST['lf_tags'])); 1269 $tags = trim(preg_replace('/\s\s+/', ' ', $_POST['lf_tags']));
1257 // Remove first '-' char in tags. 1270 // Remove first '-' char in tags.
@@ -1268,14 +1281,16 @@ function renderPage($conf, $pluginManager)
1268 } 1281 }
1269 1282
1270 $link = array( 1283 $link = array(
1284 'id' => $id,
1271 'title' => trim($_POST['lf_title']), 1285 'title' => trim($_POST['lf_title']),
1272 'url' => $url, 1286 'url' => $url,
1273 'description' => $_POST['lf_description'], 1287 'description' => $_POST['lf_description'],
1274 'private' => (isset($_POST['lf_private']) ? 1 : 0), 1288 'private' => (isset($_POST['lf_private']) ? 1 : 0),
1275 'linkdate' => $linkdate, 1289 'created' => $created,
1276 'updated' => $updated, 1290 'updated' => $updated,
1277 'tags' => str_replace(',', ' ', $tags) 1291 'tags' => str_replace(',', ' ', $tags)
1278 ); 1292 );
1293
1279 // If title is empty, use the URL as title. 1294 // If title is empty, use the URL as title.
1280 if ($link['title'] == '') { 1295 if ($link['title'] == '') {
1281 $link['title'] = $link['url']; 1296 $link['title'] = $link['url'];
@@ -1283,7 +1298,7 @@ function renderPage($conf, $pluginManager)
1283 1298
1284 $pluginManager->executeHooks('save_link', $link); 1299 $pluginManager->executeHooks('save_link', $link);
1285 1300
1286 $LINKSDB[$linkdate] = $link; 1301 $LINKSDB[$id] = $link;
1287 $LINKSDB->save($conf->get('resource.page_cache')); 1302 $LINKSDB->save($conf->get('resource.page_cache'));
1288 pubsubhub($conf); 1303 pubsubhub($conf);
1289 1304
@@ -1296,7 +1311,7 @@ function renderPage($conf, $pluginManager)
1296 $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?'; 1311 $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?';
1297 $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); 1312 $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
1298 // Scroll to the link which has been edited. 1313 // Scroll to the link which has been edited.
1299 $location .= '#' . smallHash($_POST['lf_linkdate']); 1314 $location .= '#' . smallHash($created->format('Ymd_His'));
1300 // After saving the link, redirect to the page the user was on. 1315 // After saving the link, redirect to the page the user was on.
1301 header('Location: '. $location); 1316 header('Location: '. $location);
1302 exit; 1317 exit;
@@ -1307,8 +1322,10 @@ function renderPage($conf, $pluginManager)
1307 { 1322 {
1308 // If we are called from the bookmarklet, we must close the popup: 1323 // 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; } 1324 if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; }
1325 $link = $LINKSDB[(int) escape($_POST['lf_id'])];
1310 $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' ); 1326 $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' );
1311 $returnurl .= '#'.smallHash($_POST['lf_linkdate']); // Scroll to the link which has been edited. 1327 // Scroll to the link which has been edited.
1328 $returnurl .= '#'.smallHash($link['created']->format('Ymd_His'));
1312 $returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); 1329 $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. 1330 header('Location: '.$returnurl); // After canceling, redirect to the page the user was on.
1314 exit; 1331 exit;
@@ -1318,14 +1335,17 @@ function renderPage($conf, $pluginManager)
1318 if (isset($_POST['delete_link'])) 1335 if (isset($_POST['delete_link']))
1319 { 1336 {
1320 if (!tokenOk($_POST['token'])) die('Wrong token.'); 1337 if (!tokenOk($_POST['token'])) die('Wrong token.');
1338
1321 // We do not need to ask for confirmation: 1339 // We do not need to ask for confirmation:
1322 // - confirmation is handled by JavaScript 1340 // - confirmation is handled by JavaScript
1323 // - we are protected from XSRF by the token. 1341 // - we are protected from XSRF by the token.
1324 $linkdate=$_POST['lf_linkdate'];
1325 1342
1326 $pluginManager->executeHooks('delete_link', $LINKSDB[$linkdate]); 1343 // FIXME! We keep `lf_linkdate` for consistency before a proper API. To be removed.
1344 $id = isset($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : (int) escape($_POST['lf_linkdate']);
1327 1345
1328 unset($LINKSDB[$linkdate]); 1346 $pluginManager->executeHooks('delete_link', $LINKSDB[$id]);
1347
1348 unset($LINKSDB[$id]);
1329 $LINKSDB->save('resource.page_cache'); // save to disk 1349 $LINKSDB->save('resource.page_cache'); // save to disk
1330 1350
1331 // If we are called from the bookmarklet, we must close the popup: 1351 // If we are called from the bookmarklet, we must close the popup:
@@ -1364,8 +1384,10 @@ function renderPage($conf, $pluginManager)
1364 // -------- User clicked the "EDIT" button on a link: Display link edit form. 1384 // -------- User clicked the "EDIT" button on a link: Display link edit form.
1365 if (isset($_GET['edit_link'])) 1385 if (isset($_GET['edit_link']))
1366 { 1386 {
1367 $link = $LINKSDB[$_GET['edit_link']]; // Read database 1387 $id = (int) escape($_GET['edit_link']);
1388 $link = $LINKSDB[$id]; // Read database
1368 if (!$link) { header('Location: ?'); exit; } // Link not found in database. 1389 if (!$link) { header('Location: ?'); exit; } // Link not found in database.
1390 $link['linkdate'] = $link['created']->format('Ymd_His');
1369 $data = array( 1391 $data = array(
1370 'link' => $link, 1392 'link' => $link,
1371 'link_is_new' => false, 1393 'link_is_new' => false,
@@ -1389,7 +1411,7 @@ function renderPage($conf, $pluginManager)
1389 $link_is_new = false; 1411 $link_is_new = false;
1390 // Check if URL is not already in database (in this case, we will edit the existing link) 1412 // Check if URL is not already in database (in this case, we will edit the existing link)
1391 $link = $LINKSDB->getLinkFromUrl($url); 1413 $link = $LINKSDB->getLinkFromUrl($url);
1392 if (!$link) 1414 if (! $link)
1393 { 1415 {
1394 $link_is_new = true; 1416 $link_is_new = true;
1395 $linkdate = strval(date('Ymd_His')); 1417 $linkdate = strval(date('Ymd_His'));
@@ -1430,6 +1452,8 @@ function renderPage($conf, $pluginManager)
1430 'tags' => $tags, 1452 'tags' => $tags,
1431 'private' => $private 1453 'private' => $private
1432 ); 1454 );
1455 } else {
1456 $link['linkdate'] = $link['created']->format('Ymd_His');
1433 } 1457 }
1434 1458
1435 $data = array( 1459 $data = array(
@@ -1635,18 +1659,16 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
1635 $link['description'] = format_description($link['description'], $conf->get('redirector.url')); 1659 $link['description'] = format_description($link['description'], $conf->get('redirector.url'));
1636 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; 1660 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
1637 $link['class'] = $link['private'] == 0 ? $classLi : 'private'; 1661 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
1638 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); 1662 $link['timestamp'] = $link['created']->getTimestamp();
1639 $link['timestamp'] = $date->getTimestamp();
1640 if (! empty($link['updated'])) { 1663 if (! empty($link['updated'])) {
1641 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']); 1664 $link['updated_timestamp'] = $link['updated']->getTimestamp();
1642 $link['updated_timestamp'] = $date->getTimestamp();
1643 } else { 1665 } else {
1644 $link['updated_timestamp'] = ''; 1666 $link['updated_timestamp'] = '';
1645 } 1667 }
1646 $taglist = explode(' ', $link['tags']); 1668 $taglist = explode(' ', $link['tags']);
1647 uasort($taglist, 'strcasecmp'); 1669 uasort($taglist, 'strcasecmp');
1648 $link['taglist'] = $taglist; 1670 $link['taglist'] = $taglist;
1649 $link['shorturl'] = smallHash($link['linkdate']); 1671 $link['shorturl'] = smallHash($link['created']->format('Ymd_His'));
1650 // Check for both signs of a note: starting with ? and 7 chars long. 1672 // Check for both signs of a note: starting with ? and 7 chars long.
1651 if ($link['url'][0] === '?' && 1673 if ($link['url'][0] === '?' &&
1652 strlen($link['url']) === 7) { 1674 strlen($link['url']) === 7) {