-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss-bookmarks.php
238 lines (217 loc) · 11.1 KB
/
ss-bookmarks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
define('SCRIPT_TITLE', 'SS Bookmarks');
define('SCRIPT_VERSION', 0.71);
define('SCRIPT_AUTHOR', 'Dominic Manley');
define('SCRIPT_HOMEPAGE', 'https://github.com/dominicwa/ss-bookmarks');
error_reporting(E_ERROR);
/*******************************************************************************/
/* Script configuration */
/*******************************************************************************/
$sPageTitle = SCRIPT_TITLE . ' v ' . SCRIPT_VERSION; // the page title (typically shown in the browser title bar)
$sScriptName = basename(__FILE__); // filename of this script (best not to change)
$bEnableJavascript = true; // provides some UI improvements (inc. bookmarklet)
$bShowBookmarklet = true; // whether to show a draggable bookmarklet link in the top right of the page
$sNoTagLabel = 'no-tags'; // default label for bookmarks with no tags
$sLinkTarget = '_blank'; // target for all links ('_self' will open in same window, '_blank' in a new window)
$bEnableBackups = false; // backup you script (and bookmark data)
$bBackupFilename = $sScriptName . '.bck.' . date('ymd'); // filename to backup to (using date('ymd') will increment daily)
$iViewPortWidth = 600; // viewport width in pixels (zooms in and eliminates white-space on iDevices)
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*!!!!!!!!!!!!!!!!!DO NOT EDIT ANYTHING BELOW THIS LINE!!!!!!!!!!!!!!!!!!!!!!!!*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*DATA-START*/
$iNextIndex = 1;
$aBookmarks = array(
0 => array('label' => 'SS-Bookmarks', 'url' => 'https://github.com/dominicwa/ss-bookmarks', 'tags' => ''),
);/*DATA-END*/
/*******************************************************************************/
/* Add/delete bookmarks */
/*******************************************************************************/
$bReWriteScript = false; // do we rewrite the script with updated data?
if ($_GET['action'] == 'add' || $_GET['action'] == 'bml') {
$aNewBookmark = array(
'label' => $_GET['label'],
'url' => $_GET['url'],
'tags' => str_replace(', ', ',', $_GET['tags']) // (strip spaces between commas)
);
$aBookmarks[$iNextIndex] = $aNewBookmark; // add new bookmark at next index
$iNextIndex++; // increase the index for next time
$bReWriteScript = true;
}
if ($_GET['action'] == 'delete') {
$aBookmarkTags = explode(',', $aBookmarks[intval($_GET['uid'])]['tags']);
// first just remove the tag from the bookmark's record (it may have more than one)
foreach ($aBookmarkTags as $iIndex => $aBookmarkTag) {
if ($aBookmarkTag == $_GET['tag']) {
unset($aBookmarkTags[$iIndex]);
}
}
$aBookmarks[intval($_GET['uid'])]['tags'] = implode(',', $aBookmarkTags);
// if there are no more tags for this bookmark, remove the record completely
if ($aBookmarks[intval($_GET['uid'])]['tags'] == '') {
unset($aBookmarks[intval($_GET['uid'])]);
$iNextIndex--; // descrease the index for next time
}
$bReWriteScript = true;
}
if ($bReWriteScript) {
$sScriptContents = file_get_contents($sScriptName); // get the contents of this very file
$sPreData = substr($sScriptContents, 0, strpos($sScriptContents, '/*DATA-START*/') + strlen('/*DATA-START*/')); // grab everything AFTER /*DATA-START*/
$sAftData = substr($sScriptContents, strpos($sScriptContents, '/*DATA-END*/')); // grab everything UP TO /*DATA-END*/
$sNewData = "\n"; // build new data (as PHP) to insert in the middle
$sNewData .= "\t" . '$iNextIndex = ' . $iNextIndex . ';' . "\n";
$sNewData .= "\t" . '$aBookmarks = array(' . "\n";
foreach ($aBookmarks as $iIndex => $aBookmark) {
$sNewData .= "\t\t" . $iIndex . ' => array(\'label\' => \'' .
str_replace('\'', '\\\'', $aBookmark['label']) . '\', \'url\' => \'' .
str_replace('\'', '\\\'', $aBookmark['url']) . '\', \'tags\' => \'' .
str_replace('\'', '\\\'', $aBookmark['tags']) . '\'),' . "\n";
}
$sNewData .= "\t" . ');';
if ($bEnableBackups) {
file_put_contents($bBackupFilename, $sScriptContents); // if condfigured to do so, save a copy of current script before overwriting
}
file_put_contents($sScriptName, $sPreData . $sNewData . $sAftData); // overwrite current script file with new data
}
if ($_GET['action'] == 'bml') {
if ($bEnableJavascript)
echo '<html><head><script type="text/javascript">window.close();</script></head></html>';
else
echo '<html><body>Bookmark added. Enable Javascript to auto-close this window.</body></html>';
exit();
}
/*******************************************************************************/
/* Build a tags array from tags used in bookmarks data, sort alphabetically */
/*******************************************************************************/
$aTags = array();
foreach ($aBookmarks as $aBookmark) {
if ($aBookmark['tags'] != '') {
$aBookmarkTags = explode(',', $aBookmark['tags']);
$aTags = array_merge($aTags, $aBookmarkTags);
}
}
$aTags = array_unique($aTags);
sort($aTags);
array_unshift($aTags, $sNoTagLabel);
/*******************************************************************************/
/* Identify current tag, default to no-tags */
/*******************************************************************************/
$sCurrentTag = $sNoTagLabel;
if ($_GET['tag'] != '' && in_array($_GET['tag'], $aTags)) {
$sCurrentTag = $_GET['tag'];
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=<?php echo $iViewPortWidth; ?>" />
<title><?php echo htmlentities($sPageTitle); ?></title>
<style type="text/css">
* {font-family: Arial, Helvetica, sans-serif; font-size: 11px; }
a {color: blue;}
a:hover {color: #F60;}
#tagForm {padding-bottom: 10px; border-bottom: 1px solid #CCC;}
#tagForm select {float: left;}
#tagForm a {float: right;}
ul {margin: 0; padding: 0; margin-top: 10px; margin-bottom: 10px; border-bottom: 1px solid #CCC; padding-bottom: 5px;}
li {list-style: none; margin-bottom: 5px;}
.bmLink {float: left;}
.bmEdit {float: right;}
.bmEdit a {display: block; padding: 0 5px; background: #EEE; text-decoration: none;}
.bmEdit a:hover {background: #CCC;}
.empty {margin-bottom: 10px; border-bottom: 1px solid #CCC; padding-bottom: 10px;}
#addForm {margin: 0 auto; padding: 10px; width: 300px; background: #EEE;}
#addForm label {display: block; float: left; width: 100px; margin: 5px; text-align: right; clear: left;}
#addForm input {margin-top: 4px;}
#addButton {margin-left: 110px; clear: left; _margin-left: 118px;}
#label {width: 100px;}
#url {width: 160px;}
#tags {width: 100px;}
.clear {clear: both;}<?php if ($bEnableJavascript) { echo "\n"; ?>
#submitButton {display: none;}<?php } echo "\n"; ?>
</style>
<?php if ($bEnableJavascript) { ?>
<script type="text/javascript">
var fChangeTag = function (o) {
// produces cleaner urls...
var sUrl = '<?php echo str_replace('\'', '\\\'', urlencode($sScriptName)); ?>';
if (o.value != '<?php echo str_replace('\'', '\\\'', $sNoTagLabel); ?>') {
sUrl += '?tag=' + encodeURIComponent(o.value)
}
window.location.href = sUrl;
}
window.addEventListener('load', (event) => {
var cleanURL = window.location.origin + window.location.pathname.replace('index.php', '');
document.getElementById('bml').setAttribute('href', 'javascript:(function(){var r = prompt(\'Title\', document.title);if (r!=null){window.open(\'' + cleanURL + '?action=bml&label=\' + r + \'&url=\' + window.location.href);}}());');
<?php if (isset($_GET['action'])) { ?>
window.history.pushState({}, document.title, cleanURL);
<?php } ?>
});
</script>
<?php } ?>
</head>
<body>
<form action="<?php echo htmlentities($sScriptName); ?>" method="get" id="tagForm">
<?php if ($bEnableJavascript && $bShowBookmarklet) { ?><a href="#" id="bml">Bookmarklet</a><?php } ?>
<select name="tag" id="tag"<?php if ($bEnableJavascript) { ?> onchange="fChangeTag(this);"<?php } ?>><?php
echo "\n";
for ($i = 0; $i < sizeof($aTags); $i++) {
$sSelected = '';
if ($sCurrentTag == $aTags[$i]) {
$sSelected = ' selected="selected"'; // select the tag currently displaying
}
echo "\t\t\t" . '<option value="' . htmlentities($aTags[$i]) . '"' . $sSelected . '>' . htmlentities($aTags[$i]) . '</option>' . "\n";
}
echo "\t\t";
?></select>
<input type="submit" name="submit" id="submitButton" value="Go" />
<div class="clear"></div>
</form><?php
echo "\n";
$aCurrentTagBookmarks = array();
foreach ($aBookmarks as $iIndex => $aBookmark) {
$aBookmarkTags = explode(',', $aBookmark['tags']);
if (in_array($sCurrentTag, $aBookmarkTags) || ($aBookmark['tags'] == '' && $sCurrentTag == $sNoTagLabel)) {
// here we use the label and uid (to maintain uniqueness) as the key instead so it's easier to sort later
$aCurrentTagBookmarks[($aBookmark['label'] . $aBookmark['uid'])] = array(
'uid' => $iIndex,
'label' => $aBookmark['label'],
'url' => $aBookmark['url']
);
}
}
//ksort($aCurrentTagBookmarks);
uksort($aCurrentTagBookmarks, 'strnatcasecmp');
if (sizeof($aCurrentTagBookmarks) > 0) {
echo "\t" . '<ul>' . "\n";
}
foreach ($aCurrentTagBookmarks as $aCurrentTagBookmark) {
echo "\t\t" . '<li>' . "\n";
echo "\t\t\t" . '<div class="bmLink">' . "\n";
echo "\t\t\t\t" . '<a href="' . $aCurrentTagBookmark['url'] . '" target="' . $sLinkTarget . '">' . $aCurrentTagBookmark['label'] . '</a>' . "\n";
echo "\t\t\t" . '</div>' . "\n";
echo "\t\t\t" . '<div class="bmEdit">' . "\n";
echo "\t\t\t\t" . '<a href="?action=delete&uid=' . $aCurrentTagBookmark['uid'] . '&tag=' . urlencode($sCurrentTag) . '">-</a>' . "\n";
echo "\t\t\t" . '</div>' . "\n";
echo "\t\t\t" . '<div class="clear"></div>' . "\n";
echo "\t\t" . '</li>' . "\n";
}
if (sizeof($aCurrentTagBookmarks) > 0) {
echo "\t" . '</ul>' . "\n";
} else {
echo "\t" . '<p class="empty">No bookmarks in "' . htmlentities($sCurrentTag) . '".</p>' . "\n";
}
echo "\t";
?><form action="<?php echo htmlentities($sScriptName); ?>" method="get" id="addForm">
<label for="label">Label:</label> <input type="text" name="label" id="label" value="" />
<label for="url">URL:</label> <input type="text" name="url" id="url" value="http://" />
<label for="tags">Tags (csv):</label> <input type="text" name="tags" id="tags" value="" />
<div class="clear"></div>
<input type="hidden" name="action" value="add" />
<input type="hidden" name="tag" value="<?php echo htmlentities($sCurrentTag); ?>" />
<input type="submit" name="submit" id="addButton" value="Add" />
</form>
</body>
</html>