From 30557ffadf684038367a97d6665871997dd33342 Mon Sep 17 00:00:00 2001 From: azammitdcarf Date: Mon, 1 Dec 2008 23:42:50 +0000 Subject: [PATCH] Added VoIP watch from browser (executes background PHP file process.php) --- admin/index.php | 5 + admin/process.php | 96 ++++++++++++++ admin/voipmonitor.php | 88 +++++++++++++ config.default.php | 16 +++ database/quexs.sql | 15 +++ functions/functions.process.php | 220 ++++++++++++++++++++++++++++++++ functions/functions.voip.php | 9 +- voip/voipwatch.php | 2 +- 8 files changed, 449 insertions(+), 2 deletions(-) create mode 100644 admin/process.php create mode 100644 admin/voipmonitor.php create mode 100644 functions/functions.process.php diff --git a/admin/index.php b/admin/index.php index 4c865aa7..25169a5f 100644 --- a/admin/index.php +++ b/admin/index.php @@ -82,6 +82,11 @@ print ""; +if (VOIP_ENABLED) +{ + print "
  • " . T_("VoIP") . "

    "; + print "
  • "; +} print ""; diff --git a/admin/process.php b/admin/process.php new file mode 100644 index 00000000..36d6acfd --- /dev/null +++ b/admin/process.php @@ -0,0 +1,96 @@ + + * @copyright Deakin University 2007,2008 + * @package queXS + * @subpackage admin + * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility + * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 + * + */ + +/** + * Configuration file + */ +include ("../config.inc.php"); + +/** + * Database file + */ +include ("../db.inc.php"); + +/** + * Process + */ +include ("../functions/functions.process.php"); + +/** + * VoIP functions + */ +include("../functions/functions.voip.php"); + +/** + * Update the database with the new data from the running script + * + * @param string $buffer The data to append to the database + * @return string Return a blank string to empty the buffer + */ +function update_callback($buffer) +{ + global $process_id; + + process_append_data($process_id,"

    " . $buffer . "

    "); + + return ""; //empty buffer +} + + +//get the arguments from the command line (this process_id) +if ($argc != 2) exit(); + +$process_id = $argv[1]; + +//register an exit function which will tell the database we have ended +register_shutdown_function('end_process',$process_id); + +//all output send to database instead of stdout +ob_start('update_callback',2); + +print "Monitoring " . VOIP_SERVER; + +$t = new voipWatch(); +$t->connect(VOIP_SERVER,VOIP_ADMIN_USER,VOIP_ADMIN_PASS,true); + +if ($t->isConnected()) +{ + $t->watch($process_id); +} +else +{ + print T_("Cannot connect to VoIP Server"); +} + +ob_get_contents(); +ob_end_clean(); + +?> diff --git a/admin/voipmonitor.php b/admin/voipmonitor.php new file mode 100644 index 00000000..5ee96845 --- /dev/null +++ b/admin/voipmonitor.php @@ -0,0 +1,88 @@ + + * @copyright Deakin University 2007,2008 + * @package queXS + * @subpackage admin + * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility + * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 + * + */ + +/** + * Configuration file + */ +include ("../config.inc.php"); + +/** + * Database file + */ +include ("../db.inc.php"); + +/** + * Process + */ +include ("../functions/functions.process.php"); + +/** + * XHTML functions + */ +include("../functions/functions.xhtml.php"); + + + +if (isset($_GET['watch'])) +{ + //start watching process + start_process(realpath(dirname(__FILE__) . "/process.php")); +} + +$p = is_process_running(); + + +if ($p) +{ + if (isset($_GET['kill'])) + kill_process($p); + + xhtml_head(T_("Monitor VoIP Process"),true,false,false,false,10); + + print "

    " . T_("Running process:") . " $p

    "; + + print "

    " . T_("Note: This page will automatically refresh every 10 seconds") . "

    "; + + if (is_process_killed($p)) + print "

    " . T_("Kill signal sent: Please wait... (Note: Process will be stalled until there is activity on the VoIP Server)") . "

    "; + else + print "

    " . T_("Kill the running process") . " ". T_("(requires activity on the VoIP Server to take effect)") . "

    "; + + print process_get_data($p); +} +else +{ + xhtml_head(T_("Monitor VoIP Process")); + print "

    " . T_("Click here to begin monitoring the VoIP Process") . "

    "; +} +xhtml_foot(); + +?> diff --git a/config.default.php b/config.default.php index ea6aaa8f..3a9aeba1 100644 --- a/config.default.php +++ b/config.default.php @@ -69,6 +69,16 @@ if (!defined('VOIP_ENABLED')) define('VOIP_ENABLED',false); */ if (!defined('VOIP_SERVER')) define('VOIP_SERVER','asterisk.dcarf'); +/** + * The Asterisk server username for the monitor interface + */ +if (!defined('VOIP_ADMIN_USER')) define('VOIP_ADMIN_USER','admin'); + +/** + * The Asterisk server password for the monitor interface + */ +if (!defined('VOIP_ADMIN_PASS')) define('VOIP_ADMIN_PASS','amp111'); + /** * The meet me room id for the VOIP Server */ @@ -110,6 +120,12 @@ if (!defined('QUEXS_URL')) define('QUEXS_URL','http://' . $_SERVER['SERVER_NAME' if (!defined('DEFAULT_LOCALE')) define('DEFAULT_LOCALE','en'); +/** + * PHP Executables (for forking when running background processes) + */ +if (!defined('WINDOWS_PHP_EXEC')) define('WINDOWS_PHP_EXEC', "start /b php"); +if (!defined('PHP_EXEC')) define('PHP_EXEC', "php"); + /** * Path to ADODB */ diff --git a/database/quexs.sql b/database/quexs.sql index ebc125fa..c1a6bc66 100644 --- a/database/quexs.sql +++ b/database/quexs.sql @@ -429,6 +429,21 @@ INSERT INTO `outcome_type` VALUES(5, 'Appointments'); -- -------------------------------------------------------- +-- +-- Table structure for table `process` +-- + +CREATE TABLE `process` ( + `process_id` bigint(20) NOT NULL auto_increment, + `start` datetime NOT NULL, + `stop` datetime default NULL, + `kill` tinyint(1) NOT NULL default '0', + `data` longtext collate utf8_unicode_ci NOT NULL, + PRIMARY KEY (`process_id`) +) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE=utf8_unicode_ci; + +-- -------------------------------------------------------- + -- -- Table structure for table `questionnaire` -- diff --git a/functions/functions.process.php b/functions/functions.process.php new file mode 100644 index 00000000..b40fa114 --- /dev/null +++ b/functions/functions.process.php @@ -0,0 +1,220 @@ + + * @copyright Deakin University 2007,2008 + * @package queXS + * @subpackage functions + * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility + * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 + * + */ + +/** + * Configuration file + */ +include_once(dirname(__FILE__).'/../config.inc.php'); + +/** + * Database file + */ +include_once(dirname(__FILE__).'/../db.inc.php'); + + +/** + * Determine if a process is already running + * + * @return bool|int Return false if no process already running, else return the process_id + */ +function is_process_running() +{ + global $db; + + $sql = "SELECT `process_id` + FROM `process` + WHERE `stop` IS NULL"; + + $rs = $db->GetRow($sql); + + if (!empty($rs)) + return $rs['process_id']; + + return false; +} + +/** + * Determine if this process should be killed + * + * @param int $process_id The process id + * @return bool Return false if not to be killed, else return true + */ +function is_process_killed($process_id) +{ + global $db; + + $sql = "SELECT `process_id` + FROM `process` + WHERE `kill` = 1 + AND `stop` IS NULL + AND `process_id` = '$process_id'"; + + $rs = $db->GetRow($sql); + + if (!empty($rs)) + return true; + + return false; +} + + +/** + * Start a process + * + * @param string $filename The PHP file of the process to run + * @return bool|int False if we couldnt start a process, else the process id from the process table + * + * @link http://www.djkaty.com/php/fork Cross platform process tutorial (this code adapted from here) + */ +function start_process($filename) +{ + //create a record only if no process already running + global $db; + + $db->StartTrans(); + + $process = is_process_running(); + + if ($process == false) + { + $sql = "INSERT INTO `process` (`process_id`,`start`,`stop`,`kill`,`data`) + VALUES (NULL,NOW(),NULL,0,'')"; + + $rs = $db->Execute($sql); + $args = $db->Insert_ID(); + + + //execute the process in the background - pass the process_id as the first argument + if (substr(PHP_OS, 0, 3) == 'WIN') + $proc = popen(WINDOWS_PHP_EXEC . ' "' . $filename . '" ' . $args, 'r'); + else + $proc = popen(PHP_EXEC . ' ' . $filename . ' ' . $args . ' &', 'r'); + + pclose($proc); + } + else + $db->FailTrans(); + + + $db->CompleteTrans(); + + + if (isset($args)) + return $args; + + return false; +} + + +/** + * Signal to kill a process + * + * @param int $process_id The process id + * + */ +function kill_process($process_id) +{ + global $db; + + $sql = "UPDATE `process` + SET `kill` = '1' + WHERE `process_id` = '$process_id'"; + + $db->Execute($sql); + +} + + + +/** + * End a process + * + * @param int $process_id The process id + * + */ +function end_process($process_id) +{ + global $db; + + $sql = "UPDATE `process` + SET `stop` = NOW() + WHERE `process_id` = '$process_id'"; + + $db->Execute($sql); +} + + +/** + * Append data to a process + * + * @param int $process_id The process id + * @param string $data Data to append to this process + * + */ +function process_append_data($process_id,$data) +{ + global $db; + + $data = $db->qstr($data,get_magic_quotes_gpc()); + + $sql = "UPDATE `process` + SET `data` = CONCAT(`data`, $data) + WHERE `process_id` = '$process_id'"; + + $db->Execute($sql); + +} + + +/** + * Get data from a process + * + * @param int $process_id The process id + * @return string Data from this process or an empty string if none available + * + */ +function process_get_data($process_id) +{ + global $db; + + $sql = "SELECT `data` + FROM `process` + WHERE `process_id` = '$process_id'"; + + $rs = $db->GetRow($sql); + + if (!empty($rs)) + return $rs['data']; + + return ""; +} + +?> diff --git a/functions/functions.voip.php b/functions/functions.voip.php index a35d76eb..014e4eaa 100644 --- a/functions/functions.voip.php +++ b/functions/functions.voip.php @@ -347,13 +347,18 @@ class voipWatch extends voip { * * */ - function watch() + function watch($process_id = false) { /** * Database file */ include_once(dirname(__FILE__).'/../db.inc.php'); + /** + * Process file + */ + if ($process_id) include_once(dirname(__FILE__).'/../functions/functions.process.php'); + $line = ""; if ($this->socket === false) @@ -482,6 +487,8 @@ class voipWatch extends voip { @flush(); + if ($process_id) $this->keepWatching = !is_process_killed($process_id); + } while ($this->keepWatching); } diff --git a/voip/voipwatch.php b/voip/voipwatch.php index 8df63afa..02f6c2bd 100644 --- a/voip/voipwatch.php +++ b/voip/voipwatch.php @@ -36,7 +36,7 @@ include("../functions/functions.voip.php"); $t = new voipWatch(); -$t->connect("asterisk.dcarf","admin","amp111",true); +$t->connect(VOIP_SERVER,VOIP_ADMIN_USER,VOIP_ADMIN_PASS,true); $t->watch(); ?>