Subversion Repositories Applications.annuaire

Compare Revisions

No changes between revisions

Ignore whitespace Rev 588 → Rev 589

/branches/v1.5-belier/jrest/bibliotheque/Log.php
New file
0,0 → 1,195
<?php
//declare(encoding='UTF-8');
/**
* Classe permettant de logger des messages dans les fichier situés dans le dossier de log
*
* PHP Version 5
*
* @category PHP
* @package Framework
* @author aurelien <aurelien@tela-botanica.org>
* @copyright 2009 Tela-Botanica
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
* @version SVN: <svn_id>
* @link /doc/framework/
*/
 
class Log {
 
/**
* Tableau associatif stockant les descripteurs de fichiers
*/
private static $fichiersLog = array();
 
/**
* Chemin de base du dossier log de l'application
*/
private static $cheminLogs = '';
 
/**
* Booleen indiquant si l'on peut correctement écrire dans les fichiers de logs
*/
private static $droitLogger = true;
 
/**
* Zone horaire (pour éviter des avertissements dans les dates)
*/
private static $timeZone = '';
 
/**
* Taille maximum d'un fichier de log avant que celui ne soit archivé (en octets)
*/
private static $tailleMax = 10000;
 
/**
* séparateur de chemin
*/
private static $sd = DIRECTORY_SEPARATOR;
 
/**
* Extension des fichiers de log
*/
private static $ext = '.log';
 
/**
* La classe registre se contient elle-même, (pour le pattern singleton)
*/
private static $log;
 
/**
* Constructeur par défaut, privé, car on accède à la classe par le getInstance
*/
private function __construct() {
 
self::$sd = $sd;
// gestion de la timezone pour éviter des erreurs
if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get")) {
date_default_timezone_set(self::$timeZone);
}
 
if(!is_dir(self::$cheminLogs) || !is_writable(self::$cheminLogs)) {
self::desactiverEcriture();
}
}
 
public static function setCheminLog($nouveauCheminLogs) {
self::$cheminLogs = $nouveauCheminLogs;
}
 
public static function getCheminLog() {
return self::$cheminLogs;
}
 
public static function setTimeZone($NouvelleTimeZone) {
self::$timeZone = $NouvelleTimeZone;
}
 
public static function setTailleMax($nouvelleTailleMax) {
self::$tailleMax = $nouvelleTailleMax;
}
 
/**
* Fonction qui renvoie l'instance de classe en assurant son unicité, c'est l'unique méthode qui doit être
* utilisée pour récupérer l'objet Registre
* @return Log le gestionnaire de log en cours
*/
public static function getInstance() {
if (self::$log instanceof Log) {
return self::$log;
}
self::$log = new Log();
return self::$log;
}
 
/**
* Ajoute une entrée au log spécifié par le paramètre $nomFichier
* @param string $nomFichier le nom du fichier dans lequel écrire
*/
public static function ajouterEntree($nomFichier,$entree,$mode='a+') {
if(self::$droitLogger) {
$date = "\n"."\n".date('d m Y H:i')."\n" ;
 
// si le fichier est déjà dans le tableau et qu'on peut y écrire
if(self::verifierOuvrirFichier($nomFichier,$mode)) {
// on y écrit le message de log
fwrite(self::$fichiersLog[$nomFichier],$date.$entree);
// on vérifie si le fichier ne dépasse pas la taille maximale
self::verifierTailleFichierOuArchiver($nomFichier);
} else {
// sinon on interdit l'écriture
self::desactiverEcriture($nomFichier);
}
}
}
 
/**
* Vide un fichier log indiqué
* @param string $nomFichier le nom du fichier à vider
*/
public static function viderLog($nomFichier) {
ajouterEntree($nomFichier,'','w');
}
 
