[Openfirst-cvscommit] SF.net SVN: openfirst: [162] trunk/src/includes/functions.php
Brought to you by:
xtimg
From: <ast...@us...> - 2006-06-21 02:14:47
|
Revision: 162 Author: astronouth7303 Date: 2006-06-20 19:14:41 -0700 (Tue, 20 Jun 2006) ViewCVS: http://svn.sourceforge.net/openfirst/?rev=162&view=rev Log Message: ----------- - Did a surface-skim of the ported modules - Set of:module property where appropriate - Added bugtraq:* properties for Bugzilla integration - Mimed the files Testing mismatched EOLs Added Paths: ----------- trunk/src/includes/functions.php Added: trunk/src/includes/functions.php =================================================================== --- trunk/src/includes/functions.php (rev 0) +++ trunk/src/includes/functions.php 2006-06-21 02:14:41 UTC (rev 162) @@ -0,0 +1,251 @@ +<?php +/* + * openFIRST.base - includes/functions.php + * + * Copyright (C) 2003, + * openFIRST Project + * Original Author: Jamie Bliss <ja...@op...> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +// Purpose: Provide global functions to openFIRST + +/** Replaces standard vars in config text. + * Currently includes $BasePath, $fBasePath, + * $StylePath, $fStylePath, $ModPath, and $fModPath. + */ +function ofReplaceVariables($text, $ModuleDir = false) { + if ($ModuleDir === false) { + global $CurrentModule; + if (is_object($CurrentModule)) $ModuleDir == $CurrentModule->getDir(); + } + + global $BasePath, $fBasePath, $StylePath, $fStylePath; + + $find = array('$BasePath', + '$fBasePath', + '$StylePath', + '$fStylePath', + '$ModPath', + '$fModPath', + '$DirName' + ); + + $replace = array($BasePath, + $fBasePath, + $StylePath, + $fStylePath, + "$BasePath/$ModuleDir", + "$fBasePath/$ModuleDir", + "$ModuleDir" + ); + + return str_ireplace($find, $replace, $text); + +} + + +/** Formats the size of computer data. + * Uses the units: KB, MB, GB, etc. + * Uses 1024 definition (1 KB = 1024 B) + */ +function ofFormatSize($size) { + $base = 1024; + $units = array( + 'B', + 'KB', + 'MB', + 'GB', + 'TB', + 'PB', + 'EB', + 'ZB', + 'YB', + 'XB', + false, + 'VB' + ); + + $units = array_reverse($units, true); + reset($units); + while (list($pow, $unit) = each($units)) { + if ($unit === false) continue; + if ($size >= pow($base, $pow)) { + $unitsize = $size/pow($base, $pow); + $unitsize = rtrim($unitsize, '0'); + if (substr($unitsize, -1) == '.') $unitsize = substr($unitsize, 0, -1); + return number_format($unitsize, 2, '.', '').' '.$unit; + } + } +} + +/** Strip line comment. + * Given a string and an array of commentors (in PHP, it would be array('#', '//')), + * remove all end-of-line comments. + * Now handles multiple lines + */ +function ofStripLineComment($commentors, $text) { + $rtn = array(); + $lines = explode(array("\r\n","\n","\r"), $text); + foreach($lines as $line) { + $parts = explode($commentors, $line, 2); + $rtn[] = $parts[0]; + } + return implode(PHP_EOL, $rtn); +} + +/** Converts input to boolean + * returns true on: yes, y, true, 1 + * returns false on: no, n, false, 0 + * returns nothing if neither (return;). + * if a value is in both $moretrue and $morefalse, it is treated as true + * case-insensitive + */ +function ofConvert2Bool($text, $moretrue=array(), $morefalse=array()) { + $yesvals = $moretrue; + $yesvals[] = 'yes'; + $yesvals[] = 'y'; + $yesvals[] = 'true'; + $yesvals[] = '1'; + + $novals = $morefalse; + $novals[] = 'no'; + $novals[] = 'n'; + $novals[] = 'false'; + $novals[] = '0'; + + foreach ($yesvals as $val) { + if (strcasecmp($text, $val) == 0) return true; + } + foreach ($novals as $val) { + if (strcasecmp($text, $val) == 0) return false; + } + return; +} + +/** Returns the last key of an array. + * @param $array array The array to get the key of + */ +function ofGetLastKey($array) { + return end(array_keys($array)); +} + +/** Returns an associative array based on the given URL query. + * @todo Add arrays + */ +function ofDecodeQuery($query) { + $parts = explode('&', $query); + $rtn = array(); + foreach($parts as $part) { + $items = explode('=', $part, 2); + $name = rawurldecode($items[0]); + $value = true; + if (isset($items[1])) $value = rawurldecode($items[1]); + $rtn[$name] = $value; + } + return $rtn; +} + +/** Reverse of ofDecodeQuery() + * @todo Add arrays + */ +function ofEncodeQuery($vars) { + $rtn = array(); + foreach($vars as $name => $val) { + $str = rawurlencode($name); + if ($val !== true) { + $str .= '='.rawurlencode($val); + } + $rtn[] = $str; + } + return implode('&', $rtn); +} + +/** Uses var_dump() to output a variable, except appropriately cleaned. + */ +function ofDebugVar($var) { + ob_start(); + var_dump($var); + $text = ob_get_clean(); + echo '<pre>'; + echo htmlentities($text); + echo "</pre>\n"; +} + +/** Compares 2 module versions + * + * The versions strings are either PHP-formatted versions, or "CVS" (case + * sensitive) to mean development (use dates instead). (See version_compare() + * <http://us3.php.net/manual/en/function.version-compare.php> for the details + * as to how to format versions.) + * + * It is always assumed that dev is newer if compared to a version. + * + * @param string $left The left-hand version + * @param string $right The right-hand version + * @return mixed -1 if the left is older, 1 if the right is older, 0 if they + * are equal, and false if dates should be used (ie, they're both CVS). + */ +function ofCompareVersions($left, $right) { + if ($left == 'CVS' && $right == 'CVS') { + return false; + } + version_compare($left, $right); +} + +function ofStackTrace() { + echo '<pre class="stack-trace">'; + debug_print_backtrace(); + echo '</pre>'; +} + +/** Check magic quotes settings + */ +function ofCheckMagicQuotes() { + set_magic_quotes_runtime(false); //Back, you foul demons! Back! Back! + ini_set('magic_quotes_sybase', false); + if (get_magic_quotes_gpc()) { // Gah! + // Quite making our lives difficult!! + ofFixMagicQuotes($_GET); + ofFixMagicQuotes($_POST); + ofFixMagicQuotes($_COOKIE); + } +} + +/** Fixes magic quote munging! + */ +function ofFixMagicQuotes(&$arr) { + $sybase = (bool)ini_get('magic_quotes_sybase'); + if (count($arr) <= 0) return; + foreach( $arr as $key => $val ) { + if( is_array( $val ) ) { + ofFixMagicQuotes( $arr[$key] ); + } else if (is_string($val)) { + if ($sybase) { + // We really should just die() here and make them get a decent configuration. + $arr[$key] = str_replace("''", "'", $val); + } else { + $arr[$key] = stripslashes( $val ); + } + } + } +} +?> \ No newline at end of file Property changes on: trunk/src/includes/functions.php ___________________________________________________________________ Name: of:module + openfirst.base Name: svn:mime-type + text/x-php Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |