286 lines
9.0 KiB
PHP
286 lines
9.0 KiB
PHP
<?php
|
|
namespace ktk\MediaServe;
|
|
session_start();
|
|
|
|
class MediaServe {
|
|
static public $modules = array();
|
|
static public $errors = array();
|
|
static public $warnings = array();
|
|
static private $storage = array();
|
|
/*
|
|
================================
|
|
static function loadModule($name)
|
|
|
|
This function takes in a String and attempts to load a Module by that $name. Looks for "modules/$name.php"
|
|
|
|
Params:
|
|
$name - Name of the Module
|
|
|
|
Returns:
|
|
FALSE if Module does not exist
|
|
TRUE if Module does exist
|
|
================================
|
|
*/
|
|
static function loadModule($name) {
|
|
if (file_exists('modules/'.$name.'.php')) {
|
|
include_once 'modules/'.$name.'.php';
|
|
array_push(self::$modules, $name);
|
|
return TRUE;
|
|
} else {
|
|
self::reportError(__CLASS__, __FUNCTION__, "Module ".$name." does not exist!");
|
|
return FALSE;
|
|
}
|
|
}
|
|
/*
|
|
================================
|
|
static function checkModule($name)
|
|
|
|
This function checks by name if a Module is loaded.
|
|
|
|
Params:
|
|
$name - name of Module, as per loadModule
|
|
|
|
Returns:
|
|
FALSE if not loaded
|
|
TRUE if loaded
|
|
================================
|
|
*/
|
|
static function checkModule($name) {
|
|
return array_search($name, self::$modules);
|
|
}
|
|
/*
|
|
================================
|
|
static function initProgram()
|
|
|
|
This function checks for and calls each Module's initProgram member function.
|
|
================================
|
|
*/
|
|
static function initProgram() {
|
|
foreach(self::$modules as $module) {
|
|
if (method_exists('ktk\\MediaServe\\'.$module, 'onInit')) {
|
|
call_user_func('ktk\\MediaServe\\'.$module.'::onInit');
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
================================
|
|
static function processProgram()
|
|
|
|
This function first checks for POST or GET Module commands, and if found, attempts to call them, passing parameters as needed. Thereafter, it runs through every Module loaded and executes the Module's onProcess method if it exists.
|
|
|
|
POST/GET parameter(s) can be declared in a single variable syntax, e.g., "p=value", or in array style syntax, e.g., "p[]=value1&p[]=value2". Parameters are passed to functions in the order they are provided.
|
|
================================
|
|
*/
|
|
static function processProgram() {
|
|
// check for module commands and call them if so
|
|
if (isset($_POST['m'])) {
|
|
if (isset($_POST['c'])) {
|
|
if (method_exists('ktk\\MediaServe\\'.$_POST['m'], $_POST['c'])) {
|
|
if (is_array($_POST['p'])) {
|
|
call_user_func_array('ktk\\MediaServe\\'.$_POST['m'].'::'.$_POST['c'], $_POST['p']);
|
|
} else {
|
|
call_user_func('ktk\\MediaServe\\'.$_POST['m'].'::'.$_POST['c'], $_POST['p']);
|
|
}
|
|
}
|
|
}
|
|
} else if (isset($_GET['m'])) {
|
|
if (isset($_GET['c'])) {
|
|
if (method_exists('ktk\\MediaServe\\'.$_GET['m'], $_GET['c'])) {
|
|
if (is_array($_GET['p'])) {
|
|
call_user_func_array('ktk\\MediaServe\\'.$_GET['m'].'::'.$_GET['c'], $_GET['p']);
|
|
} else {
|
|
call_user_func('ktk\\MediaServe\\'.$_GET['m'].'::'.$_GET['c'], $_GET['p']);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
foreach(self::$modules as $module) {
|
|
if (method_exists('ktk\\MediaServe\\'.$module, 'onProcess')) {
|
|
call_user_func('ktk\\MediaServe\\'.$module.'::onProcess');
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
================================
|
|
static function renderCss()
|
|
|
|
This function generates and outputs HTML CSS tags for MediaServe and, if existing, each Module's CSS. CSS files are looked for in the "modules/" directory as a .css file matching the Module's name, i.e., "modules/FileBrowser.css". This function is intended to be called during the <head> section of HTML Document generation.
|
|
================================
|
|
*/
|
|
static function renderCss() {
|
|
echo ' <link rel="stylesheet" href="main.css" type="text/css" media="screen" charset="utf-8">'.PHP_EOL;
|
|
foreach(self::$modules as $module) {
|
|
if (file_exists('modules/'.$module.'.css')) {
|
|
echo ' <link rel="stylesheet" href="modules/'.$module.'.css" type="text/css" media="screen" charset="utf-8">'.PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
================================
|
|
static function renderProgram()
|
|
|
|
This function checks for and calls each Module's onRender member function.
|
|
================================
|
|
*/
|
|
static function renderProgram() {
|
|
foreach(self::$modules as $module) {
|
|
if (method_exists('ktk\\MediaServe\\'.$module, 'onRender')) {
|
|
call_user_func('ktk\\MediaServe\\'.$module.'::onRender');
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
================================
|
|
static function closeProgram()
|
|
|
|
This function checks for and calls each Module's onRender member function.
|
|
================================
|
|
*/
|
|
static function closeProgram() {
|
|
foreach(self::$modules as $module) {
|
|
if (method_exists('ktk\\MediaServe\\'.$module, 'onClose')) {
|
|
call_user_func('ktk\\MediaServe\\'.$module.'::onClose');
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
================================
|
|
static function reportError($context, $subcontext, $error)
|
|
|
|
This function is used to report program/Module errors. The recommended calling style is:
|
|
MediaServer::reportError(__CLASS__, __FUNCTION__, "A description of the error");
|
|
|
|
Params:
|
|
$context - context of the error, generally __CLASS__
|
|
$subcontext - sub-context of the error, generally __FUNCTION__
|
|
$error - String describing the error
|
|
|
|
TODO: Probably should combine reportError and reportWarning into the same function, wherein errors can be classified by the user.
|
|
================================
|
|
*/
|
|
static function reportError($context, $subcontext, $error) {
|
|
if(!self::$errors[$context."::".$subcontext])
|
|
self::$errors[$context."::".$subcontext] = array();
|
|
array_push(self::$errors[$context."::".$subcontext], $error);
|
|
}
|
|
|
|
static function reportWarning($context, $subcontext, $warning) {
|
|
if(!self::$warnings[$context."::".$subcontext])
|
|
self::$warnings[$context."::".$subcontext] = array();
|
|
array_push(self::$warnings[$context."::".$subcontext], $warning);
|
|
}
|
|
|
|
/*
|
|
================================
|
|
static function loadConf($file)
|
|
|
|
This function takes a configuration name parameter and attempts to load a configuration .ini file within the conf/ sub-directory, i.e., "conf/some_config.ini". It returns an Array of the configuration if existing, or an empty Array otherwise.
|
|
|
|
Params:
|
|
$file - name of configuration to load, without any extensions
|
|
|
|
Returns:
|
|
Empty Array on failure
|
|
Array on success
|
|
================================
|
|
*/
|
|
static function loadConf($file) {
|
|
if (file_exists('conf/'.$file.'.ini')) {
|
|
return parse_ini_file('conf/'.$file.'.ini', 1);
|
|
} else {
|
|
self::reportWarning(__CLASS__, __FUNCTION__, "Could not load ".'conf/'.$file.'.ini');
|
|
}
|
|
return array();
|
|
}
|
|
|
|
/*
|
|
================================
|
|
static function saveConf($file, $data)
|
|
|
|
This function takes in a config name, as per loadConf, and attempts to write the passed Array as an .ini file within the conf/ sub-directory, i.e., "conf/some_config.ini".
|
|
|
|
Params:
|
|
$file - Configuration to save to, without extensions
|
|
$data - Array of data to save
|
|
|
|
Returns:
|
|
FALSE on failure
|
|
TRUE on success
|
|
================================
|
|
*/
|
|
static function saveConf($file, $data) {
|
|
if ($fp = fopen('conf/'.$file.'.ini', 'w')) {
|
|
self::writeData_r($fp, $data);
|
|
fclose($fp);
|
|
} else {
|
|
MediaServe::reportError(__CLASS__, __FUNCTION__, "Could not write to \"".$data."\", please check your permissions!");
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
/*
|
|
================================
|
|
static function writeData_r($handle, $value)
|
|
|
|
This is an internal recursive function used to write multi-dimensional arrays into PHP INI format.
|
|
|
|
Params:
|
|
$handle - file handle to write to
|
|
$value - Array or value to write
|
|
================================
|
|
*/
|
|
static function writeData_r($handle, $value) {
|
|
foreach ($data as $key=>$value) {
|
|
if (is_array($value)) {
|
|
fwrite($handle, '['.$key.']'.PHP_EOL);
|
|
self::writeData_r($handle, $value);
|
|
} else {
|
|
fwrite($handle, $key.'='.$value.PHP_EOL);
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
================================
|
|
static function storeData($var, $data)
|
|
|
|
This function is used to store SESSION-specific data. It is reset when the client closes the connection or clears session data.
|
|
|
|
Params:
|
|
$var - variable name to store
|
|
$data - data to store
|
|
|
|
Returns:
|
|
Pointer to stored variable
|
|
================================
|
|
*/
|
|
static function &storeData($var, $data) {
|
|
$_SESSION[$var] = $data;
|
|
return $_SESSION[$var];
|
|
}
|
|
/*
|
|
================================
|
|
static function &getData($data)
|
|
|
|
This function returns a pointer to some SESSION-specific data.
|
|
|
|
Params:
|
|
$var - variable name to snag
|
|
|
|
Returns:
|
|
NULL on failure
|
|
Pointer on successful data load
|
|
================================
|
|
*/
|
|
static function &getData($var) {
|
|
if (isset($_SESSION[$var])) {
|
|
return $_SESSION[$var];
|
|
} else {
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
?>
|