37 |
*/ |
*/ |
38 |
class utils extends application { |
class utils extends application { |
39 |
|
|
40 |
/** |
// {{{ |
|
* Constructeur |
|
|
* |
|
|
* @param |
|
|
* @param |
|
|
* @param |
|
|
* @param |
|
|
* @param |
|
|
*/ |
|
|
function __construct($flag = NULL, $right = NULL, $title = NULL, $icon = NULL, $help = NULL) { |
|
|
|
|
|
// |
|
|
$this->timestart = microtime(true); |
|
|
|
|
|
// Logger |
|
|
$this->addToLog("__construct() : 0.000 sec", VERBOSE_MODE); |
|
|
|
|
|
// Logger |
|
|
$this->addToLog("__construct()", EXTRA_VERBOSE_MODE); |
|
|
|
|
|
// Instanciation de la classe message |
|
|
$this->m = new message(); |
|
41 |
|
|
42 |
// |
var $om_utilisateur = array(); |
43 |
$this->setParamsFromFiles(); |
var $user_is_instr = NULL; |
44 |
$this->checkParams(); |
var $user_is_service_ext = NULL; |
|
|
|
|
// |
|
|
$this->setDefaultValues(); |
|
|
|
|
|
// Transformation des cinq éléments paramètres en attribut de l'objet |
|
|
$this->setFlag($flag); |
|
|
$this->setTitle($title); |
|
|
$this->setRight($right); |
|
|
$this->setHelp($help); |
|
|
$this->setIcon($icon); |
|
|
|
|
|
// Vérification de l'authentification de l'utilisateur et stockage du |
|
|
// résultat en attribut de l'objet |
|
|
$this->authenticated = $this->isAuthenticated(); |
|
|
|
|
|
// Déconnexion de l'utilisateur |
|
|
if ($this->flag == "logout") { |
|
|
$this->logout(); |
|
|
} |
|
45 |
|
|
46 |
// Connexion de l'utilisateur |
/** |
47 |
if ($this->flag == "login") { |
* Méthode de récupération des informations de l'utilisateur connecté. |
48 |
$this->login(); |
*/ |
49 |
} |
function getUserInfos() { |
50 |
|
|
51 |
// Demande de redéfinition du mot de passe |
// Si l'utilisateur est loggé $_SESSION existe |
52 |
if ($this->flag == "password_reset") { |
if(isset($_SESSION['login']) AND !empty($_SESSION['login'])) { |
53 |
if ($this->authenticated) { |
|
54 |
$this->redirectAuthenticatedUsers(); |
// Récupération des infos utilisateur |
55 |
|
$sqlUser = "SELECT om_utilisateur, nom, email, login, om_collectivite, om_profil ". |
56 |
|
"FROM om_utilisateur WHERE login = '".$_SESSION['login']."'"; |
57 |
|
$resUser=$this->db->query($sqlUser); |
58 |
|
$this->addToLog("getUserInfos(): db->query(\"".$sqlUser."\");", VERBOSE_MODE); |
59 |
|
$this->isDatabaseError($resUser); |
60 |
|
$this->om_utilisateur=&$resUser->fetchRow(DB_FETCHMODE_ASSOC); |
61 |
|
|
62 |
|
// Récupération des infos instructeur |
63 |
|
$sqlInstr = "SELECT instructeur.instructeur, instructeur.nom, instructeur.telephone, division.code, division.libelle ". |
64 |
|
"FROM instructeur INNER JOIN division ON division.division=instructeur.division ". |
65 |
|
"WHERE instructeur.om_utilisateur = ".$this->om_utilisateur['om_utilisateur']; |
66 |
|
$resInstr=$this->db->query($sqlInstr); |
67 |
|
$this->addToLog("getUserInfos(): db->query(\"".$sqlInstr."\");", VERBOSE_MODE); |
68 |
|
$this->isDatabaseError($resInstr); |
69 |
|
$tempInstr=&$resInstr->fetchRow(DB_FETCHMODE_ASSOC); |
70 |
|
// Si il y a un resultat c'est un instructeur |
71 |
|
if(count($tempInstr)>0) { |
72 |
|
$this->user_is_instr=true; |
73 |
|
$this->om_utilisateur = array_merge($this->om_utilisateur,$tempInstr); |
74 |
|
} else { |
75 |
|
$this->user_is_instr=false; |
76 |
} |
} |
77 |
} |
|
78 |
|
// Récupération des infos de services ext consultés |
79 |
// |
$sqlServExt = "SELECT service.service, service.abrege, service.libelle ". |
80 |
if ($this->authenticated) { |
"FROM service ". |
81 |
// Connexion à la base de données si l'utilisateur est authentifié |
"INNER JOIN lien_service_om_utilisateur ON lien_service_om_utilisateur.service=service.service ". |
82 |
$this->connectDatabase(); |
"WHERE lien_service_om_utilisateur.om_utilisateur = ".$this->om_utilisateur['om_utilisateur']; |
83 |
// on verifie que l'utilisateur connecté est toujours valide |
$resServExt=$this->db->query($sqlServExt); |
84 |
if (!defined('REST_REQUEST')) { |
$this->addToLog("getUserInfos(): db->query(\"".$sqlServExt."\");", VERBOSE_MODE); |
85 |
$this->checkIfUserIsAlwaysValid(); |
$this->isDatabaseError($resServExt); |
86 |
|
|
87 |
|
while ($tempServExt=&$resServExt->fetchRow(DB_FETCHMODE_ASSOC)) { |
88 |
|
$this->om_utilisateur['service'][]=$tempServExt; |
89 |
|
} |
90 |
|
// Si il y a un resultat c'est un utilisateur de service |
91 |
|
if(isset($this->om_utilisateur['service'])) { |
92 |
|
$this->user_is_service_ext=true; |
93 |
|
} else { |
94 |
|
$this->user_is_service_ext=false; |
95 |
} |
} |
96 |
} |
} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* getter user_is_service_ext |
101 |
|
*/ |
102 |
|
function isUserServiceExt() { |
103 |
// |
// |
104 |
if (!in_array($this->flag, $this->special_flags)) { |
if (is_null($this->user_is_service_ext)) { |
|
|
|
105 |
// |
// |
106 |
$this->getAllRights(); |
$this->getUserInfos(); |
107 |
|
} |
108 |
// |
// |
109 |
$this->getCollectivite(); |
return $this->user_is_service_ext; |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
|
* getter user_is_instr |
114 |
|
*/ |
115 |
|
function isUserInstructeur() { |
116 |
|
// |
117 |
|
if (is_null($this->user_is_instr)) { |
118 |
// |
// |
119 |
$this->isAuthorized(); |
$this->getUserInfos(); |
|
|
|
120 |
} |
} |
|
|
|
121 |
// |
// |
122 |
$this->setMoreParams(); |
return $this->user_is_instr; |
|
|
|
|
// Affichage HTML |
|
|
$this->display(); |
|
|
|
|
123 |
} |
} |
|
|
|
|
|
|
|
|
|
|
/** |
|
|
* Desctructeur de la classe, cette methode (appelee automatiquement) |
|
|
* permet d'afficher le footer de la page, le footer HTML, et de |
|
|
* deconnecter la base de donnees |
|
|
* |
|
|
* @return void |
|
|
*/ |
|
|
function __destruct() { |
|
124 |
|
|
125 |
// Footer |
// Ajout de variables de session contenant la division pour permettre une |
126 |
if (!defined('REST_REQUEST')) { |
// utilisation plus efficace dans les requetes |
127 |
$this->displayFooter(); |
function triggerAfterLogin($utilisateur = NULL) { |
128 |
|
// |
129 |
|
$sql = "SELECT instructeur.division, division.code |
130 |
|
FROM ".DB_PREFIXE."instructeur |
131 |
|
LEFT JOIN ".DB_PREFIXE."division |
132 |
|
ON instructeur.division = division.division |
133 |
|
WHERE instructeur.om_utilisateur='".$utilisateur["om_utilisateur"]."'"; |
134 |
|
$res = $this->db->query($sql); |
135 |
|
$this->addToLog("triggerAfterLogin(): db->query(\"".$sql."\");", VERBOSE_MODE); |
136 |
|
$this->isDatabaseError($res); |
137 |
|
$row = $res->fetchrow(DB_FETCHMODE_ASSOC); |
138 |
|
// |
139 |
|
if (isset($row["division"]) && $row["division"] != NULL) { |
140 |
|
$_SESSION["division"] = $row["division"]; |
141 |
|
$_SESSION["division_code"] = $row["code"]; |
142 |
|
} else { |
143 |
|
$_SESSION["division"] = "0"; |
144 |
|
$_SESSION["division_code"] = ""; |
145 |
} |
} |
146 |
|
|
147 |
// Deconnexion SGBD |
} |
|
$this->disconnectDatabase(); |
|
|
|
|
|
// Logger |
|
|
$this->addToLog("__destruct()", EXTRA_VERBOSE_MODE); |
|
|
|
|
|
// Logger |
|
|
$this->addToLog("__destruct() : ".$this->elapsedtime()." sec", VERBOSE_MODE); |
|
148 |
|
|
149 |
// Affichage des logs |
// Affichage des actions supplémentaires |
150 |
logger::instance()->displayLog(); |
function displayActionExtras() { |
151 |
|
// Affichage de la division si l'utilisateur en possède une |
152 |
// Footer HTML |
if ($_SESSION["division"] != 0) { |
153 |
if (!defined('REST_REQUEST')) { |
echo "\t\t\t<li class=\"action-division\">"; |
154 |
$this->displayHTMLFooter(); |
echo "(".$_SESSION['division_code'].")"; |
155 |
|
echo "</li>\n"; |
156 |
} |
} |
157 |
} |
} |
158 |
|
|
159 |
|
// }}} |
|
|
|
160 |
|
|
161 |
/** |
/** |
162 |
* Cette méthode permet de vérifier si l'utilisateur est autorisé ou non à |
* Cette méthode permet de vérifier si l'utilisateur est autorisé ou non à |
372 |
} |
} |
373 |
} |
} |
374 |
|
|
|
|
|
375 |
/** |
/** |
376 |
* |
* |
377 |
*/ |
*/ |
378 |
function addToLog($message, $type = DEBUG_MODE) { |
function isAccredited($obj = NULL, $operator = "AND") { |
379 |
// |
// Lorsque l'utilisateur a le login 'admin' alors il est authorisé à |
380 |
if (!defined('REST_REQUEST')) { |
// accéder à toutes les fonctions du logiciel |
381 |
logger::instance()->log("class ".get_class($this)." - ".$message, $type); |
// XXX à modifier pour changer ça vers un profil et non un login utilisateur |
382 |
|
if ($_SESSION["login"] == "admin") { |
383 |
|
return true; |
384 |
} |
} |
385 |
|
// Fonctionnement standard |
386 |
|
return parent::isAccredited($obj, $operator); |
387 |
} |
} |
388 |
|
|
389 |
/** |
/** |
390 |
* Envoie un mail avec piece jointe |
* Méthode de création de répertoire dans le dossier /trs correspondant |
391 |
* |
* à l'id du tableau de configuration de la connexion à la base de donnée |
392 |
* @param string $title Titre du mail |
* (dyn/database.inc.php) |
393 |
* @param string $message Corps du mail |
**/ |
394 |
* @param string $recipient Destinataire du mail |
function createFolder($folder) { |
395 |
* @param array $file Destinataire du mail |
if (!mkdir($this->getPathFolderTrs().$folder)) { |
396 |
* @access public |
// message |
397 |
* @return bool True si le mail est correctement envoye, false sinon. |
$message_class = "error"; |
398 |
|
$message = _("Echec lors de la creation du repertoires."); |
399 |
|
$this->addToMessage ($message_class, $message); |
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
|
/* |
404 |
|
* Methode permettant de lister tous les fichiers d'un dossier |
405 |
*/ |
*/ |
406 |
public function sendMail($title, $message, $recipient, $file = array()) { |
function getFolderFileList($id_folder) { |
407 |
|
|
408 |
@include_once "../php/phpmailer/class.phpmailer.php"; |
$file_list = array(); |
409 |
|
if(is_dir($this->getPathFolderTrs().$id_folder)) { |
410 |
|
if($dossier = opendir($this->getPathFolderTrs().$id_folder)) { |
411 |
|
|
412 |
|
while(false !== ($fichier = readdir($dossier))) { |
413 |
|
|
414 |
|
if($fichier != '.' && $fichier != '..' && !is_dir($fichier)) { |
415 |
|
$file_list[]=array( $fichier, |
416 |
|
filemtime($this->getPathFolderTrs().$id_folder."/".$fichier)); |
417 |
|
} // On ferme le if (qui permet de ne pas afficher index.php, etc.) |
418 |
|
|
419 |
|
} // On termine la boucle |
420 |
|
|
421 |
if (!class_exists("PHPMailer")) { |
closedir($dossier); |
422 |
$this->addToLog("sendMail(): !class_exists(\"PHPMailer\")", DEBUG_MODE); |
|
423 |
return false; |
} else { |
424 |
|
$this->displayMessage("error", _("Les documents du repertoire ne sont pas accessible.")); |
425 |
|
} |
426 |
|
} else { |
427 |
|
$this->displayMessage("error", _("Ce repertoire n'a pas de document.")); |
428 |
} |
} |
429 |
|
return $file_list; |
430 |
|
} |
431 |
|
|
432 |
// |
/* |
433 |
$this->setMailConfig(); |
* Store the data recived in the request into a file on the |
434 |
|
* local filesystem. |
435 |
|
* @todo This function will need to be changed for the save to |
436 |
|
* be on GED |
437 |
|
* @param mixed $data The data received with the request |
438 |
|
* @param string $id The consultation ID |
439 |
|
* @return string OK on success, KO otherwise |
440 |
|
*/ |
441 |
|
|
442 |
// |
function storeDecisionFile(&$fichier_base64, $basename, $dossier, $prefix = '') { |
443 |
if ($this->mail_config == false) { |
|
444 |
$this->addToLog("sendMail(): aucune configuration mail", DEBUG_MODE); |
if (empty($fichier_base64)) { |
445 |
|
$this -> addToMessage("error", _("Le fichier est vide")); |
446 |
return false; |
return false; |
447 |
} |
} |
448 |
|
$dir = $this->getPathFolderTrs().$dossier; |
449 |
|
|
450 |
// |
// if a file already exists by that name and it |
451 |
$mail = new PHPMailer(true); |
// is not a directory, back out |
452 |
|
/*if (file_exists($dir) AND is_dir($dir)) { |
453 |
// |
$this -> addToMessage("error", _("Le repertoire n'existe pas, le fichier ne peut pas etre enregistre.")); |
454 |
$mail->IsSMTP(); |
return false; |
455 |
|
}*/ |
456 |
$mail->SMTPAuth = true; // enable SMTP authentication |
// if a dirextory by that name exists, make sure it does |
457 |
$mail->SMTPSecure = "tls"; |
// not already contain an avis de consultation, MAYBE WE DON'T NEED THIS |
458 |
$mail->Username = $this->mail_config["mail_username"]; |
if (file_exists($dir) AND is_dir($dir)) { |
459 |
$mail->Password = $this->mail_config["mail_pass"]; |
$dir_contents = trim(shell_exec('ls '.$dir)); |
460 |
if ($this->mail_config["mail_username"] == '') { |
if (strpos($dir_contents, ' ') != false) { |
461 |
$mail->SMTPAuth = false; |
$dir_contents = explode(' ', $dir_contents); |
462 |
|
} else { |
463 |
|
$dir_contents = array($dir_contents); |
464 |
|
} |
465 |
|
foreach ($dir_contents as $basefname) { // very useful for consultation |
466 |
|
if (strpos($basefname, $prefix)!==false) { |
467 |
|
return _("Un retour d'avis existe deja."); |
468 |
|
|
469 |
|
} |
470 |
|
} |
471 |
} else { |
} else { |
472 |
$mail->SMTPAuth = true; |
$this->createFolder($dossier); |
473 |
} |
} |
|
$mail->Port = $this->mail_config["mail_port"]; |
|
|
$mail->Host = $this->mail_config["mail_host"]; |
|
|
$mail->AddReplyTo($this->mail_config["mail_from"], $this->mail_config["mail_from_name"]); |
|
|
$mail->From = $this->mail_config["mail_from"]; |
|
|
$mail->FromName = $this->mail_config["mail_from_name"]; |
|
|
foreach (explode(",",$recipient) as $adresse) { |
|
|
if (!$this->checkValidEmailAddress($adresse)) { |
|
|
$this->addToLog("sendMail(): courriel incorrect ".$adresse, DEBUG_MODE); |
|
|
return false; |
|
|
} else |
|
|
$mail->AddAddress(trim($adresse)); |
|
|
} |
|
|
$mail->IsHTML(true); |
|
474 |
|
|
475 |
// Corps du message |
// store the file contents into the file named: |
476 |
$mail_body ="<html>"; |
// consultation_<ID>_<file_name_received> |
477 |
$mail_body .= "<head><title>".$title."</title></head>"; |
$file_len = strlen($fichier_base64); |
478 |
$mail_body .= "<body>".$message."</body>"; |
|
479 |
$mail_body .= "</html>"; |
$filename = $dir."/".$prefix.$basename; |
480 |
|
|
481 |
|
$file = fopen($filename, 'w'); |
482 |
|
if (!$file) { |
483 |
|
return _("Echec a la creation du fichier."); |
484 |
|
} |
485 |
|
// check that the number of bytes written is equal to the length |
486 |
|
// of the data received |
487 |
|
$num_written = fwrite($file, $fichier_base64, $file_len); |
488 |
|
|
489 |
$mail->Subject = $title; |
if (!$num_written) { |
490 |
$mail->MsgHTML($mail_body); |
// remove the file |
491 |
foreach($file as $oneFile) { |
// the return value from shell can't be used for checking since |
492 |
|
// one can not know if the NULL returned is because there was no |
493 |
if($oneFile['stream']){ |
// output or because there was an error |
494 |
$mail->AddStringAttachment($oneFile['content'], $oneFile['title'], $oneFile['encoding'] = 'base64',$oneFile['type'] = 'application/octet-stream'); |
$ret = shell_exec("rm -f $filename 2>&1"); |
495 |
} else{ |
//if ($ret == NULL) { // an error occured while deleting the file |
496 |
$mail->AddAttachment($oneFile['url']); |
//} |
497 |
} |
return _("La sauvegarde du fichier a echoue"); |
|
} |
|
|
// Envoie de l'email |
|
|
if ($mail->Send()) { |
|
|
return true; |
|
|
} else { |
|
|
$this->addToLog("sendMail(): ".$mail->ErrorInfo, DEBUG_MODE); |
|
|
return false; |
|
498 |
} |
} |
499 |
|
fclose($file); |
500 |
|
return true; |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
|
* |
505 |
|
*/ |
506 |
|
function notExistsError ($explanation = NULL) { |
507 |
|
// message |
508 |
|
$message_class = "error"; |
509 |
|
$message = _("Cette page n'existe pas."); |
510 |
|
$this->addToMessage ($message_class, $message); |
511 |
|
// |
512 |
|
$this->setFlag(NULL); |
513 |
|
$this->display(); |
514 |
|
|
515 |
|
// |
516 |
|
die(); |
517 |
} |
} |
518 |
|
|
519 |
} |
} |