Subversion Repositories Applications.framework

Compare Revisions

No changes between revisions

Ignore whitespace Rev 303 → Rev 304

/branches/v0.3-aleaume/framework/utilitaires/Fichier.php
New file
0,0 → 1,202
<?php
// declare(encoding='UTF-8');
/**
* Classe fournissant des méthodes statiques de manipulation des fichiers.
*
* @category PHP 5.2
* @package Utilitaire
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
* @link /doc/framework/
*/
class Fichier {
/**
* Créer et stocke du contenu dans un fichier.
*
* @param string le chemin et le nom du fichier.
* @param string le contenu à stocker dans le fichier.
* @return string true en cas de succès sinon false.
*/
public static function creerFichier($fichier, $contenu) {
$erreur = null;
// Début de l'écriture du fichier
if ($resource = fopen($fichier, 'w')) {
if (!fwrite($resource, $contenu)) {
if (!fclose($resource)) {
$erreur = "Le fichier '$fichier' n'a pas pu être fermé.";
}
} else {
$erreur = "Le contenu texte n'a pas pu être écrit dans le fichier '$fichier'.";
}
} else {
$erreur = "Le fichier '$fichier' n'a pas pu être ouvert.";
}
 
// Gestion des erreurs et du retour
if (is_null($erreur)) {
return true;
} else {
trigger_error($erreur, E_USER_WARNING);
return false;
}
}
/**
* Créer et stocke du contenu dans un fichier compressé en Gzip.
*
* @param string le chemin et le nom du fichier.
* @param string le contenu à stocker dans le fichier.
* @return string true en cas de succès sinon false.
*/
public static function creerFichierGzip($fichier, $contenu) {
$erreur = null;
// Ajout de l'extension gz
if (substr($fichier, -3) != '.gz') {
$fichier = $fichier.'.gz';
}
// Début de l'écriture du fichier compressé
if ($resource = gzopen($fichier, 'w9')) {
if (gzwrite($resource, $contenu)) {
if (!gzclose($resource)) {
$erreur = "Le fichier compressé '$fichier' n'a pas pu être fermé.";
}
} else {
$erreur = "Le contenu texte n'a pas pu être écrit dans le fichier compressé '$fichier'.";
}
} else {
$erreur = "Le fichier compressé '$fichier' n'a pas pu être ouvert.";
}
// Gestion des erreurs et du retour
if (is_null($erreur)) {
return true;
} else {
trigger_error($erreur, E_USER_WARNING);
return false;
}
}
/**
* Supprime récursivement un dossier et tout son contenu.
*
* @param string $dossier le chemin vers le dossier à supprimer.
* @return void
*/
public static function supprimerDossier($dossier) {
if (is_dir($dossier)) {
$objets = scandir($dossier);
foreach ($objets as $objet) {
if ($objet != '.' && $objet != '..') {
$chemin = $dossier.'/'.$objet;
if (filetype($chemin) == 'dir') {
$this->supprimerDossier($chemin);
} else {
unlink($chemin);
}
}
}
reset($objets);
rmdir($dossier);
}
}
/**
* Convertion d'un nombre d'octet en kB, MB, GB.
* @link http://forum.webmaster-rank.info/developpement-site/code-taille-memoire-d-une-variable-en-php-t1344.html
* @param integer $taille la taille en octet à convertir
*
* @return string la chaine représentant la taille en octets.
*/
public static function convertirTaille($taille) {
$unite = array('B', 'kB', 'MB', 'GB');
return @round($taille / pow(1024, ($i = floor(log($taille,1024)))), 2).' '.$unite[$i];
}
/**
* Détermine le dossier système temporaire et détecte si nous y avons accès en lecture et écriture.
*
* Inspiré de Zend_File_Transfer_Adapter_Abstract & Zend_Cache
*
* @return string|false le chemine vers le dossier temporaire ou false en cas d'échec.
*/
public static function getDossierTmp() {
$dossier_tmp = false;
foreach (array($_ENV, $_SERVER) as $environnement) {
foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $cle) {
if (isset($environnement[$cle])) {
if (($cle == 'windir') or ($cle == 'SystemRoot')) {
$dossier = realpath($environnement[$cle] . '\\temp');
} else {
$dossier = realpath($environnement[$cle]);
}
if (self::etreAccessibleEnLectureEtEcriture($dossier)) {
$dossier_tmp = $dossier;
break 2;
}
}
}
}
if ( ! $dossier_tmp) {
$dossier_televersement_tmp = ini_get('upload_tmp_dir');
if ($dossier_televersement_tmp) {
$dossier = realpath($dossier_televersement_tmp);
if (self::etreAccessibleEnLectureEtEcriture($dossier)) {
$dossier_tmp = $dossier;
}
}
}
if ( ! $dossier_tmp) {
if (function_exists('sys_get_temp_dir')) {
$dossier = sys_get_temp_dir();
if (self::etreAccessibleEnLectureEtEcriture($dossier)) {
$dossier_tmp = $dossier;
}
}
}
if ( ! $dossier_tmp) {
// Tentative de création d'un fichier temporaire
$fichier_tmp = tempnam(md5(uniqid(rand(), TRUE)), '');
if ($fichier_tmp) {
$dossier = realpath(dirname($fichier_tmp));
unlink($fichier_tmp);
if (self::etreAccessibleEnLectureEtEcriture($dossier)) {
$dossier_tmp = $dossier;
}
}
}
if ( ! $dossier_tmp && self::etreAccessibleEnLectureEtEcriture('/tmp')) {
$dossier_tmp = '/tmp';
}
if ( ! $dossier_tmp && self::etreAccessibleEnLectureEtEcriture('\\temp')) {
$dossier_tmp = '\\temp';
}
return $dossier_tmp;
}
/**
* Vérifie si le fichier ou dossier est accessible en lecture et écriture.
*
* @param $ressource chemin vers le dossier ou fichier à tester
* @return boolean true si la ressource est accessible en lecture et écriture.
*/
protected static function etreAccessibleEnLectureEtEcriture($ressource){
$accessible = false;
if (is_readable($ressource) && is_writable($ressource)) {
$accessible = true;
}
return $accessible;
}
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/Pattern.php
New file
0,0 → 1,24
<?php
// declare(encoding='UTF-8');
/**
* Classe fournissant des constantes correspondant à des expressions régulières de vérification très courrantes.
*
* @category PHP 5.2
* @package Utilitaire
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
* @link /doc/framework/
*/
class Pattern {
const PRENOM = "[\p{L}-]+";// Pattern prénom
const NOM = "[\p{Lu}]+";// Pattern nom
const COURRIEL = "[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";// Pattern courriel
const URL = "^(?:(?:ht|f)tp(?:s?)\\:\\/\\/|~/|/)?(?:\\w+:\\w+@)?(?:(?:[-\\w]+\\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?::[\\d]{1,5})?(?:(?:(?:/(?:[-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|/)+|\\?|#)?(?:(?:\\?(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)(?:&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*(?:#(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?$";
const HEURE_MINUTE = "^(?:[0-1][0-9]|2[0-4]):(?:[0-5][0-9]|60)$";// Heure au format 24h avec séparateur d'heure et minute ':'
const LATITUDE = "^-?([0-8]?[0-9]([.,][0-9]*)?|90)$"; // Nombre décimal positif ou négatif allant de 0 à 89 ou nombre entier valant 90 avec pour séparateur des décimales "." ou ","
const LONGITUDE = "^-?((1[0-7][0-9]|[1-9]?[0-9])([.,][0-9]*)?|180)$"; // Nombre décimal positif ou négatif allant de 0 à 179 ou nombre entier valant 180 avec pour séparateur des décimales "." ou ","
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/Tableau.php
New file
0,0 → 1,43
<?php
// declare(encoding='UTF-8');
/**
* Classe fournissant des méthodes statiques de manipulation des tableaux (Array).
*
* @category PHP 5.2
* @package Utilitaire
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
* @link /doc/framework/
*/
class Tableau {
/**
* Etend le tableau à étendre avec les données du tableau à copier. Si des clés sont identiques entre les deux tableaux
* une erreur est déclenchée et la valeur du tableau à étendre est gardée. Si les deux tableaux ont des clés numériques
* leurs valeurs sont gardées (à la différence de array_merge).
* Les tableaux sont passés par références et le tableau à copier est progressivement détruit pour éviter la consomation
* de mémoire.
*
* @param array $tableau_a_etendre
* @param array $tableau_a_copier
* @return void
*/
public static function etendre(Array &$tableau_a_etendre, Array &$tableau_a_copier) {
$cles_existantes = null;
foreach($tableau_a_copier as $cle => $val) {
if (!isset($tableau_a_etendre[$cle])) {
$tableau_a_etendre[$cle] = $val;
unset($tableau_a_copier[$cle]);
} else {
$cles_existantes = $cle;
}
}
if (is_array($cles_existantes)) {
$e = "Le tableau a étendre contenait déjà les clés suivantes : ".implode(', ', $cles_existantes);
trigger_error($e, E_USER_WARNING);
}
}
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/Storage.php
New file
0,0 → 1,132
<?php
 
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_OpenId
* @subpackage Zend_OpenId_Consumer
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
 
/**
* Abstract class to implement external storage for OpenID consumer
*
* @category Zend
* @package Zend_OpenId
* @subpackage Zend_OpenId_Consumer
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Storage
{
 
/**
* Stores information about association identified by $url/$handle
*
* @param string $url OpenID server URL
* @param string $handle assiciation handle
* @param string $macFunc HMAC function (sha1 or sha256)
* @param string $secret shared secret
* @param long $expires expiration UNIX time
* @return void
*/
abstract public function addAssociation($url, $handle, $macFunc, $secret, $expires);
 
/**
* Gets information about association identified by $url
* Returns true if given association found and not expired and false
* otherwise
*
* @param string $url OpenID server URL
* @param string &$handle assiciation handle
* @param string &$macFunc HMAC function (sha1 or sha256)
* @param string &$secret shared secret
* @param long &$expires expiration UNIX time
* @return bool
*/
abstract public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires);
 
/**
* Gets information about association identified by $handle
* Returns true if given association found and not expired and false
* othverwise
*
* @param string $handle assiciation handle
* @param string &$url OpenID server URL
* @param string &$macFunc HMAC function (sha1 or sha256)
* @param string &$secret shared secret
* @param long &$expires expiration UNIX time
* @return bool
*/
abstract public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires);
 
