Browse Source

Move to dev/apps.

Mark Crane 13 years ago
parent
commit
ad69b0c913

+ 82 - 0
php_service/app_config.php

@@ -0,0 +1,82 @@
+<?php
+	//application details
+		$apps[$x]['name'] = "PHP Service";
+		$apps[$x]['uuid'] = '93f55da0-3b33-da5b-c6db-4cdd6de97fbd';
+		$apps[$x]['category'] = 'System';
+		$apps[$x]['subcategory'] = '';
+		$apps[$x]['version'] = '';
+		$apps[$x]['license'] = 'Mozilla Public License 1.1';
+		$apps[$x]['url'] = 'http://www.fusionpbx.com';
+		$apps[$x]['description']['en'] = 'Manages multiple dynamic and customizable services. There are many possible uses including alerts, ssh access control, scheduling commands to run, and many others uses that are yet to be discovered.';
+
+	//menu details
+		$apps[$x]['menu'][0]['title']['en'] = 'PHP Service';
+		$apps[$x]['menu'][0]['uuid'] = 'a8196e2f-5f60-e723-aa3e-83ed76b2ef09';
+		$apps[$x]['menu'][0]['parent_uuid'] = '594d99c5-6128-9c88-ca35-4b33392cec0f';
+		$apps[$x]['menu'][0]['category'] = 'internal';
+		$apps[$x]['menu'][0]['path'] = '/app/php_service/v_php_service.php';
+		$apps[$x]['menu'][0]['groups'][] = 'superadmin';
+
+	//permission details
+		$apps[$x]['permissions'][0]['name'] = 'php_service_view';
+		$apps[$x]['permissions'][0]['groups'][] = 'superadmin';
+
+		$apps[$x]['permissions'][1]['name'] = 'php_service_add';
+		$apps[$x]['permissions'][1]['groups'][] = 'superadmin';
+
+		$apps[$x]['permissions'][2]['name'] = 'php_service_edit';
+		$apps[$x]['permissions'][2]['groups'][] = 'superadmin';
+
+		$apps[$x]['permissions'][3]['name'] = 'php_service_delete';
+		$apps[$x]['permissions'][3]['groups'][] = 'superadmin';
+
+	//schema details
+		$y = 0; //table array index
+		$z = 0; //field array index
+		$apps[$x]['db'][$y]['table'] = 'v_php_services';
+		$apps[$x]['db'][$y]['fields'][$z]['name']['text'] = 'id';
+		$apps[$x]['db'][$y]['fields'][$z]['name']['deprecated'] = 'php_service_id';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = 'serial';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = 'integer';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = 'INT NOT NULL AUTO_INCREMENT';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$apps[$x]['db'][$y]['fields'][$z]['deprecated'] = 'true';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'php_service_uuid';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = 'uuid';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = 'char(36)';
+		$apps[$x]['db'][$y]['fields'][$z]['key']['type'] = 'primary';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'domain_uuid';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['pgsql'] = 'uuid';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = 'char(36)';
+		$apps[$x]['db'][$y]['fields'][$z]['key']['type'] = 'foreign';
+		$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['table'] = 'v_domains';
+		$apps[$x]['db'][$y]['fields'][$z]['key']['reference']['field'] = 'domain_uuid';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'v_id';
+		$apps[$x]['db'][$y]['fields'][$z]['type'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$apps[$x]['db'][$y]['fields'][$z]['deprecated'] = 'true';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'service_name';
+		$apps[$x]['db'][$y]['fields'][$z]['type'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'service_script';
+		$apps[$x]['db'][$y]['fields'][$z]['type'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'service_enabled';
+		$apps[$x]['db'][$y]['fields'][$z]['type'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+		$z++;
+		$apps[$x]['db'][$y]['fields'][$z]['name'] = 'service_description';
+		$apps[$x]['db'][$y]['fields'][$z]['type'] = 'text';
+		$apps[$x]['db'][$y]['fields'][$z]['description']['en'] = '';
+
+?>

+ 50 - 0
php_service/root.php

@@ -0,0 +1,50 @@
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+
+// make sure the PATH_SEPARATOR is defined
+	if (!defined("PATH_SEPARATOR")) {
+		if ( strpos( $_ENV[ "OS" ], "Win" ) !== false ) { define("PATH_SEPARATOR", ";"); } else { define("PATH_SEPARATOR", ":"); }
+	}
+
+// make sure the document_root is set
+	$_SERVER["SCRIPT_FILENAME"] = str_replace("\\", "/", $_SERVER["SCRIPT_FILENAME"]);
+	$_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
+	$_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]);
+	//echo "DOCUMENT_ROOT: ".$_SERVER["DOCUMENT_ROOT"]."<br />\n";
+	//echo "PHP_SELF: ".$_SERVER["PHP_SELF"]."<br />\n";
+	//echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."<br />\n";
+
+// if the project directory exists then add it to the include path otherwise add the document root to the include path
+	if (is_dir($_SERVER["DOCUMENT_ROOT"].'/fusionpbx')){
+		if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', '/fusionpbx'); }
+		set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER["DOCUMENT_ROOT"].'/fusionpbx' );
+	}
+	else {
+		if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', ''); }
+		set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] );
+	}
+
+?>

