Skip to content

Commit

Permalink
Moved get_bookmarks
Browse files Browse the repository at this point in the history
* moved the get_bookmarks function from init to click to speed up inital loading
  • Loading branch information
Offerel committed Jan 7, 2019
1 parent 33f333b commit 5d8639d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"role": "Developer"
}
],
"version": "2.1.0",
"version": "2.1.1",
"repositories": [
{
"type": "composer",
Expand Down
23 changes: 16 additions & 7 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Roundcube Bookmarks Plugin
*
* @version 2.1.0
* @version 2.1.1
* @author Offerel
* @copyright Copyright (c) 2019, Offerel
* @license GNU General Public License, version 3
Expand All @@ -26,7 +26,7 @@ function j_del(t, o) {

function bookmarks_cmd() {
if(document.getElementById("bookmarkpane").clientWidth != "300") {
document.getElementById("bookmarkpane").style.width = "300px";
rcmail.http_post("syncmarks/get_bookmarks", "_url=2")
}
else {
document.getElementById("bookmarkpane").style.width = "0";
Expand All @@ -46,12 +46,21 @@ function jadd_url() {
function urladded(t) {
console.log(t.message), 0 < t.data.length && $("#bookmarkpane").html(t.data)
}
window.rcmail && rcmail.addEventListener("init", function(t) {}), $(document).ready(function() {
/*
if(document.getElementById("phppane")) {
alert(rcmail.get_user_email());

function get_bookmarks(response) {
bookmarks = JSON.parse(response.data);

if(response.message == 'php') {
$('#bmframe').attr('srcdoc',bookmarks);
document.getElementById("bookmarkpane").style.width = "300px";
}
*/
else {
$('#bookmarkpane').html(bookmarks);
document.getElementById("bookmarkpane").style.width = "300px";
}
}

window.rcmail && rcmail.addEventListener("init", function(t) {}), $(document).ready(function() {
$("#7f3f3c06-5b85-4e7f-b527-d061478e9446").on("click", bookmarks_cmd), document.getElementById("bookmarkpane").addEventListener("click", function(t) {
"A" == t.target.tagName && bookmarks_cmd()
})
Expand Down
2 changes: 1 addition & 1 deletion plugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 47 additions & 40 deletions syncmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Roundcube Bookmarks Plugin
*
* @version 2.1.0
* @version 2.1.1
* @author Offerel
* @copyright Copyright (c) 2018, Offerel
* @license GNU General Public License, version 3
Expand All @@ -17,7 +17,8 @@ public function init() {
$this->add_texts('localization/', true);
$this->register_task('syncmarks');
$this->include_stylesheet($this->local_skin_path().'/plugin.min.css');
$this->include_script('plugin.min.js');
//$this->include_script('plugin.min.js');
$this->include_script('plugin.js');

$this->add_button(array(
'label' => 'syncmarks.bookmarks',
Expand All @@ -32,6 +33,45 @@ public function init() {
$this->add_hook('render_page', array($this, 'add_bookmarks'));
$this->register_action('add_url', array($this, 'add_url'));
$this->register_action('del_url', array($this, 'del_url'));
$this->register_action('get_bookmarks', array($this, 'get_bookmarks'));
}

function get_bookmarks() {
$rcmail = rcmail::get_instance();
$this->load_config();
$path = $rcmail->config->get('bookmarks_path', false);
$filename = $rcmail->config->get('bookmarks_filename', false);
$username = $rcmail->user->get_username();
$password = $rcmail->get_user_password();
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$remote_url = $path."/".$filename;
$opts = array('http'=>array(
'method'=>"GET",
'header' => "Authorization: Basic ".base64_encode("$username:$password")
)
);
$context = stream_context_create($opts);
$bms = file_get_contents($remote_url, false, $context);

if($ext === "json") {
foreach ($http_response_header as &$value) {
if (strpos($value, 'ast-Modified') != 0) {
$modified = strtotime(substr($value, 15));
break;
}
}
$bms = parseJSONMarks($bms,$modified, $this->gettext('bookmarks_new'));
}
elseif($ext === "html") {
$bmfile = str_replace("%u", $username, $path."/".$filename);
if(file_exists($bmfile)) {
$bms = file_get_contents($bmfile);
$bms = parseHTMLMarks($bms, filemtime($bmfile), $this->gettext('bookmarks_new'));
}
}

$rcmail->output->command('syncmarks/get_bookmarks', array('message' => $ext, 'data' => json_encode($bms)));

}

function del_url() {
Expand Down Expand Up @@ -198,54 +238,21 @@ function add_url() {
function add_bookmarks($args) {
$rcmail = rcmail::get_instance();
$this->load_config();
$path = $rcmail->config->get('bookmarks_path', false);
$filename = $rcmail->config->get('bookmarks_filename', false);
$username = $rcmail->user->get_username();
$password = $rcmail->get_user_password();
$ext = pathinfo($filename, PATHINFO_EXTENSION);

if($ext === "json") {
$url = $path."/".$filename;
$context = stream_context_create(array ('http' => array ('header' => 'Authorization: Basic ' . base64_encode("$username:$password"))));
$bookmarks = file_get_contents($url, false, $context);
foreach ($http_response_header as &$value) {
if (strpos($value, 'ast-Modified') != 0) {
$modified = strtotime(substr($value, 15));
break;
}
}
$bookmarks = parseJSONMarks($bookmarks,$modified, $this->gettext('bookmarks_new'));
$rcmail->output->add_footer("<div id=\"bookmarkpane\">".$bookmarks."</div>");
if($ext === "php") {
$rcmail->output->add_footer("<div id=\"bookmarkpane\"><iframe id='bmframe' srcdoc=\"\"></iframe></div>");
return $args;
}
elseif($ext === "html") {
$bmfile = str_replace("%u", $username, $path.$filename);
if(file_exists($bmfile)) {
$bookmarks = file_get_contents($bmfile);
$bookmarks = parseHTMLMarks($bookmarks, filemtime($bmfile), $this->gettext('bookmarks_new'));
$rcmail->output->add_footer("<div id=\"bookmarkpane\">".$bookmarks."</div>");
return $args;
}
else {
return false;
}
}
elseif($ext === "php") {
$remote_url = $path.$filename;
$opts = array('http'=>array(
'method'=>"GET",
'header' => "Authorization: Basic ".base64_encode("$username:$password")
)
);
$context = stream_context_create($opts);
$bms = htmlspecialchars(file_get_contents($remote_url, false, $context));
$rcmail->output->add_footer("<div id=\"bookmarkpane\"><iframe id='bmframe' srcdoc=\"$bms\"></iframe>");
else {
$rcmail->output->add_footer("<div id=\"bookmarkpane\"></div>");
return $args;
}
}

function bookmarks_cmd() {
$this->include_script('plugin.min.js');
$this->include_script('plugin.js');
}
}

Expand Down

0 comments on commit 5d8639d

Please sign in to comment.