/**
* Deletes association identified by $url
*
* @param string $url OpenID server URL
* @return void
*/
abstract public function delAssociation($url);
 
/**
* Stores information discovered from identity $id
*
* @param string $id identity
* @param string $realId discovered real identity URL
* @param string $server discovered OpenID server URL
* @param float $version discovered OpenID protocol version
* @param long $expires expiration UNIX time
* @return void
*/
abstract public function addDiscoveryInfo($id, $realId, $server, $version, $expires);
 
/**
* Gets information discovered from identity $id
* Returns true if such information exists and false otherwise
*
* @param string $id identity
* @param string &$realId discovered real identity URL
* @param string &$server discovered OpenID server URL
* @param float &$version discovered OpenID protocol version
* @param long &$expires expiration UNIX time
* @return bool
*/
abstract public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires);
 
/**
* Removes cached information discovered from identity $id
*
* @param string $id identity
* @return bool
*/
abstract public function delDiscoveryInfo($id);
 
/**
* The function checks the uniqueness of openid.response_nonce
*
* @param string $provider openid.openid_op_endpoint field from authentication response
* @param string $nonce openid.response_nonce field from authentication response
* @return bool
*/
abstract public function isUniqueNonce($provider, $nonce);
 
/**
* Removes data from the uniqueness database that is older then given date
*
* @param string $date Date of expired data
*/
abstract public function purgeNonces($date=null);
}
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/StorageFile.php
New file
0,0 → 1,512
<?php
 
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_OpenId
* @subpackage Zend_OpenId_Consumer
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
 