+ 176 - 0
php_service/v_php_service.php

@@ -0,0 +1,176 @@
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "includes/require.php";
+require_once "includes/checkauth.php";
+if (permission_exists('php_service_view')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+require_once "includes/header.php";
+require_once "includes/paging.php";
+
+/*
+function pkg_is_service_running($servicename) 
+{
+	exec("/bin/ps ax | awk '{ print $5 }'", $psout);
+	array_shift($psout);
+	foreach($psout as $line) {
+		$ps[] = trim(array_pop(explode(' ', array_pop(explode('/', $line)))));
+	}
+	if(is_service_running($servicename, $ps) or is_process_running($servicename) ) {
+		return true;
+	}
+	else {
+		return false;
+	}
+}
+*/
+
+$order_by = $_GET["order_by"];
+$order = $_GET["order"];
+
+//show the content
+	echo "<div align='center'>";
+	echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>\n";
+	echo "<tr class='border'>\n";
+	echo "	<td align=\"center\">\n";
+	echo "		<br>";
+
+	echo "<table width='100%' border='0'>\n";
+	echo "<tr>\n";
+	echo "<td width='50%' nowrap='nowrap' align='left'><b>PHP Service</b></td>\n";
+	echo "<td width='50%'>&nbsp;</td>\n";
+	echo "</tr>\n";
+	echo "<tr>\n";
+	echo "<td colspan='2' align='left'>\n";
+	echo "Manages multiple dynamic and customizable services. There are many possible uses including alerts, ssh access control, scheduling commands to run, and many others uses that are yet to be discovered.<br /><br />\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+	echo "</tr></table>\n";
+
+	$sql = "";
+	$sql .= " select * from v_php_services ";
+	$sql .= " where domain_uuid = '$domain_uuid' ";
+	if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+	$num_rows = count($result);
+	unset ($prep_statement, $result, $sql);
+	$rows_per_page = 10;
+	$param = "";
+	$page = $_GET['page'];
+	if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; } 
+	list($paging_controls, $rows_per_page, $var_3) = paging($num_rows, $param, $rows_per_page); 
+	$offset = $rows_per_page * $page; 
+
+	$sql = "";
+	$sql .= " select * from v_php_services ";
+	$sql .= " where domain_uuid = '$domain_uuid' ";
+	if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
+	$sql .= " limit $rows_per_page offset $offset ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+	$result_count = count($result);
+	unset ($prep_statement, $sql);
+
+	$c = 0;
+	$row_style["0"] = "row_style0";
+	$row_style["1"] = "row_style1";
+
+	echo "<div align='center'>\n";
+	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+	echo "<tr>\n";
+	echo th_order_by('service_name', 'Name', $order_by, $order);
+	//echo th_order_by('service_group', 'Group', $order_by, $order);
+	echo th_order_by('service_enabled', 'Enabled', $order_by, $order);
+	echo th_order_by('service_description', 'Description', $order_by, $order);
+	echo "<td align='right' width='42'>\n";
+	if (permission_exists('php_service_add')) {
+		echo "	<a href='v_php_service_edit.php' alt='add'>$v_link_label_add</a>\n";
+	}
+	echo "</td>\n";
+	echo "<tr>\n";
+
+	if ($result_count == 0) {
+		//no results
+	}
+	else { //received results
+		foreach($result as $row) {
+			echo "<tr >\n";
+			echo "	<td valign='top' class='".$row_style[$c]."'>".$row[service_name]."</td>\n";
+			//echo "	<td valign='top' class='".$row_style[$c]."'>".$row[service_group]."</td>\n";
+			echo "	<td valign='top' class='".$row_style[$c]."'>".$row[service_enabled]."</td>\n";
+			echo "	<td valign='top' class='row_stylebg'>".$row[service_description]."&nbsp;</td>\n";
+			echo "	<td valign='top' align='right'>\n";
+			if (permission_exists('php_service_edit')) {
+				echo "		<a href='v_php_service_edit.php?id=".$row[php_service_uuid]."' alt='edit'>$v_link_label_edit</a>\n";
+			}
+			if (permission_exists('php_service_delete')) {
+				echo "		<a href='v_php_service_delete.php?id=".$row[php_service_uuid]."' alt='delete' onclick=\"return confirm('Do you really want to delete this?')\">$v_link_label_delete</a>\n";
+			}
+			echo "	</td>\n";
+			echo "</tr>\n";
+			if ($c==0) { $c=1; } else { $c=0; }
+		} //end foreach
+		unset($sql, $result, $row_count);
+	} //end if results
+
+	echo "<tr>\n";
+	echo "<td colspan='5' align='left'>\n";
+	echo "	<table width='100%' cellpadding='0' cellspacing='0'>\n";
+	echo "	<tr>\n";
+	echo "		<td width='33.3%' nowrap>&nbsp;</td>\n";
+	echo "		<td width='33.3%' align='center' nowrap>$paging_controls</td>\n";
+	echo "		<td width='33.3%' align='right'>\n";
+	if (permission_exists('php_service_add')) {
+		echo "			<a href='v_php_service_edit.php' alt='add'>$v_link_label_add</a>\n";
+	}
+	echo "		</td>\n";
+	echo "	</tr>\n";
+ 	echo "	</table>\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "</table>";
+	echo "</div>";
+	echo "<br><br>";
+	echo "<br><br>";
+
+	echo "</td>";
+	echo "</tr>";
+	echo "</table>";
+	echo "</div>";
+	echo "<br><br>";
+
+//show the footer
+	require_once "includes/footer.php";
+?>

