aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthur <arthur@hoa.ro>2016-05-14 11:11:11 +0200
committerArthur <arthur@hoa.ro>2016-05-14 11:11:11 +0200
commit3fdcc7bd47114776a5a8a280783529ee2dd690a1 (patch)
treed5b9497bd30333e799d93d2fe8b628a4c7fbc1d9
parent52ccf0d7ee25eb0b6fc405aafcf7211ef07307e1 (diff)
parent141a86c503af8e314381b3ee39ba4287fdfac63e (diff)
downloadShaarli-3fdcc7bd47114776a5a8a280783529ee2dd690a1.tar.gz
Shaarli-3fdcc7bd47114776a5a8a280783529ee2dd690a1.tar.zst
Shaarli-3fdcc7bd47114776a5a8a280783529ee2dd690a1.zip
Merge pull request #560 from ArthurHoaro/nb-private-shaare
Private links counter in the header
-rw-r--r--application/LinkUtils.php16
-rw-r--r--application/PageBuilder.php145
-rw-r--r--index.php142
-rw-r--r--tests/LinkUtilsTest.php9
-rw-r--r--tpl/page.header.html3
5 files changed, 177 insertions, 138 deletions
diff --git a/application/LinkUtils.php b/application/LinkUtils.php
index 2df76ba8..da04ca97 100644
--- a/application/LinkUtils.php
+++ b/application/LinkUtils.php
@@ -77,3 +77,19 @@ function html_extract_charset($html)
77 77
78 return false; 78 return false;
79} 79}
80
81/**
82 * Count private links in given linklist.
83 *
84 * @param array $links Linklist.
85 *
86 * @return int Number of private links.
87 */
88function count_private($links)
89{
90 $cpt = 0;
91 foreach ($links as $link) {
92 $cpt = $link['private'] == true ? $cpt + 1 : $cpt;
93 }
94 return $cpt;
95}
diff --git a/application/PageBuilder.php b/application/PageBuilder.php
new file mode 100644
index 00000000..82580787
--- /dev/null
+++ b/application/PageBuilder.php
@@ -0,0 +1,145 @@
1<?php
2
3/**
4 * This class is in charge of building the final page.
5 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
6 * $p = new PageBuilder();
7 * $p->assign('myfield','myvalue');
8 * $p->renderPage('mytemplate');
9 */
10class PageBuilder
11{
12 /**
13 * @var RainTPL RainTPL instance.
14 */
15 private $tpl;
16
17 /**
18 * PageBuilder constructor.
19 * $tpl is initialized at false for lazy loading.
20 */
21 function __construct()
22 {
23 $this->tpl = false;
24 }
25
26 /**
27 * Initialize all default tpl tags.
28 */
29 private function initialize()
30 {
31 $this->tpl = new RainTPL();
32
33 try {
34 $version = ApplicationUtils::checkUpdate(
35 shaarli_version,
36 $GLOBALS['config']['UPDATECHECK_FILENAME'],
37 $GLOBALS['config']['UPDATECHECK_INTERVAL'],
38 $GLOBALS['config']['ENABLE_UPDATECHECK'],
39 isLoggedIn(),
40 $GLOBALS['config']['UPDATECHECK_BRANCH']
41 );
42 $this->tpl->assign('newVersion', escape($version));
43 $this->tpl->assign('versionError', '');
44
45 } catch (Exception $exc) {
46 logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], $exc->getMessage());
47 $this->tpl->assign('newVersion', '');
48 $this->tpl->assign('versionError', escape($exc->getMessage()));
49 }
50
51 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
52 $searchcrits = ''; // Search criteria
53 if (!empty($_GET['searchtags'])) {
54 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
55 }
56 if (!empty($_GET['searchterm'])) {
57 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
58 }
59 $this->tpl->assign('searchcrits', $searchcrits);
60 $this->tpl->assign('source', index_url($_SERVER));
61 $this->tpl->assign('version', shaarli_version);
62 $this->tpl->assign('scripturl', index_url($_SERVER));
63 $this->tpl->assign('pagetitle', 'Shaarli');
64 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
65 if (!empty($GLOBALS['title'])) {
66 $this->tpl->assign('pagetitle', $GLOBALS['title']);
67 }
68 if (!empty($GLOBALS['titleLink'])) {
69 $this->tpl->assign('titleLink', $GLOBALS['titleLink']);
70 }
71 if (!empty($GLOBALS['pagetitle'])) {
72 $this->tpl->assign('pagetitle', $GLOBALS['pagetitle']);
73 }
74 $this->tpl->assign('shaarlititle', empty($GLOBALS['title']) ? 'Shaarli': $GLOBALS['title']);
75 if (!empty($GLOBALS['plugin_errors'])) {
76 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
77 }
78 }
79
80 /**
81 * The following assign() method is basically the same as RainTPL (except lazy loading)
82 *
83 * @param string $placeholder Template placeholder.
84 * @param mixed $value Value to assign.
85 */
86 public function assign($placeholder, $value)
87 {
88 // Lazy initialization
89 if ($this->tpl === false) {
90 $this->initialize();
91 }
92 $this->tpl->assign($placeholder, $value);
93 }
94
95 /**
96 * Assign an array of data to the template builder.
97 *
98 * @param array $data Data to assign.
99 *
100 * @return false if invalid data.
101 */
102 public function assignAll($data)
103 {
104 // Lazy initialization
105 if ($this->tpl === false) {
106 $this->initialize();
107 }
108
109 if (empty($data) || !is_array($data)){
110 return false;
111 }
112
113 foreach ($data as $key => $value) {
114 $this->assign($key, $value);
115 }
116 }
117
118 /**
119 * Render a specific page (using a template file).
120 * e.g. $pb->renderPage('picwall');
121 *
122 * @param string $page Template filename (without extension).
123 */
124 public function renderPage($page)
125 {
126 // Lazy initialization
127 if ($this->tpl===false) {
128 $this->initialize();
129 }
130 $this->tpl->draw($page);
131 }
132
133 /**
134 * Render a 404 page (uses the template : tpl/404.tpl)
135 * usage : $PAGE->render404('The link was deleted')
136 *
137 * @param string $message A messate to display what is not found
138 */
139 public function render404($message = 'The page you are trying to reach does not exist or has been deleted.')
140 {
141 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
142 $this->tpl->assign('error_message', $message);
143 $this->renderPage('404');
144 }
145}
diff --git a/index.php b/index.php
index 1eaf70ce..5b8384ba 100644
--- a/index.php
+++ b/index.php
@@ -162,6 +162,7 @@ require_once 'application/LinkDB.php';
162require_once 'application/LinkFilter.php'; 162require_once 'application/LinkFilter.php';
163require_once 'application/LinkUtils.php'; 163require_once 'application/LinkUtils.php';
164require_once 'application/NetscapeBookmarkUtils.php'; 164require_once 'application/NetscapeBookmarkUtils.php';
165require_once 'application/PageBuilder.php';
165require_once 'application/TimeZone.php'; 166require_once 'application/TimeZone.php';
166require_once 'application/Url.php'; 167require_once 'application/Url.php';
167require_once 'application/Utils.php'; 168require_once 'application/Utils.php';
@@ -563,128 +564,6 @@ function tokenOk($token)
563} 564}
564 565
565// ------------------------------------------------------------------------------------------ 566// ------------------------------------------------------------------------------------------
566/* This class is in charge of building the final page.
567 (This is basically a wrapper around RainTPL which pre-fills some fields.)
568 p = new pageBuilder;
569 p.assign('myfield','myvalue');
570 p.renderPage('mytemplate');
571
572*/
573class pageBuilder
574{
575 private $tpl; // RainTPL template
576
577 function __construct()
578 {
579 $this->tpl = false;
580 }
581
582 /**
583 * Initialize all default tpl tags.
584 */
585 private function initialize()
586 {
587 $this->tpl = new RainTPL;
588
589 try {
590 $version = ApplicationUtils::checkUpdate(
591 shaarli_version,
592 $GLOBALS['config']['UPDATECHECK_FILENAME'],
593 $GLOBALS['config']['UPDATECHECK_INTERVAL'],
594 $GLOBALS['config']['ENABLE_UPDATECHECK'],
595 isLoggedIn(),
596 $GLOBALS['config']['UPDATECHECK_BRANCH']
597 );
598 $this->tpl->assign('newVersion', escape($version));
599 $this->tpl->assign('versionError', '');
600
601 } catch (Exception $exc) {
602 logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], $exc->getMessage());
603 $this->tpl->assign('newVersion', '');
604 $this->tpl->assign('versionError', escape($exc->getMessage()));
605 }
606
607 $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
608 $searchcrits = ''; // Search criteria
609 if (!empty($_GET['searchtags'])) {
610 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
611 }
612 if (!empty($_GET['searchterm'])) {
613 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
614 }
615 $this->tpl->assign('searchcrits', $searchcrits);
616 $this->tpl->assign('source', index_url($_SERVER));
617 $this->tpl->assign('version', shaarli_version);
618 $this->tpl->assign('scripturl', index_url($_SERVER));
619 $this->tpl->assign('pagetitle', 'Shaarli');
620 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
621 if (!empty($GLOBALS['title'])) {
622 $this->tpl->assign('pagetitle', $GLOBALS['title']);
623 }
624 if (!empty($GLOBALS['titleLink'])) {
625 $this->tpl->assign('titleLink', $GLOBALS['titleLink']);
626 }
627 if (!empty($GLOBALS['pagetitle'])) {
628 $this->tpl->assign('pagetitle', $GLOBALS['pagetitle']);
629 }
630 $this->tpl->assign('shaarlititle', empty($GLOBALS['title']) ? 'Shaarli': $GLOBALS['title']);
631 if (!empty($GLOBALS['plugin_errors'])) {
632 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
633 }
634 }
635
636 // The following assign() method is basically the same as RainTPL (except that it's lazy)
637 public function assign($what,$where)
638 {
639 if ($this->tpl===false) $this->initialize(); // Lazy initialization
640 $this->tpl->assign($what,$where);
641 }
642
643 /**
644 * Assign an array of data to the template builder.
645 *
646 * @param array $data Data to assign.
647 *
648 * @return false if invalid data.
649 */
650 public function assignAll($data)
651 {
652 // Lazy initialization
653 if ($this->tpl === false) {
654 $this->initialize();
655 }
656
657 if (empty($data) || !is_array($data)){
658 return false;
659 }
660
661 foreach ($data as $key => $value) {
662 $this->assign($key, $value);
663 }
664 }
665
666 // Render a specific page (using a template).
667 // e.g. pb.renderPage('picwall')
668 public function renderPage($page)
669 {
670 if ($this->tpl===false) $this->initialize(); // Lazy initialization
671 $this->tpl->draw($page);
672 }
673
674 /**
675 * Render a 404 page (uses the template : tpl/404.tpl)
676 *
677 * usage : $PAGE->render404('The link was deleted')
678 * @param string $message A messate to display what is not found
679 */
680 public function render404($message='The page you are trying to reach does not exist or has been deleted.') {
681 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
682 $this->tpl->assign('error_message', $message);
683 $this->renderPage('404');
684 }
685}
686
687// ------------------------------------------------------------------------------------------
688// Daily RSS feed: 1 RSS entry per day giving all the links on that day. 567// Daily RSS feed: 1 RSS entry per day giving all the links on that day.
689// Gives the last 7 days (which have links). 568// Gives the last 7 days (which have links).
690// This RSS feed cannot be filtered. 569// This RSS feed cannot be filtered.
@@ -857,7 +736,6 @@ function showDaily($pageBuilder, $LINKSDB)
857 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000'); 736 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
858 $data = array( 737 $data = array(
859 'linksToDisplay' => $linksToDisplay, 738 'linksToDisplay' => $linksToDisplay,
860 'linkcount' => count($LINKSDB),
861 'cols' => $columns, 739 'cols' => $columns,
862 'day' => $dayDate->getTimestamp(), 740 'day' => $dayDate->getTimestamp(),
863 'previousday' => $previousday, 741 'previousday' => $previousday,
@@ -912,7 +790,9 @@ function renderPage()
912 die($e->getMessage()); 790 die($e->getMessage());
913 } 791 }
914 792
915 $PAGE = new pageBuilder; 793 $PAGE = new PageBuilder();
794 $PAGE->assign('linkcount', count($LINKSDB));
795 $PAGE->assign('privateLinkcount', count_private($LINKSDB));
916 796
917 // Determine which page will be rendered. 797 // Determine which page will be rendered.
918 $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : ''; 798 $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
@@ -979,7 +859,6 @@ function renderPage()
979 } 859 }
980 860
981 $data = array( 861 $data = array(
982 'linkcount' => count($LINKSDB),
983 'linksToDisplay' => $linksToDisplay, 862 'linksToDisplay' => $linksToDisplay,
984 ); 863 );
985 $pluginManager->executeHooks('render_picwall', $data, array('loggedin' => isLoggedIn())); 864 $pluginManager->executeHooks('render_picwall', $data, array('loggedin' => isLoggedIn()));
@@ -1029,7 +908,6 @@ function renderPage()
1029 } 908 }
1030 909
1031 $data = array( 910 $data = array(
1032 'linkcount' => count($LINKSDB),
1033 'tags' => $tagList, 911 'tags' => $tagList,
1034 ); 912 );
1035 $pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => isLoggedIn())); 913 $pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => isLoggedIn()));
@@ -1217,7 +1095,6 @@ function renderPage()
1217 if ($targetPage == Router::$PAGE_TOOLS) 1095 if ($targetPage == Router::$PAGE_TOOLS)
1218 { 1096 {
1219 $data = array( 1097 $data = array(
1220 'linkcount' => count($LINKSDB),
1221 'pageabsaddr' => index_url($_SERVER), 1098 'pageabsaddr' => index_url($_SERVER),
1222 ); 1099 );
1223 $pluginManager->executeHooks('render_tools', $data); 1100 $pluginManager->executeHooks('render_tools', $data);
@@ -1262,7 +1139,6 @@ function renderPage()
1262 } 1139 }
1263 else // show the change password form. 1140 else // show the change password form.
1264 { 1141 {
1265 $PAGE->assign('linkcount',count($LINKSDB));
1266 $PAGE->assign('token',getToken()); 1142 $PAGE->assign('token',getToken());
1267 $PAGE->renderPage('changepassword'); 1143 $PAGE->renderPage('changepassword');
1268 exit; 1144 exit;
@@ -1310,7 +1186,6 @@ function renderPage()
1310 } 1186 }
1311 else // Show the configuration form. 1187 else // Show the configuration form.
1312 { 1188 {
1313 $PAGE->assign('linkcount',count($LINKSDB));
1314 $PAGE->assign('token',getToken()); 1189 $PAGE->assign('token',getToken());
1315 $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] ); 1190 $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] );
1316 $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'] ); 1191 $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'] );
@@ -1326,7 +1201,6 @@ function renderPage()
1326 if ($targetPage == Router::$PAGE_CHANGETAG) 1201 if ($targetPage == Router::$PAGE_CHANGETAG)
1327 { 1202 {
1328 if (empty($_POST['fromtag']) || (empty($_POST['totag']) && isset($_POST['renametag']))) { 1203 if (empty($_POST['fromtag']) || (empty($_POST['totag']) && isset($_POST['renametag']))) {
1329 $PAGE->assign('linkcount', count($LINKSDB));
1330 $PAGE->assign('token', getToken()); 1204 $PAGE->assign('token', getToken());
1331 $PAGE->assign('tags', $LINKSDB->allTags()); 1205 $PAGE->assign('tags', $LINKSDB->allTags());
1332 $PAGE->renderPage('changetag'); 1206 $PAGE->renderPage('changetag');
@@ -1375,7 +1249,6 @@ function renderPage()
1375 // -------- User wants to add a link without using the bookmarklet: Show form. 1249 // -------- User wants to add a link without using the bookmarklet: Show form.
1376 if ($targetPage == Router::$PAGE_ADDLINK) 1250 if ($targetPage == Router::$PAGE_ADDLINK)
1377 { 1251 {
1378 $PAGE->assign('linkcount',count($LINKSDB));
1379 $PAGE->renderPage('addlink'); 1252 $PAGE->renderPage('addlink');
1380 exit; 1253 exit;
1381 } 1254 }
@@ -1501,7 +1374,6 @@ function renderPage()
1501 $link = $LINKSDB[$_GET['edit_link']]; // Read database 1374 $link = $LINKSDB[$_GET['edit_link']]; // Read database
1502 if (!$link) { header('Location: ?'); exit; } // Link not found in database. 1375 if (!$link) { header('Location: ?'); exit; } // Link not found in database.
1503 $data = array( 1376 $data = array(
1504 'linkcount' => count($LINKSDB),
1505 'link' => $link, 1377 'link' => $link,
1506 'link_is_new' => false, 1378 'link_is_new' => false,
1507 'token' => getToken(), 1379 'token' => getToken(),
@@ -1569,7 +1441,6 @@ function renderPage()
1569 } 1441 }
1570 1442
1571 $data = array( 1443 $data = array(
1572 'linkcount' => count($LINKSDB),
1573 'link' => $link, 1444 'link' => $link,
1574 'link_is_new' => $link_is_new, 1445 'link_is_new' => $link_is_new,
1575 'token' => getToken(), // XSRF protection. 1446 'token' => getToken(), // XSRF protection.
@@ -1591,7 +1462,6 @@ function renderPage()
1591 // Export links as a Netscape Bookmarks file 1462 // Export links as a Netscape Bookmarks file
1592 1463
1593 if (empty($_GET['selection'])) { 1464 if (empty($_GET['selection'])) {
1594 $PAGE->assign('linkcount',count($LINKSDB));
1595 $PAGE->renderPage('export'); 1465 $PAGE->renderPage('export');
1596 exit; 1466 exit;
1597 } 1467 }
@@ -1650,7 +1520,6 @@ function renderPage()
1650 // -------- Show upload/import dialog: 1520 // -------- Show upload/import dialog:
1651 if ($targetPage == Router::$PAGE_IMPORT) 1521 if ($targetPage == Router::$PAGE_IMPORT)
1652 { 1522 {
1653 $PAGE->assign('linkcount',count($LINKSDB));
1654 $PAGE->assign('token',getToken()); 1523 $PAGE->assign('token',getToken());
1655 $PAGE->assign('maxfilesize',getMaxFileSize()); 1524 $PAGE->assign('maxfilesize',getMaxFileSize());
1656 $PAGE->renderPage('import'); 1525 $PAGE->renderPage('import');
@@ -1882,7 +1751,6 @@ function buildLinkList($PAGE,$LINKSDB)
1882 1751
1883 // Fill all template fields. 1752 // Fill all template fields.
1884 $data = array( 1753 $data = array(
1885 'linkcount' => count($LINKSDB),
1886 'previous_page_url' => $previous_page_url, 1754 'previous_page_url' => $previous_page_url,
1887 'next_page_url' => $next_page_url, 1755 'next_page_url' => $next_page_url,
1888 'page_current' => $page, 1756 'page_current' => $page,
@@ -2157,7 +2025,7 @@ function install()
2157 $timezone_html = '<tr><td><b>Timezone:</b></td><td>'.$timezone_form.'</td></tr>'; 2025 $timezone_html = '<tr><td><b>Timezone:</b></td><td>'.$timezone_form.'</td></tr>';
2158 } 2026 }
2159 2027
2160 $PAGE = new pageBuilder; 2028 $PAGE = new PageBuilder();
2161 $PAGE->assign('timezone_html',$timezone_html); 2029 $PAGE->assign('timezone_html',$timezone_html);
2162 $PAGE->assign('timezone_js',$timezone_js); 2030 $PAGE->assign('timezone_js',$timezone_js);
2163 $PAGE->renderPage('install'); 2031 $PAGE->renderPage('install');
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php
index 609a80cb..d1b022fd 100644
--- a/tests/LinkUtilsTest.php
+++ b/tests/LinkUtilsTest.php
@@ -84,4 +84,13 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
84 $html = '<html><meta>stuff</meta><meta charset=""/></html>'; 84 $html = '<html><meta>stuff</meta><meta charset=""/></html>';
85 $this->assertFalse(html_extract_charset($html)); 85 $this->assertFalse(html_extract_charset($html));
86 } 86 }
87
88 /**
89 * Test count_private.
90 */
91 public function testCountPrivateLinks()
92 {
93 $refDB = new ReferenceLinkDB();
94 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
95 }
87} 96}
diff --git a/tpl/page.header.html b/tpl/page.header.html
index 52429f23..3a09ecd9 100644
--- a/tpl/page.header.html
+++ b/tpl/page.header.html
@@ -2,7 +2,8 @@
2<div id="logo" title="Share your links !" onclick="document.location='?';"></div> 2<div id="logo" title="Share your links !" onclick="document.location='?';"></div>
3 3
4<div id="linkcount" class="nomobile"> 4<div id="linkcount" class="nomobile">
5 {if="!empty($linkcount)"}{$linkcount} links{/if} 5 {if="!empty($linkcount)"}{$linkcount} links{/if}<br>
6 {if="!empty($privateLinkcount)"}{$privateLinkcount} private links{/if}
6</div> 7</div>
7 8
8<div id="menu"> 9<div id="menu">