$maxValue){ $data = $maxValue; } } return $data; } // end constrainTo(...) //// // Turns a string into an array where the string represents a nested array where the top-level array // values are separated by a pipe, and the values in the sub-array are separated by two pipes, etc. // // See also: serialize_nested. //// function deserialize_nested($serializedData, $separator=","){ $retVal = array(); $matches = array(); while(0 < preg_match("/^(.*?[^$separator])[$separator]([^$separator].*)$/is", $serializedData, $matches)){ $retVal[] = $matches[1]; $serializedData = $matches[2]; } $retVal[] = $serializedData; // Recursively deserialize all of the sub-arrays. for($cnt=0; $cnt < count($retVal); $cnt++){ if(false !== strpos($retVal[$cnt], $separator)){ // Instead of making the delimiter longer, just change it so that all remaining delimiters are one character shorter. $subData = $retVal[$cnt]; $subData = preg_replace("/($separator+)$separator/is", "$1", $subData); $retVal[$cnt] = deserialize_nested($subData, $separator); } } return $retVal; } // end deserialize_nested() //// // Given an array of arbitrary depth, serializes it to a string delimited by groups of 'separator' characters. // The amount of depth will be equal to the number of sequential separators. To deserialize these values later, // use to the deserialize_nested() function. //// function serialize_nested($arrayData, $separator=","){ if($separator == ""){return implode($separator, $arrayData);} // not recommended. the deserializer won't work on this. $retVal = ""; foreach($arrayData as $currValue){ $retVal .= (($retVal=="")?"":$separator); if(is_array($currValue)){ $retVal .= serialize_nested($currValue, $separator.substr($separator,0,1)); } else { $retVal .= $currValue; } } return $retVal; } // end serialize_nested() //// // Returns true if all of suffix is found at the end of haystack. //// function endsWith($haystack, $suffix){ return ((strlen($haystack)>=strlen($suffix)) && (substr($haystack, strlen($haystack)-strlen($suffix))==$suffix)); } // end endsWith(...) //// // Takes a date and and returns the time since that date in years (aka age) //// function getAgeFromDob($dateStr){ $daysInYear = 365.242199; // Takes into account leap-years, etc. $secondsInYear = (60*60*24*$daysInYear); // seconds*minutes*hours*days return floor((strtotime('now')-strtotime($dateStr)) / $secondsInYear); } // end getAgeFromDob() //// // Returns the path from the root of the server to the current script. This is useful in letting the system function even // if it is put into a subdirectory on a server. //// function getCodePath(){ $codePath = preg_replace("/^(.*)\/[^\/]*$/", "$1", $_SERVER['SCRIPT_NAME']); // directory of this file. $codePath = str_replace(" ", "%20", $codePath); // Subdirectories that are okay to enter into. These will be chopped off of the path of the running script. // WARNING: It is not recommended to run the app in a root directory which is the same as any of these. For instance, do not run the app in a directory called "/admin". $okSubDirs = array( "/admin" ); foreach($okSubDirs as $dirName){ if(endsWith($codePath, $dirName)){ $codePath = substr($codePath, 0, strlen($codePath) - strlen($dirName)); break; // once we've found one match, don't keep searching } } return $codePath; } // end getCodePath() //// // Returns the domain that the current site is running on (but not the www if that is the subdomain - this helps normalize results). //// function getDomain(){ $retVal = $_SERVER['SERVER_NAME']; while("www." == substr($retVal, 0, 4)){ $retVal = substr($retVal, 4); } $retVal = strtolower($retVal); return $retVal; } // end getDomain() //// // Returns all allowed file-extensions. There are some weird-looking extras to account // for IE's weird mimetypes. //// function getImageExts(){ return array("png", "bmp", "gif", "jpg", "jpeg", "tif", "tiff", "pjpeg", "pjpg", "x-png", "x-bmp"); } // end getImageTypes() //// // Returns mime-types of allowed image types separated by commas. //// function getImageMimes(){ return "image/".implode(",image/", getImageExts()); } // end getImageMimes() //// // Returns the value set in the post array, or empty string on failure. //// function getPost($key, $default=''){ return ((isset($_POST[$key]))?$_POST[$key]:$default); } // end getPost() //// // Returns the value set in the data array, or empty string on failure. //// function getVal($dataArray, $key, $default=''){ return ((isset($dataArray[$key]))?$dataArray[$key]:$default); } // end getVal // Functions for handling mysql transactions (requires that the tables are of type innodb). function mysql_begin(){return sendQuery("BEGIN");} function mysql_commit(){return sendQuery("COMMIT");} function mysql_rollback(){return sendQuery("ROLLBACK");} //// // Returns a number, the results of which are guaranteed to be unique from // its previous results for any single page-load (ie: it counts up from 1). //// GLOBAL $NEXT_SEQ_NUMBER; $NEXT_SEQ_NUMBER = 1; function nextNum(){ GLOBAL $NEXT_SEQ_NUMBER; return $NEXT_SEQ_NUMBER++; } // end nextNum //// // Scales the provided width and height (by reference) to make them fit inside of the default // MAX_WIDTH and MAX_HEIGHT for the media area of the page. //// function scaleMedia(&$width, &$height){ $MAX_WIDTH = 500; $MAX_HEIGHT = 900; if($width > $MAX_WIDTH){ $newWidth = ($MAX_WIDTH * $width / $width); $newHeight = ($MAX_WIDTH * $height / $width); $width = $newWidth; $height = $newHeight; } if($height > $MAX_HEIGHT){ $newWidth = ($MAX_HEIGHT * $width / $height); $newHeight = ($MAX_HEIGHT * $height / $height); $width = $newWidth; $height = $newHeight; } } // end scaleMedia(...) //// // Returns true if all of prefix is found at the beginning of haystack. //// function startsWith($haystack, $prefix){ return (strpos($haystack, $prefix) === 0); } // end startsWith(...) ?>