Browse Source

- Change RPC call in JSON

- Clean some old code
- Issues fixed
pull/16/head
Jaussoin Timothée 13 years ago
parent
commit
868c459209
  1. 2
      jajax.php
  2. 77
      system/RPC.php
  3. 17
      system/Widget/WidgetWrapper.php
  4. 5
      system/Widget/widgets/Poller/poller.js
  5. 10
      system/Widget/widgets/Syndication/Syndication.php
  6. 104
      system/js/movimrpc.js

2
jajax.php

@ -28,7 +28,7 @@ set_time_limit(200);
$polling = true;
$rpc = new RPC();
$rpc->handle();
$rpc->handle_json();
// Closing stuff
WidgetWrapper::destroyInstance();

77
system/RPC.php

@ -57,73 +57,56 @@ class RPC
ob_clean();
ob_start();
// Starting XML output.
header('Content-Type: text/xml');
println('<?xml version="1.0" encoding="UTF-8" ?>');
println('<movimcontainer>');
// Just in case (warning)
if(!is_array(self::$funcalls)) {
self::$funcalls = array();
self::$funcalls = array('ping');
}
foreach(self::$funcalls as $funcall) {
println('<funcall name="%s">', $funcall['func']);
if(is_array($funcall['params'])) {
foreach($funcall['params'] as $param) {
println('<param><![CDATA[%s]]></param>', $param);
}
}
println('</funcall>');
}
println('</movimcontainer>');
header('Content-Type: application/json');
printf('%s', json_encode(self::$funcalls));
}
/**
* Handles incoming requests.
*/
public function handle()
public function handle_json()
{
if(isset($_GET['do']) && $_GET['do'] == 'poll') {
moxl\ping();
} else {
$xml = file_get_contents('php://input');
$request = simplexml_load_string($xml);
$json = file_get_contents('php://input');
$request = json_decode($json);
// Loading the widget.
$widget_name = (string)$request['widget'];
$widget_name = (string)$request->widget;
// Preparing the parameters and calling the function.
$params = array();
foreach($request->children() as $child) {
if($child->getName() == 'param') {
if($child->count() > 0) { // Probably contains an array.
$arr = array();
foreach($child->children() as $data) {
if($data->getName() == 'array') {
foreach($data->children() as $elt) {
if($elt->getName() == 'arrayelt') {
if(isset($elt['name'])) {
$arr[(string)$elt['name']] = (string)$elt;
} else {
$arr[] = (string)$elt;
}
}
}
}
}
$params[] = $arr;
} else {
$params[] = (string)$child;
}
}
$params = (array)$request->params;
$result = array();
foreach($params as $p) {
if(is_object($p))
array_push($result, (array)$p->container);
else
array_push($result, $p);
}
/*if($params[0]->container)
$params = array((array)$params[0]->container);
elseif(count($params) > 1)
$params;
else
$params = array($params[0]);*/
$widgets = WidgetWrapper::getInstance(false);
$widgets->run_widget($widget_name, (string)$request['name'], $params);
}
$widgets->run_widget($widget_name, (string)$request->func, $result);
}
}
}

17
system/Widget/WidgetWrapper.php

