Browse Source

Modification de l'exécution des crons

master
Bastien 8 years ago
committed by Squiz
parent
commit
c2c0eecdec
8 changed files with 127 additions and 113 deletions
  1. +18
    -34
      inc/cron.php
  2. +2
    -0
      inc/exceptions.php
  3. +55
    -20
      inc/module.php
  4. +3
    -1
      inc/mvc.php
  5. +5
    -3
      inc/parser.php
  6. +30
    -18
      index.php
  7. +0
    -35
      modules/user/cron.php
  8. +14
    -2
      modules/user/mvc.php

+ 18
- 34
inc/cron.php View File

@ -1,41 +1,25 @@
<?php
abstract class cron {
protected $database;
protected $model;
class cron {
public static function install($module, $cron) {
if(!module::is_installed($module))
die('Le module n\'est pas installé'."\n");
if(exec('crontab -l | grep "'.$cron.'"') != '')
die('La cron est déjà installée'."\n");
exec('echo -e "$(crontab -l)\n'.$cron.'" > /tmp/cron; crontab /tmp/cron; rm /tmp/cron');
}
public static function uninstall($module, $cron) {
if(module::is_installed($module))
die('Le module n\'est pas désinstallé'."\n");
if(exec('crontab -l | grep "'.$cron.'"') == '')
die('La cron est déjà désinstallée'."\n");
exec('echo -e "$(crontab -l)" | grep -v "'.$cron.'" > /tmp/cron; crontab /tmp/cron; rm /tmp/cron');
public static function add($time, $params) {
$params_raw = '';
foreach($params as $key => $value)
$params_raw .= '--'.$key.' '.$value . ' ';
$cron_raw = $time . ' '. config::read_config('cron', 'php_path') . ' ' . $_SERVER['PWD'].'/'.$_SERVER['PHP_SELF']. ' ' . $params_raw;
echo $cron_raw;
if(exec('crontab -l | grep "'.$cron_raw.'"') != '')
throw new cron_already_exists();
exec('echo -e "$(crontab -l)\n'.$cron_raw.'" > /tmp/cron; crontab /tmp/cron; rm /tmp/cron');
}
public function __construct($module,$action) {
try {
$this->database = new database();
$this->database->connect(config::read_config('database', 'url'), config::read_config('database', 'login'), config::read_config('database', 'password'));
} catch(PDOException $e) {
die('PDOException : '.$e->getMessage());
} catch(config_section_not_found $e) {
die('config_section_not_found');
} catch(config_key_not_found $e) {
die('config_key_not_found');
} catch(Exception $e) {
die('erreur :'.$e.getMessage());
}
module::load('user');
$class_model = $module.'_model';
$this->model = new $class_model(null, $this->database);
$this->execute($action);
public static function delete($time, $params) {
$params_raw = '';
foreach($params as $key => $value)
$params_raw .= '--'.$key.' '.$value . ' ';
$cron_raw = $time . ' '. config::read_config('cron', 'php_path') . ' ' . $_SERVER['PWD'].'/'.$_SERVER['PHP_SELF']. ' ' . $params_raw;
if(exec('crontab -l | grep "'.$cron_raw.'"') == '')
throw new cron_not_found();
exec('echo -e "$(crontab -l)" | grep -v "'.$cron_raw.'" > /tmp/cron; crontab /tmp/cron; rm /tmp/cron');
}
abstract public function execute($action);
}

+ 2
- 0
inc/exceptions.php View File

@ -3,6 +3,8 @@ class module_not_found extends Exception {}
class variable_not_found extends Exception { }
class array_duplicate_key extends Exception { }
class array_bad_length extends Exception {}
class cron_already_exists extends Exception {}
class cron_not_found extends Exception {}
class sql_error extends PDOException {
public function __construct(PDOException $e) {


+ 55
- 20
inc/module.php View File

@ -16,46 +16,72 @@ class module {
}
public static function main($module_name, database $database, raintpl $tpl) {
public static function main($var_get, database $database, raintpl $tpl) {
/* on charge le module */
module::load($module_name);
switch((self::is_installed($module_name)?1:0)+(isset($_GET['install'])==true?2:0)+(isset($_GET['uninstall'])?4:0)) {
module::load($var_get['module']);
switch((self::is_installed($var_get['module'])?1:0)+($var_get['install']==true?2:0)+($var_get['uninstall']==true?4:0)+($var_get['cli']==true?8:0)) {
case 1: /* le module est installé, cas nominal */
self::execute($module_name, $database, $tpl);
case 9: /* le module est installé, on le lance en cron */
self::execute($var_get['module'], $database, $tpl, $var_get['cli'], $var_get['action']);
break;
case 0: /* pas installé, mais on ne demande pas de l'installer */
$view = new view(null, $tpl);
$view->print_header('Module « '.htmlspecialchars($module_name).' » non installé');
$view->print_message('le module '.htmlspecialchars($module_name).' n\'est pas installé. <a href="'.model::url(array('module' => htmlspecialchars($module_name), 'install' => '')).'">installer le module</a>');
$view->print_header('Module « '.htmlspecialchars($var_get['module']).' » non installé');
$view->print_message('le module '.htmlspecialchars($var_get['module']).' n\'est pas installé. <a href="'.model::url(array('module' => htmlspecialchars($var_get['module']), 'install' => '')).'">installer le module</a>');
$view->print_footer();
break;
case 3: /* on demande à installer un module déjà installé */
$view = new view(null, $tpl);
$view->print_header('Installation du module « '.htmlspecialchars($module_name).' »');
$view->print_message('le module '.htmlspecialchars($module_name).' est déjà installé');
$view->print_header('Installation du module « '.htmlspecialchars($var_get['module']).' »');
$view->print_message('le module '.htmlspecialchars($var_get['module']).' est déjà installé');
$view->print_footer();
break;
case 6: /* on demande à la fois d'installer et de désinstaller un module non installé */
case 7: /* on demande à la fois d'installer et de désinstaller un module déjà installé */
$view = new view(null, $tpl);
$view->print_header('Installation ou désinstallation du module « '.htmlspecialchars($module_name).' »');
$view->print_header('Installation ou désinstallation du module « '.htmlspecialchars($var_get['module']).' »');
if(isset($_SESSION['user']) && $_SESSION['user'] instanceof admin)
$view->print_message('il faut choisir : <a href="'.model::url(array('module' => htmlspecialchars($module_name), 'install' => '')).'">installer le module</a> <a href="'.model::url(array('module' => htmlspecialchars($module_name), 'uninstall' => '')).'">désinstaller le module</a>');
$view->print_message('il faut choisir : <a href="'.model::url(array('module' => htmlspecialchars($var_get['module']), 'install' => '')).'">installer le module</a> <a href="'.model::url(array('module' => htmlspecialchars($var_get['module']), 'uninstall' => '')).'">désinstaller le module</a>');
else
$view->print_message_forbidden();
$view->print_footer();
break;
case 2: /* on demande à installer un module non installé */
self::install($module_name, $database, $tpl);
self::install($var_get['module'], $database, $tpl);
break;
case 4: /* on demande à désinstaller un module non installé */
case 5: /* on demande à désinstaller un module installé */
self::uninstall($module_name, $database, $tpl);
self::uninstall($var_get['module'], $database, $tpl);
break;
case 8:
die('la cron n\'est pas installée');
break;
case 10: /* on demande à installer la cron mais le module n'est pas installé */
die('le module doit etre installé avant la cron');
break;
case 11: /* on demande à installer la cron et le module est installé */
$class_controler = $var_get['module'].'_controler';
$controler = new $class_controler($database, $tpl);
$controler->install_cron();
break;
case 12: /* on demande à désinstaller la cron et le module est déjà désinstallé */
case 13: /* prov */
$class_controler = $var_get['module'].'_controler';
$controler = new $class_controler($database, $tpl);
$controler->uninstall_cron();
break;
case 13: /* on demande à désinstaller la cron, le module est installé */
die('le module doit etre désinstallé avant la cron');
break;
case 14: /* on demande à désinstaller et à insaller */
case 15:
die('il faut choisir !');
break;
}
}
public static function execute($module_name, database $database, raintpl $tpl) {
public static function execute($module_name, database $database, raintpl $tpl, $cli, $action) {
$class_controler = $module_name.'_controler';
$controler = new $class_controler($database, $tpl);
@ -63,20 +89,29 @@ class module {
$controler->action_confirmation('','','index');
/* on récupère l'action */
$action = (isset($_GET['action']) && !empty($_GET['action']))?$_GET['action']:config::read_config('modules', 'action_default');
if(empty($action) || is_null($action))
$action = config::read_config('modules', 'action_default');
if($action != config::read_config('modules', 'action_default') && (!isset($controler->route[$action]) || !isset($controler->route[$action]['function'])))
$controler->view->print_message('action invalide');
else {
if($action == config::read_config('modules', 'action_default'))
$function_name = 'controler_default';
else
$function_name = $controler->route[$action]['function'];
$function_name = $controler->route[$action]['function'];
if($action != config::read_config('modules', 'action_default') && $cli && (!isset($controler->route[$action]['action_cron']) || $controler->route[$action]['action_cron'] == false)) /* on vérifie l'environnement d'exécution */ {
$controler->view->print_message('l\'action ne peut pas etre exécutée en cli');
exit;
}
else if($action != config::read_config('modules', 'action_default') && !$cli && isset($controler->route[$action]['action_cron']) && $controler->route[$action]['action_cron'] == true) {
$controler->view->print_message('l\'action doit etre exécutée en cli');
exit;
}
/* on vérifie que la fonction existe et on appelle */
if(method_exists($controler, $function_name))
$controler->$function_name();
$controler->$function_name(); /* exécution */
else
$controler->view->print_message('action invalide');
$controler->view->print_message('action invalide');
}
}
public static function install($module_name, database $database, raintpl $tpl) {
$view = new view(null, $tpl);
if($module_name == 'user' || (isset($_SESSION['user']) && $_SESSION['user'] instanceof admin)) {


+ 3
- 1
inc/mvc.php View File

@ -161,7 +161,7 @@ class view {
$this->tpl->assign('link_session_list_intervenant', model::url(array('module' => 'session', 'action' => 'list', 'user_intervenant' => isset($_SESSION['user'])?$_SESSION['user']->__get('id'):false)));
$this->tpl->assign('link_list_formations', model::url(array('module' => 'formation', 'action' => 'list')));
$this->tpl->draw('templates/header');
}
@ -185,6 +185,8 @@ abstract class controler {
protected $route;
abstract public function install();
abstract public function uninstall();
abstract public function install_cron();
abstract public function uninstall_cron();
public function __get($name) {
return $this->$name;


+ 5
- 3
inc/parser.php View File

@ -1,6 +1,7 @@
<?php
class parser {
public static function to_html($text) {
$text = htmlspecialchars($text);
$text = preg_replace('/=====(.*?)=====/', '<h4>$1</h4>', $text);
$text = preg_replace('/====(.*?)====/', '<h3>$1</h3>', $text);
$text = preg_replace('/===(.*?)===/', '<h2>$1</h2>', $text);
@ -20,7 +21,8 @@ class parser {
$text = '<p>'.$text.'</p>';
return $text;
}
public static function to_latex($text) {
public static function to_latex($text) {
$text = str_replace('\\', '\\\\', $text);
$text = str_replace('{', '\{', $text);
$text = str_replace('}', '\}', $text);
@ -38,11 +40,11 @@ class parser {
$text = preg_replace('/\*\*(.*?)\*\*/', '\textbf{$1}', $text);
$text = preg_replace('/__(.*?)__/', '\underline{$1}', $text);
$text = preg_replace('#\/\/(.*?)\/\/#', '\textit{$1}', $text);
$text = preg_replace('/\[(.*?) (.*?)\]/', '\url{$1} title="$2">$2</a>', $text);
$text = preg_replace('/\[(.*?) (.*?)\]/', '\url{$1}{$2}', $text);
$text = preg_replace("/ \* (.+)(\r\n|$)/", "\begin{itemize}\item $1\n\end{itemize}", $text);
$text = str_replace('\end{itemize}\begin{itemize}', '', $text);
$text = preg_replace("/ \* (.+)(\r\n|$)/", "\begin{enumerate}\item $1\n\end{enumerate}", $text);
$text = preg_replace("/ \+ (.+)(\r\n|$)/", "\begin{enumerate}\item $1\n\end{enumerate}", $text);
$text = str_replace('\end{enumerate}\begin{enumerate}', '', $text);
$text = preg_replace('/(\r\n){2,}/', "\n\n", $text);


+ 30
- 18
index.php View File

@ -1,9 +1,4 @@
<?php
/*if(!isset($_GET['install']) && !isset($_GET['uninstall'])) {
echo 'La base de données est en cours de modifications !';
exit;
}*/
require_once('inc/exceptions.php');
require_once('inc/config.php');
require_once('inc/database.php');
@ -19,11 +14,13 @@ ob_start();
/* on restaure les objets de la session */
module::load_data();
/* on charge la session */
ini_set('session.name', config::read_config('session', 'cookie_name'));
ini_set('session.hash_function', 1 /* SHA-1 */);
ini_set('session.cookie_httponly', 1);
session_start();
if(php_sapi_name() != 'cli') {
/* on charge la session */
ini_set('session.name', config::read_config('session', 'cookie_name'));
ini_set('session.hash_function', 1 /* SHA-1 */);
ini_set('session.cookie_httponly', 1);
session_start();
}
/* connexion à la base de données */
try {
@ -44,20 +41,35 @@ $tpl = new raintpl();
raintpl::configure( 'tpl_dir', '' );
/* on charge le module demandé */
if(!array_key_exists('module', $_GET))
die('You must indicate a module');
$params = array();
$var_get;
if(php_sapi_name() != 'cli') {
$var_get = &$_GET;
$params['cli'] = false;
} else {
$var_get = getopt('', array('install', 'uninstall','action:','module:'));
$params['cli'] = true;
}
if(isset($var_get['module']))
$params['module'] = $var_get['module'];
else
die('You must specify a module');
$params['action'] = isset($var_get['action'])?$var_get['action']:null;
$params['install'] = isset($var_get['install']);
$params['uninstall'] = isset($var_get['uninstall']);
try {
module::main($_GET['module'], $database, $tpl);
module::main($params, $database, $tpl);
} catch(module_not_found $e) {
die('Module "'.$_GET['module'].'" does not exist');
die('Module "'.$var_get['module'].'" does not exist');
}
/*
catch(Exception $e) {
die('erreur');
}
*/
echo '<br />Debug : <pre>';
print_r($_SESSION);
echo '</pre>';
if(php_sapi_name() != 'cli') {
echo '<br />Debug : <pre>';
print_r($_SESSION);
echo '</pre>';
}
ob_end_flush();

+ 0
- 35
modules/user/cron.php View File

@ -1,35 +0,0 @@
<?php
require_once('inc/exceptions.php');
require_once('inc/config.php');
require_once('inc/database.php');
require_once('inc/sql_types.php');
require_once('inc/mvc.php');
require_once('inc/module.php');
require_once('inc/cron.php');
require_once('modules/user/data.php');
class user_cron extends cron {
public function execute($action) {
switch($action) {
case 'delete_user_not_validated':
$this->model->delete_not_validated_account_participant(config::read_config('module_user', 'max_days_account_not_validated'));
break;
default:
die('action invalide'."\n");
}
}
}
$options = getopt('', array('install', 'uninstall','action:'));
if(isset($options['install'])) {
$action = 'delete_user_not_validated';
$cron = '@daily '.config::read_config('cron', 'php_path').' '.$_SERVER['PWD'].'/'.$_SERVER['SCRIPT_NAME'].' --action '.$action;
user_cron::install('user', $cron);
} else if(isset($options['uninstall'])) {
$action = 'delete_user_not_validated';
$cron = '@daily '.config::read_config('cron', 'php_path').' '.$_SERVER['PWD'].'/'.$_SERVER['SCRIPT_NAME'].' --action '.$action;
user_cron::uninstall('user', $cron);
} else {
if(!isset($options['action']))
throw new variable_not_found('l\'option action n\'a pas été renseignée');
$cron = new user_cron('user',$options['action']);
}

+ 14
- 2
modules/user/mvc.php View File

@ -467,7 +467,8 @@ class user_controler extends controler {
'edit' => array('function' => 'controler_edit', 'filter_url' => array('user')),
'list' => array('function' => 'controler_list', 'filter_url' => array('sort', 'asc', 'page', 'user', 'type')),
'enable' => array('function' => 'controler_enable', 'filter_url' => array('user')),
'disable' => array('function' => 'controler_disable', 'filter_url' => array('user'))
'disable' => array('function' => 'controler_disable', 'filter_url' => array('user')),
'delete_not_validated_account_participant' => array('function' => 'delete_not_validated_account_participant', 'filter_url' => array(), 'action_cron' => true)
);
}
@ -477,7 +478,14 @@ class user_controler extends controler {
public function uninstall() {
$this->model->uninstall();
}
public function install_cron() {
require_once('inc/cron.php');
cron::add('@daily', array('module' => 'user', 'action' => 'delete_not_validated_account_participant'));
}
public function uninstall_cron() {
require_once('inc/cron.php');
cron::delete('@daily', array('module' => 'user', 'action' => 'delete_not_validated_account_participant'));
}
public function controler_login() {
$this->view->print_header('Login');
if(isset($_SESSION['user']))
@ -845,4 +853,8 @@ class user_controler extends controler {
return $this->view->user_select($this->model->list_account($type, $page=1, $order_by='id', $order_by_asc=true, $nb_item_per_page=-1), $multiple_selection, $field_name, $selection, $return_string=true);
}
public function delete_not_validated_account_participant() {
$this->model->delete_not_validated_account_participant(config::read_config('module_user', 'max_days_account_not_validated'));
}
}

Loading…
Cancel
Save