+ 82 - 0
php_service/v_php_service_delete.php

@@ -0,0 +1,82 @@
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "includes/require.php";
+require_once "includes/checkauth.php";
+if (permission_exists('php_service_delete')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+if (count($_GET)>0) {
+	$id = check_str($_GET["id"]);
+}
+
+if (strlen($id)>0) {
+	$sql = "";
+	$sql .= "select * from v_php_services ";
+	$sql .= "where domain_uuid = '$domain_uuid' ";
+	$sql .= "and php_service_uuid = '$id' ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+	foreach ($result as &$row) {
+		$service_name = $row["service_name"];
+		$tmp_service_name = str_replace(" ", "_", $service_name);
+		break; //limit to 1 row
+	}
+	unset ($prep_statement, $result, $row);
+
+	//delete the php service file
+		unlink($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure/php_service_'.$tmp_service_name.'.php');
+	//delete the start up script
+		unlink($startup_script_dir.'/php_service_'.$tmp_service_name.'.sh');
+	//delete the pid file
+		unlink($tmp_dir.'/php_service_'.$tmp_service_name.'.pid');
+
+	$sql = "";
+	$sql .= "delete from v_php_services ";
+	$sql .= "where domain_uuid = '$domain_uuid' ";
+	$sql .= "and php_service_uuid = '$id' ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	unset($sql);
+}
+
+require_once "includes/header.php";
+echo "<meta http-equiv=\"refresh\" content=\"2;url=v_php_service.php\">\n";
+echo "<div align='center'>\n";
+echo "Delete Complete\n";
+echo "</div>\n";
+
+require_once "includes/footer.php";
+return;
+
+?>
+

+ 556 - 0
php_service/v_php_service_edit.php

@@ -0,0 +1,556 @@
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "includes/require.php";
+require_once "includes/checkauth.php";
+if (permission_exists('php_service_add') || permission_exists('php_service_edit')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+/*
+function pkg_is_service_running($servicename) {
+	exec("/bin/ps ax | awk '{ print $5 }'", $psout);
+	array_shift($psout);
+	foreach($psout as $line) {
+		$ps[] = trim(array_pop(explode(' ', array_pop(explode('/', $line)))));
+	}
+	if(is_service_running($servicename, $ps) or is_process_running($servicename) ) {
+		return true;
+	}
+	else {
+		return false;
+	}
+}
+
+function byte_convert( $bytes ) {
+	if ($bytes<=0)
+		return '0 Byte';
+
+	$convention=1000; //[1000->10^x|1024->2^x]
+	$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
+	$e=floor(log($bytes,$convention));
+	return round($bytes/pow($convention,$e),2).' '.$s[$e];
+}
+*/
+
+function php_services_sync_package_php() {
+	global $db, $domain_uuid, $startup_script_dir, $tmp_dir;
+	$sql = "";
+	$sql .= "select * from v_php_services ";
+	$sql .= "where domain_uuid = '$domain_uuid' ";
+	$tmp_prep_statement = $db->prepare(check_sql($sql));
+	$tmp_prep_statement->execute();
+	$tmp_result = $tmp_prep_statement->fetchAll(PDO::FETCH_NAMED);
+	foreach ($tmp_result as &$row) {
+		$service_name = $row["service_name"];
+		$tmp_service_name = str_replace(" ", "_", $service_name);
+		$service_script = base64_decode($row["service_script"]);
+		//$service_enabled = $row["service_enabled"];
+		$service_description = $row["service_description"];
+		$php_service_file = "php_service_".$tmp_service_name.".php";
+
+		if ($row['service_enabled'] == "false") {
+			//delete the php service file
+				unlink($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure/php_service_'.$tmp_service_name.'.php');
+			//delete the start up script
+				unlink($startup_script_dir.'/php_service_'.$tmp_service_name.'.sh');
+			//delete the pid file
+				unlink($tmp_dir.'/php_service_'.$tmp_service_name.'.pid');
+		}
+		else {
+			//write the php service
+				$tmp = "<?php\n";
+				$tmp .= "// name: ".$service_name." \n";
+				$tmp .= "// description: ".$service_description." \n";
+				$tmp .= "\n";
+				$tmp .= "// set time limit to indefinite execution\n";
+				$tmp .= "set_time_limit (0);\n";
+				$tmp .= "\n";
+				$tmp .= "//run this program as long as the pid file exists\n";
+				$tmp .= "\$filename = '".$tmp_dir."/php_service_".$tmp_service_name.".pid';\n";
+				$tmp .= "\$fp = fopen(\$filename, 'w');\n";
+				$tmp .= "fwrite(\$fp, getmypid());\n";
+				$tmp .= "fclose(\$fp);\n";
+				$tmp .= "chmod(\"".$tmp_dir."/php_service_".$tmp_service_name.".pid\", 0776);\n";
+				$tmp .= "unset(\$filename);\n";
+				$tmp .= "\n";
+
+				//$tmp .= "//require_once(\"config.inc\");\n";
+				//$tmp .= "//global \$config;\n";
+				//$tmp .= "//\$sys_log_address = \$config['syslog']['remoteserver'];\n";
+				//$tmp .= "\$sys_log_address = \"127.0.0.1\";\n";
+				//$tmp .= "\$syslogport = 514;\n";
+				//$tmp .= "echo \"syslog server: \".\$sys_log_address.\"\\n\";\n";
+				//$tmp .= "\n";
+				//$tmp .= "\n";
+				//$tmp .= "\n";
+				//$tmp .= "function send_to_syslog(\$sys_log_address, \$syslogport, \$syslogmsg) {\n";
+				//$tmp .= "\n";
+				//$tmp .= "  \$fp = fsockopen(\"udp://\".\$sys_log_address, \$syslogport, \$errno, \$errstr);\n";
+				//$tmp .= "  if (!\$fp) {\n";
+				//$tmp .= "      //echo \"ERROR: \$errno - \$errstr<br />\\n\";\n";
+				//$tmp .= "  } else {\n";
+				//$tmp .= "      fwrite(\$fp, \$syslogmsg);\n";
+				//$tmp .= "      fclose(\$fp);\n";
+				//$tmp .= "  }\n";
+				//$tmp .= "\n";
+				//$tmp .= "}\n";
+				//$tmp .= "\n";
+				//$tmp .= "\n";
+				//$tmp .= "//\$msg = \"1.begin loop. \".date('r').\"\\n\";\n";
+				//$tmp .= "//\$fp = fopen('/tmp/".$tmp_service_name.".txt', 'a');\n";
+				//$tmp .= "//fwrite(\$fp, \$msg.\"\\n\");\n";
+				//$tmp .= "//fclose(\$fp);\n";
+
+				//$tmp .= "\n";
+				$tmp .= $service_script;
+				$tmp .= "\n";
+				$tmp .= "?>";
+
+				$fout = fopen($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure/'.$php_service_file,"w");
+				fwrite($fout, $tmp);
+				unset($tmp);
+				fclose($fout);
+
+			//add execute permissions to the php service script
+				chmod($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure/'.$php_service_file, 0776);
+
+			//write the start up script
+				// CYGWIN_NT-5.1
+				// Darwin
+				// FreeBSD
+				// HP-UX
+				// IRIX64
+				// Linux
+				// NetBSD
+				// OpenBSD
+				// SunOS
+				// Unix
+				// WIN32
+				// WINNT
+				// Windows
+				switch (PHP_OS) {
+				case "FreeBSD":
+					// make sure the start up directory i set
+					if (strlen($startup_script_dir) > 0) {
+						$startup_script_dir = "/usr/local/etc/rc.d";
+					}
+					$tmp = "";
+					$tmp = "#!/bin/sh\n";
+					$tmp .= "# This file was automatically generated\n";
+					$tmp .= "# by the PHP Service handler.\n";
+					$tmp .= "# \n";
+					$tmp .= "# Copy this script to the startup directory.\n";
+					$tmp .= "# cp -a ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh ".$startup_script_dir."/php_service_".$tmp_service_name.".sh";
+					$tmp .= "# \n";
+					$tmp .= "# Usage: ./php_service_".$tmp_service_name.".sh {start|stop|restart}\n";
+					$tmp .= "# ".$startup_script_dir."/./php_service_".$tmp_service_name.".sh start";
+					$tmp .= "\n";
+					$tmp .= "\n";
+					$tmp .= "rc_start() {\n";
+					$tmp .= "	".PHP_BINDIR."/php ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/".$php_service_file." >> /var/log/php_service_".$tmp_service_name.".log &\n";
+					$tmp .= "}\n";
+					$tmp .= "\n";
+					$tmp .= "rc_stop() {\n";
+					$tmp .= "	rm ".$tmp_dir."/php_service_".$tmp_service_name.".pid\n";
+					$tmp .= "}\n";
+					$tmp .= "\n";
+					$tmp .= "case \"\$1\" in\n";
+					$tmp .= "	start)\n";
+					$tmp .= "		echo \"Starting the service. \"\n";
+					$tmp .= "		rc_start\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	stop)\n";
+					$tmp .= "		echo \"Stopping the service. \"\n";
+					$tmp .= "		rc_stop\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	restart)\n";
+					$tmp .= "		echo \"Restarting the service. \"\n";
+					$tmp .= "		rc_stop\n";
+					$tmp .= "		rc_start\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	*)\n";
+					$tmp .= "		echo \"Usage: ".$startup_script_dir."/php_service_".$tmp_service_name.".sh {start|stop|restart}\"\n";
+					$tmp .= "		exit 1\n";
+					$tmp .= "		;;\n";
+					$tmp .= "esac\n";
+					$fout = fopen($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh","w");
+					fwrite($fout, $tmp);
+					unset($tmp);
+					fclose($fout);
+
+					//add execute permissions to the start script
+						chmod($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh", 0755);
+
+					break;
+				default:
+					// make sure the start up directory i set
+					if (strlen($startup_script_dir) > 0) {
+						$startup_script_dir = "/etc/init.d";
+					}
+					$tmp = "";
+					$tmp .= "#!/bin/sh\n";
+					$tmp .= "# /etc/init.d/".$tmp_service_name."\n";
+					$tmp .= "# This file was automatically generated\n";
+					$tmp .= "# by the PHP Service handler.\n";
+					$tmp .= "# \n";
+					$tmp .= "# Copy this script to the startup directory.\n";
+					$tmp .= "# cp -a ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh ".$startup_script_dir."/php_service_".$tmp_service_name.".sh";
+					$tmp .= "# \n";
+					$tmp .= "# Usage: ./php_service_".$tmp_service_name.".sh {start|stop|restart}\n";
+					$tmp .= "# ".$startup_script_dir."/./php_service_".$tmp_service_name.".sh start";
+					$tmp .= "\n";
+					$tmp .= "\n";
+					$tmp .= "rc_start() {\n";
+					$tmp .= "	".PHP_BINDIR."/php ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/".$php_service_file." >> /var/log/".tmp_service_name.".log &\n";
+					$tmp .= "}\n";
+					$tmp .= "\n";
+					$tmp .= "rc_stop() {\n";
+					$tmp .= "	rm ".$tmp_dir."/php_service_".$tmp_service_name.".pid\n";
+					$tmp .= "}\n";
+					$tmp .= "\n";
+					$tmp .= "case \"\$1\" in\n";
+					$tmp .= "	start)\n";
+					$tmp .= "		echo \"Starting the service. \"\n";
+					$tmp .= "		rc_start\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	stop)\n";
+					$tmp .= "		echo \"Stopping the service. \"\n";
+					$tmp .= "		rc_stop\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	restart)\n";
+					$tmp .= "		echo \"Restarting the service. \"\n";
+					$tmp .= "		rc_stop\n";
+					$tmp .= "		rc_start\n";
+					$tmp .= "		;;\n";
+					$tmp .= "	*)\n";
+					$tmp .= "		echo \"Usage: ".$startup_script_dir."/".$tmp_service_name.".sh {start|stop|restart}\"\n";
+					$tmp .= "		exit 1\n";
+					$tmp .= "		;;\n";
+					$tmp .= "esac\n";
+					$tmp .= "\n";
+					$tmp .= "exit 0";
+					$fout = fopen($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh","w");
+					fwrite($fout, $tmp);
+					unset($tmp);
+					fclose($fout);
+
+					//add execute permissions to the start script
+						chmod($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/php_service_".$tmp_service_name.".sh", 0755);
+				}
+		} //end if enabled
+	}
+}
+
+//set the action as an add or an update
+	if (isset($_REQUEST["id"])) {
+		$action = "update";
+		$php_service_uuid = check_str($_REQUEST["id"]);
+	}
+	else {
+		$action = "add";
+	}
+
+//set the http values to variabless
+if (count($_POST)>0) {
+	$service_name = check_str($_POST["service_name"]);
+	$service_script = $_POST["service_script"];
+	$service_enabled = check_str($_POST["service_enabled"]);
+	$service_description = check_str($_POST["service_description"]);
+
+	//set defaults
+	$service_type = "php";
+
+	//setup the default script
+		$tmp_service_name = str_replace(" ", "_", $service_name);
+		if (strlen($service_script) == 0) {
+			$tmp = "\n";
+			$tmp .= "\n";
+			$tmp .= "\$x = 0;\n";
+			$tmp .= "while(\$x == 0) {\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "\n";
+			$tmp .= "	if(!file_exists('".$tmp_dir."/php_service_".$tmp_service_name.".pid')) { return; }\n";
+			$tmp .= "	usleep(1000000);  //1 000 000 microseconds = 1 second\n";
+			//$tmp .= "  //if (\$x > 60){ exit; } //exit after 60 loops for testing\n";
+			$tmp .= "} //end while\n";
+			$service_script = $tmp;
+		}
+}
+
+if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) {
+
+	$msg = '';
+	if ($action == "update") {
+		$php_service_uuid = check_str($_POST["php_service_uuid"]);
+	}
+
+	//check for all required data
+		if (strlen($service_name) == 0) { $msg .= "Please provide: Name<br>\n"; }
+		//if (strlen($service_script) == 0) { $msg .= "Please provide: Script<br>\n"; }
+		if (strlen($service_enabled) == 0) { $msg .= "Please provide: Enabled<br>\n"; }
+		//if (strlen($service_description) == 0) { $msg .= "Please provide: Description<br>\n"; }
+		if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
+			require_once "includes/header.php";
+			require_once "includes/persistformvar.php";
+			echo "<div align='center'>\n";
+			echo "<table><tr><td>\n";
+			echo $msg."<br />";
+			echo "</td></tr></table>\n";
+			persistformvar($_POST);
+			echo "</div>\n";
+			require_once "includes/footer.php";
+			return;
+		}
+
+	//add or update the database
+	if ($_POST["persistformvar"] != "true") {
+		if ($action == "add" && permission_exists('php_service_add')) {
+			$php_service_uuid = uuid();
+			$sql = "insert into v_php_services ";
+			$sql .= "(";
+			$sql .= "domain_uuid, ";
+			$sql .= "php_service_uuid, ";
+			$sql .= "service_name, ";
+			$sql .= "service_script, ";
+			$sql .= "service_enabled, ";
+			$sql .= "service_description ";
+			$sql .= ")";
+			$sql .= "values ";
+			$sql .= "(";
+			$sql .= "'$domain_uuid', ";
+			$sql .= "'$php_service_uuid', ";
+			$sql .= "'$service_name', ";
+			$sql .= "'".base64_encode($service_script)."', ";
+			$sql .= "'$service_enabled', ";
+			$sql .= "'$service_description' ";
+			$sql .= ")";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			//create the php service files
+				php_services_sync_package_php();
+
+			require_once "includes/header.php";
+			echo "<meta http-equiv=\"refresh\" content=\"2;url=v_php_service.php\">\n";
+			echo "<div align='center'>\n";
+			echo "Add Complete\n";
+			echo "</div>\n";
+			require_once "includes/footer.php";
+			return;
+		} //if ($action == "add")
+
+		if ($action == "update" && permission_exists('php_service_edit')) {
+			$sql = "update v_php_services set ";
+			$sql .= "service_name = '$service_name', ";
+			$sql .= "service_script = '".base64_encode($service_script)."', ";
+			$sql .= "service_enabled = '$service_enabled', ";
+			$sql .= "service_description = '$service_description' ";
+			$sql .= "where domain_uuid = '$domain_uuid' ";
+			$sql .= "and php_service_uuid = '$php_service_uuid' ";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			//create the php service files
+				php_services_sync_package_php();
+
+			require_once "includes/header.php";
+			echo "<meta http-equiv=\"refresh\" content=\"2;url=v_php_service.php\">\n";
+			echo "<div align='center'>\n";
+			echo "Update Complete\n";
+			echo "</div>\n";
+			require_once "includes/footer.php";
+			return;
+		} //if ($action == "update")
+	} //if ($_POST["persistformvar"] != "true")
+} //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
+
+//pre-populate the form
+	if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
+		$php_service_uuid = $_GET["id"];
+		$sql = "";
+		$sql .= "select * from v_php_services ";
+		$sql .= "where domain_uuid = '$domain_uuid' ";
+		$sql .= "and php_service_uuid = '$php_service_uuid' ";
+		$prep_statement = $db->prepare(check_sql($sql));
+		$prep_statement->execute();
+		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+		foreach ($result as &$row) {
+			$service_name = $row["service_name"];
+			$tmp_service_name = str_replace(" ", "_", $service_name);
+			$service_script = base64_decode($row["service_script"]);
+			$service_enabled = $row["service_enabled"];
+			$service_description = $row["service_description"];
+			break; //limit to 1 row
+		}
+		unset ($prep_statement);
+	}
+
+//include the header
+	require_once "includes/header.php";
+
+// edit area
+	echo "    <script language=\"javascript\" type=\"text/javascript\" src=\"".PROJECT_PATH."/includes/edit_area/edit_area_full.js\"></script>\n";
+	echo "    <!-- -->\n";
+
+	echo "	<script language=\"Javascript\" type=\"text/javascript\">\n";
+	echo "		// initialisation //load,\n";
+	echo "		editAreaLoader.init({\n";
+	echo "			id: \"service_script\"	// id of the textarea to transform //, |, help\n";
+	if (strlen($service_script) < 3000) {
+		echo "			,start_highlight: true\n";
+	}
+	else {
+		echo "			,start_highlight: false\n";
+		echo "			,display: \"later\"\n";
+	}
+	echo "			,font_size: \"8\"\n";
+	echo "			,allow_toggle: true\n";
+	echo "			,language: \"en\"\n";
+	echo "			,syntax: \"html\"\n";
+	echo "			,toolbar: \"search, go_to_line,|, fullscreen, |, undo, redo, |, select_font, |, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help\" //new_document,\n";
+	echo "			,plugins: \"charmap\"\n";
+	echo "			,charmap_default: \"arrows\"\n";
+	echo "\n";
+	echo "    });\n";
+	echo "\n";
+	echo "    </script>";
+
+//show the form
+	echo "<div align='center'>";
+	echo "<table width='100%' border='0' cellpadding='0' cellspacing=''>\n";
+
+	echo "<tr class='border'>\n";
+	echo "	<td align=\"left\">\n";
+	echo "	  <br>";
+
+	echo "<form method='post' name='frm' action=''>\n";
+
+	echo "<div align='center'>\n";
+	echo "<table width='100%'  border='0' cellpadding='6' cellspacing='0'>\n";
+
+	echo "<tr>\n";
+	if ($action == "add") {
+		echo "<td align='left' width='30%' nowrap><b>PHP Service Add</b></td>\n";
+	}
+	if ($action == "update") {
+		echo "<td align='left' width='30%' nowrap><b>PHP Service Edit</b></td>\n";
+	}
+	echo "<td width='70%' align='right'><input type='button' class='btn' name='' alt='back' onclick=\"window.location='v_php_service.php'\" value='Back'></td>\n";
+	echo "</tr>\n";
+	echo "<tr>\n";
+	echo "<td colspan='2'>\n";
+	echo "Manages multiple dynamic and customizable services. There are many possible uses including alerts, ssh access control, scheduling commands to run, and many others uses that are yet to be discovered.<br /><br />\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "<tr>\n";
+	echo "<td class='vncellreq' valign='top' align='left' nowrap>\n";
+	echo "	Name:\n";
+	echo "</td>\n";
+	echo "<td class='vtable' align='left'>\n";
+	echo "	<input class='formfld' type='text' name='service_name' maxlength='255' value=\"$service_name\">\n";
+	echo "<br />\n";
+	echo "Enter a name.\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "<tr>\n";
+	echo "<td class='vncellreq' valign='top' align='left' nowrap>\n";
+	echo "	Script:\n";
+	echo "</td>\n";
+	echo "<td class='vtable' align='left'>\n";
+	echo "	<textarea class='formfld' style='width: 90%;' wrap='off' rows='17' name='service_script' id='service_script' rows='4'>$service_script</textarea>\n";
+	echo "<br />\n";
+	echo "Enter the PHP script here.\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "<tr>\n";
+	echo "<td class='vncellreq' valign='top' align='left' nowrap>\n";
+	echo "	Enabled:\n";
+	echo "</td>\n";
+	echo "<td class='vtable' align='left'>\n";
+	echo "	<select class='formfld' name='service_enabled'>\n";
+	echo "	<option value=''></option>\n";
+	if ($service_enabled == "true") { 
+		echo "	<option value='true' selected >true</option>\n";
+	}
+	else {
+		echo "	<option value='true'>true</option>\n";
+	}
+	if ($service_enabled == "false") { 
+		echo "	<option value='false' selected >false</option>\n";
+	}
+	else {
+		echo "	<option value='false'>false</option>\n";
+	}
+	echo "	</select>\n";
+	echo "<br />\n";
+	echo "\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "<tr>\n";
+	echo "<td class='vncell' valign='top' align='left' nowrap>\n";
+	echo "	Description:\n";
+	echo "</td>\n";
+	echo "<td class='vtable' align='left'>\n";
+	echo "	<input class='formfld' type='text' name='service_description' maxlength='255' value=\"$service_description\">\n";
+	echo "<br />\n";
+	echo "\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+	echo "	<tr>\n";
+	echo "		<td colspan='2' align='right'>\n";
+	if ($action == "update") {
+		echo "				<input type='hidden' name='php_service_uuid' value='$php_service_uuid'>\n";
+	}
+	echo "				<input type='submit' name='submit' class='btn' value='Save'>\n";
+	echo "		</td>\n";
+	echo "	</tr>";
+	echo "</table>";
+	echo "</form>";
+
+	echo "	</td>";
+	echo "	</tr>";
+	echo "</table>";
+	echo "</div>";
+
+//include the footer
+	require_once "includes/footer.php";
+?>