First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,37 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jQuery Server Plugin</title>
<script type="text/javascript" src='jquery.js'></script>
<script type="text/javascript" src='../jQueryServer.js'></script>
<script type="text/javascript">
jQuery.serverConfig.url = '/phpQuery/jQueryServer/jQueryServer.php';
function demo() {
$.server({
url: document.location.toString(),
dataType: 'json'
})
.find('li')
.client(function(response){
$.each(response, function(k, li){
$('ul').append(li);
});
});
}
$(function(){
$('ul').append('<li>above LIs will be downloaded and appended below in 2 seconds...</li>');
setTimeout(demo, 2000);
});
</script>
</head>
<body>
<div>jQuery Server Plugin demo...</div>
<ul>
<li>test1</li>
<li>test2</li>
<li>test3</li>
</ul>
</body>
</html>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,18 @@
<?php
/**
* Example of jQueryServer config file.
*
* To make it work just delete '.example' suffix.
* @var array
*/
$jQueryServerConfig = array();
// Ajax host authorization
// phpQuery::ajaxAllowHost('somehostname.com');
// phpQuery::ajaxAllowURL('http://fullurl.com/witheverything?foo=bar');
// Restrict access without referer
// $jQueryServerConfig['refererMustMatch'] = true;
// dot '.' means $_SERVER['HTTP_HOST']
// $jQueryServerConfig['allowedRefererHosts'] = array('.', 'my-other-host.com');
?>

View file

@ -0,0 +1,201 @@
/**
* jQuery Server Plugin
*
* Server-side Ajax requests supporting jQuery manipulations
* before sending content to the browser.
*
* Example:
* $.server({url: ${URL})
* .find('.my-class')
* .client(${CALLBACK});
*
* @version 0.5.1
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
* @link http://code.google.com/p/phpquery/wiki/jQueryServer
* @link http://code.google.com/p/phpquery/
*/
jQuery.extend({
serverConfig: function() {
if (typeof jQueryServerConfig != 'undefined')
return jQueryServerConfig;
return {};
}(),
server: function(options){
// set default url
if (! jQuery.serverConfig.url)
jQuery.serverConfig.url = jQuery('script[src$=jquery.js]')
.attr('src').replace(/jquery\.js$/, '')
+'jQueryServer.php';
// this is cache object
var objectCache = {};
// dump all jQuery methods, but only once
// $.each doesn't work ?
for( var i in jQuery.fn) {
// closure to preserve loop iterator in scope
(function(){
var name = i;
// create dummy method
objectCache[name] = function(){
// create method data object
var data = {
method: name,
arguments: []
};
// collect arguments
$.each(arguments, function(k, v){
data.arguments.push(v);
});
// push data into stack
this.stack.push(data);
// preserve chain
return this;
}
})();
}
/**
* Fetches results from phpQuery.
*
* @param {Function} callback Optional. Turns on async request.
* First parameter for callback is usually an JSON array of mathed elements. Use $(result) to append it to DOM.
* It can also be a boolean value or string, depending on last method called.
*/
objectCache.client = function(success, error){
// console.log(this.stack.toSource());
// success = success || function(){
// return $result;
// };
$.ajax({
type: 'POST',
data: {data: $.toJSON(this.stack)},
async: false,
// jQuery.server.config ???
url: jQuery.serverConfig.url,
// success: function(response){
// var $result = jQuery();
// $.each(response, function(v) {
// $result.add(v);
// })
// success.call(null, $result);
// },
// success: success,
success: function(response){
if (options['dataType'] == 'json')
response = $.parseJSON(response);
success(response);
},
error: error
})
}
// replace orginal method with generated method using cache (lazy-load)
jQuery.server = function(options){
// clone cache object
var myCache = jQuery.extend({}, objectCache);
myCache.stack = [options];
return myCache;
}
// returen result from new method (only done for first call)
return jQuery.server(options);
}
});
// toJSON by Mark Gibson
if (typeof $.toJSON == 'undefined') {
(function ($) {
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
'array': function (x) {
var a = ['['], b, f, i, l = x.length, v;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
}
a[a.length] = ']';
return a.join('');
},
'boolean': function (x) {
return String(x);
},
'null': function (x) {
return "null";
},
'number': function (x) {
return isFinite(x) ? String(x) : 'null';
},
'object': function (x) {
if (x) {
if (x instanceof Array) {
return s.array(x);
}
var a = ['{'], b, f, i, v;
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
}
a[a.length] = '}';
return a.join('');
}
return 'null';
},
'string': function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
}
};
$.toJSON = function(v) {
var f = isNaN(v) ? s[typeof v] : s['number'];
if (f) return f(v);
};
$.parseJSON = function(v, safe) {
if (JSON)
return JSON.parse(v);
if (safe === undefined)
safe = $.parseJSON.safe;
if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
return undefined;
return eval('('+v+')');
};
$.parseJSON.safe = false;
})(jQuery);
}

