<?php
/**
* This class provides access to system commands & logging features.
*
*/
require_once('config.php');
class SystemObject {
private static $instance = null; // holds the singleton object
private $logfile = null;
protected $all_messages = ''; // in case we want to dump all messages to browser, email, etc
protected function __construct() {
// open the log file for appending - only if logging level is nonzero.
if (LOG_LEVEL) {
$this->logfile = fopen(LOG_DIR.'/'.EXECUTION_CONTEXT, 'a');
}
}
/**
* singleton creator
*/
public static final function getInstance() {
if (!self::$instance) {
self::$instance = new SystemObject();
}
return self::$instance;
}
/**
* Runs a shell command on the host system
*
* @param string $cmd the command to be run
* @param string $user the user account under which to run the command
* @param boolean $fail_on_nonzero set to true if the command will return a nonzero return value (i.e. the '$?' variable on *NIX) to indicate failure
* @param boolean $merge_streams if false, stderr is dumped into apache's error log. true, it's merged with other output (LOG_LEVEL can be set to put it into orion log)
* @return mixed array of result lines on success; false on error
*/
function do_command($cmd, $user = null, $fail_on_nonzero = true, $merge_streams = true) {
if ($user) $cmd = "sudo -u $user $cmd";
if ($merge_streams) $cmd .= ' 2>&1';
$this->logmessage("RUNNING COMMAND: $cmd", 2);
exec($cmd, $res, $return_val);
if ($return_val != 0) $this->logmessage("COMMAND FAILED:$cmd\n (return value $return_val)");
$results_string =
str_replace("\n","\n ", // indent subsequent lines
//preg_replace("/^(array \(|\))/ms","", // remove the array( ... ) format
var_export($res,true));
$this->logmessage("results returned: $results_string", 2);
if ($return_val != 0 && $fail_on_nonzero) return false; // failure
return $res; // success
}
/**
* Throw an error and exit.
*
* @param string $msg the message to include in the error
*/
function error_exit($msg) {
// $this->logmessage($msg, 1, false); // necessary?
throw new ErrorException($msg);
}
/**
* Log a message in the system log and/or standard output, depending on application settings.
*
* @param string $msg the message to log
* @param int $level set a higher level for lower priority messages. Messages with a priority above DEBUG_LEVEL or LOG_LEVEL will be filtered out from those outputs respectively.
* @param boolean $display set to false to disable standard output, regardless of DEBUG_LEVEL setting.
*/
function logmessage($msg, $level = 1, $display = true) {
$date = date('c');
$msg = "$date $msg\n";
// concatenate all_messages string (do this before modifying for web)
$this->all_messages .= $msg;
// output to browser or terminal
if ($level <= DEBUG_LEVEL) {
if (EXECUTION_CONTEXT == 'web') {
if (DEBUG_LEVEL < 2) $msg = "\n<!--$msg-->";
else $msg = "\n$msg<br>";
}
if ($display) echo "$msg";
}
// output to log file
if ($this->logfile && $level <= LOG_LEVEL) {
fwrite($this->logfile, $msg);
}
}
/**
* dumps entire contents of $all_messages string, which stores everything for a given page load or script run
*/
function getAllMessages() {
return "\n{$this->all_messages}\n";
}
/**
* Gets all the subdirectories of a given directory on the filesystem.
*
* @param string $parent_dir
* @param boolean $include_hidden set to true to include hidden subdirectories (i.e. beginning with a period)
* @param boolean $include_selfs set to true to include the self-referencing '.' and '..' directory listings
* @return array returns array of strings - full name of each child directory
*/
function get_subdirs($parent_dir, $include_hidden = false, $include_selfs = false) {
$child_dirs = array();
$parent_dir_obj = opendir($parent_dir);
while($this_dir = readdir($parent_dir_obj)) {
// filter out the . and .. dir's, and hidden dir's - if necessary
if(
($include_selfs || ($this_dir != '.' && $this_dir != '..'))
&& ($include_hidden || substr($this_dir,0,1) != '.')
&& filetype($parent_dir.$this_dir) == 'dir'
) {
// Assign all values to an array to allow sorting
$child_dirs[]=$this_dir;
}
}
closedir($parent_dir_obj);
return $child_dirs;
}
public function getMimeType($filename) {
$cmd = MIMETYPE_COMMAND.escapeshellarg($filename);
$mimetype = $this->do_command($cmd);
if ($mimetype) $mimetype = $mimetype[0];
else $mimetype = '';
if (stristr($mimetype,';')) {
$mimetype = array_shift(explode(';',$mimetype));
}
return $mimetype;
}
public function getFileSize($filename) {
return filesize($filename);
}
public function getSystemUserList() {
$userlist = $this->do_command(USERLIST_COMMAND);
return $userlist;
}
}
?>