158 lines
4.3 KiB
PHP
158 lines
4.3 KiB
PHP
<?php
|
|
|
|
class App_Service_Cpanel
|
|
{
|
|
protected $_configs;
|
|
protected $_enviroments;
|
|
protected $_cpanelApi;
|
|
|
|
protected $_CPANEL_USERNAME;
|
|
protected $_CPANEL_PASSWORD;
|
|
protected $_RESTUSERNAME;
|
|
|
|
protected $_CPANEL_HOST;
|
|
|
|
private $_cronCommandBase = '/usr/bin/lynx -source %baseUrlSite%/v1/services/m/send-emails-sync &> /dev/null';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->_configs = Zend_Registry::get('config');
|
|
$this->_enviroments = Zend_Registry::get('ENVIROMENTS');
|
|
|
|
$this->_cronCommandBase = str_replace('%baseUrlSite%', $this->_enviroments->get('baseUrlSite'), $this->_cronCommandBase);
|
|
|
|
$this->_CPANEL_HOST = $this->_enviroments->get('cpanel')->get('host');
|
|
$this->_CPANEL_USERNAME = $this->_enviroments->get('cpanel')->get('username');
|
|
$this->_CPANEL_PASSWORD = $this->_enviroments->get('cpanel')->get('password');
|
|
$this->_RESTUSERNAME = $this->_enviroments->get('cpanel')->get('restusername');
|
|
|
|
$this->_cpanelApi = new App_Service_CpanelApi($this->_CPANEL_USERNAME, $this->_CPANEL_PASSWORD, $this->_CPANEL_HOST);
|
|
}
|
|
|
|
public function listCron()
|
|
{
|
|
|
|
try {
|
|
|
|
$res = $this->_cpanelApi->api2->Cron->listcron();
|
|
|
|
if ($res->errors) {
|
|
if (is_array($res->errors))
|
|
return implode(';', $res->errors);
|
|
else
|
|
return "ERRORE: richiesta elenco funzioni cron --- " . $res->errors;
|
|
}
|
|
|
|
return $res;
|
|
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function attivaFunzioneCron()
|
|
{
|
|
|
|
try {
|
|
|
|
$res = $this->_cpanelApi->api2->Cron->add_line(array(
|
|
'command' => $this->_cronCommandBase,
|
|
'day' => '*',
|
|
'hour' => '*',
|
|
'minute' => '*/2', // Una volta ogni due minuti
|
|
'month' => '*',
|
|
'weekday' => '*',
|
|
));
|
|
|
|
if ($res->errors) {
|
|
if (is_array($res->errors))
|
|
return implode(';', $res->errors);
|
|
else
|
|
return "ERRORE: attivazione cron sync ordini --- " . $res->errors;
|
|
}
|
|
|
|
$data = $res->cpanelresult->data[0];
|
|
|
|
if($data->status == 1){
|
|
return ['stato'=>true,'message'=>'Sincronizzazione attivata'];
|
|
}elseif($data->status === 0 && $data->statusmsg == "This cron job already exists.") {
|
|
return ['stato'=>true,'message'=>'Sincronizzazione già attiva'];
|
|
}else{
|
|
return ['stato'=>false,'message'=>'Errore durante l\'operazione di sincronizzazione'];
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function disattivaFunzioneCron()
|
|
{
|
|
|
|
try {
|
|
|
|
$res = null;
|
|
$cronList = $this->listCron();
|
|
|
|
$x = 0;
|
|
foreach ($cronList->cpanelresult->data as $cron) {
|
|
$x++;
|
|
if ($cron->command === $this->_cronCommandBase) {
|
|
$res = $this->removeCron($x);
|
|
}
|
|
}
|
|
|
|
$data = $res->cpanelresult->data[0];
|
|
|
|
if($data !== null && $data->status === 1){
|
|
return ['stato'=>false,'message'=>'Sincronizzazione disattivata'];
|
|
}else{
|
|
return ['stato'=>false,'message'=>'Sincronizzazione non attiva'];
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function removeCron($lineKey)
|
|
{
|
|
|
|
try {
|
|
|
|
$res = $this->_cpanelApi->api2->Cron->remove_line(array('line' => $lineKey));
|
|
|
|
if ($res->errors) {
|
|
if (is_array($res->errors))
|
|
return implode(';', $res->errors);
|
|
else
|
|
return "ERRORE: rimozione funzione cron --- " . $res->errors;
|
|
}
|
|
|
|
return $res;
|
|
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function checkFunzioneCronIsActive()
|
|
{
|
|
try {
|
|
$listCron = $this->listCron();
|
|
foreach ($listCron->cpanelresult->data as $cron){
|
|
if($cron->command === $this->_cronCommandBase) return true;
|
|
}
|
|
return false;
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
}
|
|
|
|
|
|
}
|