File: /home/posscale/subdomains/ccm/includes/php/functions.inc.php
<?
function sec2hms ($sec, $padHours = false)
{
// holds formatted string
$hms = "";
// there are 3600 seconds in an hour, so if we
// divide total seconds by 3600 and throw away
// the remainder, we've got the number of hours
$hours = intval(intval($sec) / 3600);
// add to $hms, with a leading 0 if asked for
$hms .= ($padHours)
? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
: $hours. ':';
// dividing the total seconds by 60 will give us
// the number of minutes, but we're interested in
// minutes past the hour: to get that, we need to
// divide by 60 again and keep the remainder
$minutes = intval(($sec / 60) % 60);
// then add to $hms (with a leading 0 if needed)
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
// seconds are simple - just divide the total
// seconds by 60 and keep the remainder
$seconds = intval($sec % 60);
// add to $hms, again with a leading 0 if needed
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
// done!
return $hms;
}
function str2search($search_cols, $strings)
{
$and_str = true;
foreach($strings as $str)
{
if($and_str)
{
$where .= " (";
$and_str = false;
}
else
{
$where .= " AND (";
}
$or_str = true;
foreach($search_cols as $col)
{
if($or_str)
{
$where .= "`$col` LIKE '%$str%'";
$or_str = false;
}
else
{
$where .= " OR `$col` LIKE '%$str%'";
}
}
$where .= ")";
}
return $where;
}
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
function redir($path)
{
header('location: '.$path);
exit();
}
function db_insert($table, $data, $debug=FALSE)
{
$new_id = next_id($table);
$query = "INSERT INTO `$table` SET";
foreach($data as $attr=>$val)
{
if($attr!="action" && $attr!="id" && $attr!="password" && $attr!="_")
{
$query .= " `".addslashes($attr)."`='".addslashes($val)."',";
}
elseif($attr=="password" && !empty($val))
{
$query .= " `".addslashes($attr)."`='".password($val)."',";
}
}
if($table!='user')
{
$query .= " `user_id`='".$_SESSION['user']['id'][0]."',";
}
$query .= " `id`='".$new_id."'";
if($debug)
{
die($query);
}
db_query($query, 0);
return $new_id;
}
function db_update($table, $data, $debug=FALSE)
{
$query = "UPDATE `$table` SET";
foreach($data as $attr=>$val)
{
if($attr!="action" && $attr!="id" && $attr!="password" && $attr!="_")
{
$query .= " `".addslashes($attr)."`='".addslashes($val)."',";
}
elseif($attr=="password" && !empty($val))
{
$query .= " `".addslashes($attr)."`='".password($val)."',";
}
}
if($table!='user')
{
$query .= " `user_id`='".$_SESSION['user']['id'][0]."',";
}
// strip extra comma
$query = substr($query, 0, -1);
$query .= " WHERE `id`='".addslashes($data['id'])."'";
//echo $query;
if($debug)
{
die($query);
}
db_query($query, 0);
}
function url_trailing_slash()
{
// make sure that there is a trailing "/"
if(!empty($_GET['uri']))
{
if (!preg_match('/\/$/', $_GET['uri']))
{
header("Location: "._BASE_URL."/".$_GET['uri']."/");
exit();
}
}
}
function br2nl($text)
{
return preg_replace('/<br\\\\s*?\\/??>/i', "\\n", $text);
}
function print_array($arr, $depth=0)
{
?>
<div style="font-family:'Courier New'; font-size:12px;" align="left">
<?
foreach ($arr as $key => $data)
{
echo str_repeat(" ",$depth);
if (is_array($data))
{
// If dealing with another array, recurse and process
if(is_numeric($key))
{
echo "[<font color='#FF0000'>".$key."</font>]";
}
else
{
echo "[<font color='#006600'>'".$key."'</font>]";
}
$depth++;
print_array($data, $depth);
$depth--;
}
else
{
// Make sure to clean up any HTML for display
$cleandata = htmlentities($data);
if(is_numeric($key))
{
echo "[<font color='#FF0000'>".$key."</font>]";
}
else
{
echo "[<font color='#006600'>'".$key."'</font>]";
}
if(is_numeric($cleandata))
{
echo "<font color='#FF00FF'><b>=</b></font><font color='#FF0000'><b>".$cleandata."</b></font><br>";
}
else
{
echo "<font color='#FF00FF'><b>=</b></font><font color='#006600'><b>'".$cleandata."</b>'</font><br>";
}
}
}
?>
</div>
<?
}
function db_query($query, $return=1, $db_host=_DB_HOST, $db_user=_DB_USER, $db_pass=_DB_PASS, $db_name=_DB_NAME)
{
// connect to the database
mysql_connect($db_host, $db_user, $db_pass);
@mysql_select_db($db_name) or die();
// if the return result variable is set to 1 (TRUE)
if($return==1)
{
// run the query, and put the result into a variable
$result=mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
$sql_error=mysql_error();
$row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
//$row = mysql_fetch_array($result);
$i=0;
while($i<mysql_num_fields($result))
{
$field_name = mysql_fetch_field($result, $i);
$f_names[$field_name->name]=$field_name->name;
$i++;
}
foreach($f_names as $arr_field_name)
{
$i=0;
while($i<mysql_num_rows($result))
{
$row[$arr_field_name][$i] = stripslashes(mysql_result($result, $i, $arr_field_name));
$i++;
}
}
$row['mysql_num_rows'] = mysql_num_rows($result);
// then return the result variable as an array
return $row;
}
}
else
{
// else just run the query
mysql_query($query);
}
// close the database connection
mysql_close();
}
function db_query_1($query, $db_host=_DB_HOST, $db_user=_DB_USER, $db_pass=_DB_PASS, $db_name=_DB_NAME)
{
// connect to the database
mysql_connect(_DB_HOST, _DB_USER, _DB_PASS);
@mysql_select_db(_DB_NAME) or die("");
// run the query, and put the result into a variable
$result=mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
$sql_error=mysql_error();
$row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
if(mysql_num_rows($result)>0)
{
return stripslashes(mysql_result($result,0,0));
}
}
}
function next_id($table)
{
$id = db_query_1("SELECT `id`
FROM `id`
WHERE `table` ='".$table."'")+1;
db_query("UPDATE `id`
SET `id` ='".$id."'
WHERE `table` ='".$table."'", 0);
return $id;
}
function db($data)
{
return addslashes($data);
}
function password($password)
{
return md5(md5(md5($password)));
}
function truncate($str, $pre_length=10, $post_length=10, $new_text='...', $abbr=false)
{
if (strlen($str) > ($post_length + $pre_length))
{
if(!empty($post_length))
{
$post_text = substr($str, -$post_length);
}
if(!empty($pre_length))
{
$pre_text = substr($str,0,$pre_length);
}
if($abbr)
{
$pre_text = "<acronym title='".$str."'>".$pre_text;
$post_text = $post_text."</acronym>";
}
return $pre_text.$new_text.$post_text;
}
else
{
return $str;
}
}
// Parse a CSV data to a associated 2D array
function delimited_to_array($data, $row="\n", $col=',')
{
// output
$csv = array();
$line = array();
$fieldnames = array();
$got_fieldnames = false;
$escaped = false; // Flag: escape char
$quoted = false; // Flag: quoted string
$buffer = ''; // Buffer (quoted values)
$junk = ''; // Junk buffer (unquoted values)
$fieldname_index = 0;
for($i = 0; $i < strlen($data); $i++)
{
$char = $data[$i];
if($quoted)
{
if(($char == '\\') && ($escaped === false))
{
// Set flags
$escaped = true;
}
elseif(($char == '"') && ($escaped === false))
{
// Set flags
$quoted = false;
$escaped = false;
}
else
{
// Add char to buffer
$buffer .= $char;
// Set flags
$escaped = false;
}
}
else
{
if($char == $row) // Start a new line
{
if(strlen($buffer) > 0)
{
// Add buffer to line
if($got_fieldnames)
{
$line[$fieldnames[$fieldname_index]] = $buffer;
$fieldname_index++;
}
else
{
$fieldnames[] = $buffer;
}
// Clear buffer
$buffer = '';
}
else
{
$junk = trim($junk);
// Add junk to line (possible unquoted values?)
if($got_fieldnames)
{
$line[$fieldnames[$fieldname_index]] = $junk;
$fieldname_index++;
}
else
{
$fieldnames[] = $junk;
}
}
// Clear junk
$junk = '';
// Add line to CSV
if($got_fieldnames)
{
$csv[] = $line;
}
$got_fieldnames = true;
// Clear line
$line = array();
$fieldname_index = 0;
}
elseif($char == '"') // Start new value
{
// Set flags
$quoted = true;
}
elseif($char == $col)
{
if(strlen($buffer) > 0)
{
// Add buffer to line
if($got_fieldnames)
{
$line[$fieldnames[$fieldname_index]] = $buffer;
$fieldname_index++;
}
else
{
$fieldnames[] = $buffer;
}
// Clear buffer
$buffer = '';
}
else
{
$junk = trim($junk);
// Add junk to line (possible unquoted values?)
if($got_fieldnames)
{
$line[$fieldnames[$fieldname_index]] = $junk;
$fieldname_index++;
}
else
{
$fieldnames[] = $junk;
}
}
// Clear junk
$junk = '';
}
else // Add to junk char
{
$junk .= $char;
}
}
}
return $csv;
}
?>