Subversion Repositories cryptochat

Compare Revisions

No changes between revisions

Regard whitespace Rev 1 → Rev 2

/trunk/dependencies/sajax/php/Readme.txt
0,0 → 1,33
SAJAX PHP BACKEND
-----------------
 
Contributed and copyighted by Thomas Lackner (http://www.modernmethod.com/) and Anders Jenbo.
 
If you are using PHP < 5.2 you will need to a substitue for JSON_encode and JSON_decode, you might want to try this out http://abeautifulsite.net/notebook/71
 
Usage:
sajax_export() takes a list of functions that are alowed to be called from javascript, there are 4 parameters for each function you send to sajax_export. If strings are used in stead of arrays the functions will just have all options set to there default (like in older versions of Sajax).
 
name = the name of the function (requred)
method = GET : POST (default is GET)
asynchronous = true : false (default is true, if set to false the script will pause untill the call has been compleated)
uri = the uri where the function lives.
 
The default method can be changed by setting $sajax_request_type to "POST" (this can also be done at runtime).
The default uri can be changed by setting $sajax_remote_uri (this can also be done at runtime).
 
Sample:
sajax_export(
"myFunction",
array("name" => "myPostFunction", "method" => "POST"),
array("name" => "sleep", "asynchronous" => true),
array("name" => "myOffSitefunction", "uri" => "http://myotheresite.com/ajax.php")
);
 
sajax_export() may be called multiple times.
 
To get debug alert's set $sajax_debug_mode to true.
 
You can redirect the users browser to a frindly error page incase his/her borwser issn't supported by sajax by setting $sajax_failure_redirect to an URI.
 
Calling sajax_get_common_js() will print the client side script.
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/trunk/dependencies/sajax/php/example_multiply.php
0,0 → 1,48
<?php
function multiply($x, $y) {
return $x * $y;
}
require("sajax.php");
// $sajax_debug_mode = true;
$sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
sajax_export("multiply");
sajax_handle_client_request();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sajax Multiplier example</title>
<script type="text/javascript" src="json2.stringify.js"></script>
<script type="text/javascript" src="json_stringify.js"></script>
<script type="text/javascript" src="json_parse_state.js"></script>
<script type="text/javascript" src="sajax.js"></script>
<script type="text/javascript"><!--
<?php sajax_show_javascript(); ?>
function do_multiply_cb(z) {
document.getElementById("z").value = z;
}
function do_multiply() {
// get the folder name
var x, y;
x = parseFloat(document.getElementById("x").value);
y = parseFloat(document.getElementById("y").value);
x_multiply(x, y, do_multiply_cb);
}
//-->
</script>
</head>
<body>
<input type="text" name="x" id="x" value="2" size="3" />
*
<input type="text" name="y" id="y" value="3" size="3" />
=
<input type="text" name="z" id="z" value="" size="3" />
<input type="button" name="check" value="Calculate" onclick="do_multiply(); return false;" />
</body>
</html>
/trunk/dependencies/sajax/php/example_options.php
0,0 → 1,83
<?php
date_default_timezone_set('Europe/Copenhagen');
function test_get() {
return test();
}
function test_post() {
return test();
}
function test() {
$s = "URI: ".$_SERVER['PHP_SELF'];
$s .= "\n\n-- GET --\n";
if(!empty($_GET["rsargs"]))
$s .= $_GET["rsargs"];
$s .= "\n-- POST --\n";
if(!empty($_POST["rsargs"]))
$s .= $_POST["rsargs"];
return $s;
}
function get_the_time() {
return date("Y-m-d h:i:s");
}
require_once("sajax.php");
// $sajax_debug_mode = true;
$sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
sajax_export(
array("name" => "test_get", "method" => "GET"),
array("name" => "test_post", "method" => "POST"),
array("name" => "get_the_time", "method" => "GET"),
array("name" => "test", "method" => "GET"),
array("name" => "sleep", "asynchronous" => false),
array("name" => "otherefucntion", "uri" => "example_otheruri.php"),
array("name" => "otherefucntion2")
);
sajax_handle_client_request();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example of Sajax Options</title>
<script type="text/javascript" src="json2.stringify.js"></script>
<script type="text/javascript" src="json_stringify.js"></script>
<script type="text/javascript" src="json_parse_state.js"></script>
<script type="text/javascript" src="sajax.js"></script>
<script type="text/javascript"><!--
<?php sajax_show_javascript(); ?>
function print_result(v) {
alert(v);
}
//-->
</script>
</head>
<body>
<!-- Testing if the browser supports GET -->
<button onclick="x_test_get(1, 2, 3, print_result);">Test GET</button>
 
<!-- Testing if the browser supports POST -->
<button onclick="x_test_post(1, 2, 3, print_result);">Test POST</button>
 
<!-- Forcing the function to POST -->
<button onclick="sajax_request_type ='POST'; x_test(1, 2, 3, print_result); sajax_request_type ='';">Test force POST</button>
 
<!-- if sajax_target_id is set, the sesponce will be inserted as HTML in an element with sajax_target_id for id-->
<button onclick="sajax_target_id = 'time'; x_get_the_time(); sajax_target_id = '';">Test updating IDs</button>
 
<!-- Calling a synchronous will cause the script to wait for the responce -->
<button onclick="x_sleep(3, function(){}); alert('Link was clicked!');">Test synchronous</button>
 
<!-- Different URI set at config -->
<button onclick="x_otherefucntion(print_result);">Call to other uri.</button>
 
<!-- Forece different URI at runtime -->
<button onclick="sajax_remote_uri = 'example_otheruri.php'; x_otherefucntion2(print_result); sajax_remote_uri = '';">Force call to other uri.</button>
<div id="time"><em>Time will appear here</em></div>
</body>
</html>
/trunk/dependencies/sajax/php/example_otheruri.php
0,0 → 1,13
<?php
function otherefucntion() {
return "URI: ".$_SERVER['PHP_SELF'];
}
function otherefucntion2() {
return otherefucntion();
}
require("sajax.php");
sajax_export("otherefucntion", "otherefucntion2");
sajax_handle_client_request();
?>
/trunk/dependencies/sajax/php/example_types.php
0,0 → 1,85
<?php
date_default_timezone_set('Europe/Copenhagen');
$bday = strtotime("5 September 1983");
$age = (date('z')-date('z', $bday))/1000+date('Y')-date('Y', $bday);
function return_array() {
global $age;
return array("name" => "Anders", "age" => $age);
}
function return_object() {
global $age;
class MyObj {
var $name, $age;
function MyObj($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$o = new MyObj("Anders", $age);
return $o;
}
function return_int() {
global $age;
return floor($age);
}
function return_float() {
global $age;
return $age;
}
function return_string() {
global $age;
return "Anders is ".floor($age)." years old.";
}
require("sajax.php");
$sajax_request_type = "GET";
// $sajax_debug_mode = true;
$sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
sajax_export("return_array", "return_object", "return_int", "return_float", "return_string");
sajax_handle_client_request();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sajax PHP return types example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="json2.stringify.js"></script>
<script type="text/javascript" src="json_stringify.js"></script>
<script type="text/javascript" src="json_parse_state.js"></script>
<script type="text/javascript" src="sajax.js"></script>
<script type="text/javascript"><!--
<?php sajax_show_javascript(); ?>
function display_result(val) {
var repr;
repr = "";
repr += "Type: " + typeof val + "\n";
repr += "Value: ";
if (typeof val == "object" ||
typeof val == "array") {
repr += "{ ";
for (var i in val)
repr += i + ": " + val[i] + ", ";
repr = repr.substr(0, repr.length-2) + " }";
} else {
repr += val;
}
alert(repr);
}
//-->
</script>
</head>
<body>
<button onclick="x_return_array(display_result);">Return as array (will become an object)</button>
<button onclick="x_return_object(display_result);">Return as object</button>
<button onclick="x_return_int(display_result);">Return as int</button>
<button onclick="x_return_float(display_result);">Return as float/double</button>
<button onclick="x_return_string(display_result);">Return as string</button>
</body>
</html>
/trunk/dependencies/sajax/php/example_wall.php
0,0 → 1,122
<?php
//
// The world's least efficient wall implementation (now a bit more efficient)
//
date_default_timezone_set('Europe/Copenhagen');
$filename = "tmp/wall.html";
function colorify_ip($ip)
{
$parts = explode(".", $ip);
$color = sprintf("%02s", dechex($parts[1])) .
sprintf("%02s", dechex($parts[2])) .
sprintf("%02s", dechex($parts[3]));
return $color;
}
function add_line($msg) {
global $filename;
$f = fopen($filename, "a");
$date = date("Y-m-d h:i:s");
$msg = strip_tags(stripslashes($msg));
$remote = $_SERVER["REMOTE_ADDR"];
// generate unique-ish color for IP
$color = colorify_ip($remote);
fwrite($f, '<span style="color:#'.$color.'">'.$date.'</span> '.htmlspecialchars($msg).'<br />'."\r\n");
fclose($f);
return refresh(0);
}
function refresh($lastrefresh) {
global $filename;
if(filemtime($filename) > $lastrefresh) {
$lines = file($filename);
// return the last 25 lines
return array("wall" => join("\n", array_slice($lines, -25)), "update" => filemtime($filename));
} else {
return false;
}
}
require("sajax.php");
// $sajax_debug_mode = true;
$sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
sajax_export(
array("name" => "add_line", "method" => "POST"),
array("name" => "refresh", "method" => "GET")
);
sajax_handle_client_request();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sajax graffiti wall example</title>
<style type="text/css">
.date {
color: blue;
}
</style>
<script type="text/javascript" src="json_stringify.js"></script>
<script type="text/javascript" src="json_parse.js"></script>
<script type="text/javascript" src="sajax.js"></script>
<script type="text/javascript"><!--
<?php sajax_show_javascript(); ?>
var check_n = 1;
var nextrefresh;
function refresh_cb(data) {
if(data !== false) {
document.getElementById("wall").innerHTML = data["wall"];
lastrefresh = data["update"];
clearTimeout(nextrefresh);
nextrefresh = setTimeout("refresh();", 1000);
} else {
clearTimeout(nextrefresh);
nextrefresh = setTimeout("refresh();", 2500);
}
document.getElementById("status").innerHTML = "Checked #" + check_n++;
}
var lastrefresh = <?php echo(filemtime($filename)); ?>;
function refresh() {
document.getElementById("status").innerHTML = "Checking..";
x_refresh(lastrefresh, refresh_cb);
}
function add() {
var line;
var handle;
handle = document.getElementById("handle").value;
line = document.getElementById("line").value;
if(line == "")
return;
x_add_line("[" + handle + "] " + line, refresh_cb);
document.getElementById("line").value = "";
}
function keypress(keyCode) {
if (keyCode==13) {
add();
document.getElementById("line").select();
return false;
}
return true;
}
nextrefresh = setTimeout("refresh();", 1000);
//-->
</script>
</head>
<body>
<b><a href="http://www.sajax.info/">Sajax</a> v<?php echo($sajax_version); ?></b> - You are a guinea pig - This example illustrates the simplest possible graffiti wall. It isn't meant to be perfect, featureful, or even useful.<br />
<form action="" method="post" onsubmit="add(); return false;">
<input type="text" name="handle" id="handle" value="(name)" onfocus="this.select()" style="width:130px;" />
<input type="text" name="line" id="line" value="(enter your message here)" onfocus="this.select();" onkeypress="keypress(event.keyCode);" style="width:300px;" />
<input type="button" name="check" value="Post message" onclick="add(); return false;" />
</form>
<div id="wall"> <?php $temp = refresh(0); echo($temp["wall"]); ?></div>
<div id="status">Checked #0</div>
</body>
</html>
/trunk/dependencies/sajax/php/json2.parse.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/json2.stringify.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/json_parse.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/json_parse_state.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/json_stringify.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/sajax.js
Cannot display: file marked as a binary type.
svn:mime-type = application/javascript
Property changes:
Added: svn:mime-type
+application/javascript
\ No newline at end of property
/trunk/dependencies/sajax/php/sajax.php
0,0 → 1,141
<?php
if (!isset($SAJAX_INCLUDED)) {
/*
* GLOBALS AND DEFAULTS
*
*/
$GLOBALS['sajax_version'] = "0.13";
$GLOBALS['sajax_debug_mode'] = false;
$GLOBALS['sajax_export_array'] = array();
$GLOBALS['sajax_export_list'] = array();
$GLOBALS['sajax_remote_uri'] = "";
$GLOBALS['sajax_failure_redirect'] = "";
$GLOBALS['sajax_request_type'] = "GET";
/*
* CODE
*
*/
function sajax_handle_client_request() {
if (empty($_GET["rs"]) && empty($_POST["rs"]))
return;
ob_start();
if (!empty($_GET["rs"])) {
// Always call server
header ("Cache-Control: max-age=0, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
$func_name = $_GET["rs"];
if (! empty($_GET["rsargs"]))
$args = $_GET["rsargs"];
} else {
$func_name = $_POST["rs"];
if (! empty($_POST["rsargs"]))
$args = $_POST["rsargs"];
}
if(! empty($args)) {
if(get_magic_quotes_gpc())
$args = stripslashes($args);
$args = json_decode($args, true);
if(get_magic_quotes_gpc()) {
function array_addslashes($value) {
if(is_array($value))
return array_map("array_addslashes", $value);
else
return addslashes($value);
}
$args = array_map("array_addslashes", $args);
}
} else {
$args = array();
}
global $sajax_export_list;
if (! in_array($func_name, $sajax_export_list)) {
$error = $func_name." not callable";
} else {
$result = call_user_func_array($func_name, $args);
$error = ob_get_contents();
ob_end_clean();
}
header('Content-Type: text/plain; charset=UTF-8');
if(!empty($error)) {
echo '-:'.$error;
} else {
echo "+:".json_encode($result);
}
exit;
}
$sajax_js_has_been_shown = false;
function sajax_show_javascript()
{
global $sajax_js_has_been_shown;
global $sajax_debug_mode;
global $sajax_failure_redirect;
if (! $sajax_js_has_been_shown) {
?>
sajax_debug_mode = <?php echo($sajax_debug_mode ? "true" : "false"); ?>;
sajax_failure_redirect = "<?php echo($sajax_failure_redirect); ?>";
<?php
global $sajax_export_array;
foreach($sajax_export_array as $function) {
?>
function x_<?php echo($function["name"]); ?>() {
return sajax_do_call("<?php echo($function["name"]); ?>", arguments, "<?php echo($function["method"]); ?>", <?php echo($function["asynchronous"] ? 'true' : 'false'); ?>, "<?php echo($function["uri"]); ?>");
}
<?php
}
$sajax_js_has_been_shown = true;
}
}
function sajax_export() {
global $sajax_export_array;
global $sajax_export_list;
global $sajax_request_type;
global $sajax_remote_uri;
$num = func_num_args();
for ($i=0; $i<$num; $i++) {
$function = func_get_arg($i);
if(!is_array($function))
$function = array("name" => $function);
if(!isset($function["method"]))
$function["method"] = $sajax_request_type;
if(!isset($function["asynchronous"]))
$function["asynchronous"] = true;
if(!isset($function["uri"]))
$function["uri"] = $sajax_remote_uri;
$key = array_search($function["name"], $sajax_export_list);
if ($key === false) {
$sajax_export_array[] = $function;
$sajax_export_list[] = $function["name"];
} else {
//Overwrite old function
$sajax_export_array[$key] = $function;
$sajax_export_list[$key] = $function["name"];
}
}
}
$SAJAX_INCLUDED = 1;
}
?>
/trunk/dependencies/sajax/php/tmp/wall.html
0,0 → 1,46
<span style="color:#5b024d">2009-08-21 12:42:37</span> [Netscape 6.0] Shows fail page correctly<br />
<span style="color:#5b024d">2009-08-21 12:42:37</span> [Netscape 6.1] Shows fail page correctly<br />
<span style="color:#3ca206">2009-08-22 12:37:12</span> [Mozilla 0.9.4] Works *<br />
<span style="color:#5b024d">2009-08-21 08:26:49</span> [Mozilla 0.9.9] Works<br />
<span style="color:#5b024d">2009-08-21 12:38:53</span> [Firefox 2.0.0.20] Works<br />
<span style="color:#5b024d">2009-08-21 12:41:09</span> [Firefox 3.5.2] Works<br />
<span style="color:#5b024d">2009-08-21 12:42:37</span> [IE 4] Failes horribly<br />
<span style="color:#fbc320">2009-08-22 03:32:36</span> [Fennec M10] Works<br />
<span style="color:#5b024d">2009-08-21 12:42:37</span> [IE 5.01] Works<br />
<span style="color:#5b024d">2009-08-21 12:42:30</span> [IE 5.5] Works<br />
<span style="color:#5b024d">2009-08-21 12:42:20</span> [IE 6] Works<br />
<span style="color:#5b024d">2009-08-21 12:41:56</span> [IE 7] Works<br />
<span style="color:#5b024d">2009-08-21 12:41:41</span> [IE 8] Works<br />
<span style="color:#fbc320">2009-08-22 02:42:44</span> [Pocket Interrnet Explorer 7.11] Works<br />
<span style="color:#5b024d">2009-08-21 01:47:23</span> [iCab 3.0.5] Works **<br />
<span style="color:#5b024d">2009-08-21 06:13:48</span> [Safari 1.0] Shows fail page correctly<br />
<span style="color:#5b024d">2009-08-21 06:13:48</span> [Safari 1.1] Shows fail page correctly<br />
<span style="color:#5b024d">2009-08-21 06:13:48</span> [Safari 1.2] Works<br />
<span style="color:#5b024d">2009-08-21 08:20:58</span> [Safari 1.3] Works<br />
<span style="color:#5b024d">2009-08-21 09:02:25</span> [Safari 1.3.1] Works<br />
<span style="color:#5b024d">2009-08-21 12:38:37</span> [Safari 1.3.2] Works<br />
<span style="color:#fbc302">2009-08-24 03:17:34</span> [Safari 2.0] Works<br />
<span style="color:#fbc302">2009-08-24 03:18:16</span> [Safari 2.0.2] Works<br />
<span style="color:#fbc302">2009-08-24 03:18:58</span> [Safari 2.0.3] Works<br />
<span style="color:#fbc302">2009-08-24 03:19:31</span> [Safari 2.0.4] Works<br />
<span style="color:#3ca206">2009-08-22 12:41:35</span> [Safari 3.0] Works<br />
<span style="color:#5b024d">2009-08-21 08:45:50</span> [Safari 3.0.4] Works<br />
<span style="color:#fbc302">2009-08-24 03:15:07</span> [Safari 3.1.2] Works<br />
<span style="color:#fbc302">2009-08-24 03:15:54</span> [Safari 3.2.1] Works<br />
<span style="color:#fbc31f">2009-08-23 02:23:18</span> [Safari 3.2.3] Works<br />
<span style="color:#5b024d">2009-08-21 10:17:10</span> [Safari 4.0.3] Works<br />
<span style="color:#fbc320">2009-08-22 01:58:58</span> [iPhone 3.0.1] Works<br />
<span style="color:#5b024d">2009-08-21 08:36:46</span> [Google Chrome 2.0.172.39] Works<br />
<span style="color:#3ca206">2009-08-22 12:39:57</span> [Opera 8.0] Works *<br />
<span style="color:#3ca206">2009-08-22 02:30:58</span> [Opera 8.01] Only with GET<br />
<span style="color:#3ca206">2009-08-22 01:57:33</span> [Opera 8.02] Works<br />
<span style="color:#3ca206">2009-08-22 01:50:09</span> [Opera 8.50] Works<br />
<span style="color:#5b024d">2009-08-21 06:32:43</span> [Opera 8.54] Works<br />
<span style="color:#5b024d">2009-08-21 12:41:17</span> [Opera 9.64] Works<br />
<span style="color:#5b024d">2009-08-21 06:31:49</span> [Opera 10b] Works<br />
<span style="color:#5b024d">2009-08-24 12:26:46</span> [Opera 10b3] Works<br />
<span style="color:#fbc320">2009-08-22 03:15:22</span> [Opera Mobile 8.65] Works<br />
<span style="color:#fbc320">2009-08-22 02:38:22</span> [Opera Mobile 9.5] Works<br />
<span style="color:#fbc320">2009-08-22 03:07:59</span> [Opera Mobile 9.7b] Works<br />
<span style="color:#cb6018">2009-08-22 03:09:52</span> [Opera Mobile 9.7b Turbo] Works<br />
<span style="color:#bd8e2a">2009-08-22 02:40:12</span> [Opera Mini 4.2] Works<br />
Property changes:
Added: svn:mime-type
+text/html
\ No newline at end of property