/**
* Vérifie la présence d'un fichier dans le tableau, ses droits d'écriture,
* l'ouvre si nécessaire
* @param string $nomFichier le nom du fichier dont on doit vérifier la présence
* @return boolean true si le fichier est ouvert ou maintenant accessible, false sinon
*/
public static function verifierOuvrirFichier($nomFichier,$mode) {
// le fichier est il déjà ouvert ?
if(in_array($nomFichier,self::$fichiersLog)) {
// si oui peut on y écrire ?
if(is_writable(self::$cheminLogs.$nomFichier.self::$ext)) {
// si oui on renvoie le descripteur
return true;
}
return false;
} else {
// sinon on l'ouvre
$fp = @fopen(self::$cheminLogs.$nomFichier.self::$ext,$mode);
// si l'ouverture a réussi et si le fichier a les droits d'écriture
if($fp && is_writable(self::$cheminLogs.$nomFichier.self::$ext)) {
// si oui on renvoie le descripteur qu'on ajoute au tableau
self::$fichiersLog[$nomFichier] = $fp;
return true;
}
return false;
}
}
 
/**
* Vérifie la taille d'un fichier donné et si celle ci est trop importante
* archive le fichier de log
* @param string $nomFichier nom du fichier à vérifier
*/
private static function verifierTailleFichierOuArchiver($nomFichier) {
if(filesize(self::$cheminLogs.$nomFichier.self::$ext) > self::$tailleMax) {
rename(self::$cheminLogs.$nomFichier.self::$ext,self::$cheminLogs.$nomFichier.date('d_m_Y_H:i').self::$ext);
self::ajouterEntree($nomFichier,'');
}
}
 
/**
* Désactive l'écriture du log et envoie un message au gestionnaire d'erreurs
* @param string $nomFichier le nom du fichier qui a causé l'erreur
*/
private static function desactiverEcriture($nomFichier = '') {
self::$droitLogger = false;
if($nomFichier != '') {
$fichierDossier = 'fichier '.$nomFichier ;
} else {
$fichierDossier = 'dossier des logs';
}
}
 
