Source for file _functions.php
Documentation is available at _functions.php
* dbscript for PHP 4 & 5 - restful crud framework
* @version 0.1.2 -- 19-Feb-2007
* @author Brian Hendrickson <brian@dbscript.net>
* @link http://dbscript.net/
* @copyright Copyright 2007 Brian Hendrickson
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* takes a table/resource name ('entries')
* makes it singular & capitalized ('Entry')
* @param string $resource
case ( substr( $n, - 3 ) == 'ies' ) :
$n = substr( $n, 0, - 3 ) . "y";
case ( substr( $n, - 1 ) == 's' ) :
* takes a (CamelCase or not) name ('DbSession')
* makes it lower_case and plural ('db_sessions')
// replace uppercase Chars with _ and lowercase char
foreach ( $n as $idx=> $char ) {
case ( $n == 'person' ) :
case ( substr( $n, - 1 ) == 'y' ) :
$n = substr( $n, 0, - 1 ) . "ies";
case ( substr( $n, - 1 ) != 's' ) :
* custom Error handling per-client-type
* @author Brian Hendrickson <brian@dbscript.net>
* @param integer $errline
* @todo return based on content-negotiation status
if (isset ($_GET['dbscript_xml_error_continue'])) {
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= " <dbscript_error>Fatal error in line $errline of file $errfile<br>: $errstr</dbscript_error>\n";
} elseif (isset ($req->error)) {
$req->handle_error( $errstr );
print "<b>ERROR</b> [$errno] $errstr<br />\n";
print " Fatal error in line $errline of file $errfile<br />\n";
print "Aborting...<br />\n";
print "<b>ERROR</b> [$errno] $errstr<br />\n";
print " Fatal error in line $errline of file $errfile<br />\n";
print "Aborting...<br />\n";
print "<b>WARNING</b> [$errno] $errstr<br />\n";
print "<b>NOTICE</b> [$errno] $errstr<br />\n";
print "<b>ERROR</b> [$errno] $errstr<br />\n";
print " Error in line $errline of file $errfile<br />\n";
* trip before filters for a function
if ( isset ( $GLOBALS['ASPECTS']['before'][$func] ) ) {
foreach( $GLOBALS['ASPECTS']['before'][$func] as $callback ) {
* trip after filters for a function
if ( isset ( $GLOBALS['ASPECTS']['after'][$func] ) ) {
foreach( $GLOBALS['ASPECTS']['after'][$func] as $callback ) {
* add trigger function name pairs to GLOBALS
* @param string $callback
$GLOBALS['ASPECTS'][$type][$func][] = $callback;
* set an aspect function to trigger before another function
* set an aspect function to trigger after another function
return $GLOBALS['PATH']['models'];
return $GLOBALS['PATH']['library'];
* path to library/dbscript
return $GLOBALS['PATH']['dbscript'];
* returns value of environment variable, if set
if ( isset ( $env['ignore_errors'] ) && $env['ignore_errors'] )
return $GLOBALS['PATH']['plugins'];
return $GLOBALS['PATH']['controllers'];
* get global Database object
$db->large_object_fetch( $value );
$db->large_object_fetch( $value, true );
* find a template during content-negotiation
* @param string $extension
#if ($template == 'introspection') print 'ye';
#print "template_exists $template ".$extension."<br>";
$view = $request->get_template_path( $extension, $template );
* generate a form action string
* @param string $template
function form_for( &$resource, &$member, $url ) {
if ( isset ( $resource->table )) {
// remote_form_for :entry, @new_entry, :url => entries_url(:project_id => @project.id )
return "<form method=\"post\" >";
* generate a url from a Route
* @param array $altparams
function url_for( $params, $altparams = NULL ) {
print $request->url_for( $params, $altparams );
* redirect the browser via Routes
* @param string $template
* render template or blob using content-negotiation
* @param string $template
function render( $param, $value ) {
if ($param == 'action' && !(strpos($value,".") === false)) {
$spleet = split("\.",$value);
$request->set_param( 'client_wants', $spleet[1] );
$request->set_param( $param, $value );
$response->render( $request );
* render a _template using content-negotiation
* @param string $template
if (!(strpos($template,".") === false)) {
$spleet = split("\.",$template);
$request->set_param( 'client_wants', $spleet[1] );
* render a _template using content-negotiation
* @param string $template
* get global Mapper object
'charset' => 'iso-8859-1',
if (isset ($env['content_types']))
return $env['content_types'];
* get global Mapper object
* get global DbSession data model
* magic_quotes_stripslashes_b
* magic_quotes_stripslashes
* magic_quotes_stripquotes_b
* magic_quotes_stripquotes
header( 'Status: ' . $status, TRUE );
header( $_SERVER['SERVER_PROTOCOL'] . ' '. $status );
$cookie->userid = $userid;
* throw the Cookie away? noo..
* is the Cookie fit for consumption?
if ($cookie->validate()) {
* mutator function makes an array of local variables extractable
function vars($varios, $scope= false, $prefix= 'unique', $suffix= 'value') {
foreach ($varios as $orig) {
$var = $new = $prefix . rand() . $suffix;
foreach( $vals as $key => $val ) {
if ( $val === $new ) $vname = $key;
* give it a string length and you will receive a random string!
* returns the array minus the named element
if (count($key_index) != '') {
return $array_with_elements;
* Parses an xml dictionary.
* First argument is file name OR xml data.
* Second argument must be 'true' if the first arg is file name.
* $array = dictionary_parse( $file_name, true );
* @author Brian Hendrickson <brian@dbscript.net>
function dictionary_parse( $data ) { /* parse XML dictionaries (lookup tables) */
trigger_error("error creating xml parser (xml_parser_create) in dictionary_parse", E_USER_ERROR );
if (count($func_args) > 1) {
if ($func_args[1] == true) {
trigger_error("error reading file contents in dictionary_parse, file name was: ". $func_args[0], E_USER_ERROR );
foreach ($tags as $tagname=> $valuelocations) {
foreach ($valuelocations as $valkey=> $valloc) {
$map[$i] = $values[$valloc]['value'];
$dict[$values[$valloc]['value']] = "";
if ($tagname == "STRING") {
foreach ($valuelocations as $valkey=> $valloc) {
if (isset ($values[$valloc]['value'])) {
$dict[$map[$i]] = $values[$valloc]['value'];
* @param integer $insensitive
function in_string($needle, $haystack, $insensitive = 0) {
return (false !== stristr($haystack, $needle)) ? true : false;
return (false !== strpos($haystack, $needle)) ? true : false;
* the name of the current script
if (!empty($_SERVER['PHP_SELF'])) {
$strScript = $_SERVER['PHP_SELF'];
} else if (!empty($_SERVER['SCRIPT_NAME'])) {
$strScript = @$_SERVER['SCRIPT_NAME'];
trigger_error("error reading script name in get_script_name", E_USER_ERROR );
$intLastSlash = strrpos($strScript, "/");
if (strrpos($strScript, "\\")> $intLastSlash) {
$intLastSlash = strrpos($strScript, "\\");
return substr($strScript, $intLastSlash+ 1, strlen($strScript));
function dircopy($srcdir, $dstdir, $verbose = false) {
if($file != '.' && $file != '..') {
$srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
$dstfile = $dstdir . DIRECTORY_SEPARATOR . $file;
if($verbose) echo "Copying '$srcfile' to '$dstfile'...";
if(copy($srcfile, $dstfile)) {
if($verbose) echo "OK\n";
else echo "Error: File '$srcfile' could not be copied!\n";
$num += dircopy($srcfile, $dstfile, $verbose);
* more flexible is_object
function is_obj( &$object, $check= null, $strict= true ) {
return ($strict === true)?
( $object_name == $check ):
|