/**
* External storage implemmentation using serialized files
*
* @category Zend
* @package Zend_OpenId
* @subpackage Zend_OpenId_Consumer
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class StorageFile extends Storage
{
 
/**
* Directory name to store data files in
*
* @var string $_dir
*/
private $_dir;
 
/**
* Constructs storage object and creates storage directory
*
* @param string $dir directory name to store data files in
* @throws Zend_OpenId_Exception
*/
public function __construct($dir = null)
{
if ($dir === null) {
$tmp = getenv('TMP');
if (empty($tmp)) {
$tmp = getenv('TEMP');
if (empty($tmp)) {
$tmp = "/tmp";
}
}
$user = get_current_user();
if (is_string($user) && !empty($user)) {
$tmp .= '/' . $user;
}
$dir = $tmp . '/openid/consumer';
}
$this->_dir = $dir;
if (!is_dir($this->_dir)) {
if (!@mkdir($this->_dir, 0700, 1)) {
/**
* @see Zend_OpenId_Exception
*/
require_once 'Zend/OpenId/Exception.php';
throw new Zend_OpenId_Exception(
'Cannot access storage directory ' . $dir,
Zend_OpenId_Exception::ERROR_STORAGE);
}
}
if (($f = fopen($this->_dir.'/assoc.lock', 'w+')) === null) {
/**
* @see Zend_OpenId_Exception
*/
/*require_once 'Zend/OpenId/Exception.php';
throw new Zend_OpenId_Exception(
'Cannot create a lock file in the directory ' . $dir,
Zend_OpenId_Exception::ERROR_STORAGE);*/
trigger_error('Cannot create a lock file in the directory ', E_STRICT);
}
fclose($f);
if (($f = fopen($this->_dir.'/discovery.lock', 'w+')) === null) {
/**
* @see Zend_OpenId_Exception
*/
/*require_once 'Zend/OpenId/Exception.php';
throw new Zend_OpenId_Exception(
'Cannot create a lock file in the directory ' . $dir,
Zend_OpenId_Exception::ERROR_STORAGE);*/
trigger_error('Cannot create a lock file in the directory', E_STRICT);
}
fclose($f);
if (($f = fopen($this->_dir.'/nonce.lock', 'w+')) === null) {
/**
* @see Zend_OpenId_Exception
*/
/*require_once 'Zend/OpenId/Exception.php';
throw new Zend_OpenId_Exception(
'Cannot create a lock file in the directory ' . $dir,
Zend_OpenId_Exception::ERROR_STORAGE);*/
trigger_error('Cannot create a lock file in the directory', E_STRICT);
}
fclose($f);
}
 
