Skip to content
Snippets Groups Projects
Commit db661940 authored by Alex ORLUC's avatar Alex ORLUC
Browse files

FIX #5563 add if id BASKETS create notif scripts

parent 9e676a20
No related branches found
No related tags found
No related merge requests found
<?php <?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
/* * @brief class_schedule_notifications_Abstract
* Copyright 2008-2016 Maarch * @author dev <dev@maarch.org>
* * @ingroup core
* This file is part of Maarch Framework.
*
* Maarch Framework 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.
*
* Maarch Framework 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 Maarch Framework. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** abstract class ScheduleNotifications_Abstract
* Class for schedule notifications {
*
* function getCrontab()
* @package maarch {
* @version 1.5 $crontab = shell_exec('crontab -l');
* @since 01/2015 $lines = explode("\n", $crontab);
* @license GPL v3 $data = array();
* @author <dev@maarch.org>
* foreach ($lines as $id => $l) {
*/ $l = trim($l);
if (strpos($l, '#') !== false)
$l = substr($l, 0, strpos($l, '#'));
if (empty($l))
continue;
$l = preg_replace('![ \t]+!', ' ', $l);
if ($l[0] == '@')
list($time, $cmd) = explode(' ', $l, 2);
else
list($m, $h, $dom, $mon, $dow, $cmd) = explode(' ', $l, 6);
$data[$id] = array(
'm' => $m,
'h' => $h,
'dom' => $dom,
'mon' => $mon,
'dow' => $dow,
'cmd' => $cmd,
);
if ($_SESSION['custom_override_id'] <> '') {
$pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/';
} else {
$pathToFolow = $_SESSION['config']['corepath'];
}
if (strpos($data[$id]['cmd'], $pathToFolow.'modules/notifications/batch/scripts/') !== 0 ) {
$data[$id]['state'] = 'hidden';
}
}
return $data;
}
function saveCrontab($data, $delete=false)
{
foreach ($data AS $id => $d) {
if ($d['state'] == 'deleted') {
unset($file[$id]);
} else
$file[$id] = "{$d['m']}\t{$d['h']}\t{$d['dom']}\t{$d['mon']}\t{$d['dow']}\t{$d['cmd']}";
}
$output = '';
if (isset($file)) {
foreach ($file as $l)
$output .= "$l\n";
}
$output = preg_replace("!\n+$!", "\n", $output);
file_put_contents('/tmp/crontab.plain', print_r($file, true));
file_put_contents('/tmp/crontab.txt', $output);
exec('crontab /tmp/crontab.txt');
abstract class ScheduleNotifications_Abstract{ $core_tools = new core_tools();
$core_tools->load_lang();
function getCrontab(){
$crontab = shell_exec('crontab -l'); if (!$delete) {
$lines = explode("\n", $crontab); $_SESSION['info'] = _CRONTAB_SAVED;
$data = array(); }
foreach ($lines as $id => $l) { return 0;
$l = trim($l); }
if (strpos($l, '#') !== false)
$l = substr($l, 0, strpos($l, '#')); function getAuthorizedNotifications()
if (empty($l)) {
continue; include_once "core/class/class_request.php";
$l = preg_replace('![ \t]+!', ' ', $l); $db = new Database();
if ($l[0] == '@') $stmt = $db->query("SELECT notification_sid, description FROM notifications WHERE is_enabled = 'Y'");
list($time, $cmd) = explode(' ',$l, 2); $notificationsArray = array();
else
list($m, $h, $dom, $mon, $dow, $cmd) = explode(' ',$l, 6); while ($result = $stmt->fetchObject()) {
$data[$id] = array(
'm' => $m,
'h' => $h,
'dom' => $dom,
'mon' => $mon,
'dow' => $dow,
'cmd' => $cmd,
);
if ($_SESSION['custom_override_id'] <> '') {
$pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/';
} else {
$pathToFolow = $_SESSION['config']['corepath'];
}
if ( strpos($data[$id]['cmd'], $pathToFolow.'modules/notifications/batch/scripts/') !== 0 ) {
$data[$id]['state'] = 'hidden';
}
}
return $data;
}
function saveCrontab($data, $delete=false){
foreach($data AS $id => $d) {
if ($d['state'] == 'deleted') {
unset($file[$id]);
} else
$file[$id] = "{$d['m']}\t{$d['h']}\t{$d['dom']}\t{$d['mon']}\t{$d['dow']}\t{$d['cmd']}";
}
$output = '';
if (isset($file)) {
foreach ($file as $l)
$output .= "$l\n";
}
$output = preg_replace("!\n+$!", "\n", $output);
file_put_contents('/tmp/crontab.plain', print_r($file, true));
file_put_contents('/tmp/crontab.txt', $output);
exec('crontab /tmp/crontab.txt');
$core_tools = new core_tools();
$core_tools->load_lang();
if (!$delete) {
$_SESSION['info'] = _CRONTAB_SAVED;
}
return 0;
}
function getAuthorizedNotifications (){
require_once ("core/class/class_request.php");
$db = new Database();
$stmt = $db->query("SELECT notification_sid, description FROM notifications WHERE is_enabled = 'Y'");
$notificationsArray = array();
while($result = $stmt->fetchObject()){
$filename = "notification"; $filename = "notification";
if (isset($_SESSION['custom_override_id']) && $_SESSION['custom_override_id']<>"") { if (isset($_SESSION['custom_override_id']) && $_SESSION['custom_override_id']<>"") {
$filename.="_".str_replace(" ", "", $_SESSION['custom_override_id']); $filename.="_".str_replace(" ", "", $_SESSION['custom_override_id']);
} }
$filename.="_".$result->notification_sid.".sh"; $filename.="_".$result->notification_sid.".sh";
if ($_SESSION['custom_override_id'] <> '') { if ($_SESSION['custom_override_id'] <> '') {
$pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/'; $pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/';
} else { } else {
$pathToFolow = $_SESSION['config']['corepath']; $pathToFolow = $_SESSION['config']['corepath'];
} }
$path = $pathToFolow.'modules/notifications/batch/scripts/'.$filename; $path = $pathToFolow.'modules/notifications/batch/scripts/'.$filename;
if (file_exists($path)) { if (file_exists($path)) {
$notificationsArray[$path] = $result->description; $notificationsArray[$path] = $result->description;
} }
} }
return $notificationsArray; return $notificationsArray;
} }
function createScriptNotification($notification_sid, $notification_id){ function createScriptNotification($notification_sid, $notification_id)
{
//Creer le script sh pour les notifications //Creer le script sh pour les notifications
$filename = "notification"; $filename = "notification";
if (isset($_SESSION['custom_override_id']) && $_SESSION['custom_override_id']<>"") { if (isset($_SESSION['custom_override_id']) && $_SESSION['custom_override_id']<>"") {
...@@ -152,16 +135,16 @@ abstract class ScheduleNotifications_Abstract{ ...@@ -152,16 +135,16 @@ abstract class ScheduleNotifications_Abstract{
} }
if ($_SESSION['custom_override_id'] <> '') { if ($_SESSION['custom_override_id'] <> '') {
$pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/'; $pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/';
//shell_exec("mkdir " . escapeshellarg($pathToFolow.'modules/notifications/batch/scripts/')); //shell_exec("mkdir " . escapeshellarg($pathToFolow.'modules/notifications/batch/scripts/'));
if(!file_exists($pathToFolow.'modules/notifications/batch/scripts/')){ if (!file_exists($pathToFolow.'modules/notifications/batch/scripts/')) {
mkdir($pathToFolow.'modules/notifications/batch/scripts/',0777,true); mkdir($pathToFolow.'modules/notifications/batch/scripts/', 0777, true);
} }
$file_open = fopen($pathToFolow.'modules/notifications/batch/scripts/'.$filename, 'w+'); $file_open = fopen($pathToFolow.'modules/notifications/batch/scripts/'.$filename, 'w+');
} else { } else {
$pathToFolow = $_SESSION['config']['corepath']; $pathToFolow = $_SESSION['config']['corepath'];
$file_open = fopen($pathToFolow.'modules/notifications/batch/scripts/'.$filename, 'w+'); $file_open = fopen($pathToFolow.'modules/notifications/batch/scripts/'.$filename, 'w+');
} }
fwrite($file_open, '#!/bin/sh'); fwrite($file_open, '#!/bin/sh');
...@@ -170,48 +153,54 @@ abstract class ScheduleNotifications_Abstract{ ...@@ -170,48 +153,54 @@ abstract class ScheduleNotifications_Abstract{
fwrite($file_open, "\n"); fwrite($file_open, "\n");
fwrite($file_open, 'cd $path'); fwrite($file_open, 'cd $path');
fwrite($file_open, "\n"); fwrite($file_open, "\n");
fwrite($file_open, 'php \'process_event_stack.php\' -c '.$ConfigNotif.' -n '.$notification_id); if ($notification_id == 'BASKETS') {
fwrite($file_open, 'php \'basket_event_stack.php\' -c '.$ConfigNotif.' -n '.$notification_id);
} else {
fwrite($file_open, 'php \'process_event_stack.php\' -c '.$ConfigNotif.' -n '.$notification_id);
}
fwrite($file_open, "\n"); fwrite($file_open, "\n");
fwrite($file_open, 'cd $path'); fwrite($file_open, 'cd $path');
fwrite($file_open, "\n"); fwrite($file_open, "\n");
fwrite($file_open, 'php \'process_email_stack.php\' -c '.$ConfigNotif); fwrite($file_open, 'php \'process_email_stack.php\' -c '.$ConfigNotif);
fwrite($file_open, "\n"); fwrite($file_open, "\n");
fclose($file_open); fclose($file_open);
shell_exec("chmod +x " shell_exec(
. escapeshellarg($pathToFolow . "modules/notifications/batch/scripts/" . $filename) "chmod +x "
. escapeshellarg($pathToFolow . "modules/notifications/batch/scripts/" . $filename)
); );
} }
function checkCrontab($crontabToSave){ function checkCrontab($crontabToSave)
{
$crontabBeforeSave = $this->getCrontab();
$error = 0; $crontabBeforeSave = $this->getCrontab();
foreach ($crontabToSave as $id => $e) { $error = 0;
if ($e['state'] == "deleted"){ foreach ($crontabToSave as $id => $e) {
// nothing to do if ($e['state'] == "deleted") {
} else if($e['state'] == "new" || $e['state'] == "normal"){ // nothing to do
} else if ($e['state'] == "new" || $e['state'] == "normal") {
if ($_SESSION['custom_override_id'] <> '') {
$pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/'; if ($_SESSION['custom_override_id'] <> '') {
} else { $pathToFolow = $_SESSION['config']['corepath'] . 'custom/'.$_SESSION['custom_override_id'] . '/';
$pathToFolow = $_SESSION['config']['corepath']; } else {
} $pathToFolow = $_SESSION['config']['corepath'];
if(strpos($crontabToSave[$id]['cmd'], $pathToFolow.'modules/notifications/batch/scripts/') !== 0) { }
$error = 1; if (strpos($crontabToSave[$id]['cmd'], $pathToFolow.'modules/notifications/batch/scripts/') !== 0) {
break; $error = 1;
} break;
} else if($e['state'] == "hidden"){ }
if ($e['cmd'] != $crontabBeforeSave[$id]['cmd']) { } else if ($e['state'] == "hidden") {
$error = 1; if ($e['cmd'] != $crontabBeforeSave[$id]['cmd']) {
break; $error = 1;
} break;
} else { }
$error = 1; } else {
break; $error = 1;
} break;
} }
}
return $error;
} return $error;
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment