Portions created by the Initial Developer are Copyright (C) 2008-2012 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ 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 = "\\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
\n"; } //if (strlen($service_script) == 0) { $msg .= "Please provide: Script
\n"; } if (strlen($service_enabled) == 0) { $msg .= "Please provide: Enabled
\n"; } //if (strlen($service_description) == 0) { $msg .= "Please provide: Description
\n"; } if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) { require_once "includes/header.php"; require_once "includes/persistformvar.php"; echo "
\n"; echo "
\n"; echo $msg."
"; echo "
\n"; persistformvar($_POST); echo "
\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 "\n"; echo "
\n"; echo "Add Complete\n"; echo "
\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 "\n"; echo "
\n"; echo "Update Complete\n"; echo "
\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 " \n"; echo " \n"; echo " "; //show the form echo "
"; echo "\n"; echo "\n"; echo " "; echo " "; echo "
\n"; echo "
"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; if ($action == "add") { echo "\n"; } if ($action == "update") { echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo " "; echo "
PHP Service AddPHP Service Edit
\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.

\n"; echo "
\n"; echo " Name:\n"; echo "\n"; echo " \n"; echo "
\n"; echo "Enter a name.\n"; echo "
\n"; echo " Script:\n"; echo "\n"; echo " \n"; echo "
\n"; echo "Enter the PHP script here.\n"; echo "
\n"; echo " Enabled:\n"; echo "\n"; echo " \n"; echo "
\n"; echo "\n"; echo "
\n"; echo " Description:\n"; echo "\n"; echo " \n"; echo "
\n"; echo "\n"; echo "
\n"; if ($action == "update") { echo " \n"; } echo " \n"; echo "
"; echo ""; echo "
"; echo "
"; //include the footer require_once "includes/footer.php"; ?>