Current File : //root/panel/modules/csr/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
{
static $complete;
static $error;
static $nosub;
static $alreadyexists;
static $badname;
static $blank;
static $ok;
static $org;
static $logfile;
static $logpathexist;
static $tryagain;
static function getCreatedDomain()
{
$currentuser = ctrl_users::GetUserDetail();
return self::ListDomain($currentuser['userid']);
}
/*===========================================================================================================*/
static function getErrorTypes(){
global $controller;
$formvars = $controller->GetAllControllerRequests('FORM');
$error_types = array(1=>'CSR File', 2=>'Key File');
$res = array();
foreach($error_types as $row_idx=>$row){
$selected_yn = '';
if(isset($formvars['log_type_id'])){
$selected_yn = ($formvars['log_type_id'] == $row_idx) ? 'selected="selected"':'';
}
array_push($res, array(
'id' => $row_idx,
'name' => $row,
'selected' => $selected_yn,
));
}
return $res;
}
static function getErrorType(){
global $controller;
$formvars = $controller->GetAllControllerRequests('FORM');
$log_type = '';
if(isset($formvars['log_type_id']) && (int) $formvars['log_type_id']){
if($formvars['log_type_id'] == 1){
$log_type = ui_language::translate("CSR File");
} elseif($formvars['log_type_id'] == 2){
$log_type = ui_language::translate("Key File");
}
}
return $log_type;
}
static function getLogFile(){
if(isset(self::$logfile) && self::$logfile != ''){
echo $logfile;
?>
<script>alert(<?php echo $logfile; ?>);</script>
<?php
$log_output = '';
//$log_content = file(self::$logfile);
//if(is_array($logfile) && count($logfile) > 1){
$logfile=$log_output;
//foreach($logfile as $log){
//$log_output .= $log.'<br><br><br><br>';
//}
// } else {
// $log_output = ui_language::translate("There are no log items to display.");
//}
$log_output=$logfile;
return $log_output;
}
//return false;
}
static function gethelpicon()
{
global $zdbh;
global $controller;
$temp=$controller->GetControllerRequest('URL','module') ;
$val = '/assets/one.txt';
$val1 = '/modules/';
$name=file_get_contents("modules/$temp/assets/helpicon.txt");
return $name;
}
static function getDomains(){
global $zdbh,$controller;
$currentuser = ctrl_users::GetUserDetail();
$formvars = $controller->GetAllControllerRequests('FORM');
$sql = "SELECT vh_directory_vc,vh_name_vc,vh_id_pk FROM x_vhosts WHERE vh_acc_fk=:user_id AND vh_deleted_ts IS NULL AND vh_name_vc IN(SELECT csr_domain FROM x_csr WHERE csr_delete IS NULL) ORDER BY vh_name_vc ASC";
$bindArray = array(':user_id' => $currentuser['userid']);
$zdbh->bindQuery($sql, $bindArray);
$rows = $zdbh->returnRows();
$count= count($rows);
if ($count > 0) {
$res = array();
foreach($rows as $row) {
$selected_yn = '';
print_r($formvars);
if(isset($formvars['domain_id'])){
$selected_yn = ($formvars['domain_id'] == $row['vh_id_pk']) ? 'selected="selected"':'';
}
array_push($res, array(
'selected' => $selected_yn,
'name' => $row['vh_name_vc'],
'id' => $row['vh_id_pk'],
));
}
return $res;
} else {
$res = array();
$res[] = array('name' => "Generate CSR", 'id' => "0");
//$res = "Generate CSR";
return $res;
}
}
static function getisDisplayResultSuccess()
{
if((isset($_GET['result']) && $_GET['result'] == 'success')){
return true;
}
return false;
}
static function getisDisplayResultFailed()
{
if((isset($_GET['result']) && $_GET['result'] == 'failed')){
return true;
}
return false;
}
static function getisDisplayLogs()
{
global $controller;
$urlvars = $controller->GetAllControllerRequests('URL');
$formvars = $controller->GetAllControllerRequests('FORM');
if((isset($urlvars['action']) && $urlvars['action'] == 'DisplayLogs')){
return true;
}
return false;
}
static function getDisplayLogs()
{
global $controller;
global $zdbh;
//runtime_csfr::Protect();
$from_val=array();
$from_val=$controller->GetAllControllerRequests('FORM');
self::$logfile = '';
if((int) $from_val['domain_id'] && $from_val['domain_id'] !="0" && (int) $from_val['log_type_id'])
{
$currentuser = ctrl_users::GetUserDetail();
$log_dir = "/etc/csr";
$sql = "SELECT csr_domain,csr_organization FROM x_csr WHERE csr_domain IN (SELECT vh_name_vc FROM x_vhosts WHERE vh_id_pk=:vh_id AND vh_acc_fk=:uid AND vh_deleted_ts IS NULL) AND csr_delete IS NULL";
$bindArray = array(':vh_id' => (int)$from_val['domain_id'], ':uid' => $currentuser['userid']);
$zdbh->bindQuery($sql, $bindArray);
$domain_info = $zdbh->returnRow();
if($from_val['log_type_id'] == 1){
$log_file = '.csr';
} elseif($from_val['log_type_id'] == 2){
$log_file = '.key';
}
$log_path = $log_dir.'/'.$domain_info['csr_domain'].'/'.$domain_info['csr_domain'].''.$log_file;
$log_path = fs_director::ConvertSlashes($log_path);
if (!file_exists($log_path)) {
self::$logpathexist = true;
return false;
}else
{
/*==============================Socket==============================*/
$service_port = 4444 ;
$address = gethostbyname('localhost');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
// echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
self::$tryagain=true;
return false;
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
// echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
self::$tryagain=true;
return false;
}
$in="command openCSR $log_path";
$out = '';
socket_write($socket, $in, strlen($in));
$output = "" ;
while(true)
{
sleep(5);
$out = socket_read($socket, 2048);
$output .= '<pre>';
$output .= $out ;
$output .= '</pre>';
if ($out)
{
socket_close($socket);
break ;
}
}
//echo $output;
//self::$logfile = $output;
//exit;
/*==============================END==============================*/
//$res = array();
//$res[] = array('Log' => "kesava", 'list' => $log_path);
//return $res;
//exit;
self::$logfile = $output;
self::$ok = true;
return $output;
}
} else {
//self::$error = true;
$output = "Please Generate CSR Certificate For Domain.. ";
self::$logfile = $output;
self::$ok = true;
return $output;
//return false;
}
}
/*=============================================================================================================*/
static function doshowLogcontent()
{
$listdomain=$_POST['inDomain'];
echo file_get_contents("/root/$listdomain.key");
fclose($file);
exit;
}
static function ListDomain($uid)
{
global $zdbh;
global $controller;
$currentuser = ctrl_users::GetUserDetail($uid);
$sql = "SELECT * FROM x_ssl WHERE ssl_delete IS NULL AND userid=:userid";
$numrows = $zdbh->prepare($sql);
$numrows->bindParam(':userid', $currentuser['userid']);
$numrows->execute();
if ($numrows->fetchColumn() > 0) {
$sql = $zdbh->prepare($sql);
$sql->bindParam(':userid', $currentuser['userid']);
$res = array();
$sql->execute();
while ($rowdomain = $sql->fetch()) {
$res[] = array('list' => $rowdomain['ssl_doamin'],
'subid' => $rowdomain['ssl_no']);
}
return $res;
} else {
return false;
}
}
static function getCurrentID()
{
global $controller;
$id = $controller->GetControllerRequest('URL', 'id');
return ($id) ? $id : '';
}
static function getCurrentDomain()
{
global $controller;
$domain = $controller->GetControllerRequest('URL', 'domain');
return ($domain) ? $domain : '';
}
static function doCreatecsr()
{
global $controller;
if (isset($_POST['inDomain'])) {
if (self::ExecuteCreateCSR($_POST['inDomain'],$_POST['inOrganization'],$_POST['inDepartment'],$_POST['inCity'],$_POST['inState'],$_POST['inCountry'])) {
self::$ok = TRUE;
return true;
}
else {
return false;
}
}
return;
}
static function ExecuteCreateCSR($domain,$organization,$department,$city,$state,$country)
{
global $zdbh;
global $controller;
$retval = FALSE;
$currentuser = ctrl_users::GetUserDetail();
$time = time();
$uid = $currentuser['userid'];
$sql = $zdbh->prepare("INSERT INTO x_csr (userid,csr_domain,csr_organization,csr_department,csr_city,csr_state,csr_country,csr_created) VALUES ($uid, '".$domain."','".$organization."','".$department."','".$city."','".$state."','".$country."','".$time."')");
$sql->execute();
$retval = TRUE;
return $retval;
}
static function docsrSock()
{
global $controller;
if (isset($_POST['inDomain'])) {
if (self::ExecutecsrSock($_POST['inDomain'],$_POST['inOrganization'],$_POST['inDepartment'],$_POST['inCity'],$_POST['inState'],$_POST['inCountry'])) {
self::$ok = TRUE;
return true;
}
else {
return false;
}
}
return;
}
static function ExecutecsrSock($domain,$organization,$department,$city,$state,$country)
{
global $zdbh;
global $controller;
$retval = FALSE;
$currentuser = ctrl_users::GetUserDetail();
//$organization = addcslashes(trim($organization, ' '));
$department = str_replace(" ","***",trim($department));
$city = str_replace(" ","***",trim($city));
$state = str_replace(" ","***",trim($state));
$organization = str_replace(" ","***",trim($organization));
/*$organization = addcslashes($organization, ' ');
$department = addcslashes($department, ' ');
$city = addcslashes($city, ' ');
$state = addcslashes($state, ' ');*/
/*==============================Socket==============================*/
$service_port = 4444 ;
$address = gethostbyname('localhost');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
// echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
self::$tryagain=true;
return false;
}
//$accept = socket_accept($socket);
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
// echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
self::$tryagain=true;
return false;
}
//socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
$in="command installCSR $domain $organization $department $city $state $country";
$out = '';
socket_write($socket, $in, strlen($in));
$output = "" ;
while(true)
{
sleep(5);
$out = socket_read($socket, 2048);
$output .= $out ;
if ($out)
{
socket_close($socket);
break ;
}
}
$domain = trim($domain);
$key_file = "/etc/csr/".$domain."/".$domain.".key";
$csr_file = "/etc/csr/".$domain."/".$domain.".csr";
if (file_exists($key_file) && file_exists($csr_file))
{
echo $output;
exit;
}
else {
$sql = "DELETE FROM x_csr WHERE csr_domain=:domain";
$numrows = $zdbh->prepare($sql);
$numrows->bindParam(':domain', $domain);
$numrows->execute();
echo "failed";
exit;
}
/*==============================END==============================*/
}
static function CheckCreateForErrors($domain)
{
global $zdbh;
// Check for spaces and remove if found...
$domain = strtolower(str_replace(' ', '', $domain));
// Check to make sure the domain is not blank before we go any further...
if ($domain == '') {
self::$blank = TRUE;
return FALSE;
}
// Check for invalid characters in the domain...
if (!self::IsValidDomainName($domain)) {
self::$badname = TRUE;
return FALSE;
}
// Check to make sure the domain is in the correct format before we go any further...
if (strpos($domain, 'www.') === 0) {
self::$error = TRUE;
return FALSE;
}
// Check to see if the domain already exists in Sentora somewhere and redirect if it does....
$sql = "SELECT COUNT(*) FROM x_vhosts WHERE vh_name_vc=:domain AND vh_deleted_ts IS NULL";
$numrows = $zdbh->prepare($sql);
$numrows->bindParam(':domain', $domain);
if ($numrows->execute()) {
if ($numrows->fetchColumn() == 0) {
self::$alreadyexists = TRUE;
return FALSE;
}
}
return TRUE;
}
static function ListDomains($uid)
{
global $zdbh;
$currentuser = ctrl_users::GetUserDetail($uid);
$sql = "SELECT * FROM x_vhosts WHERE vh_acc_fk=:userid AND vh_enabled_in=1 AND vh_deleted_ts IS NULL AND vh_name_vc NOT IN (select csr_domain from x_csr where csr_delete IS NULL And userid=:userid) ORDER BY vh_name_vc ASC";
$numrows = $zdbh->prepare($sql);
$numrows->bindParam(':userid', $currentuser['userid']);
$numrows->execute();
$domaincount = $numrows->fetchColumn();
if ($domaincount > 0) {
$sql = $zdbh->prepare($sql);
$sql->bindParam(':userid', $currentuser['userid']);
$res = array();
$sql->execute();
while ($rowdomains = $sql->fetch()) {
$res[] = array('domain' => ui_language::translate($rowdomains['vh_name_vc']));
}
return $res;
} else {
return false;
}
}
static function ListCountry($uid)
{
global $zdbh;
$currentuser = ctrl_users::GetUserDetail($uid);
$sql = "SELECT * FROM countries";
$numrows = $zdbh->prepare($sql);
$numrows->execute();
$Countrycount = $numrows->fetchColumn();
if ($Countrycount > 0) {
$sql = $zdbh->prepare($sql);
$res = array();
$sql->execute();
while ($rowCountry = $sql->fetch()) {
$res[] = array('Country' => ui_language::translate($rowCountry['name']), 'Short' => ui_language::translate($rowCountry['sortname']));
}
return $res;
} else {
return false;
}
}
static function getCountryList()
{
$currentuser = ctrl_users::GetUserDetail();
return self::ListCountry($currentuser['userid']);
}
static function getDomainList()
{
$currentuser = ctrl_users::GetUserDetail();
return self::ListDomains($currentuser['userid']);
}
static function getEmailList()
{
$currentuser = ctrl_users::GetUserDetail();
return $currentuser['email'];
// return self::ListEmail($currentuser['userid']);
}
static function getQuotaLimit()
{
$currentuser = ctrl_users::GetUserDetail();
return ($currentuser['mailboxquota'] < 0) or //-1 = unlimited
($currentuser['mailboxquota'] > ctrl_users::GetQuotaUsages('mailboxes', $currentuser['userid']));
}
static function getResult()
{
if (!fs_director::CheckForEmptyValue(self::$tryagain)) {
return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please try again Later. Server service not available."), "Error");
}
if (!fs_director::CheckForEmptyValue(self::$blank)) {
return ui_sysmessage::shout(ui_language::translate("Your Domain can not be empty. Please enter a valid Domain Name and try again."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$badname)) {
return ui_sysmessage::shout(ui_language::translate("Your Domain name is not valid. Please enter a valid Domain Name: i.e. 'domain.com'"), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$alreadyexists)) {
return ui_sysmessage::shout(ui_language::translate("Your Domain name is not registred on this server."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$error)) {
return ui_sysmessage::shout(ui_language::translate("Please remove 'www'. The 'www' will automatically work with all Domains / Subdomains."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$org)) {
return ui_sysmessage::shout(ui_language::translate("Your Organization is not valid."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$logpathexist)) {
return ui_sysmessage::shout(ui_language::translate("CSR file not generate properly. Create again."), "zannounceerror");
}
return "";
}
}