Source for file common_class.php
Documentation is available at common_class.php
* The common class, used in various files.
* This file contains the common class, a container for commonly used functions.
* This file is part of HRDIODB.
* HRDIODB 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.
* HRDIODB 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 HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* A common class containing functions used in various files.
* Returns value for key in first or second array.
* This function returns the value for a key from the first or from the
* second array. The first array will be looked at first, the second array
* will be looked at second. If the key exists for the array, its value will
* be returned. The value will be stripped of slashes, trimmed and any HTML
* special characters will be encoded. The second array is optional. If
* neither array contains the key, an empty string will be returned. This
* function works really well for getting the default value of an input
* field from either the $_POST array or the stored data.
* @param string $key The key to look up
* @param array $array1 The first array
* @param array $array2 The optional second array
* @return string The value for the key or an empty string
function get_value($key, $array1, $array2 = NULL) {
if (is_array($array1) && isset ($array1[$key])) {
} else if (is_array($array2) && isset ($array2[$key])) {
* Prints out table cell for specific coverage.
* The function prints out a table cell for a specific coverage type. It is
* used to quickly create the composition table based on the standardized
* @param day $day The reference to a uars_day object
* @param string $str A part of the field name: cov_{$str}_{$suffix}
$suffixes = array("WDA", "WDD", "WNA", "WND",
"CDA", "CDD", "CNA", "CND");
foreach ($suffixes as $suffix) {
print "<td class=\"data\" align=right>". @$day->data["cov_{$str}_{$suffix}"]. "</td>\n";
* Prints a definition link.
* The function prints out a link which calls the JavaScript pop() routine.
* It is used to pop a definition for a data label, which is stored in a
* static JavaScript associative array.
* @param string $string The link string
* @param string $key The key into the JavaScript array of definitions
print "<a href=\"javascript:void(0);\" class=\"def\"
onClick=\"return pop(this, event, '{$key}');\">$string</a>";
* Returns the "human readable" form of a file size.
* This function returns the "human readable" form of the size of a file. It
* is based on a function posted in the comments of the
* {@link PHP_MANUAL#filesize} function.
* @param integer $size File size in bytes
* @param integer $decimals Number of decimal places to use for rounding
* @return string Size in "human readable" form
$suffix = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB','NB','DB');
while ($size >= 1024 && ($i < count($suffix) - 1)){
return round($size, $decimals). ' '. $suffix[$i];
* Converts Day of Year to UARS day.
* This function takes the year and day of year in the format 'YYYY-DDD'
* and converts them to the UARS day.
* @param string $doy The year and day of year in the 'YYYY-DDD' format
* @return integer The UARS day
$year_day = split("-",$doy);
return error ("Year - Day of Year requires the following format: YYYY-DDD");
$stamp = mktime(0,0,0,1,$day,$year);
return (($stamp - $day1_ts) / 24 / 60 / 60) + 1;
$year_day = split("-",$doy);
if (count($year_day) != 2) {
return error ("Year - Day of Year requires the following format: YYYY-DDD");
$stamp = mktime(0,0,0,1,$day,$year);
return date("Y-m-d H:i:s",$timestamp);
return $day1_ts + (($uars_day- 1) * 24 * 60 * 60);
* Converts a calendar date string to UARS day.
* This function takes a calendar date string and converts it to the UARS
* day. The date string has to be compatible with the PHP
* {@link PHP_MANUAL#strtotime} function.
* @param string $calendar The calendar date string
* @return integer The UARS day
return error ("Your date format could not be understood: $calendar");
return floor((($stamp - $day1_ts) / 24 / 60 / 60) + 1);
* Print select dropdown options.
* This is a helper function to print out HTML option tags for a select
* @param array Array of values and descriptions
* @param mixed The selected value
foreach($values as $value => $description) {
print "<option value=\"$value\" ";
print ">$description</option>";
* Returns the run mode of the script.
* This function detects and returns the run mode of the script. If the
* the script is being executed at the command line (offline), it returns
* TRUE. If the script is being accessed via HTTP (online), it returns
* @return TRUE|FALSETRUE if offline, FALSE if online.
return !isset ($_SERVER['HTTP_HOST']);
* Performs an HTTP header redirect.
* This function performs an HTTP header redirect, taking the user to the
* specified page. The exit() is needed in order to make sure that the code
* below the redirect isn't executed, see {@link PHP_MANUAL#header}.
* @param string $page The page to redirect to
|