Sajax – PHP and AJAX
Sajax is an open source tool to make programming websites using the Ajax framework — also known as XMLHTTPRequest or remote scripting — as easy as possible. Sajax makes it easy to call PHP, Perl or Python functions from your webpages via JavaScript without performing a browser refresh. The toolkit does 99% of the work for you so you have no excuse to not use it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | <?php if (!isset($SAJAX_INCLUDED)) { /* * GLOBALS AND DEFAULTS * */ $GLOBALS['sajax_version'] = '0.12'; $GLOBALS['sajax_debug_mode'] = 0; $GLOBALS['sajax_export_list'] = array(); $GLOBALS['sajax_request_type'] = 'GET'; $GLOBALS['sajax_remote_uri'] = ''; $GLOBALS['sajax_failure_redirect'] = ''; /* * CODE * */ // // Initialize the Sajax library. // function sajax_init() { } // // Helper function to return the script's own URI. // function sajax_get_my_uri() { return $_SERVER["REQUEST_URI"]; } $sajax_remote_uri = sajax_get_my_uri(); // // Helper function to return an eval()-usable representation // of an object in JavaScript. // function sajax_get_js_repr($value) { $type = gettype($value); if ($type == "boolean") { return ($value) ? "Boolean(true)" : "Boolean(false)"; } elseif ($type == "integer") { return "parseInt($value)"; } elseif ($type == "double") { return "parseFloat($value)"; } elseif ($type == "array" || $type == "object" ) { // // XXX Arrays with non-numeric indices are not // permitted according to ECMAScript, yet everyone // uses them.. We'll use an object. // $s = "{ "; if ($type == "object") { $value = get_object_vars($value); } foreach ($value as $k=>$v) { $esc_key = sajax_esc($k); if (is_numeric($k)) $s .= "$k: " . sajax_get_js_repr($v) . ", "; else $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", "; } if (count($value)) $s = substr($s, 0, -2); return $s . " }"; } else { $esc_val = sajax_esc($value); $s = "'$esc_val'"; return $s; } } 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; $target = ""; 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 "var res = " . trim(sajax_get_js_repr($result)) . "; res;"; } exit; } function sajax_get_common_js() { global $sajax_debug_mode; global $sajax_request_type; global $sajax_remote_uri; global $sajax_failure_redirect; $t = strtoupper($sajax_request_type); if ($t != "" && $t != "GET" && $t != "POST") return "// Invalid type: $t.. \n\n"; ob_start(); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | // remote scripting library // (c) copyright 2005 modernmethod, inc var sajax_debug_mode = "<?php echo $sajax_debug_mode ? "true" : "false"; ?>"; var sajax_request_type = "<?php echo $t; ?>"; var sajax_target_id = ""; var sajax_failure_redirect = "<?php echo $sajax_failure_redirect; ?>"; function sajax_debug(text) { if (sajax_debug_mode) alert(text); } function sajax_init_object() { sajax_debug("sajax_init_object() called..") var A; var msxmlhttp = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'); for (var i = 0; i < msxmlhttp.length; i++) { try { A = new ActiveXObject(msxmlhttp[i]); } catch (e) { A = null; } } if(!A && typeof XMLHttpRequest != "undefined") A = new XMLHttpRequest(); if (!A) sajax_debug("Could not create connection object."); return A; } var sajax_requests = new Array(); function sajax_cancel() { for (var i = 0; i < sajax_requests.length; i++) sajax_requests[i].abort(); } function sajax_do_call(func_name, args) { var i, x, n; var uri; var post_data; var target_id; sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id); target_id = sajax_target_id; if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "") sajax_request_type = "GET"; uri = "<?php echo $sajax_remote_uri; ?>"; if (sajax_request_type == "GET") { if (uri.indexOf("?") == -1) uri += "?rs=" + escape(func_name); else uri += "&rs=" + escape(func_name); uri += "&rst=" + escape(sajax_target_id); uri += "&rsrnd=" + new Date().getTime(); for (i = 0; i < args.length-1; i++) uri += "&rsargs[]=" + escape(args[i]); post_data = null; } else if (sajax_request_type == "POST") { post_data = "rs=" + escape(func_name); post_data += "&rst=" + escape(sajax_target_id); post_data += "&rsrnd=" + new Date().getTime(); for (i = 0; i < args.length-1; i++) post_data = post_data + "&rsargs[]=" + escape(args[i]); } else { alert("Illegal request type: " + sajax_request_type); } x = sajax_init_object(); if (x == null) { if (sajax_failure_redirect != "") { location.href = sajax_failure_redirect; return false; } else { sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent); return false; } } else { x.open(sajax_request_type, uri, true); // window.open(uri); sajax_requests[sajax_requests.length] = x; if (sajax_request_type == "POST") { x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1"); x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } x.onreadystatechange = function() { if (x.readyState != 4) return; sajax_debug("received " + x.responseText); var status; var data; var txt = x.responseText.replace(/^\s*|\s*$/g,""); status = txt.charAt(0); data = txt.substring(2); if (status == "") { // let's just assume this is a pre-response bailout and let it slide for now } else if (status == "-") alert("Error: " + data); else { if (target_id != "") document.getElementById(target_id).innerHTML = eval(data); else { try { var callback; var extra_data = false; if (typeof args[args.length-1] == "object") { callback = args[args.length-1].callback; extra_data = args[args.length-1].extra_data; } else { callback = args[args.length-1]; } callback(eval(data), extra_data); } catch (e) { sajax_debug("Caught error " + e + ": Could not eval " + data ); } } } } } sajax_debug(func_name + " uri = " + uri + "/post = " + post_data); x.send(post_data); sajax_debug(func_name + " waiting.."); delete x; return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php $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) { $val = str_replace("\\", "\\\\", $val); $val = str_replace("\r", "\\r", $val); $val = str_replace("\n", "\\n", $val); $val = str_replace("'", "\\'", $val); return str_replace('"', '\\"', $val); } function sajax_get_one_stub($func_name) { ob_start(); ?> // wrapper for <?php echo $func_name; ?> function x_<?php echo $func_name; ?>() { sajax_do_call("<?php echo $func_name; ?>", x_<?php echo $func_name; ?>.arguments); } <?php $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; } ?> |
You can reference more info here at the home site
PHP Class – PHPMailer
PHPMailer is a PHP class for PHP that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "smtp.example.com"; // SMTP server $mail->From = "from@example.com"; $mail->AddAddress("myfriend@example.net"); $mail->Subject = "First PHPMailer Message"; $mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer."; $mail->WordWrap = 50; if(!$mail->Send()) { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?> |
You can find more code samples and the base class to download here: Click Here
PHP Class – Geshi
GeSHi is a Generic Syntax Highlighter for PHP and many other languages. The user inputs the source to be highlighted and a language to highlight it in, and GeSHi returns the source code, highlighted and formatted for the web. It includes features for increasing the speed of highlighting, changing how the source is displayed and decreasing the amount of HTML source outputted for speed over slower connections.
GeSHi features also include:
- The ability to check for keywords in source code in either a case sensitive or non-case sensitive manner (for example, Java will only accept classes with TheCorrectCapitalisation, while in PHP it doesn’t matter)
- The ability to auto-caps/auto-noncaps keywords in the source (particularly for SQL and older BASIC dialects)
- The ability to change the style of almost any aspect of the source on the fly, or even choose whether some parts of the source (for example, strings) are highlighted at all. The use of CSS means that the source can take on many aspects – rather than those provided by deprecated HTML elements such as font, b etc.
- The ability to use CSS classes to massively reduce the amount of outputted code.
- XHTML compliant output.
- Simple adding and removing languages.
- Function to URL conversion so functions can be linked to API documentation
- Line numbering, context highlighting and much, much more!

The PHPClass page can be found here: Click Here
The GeSHI HomePage can be found here
GraphicsMagick or ImageMagick
At some point in a web site or web application project you are going to need Photoshop like capabilites and you are going to need them dynamically on the web. This is where packages like GD fall short and packages like ImageMagick nicely fit this bill however recently I was doing some research on ImageMagick something that we use a lot in our shop, ( using php just making command calls to ImageMagick ) and I came across a new project GraphicsMagic or rather fork of the ImageMagick project that not only claims to be faster and be less resource intensive but also claims that its Open Soucre Licensening is valid and the Licensing of ImageMagick is in question or at worst in violation. So as usual I am interested to here from others on this topic.
PHP Class – getID3
Today I was working on a media managent CMS tool and needed the ability to know what size ( screen resolution ) a video is, similar but different to using getimagesize() for an image. I came across an article on php.net and a class project on soureforge that offers a wealth of info on media files of all types. I thought I would share what I found, getID3() is a PHP script that extracts useful information (such as ID3 tags, bitrate, playtime, etc.) from MP3s & other multimedia file formats (Ogg, WMA, WMV, ASF, WAV, AVI, AAC, VQF, FLAC, MusePack, Real, QuickTime, Monkey’s Audio, MIDI and more).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php // include getID3() library (can be in a different directory if full path is specified) include_once('getid3.php'); // Initialize getID3 engine $getID3 = new getID3; // File to get info from $file_location = './your/path/to/file.mov'; // Get information from the file $fileinfo = $getID3->analyze($file_location); getid3_lib::CopyTagsToComments($fileinfo); // Output results if (!empty($fileinfo['video']['resolution_x'])) { echo '<p> video width: '.$fileinfo['video']['resolution_x'].'</p>'; } if (!empty($fileinfo['video']['resolution_y'])) { echo '<p> video height: '.$fileinfo['video']['resolution_y'].'</p>'; } ?> |
The package referenced (http://www.getid3.org/)
Reset OS X Password
I actually needed to do this for someone who had purchased a macbook and then did not use it for a bit, and subsequently forgot what they had set the admin password to. So this details how to reset the mac back and rerun the intsall utility so that you can recreate the new admin account and then delete the old one. I know that I tested this on a really old version of OS X so I am sure that this would / should work on future versions of OS X however as always, if this has changed I would enjoy some feedback.
To reset your OS X password without an OS X CD you need to enter terminal and create a new admin account:
- Reboot
- Hold apple + s down after you hear the chime.
- When you get text prompt enter in these terminal commands to create a brand new admin account (hitting return after each line):
- mount -uw /
- rm /var/db/.AppleSetupDone
- shutdown -h now
After rebooting you should have a brand new admin account. When you login as the new admin you can simply delete the old one and you’re good to go again!
Moving files – Taking ownership of file or folder
So I was asked by a friend to help him with his computer……again. So this time it was a moving of files from one computer to the new one. The old box had Windows XP on it and the new box was of course the dreaded Vista. The problem occured when I took the hard drive out of the old computer and hooked this up as a n USB external device to the new computer the drive would not let me access the contents of the folders. I was logged in as admin on the new machine, but still no go. I did some research and came accross this article on mircosoft’s website which in detail explained the problem and how to fix this. I am sure this would work regardless of how the transfer would have worked ( Vista to XP ?? ), however if not respond and let me know.
Here is the Microsoft article
CocoaMySQL – MAC database management
The Mac database management app, CocoaMySQL, is the brainchild of Lorenz Textor. Lorenz was the creator and the main developer of CocoaMySQL from its early beginnings in 2003. Without Lorenz’s contributions to CocoaMySQL, the Mac database landscape would be very different today. Sequel Pro is the successor of CocoaMySQL. Download Sequel Pro from the dowload page. A copy of CocoaMySQL 0.7 beta 6 is available from the release archives for users running Mac OS X Tiger and Mac OS X Panther.CocoaMySQL is an application used to manage MySQL databases (locally or over the internet). It lets you add and remove databases and tables, change fields and indexes, view and filter the content of tables, add, edit and remove rows, perform custom queries and dump tables or entire databases.CocoaMySQL is written in Cocoa and Objective-C and uses the SMySQL framework by Serge Cohen to connect to the MySQL-Server. You can manage servers that are local and remote.
For more information: http://www.sequelpro.com/cocoamysql.html
Google Code Home: http://code.google.com/p/sequel-pro/