<commit_before><?php
function addhttp($url, $check_js = 1) {
  if (!($js = preg_match("/^javascript:.*$/i", $url)) && !preg_match("~^(?:f|ht)tps?://~i", $url)) {
    $url = "http://" . $url;
  }
  return ($check_js && $js ? true : $url);
}
function toutf8($str) {
  if (!$str)
    return $str;
  if (mb_detect_encoding($str, 'ascii, utf-8'))
    return $str;
  elseif ($encode = mb_detect_encoding($str, 'gbk, gb2312, gb18030, big5, big5-hkscs, iso-8859-1, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, iso-8859-10, iso-8859-11, iso-8859-13, iso-8859-14, iso-8859-15, iso-8859-16, utf-16, utf-32, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, euc-jp, euc-kr, euc-tw, hz-gb-2312, ibm866, iso-2022-cn, iso-2022-jp, iso-2022-jp-1, iso-2022-kr, koi8-r, koi8-u, shift-jis, us-ascii, viscii'))
    return mb_convert_encoding($str, 'UTF-8', $encode);
  else
    return '';
}

function escattr($match) {
  if (is_array($match))
    $match = '<'.$match[1].(isset($match[6]) ? $match[6] : '').'>';
  return preg_replace_callback('/<([^>]*\s+)?(on[^=]+|jsaction|data|data-[a-z]+|dynsrc|accesskey|tabindex|shape|srcset|alt|title)\s*=\s*(("[^"]*")|(\'[^\']*\'))?(\s+[^>]*\/?)?>/i', 'escattr', $match);
}

function auth($expire = null) {
  if (isset($expire))
    session_set_cookie_params($expire, '/', '', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 1 : 0), 1);
  session_name('_spbkmk_bookmark_');
  session_save_path(__DIR__ . '/session');
  if (session_status() !== PHP_SESSION_ACTIVE)
    session_start();

  if (session_status() === PHP_SESSION_ACTIVE && (!isset($_SESSION['robot']) || $_SESSION['robot'] !== 0)) {
    if (isset($_COOKIE['_spbkmk_bookmark_notRobot']) && $_COOKIE['_spbkmk_bookmark_notRobot'] == 1)
      $_SESSION['robot'] = 0;
    else {
      session_destroy();
      return false;
    }
  }

  if (!isset($_SESSION['auth']) || $_SESSION['auth'] !== 1)
    return false;
  else
    return true;
}

function parse_bookmark_json($bookmark_json) {
  $bookmarks = (file_exists($bookmark_json) ? json_decode(file_get_contents($bookmark_json), 1) : false);
  return $bookmarks;
}