/**
* destructeur de classe, ferme les descripteurs ouverts
*/
public function __destruct() {
foreach(self::$fichiersLog as $nomFichier => $fp) {
fclose($fp);
}
}
}
?>
/branches/v1.5-belier/jrest/bibliotheque/JRestService.php
New file
0,0 → 1,566
<?php
/**
* Classe mère abstraite contenant les méthodes génériques des services.
* Encodage en entrée : utf8
* Encodage en sortie : utf8
*
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version $Id$
* @copyright 2009
*/
abstract class JRestService {
 
public $config;
protected $bdd;
protected $ressources;
protected $log = array();
protected $messages = array();
protected $debug = array();
protected $distinct = false;
protected $orderby = null;
protected $formatRetour = 'objet';
protected $start = 0;
protected $limit = 150;
 
/** pour l'envoi de XML : éventuelle balise dans laquelle placer tout le contenu */
protected $baliseMaitresse;
 
public function __construct($config, $demarrer_session = true) {
// Tableau contenant la config de Jrest
$this->config = $config;
 
// Connection à la base de données
$this->bdd = $this->connecterPDO($this->config, 'appli');
 
// Nettoyage du $_GET (sécurité)
if (isset($_GET)) {
$get_params = array('orderby', 'distinct', 'start', 'limit', 'formatRetour');
foreach ($get_params as $get) {
$verifier = array('NULL', "\n", "\r", "\\", "'", '"', "\x00", "\x1a", ';');
if (isset($_GET[$get])) {
$_GET[$get] = str_replace($verifier, '', $_GET[$get]);
if ($_GET[$get] != '') {
$this->$get = $_GET[$get];
}
} else {
$_GET[$get] = null;
}
}
}
}
 
/**
* Méthode appelée quand aucun paramètre n'est passé dans l'url et avec une requête de type GET.
*/
public function getRessource() {
$this->getElement(array());
}
 
//+----------------------------------------------------------------------------------------------------------------+
// GESTION de l'ENVOI au NAVIGATEUR pas la PEINE de CRIER
 
protected function envoyerJson($donnees, $encodage = 'utf-8') {
$contenu = json_encode($donnees);
$this->envoyer($contenu, 'application/json', $encodage, false);
}
 
/** à l'arrache pour rétrocompatibilité avec le service "annuaire_tela" de eFlore_chatin */
protected function envoyerXml($donnees, $encodage = 'utf-8') {
$xml = '<?xml version="1.0" encoding="' . strtoupper($encodage) . '"?>';
if ($this->baliseMaitresse) {
$xml .= '<' . $this->baliseMaitresse . '>';
}
$xml .= $this->genererXmlAPartirDeTableau($donnees);
if ($this->baliseMaitresse) {
$xml .= '</' . $this->baliseMaitresse . '>';
}
$this->envoyer($xml, 'application/xml', $encodage, false);
}
 
/**
* Génère un XML minimaliste à partir d'un tableau associatif
* Note : gère mal les indices numériques
* @TODO utiliser une vraie lib
*/
protected function genererXmlAPartirDeTableau($tableau) {
$xml = '';
foreach ($tableau as $balise => $donnee) {
$xml .= '<' . $balise . '>';
if (is_array($donnee)) {
// récurer, balayer, que ce soit toujours pimpant
$xml .= $this->genererXmlAPartirDeTableau($donnee);
} else {
$xml .= $donnee;
}
$xml .= '</' . $balise . '>';
}
return $xml;
}
 
protected function envoyerJsonVar($variable, $donnees = null, $encodage = 'utf-8') {
$contenu = "var $variable = ".json_encode($donnees);
$this->envoyer($contenu, 'text/html', $encodage, false);
}
 
protected function envoyerJsonp($donnees = null, $encodage = 'utf-8') {
$contenu = $_GET['callback'].'('.json_encode($donnees).');';
$this->envoyer($contenu, 'text/html', $encodage, false);
}
 
protected function envoyerTxt($donnees, $encodage = 'utf-8') {
$this->envoyer($contenu, 'text/html', $encodage, false);
}
 
protected function envoyer($donnees = null, $mime = 'text/html', $encodage = 'utf-8', $json = true) {
// Traitements des messages d'erreurs et données
if (count($this->messages) != 0) {
header('HTTP/1.1 500 Internal Server Error');
$mime = 'text/html';
$encodage = 'utf-8';
$json = true;
$sortie = $this->messages;
} else {
$sortie = $donnees;
if (is_null($donnees)) {
$sortie = 'OK';
}
}
 
// Gestion de l'envoie du déboguage
$this->envoyerDebogage();
 
// Encodage au format et JSON et envoie sur la sortie standard
$contenu = $json ? json_encode($sortie) : $sortie;
$this->envoyerContenu($encodage, $mime, $contenu);
}
 
private function envoyerDebogage() {
if (!is_array($this->debug)) {
$this->debug[] = $this->debug;
}
if (count($this->debug) != 0) {
foreach ($this->debug as $cle => $val) {
if (is_array($val)) {
$this->debug[$cle] = print_r($val, true);
}
}
header('X-DebugJrest-Data:'.json_encode($this->debug));
}
}
 
private function envoyerContenu($encodage, $mime, $contenu) {
if (!is_null($mime) && !is_null($encodage)) {
header("Content-Type: $mime; charset=$encodage");
} else if (!is_null($mime) && is_null($encodage)) {
header("Content-Type: $mime");
}
print $contenu;
}
 
private function envoyerAuth($message_accueil, $message_echec) {
header('HTTP/1.0 401 Unauthorized');
header('WWW-Authenticate: Basic realm="'.mb_convert_encoding($message_accueil, 'ISO-8859-1', 'UTF-8').'"');
header('Content-type: text/plain; charset=UTF-8');
print $message_echec;
exit(0);
}
 
protected function envoyerMessageErreur($msg, $code) {
$textHttp = $this->getCodeHttpText($code);
header("HTTP/1.0 $code $textHttp");
header("Content-Type: text/plain; charset=utf-8");
die($msg);
}
 
private function getCodeHttpText($code) {
$text = '';
switch ($code) {
case 100: $text = 'Continue'; break;
case 101: $text = 'Switching Protocols'; break;
case 200: $text = 'OK'; break;
case 201: $text = 'Created'; break;
case 202: $text = 'Accepted'; break;
case 203: $text = 'Non-Authoritative Information'; break;
case 204: $text = 'No Content'; break;
case 205: $text = 'Reset Content'; break;
case 206: $text = 'Partial Content'; break;
case 300: $text = 'Multiple Choices'; break;
case 301: $text = 'Moved Permanently'; break;
case 302: $text = 'Moved Temporarily'; break;
case 303: $text = 'See Other'; break;
case 304: $text = 'Not Modified'; break;
case 305: $text = 'Use Proxy'; break;
case 400: $text = 'Bad Request'; break;
case 401: $text = 'Unauthorized'; break;
case 402: $text = 'Payment Required'; break;
case 403: $text = 'Forbidden'; break;
case 404: $text = 'Not Found'; break;
case 405: $text = 'Method Not Allowed'; break;
case 406: $text = 'Not Acceptable'; break;
case 407: $text = 'Proxy Authentication Required'; break;
case 408: $text = 'Request Time-out'; break;
case 409: $text = 'Conflict'; break;
case 410: $text = 'Gone'; break;
case 411: $text = 'Length Required'; break;
case 412: $text = 'Precondition Failed'; break;
case 413: $text = 'Request Entity Too Large'; break;
case 414: $text = 'Request-URI Too Large'; break;
case 415: $text = 'Unsupported Media Type'; break;
case 500: $text = 'Internal Server Error'; break;
case 501: $text = 'Not Implemented'; break;
case 502: $text = 'Bad Gateway'; break;
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
exit('Unknown http status code "' . htmlentities($code) . '"');
break;
}
return $text;
}
 
//+----------------------------------------------------------------------------------------------------------------+
// GESTION de la BASE de DONNÉES
 
private function connecterPDO($config, $base = 'database') {
$cfg = $config[$base];
// ATTENTION : la connexin à la bdd peut échouer si l'host vaut localhost. Utiliser 127.0.0.1 à la place.
$dsn = $cfg['phptype'].':dbname='.$cfg['database'].';host='.$cfg['hostspec'];
try {
// Création de la connexion en UTF-8 à la BDD
$PDO = new PDO($dsn, $cfg['username'], $cfg['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
} catch (PDOException $e) {
echo 'La connexion à la base de donnée via PDO a échouée : ' .$dsn. $e->getMessage();
}
// Affiche les erreurs détectées par PDO (sinon mode silencieux => aucune erreur affiché)
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $PDO;
}
 
protected function getTxt($id) {
$sortie = '';
switch ($id) {
case 'sql_erreur' : $sortie = 'Requête echec. Fichier : "%s". Ligne : "%s". Message : %s'; break;
default : $sortie = $id;
}
return $sortie;
}
 
//+----------------------------------------------------------------------------------------------------------------+
// TRAITEMENT des URLs et des PARAMÊTRES
 
protected function traiterNomMethodeGet($nom) {
$methode = 'get';
$methode .= str_replace(' ', '', ucwords(str_replace('-', ' ', strtolower($nom))));
return $methode;
}
 
protected function traiterNomMethodePost($nom) {
$methode = 'update';
$methode .= str_replace(' ', '', ucwords(str_replace('-', ' ', strtolower($nom))));
return $methode;
}
 
protected function traiterNomMethodePut($nom) {
$methode = 'create';
$methode .= str_replace(' ', '', ucwords(str_replace('-', ' ', strtolower($nom))));
return $methode;
}
 
protected function traiterParametresUrl($params_attendu, $params, $pourBDD = true) {
$sortie = array();
foreach ($params_attendu as $num => $nom) {
if (isset($params[$num]) && $params[$num] != '*') {
if ($pourBDD) {
$params[$num] = $this->bdd->quote($params[$num]);
}
$sortie[$nom] = $params[$num];
}
}
return $sortie;
}
 
protected function traiterParametresPost($params) {
$sortie = array();
foreach ($params as $cle => $valeur) {
$sortie[$cle] = $this->bdd->quote($valeur);
}
return $sortie;
}
 
//+----------------------------------------------------------------------------------------------------------------+
// GESTION DE L'IDENTIFICATION
 
public function controlerIpAutorisees() {
$ipsAutorisees = $this->config['jrest_admin']['ip_autorisees'];
 
$remoteIp = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
$serverIp = filter_input(INPUT_SERVER, 'SERVER_ADDR', FILTER_VALIDATE_IP);
if (in_array($remoteIp, $ipsAutorisees) == false) {
if ($remoteIp != $serverIp) {// ATTENTION : maintenir ce test à l'intérieur du précédent
$message = "Accès interdit. \n".
"Vous n'êtes pas autorisé à accéder à ce service depuis '$remoteIp' !\n";
$this->envoyerMessageErreur($message, 401);
}
}
return true;
}
 
protected function getIdentification(&$params) {
// Initialisation des variables
$utilisateur = array(0, session_id());
 
// L'id utilisateur est soit passé par le POST soit dans l'url
if (is_array($params) && isset($params['cmhl_ce_modifier_par'])) {
$utilisateur[0] = $params['cmhl_ce_modifier_par'];
unset($params['cmhl_ce_modifier_par']);
} else if (is_string($params)) {
$utilisateur[0] = $params;
}
 
return $utilisateur;
}
 
protected function etreAutorise($id_utilisateur) {
$autorisation = false;
if (($_SESSION['coel_utilisateur'] != '') && $_SESSION['coel_utilisateur']['id'] != $id_utilisateur) {
$this->messages[] = 'Accès interdit.';
} else if ($_SESSION['coel_utilisateur'] == '') {
$this->messages[] = 'Veuillez vous identifiez pour accéder à cette fonction.';
} else {
$autorisation = true;
}
return $autorisation;
}
 
// WTF coel en dur ??
private function gererIdentificationPermanente() {
// Pour maintenir l'utilisateur tjrs réellement identifié nous sommes obligé de recréer une SESSION et de le recharger depuis la bdd
if ($this->getUtilisateur() == ''
&& isset($_COOKIE['coel_login'])
&& ($utilisateur = $this->chargerUtilisateur($_COOKIE['coel_login'], $_COOKIE['coel_mot_de_passe']))) {
$this->setUtilisateur($utilisateur, $_COOKIE['coel_permanence']);
}
}
 
// WTF coel en dur ??
protected function getUtilisateur() {
return (isset($_SESSION['coel_utilisateur']) ? $_SESSION['coel_utilisateur'] : '');
}
 
protected function authentifier() {
// @TODO @WARNING @ACHTUNG @ ALARM enlever le patch CGI quand on aura mis à jour Apache/PHP !!
if (JRest::$cgi === false) { // si on est en CGI, accès libre pour tous (pas trouvé mieux)
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
header('HTTP/1.0 401 Unauthorized');
header('Content-type: text/html; charset=UTF-8');
echo 'Accès interdit';
exit;
} else {
if ($this->verifierAcces()) {
return ;
} else {
header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
header('HTTP/1.0 401 Unauthorized');
header('Content-type: text/html; charset=UTF-8');
echo 'Accès interdit';
exit ;
}
}
}
}
 
/**
* Vérifie l'accès en se basant sur $id et $mdp si ceux-ci sont fournis; sinon,
* lit les valeurs transmises par l'authentification HTTP BASIC AUTH
*/
protected function verifierAcces($id = null, $mdp = null) {
$basicAuth = false;
if ($id == null && $mdp == null) {
$id = is_null($id) ? $_SERVER['PHP_AUTH_USER'] : $id;
$mdp = is_null($mdp) ? $_SERVER['PHP_AUTH_PW'] : $mdp;
$basicAuth = true;
}
// mode super admin debug super sioux - attention aux fuites de mdp !
$mdpMagiqueHache = $this->config['jrest_admin']['mdp_magique_hache'];
if ($mdpMagiqueHache != '') {
if (md5($mdp) === $mdpMagiqueHache) {
return true;
}
}
// mode pour les gens normaux qu'ont pas de passe-droits
if ($basicAuth === false || JRest::$cgi === false) { // en mode non-CGI ou pour une identification $id / $mdp
 
$requete = 'SELECT '.$this->config['database_ident']['ann_id'].' AS courriel '.
'FROM '.$this->config['database_ident']['database'].'.'.$this->config['database_ident']['annuaire'].' '.
'WHERE '.$this->config['database_ident']['ann_id'].' = '.$this->bdd->quote($id).' '.
' AND '.$this->config['database_ident']['ann_pwd'].' = '.$this->config['database_ident']['pass_crypt_funct'].'('.$this->bdd->quote($mdp).')' ;
 
$resultat = $this->bdd->query($requete)->fetch();
$identifie = false;
if (isset($resultat['courriel'])) {
$identifie = true;
}
return $identifie;
} else { // si on est en CGI, accès libre pour tous (pas trouvé mieux)
return true; // ça fait un peu mal...
}
}
 
/**
* Envoie une demande d'authentification HTTP puis compare le couple
* login / mot de passe envoyé par l'utilisateur, à ceux définis dans
* la config (section database_ident).
* En cas d'erreur, sort du programme avec un entête HTTP 401
* @TODO redondant avec les trucs du dessus :'(
*/
protected function authentificationHttpSimple() {
$autorise = true;
// contrôle d'accès @TODO @WARNING @ACHTUNG @ ALARM enlever le patch CGI quand on aura mis à jour Apache/PHP !!
if (JRest::$cgi === false) { // si on est en CGI, accès libre pour tous (pas trouvé mieux)
$nomUtil = $_SERVER['PHP_AUTH_USER'];
$mdp = $_SERVER['PHP_AUTH_PW'];
$autorise = (($nomUtil == $this->config['database_ident']['username']) && ($mdp == $this->config['database_ident']['password']));
}
// entêtes HTTP
if (! $autorise) {
header('WWW-Authenticate: Basic realm="Annuaire de Tela Botanica"');
header('HTTP/1.0 401 Unauthorized');
echo 'Veuillez vous authentifier pour utiliser ce service';
exit;
}
}
 
protected function creerCookiePersistant($duree = null, $id = null, $mdp = null) {
$id = is_null($id) ? $_SERVER['PHP_AUTH_USER'] : $id;
$mdp = is_null($mdp) ? $_SERVER['PHP_AUTH_PW'] : $mdp;
$duree = (int) is_null($duree) ? time()+3600*24*30 : $duree;
 
$nomCookie = $this->config['database_ident']['nom_cookie_persistant'];
$valeurCookie = md5($mdp).$id;
 
setcookie($nomCookie, $valeurCookie, $duree, '/');
}
 
protected function creerCookieUtilisateur($duree = null, $id = null, $mdp = null) {
$id = is_null($id) ? $_SERVER['PHP_AUTH_USER'] : $id;
$mdp = is_null($mdp) ? $_SERVER['PHP_AUTH_PW'] : $mdp;
$duree = (int) is_null($duree) ? 0 : $duree;
 
$nomCookie = $this->config['database_ident']['nom_cookie_utilisateur'];
$valeurCookie = md5($mdp).$id;
 
setcookie($nomCookie, $valeurCookie, $duree, '/');
}
protected function supprimerCookieUtilisateur() {
session_destroy();
setcookie($this->config['database_ident']['nom_cookie_utilisateur'], "", time()-7200, "/");
setcookie($this->config['database_ident']['nom_cookie_persistant'], "", time()-7200, "/");
}
 
protected function estAutoriseMessagerie($adresse) {
$utilisateurs_messagerie = explode(',', $this->config['messagerie']['utilisateurs_autorises']);
return in_array($adresse, $utilisateurs_messagerie);
}
 
//+----------------------------------------------------------------------------------------------------------------+
// GESTION DES SQUELETTES PHP
 
/**
* Méthode prenant en paramètre un chemin de fichier squelette et un tableau associatif de données,
* en extrait les variables, charge le squelette et retourne le résultat des deux combinés.
*
* @param String $fichier le chemin du fichier du squelette
* @param Array $donnees un tableau associatif contenant les variables a injecter dans le squelette.
*
* @return boolean false si le squelette n'existe pas, sinon la chaine résultat.
*/
public static function traiterSquelettePhp($fichier, Array $donnees = array()) {
$sortie = false;
if (file_exists($fichier)) {
// Extraction des variables du tableau de données
extract($donnees);
// Démarage de la bufferisation de sortie
ob_start();
// Si les tags courts sont activés
if ((bool) @ini_get('short_open_tag') === true) {
// Simple inclusion du squelette
include $fichier;
} else {
// Sinon, remplacement des tags courts par la syntaxe classique avec echo
$html_et_code_php = self::traiterTagsCourts($fichier);
// Pour évaluer du php mélangé dans du html il est nécessaire de fermer la balise php ouverte par eval
$html_et_code_php = '?>'.$html_et_code_php;
// Interprétation du html et du php dans le buffer
echo eval($html_et_code_php);
}
// Récupèration du contenu du buffer
$sortie = ob_get_contents();
// Suppression du buffer
@ob_end_clean();
} else {
$msg = "Le fichier du squelette '$fichier' n'existe pas.";
trigger_error($msg, E_USER_WARNING);
}
// Retourne le contenu
return $sortie;
}
 
/**
* Fonction chargeant le contenu du squelette et remplaçant les tags court php (<?= ...) par un tag long avec echo.
*
* @param String $chemin_squelette le chemin du fichier du squelette
*
* @return string le contenu du fichier du squelette php avec les tags courts remplacés.
*/
private static function traiterTagsCourts($chemin_squelette) {
$contenu = file_get_contents($chemin_squelette);
// Remplacement de tags courts par un tag long avec echo
$contenu = str_replace('<?=', '<?php echo ', $contenu);
// Ajout systématique d'un point virgule avant la fermeture php
$contenu = preg_replace("/;*\s*\?>/", "; ?>", $contenu);
return $contenu;
}
 
/**
* Crée un nom Wiki (de la forme "JeanTalus") à partir des données de l'utilisateur;
* gère l'utilisation du pseudo mais pas la collision de noms Wiki @TODO s'en occuper
*
* @param array $infos des infos de profil utilisateur - on admet qu'elles contiennent "intitule"
* @return string un nom wiki correspondant à l' "intitulé" de l'utilisateur (prénom-nom ou pseudo)
* ou la valeur par défaut de $defaut si celui-ci est fourni et si le nom Wiki n'a pu être construit
*/
public function formaterNomWiki($intitule, $defaut="ProblemeNomWiki") {
$nw = $this->convertirEnCamelCase($intitule);
// on sait jamais
if ($nw == "") {
$nw = $defaut;
}
 
return $nw;
}
protected function convertirEnCamelCase($str) {
// Suppression des accents
$str = $this->supprimerAccents($str);
// Suppression des caractères non alphanumériques
$str = preg_replace('/[^\da-z]/i', '', ucwords(strtolower($str)));
return $str;
}
 
protected function supprimerAccents($str, $charset='utf-8') {
$str = htmlentities($str, ENT_NOQUOTES, $charset);
 
$str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;'
$str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
 
return $str;
}
}
?>
/branches/v1.5-belier/jrest/bibliotheque/GoogleAnalyticsAPI.php
New file
0,0 → 1,294
<?php
 
if(isset($_GET['source'])) {
highlight_file(__FILE__);
die;
}
 
/*
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
*
* @author CERDAN Yohann <cerdanyohann@yahoo.fr>
* @copyright (c) 2009 CERDAN Yohann, All rights reserved
* @ version 12:38 04/08/2009
*/
 
class GoogleAnalyticsAPI
{
/** Google account login (email) **/
private $login;
/** Google account password **/
private $password;
/** Google analytics website ID (avalaible on your google analytics account url) **/
private $ids;
/** The login token to the google analytics service **/
private $loginToken;
/** The XML response send by the service **/
private $response;
/** Begin date of the displaying datas **/
private $date_begin;
/** End date of the displaying datas **/
private $date_end;
/** Sort the results **/
private $sort;
/** The param to sort (metrics or dimensions) **/
private $sort_param;
/**
* Class constructor
*
* @param string $login the login (email)
* @param string $password the password
* @param string $ids the IDs of the website (find it in the google analytics gui)
* @param string $date_begin the begin date
* @param string $date_end the end date
*
* @return void
*/
public function __construct($login,$password,$ids,$date_begin,$date_end = null)
{
$this->login = $login;
$this->password = $password;
$this->ids = $ids;
$this->date_begin = $date_begin;
if (!$date_end) {
$this->date_end = $date_begin;
} else {
$this->date_end = $date_end;
}
$this->sort = "-";
$this->sort_param = "metrics";
// Authentication
$this->login();
}
/**
* Set the result's sort by metrics
*
* @param boolean $sort asc or desc sort
*
* @return void
*/
public function setSortByMetrics ($sort)
{
if ($sort==true) {
$this->sort = "";
} else {
$this->sort = "-";
}
$this->sort_param = 'metrics';
}
/**
* Set the result's sort by dimensions
*
* @param boolean $sort asc or desc sort
*
* @return void
*/
public function setSortByDimensions ($sort)
{
if ($sort==true) {
$this->sort = "";
} else {
$this->sort = "-";
}
$this->sort_param = 'dimensions';
}
/**
* Set the IDs of the website
*
* @param string $ids the IDs of the website (find it in the google analytics gui)
*
* @return void
*/
public function setIds($ids)
{
$this->ids = $ids;
}
/**
* Set the date of the export
*
* @param string $date_begin the begin date
* @param string $date_end the end date
*
* @return void
*/
public function setDate ($date_begin,$date_end = null)
{
$this->date_begin = $date_begin;
if (!$date_end) {
$this->date_end = $date_begin;
} else {
$this->date_end = $date_end;
}
}
/**
* Login to the google server
* See : http://google-data-api.blogspot.com/2008/05/clientlogin-with-php-curl.html
*
* @return void
*/
private function login()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array('accountType' => 'GOOGLE',
'Email' => $this->login,
'Passwd' => $this->password,
'source'=>'php_curl_analytics',
'service'=>'analytics');
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
$hasil = curl_exec($ch);
curl_close($ch);
// Get the login token
// SID=DQA...oUE
// LSID=DQA...bbo
// Auth=DQA...Sxq
if (preg_match('/Auth=(.*)$/',$hasil,$matches)>0) {
$this->loginToken = $matches[1];
} else {
trigger_error('Authentication problem',E_USER_WARNING);
return null;
}
}
/**
* Get URL content using cURL.
*
* @param string $url the url
*
* @return string the html code
*/
function getContent ($url)
{
if (!extension_loaded('curl')) {
throw new Exception('curl extension is not available');
}
$ch = curl_init($url);
$header[] = 'Authorization: GoogleLogin auth=' . $this->loginToken;
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$this->response = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
return $infos['http_code'];
}
/**
* Get the google analytics datas by dimensions and metrics
* See : http://code.google.com/intl/fr/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
*
* @param string $metrics the metrics
* @param string $dimensions the dimensions
*
* @return array
*/
public function getDimensionByMetric($metrics, $dimensions)
{
$url = "https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metrics . "&dimensions=ga:" . $dimensions . "&start-date=" . $this->date_begin . "&end-date=" . $this->date_end ."&sort=" . $this->sort . "ga:";
if ($this->sort_param=='metrics') { // sort by metrics
$url .= $metrics;
}
if ($this->sort_param=='dimensions') { // sort by dimensions
$url .= $dimensions;
}
 
if($this->getContent($url) == 200) {
$XML_object = simplexml_load_string($this->response);
$labels_array = array();
$datas_array = array();
foreach($XML_object->entry as $m) {
$dxp = $m->children('http://schemas.google.com/analytics/2009');
$metric_att = $dxp->metric->attributes();
$dimension_att = $dxp->dimension->attributes();
$labels_array []= $dimension_att['value'] . ' (' . $metric_att['value'] . ')';
$datas_array []= (string)$metric_att['value'];
}
return array('labels' => $labels_array, 'datas' => $datas_array);
} else {
return null;
}
}
/**
* Get the google analytics datas by metrics
* See : http://code.google.com/intl/fr/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
*
* @param string $metrics the metrics
* @param string $uri the url of the website page (ex : /myurl/)
*
* @return array
*/
public function getMetric($metric,$uri=null)
{
$url = "https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metric . "&start-date=" . $this->date_begin . "&end-date=" . $this->date_end;
 
if ($uri) {
$url .= "&dimensions=ga:pagePath&filters=ga:pagePath%3D%3D" . $uri;
}
if($this->getContent($url) == 200) {
$XML_object = simplexml_load_string($this->response);
$dxp = $XML_object->entry->children('http://schemas.google.com/analytics/2009');
if (@count($dxp)>0) {
$metric_att = $dxp->metric->attributes();
}
return $metric_att['value'] ? (string)$metric_att['value'] : 0;
} else {
return null;
}
}
}
 
?>
/branches/v1.5-belier/jrest/bibliotheque/.
New file
Property changes:
Added: svn:ignore
+MiniRest.php