<?php
// This file is part of the Huygens Remote Manager
// Copyright and license notice: see license.txt
//!---------------------------------------------------------
// @function
// @desc
// @param
// @return
//!---------------------------------------------------------
function assign_rand_value($num) {
// accepts 1 - 36
switch($num) {
case "1":
$rand_value = "a";
break;
case "2":
$rand_value = "b";
break;
case "3":
$rand_value = "c";
break;
case "4":
$rand_value = "d";
break;
case "5":
$rand_value = "e";
break;
case "6":
$rand_value = "f";
break;
case "7":
$rand_value = "g";
break;
case "8":
$rand_value = "h";
break;
case "9":
$rand_value = "i";
break;
case "10":
$rand_value = "j";
break;
case "11":
$rand_value = "k";
break;
case "12":
$rand_value = "l";
break;
case "13":
$rand_value = "m";
break;
case "14":
$rand_value = "n";
break;
case "15":
$rand_value = "o";
break;
case "16":
$rand_value = "p";
break;
case "17":
$rand_value = "q";
break;
case "18":
$rand_value = "r";
break;
case "19":
$rand_value = "s";
break;
case "20":
$rand_value = "t";
break;
case "21":
$rand_value = "u";
break;
case "22":
$rand_value = "v";
break;
case "23":
$rand_value = "w";
break;
case "24":
$rand_value = "x";
break;
case "25":
$rand_value = "y";
break;
case "26":
$rand_value = "z";
break;
case "27":
$rand_value = "0";
break;
case "28":
$rand_value = "1";
break;
case "29":
$rand_value = "2";
break;
case "30":
$rand_value = "3";
break;
case "31":
$rand_value = "4";
break;
case "32":
$rand_value = "5";
break;
case "33":
$rand_value = "6";
break;
case "34":
$rand_value = "7";
break;
case "35":
$rand_value = "8";
break;
case "36":
$rand_value = "9";
break;
}
return $rand_value;
}
//!---------------------------------------------------------
// @function
// @desc
// @param
// @return
//!---------------------------------------------------------
function get_rand_id($length) {
if($length>0) {
$rand_id="";
for($i=1; $i<=$length; $i++) {
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,36);
$rand_id .= assign_rand_value($num);
}
}
return $rand_id;
}
//!---------------------------------------------------------
// @function escapeJavaScript
// @desc Escapes html strings generated in PHP so that can be used as
// arguments in JavaScript functions.
// parameters. This local hucore doesn't need to have a license!
// @param A html string.
// @return Conveniently formated string.
//!---------------------------------------------------------
function escapeJavaScript ($str) {
$str = str_replace("'", "\'", $str);
$str = str_replace("\n", "\\n", $str);
$str = str_replace('"', "'+String.fromCharCode(34)+'", $str);
return $str;
}
//!---------------------------------------------------------
// @function huCoreTools
// @desc Calls a local hucore (that must be in the $PATH) to
// execute some freeware image processing tools, or report
// parameters. This local hucore doesn't need to have a license!
// @param $tool is the procedure in scripts/hucore.tcl to be executed.
// $options are extra command line options to send to that script
// @return An array with all stdout lines
//!---------------------------------------------------------
function huCoreTools ($tool, $options) {
global $hrm_path, $local_huygens_core;
if (! isset($local_huygens_core) ) {
echo "Huygens tools can only work if you define a variable ".
"'local_huygens_core' in the configuration files pointing to a local ".
"hucore. Administrator: see hrm_config.inc.sample.";
return;
}
$cmd = "$local_huygens_core -noExecLog -checkUpdates disable ".
"-task \"$hrm_path/scripts/hucore.tcl\" ".
"-huCoreTcl \"$hrm_path/scripts/hucore.tcl\" ".
"-tool $tool $options 2>&1";
$answer = exec($cmd, $output, $result);
# printDebug ($cmd, $output, "res: $result ans: $answer");
if ($result == 0) {
$begin = array_search("BEGIN PROC", $output);
if ($begin) {
$ret = array_slice($output, $begin);
return $ret;
}
}
$more = 0;
echo "<pre>ERROR with $cmd\n";
if ($result != 0) {
echo "$answer\n";
}
foreach ( $output as $line ) {
if ($more > 0) {
echo ("$line\n");
$more -= 1;
if ( $more == 0 ) echo ("...\n");
}
if (stristr($line, "error")) {
echo ("$line\n");
$more = 2;
}
}
echo "</pre>";
return NULL;
}
//!---------------------------------------------------------
// @function askHuCore
// @desc A wrapper around huCoreTools to retrieve an array,
// which is 'calculated' by hucore in the background.
// @param $tool is the procedure in scripts/hucore.tcl to be executed.
// $options are extra command line options to send to that script
// hucore output. By default, it uses the same tool name.
// @return The requested array.
//!---------------------------------------------------------
function askHuCore ($tool, $options = "") {
$answer = huCoreTools( $tool, $options );
if (! $answer ) return "(nothing)";
# printDebug ($answer);
$lines = count($answer);
$msg = "";
$ret = "";
$sep = "";
$retArr = array();
$array_key = NULL;
$ok = true;
for ($i = 0; $i < $lines; $i++ ) {
$key = $answer[$i];
switch ($key) {
case "ERROR":
$i ++;
$retArr['error'][] = $answer[$i];
$msg .= $answer[$i]."<br>";
$ok = false;
break;
case "REPORT":
$i ++;
$retArr['report'][] = $answer[$i];
echo $answer[$i]."\n";
ob_flush();
flush();
break;
case "KEY":
$i ++;
$array_key = $answer[$i];
break;
case "VALUE":
if ($array_key) {
$i ++;
$retArr[$array_key] = $answer[$i];
}
break;
default :
break;
}
}
if ( $msg != "" ) { echo $msg; }
return $retArr;
}
//!---------------------------------------------------------
// @function report
// @desc Write text to the logfile if level is not
// bigger than log_verbosity. If logfile becomes
// to big it is renamed and started over.
// @param text String The text to be logged
// @param level Int The log level of the text
// @return Void
//!---------------------------------------------------------
function report($text, $level) {
global $log_verbosity;
global $hrm_path;
global $logdir;
global $logfile;
global $logfile_max_size;
$text = date("Y-m-d H:i:s"). " ".$text;
$logpath = $logdir."/".$logfile;
if ($log_verbosity == 0)
return True;
if (file_exists($logpath)
&& (filesize($logpath) > $logfile_max_size * 1000 * 1000)) {
if (file_exists($logpath . ".old")) {
unlink($logpath . ".old");
}
rename($logpath, $logpath . ".old");
}
$file = fopen($logpath, 'a');
if ($log_verbosity >= $level) {
fwrite($file, $text);
fwrite($file, "\n");
fflush($file);
}
fclose($file);
}
//!---------------------------------------------------------
// @function notifyRuntimeError
// @desc Sends an email to the admin to notify any message.
// It helps in debugging strange events, and in being
// aware of abnormal scheduler stops.
// @param subject String A string to be appended to the email subject.
// @param message String The body of the email.
// @return Void
//!---------------------------------------------------------
function notifyRuntimeError($subject, $message) {
global $email_sender;
global $email_admin;
$text = "Huygens Remote Manager warning:\n"
. $name . " could not be pinged on " . date("r", time());
$mail = new Mail($email_sender);
$mail->setReceiver($email_admin);
$mail->setSubject('Huygens Remote Manager - '.$subject);
$mail->setMessage($message);
$mail->send();
# No need to report to log, send() already did.
}
/* A global debugging function, that will print all its arguments whether they
* are strings, arrays or objects. This works if a global variable $debug =
* true, that can be defined in hrm_client_config.inc. Otherwise it does
* nothing. */
if ( ! function_exists('printDebug') ) {
function printDebug ( ) {
global $debug;
if ( ! $debug ) return;
$args = func_get_args();
echo "<small><kbd><font color=\"red\">Debugging: </font></kbd></small>";
foreach ($args as $item) {
if (is_object($item)) {
$msg = (array) $item;
} else {
$msg = $item;
}
if (is_array($msg)) {
echo "<pre>";
print_r($msg);
echo "</pre>";
} else {
echo "<kbd>$msg</kbd>";
}
}
}
}
//!---------------------------------------------------------
// @function getHucoreVersionAsInteger
// @desc Returns the hucore version (as set in the configuration file)
// as an integer for comparisons
// @return The version number as integer
//!---------------------------------------------------------
function getHucoreVersionAsInteger( $hucoreVersion ) {
$matches = array( );
// Is there a patch information?
$ver = preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)p([0-9]+)/",
$hucoreVersion, $matches );
if ( $ver == 0 ) {
$ver = preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)/",
$hucoreVersion, $matches );
}
// Now calculate the integer version
$version = 0;
if ( count( $matches ) != 0 ) {
$version = $matches[1] * 1000000 + $matches[2] * 10000 + $matches[3] * 100;
if ( count( $matches ) == 5 ) {
$version = $version + $matches[ 4 ];
}
}
return $version;
}
//!---------------------------------------------------------
// @function getThisPageName
// @desc Returns the name of current page
// @return string containing the name of current page
//!---------------------------------------------------------
function getThisPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
// This function transforms the php.ini notation for numbers (like '2M') to an
// integer (2*1024*1024 in this case)
function let_to_num($v){
$l = substr($v, -1);
$ret = substr($v, 0, -1);
switch(strtoupper($l)){
case 'P':
$ret *= 1024;
case 'T':
$ret *= 1024;
case 'G':
$ret *= 1024;
case 'M':
$ret *= 1024;
case 'K':
$ret *= 1024;
break;
}
return $ret;
}
// Report maximum upload size, in bytes.
function getMaxSingleUploadSize() {
$max_upload_size = min(let_to_num(ini_get('post_max_size')), let_to_num(ini_get('upload_max_filesize')));
return $max_upload_size;
}
function getMaxPostSize() {
return let_to_num(ini_get('post_max_size'));
}
function getMaxFileSize() {
return let_to_num(ini_get('upload_max_filesize'));
}
//!---------------------------------------------------------
// @function Util::readfile_chunked
// @desc To be used instead of PHP's own readfile function to read
// very large files. This should prevent memory errors.
//
// @param filename String name of the file to read
// @param retbytes boolean
//
// @see http://nl.php.net/manual/en/function.readfile.php#54295
//!---------------------------------------------------------
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>