function output_bookmarks_recursive($bookmarks, $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json, $output = array('url' => '', 'folder' => '', 'urls' =>array()), $level = '') {
  global $site_url, $sync_file_prefix;

  if (!$bookmarks)
    return false;

  foreach ($bookmarks as $bookmark_id => $entry) {
    if (isset($entry['id'])) {
      if ($entry['type'] == 'url') {
        if (!isset($entry['hash']))
          $entry['hash'] = sha1($entry['url']);
        if ($deduplicate && is_array($deduplicate) && isset($deduplicate[$entry['hash']]))
          $entry = delete_bookmark(($level ? $level : 0).'_'.$entry['id'], 0, $bookmark_json, $cache_prefix, $update_status);
        else {
          if ($deduplicate) {
            if (is_array($deduplicate))
              $deduplicate[$entry['hash']] = 1;
            else
              $deduplicate = array($entry['hash'] => 1);
          }
          if (!isset($entry['name']) || $entry['name'] == '')
            $entry['name'] = $entry['url'];
          $output['url'] .= '<!-- url '.$entry['id'].' in '.($level ? $level : '0').' -->
'.(!$allow_edit ? '<form class="save" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=add" method="post">
<input type="hidden" name="u" value="'.htmlentities($entry['url']).'">
<input type="hidden" name="t" value="sync">
<input type="hidden" name="n" value="'.($entry['name'] == $entry['url'] ? '' : htmlentities($entry['name'])).'">' : '').'
<p class="entry url'.(!$allow_edit ? ' sync' : '').'" style="display:block;"'.($allow_edit ? ' id="entry-'.$level.'_'.$entry['id'].'"' : '').' data-id="'.$level.'_'.$entry['id'].'">
<span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="move'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="move-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'" draggable="true"' : '').'></span>
<span class="border touchOver" data-id="'.$level.'_'.$entry['id'].'">
<a class="url touchOver'.($allow_edit ? ' search'.(!$check_url && isset($entry['not_found']) && $entry['not_found'] ? ' not-found' : '').(isset($entry['hide_not_found']) && $entry['hide_not_found'] ? '' : ' show-not-found') : '').'" href="'.$entry['url'].'"'.($allow_edit ? ' id="'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').' data-type="url" '.($allow_edit ? 'data-level="'.$level.'_" ' : '').'title="'.htmlentities($entry['url']).'">
'.((isset($entry['meta']['preview']) && ($r = $entry['meta']['preview'])) || ((!$check_url || !isset($entry['meta']['preview'])) && in_array(strtolower(substr(($r = (!$allow_edit && isset($entry['original_url']) && $entry['original_url'] ? $entry['original_url'] : $entry['url'])), strrpos($r, '.')+1)), array('jpg', 'jpeg', 'png', 'gif'))) ? '<img class="preview" id="preview-'.$level.'_'.$entry['id'].'" src="index.php?action=preview&id='.($allow_edit ? '' : $sync_file_prefix).$entry['id'].'&url='.urlencode($r).'" onerror="this.style.display=\'none\';" />' : '').'
<span class="touchOver" data-id="'.$level.'_'.$entry['id'].'" id="title-'.$level.'_'.$entry['id'].'">'.$entry['name'].'</span></a>
'.($allow_edit ? (isset($entry['meta']['offline']) && $entry['meta']['offline'] ? '<a class="offline" href="index.php?action=view&id='.$entry['id'].'-'.$entry['meta']['offline'].'" target="_blank">Cache</a>' : '').(addhttp($entry['url']) !== true ? '<a class="qr" href="javascript:var qr=document.getElementById(\'qrcode\');qr.innerHTML=\'\';new QRCode(qr,{text:\''.str_replace('\'', '\\\'', $entry['url']).'\',width:100,height:100});"><img src="lib/mobile.png" alt="QR Code" title="QR Code" /></a>' : '').'<a class="edit" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Edit</a>' : '<a class="delete noedit" onclick="return confirm(\'Permanently delete this bookmark?\');" href="index.php?mode=sync&action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>').'
'.(!$allow_edit ? '<select name="d" onchange="if(this.selectedIndex)this.form.submit();"><option value="-1" selected disabled style="display:none;">Save</option>##FOLDERLIST##</select>' : '').'
</span>
</span>
</p>
'.(!$allow_edit ? '</form>' : '').'
'.($allow_edit ? '<form class="editform" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=edit&id='.$level.'_'.$entry['id'].'" method="post">
<input name="n" type="text" required value="'.htmlentities($entry['name']).'"><br/>
<input name="u" type="text" required value="'.htmlentities($entry['url']).'"><br/>
<input type="hidden" name="ou" value="'.htmlentities($entry['url']).'">
<select name="l">##FOLDERLIST-'.($level ? $level : '_0').'##</select>
<input type="hidden" name="h" value="0">
<span class="ignore"><label><input class="cache" type="checkbox" name="h" value="1"'.(isset($entry['hide_not_found']) && $entry['hide_not_found'] ? ' checked' : '').'>Mark page as accessible</label></span><br/>
<p id="last-access-'.($level.'_'.$entry['id']).'" class="last-access">'.'<span id="last-access-message-'.($level.'_'.$entry['id']).'" data-content="Page Not Available.">'.(($n = (!$check_url && isset($entry['meta']['not_found']) && $entry['meta']['not_found'])) ? 'Page Not Available.' : '').'</span><span class="last-access-code" id="last-access-code-'.($level.'_'.$entry['id']).'"></span>'.(addhttp($entry['url']) !== true ? '<a href="index.php?action=checkurl&id='.$level.'_'.$entry['id'].'&url='.urlencode($entry['url']).'" class="url-checker">Check Page</a>' : '').'<span class="last-access-time" id="last-access-time-'.($level.'_'.$entry['id']).'">'.($n && isset($entry['meta']['last_access']) && $entry['meta']['last_access'] ? date('Y-m-d, G:i', $entry['meta']['last_access']) : '').'</span>'.'</p>'.'
<input type="submit" value="Update">
<a class="cancel" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Cancel</a>
<a class="delete" onclick="return confirm(\'Permanently delete this bookmark?\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>
'.(($s = '<a class="save" id="save-'.$level.'_'.$entry['id'].'" href="index.php?action=save&level='.$level.'&id='.$entry['id'].'&url='.urlencode($entry['url'])) && isset($entry['meta']['offline']) && $entry['meta']['offline'] ? $s.'&delete='.$entry['meta']['offline'].'">Delete Cache</a>' : (!isset($entry['meta']['downloadable']) || $entry['meta']['downloadable'] ? $s.'">Enable Cache</a>' : '')).'
<br/>
</form>' : '')."\n";
          if ($check_url && addhttp($entry['url']) !== true)
            $output['urls'][(isset($entry['meta']['not_found']) ? $entry['meta']['not_found'] : 0)]['_'.$entry['id']] = array('meta' => array_merge(array('not_found' => 0, 'http_code' => 200, 'last_access' => 0, 'offline' => '', 'downloadable' => 1, 'preview' => 0), (isset($entry['meta']) ? $entry['meta'] : array())), 'id' => $level.'_'.$entry['id'], 'url' => $entry['url'], 'index' => '_'.$entry['id']);
        }
      } elseif ($entry['type'] == 'folder') {
        $output['url'] .= '<!-- folder '.$entry['id'].' in '.($level ? $level : '0').' -->
<div class="folder"'.($allow_edit ? ' id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="entry'.(!$allow_edit ? ' sync' : '').'"'.($allow_edit ? ' id="entry-'.$level.'_'.$entry['id'].'"' : '').' style="display:block;" data-id="'.$level.'_'.$entry['id'].'">
<h3 class="folder_title touchOver" style="display:block;" data-id="'.$level.'_'.$entry['id'].'">
<span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="folder_title_name touchOver"'.($allow_edit ? ' onclick="document.getElementById(\'search\').value=\'\';searchStrFunction();location.href=\'index.php#'.$level.'_'.$entry['id'].'\';" id="title-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').' data-type="folder">&raquo;&nbsp;'.$entry['name'].'</span>
<span class="move"'.($allow_edit ? ' id="move-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'" draggable="true" onclick="toggleShow(\'folder-wrap-'.$level.'_'.$entry['id'].'\');"' : '').'></span>
'.($allow_edit ? '<a class="edit" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Edit</a>
'.(isset($entry['cache']) && $entry['cache'] ? '<span class="cache">Cache enabled</span>' : '').'
<a class="bookmarklet" href="javascript:var url=\''.$site_url.'\';var x=document.createElement(\'SCRIPT\');x.type=\'text/javascript\';x.src=url+\'bookmarklet.php?d='.$level.'_'.$entry['id'].'\';document.getElementsByTagName(\'head\')[0].appendChild(x);void(0);" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Add to '.htmlentities($entry['name']).'</a>
<a class="bookmarklet" href="javascript:if(document.getElementById(\'spbkmk\')){document.getElementById(\'spbkmk\').parentNode.removeChild(document.getElementById(\'spbkmk\'));}var bml=document.createElement(\'div\');bml.id=\'spbkmk\';bml.style.setProperty(\'position\',\'fixed\',\'important\');bml.style.setProperty(\'z-index\',2147483640,\'important\');bml.style.setProperty(\'top\',0,\'important\');bml.style.setProperty(\'left\',0,\'important\');bml.style.setProperty(\'right\',0,\'important\');bml.style.setProperty(\'text-align\',\'left\',\'important\');bml.style.setProperty(\'background-color\',\'#fff\',\'important\');bml.style.setProperty(\'min-height\',\'28px\',\'important\');bml.style.setProperty(\'max-height\',\'56px\',\'important\');bml.style.setProperty(\'overflow\',\'hidden\',\'important\');bml.style.setProperty(\'border-bottom-width\',\'1px\',\'important\');bml.style.setProperty(\'border-bottom-style\',\'solid\',\'important\');bml.style.setProperty(\'border-bottom-color\',\'#666\',\'important\');document.body.appendChild(bml);var script=document.createElement(\'script\');script.src=\''.$site_url.'bookmarkbar.php?folder='.$level.'_'.$entry['id'].'\';bml.appendChild(script);" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Open '.htmlentities($entry['name']).' as bookmarkbar</a>
<a class="bookmarklet" href="javascript:window.open(\''.$site_url.'#'.$level.'_'.$entry['id'].'\', \'spbkmk\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800\');" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Open '.htmlentities($entry['name']).' in new window</a>' : '').'
</span>
</h3>
</span>
'.($allow_edit ? '<form class="editform editfolder" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=edit&id='.$level.'_'.$entry['id'].'" method="post">
<input name="n" type="text" required value="'.htmlentities($entry['name']).'"><br/>
<select name="l">##FOLDERLIST-'.($level ? $level : '_0').'##</select><br>
<input type="hidden" name="c" value="0">
<p><label><input class="cache" type="checkbox" name="c" value="1"'.(isset($entry['cache']) && $entry['cache'] ? ' checked' : '').'>Cache new bookmarks</label></p>
<input type="submit" value="Update">
<a class="cancel" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Cancel</a>
<a class="delete" onclick="return confirm(\'Permanently delete this folder? Note: bookmarks in this folder will NOT be deleted.\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>
<a class="delete" onclick="return confirm(\'Permanently delete this folder? Note: all items in this folder WILL be deleted.\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'&items=1">Delete all</a>
<p class="sort">Sort folder by:
<a class="sort-name" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=name">Name</a>,
<a class="sort-date" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=date_added">Date</a> or
<a class="sort-reverse" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=0">Reverse order</a>
</p>
</form>' : '').'
<span'.($allow_edit ? ' class="folder-wrap" id="folder-wrap-'.$level.'_'.$entry['id'].'"' : '').' style="display:block;">'."\n";
        if ($allow_edit)
          $output['folder'] .= '<option value="'.$level.'_'.$entry['id'].'">'.$entry['name'].'</option>'."\n";
        if (isset($entry['entries']) && !empty($entry['entries']))
          $output = output_bookmarks_recursive($entry['entries'], $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json, $output, $level.'_'.$entry['id']);
        $output['url'] .= '</span><span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'_0" data-id="'.$level.'_'.$entry['id'].'_0"' : '').'>&nbsp;</span></div>'."\n";
      }
    }
  }
  return $output;
}