View file

@ -0,0 +1,107 @@
`<?php
/**
* jQuery Server Plugin
*
* Backend class using phpQuery.
*
* @version 0.5.1
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
* @link http://code.google.com/p/phpquery/wiki/jQueryServer
* @link http://code.google.com/p/phpquery/
* @todo local files support (safe...)
* @todo respond with proper HTTP code
* @todo persistant thread support (with timeout...)
* @todo 2.0: JSON RPC - Zend_Json_Server
* @todo 2.0: XML RPC ?
*/
class jQueryServer {
public $config = array(
'allowedRefererHosts' => array('.'),
'refererMustMatch' => true,
);
public $calls = null;
public $options = null;
public $allowedHosts = null;
function __construct($data) {
$pq = null;
include_once(dirname(__FILE__).'/../phpQuery/phpQuery.php');
if (file_exists(dirname(__FILE__).'/jQueryServer.config.php')) {
include_once(dirname(__FILE__).'/jQueryServer.config.php');
if ($jQueryServerConfig)
$this->config = array_merge_recursive($this->config, $jQueryServerConfig);
}
if ($this->config['refererMustMatch']) {
foreach($this->config['allowedRefererHosts'] as $i => $host)
if ($host == '.')
$this->config['allowedRefererHosts'][$i] = $_SERVER['HTTP_HOST'];
$referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$authorized = $referer
&& in_array($referer, $this->config['allowedRefererHosts']);
if (! $authorized) {
throw new Exception("Host '{$_SERVER['HTTP_REFERER']}' not authorized to make requests.");
return;
}
}
// phpQueryClass::$debug = true;
// if (! function_exists('json_decode')) {
// include_once(dirname(__FILE__).'/JSON.php');
// $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
// }
// $data = $this->jsonDecode($data);
$data = phpQuery::parseJSON($data);
// load document (required for first $data element)
if (is_array($data[0]) && isset($data[0]['url'])) {
$this->options = $data[0];
$ajax = $this->options;
$this->calls = array_slice($data, 1);
$ajax['success'] = array($this, 'success');
phpQuery::ajax($ajax);
} else {
throw new Exception("URL needed to download content");
}
}
public function success($response) {
$pq = phpQuery::newDocument($response);
foreach($this->calls as $k => $r) {
// check if method exists
if (! method_exists(get_class($pq), $r['method'])) {
throw new Exception("Method '{$r['method']}' not implemented in phpQuery, sorry...");
// execute method
} else {
$pq = call_user_func_array(
array($pq, $r['method']),
$r['arguments']
);
}
}
if (! isset($this->options['dataType']))
$this->options['dataType'] = '';
switch(strtolower($this->options['dataType'])) {
case 'json':
if ( $pq instanceof PHPQUERYOBJECT ) {
$results = array();
foreach($pq as $node)
$results[] = pq($node)->htmlOuter();
print phpQuery::toJSON($results);
} else {
print phpQuery::toJSON($pq);
}
break;
default:
print $pq;
}
// output results
}
// public function jsonEncode($data) {
// return function_exists('json_encode')
// ? json_encode($data)
// : $this->json->encode($data);
// }
// public function jsonDecode($data) {
// return function_exists('json_decode')
// ? json_decode($data, true)
// : $this->json->decode($data);
// }
}
new jQueryServer($_POST['data']);
?>