/**
* Stores information about association identified by $url/$handle
*
* @param string $url OpenID server URL
* @param string $handle assiciation handle
* @param string $macFunc HMAC function (sha1 or sha256)
* @param string $secret shared secret
* @param long $expires expiration UNIX time
* @return bool
*/
public function addAssociation($url, $handle, $macFunc, $secret, $expires)
{
$name1 = $this->_dir . '/assoc_url_' . md5($url);
$name2 = $this->_dir . '/assoc_handle_' . md5($handle);
$lock = @fopen($this->_dir . '/assoc.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name1, 'w+');
if ($f === false) {
fclose($lock);
return false;
}
$data = serialize(array($url, $handle, $macFunc, $secret, $expires));
fwrite($f, $data);
if (function_exists('symlink')) {
@unlink($name2);
if (symlink($name1, $name2)) {
fclose($f);
fclose($lock);
return true;
}
}
$f2 = @fopen($name2, 'w+');
if ($f2) {
fwrite($f2, $data);
fclose($f2);
@unlink($name1);
$ret = true;
} else {
$ret = false;
}
fclose($f);
fclose($lock);
return $ret;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Gets information about association identified by $url
* Returns true if given association found and not expired and false
* otherwise
*
* @param string $url OpenID server URL
* @param string &$handle assiciation handle
* @param string &$macFunc HMAC function (sha1 or sha256)
* @param string &$secret shared secret
* @param long &$expires expiration UNIX time
* @return bool
*/
public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
{
$name1 = $this->_dir . '/assoc_url_' . md5($url);
$lock = @fopen($this->_dir . '/assoc.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name1, 'r');
if ($f === false) {
fclose($lock);
return false;
}
$ret = false;
$data = stream_get_contents($f);
if (!empty($data)) {
list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
if ($url === $storedUrl && $expires > time()) {
$ret = true;
} else {
$name2 = $this->_dir . '/assoc_handle_' . md5($handle);
fclose($f);
@unlink($name2);
@unlink($name1);
fclose($lock);
return false;
}
}
fclose($f);
fclose($lock);
return $ret;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Gets information about association identified by $handle
* Returns true if given association found and not expired and false
* otherwise
*
* @param string $handle assiciation handle
* @param string &$url OpenID server URL
* @param string &$macFunc HMAC function (sha1 or sha256)
* @param string &$secret shared secret
* @param long &$expires expiration UNIX time
* @return bool
*/
public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires)
{
$name2 = $this->_dir . '/assoc_handle_' . md5($handle);
$lock = @fopen($this->_dir . '/assoc.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name2, 'r');
if ($f === false) {
fclose($lock);
return false;
}
$ret = false;
$data = stream_get_contents($f);
if (!empty($data)) {
list($url, $storedHandle, $macFunc, $secret, $expires) = unserialize($data);
if ($handle === $storedHandle && $expires > time()) {
$ret = true;
} else {
fclose($f);
@unlink($name2);
$name1 = $this->_dir . '/assoc_url_' . md5($url);
@unlink($name1);
fclose($lock);
return false;
}
}
fclose($f);
fclose($lock);
return $ret;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Deletes association identified by $url
*
* @param string $url OpenID server URL
* @return bool
*/
public function delAssociation($url)
{
$name1 = $this->_dir . '/assoc_url_' . md5($url);
$lock = @fopen($this->_dir . '/assoc.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name1, 'r');
if ($f === false) {
fclose($lock);
return false;
}
$data = stream_get_contents($f);
if (!empty($data)) {
list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
if ($url === $storedUrl) {
$name2 = $this->_dir . '/assoc_handle_' . md5($handle);
fclose($f);
@unlink($name2);
@unlink($name1);
fclose($lock);
return true;
}
}
fclose($f);
fclose($lock);
return true;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Stores information discovered from identity $id
*
* @param string $id identity
* @param string $realId discovered real identity URL
* @param string $server discovered OpenID server URL
* @param float $version discovered OpenID protocol version
* @param long $expires expiration UNIX time
* @return bool
*/
public function addDiscoveryInfo($id, $realId, $server, $version, $expires)
{
$name = $this->_dir . '/discovery_' . md5($id);
$lock = @fopen($this->_dir . '/discovery.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name, 'w+');
if ($f === false) {
fclose($lock);
return false;
}
$data = serialize(array($id, $realId, $server, $version, $expires));
fwrite($f, $data);
fclose($f);
fclose($lock);
return true;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Gets information discovered from identity $id
* Returns true if such information exists and false otherwise
*
* @param string $id identity
* @param string &$realId discovered real identity URL
* @param string &$server discovered OpenID server URL
* @param float &$version discovered OpenID protocol version
* @param long &$expires expiration UNIX time
* @return bool
*/
public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires)
{
$name = $this->_dir . '/discovery_' . md5($id);
$lock = @fopen($this->_dir . '/discovery.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name, 'r');
if ($f === false) {
fclose($lock);
return false;
}
$ret = false;
$data = stream_get_contents($f);
if (!empty($data)) {
list($storedId, $realId, $server, $version, $expires) = unserialize($data);
if ($id === $storedId && $expires > time()) {
$ret = true;
} else {
fclose($f);
@unlink($name);
fclose($lock);
return false;
}
}
fclose($f);
fclose($lock);
return $ret;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Removes cached information discovered from identity $id
*
* @param string $id identity
* @return bool
*/
public function delDiscoveryInfo($id)
{
$name = $this->_dir . '/discovery_' . md5($id);
$lock = @fopen($this->_dir . '/discovery.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
@unlink($name);
fclose($lock);
return true;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* The function checks the uniqueness of openid.response_nonce
*
* @param string $provider openid.openid_op_endpoint field from authentication response
* @param string $nonce openid.response_nonce field from authentication response
* @return bool
*/
public function isUniqueNonce($provider, $nonce)
{
$name = $this->_dir . '/nonce_' . md5($provider.';'.$nonce);
echo $name;
$lock = @fopen($this->_dir . '/nonce.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
}
try {
$f = @fopen($name, 'x');
if ($f === false) {
fclose($lock);
return false;
}
fwrite($f, $provider.';'.$nonce);
fclose($f);
fclose($lock);
return true;
} catch (Exception $e) {
fclose($lock);
throw $e;
}
}
 
/**
* Removes data from the uniqueness database that is older then given date
*
* @param mixed $date date of expired data
*/
public function purgeNonces($date=null)
{
$lock = @fopen($this->_dir . '/nonce.lock', 'w+');
if ($lock !== false) {
flock($lock, LOCK_EX);
}
try {
if (!is_int($date) && !is_string($date)) {
$nonceFiles = glob($this->_dir . '/nonce_*');
foreach ((array) $nonceFiles as $name) {
@unlink($name);
}
unset($nonceFiles);
} else {
if (is_string($date)) {
$time = time($date);
} else {
$time = $date;
}
$nonceFiles = glob($this->_dir . '/nonce_*');
foreach ((array) $nonceFiles as $name) {
if (filemtime($name) < $time) {
@unlink($name);
}
}
unset($nonceFiles);
}
if ($lock !== false) {
fclose($lock);
}
} catch (Exception $e) {
if ($lock !== false) {
fclose($lock);
}
throw $e;
}
}
}
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/DiffieHellmanUtil.php
New file
0,0 → 1,224
<?php
 
/**
* Classe utilitaire proposant des fonctions permettant la réalisation d'un
* échange de clé Diffie Hellman
*
* "En cryptographie, l'échange de clés Diffie-Hellman, du nom de ses auteurs
* Whitfield Diffie et Martin Hellman, est une méthode par laquelle deux
* personnes peuvent se mettre d'accord sur un nombre (qu'ils peuvent utiliser
* comme clé pour chiffrer la conversation suivante) sans qu'une troisième
* personne appelée Ève puisse découvrir le nombre, même en ayant écouté tous
* leurs échanges."
*
* Voir http://fr.wikipedia.org/wiki/%C3%89change_de_cl%C3%A9s_Diffie-Hellman
*
* */
class DiffieHellmanUtil {
// Default Diffie-Hellman key generator (1024 bit)
const DH_P = 'dcf93a0b883972ec0e19989ac5a2ce310e1d37717e8d9571bb7623731866e61ef75a2e27898b057f9891c2e27a639c3f29b60814581cd3b2ca3986d2683705577d45c2e7e52dc81c7a171876e5cea74b1448bfdfaf18828efd2519f14e45e3826634af1949e5b535cc829a483b8a76223e5d490a257f05bdff16f2fb22c583ab';
//Default Diffie-Hellman prime number (should be 2 or 5)
const DH_G = '02';
 
/**
* Performs the first step of a Diffie-Hellman key exchange by generating
* private and public DH values based on given prime number $p and
* generator $g. Both sides of key exchange MUST have the same prime number
* and generator. In this case they will able to create a random shared
* secret that is never send from one to the other.
*
* @param string $p prime number in binary representation
* @param string $g generator in binary representation
* @param string $priv_key private key in binary representation
* @return mixed
*/
public static function createDhKey($p, $g, $priv_key = null)
{
if (function_exists('openssl_dh_compute_key')) {
$dh_details = array(
'p' => $p,
'g' => $g
);
if ($priv_key !== null) {
$dh_details['priv_key'] = $priv_key;
}
return openssl_pkey_new(array('dh'=>$dh_details));
} else {
$bn_p = self::binToBigNum($p);
$bn_g = self::binToBigNum($g);
if ($priv_key === null) {
$priv_key = self::randomBytes(self::strlen($p));
}
$bn_priv_key = self::binToBigNum($priv_key);
if (extension_loaded('gmp')) {
$bn_pub_key = gmp_powm($bn_g, $bn_priv_key, $bn_p);
} else if (extension_loaded('bcmath')) {
$bn_pub_key = bcpowmod($bn_g, $bn_priv_key, $bn_p);
}
$pub_key = self::bigNumToBin($bn_pub_key);
 
return array(
'p' => $bn_p,
'g' => $bn_g,
'priv_key' => $bn_priv_key,
'pub_key' => $bn_pub_key,
'details' => array(
'p' => $p,
'g' => $g,
'priv_key' => $priv_key,
'pub_key' => $pub_key));
}
}
/**
* Returns an associative array with Diffie-Hellman key components in
* binary representation. The array includes original prime number 'p' and
* generator 'g', random private key 'priv_key' and corresponding public
* key 'pub_key'.
*
* @param mixed $dh Diffie-Hellman key
* @return array
*/
public static function getDhKeyDetails($dh)
{
if (function_exists('openssl_dh_compute_key')) {
$details = openssl_pkey_get_details($dh);
if (isset($details['dh'])) {
return $details['dh'];
}
} else {
return $dh['details'];
}
}
// Depuis OpenId.php, les fonctions de Diffie-Hellman
// TODO : voir si ça peut être externaliser pour être réutilisé ? => dans utilitaires
/**
* Computes the shared secret from the private DH value $dh and the other
* party's public value in $pub_key
*
* @param string $pub_key other party's public value
* @param mixed $dh Diffie-Hellman key
* @return string
* @throws Zend_OpenId_Exception
*/
public function computeDhSecret($pub_key, $dh)
{
if (function_exists('openssl_dh_compute_key')) {
$ret = openssl_dh_compute_key($pub_key, $dh);
if (ord($ret[0]) > 127) {
$ret = "\0" . $ret;
}
return $ret;
} else if (extension_loaded('gmp')) {
$bn_pub_key = self::binToBigNum($pub_key);
$bn_secret = gmp_powm($bn_pub_key, $dh['priv_key'], $dh['p']);
return self::bigNumToBin($bn_secret);
} else if (extension_loaded('bcmath')) {
$bn_pub_key = self::binToBigNum($pub_key);
$bn_secret = bcpowmod($bn_pub_key, $dh['priv_key'], $dh['p']);
return self::bigNumToBin($bn_secret);
}
/*require_once "Zend/OpenId/Exception.php";
throw new Zend_OpenId_Exception(
'The system doesn\'t have proper big integer extension',
Zend_OpenId_Exception::UNSUPPORTED_LONG_MATH);*/
trigger_error('Le système ne gère pas les nombre de taille arbitraire', E_STRICT);
}
/**
* Takes an arbitrary precision integer and returns its shortest big-endian
* two's complement representation.
*
* Arbitrary precision integers MUST be encoded as big-endian signed two's
* complement binary strings. Henceforth, "btwoc" is a function that takes
* an arbitrary precision integer and returns its shortest big-endian two's
* complement representation. All integers that are used with
* Diffie-Hellman Key Exchange are positive. This means that the left-most
* bit of the two's complement representation MUST be zero. If it is not,
* implementations MUST add a zero byte at the front of the string.
*
* @param string $str binary representation of arbitrary precision integer
* @return string big-endian signed representation
*/
public function btwoc($str)
{
if (ord($str[0]) > 127) {
return "\0" . $str;
}
return $str;
}
/**
* Produces string of random byte of given length.
*
* @param integer $len length of requested string
* @return string RAW random binary string
*/
public function randomBytes($len)
{
$key = '';
for($i=0; $i < $len; $i++) {
$key .= chr(mt_rand(0, 255));
}
return $key;
}
/**
* Returns lenght of binary string in bytes
*
* @param string $str
* @return int the string lenght
*/
static public function strlen($str)
{
if (extension_loaded('mbstring') &&
(((int)ini_get('mbstring.func_overload')) & 2)) {
return mb_strlen($str, 'latin1');
} else {
return strlen($str);
}
}
/**
* Converts binary representation into ext/gmp or ext/bcmath big integer
* representation.
*
* @param string $bin binary representation of big number
* @return mixed
* @throws Zend_OpenId_Exception
*/
protected function binToBigNum($bin)
{
if (extension_loaded('gmp')) {
return gmp_init(bin2hex($bin), 16);
} else if (extension_loaded('bcmath')) {
$bn = 0;
$len = self::strlen($bin);
for ($i = 0; $i < $len; $i++) {
$bn = bcmul($bn, 256);
$bn = bcadd($bn, ord($bin[$i]));
}
return $bn;
}
/*require_once "Zend/OpenId/Exception.php";
throw new Zend_OpenId_Exception(
'The system doesn\'t have proper big integer extension',
Zend_OpenId_Exception::UNSUPPORTED_LONG_MATH);*/
trigger_error('Le système ne gère pas les nombre de taille arbitraire', E_STRICT);
}
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/Encodage.php
New file
0,0 → 1,57
<?php
// declare(encoding='UTF-8');
/**
* Classe fournissant des méthodes statiques concernant l'encodage et le décodage des caractères de variable.
*
* @category PHP 5.2
* @package Utilitaire
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
* @link /doc/framework/
*/
class Encodage {
/**
* Méthode permettant d'encoder par défaut de ISO-8859-15 vers UTF-8 une variable ou un tableau de variables.
*
* @param mixed la chaine ou le tableau à encoder en UTF-8 depuis ISO-8859-15.
* @param string l'encodage d'origine si ce n'est pas ISO-8859-15.
* @return mixed la chaine ou le tableau encodé en UTF-8.
* @access protected
*/
public static function encoderEnUtf8(&$variable, $encodage = 'ISO-8859-15') {
//echo print_r($variable, true)."\n";
if (is_array($variable)) {
foreach ($variable as $c => $v) {
$variable[$c] = self::encoderEnUtf8($v);
}
} else {
// Nous vérifions si nous avons un bon encodage UTF-8
if (!is_numeric($variable) && !empty($variable) && !self::detecterUtf8($variable)) {
// Les nombres, les valeurs vides et ce qui est déjà en UTF-8 ne sont pas encodés.
$variable = mb_convert_encoding($variable, 'UTF-8', $encodage);
}
}
return $variable;
}
/**
* Méthode permettant de détecter réellement l'encodage UTF-8.
* mb_detect_encoding plante si la chaine de caractère se termine par un caractère accentué.
* Provient de PHPDIG.
*
* @param string la chaine à vérifier.
* @return bool true si c'est de UTF-8, sinon false.
* @access private
*/
public static function detecterUtf8($chaine) {
if ($chaine === mb_convert_encoding(mb_convert_encoding($chaine, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32')) {
return true;
} else {
return false;
}
}
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property
/branches/v0.3-aleaume/framework/utilitaires/Chaine.php
New file
0,0 → 1,68
<?php
// declare(encoding='UTF-8');
/**
* Classe fournissant des méthodes statiques de manipulation des chaînes de caractères (String).
*
* @category PHP 5.2
* @package Utilitaire
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
* @link /doc/framework/
*/
class Chaine {
 
private static $caracteres_accentues = array(
'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā',
'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď',
'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ',
'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ',
'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ',
'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ',
'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ',
'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ',
'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż',
'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ',
'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
 
private static $caracteres_normaux = array(
'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a',
'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd',
'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g',
'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i',
'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l',
'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R',
'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't',
'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y',
'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I',
'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
/**
* Supprime ou remplace les accents d'une chaine en PHP.
* La solution retenu, provenant du site
* {@link http://www.lecoindunet.com/zone_php/scripts_utiles/remplacer-les-caracteres-accentues-dune-chaine-en-php-72 lecoindunet.com},
* consiste en deux tableaux (caractères avec et sans accents) car cela permetra éventuellement
* de fournir des méthodes pour récupérer ces tableaux pour d'autres traitements.
* Une autre solution aurait été d'utiliser les entités HTML comme proposé par le site
* {@link http://www.weirdog.com/blog/php/supprimer-les-accents-des-caracteres-accentues.html weirdog.com}.
*
* @param String $chaine la chaine de caractères dans laquelle supprimer les accents.
* @param String $encodage le code d'encodage de la chaine si ce n'est pas de l'UTF-8.
* @return la chaine de caractères sans accent.
*/
function supprimerAccents($chaine, $encodage = 'UTF-8') {
if ($encodage != 'UTF-8') {
mb_convert_variables($encodage, 'UTF-8', self::$caracteres_accentues, self::$caracteres_normaux);
}
return str_replace(self::$caracteres_accentues, self::$caracteres_normaux, $chaine);
}
}
?>
Property changes:
Added: svn:keywords
+Id Author Date Revision HeadURL
\ No newline at end of property