function output_bookmarks($bookmarks, $bookmark_json, $allow_edit = 1, $deduplicate = 1, $check_url = 0, $cache_prefix = '', $update_status = 1) {
  global $cache_file_urllist;

  $output = output_bookmarks_recursive($bookmarks, $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json);

  $output['folder'] = '<option value="_0">My Bookmarks</option>'."\n".$output['folder'];
  $output['url'] = (isset($output['url']) && $output['url'] ? preg_replace_callback('/##FOLDERLIST-([_0-9]+)##/i', function ($match) use ($output) {
    return str_replace('value="'.$match[1].'"', 'value="'.$match[1].'" selected', $output['folder']);
  }, $output['url']) : '');

  if ($check_url)
    file_put_contents($cache_file_urllist, json_encode($output['urls']));

  return $output;
}

function add_bookmark($url, $folder, $type, $bookmark_json, $name = null, $redirect = 0, $update_status = 1, $reverse_order = 0) {
  global $site_url;

  if ($update_status)
    $bookmark = edit_bookmark_status($bookmark_json);

  if (($bookmark = (!isset($bookmark) || !$bookmark ? parse_bookmark_json($bookmark_json) : $bookmark)))
    $id = (++$bookmark[1]);
  else
    $id = 1;
  if ($type == 'url') {
    //$str = @file_get_contents($url);
    $response = get_url_response($url);
    $header = $response['header'];
    $body = $response['body'];

    if (!isset($name) || !$name) {
      if ($header['http_code'] == 200 && strlen($body)) {
        preg_match('/<title(?:\s+[^>]*)?>((?:.(?!<\/title>))*.)<\/title>/si', $body, $title);<commit_msg>Fix bug: webpage title parse<commit_after><?php
function addhttp($url, $check_js = 1) {
  if (!($js = preg_match("/^javascript:.*$/i", $url)) && !preg_match("~^(?:f|ht)tps?://~i", $url)) {
    $url = "http://" . $url;
  }
  return ($check_js && $js ? true : $url);
}
function toutf8($str) {
  if (!$str)
    return $str;
  if (mb_detect_encoding($str, 'ascii, utf-8'))
    return $str;
  elseif ($encode = mb_detect_encoding($str, 'gbk, gb2312, gb18030, big5, big5-hkscs, iso-8859-1, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, iso-8859-10, iso-8859-11, iso-8859-13, iso-8859-14, iso-8859-15, iso-8859-16, utf-16, utf-32, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, euc-jp, euc-kr, euc-tw, hz-gb-2312, ibm866, iso-2022-cn, iso-2022-jp, iso-2022-jp-1, iso-2022-kr, koi8-r, koi8-u, shift-jis, us-ascii, viscii'))
    return mb_convert_encoding($str, 'UTF-8', $encode);
  else
    return '';
}

function escattr($match) {
  if (is_array($match))
    $match = '<'.$match[1].(isset($match[6]) ? $match[6] : '').'>';
  return preg_replace_callback('/<([^>]*\s+)?(on[^=]+|jsaction|data|data-[a-z]+|dynsrc|accesskey|tabindex|shape|srcset|alt|title)\s*=\s*(("[^"]*")|(\'[^\']*\'))?(\s+[^>]*\/?)?>/i', 'escattr', $match);
}

function auth($expire = null) {
  if (isset($expire))
    session_set_cookie_params($expire, '/', '', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 1 : 0), 1);
  session_name('_spbkmk_bookmark_');
  session_save_path(__DIR__ . '/session');
  if (session_status() !== PHP_SESSION_ACTIVE)
    session_start();

  if (session_status() === PHP_SESSION_ACTIVE && (!isset($_SESSION['robot']) || $_SESSION['robot'] !== 0)) {
    if (isset($_COOKIE['_spbkmk_bookmark_notRobot']) && $_COOKIE['_spbkmk_bookmark_notRobot'] == 1)
      $_SESSION['robot'] = 0;
    else {
      session_destroy();
      return false;
    }
  }

  if (!isset($_SESSION['auth']) || $_SESSION['auth'] !== 1)
    return false;
  else
    return true;
}

function parse_bookmark_json($bookmark_json) {
  $bookmarks = (file_exists($bookmark_json) ? json_decode(file_get_contents($bookmark_json), 1) : false);
  return $bookmarks;
}

function output_bookmarks_recursive($bookmarks, $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json, $output = array('url' => '', 'folder' => '', 'urls' =>array()), $level = '') {
  global $site_url, $sync_file_prefix;

  if (!$bookmarks)
    return false;

  foreach ($bookmarks as $bookmark_id => $entry) {
    if (isset($entry['id'])) {
      if ($entry['type'] == 'url') {
        if (!isset($entry['hash']))
          $entry['hash'] = sha1($entry['url']);
        if ($deduplicate && is_array($deduplicate) && isset($deduplicate[$entry['hash']]))
          $entry = delete_bookmark(($level ? $level : 0).'_'.$entry['id'], 0, $bookmark_json, $cache_prefix, $update_status);
        else {
          if ($deduplicate) {
            if (is_array($deduplicate))
              $deduplicate[$entry['hash']] = 1;
            else
              $deduplicate = array($entry['hash'] => 1);
          }
          if (!isset($entry['name']) || $entry['name'] == '')
            $entry['name'] = $entry['url'];
          $output['url'] .= '<!-- url '.$entry['id'].' in '.($level ? $level : '0').' -->
'.(!$allow_edit ? '<form class="save" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=add" method="post">
<input type="hidden" name="u" value="'.htmlentities($entry['url']).'">
<input type="hidden" name="t" value="sync">
<input type="hidden" name="n" value="'.($entry['name'] == $entry['url'] ? '' : htmlentities($entry['name'])).'">' : '').'
<p class="entry url'.(!$allow_edit ? ' sync' : '').'" style="display:block;"'.($allow_edit ? ' id="entry-'.$level.'_'.$entry['id'].'"' : '').' data-id="'.$level.'_'.$entry['id'].'">
<span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="move'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="move-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'" draggable="true"' : '').'></span>
<span class="border touchOver" data-id="'.$level.'_'.$entry['id'].'">
<a class="url touchOver'.($allow_edit ? ' search'.(!$check_url && isset($entry['not_found']) && $entry['not_found'] ? ' not-found' : '').(isset($entry['hide_not_found']) && $entry['hide_not_found'] ? '' : ' show-not-found') : '').'" href="'.$entry['url'].'"'.($allow_edit ? ' id="'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').' data-type="url" '.($allow_edit ? 'data-level="'.$level.'_" ' : '').'title="'.htmlentities($entry['url']).'">
'.((isset($entry['meta']['preview']) && ($r = $entry['meta']['preview'])) || ((!$check_url || !isset($entry['meta']['preview'])) && in_array(strtolower(substr(($r = (!$allow_edit && isset($entry['original_url']) && $entry['original_url'] ? $entry['original_url'] : $entry['url'])), strrpos($r, '.')+1)), array('jpg', 'jpeg', 'png', 'gif'))) ? '<img class="preview" id="preview-'.$level.'_'.$entry['id'].'" src="index.php?action=preview&id='.($allow_edit ? '' : $sync_file_prefix).$entry['id'].'&url='.urlencode($r).'" onerror="this.style.display=\'none\';" />' : '').'
<span class="touchOver" data-id="'.$level.'_'.$entry['id'].'" id="title-'.$level.'_'.$entry['id'].'">'.$entry['name'].'</span></a>
'.($allow_edit ? (isset($entry['meta']['offline']) && $entry['meta']['offline'] ? '<a class="offline" href="index.php?action=view&id='.$entry['id'].'-'.$entry['meta']['offline'].'" target="_blank">Cache</a>' : '').(addhttp($entry['url']) !== true ? '<a class="qr" href="javascript:var qr=document.getElementById(\'qrcode\');qr.innerHTML=\'\';new QRCode(qr,{text:\''.str_replace('\'', '\\\'', $entry['url']).'\',width:100,height:100});"><img src="lib/mobile.png" alt="QR Code" title="QR Code" /></a>' : '').'<a class="edit" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Edit</a>' : '<a class="delete noedit" onclick="return confirm(\'Permanently delete this bookmark?\');" href="index.php?mode=sync&action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>').'
'.(!$allow_edit ? '<select name="d" onchange="if(this.selectedIndex)this.form.submit();"><option value="-1" selected disabled style="display:none;">Save</option>##FOLDERLIST##</select>' : '').'
</span>
</span>
</p>
'.(!$allow_edit ? '</form>' : '').'
'.($allow_edit ? '<form class="editform" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=edit&id='.$level.'_'.$entry['id'].'" method="post">
<input name="n" type="text" required value="'.htmlentities($entry['name']).'"><br/>
<input name="u" type="text" required value="'.htmlentities($entry['url']).'"><br/>
<input type="hidden" name="ou" value="'.htmlentities($entry['url']).'">
<select name="l">##FOLDERLIST-'.($level ? $level : '_0').'##</select>
<input type="hidden" name="h" value="0">
<span class="ignore"><label><input class="cache" type="checkbox" name="h" value="1"'.(isset($entry['hide_not_found']) && $entry['hide_not_found'] ? ' checked' : '').'>Mark page as accessible</label></span><br/>
<p id="last-access-'.($level.'_'.$entry['id']).'" class="last-access">'.'<span id="last-access-message-'.($level.'_'.$entry['id']).'" data-content="Page Not Available.">'.(($n = (!$check_url && isset($entry['meta']['not_found']) && $entry['meta']['not_found'])) ? 'Page Not Available.' : '').'</span><span class="last-access-code" id="last-access-code-'.($level.'_'.$entry['id']).'"></span>'.(addhttp($entry['url']) !== true ? '<a href="index.php?action=checkurl&id='.$level.'_'.$entry['id'].'&url='.urlencode($entry['url']).'" class="url-checker">Check Page</a>' : '').'<span class="last-access-time" id="last-access-time-'.($level.'_'.$entry['id']).'">'.($n && isset($entry['meta']['last_access']) && $entry['meta']['last_access'] ? date('Y-m-d, G:i', $entry['meta']['last_access']) : '').'</span>'.'</p>'.'
<input type="submit" value="Update">
<a class="cancel" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Cancel</a>
<a class="delete" onclick="return confirm(\'Permanently delete this bookmark?\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>
'.(($s = '<a class="save" id="save-'.$level.'_'.$entry['id'].'" href="index.php?action=save&level='.$level.'&id='.$entry['id'].'&url='.urlencode($entry['url'])) && isset($entry['meta']['offline']) && $entry['meta']['offline'] ? $s.'&delete='.$entry['meta']['offline'].'">Delete Cache</a>' : (!isset($entry['meta']['downloadable']) || $entry['meta']['downloadable'] ? $s.'">Enable Cache</a>' : '')).'
<br/>
</form>' : '')."\n";
          if ($check_url && addhttp($entry['url']) !== true)
            $output['urls'][(isset($entry['meta']['not_found']) ? $entry['meta']['not_found'] : 0)]['_'.$entry['id']] = array('meta' => array_merge(array('not_found' => 0, 'http_code' => 200, 'last_access' => 0, 'offline' => '', 'downloadable' => 1, 'preview' => 0), (isset($entry['meta']) ? $entry['meta'] : array())), 'id' => $level.'_'.$entry['id'], 'url' => $entry['url'], 'index' => '_'.$entry['id']);
        }
      } elseif ($entry['type'] == 'folder') {
        $output['url'] .= '<!-- folder '.$entry['id'].' in '.($level ? $level : '0').' -->
<div class="folder"'.($allow_edit ? ' id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="entry'.(!$allow_edit ? ' sync' : '').'"'.($allow_edit ? ' id="entry-'.$level.'_'.$entry['id'].'"' : '').' style="display:block;" data-id="'.$level.'_'.$entry['id'].'">
<h3 class="folder_title touchOver" style="display:block;" data-id="'.$level.'_'.$entry['id'].'">
<span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').'>
<span class="folder_title_name touchOver"'.($allow_edit ? ' onclick="document.getElementById(\'search\').value=\'\';searchStrFunction();location.href=\'index.php#'.$level.'_'.$entry['id'].'\';" id="title-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'"' : '').' data-type="folder">&raquo;&nbsp;'.$entry['name'].'</span>
<span class="move"'.($allow_edit ? ' id="move-'.$level.'_'.$entry['id'].'" data-id="'.$level.'_'.$entry['id'].'" draggable="true" onclick="toggleShow(\'folder-wrap-'.$level.'_'.$entry['id'].'\');"' : '').'></span>
'.($allow_edit ? '<a class="edit" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Edit</a>
'.(isset($entry['cache']) && $entry['cache'] ? '<span class="cache">Cache enabled</span>' : '').'
<a class="bookmarklet" href="javascript:var url=\''.$site_url.'\';var x=document.createElement(\'SCRIPT\');x.type=\'text/javascript\';x.src=url+\'bookmarklet.php?d='.$level.'_'.$entry['id'].'\';document.getElementsByTagName(\'head\')[0].appendChild(x);void(0);" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Add to '.htmlentities($entry['name']).'</a>
<a class="bookmarklet" href="javascript:if(document.getElementById(\'spbkmk\')){document.getElementById(\'spbkmk\').parentNode.removeChild(document.getElementById(\'spbkmk\'));}var bml=document.createElement(\'div\');bml.id=\'spbkmk\';bml.style.setProperty(\'position\',\'fixed\',\'important\');bml.style.setProperty(\'z-index\',2147483640,\'important\');bml.style.setProperty(\'top\',0,\'important\');bml.style.setProperty(\'left\',0,\'important\');bml.style.setProperty(\'right\',0,\'important\');bml.style.setProperty(\'text-align\',\'left\',\'important\');bml.style.setProperty(\'background-color\',\'#fff\',\'important\');bml.style.setProperty(\'min-height\',\'28px\',\'important\');bml.style.setProperty(\'max-height\',\'56px\',\'important\');bml.style.setProperty(\'overflow\',\'hidden\',\'important\');bml.style.setProperty(\'border-bottom-width\',\'1px\',\'important\');bml.style.setProperty(\'border-bottom-style\',\'solid\',\'important\');bml.style.setProperty(\'border-bottom-color\',\'#666\',\'important\');document.body.appendChild(bml);var script=document.createElement(\'script\');script.src=\''.$site_url.'bookmarkbar.php?folder='.$level.'_'.$entry['id'].'\';bml.appendChild(script);" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Open '.htmlentities($entry['name']).' as bookmarkbar</a>
<a class="bookmarklet" href="javascript:window.open(\''.$site_url.'#'.$level.'_'.$entry['id'].'\', \'spbkmk\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800\');" onclick="if(event.preventDefault){event.preventDefault();}if(event.stopPropagation){event.stopPropagation();}return false;" title="Drag to add bookmarklet">Open '.htmlentities($entry['name']).' in new window</a>' : '').'
</span>
</h3>
</span>
'.($allow_edit ? '<form class="editform editfolder" id="editform-'.$level.'_'.$entry['id'].'" action="index.php?action=edit&id='.$level.'_'.$entry['id'].'" method="post">
<input name="n" type="text" required value="'.htmlentities($entry['name']).'"><br/>
<select name="l">##FOLDERLIST-'.($level ? $level : '_0').'##</select><br>
<input type="hidden" name="c" value="0">
<p><label><input class="cache" type="checkbox" name="c" value="1"'.(isset($entry['cache']) && $entry['cache'] ? ' checked' : '').'>Cache new bookmarks</label></p>
<input type="submit" value="Update">
<a class="cancel" href="javascript:;" onclick="toggleShow(\'entry-'.$level.'_'.$entry['id'].'\');toggleShow(\'editform-'.$level.'_'.$entry['id'].'\')">Cancel</a>
<a class="delete" onclick="return confirm(\'Permanently delete this folder? Note: bookmarks in this folder will NOT be deleted.\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'">Delete</a>
<a class="delete" onclick="return confirm(\'Permanently delete this folder? Note: all items in this folder WILL be deleted.\');" href="index.php?action=delete&id='.$level.'_'.$entry['id'].'&items=1">Delete all</a>
<p class="sort">Sort folder by:
<a class="sort-name" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=name">Name</a>,
<a class="sort-date" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=date_added">Date</a> or
<a class="sort-reverse" href="index.php?action=sort&id='.$level.'_'.$entry['id'].'&sort=0">Reverse order</a>
</p>
</form>' : '').'
<span'.($allow_edit ? ' class="folder-wrap" id="folder-wrap-'.$level.'_'.$entry['id'].'"' : '').' style="display:block;">'."\n";
        if ($allow_edit)
          $output['folder'] .= '<option value="'.$level.'_'.$entry['id'].'">'.$entry['name'].'</option>'."\n";
        if (isset($entry['entries']) && !empty($entry['entries']))
          $output = output_bookmarks_recursive($entry['entries'], $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json, $output, $level.'_'.$entry['id']);
        $output['url'] .= '</span><span class="target touchOver'.(!$allow_edit ? ' noedit' : '').'"'.($allow_edit ? ' id="target-'.$level.'_'.$entry['id'].'_0" data-id="'.$level.'_'.$entry['id'].'_0"' : '').'>&nbsp;</span></div>'."\n";
      }
    }
  }
  return $output;
}

function output_bookmarks($bookmarks, $bookmark_json, $allow_edit = 1, $deduplicate = 1, $check_url = 0, $cache_prefix = '', $update_status = 1) {
  global $cache_file_urllist;

  $output = output_bookmarks_recursive($bookmarks, $allow_edit, $deduplicate, $check_url, $cache_prefix, $update_status, $bookmark_json);

  $output['folder'] = '<option value="_0">My Bookmarks</option>'."\n".$output['folder'];
  $output['url'] = (isset($output['url']) && $output['url'] ? preg_replace_callback('/##FOLDERLIST-([_0-9]+)##/i', function ($match) use ($output) {
    return str_replace('value="'.$match[1].'"', 'value="'.$match[1].'" selected', $output['folder']);
  }, $output['url']) : '');

  if ($check_url)
    file_put_contents($cache_file_urllist, json_encode($output['urls']));

  return $output;
}

function add_bookmark($url, $folder, $type, $bookmark_json, $name = null, $redirect = 0, $update_status = 1, $reverse_order = 0) {
  global $site_url;

  if ($update_status)
    $bookmark = edit_bookmark_status($bookmark_json);

  if (($bookmark = (!isset($bookmark) || !$bookmark ? parse_bookmark_json($bookmark_json) : $bookmark)))
    $id = (++$bookmark[1]);
  else
    $id = 1;
  if ($type == 'url') {
    //$str = @file_get_contents($url);
    $response = get_url_response($url);
    $header = $response['header'];
    $body = $response['body'];

    if (!isset($name) || !$name) {
      if ($header['http_code'] == 200 && strlen($body)) {
        preg_match('/<title(?:\s+[^>]*)?>((?:(?!<\/title>).)*.)<\/title>/si', $body, $title);
{'fraction_non_alphanumeric': 0.2993290950106935, 'fraction_numerical': 0.015234523774646238, 'mean_word_length': 5.863864870299618, 'pattern_counts': {'":': 0, '<': 267, '<?xml version=': 0, '>': 303, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2168670', 'n_tokens_mistral': 12452, 'n_tokens_neox': 12307, 'n_words': 1611}