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 ''; else echo 'Bookmark added. Enable Javascript to auto-close this window.'; 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']; } ?> <?php echo htmlentities($sPageTitle); ?>
Bookmarklet
$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" . '' . "\n"; } else { echo "\t" . '

No bookmarks in "' . htmlentities($sCurrentTag) . '".

' . "\n"; } echo "\t"; ?>