@ -26,8 +26,6 @@ class WidgetWrapper
private $all_widgets = array();
private $loaded_widgets = array();
private $loaded_widgets_old;
//private $loaded_widgets_cached = array();
//private $cached_widgets;
private static $instance;
@ -201,21 +199,6 @@ class WidgetWrapper
return $isevent;
}
/*function iterateCached($method, array $params = NULL)
{
$buff = array();
$widgets = $this->cached_widgets;
if(!empty($widgets)) {
foreach($widgets as $widget => $val) {
$buff[] = $this->run_widget($widget, $method, $params);
}
}
return $buff;
}*/
/**
* Returns the list of loaded CSS.

5
system/Widget/widgets/Poller/poller.js

@ -25,14 +25,15 @@ function movim_poll()
{
if(poller.status == 200) {
// Handling poll return.
if(poller.responseXML == null) {
if(poller.response == null) {
if(empty_count == 3)
movim_disconnect('&err=session');
else
empty_count++;
} else {
empty_count = 0;
rpc.handle_rpc(poller.responseXML);
rpc.handle_rpc_json(poller.response);
}
} else if(poller.status == 500 || poller.status == 400) {
movim_disconnect('&err=internal');

10
system/Widget/widgets/Syndication/Syndication.php

@ -33,9 +33,7 @@ class Syndication extends WidgetBase
$xml = '
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>'.t("%s's feed",$messages[0]->getContact()->getTrueName()).'</title>
<link href="'.BASE_URI.'?q=feed&f='.$from.'"/>
<updated>'.date('c').'</updated>
<author>
<name>'.$messages[0]->getContact()->getTrueName().'</name>
@ -46,15 +44,15 @@ class Syndication extends WidgetBase
foreach($messages as $message) {
$title = $message->title;
if($title == null)
$title = substr(strip_tags(html_entity_decode($message->content)), 0, 40).'...';
$title = trim(substr(strip_tags(html_entity_decode($message->content)), 0, 40)).'...';
$xml .= '
<entry>
<title>'.prepareString(html_entity_decode($title)).'</title>
<id>urn:uuid:'.$message->nodeid.'</id>
<updated>'.date('c', strtotime($message->published)).'</updated>
<summary type="html">
<content type="html">
<![CDATA['.prepareString(html_entity_decode($message->content)).']]>
</summary>
</content>
</entry>
';
}
@ -64,6 +62,8 @@ class Syndication extends WidgetBase
} else {
echo t('No public feed for this contact');
}
//var_dump($messages);
} else {
echo t('No contact specified');
}

104
system/js/movimrpc.js

@ -108,27 +108,22 @@ function MovimRPC()
movim_xmlhttp.open('POST', 'jajax.php', true);
var handler = this.handle_rpc;
//var handler = this.handle_rpc;
var handler = this.handle_rpc_json;
movim_xmlhttp.onreadystatechange = function()
{
//if(movim_xmlhttp.readyState == 4 && movim_xmlhttp.status == 200) {
handler(movim_xmlhttp.responseXML);
//}
handler(movim_xmlhttp.response);
if(movim_xmlhttp.readyState == 4 && movim_xmlhttp.status == 500) {
var url = window.location.href;
var urlparts = url.split('/');
var txt = urlparts[0]+'//';
for(i = 2; i < urlparts.length-1; i++) {
txt = txt+urlparts[i]+'/'
}
window.location.replace(txt+'index.php?q=disconnect&err=internal');
movim_disconnect('&err=internal');
}
};
movim_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = this.generate_xml();
movim_xmlhttp.send(data);
movim_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
//var data = this.generate_xml();
var json = this.generate_json();
movim_xmlhttp.send(json);
};
/**
@ -168,77 +163,32 @@ function MovimRPC()
/**
* Handles returns (xmlrpc)
*/
this.handle_rpc = function(xml)
this.handle_rpc_json = function(json)
{
if(xml != null) {
var funcalls = xml.getElementsByTagName("funcall");
for(h = 0; h < funcalls.length; h++) {
var func = funcalls[h];
var funcname = func.attributes.getNamedItem("name").value;
var f = window[funcname];
var params = func.childNodes;
var aparams = new Array();
for(p = 0; p < params.length; p++) {
if(params[p].nodeName != "param")
continue;
aparams.push(params[p].textContent);
}
try {
f(aparams);
}
catch(err) {
log("Error caught: " + err.toString());
if(json != null) {
var funcalls = eval(json);
if(funcalls != null) {
for(h = 0; h < funcalls.length; h++) {
//console.log(funcalls);
var funcall = funcalls[h];
try {
window[funcall.func](funcall.params);
}
catch(err) {
log("Error caught: " + err.toString());
}
}
}
}
};
}
/**
* Generates the XML document corresponding to the provided parameters.
* Generates the JSON document corresponding to the provided parameters.
*/
this.generate_xml = function()
this.generate_json = function()
{
var params = "";
for(var i = 0; i < this.params.length; i++) {
params += '<param>';
// Argh! this is an array!
if(this.params[i].constructor == Array) {
var array = this.params[i]
params += "<array>\n";
for(var j = 0; j < array.length; j++) {
params += "<arrayelt><![CDATA[" + array[j] + "]]></arrayelt>\n";
}
params += "</array>\n";
}
else if(this.params[i].constructor == Hash) {
var iter = this.params[i].iterate();
iter.start();
params += "<array>\n";
while(iter.next()) {
params += '<arrayelt name="' + iter.key() + '">'
+ '<![CDATA[' + iter.val() + ']]>'
+ "</arrayelt>\n";
}
params += "</array>\n";
}
else {
params += '<![CDATA[' + this.params[i] + ']]>';
}
params +="</param>\n";
}
var request =
'<?xml version="1.0" encoding="UTF-8" ?>'
+ '<funcall widget="'+ this.widget + '" name="' + this.func + '">' + "\n"
+ params + "\n"
+ '</funcall>' + "\n";
return request;
return JSON.stringify(this);
};
/* Properties */

Loading…
Cancel
Save