/trunk/jrest/bibliotheque/JrestService.php |
---|
New file |
0,0 → 1,72 |
<?php |
/** |
* PHP Version 5 |
* |
* @category PHP |
* @package jrest |
* @author aurelien <aurelien@tela-botanica.org> |
* @copyright 2009 Tela-Botanica |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL |
* @version $$id$$ |
* @link /doc/jrest/ |
*/ |
class JrestService { |
protected $config; |
protected $script_time; |
protected $max_exec_time; |
public function JrestService($config) { |
$this->config = config; |
$this->script_time = microtime(true); |
$this->max_exec_time = ini_get('max_execution_time'); |
} |
public function isAdmin($id) { |
$admins = $this->config['jrest_admin']['admin']; |
$admin_tab = split(',',$admins); |
if (in_array($id,$admin_tab)) { |
return true; |
} else { |
return false; |
} |
} |
public function controleUtilisateur($id) { |
if ($_SESSION['user']['name'] == '') { |
//cas de la session temporaire, on ne fait rien de particulier |
} else { |
if (!$this->isAdmin($_SESSION['user']['name']) && $_SESSION['user']['name'] != $id) { |
// cas d'usurpation d'identité |
print 'Accès interdit'; |
exit(); |
} |
} |
} |
public function logger($index,$chaine) { |
if(!class_exists('Log')) { |
include_once('Log.php'); |
Log::getInstance(); |
} |
Log::setCheminLog($this->config['log']['cheminlog']); |
Log::setTimeZone($this->config['log']['timezone']); |
Log::setTailleMax($this->config['log']['taillemax']); |
Log::ajouterEntree($index,$chaine); |
} |
public function verifierOuRelancerExecution() { |
if((microtime(true) - $this->script_time) > ($this->max_exec_time - 5)*100) { |
set_time_limit(2); |
$this->logger('JRestService','Durée du script augmentée :'.microtime(true).' - '.$this->script_time.'.) > ('.$this->max_exec_time.' - 5)*1000000'); |
return true; |
} |
return false; |
} |
} |
?> |
/trunk/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; |
} |
} |
} |
?> |
/trunk/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); |
} |
} |
} |
?> |