Current File : //root/panel/modules/nodejs/code/controller.ext.php |
<?php
/**
* @copyright 2014-2015 Sentora Project (http://www.sentora.org/)
* Sentora is a GPL fork of the ZPanel Project whose original header follows:
*
* ZPanel - A Cross-Platform Open-Source Web Hosting Control panel.
*
* @package ZPanel
* @version $Id$
* @author Bobby Allen - ballen@bobbyallen.me
* @copyright (c) 2008-2014 ZPanel Group - http://www.zpanelcp.com/
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License v3
*
* This program (ZPanel) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class module_controller extends ctrl_module
{
public static $m_error;
public static $default_port = 3000;
static function n_sanitize($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
static function getListDomaindata()
{
global $zdbh;
global $controller;
$domain_Array = array(array('d_id'=>0,"d_name"=>"Select domain name"));
$sql="";
$currentuser = ctrl_users::GetUserDetail();
$suid = $currentuser['userid'];
if($suid==1)
{
$sql="select vh_id_pk,vh_name_vc from x_vhosts where vh_deleted_ts IS NULL and vh_name_vc not in(select nd_domain from x_nodejs where nd_deleted IS NULL) ";
}else
{
$sql="select vh_id_pk,vh_name_vc from x_vhosts where vh_deleted_ts IS NULL AND vh_acc_fk='".$suid."' AND vh_name_vc not in(select nd_domain from x_nodejs where nd_deleted IS NULL) ";
}
$stmt = $zdbh->prepare($sql);
$stmt->execute();
$row_count = $stmt->rowCount();
if($row_count >0)
{
while($rows = $stmt->fetch())
{
array_push($domain_Array,array('d_id' => $rows['vh_id_pk'],'d_name' => $rows['vh_name_vc']) ) ;
}
return $domain_Array;
}
else { return $domain_Array; }
}
static function getCheckPort()
{
global $zdbh;
$stmt = $zdbh->prepare("select nd_id from x_nodejs where nd_deleted IS NULL and nd_port=:n_port ");
$stmt->bindParam(':n_port',self::$default_port);
$stmt->execute();
$row_count = $stmt->rowCount();
if($row_count >0)
{
return "<input name=\"nodejs_port\" id=\"nodejs_port\" type=\"text\" size=\"30\" maxlength=\"5\" placeholder=\"Enter port\" onkeyup =\"isd.checkPort()\" onfocus=\"isd.checkPort()\" onblur=\"isd.remv_msg()\" >";
}
else { return "<input name=\"nodejs_port\" id=\"nodejs_port\" type=\"text\" size=\"30\" maxlength=\"5\" placeholder=\"3000 port recommend\" onkeyup=\"isd.checkPort()\" onblur=\"isd.remv_msg()\" >"; }
}
static function doEnableNodejs()
{
global $controller;
global $zdbh;
//runtime_csfr::Protect();
$formvars = $controller->GetAllControllerRequests('FORM');
if (array_key_exists("domain_detail",$formvars) && array_key_exists("nodejs_port",$formvars) && is_numeric($formvars['nodejs_port']) && is_numeric($formvars['domain_detail']))
{
$stmt5 = $zdbh->prepare("select x_varnish,x_nginx from x_varnish limit 1");
$stmt5->execute();
if ($stmt5->rowCount() > 0)
{
$row5 = $stmt5->fetch();
$varnish_status = $row5['x_varnish'];
$nginx_status = $row5['x_nginx'];
if(strtolower($varnish_status) == "on")
{
self::$m_error = "Please off varnish to enable nodejs ";
}
else
{
$domain_detail = self::n_sanitize($formvars['domain_detail']);
$nodejs_port = self::n_sanitize($formvars['nodejs_port']);
$currentuser = ctrl_users::GetUserDetail();
$stmt1 = $zdbh->prepare("select vh_id_pk,vh_acc_fk,vh_name_vc, vh_directory_vc,(select ac_user_vc from x_accounts where x_accounts.ac_id_pk = x_vhosts.vh_acc_fk and x_accounts.ac_deleted_ts IS NULL ) as folder_path from x_vhosts where vh_id_pk =:dom_id and vh_deleted_ts IS NULL ");
$stmt1->bindParam(':dom_id',$domain_detail);
$stmt1->execute();
$row_count1 = $stmt1->rowCount();
if($row_count1 >0)
{
$row1 = $stmt1->fetch();
$n_dom = $row1['vh_name_vc'];
$dom_path = $row1['folder_path'];
$dir_path = $row1['vh_directory_vc'];
$nd_acc_fk = $row1['vh_acc_fk'];
$nd_vh_id = $row1['vh_id_pk'];
$stmt3 = $zdbh->prepare("select nd_id,nd_port from x_nodejs where (nd_domain =:dom_detail or nd_port =:dom_port) and nd_deleted IS NULL ");
$stmt3->bindParam(':dom_detail',$n_dom);
$stmt3->bindParam(':dom_port',$nodejs_port);
$stmt3->execute();
$row_count3 = $stmt3->rowCount();
if($row_count3 > 0 )
{
$rows3 = $stmt3->fetch();
if ($rows3['nd_port'] == $nodejs_port) { self::$m_error = "Given port is attached with domain.";
}
else { self::$m_error = "Select domain attached with port, select different domain."; }
}
else
{
date_default_timezone_set("UTC");
$str_time = strtotime(date("Y-m-d H:i:s"));
$stmt4 = $zdbh->prepare("insert into x_nodejs (nd_domain,nd_created,nd_port,nd_acc_fk,nd_vh_id) values(:dom_detail,:dom_created,:dom_port,:acc_fk,:vh_id) ");
$stmt4->bindParam(":dom_detail",$n_dom);
$stmt4->bindParam(":dom_created",$str_time);
$stmt4->bindParam(":dom_port",$nodejs_port);
$stmt4->bindParam(":acc_fk",$nd_acc_fk);
$stmt4->bindParam(":vh_id",$nd_vh_id);
$stmt4->execute();
if(strtolower($nginx_status) == "off" )
{
$n_dom_path =trim(ctrl_options::GetSystemOption('hosted_dir')).$dom_path."/public_html".$dir_path;
$cmd_string ="nodejs apache ".$n_dom." 1 ".$nodejs_port." ".$n_dom_path;
self::call_socket($cmd_string);
self::$m_error = "we configure your node js server for ".$n_dom.", please check after 2 minute";
}
else
{
self::$m_error = "Node js for nginx is currently not available";
//$cmd_string = "nodejs nginx ".$domain_detail." 1 ".$nodejs_port;
//self::call_socket($cmd_string);
}
}
}
else { self::$m_error = "Domain is not exist in server, create this domain"; }
}
}
else { self::$m_error = "We are getting some server issue, please contact system admin"; }
}
else { self::$m_error = "Enter all field to enable node js for this domain"; }
}
static function getall_nodejs_domain()
{
global $zdbh;
$currentuser = ctrl_users::GetUserDetail();
$suid = $currentuser['userid'];
if($suid == 1)
{
$stmt3 =$zdbh->prepare("select nd_id,nd_domain,nd_port from x_nodejs where nd_deleted IS NULL and nd_domain in (select vh_name_vc from x_vhosts where vh_deleted_ts IS NULL)");
// $stmt3->bindParam(':sid',$suid);
}
else
{
$stmt3 =$zdbh->prepare("select nd_id,nd_domain,nd_port from x_nodejs where nd_deleted IS NULL and nd_domain in (select vh_name_vc from x_vhosts where vh_acc_fk=:sid AND vh_deleted_ts IS NULL)");
$stmt3->bindParam(':sid',$suid);
}
$stmt3->execute();
$row_count3 = $stmt3->rowCount();
if($row_count3 >0)
{
$dom_array = array();
$sl_no = 1;
while($rows=$stmt3->fetch())
{
array_push($dom_array,array('nd_id' => $rows['nd_id'],'nd_dom' => $rows['nd_domain'],'nd_port' => $rows['nd_port'],"nd_no"=>$sl_no) ) ;
$sl_no++;
}
return $dom_array;
}
else { return false; }
}
static function call_socket($cmd_detail)
{
$service_port = 4444 ;
$address = gethostbyname('localhost');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
return "2";
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
return "2";
}
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 20, "usec" =>0));
$in="command ".$cmd_detail;
$out = '';
if(@socket_write($socket, $in, strlen($in)))
{
sleep(1);
$s_data = socket_read($socket,1024);
socket_close($socket);
return $s_data;
}
else { return "2"; }
}
static function doconform_nodejs()
{
global $zdbh;
global $controller;
runtime_csfr::Protect();
$formvars = $controller->GetAllControllerRequests('FORM');
if(array_key_exists("nodejsdata",$formvars))
{
$nodejs_dom_id =self::n_sanitize($formvars['nodejsdata']);
if ( !fs_director::CheckForEmptyValue($nodejs_dom_id) && is_numeric( $nodejs_dom_id) )
{
$stmt3 =$zdbh->prepare("select nd_id,nd_domain from x_nodejs where nd_id =:vhid and nd_deleted IS NULL ");
$stmt3->bindParam(':vhid', $nodejs_dom_id);
$stmt3->execute();
$row_count3 = $stmt3->rowCount();
if($row_count3 >0)
{
$rows=$stmt3->fetch();
header("location: ./?module=" . $controller->GetCurrentModule() . "&show=Delete&id=" . $rows['nd_id'] . "&domain=" . $rows['nd_domain'] . " ");
}
else
{
header("location: ./?module=" . $controller->GetCurrentModule() );
exit;
}
}
else
{
header("location: ./?module=" . $controller->GetCurrentModule() );
exit;
}
}
else
{
header("location: ./?module=" . $controller->GetCurrentModule() );
exit;
}
}
static function getisDeleteNodejs()
{
global $controller;
$urlvars = $controller->GetAllControllerRequests('URL');
return (isset($urlvars['show'])) && ($urlvars['show'] == "Delete");
}
static function getCurrentNodejs()
{
global $controller;
$auto_res = $controller->GetControllerRequest('URL', 'domain');
return ($auto_res) ? $auto_res : '';
}
static function getid_data()
{
global $controller;
$auto_resid = $controller->GetControllerRequest('URL', 'id');
return ($auto_resid) ? $auto_resid : 0;
}
static function doDeleteNodejs()
{
global $zdbh;
global $controller;
runtime_csfr::Protect();
$formvars = $controller->GetAllControllerRequests('FORM');
if(array_key_exists("inDelete",$formvars))
{
$auto_id =self::n_sanitize($formvars['inDelete']);
if ( !fs_director::CheckForEmptyValue($auto_id) && is_numeric($auto_id) && $auto_id > 0)
{
date_default_timezone_set("UTC");
$current_time = date("Y-m-d H:i:s");
$current_str_time = strtotime($current_time);
$stmt3 =$zdbh->prepare("select nd_domain from x_nodejs where nd_id =:ndid and nd_deleted IS NULL ");
$stmt3->bindParam(':ndid',$auto_id);
$stmt3->execute();
$row_count3 = $stmt3->rowCount();
if($row_count3 >0)
{
$rows = $stmt3->fetch();
$dom_name = $rows['nd_domain'];
$stmt1 = $zdbh->prepare("select vh_acc_fk,vh_name_vc, vh_directory_vc,(select ac_user_vc from x_accounts where x_accounts.ac_id_pk = x_vhosts.vh_acc_fk and x_accounts.ac_deleted_ts IS NULL ) as folder_path from x_vhosts where vh_name_vc =:dom_name and vh_deleted_ts IS NULL ");
$stmt1->bindParam(':dom_name',$dom_name);
$stmt1->execute();
$row_count1 = $stmt1->rowCount();
if($row_count1 >0)
{
$rs_status = 1;
$stmt5 = $zdbh->prepare("select x_varnish,x_nginx from x_varnish limit 1");
$stmt5->execute();
if ($stmt5->rowCount() > 0)
{
$row5=$stmt5->fetch();
if(strtolower($row5['x_nginx']) == "on")
{
$rs_status = 2;
}
}
$row1 = $stmt1->fetch();
$n_dom = $row1['vh_name_vc'];
$dom_path = $row1['folder_path'];
$dir_path = $row1['vh_directory_vc'];
$stmt4 = $zdbh->prepare("update x_nodejs set nd_deleted =:str_time where nd_id =:vhid ");
$stmt4->bindParam(':str_time',$current_str_time);
$stmt4->bindParam(':vhid',$auto_id);
$stmt4->execute();
$n_dom_path =trim(ctrl_options::GetSystemOption('hosted_dir')).$dom_path."/public_html".$dir_path;
$cmd_string ="nodejs apache ".$n_dom." 2 ".$n_dom_path." ".$rs_status;
self::call_socket($cmd_string);
}
}
else
{
self::$m_error = "Problem in remove nodejs,please contact system admin";
}
}
else
{
self::$m_error = "Nodejs was not enable for this domain";
}
}
else
{
self::$m_error = "Nodejs was not enable for this domain";
}
}
static function getresult()
{
if (!fs_director::CheckForEmptyValue(self::$m_error)) {
return ui_sysmessage::shout(ui_language::translate(self::$m_error), "zannounceerror");
}
return;
}
}