1
2
3/**
4
5* LibAjax
6
7* An open source, LGPL AJAX/XMLHTTPRequest (Asynchronous JavaScript and XML) class for PHP.
8
9*
<br/>
1\------
<br/>
1* This library is free software; you can redistribute it and/or
<br/>
1* (modify it under the terms of the GNU Lesser General Public
<br/>
1* License as published by the Free Software Foundation; either
<br/>
1* version 2.1 of the License, or (at your option) any later version.
<br/>
1* This library is distributed in the hope that it will be useful,
<br/>
1* but WITHOUT ANY WARRANTY; without even the implied warranty of
<br/>
1* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
<br/>
1* Lesser General Public License for more details.
<br/>
1* You should have received a copy of the GNU Lesser General Public
<br/>
1* License along with this library; if not, write to the Free Software
<br/>
1* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
<br/>
1*
<br/>
1\------
<br/>
1* @copyright 2005 htpt://ominousminds.com
2
3* @version 1.0.0a
4
5* @author Justin Shreve
@link http://ominousminds Ominous Minds
@package LibAjax
*/
/**
AJAX
The class to wrap all the lib's functions together.
*/
class ajax {
/**
Export
Tells which functions to export and allow use of in JavaScript.
*/
var $export = array();
/**
URL
Act's as a wrapper for $_SERVER['REQUEST_URI'].
@see ajax::ajax()
*/
var $url = "" ;
/**
Mode
Tells which mode we are using to send data. <br/>
GET Should be used to view data, and POST should be used to send long strings of data <br/>
or to edit or change data.
@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html RFC Standard
*/
var $mode ;
/**
Function
Tells which function to send to the javascript functions from PHP.
*/
var $func ;
/**
Arguments
The list of the arguments to send to the javascript functions, from PHP.
*/
var $args = array();
/**
Javascript.
The string that holds all the javascript to print out.
@see ajax:<img alt="" border="0" src="images/smilies/redface.gif"/>utput()
*/
var $return_javascript ;
/**
AJAX
Sets the URL value to take on $_SERVER['REQUEST_URI'];
@see ajax:: $url
*/
function ajax ()
{
$this -> url = $_SERVER [ 'REQUEST_URI' ];
}
/**
Client Request
Sets the variables such as func and args from the get or post methods. <br/>
and then calls back to set the values.
@see ajax:: $func
@see ajax:: $args
*/
function client_request ()
{
// Are we using POST or GET methods?
if(!empty( $_GET [ 'libajax_function' ]))
{
$this -> func = $_GET [ 'libajax_function' ];
if(!empty( $_GET [ 'libajax_args' ]))
{
$this -> args = $_GET [ 'libajax_args' ];
}
}
if(!empty( $_POST [ 'libajax_function' ]))
{
$this -> func = $_POST [ 'libajax_function' ];
if(!empty( $_POST [ 'libajax_args' ]))
{
$this -> args = $_POST [ 'libajax_args' ];
}
}
@ call_user_func_array ( $this -> func , $this -> args );
}
/**
JAVASCRIPT
Loads all the javascript side of things into a string for later. <br/>
I used a ob and heredoc work around due to the sheer ammount of data that had to be passed.
@return string The libajax_init and libajax_call functions to act as a wrapper for XMLHttpRequest.
*/
function javascript () {
ob_start ();
print <<<libajax "&";="" ");="" "?";="" "libajax_function=" + escape(name);
for ( var i = 0; i < args.length-1; i++) { url = url + " $this="" &libajax_args="" (ajax.readystate="4)" (mode="GET" (ndata);="" (url.indexof("?")="-1)" (window.activexobject)="" (window.xmlhttprequest)="" )="" +="" -="" ;="" [="" []=" + escape(args [ i ] ); }
}
ajax.open(mode, url, true);
if(mode == " \n="" ]="" activexobject("microsoft.xmlhttp")="" ajax="new" ajax.onreadystatechange="function()" ajax.send(data);="" ajax.setrequestheader("content-type","application="" args="" args)="" args.length-1="" else="" function="" http:="" if="" justin="" libajax.sf.net="" libajax;="" libajax_call(name,="" libajax_function=" + escape(name);
for ( var i = 0; i < args.length-1; i++) { data = data + " libajax_init()="" mode=" { $this -> mode } " ndata="ajax.responseText.substring(0," ominousminds.com="" post")="" shreve="" url="url" var="" work='ajax.responseText.indexOf("' work);="" x-www-form-urlencoded");="" xmlhttprequest();="" {="" }=""> return_javascript = ob_get_contents ();
ob_end_clean ();
}
/**
Create Function
Creates the wrapper function to call to libajax_call and to send the arguments and function name. <br/>
The ob and heredoc goes for here as well, though it's not as much data.
@param $name string The name of the function to emulate.
@param $arguments mixed The arguments to emulate with the function name.
*/
function create_jsfunction ( $name , $arguments )
{
ob_start ();
print <<<libajax ",="" $html="ob_get_contents" $name="" $this="" ()="" ();="" -="" .arguments);="" ajax_="" function="" libajax;="" libajax_call("="" ob_end_clean="" {="" }=""> return_javascript = $this -> return_javascript . $html ;
}
/**
Output
This function should be called in a javascript tag, or a file acting as a js file using the php header function, <br/>
as it will call to create_jsfunction, and the javascript function, to generate a string to return, which holds every <br/>
bit of the javascript side of things.
@return string All the javascript to be used by the script.
*/
function output ()
{
$this -> javascript ();
foreach ( $this -> export as $function ) {
$final .= $this -> create_jsfunction ( $function , $this -> args );
}
$this -> return_javascript = $this -> return_javascript . $final ;
print $this -> return_javascript ;
}
}
if (!isset( $SAJAX_INCLUDED)) {
/*
- GLOBALS AND DEFAULTS
*/
$sajax_debug_mode = 0;
$sajax_export_list = array();
$sajax_request_type = "GET";
$sajax_remote_uri = "";
/*
- CODE
*/
function sajax_init() {
}
function sajax_get_my_uri() {
global $REQUEST_URI;
return $REQUEST_URI;
}
$sajax_remote_uri = sajax_get_my_uri();
function sajax_handle_client_request() {
global $sajax_export_list;
$mode = "";
if (! empty( $_GET["rs"]))
$mode = "get";
if (!empty( $_POST["rs"]))
$mode = "post";
if (empty( $mode))
return;
if ( $mode == "get") {
// Bust cache in the head
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
$func_name = $_GET["rs"];
if (! empty( $_GET["rsargs"]))
$args = $_GET["rsargs"];
else
$args = array();
}
else {
$func_name = $_POST["rs"];
if (! empty( $_POST["rsargs"]))
$args = $_POST["rsargs"];
else
$args = array();
}
if (! in_array( $func_name, $sajax_export_list))
echo "-: $func_name not callable";
else {
echo "+:";
$result = call_user_func_array( $func_name, $args);
echo $result;
}
exit;
}
function sajax_get_common_js() {
global $sajax_debug_mode;
global $sajax_request_type;
global $sajax_remote_uri;
$t = strtoupper( $sajax_request_type);
if ( $t != "GET" && $t != "POST")
return "// Invalid type: $t.. \n\n";
ob_start();
1
2// remote scripting library
3// (c) copyright 2005 modernmethod, inc
4var sajax_debug_mode = ```
5 echo $sajax_debug_mode ? "true" : "false";
6```;
7var sajax_request_type = "```
8 echo $t;
9```";
10
11function sajax_debug(text) {
12if (sajax_debug_mode)
13alert("RSD: " + text)
14}
15function sajax_init_object() {
16sajax_debug("sajax_init_object() called..")
17
18var A;
19try {
20A=new ActiveXObject("Msxml2.XMLHTTP");
21} catch (e) {
22try {
23A=new ActiveXObject("Microsoft.XMLHTTP");
24} catch (oc) {
25A=null;
26}
27}
28if(!A && typeof XMLHttpRequest != "undefined")
29A = new XMLHttpRequest();
30if (!A)
31sajax_debug("Could not create connection object.");
32return A;
33}
34function sajax_do_call(func_name, args) {
35var i, x, n;
36var uri;
37var post_data;
38
39uri = "```
40 echo $sajax_remote_uri;
41```";
42if (sajax_request_type == "GET") {
43if (uri.indexOf("?") == -1)
44uri = uri + "?rs=" + escape(func_name);
45else
46uri = uri + "&rs=" + escape(func_name);
47for (i = 0; i < args.length-1; i++)
48uri = uri + "&rsargs[]=" + escape(args[i]);
49uri = uri + "&rsrnd=" + new Date().getTime();
50post_data = null;
51} else {
52post_data = "rs=" + escape(func_name);
53for (i = 0; i < args.length-1; i++)
54post_data = post_data + "&rsargs[]=" + escape(args[i]);
55}
56
57x = sajax_init_object();
58x.open(sajax_request_type, uri, true);
59if (sajax_request_type == "POST") {
60x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
61x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
62}
63x.onreadystatechange = function() {
64if (x.readyState != 4)
65return;
66sajax_debug("received " + x.responseText);
67
68var status;
69var data;
70status = x.responseText.charAt(0);
71data = x.responseText.substring(2);
72if (status == "-")
73alert("Error: " + data);
74else
75args[args.length-1](data);
76}
77x.send(post_data);
78sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
79sajax_debug(func_name + " waiting..");
80delete x;
81}
82
$html = ob_get_contents();
ob_end_clean();
return $html;
}
function sajax_show_common_js() {
echo sajax_get_common_js();
}
// javascript escape a value
function sajax_esc( $val)
{
return str_replace('"', '\\\"', $val);
}
function sajax_get_one_stub( $func_name) {
ob_start();
1
2// wrapper for ```
3 echo $func_name;
function x_``` echo $func_name;
1sajax_do_call("```
2 echo $func_name;
3```",
4x_```
5 echo $func_name;
6```.arguments);
7}
8
$html = ob_get_contents();
ob_end_clean();
return $html;
}
function sajax_show_one_stub( $func_name) {
echo sajax_get_one_stub( $func_name);
}
function sajax_export() {
global $sajax_export_list;
$n = func_num_args();
for ( $i = 0; $i < $n; $i++) {
$sajax_export_list[] = func_get_arg( $i);
}
}
$sajax_js_has_been_shown = 0;
function sajax_get_javascript()
{
global $sajax_js_has_been_shown;
global $sajax_export_list;
$html = "";
if (! $sajax_js_has_been_shown) {
$html .= sajax_get_common_js();
$sajax_js_has_been_shown = 1;
}
foreach ( $sajax_export_list as $func) {
$html .= sajax_get_one_stub( $func);
}
return $html;
}
function sajax_show_javascript()
{
echo sajax_get_javascript();
}
$SAJAX_INCLUDED = 1;
}