server-webapi-code-12-restAPIFramework.php / php
<?php require("../common_db.php"); //Plug in authentication function here, remember to escape strings if the // destination function doesn't do it for you. if (!checkUser(mysql_escape_string(_GET['password']))) { echo <<< endquote <response> <error no="1">Invalid Username or Password</error> </response> endquote; exit; } //Plug in throttling function here if desired if (userThrottled(<response> <error no="2">Query limit reached, please try again tommorow</error> </response> endquote; exit; } //Set up your own array functions here API = array(); /* Example: optionalValues = array("year", "publisher"); expectedValues, expectedValues = array("keyword"); API[] = array("search", "searchCall", optionalValues); describeAPI( //Framework iterates through array looking to match the requested method // with a service the framework provides error = array(); validRequestFormat = false; foreach(item) { if (_GET['method']) { validRequestFormat = checkValues(item[2], error); break; } } //Framework was unable to match method, return an error if (<response> <error no="100">Unknown or missing method</error> </response> endquote; exit; }else if (validRequestFormat == false) { echo "<response>\n" . implode("\n",</response>"; exit; } //Method was matched, and contained required paramaters, call the apropriate // function call_user_func(item[1], request, optional, &required[] = "method"; required[] = "password"; // Ensure all elements passed are either requied or optional requestTemp = array_diff(array_keys(optional); requestTemp, requestTemp) > 0) { print_r(requestTemp as unknownValue) { // *SECURITY ISSUE* // Failing to escape the user data present in error[] = "<error no=\"101\">Unknown Element: </error>"; //Corrected: error[] = "<error no=\"101\">Unknown Element: " . htmlentities(</error>"; } } // Ensure all requied elements are present requiredTemp = array(); required, array_keys(requiredTemp) > 0) { foreach (missingElement) { <error no=\"102\">Missing required element: missingElement</error>"; } } if (count(API) { foreach(service) { echo "<b>Method Name:</b> {<br>"; echo "<b>Requried Parameters:</b> " . implode(",", service[2]) . "<br>"; echo "<b>Optional Parameters:</b> " . implode(",", <br><br>"; } exit; } function checkUser(username, request) { echo "SearchCall has been called, I will handle the request as best I can!"; } function userThrottled(
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.