summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2014-01-04 01:47:07 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2014-01-04 01:47:07 +0100
commitd7c929e53b889460cd416d2864563e16200d2a01 (patch)
treeb91657455d48fa31aaa9b0bc6d8828db3622db1d /lib
parentcf8b3d080942ad682665569250038eda494d346b (diff)
parentc80ab2af7e0f6de4acf6dc02fab208d7b5baff45 (diff)
Merge remote-tracking branch 'origin/dev' into beta
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Configuration.php159
-rw-r--r--lib/Minz/Dispatcher.php25
-rw-r--r--lib/Minz/FileNotExistException.php2
-rw-r--r--lib/Minz/FrontController.php15
-rw-r--r--lib/Minz/Log.php37
-rw-r--r--lib/Minz/ModelArray.php147
-rw-r--r--lib/Minz/ModelPdo.php20
-rw-r--r--lib/Minz/ModelTxt.php84
-rw-r--r--lib/Minz/Request.php9
-rw-r--r--lib/Minz/Session.php18
-rw-r--r--lib/Minz/View.php29
-rw-r--r--lib/SimplePie/SimplePie/File.php1
-rw-r--r--lib/lib_opml.php121
-rw-r--r--lib/lib_rss.php177
14 files changed, 396 insertions, 448 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index 6c7206988..2c30661ed 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -28,7 +28,7 @@ class Minz_Configuration {
/**
* définition des variables de configuration
- * $sel_application une chaîne de caractères aléatoires (obligatoire)
+ * $salt une chaîne de caractères aléatoires (obligatoire)
* $environment gère le niveau d'affichage pour log et erreurs
* $use_url_rewriting indique si on utilise l'url_rewriting
* $base_url le chemin de base pour accéder à l'application
@@ -42,7 +42,7 @@ class Minz_Configuration {
* - password mot de passe de l'utilisateur
* - base le nom de la base de données
*/
- private static $sel_application = '';
+ private static $salt = '';
private static $environment = Minz_Configuration::PRODUCTION;
private static $base_url = '';
private static $use_url_rewriting = false;
@@ -51,20 +51,23 @@ class Minz_Configuration {
private static $cache_enabled = false;
private static $delay_cache = 3600;
private static $default_user = '';
- private static $current_user = '';
+ private static $allow_anonymous = false;
+ private static $auth_type = 'none';
private static $db = array (
- 'host' => false,
- 'user' => false,
- 'password' => false,
- 'base' => false
+ 'type' => 'mysql',
+ 'host' => '',
+ 'user' => '',
+ 'password' => '',
+ 'base' => '',
+ 'prefix' => '',
);
/*
* Getteurs
*/
public static function salt () {
- return self::$sel_application;
+ return self::$salt;
}
public static function environment () {
return self::$environment;
@@ -76,7 +79,7 @@ class Minz_Configuration {
return self::$use_url_rewriting;
}
public static function title () {
- return stripslashes(self::$title);
+ return self::$title;
}
public static function language () {
return self::$language;
@@ -93,8 +96,34 @@ class Minz_Configuration {
public static function defaultUser () {
return self::$default_user;
}
- public static function currentUser () {
- return self::$current_user;
+ public static function isAdmin($currentUser) {
+ return $currentUser === self::$default_user;
+ }
+ public static function allowAnonymous() {
+ return self::$allow_anonymous;
+ }
+ public static function authType() {
+ return self::$auth_type;
+ }
+ public static function needsLogin() {
+ return self::$auth_type !== 'none';
+ }
+ public static function canLogIn() {
+ return self::$auth_type === 'persona';
+ }
+
+ public static function _allowAnonymous($allow = false) {
+ self::$allow_anonymous = (bool)$allow;
+ }
+ public static function _authType($value) {
+ $value = strtolower($value);
+ switch ($value) {
+ case 'http_auth':
+ case 'persona':
+ case 'none':
+ self::$auth_type = $value;
+ break;
+ }
}
/**
@@ -113,22 +142,37 @@ class Minz_Configuration {
}
}
+ public static function writeFile() {
+ $ini_array = array(
+ 'general' => array(
+ 'environment' => self::$environment,
+ 'use_url_rewriting' => self::$use_url_rewriting,
+ 'salt' => self::$salt,
+ 'base_url' => self::$base_url,
+ 'title' => self::$title,
+ 'default_user' => self::$default_user,
+ 'allow_anonymous' => self::$allow_anonymous,
+ 'auth_type' => self::$auth_type,
+ ),
+ 'db' => self::$db,
+ );
+ @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
+ $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
+ if (function_exists('opcache_invalidate')) {
+ opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
+ }
+ return (bool)$result;
+ }
+
/**
- * Parse un fichier de configuration de type ".ini"
- * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
+ * Parse un fichier de configuration
+ * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
* @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
*/
private static function parseFile () {
- if (!file_exists (DATA_PATH . self::CONF_PATH_NAME)) {
- throw new Minz_FileNotExistException (
- DATA_PATH . self::CONF_PATH_NAME,
- Minz_Exception::ERROR
- );
- }
-
$ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
- if (!$ini_array) {
+ if (!is_array($ini_array)) {
throw new Minz_PermissionDeniedException (
DATA_PATH . self::CONF_PATH_NAME,
Minz_Exception::ERROR
@@ -144,23 +188,30 @@ class Minz_Configuration {
}
$general = $ini_array['general'];
- // sel_application est obligatoire
- if (!isset ($general['sel_application'])) {
- throw new Minz_BadConfigurationException (
- 'sel_application',
- Minz_Exception::ERROR
- );
+ // salt est obligatoire
+ if (!isset ($general['salt'])) {
+ if (isset($general['sel_application'])) { //v0.6
+ $general['salt'] = $general['sel_application'];
+ } else {
+ throw new Minz_BadConfigurationException (
+ 'salt',
+ Minz_Exception::ERROR
+ );
+ }
}
- self::$sel_application = $general['sel_application'];
+ self::$salt = $general['salt'];
if (isset ($general['environment'])) {
switch ($general['environment']) {
+ case Minz_Configuration::SILENT:
case 'silent':
self::$environment = Minz_Configuration::SILENT;
break;
+ case Minz_Configuration::DEVELOPMENT:
case 'development':
self::$environment = Minz_Configuration::DEVELOPMENT;
break;
+ case Minz_Configuration::PRODUCTION:
case 'production':
self::$environment = Minz_Configuration::PRODUCTION;
break;
@@ -195,26 +246,28 @@ class Minz_Configuration {
}
}
if (isset ($general['delay_cache'])) {
- self::$delay_cache = $general['delay_cache'];
+ self::$delay_cache = inval($general['delay_cache']);
}
if (isset ($general['default_user'])) {
self::$default_user = $general['default_user'];
- self::$current_user = self::$default_user;
+ }
+ if (isset ($general['allow_anonymous'])) {
+ self::$allow_anonymous = ((bool)($general['allow_anonymous'])) && ($general['allow_anonymous'] !== 'no');
+ }
+ if (isset ($general['auth_type'])) {
+ self::_authType($general['auth_type']);
}
// Base de données
- $db = false;
if (isset ($ini_array['db'])) {
$db = $ini_array['db'];
- }
- if ($db) {
- if (!isset ($db['host'])) {
+ if (empty($db['host'])) {
throw new Minz_BadConfigurationException (
'host',
Minz_Exception::ERROR
);
}
- if (!isset ($db['user'])) {
+ if (empty($db['user'])) {
throw new Minz_BadConfigurationException (
'user',
Minz_Exception::ERROR
@@ -226,33 +279,41 @@ class Minz_Configuration {
Minz_Exception::ERROR
);
}
- if (!isset ($db['base'])) {
+ if (empty($db['base'])) {
throw new Minz_BadConfigurationException (
'base',
Minz_Exception::ERROR
);
}
- self::$db['type'] = isset ($db['type']) ? $db['type'] : 'mysql';
+ if (!empty($db['type'])) {
+ self::$db['type'] = $db['type'];
+ }
self::$db['host'] = $db['host'];
self::$db['user'] = $db['user'];
self::$db['password'] = $db['password'];
self::$db['base'] = $db['base'];
- self::$db['prefix'] = isset ($db['prefix']) ? $db['prefix'] : '';
+ if (isset($db['prefix'])) {
+ self::$db['prefix'] = $db['prefix'];
+ }
}
}
- private static function setReporting () {
- if (self::environment () == self::DEVELOPMENT) {
- error_reporting (E_ALL);
- ini_set ('display_errors','On');
- ini_set('log_errors', 'On');
- } elseif (self::environment () == self::PRODUCTION) {
- error_reporting(E_ALL);
- ini_set('display_errors','Off');
- ini_set('log_errors', 'On');
- } else {
- error_reporting(0);
+ private static function setReporting() {
+ switch (self::$environment) {
+ case self::PRODUCTION:
+ error_reporting(E_ALL);
+ ini_set('display_errors','Off');
+ ini_set('log_errors', 'On');
+ break;
+ case self::DEVELOPMENT:
+ error_reporting(E_ALL);
+ ini_set('display_errors','On');
+ ini_set('log_errors', 'On');
+ break;
+ case self::SILENT:
+ error_reporting(0);
+ break;
}
}
}
diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php
index 2898b5f00..71dfe8ac6 100644
--- a/lib/Minz/Dispatcher.php
+++ b/lib/Minz/Dispatcher.php
@@ -22,7 +22,7 @@ class Minz_Dispatcher {
* Récupère l'instance du Dispatcher
*/
public static function getInstance ($router) {
- if (is_null (self::$instance)) {
+ if (self::$instance === null) {
self::$instance = new Minz_Dispatcher ($router);
}
return self::$instance;
@@ -40,19 +40,26 @@ class Minz_Dispatcher {
* Remplit le body de Response à partir de la Vue
* @exception Minz_Exception
*/
- public function run () {
+ public function run ($ob = true) {
$cache = new Minz_Cache();
// Le ob_start est dupliqué : sans ça il y a un bug sous Firefox
// ici on l'appelle avec 'ob_gzhandler', après sans.
// Vraisemblablement la compression fonctionne mais c'est sale
// J'ignore les effets de bord :(
- ob_start ('ob_gzhandler');
+ if ($ob) {
+ ob_start ('ob_gzhandler');
+ }
if (Minz_Cache::isEnabled () && !$cache->expired ()) {
- ob_start ();
+ if ($ob) {
+ ob_start ();
+ }
$cache->render ();
- $text = ob_get_clean();
+ if ($ob) {
+ $text = ob_get_clean();
+ }
} else {
+ $text = ''; //TODO: Clean this code
while (Minz_Request::$reseted) {
Minz_Request::$reseted = false;
@@ -67,9 +74,13 @@ class Minz_Dispatcher {
$this->controller->lastAction ();
if (!Minz_Request::$reseted) {
- ob_start ();
+ if ($ob) {
+ ob_start ();
+ }
$this->controller->view ()->build ();
- $text = ob_get_clean();
+ if ($ob) {
+ $text = ob_get_clean();
+ }
}
} catch (Minz_Exception $e) {
throw $e;
diff --git a/lib/Minz/FileNotExistException.php b/lib/Minz/FileNotExistException.php
index df2b8ff6c..f8dfbdf66 100644
--- a/lib/Minz/FileNotExistException.php
+++ b/lib/Minz/FileNotExistException.php
@@ -1,7 +1,7 @@
<?php
class Minz_FileNotExistException extends Minz_Exception {
public function __construct ($file_name, $code = self::ERROR) {
- $message = 'File doesn\'t exist : `' . $file_name.'`';
+ $message = 'File not found: `' . $file_name.'`';
parent::__construct ($message, $code);
}
diff --git a/lib/Minz/FrontController.php b/lib/Minz/FrontController.php
index 8e9c511a6..7b8526bc8 100644
--- a/lib/Minz/FrontController.php
+++ b/lib/Minz/FrontController.php
@@ -26,6 +26,8 @@ class Minz_FrontController {
protected $dispatcher;
protected $router;
+ private $useOb = true;
+
/**
* Constructeur
* Initialise le router et le dispatcher
@@ -61,7 +63,7 @@ class Minz_FrontController {
*/
public function run () {
try {
- $this->dispatcher->run ();
+ $this->dispatcher->run ($this->useOb);
Minz_Response::send ();
} catch (Minz_Exception $e) {
try {
@@ -94,4 +96,15 @@ class Minz_FrontController {
}
exit ('### Application problem ###<br />'."\n".$txt);
}
+
+ public function useOb() {
+ return $this->useOb;
+ }
+
+ /**
+ * Use ob_start('ob_gzhandler') or not.
+ */
+ public function _useOb($ob) {
+ return $this->useOb = (bool)$ob;
+ }
}
diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php
index a9b657271..e710aad4a 100644
--- a/lib/Minz/Log.php
+++ b/lib/Minz/Log.php
@@ -1,5 +1,5 @@
<?php
-/**
+/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
@@ -19,7 +19,7 @@ class Minz_Log {
const WARNING = 4;
const NOTICE = 8;
const DEBUG = 16;
-
+
/**
* Enregistre un message dans un fichier de log spécifique
* Message non loggué si
@@ -32,14 +32,14 @@ class Minz_Log {
*/
public static function record ($information, $level, $file_name = null) {
$env = Minz_Configuration::environment ();
-
+
if (! ($env === Minz_Configuration::SILENT
|| ($env === Minz_Configuration::PRODUCTION
&& ($level >= Minz_Log::NOTICE)))) {
- if (is_null ($file_name)) {
- $file_name = LOG_PATH . '/application.log';
+ if ($file_name === null) {
+ $file_name = LOG_PATH . '/' . Minz_Session::param('currentUser', '_') . '.log';
}
-
+
switch ($level) {
case Minz_Log::ERROR :
$level_label = 'error';
@@ -56,24 +56,13 @@ class Minz_Log {
default :
$level_label = 'unknown';
}
-
- if ($env == Minz_Configuration::PRODUCTION) {
- $file = @fopen ($file_name, 'a');
- } else {
- $file = fopen ($file_name, 'a');
- }
-
- if ($file !== false) {
- $log = '[' . date('r') . ']';
- $log .= ' [' . $level_label . ']';
- $log .= ' --- ' . $information . "\n";
- fwrite ($file, $log);
- fclose ($file);
- } else {
- throw new Minz_PermissionDeniedException (
- $file_name,
- Minz_Exception::ERROR
- );
+
+ $log = '[' . date('r') . ']'
+ . ' [' . $level_label . ']'
+ . ' --- ' . $information . "\n";
+
+ if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
+ throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
}
}
}
diff --git a/lib/Minz/ModelArray.php b/lib/Minz/ModelArray.php
index 4ba022143..ff23dbc83 100644
--- a/lib/Minz/ModelArray.php
+++ b/lib/Minz/ModelArray.php
@@ -1,5 +1,5 @@
<?php
-/**
+/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
@@ -7,116 +7,75 @@
/**
* La classe Model_array représente le modèle interragissant avec les fichiers de type texte gérant des tableaux php
*/
-class Minz_ModelArray extends Minz_ModelTxt {
+class Minz_ModelArray {
/**
- * $array Le tableau php contenu dans le fichier $nameFile
+ * $filename est le nom du fichier
*/
- protected $array = array ();
-
+ protected $filename;
+
/**
- * Ouvre le fichier indiqué, charge le tableau dans $array et le $nameFile
- * @param $nameFile le nom du fichier à ouvrir contenant un tableau
+ * Ouvre le fichier indiqué, charge le tableau dans $array et le $filename
+ * @param $filename le nom du fichier à ouvrir contenant un tableau
* Remarque : $array sera obligatoirement un tableau
*/
- public function __construct ($nameFile) {
- parent::__construct ($nameFile);
-
- if (!$this->getLock ('read')) {
- throw new Minz_PermissionDeniedException ($this->filename);
+ public function __construct ($filename) {
+ $this->filename = $filename;
+ }
+
+ protected function loadArray() {
+ if (!file_exists($this->filename)) {
+ throw new Minz_FileNotExistException($this->filename, Minz_Exception::WARNING);
+ }
+ elseif (($handle = $this->getLock()) === false) {
+ throw new Minz_PermissionDeniedException($this->filename);
} else {
- $this->array = include ($this->filename);
- $this->releaseLock ();
-
- if (!is_array ($this->array)) {
- $this->array = array ();
+ $data = include($this->filename);
+ $this->releaseLock($handle);
+
+ if ($data === false) {
+ throw new Minz_PermissionDeniedException($this->filename);
+ } elseif (!is_array($data)) {
+ $data = array();
}
-
- $this->array = $this->decodeArray ($this->array);
+ return $data;
}
- }
-
+ }
+
/**
- * Écrit un tableau dans le fichier $nameFile
- * @param $array le tableau php à enregistrer
+ * Sauve le tableau $array dans le fichier $filename
**/
- public function writeFile ($array) {
- if (!$this->getLock ('write')) {
- throw new Minz_PermissionDeniedException ($this->namefile);
- } else {
- $this->erase ();
-
- $this->writeLine ('<?php');
- $this->writeLine ('return ', false);
- $this->writeArray ($array);
- $this->writeLine (';');
-
- $this->releaseLock ();
- }
- }
-
- private function writeArray ($array, $profondeur = 0) {
- $tab = '';
- for ($i = 0; $i < $profondeur; $i++) {
- $tab .= "\t";
- }
- $this->writeLine ('array (');
-
- foreach ($array as $key => $value) {
- if (is_int ($key)) {
- $this->writeLine ($tab . "\t" . $key . ' => ', false);
- } else {
- $this->writeLine ($tab . "\t" . '\'' . $key . '\'' . ' => ', false);
- }
-
- if (is_array ($value)) {
- $this->writeArray ($value, $profondeur + 1);
- $this->writeLine (',');
- } else {
- if (is_numeric ($value)) {
- $this->writeLine ($value . ',');
- } else {
- $this->writeLine ('\'' . addslashes ($value) . '\',');
- }
- }
+ protected function writeArray($array) {
+ if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) {
+ throw new Minz_PermissionDeniedException($this->filename);
}
-
- $this->writeLine ($tab . ')', false);
- }
-
- private function decodeArray ($array) {
- $new_array = array ();
-
- foreach ($array as $key => $value) {
- if (is_array ($value)) {
- $new_array[$key] = $this->decodeArray ($value);
- } else {
- $new_array[$key] = stripslashes ($value);
- }
+ if (function_exists('opcache_invalidate')) {
+ opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
}
-
- return $new_array;
+ return true;
}
-
- private function getLock ($type) {
- if ($type == 'write') {
- $lock = LOCK_EX;
- } else {
- $lock = LOCK_SH;
+
+ private function getLock() {
+ $handle = fopen($this->filename, 'r');
+ if ($handle === false) {
+ return false;
}
-
- $count = 1;
- while (!flock ($this->file, $lock) && $count <= 50) {
- $count++;
+
+ $count = 50;
+ while (!flock($handle, LOCK_SH) && $count > 0) {
+ $count--;
+ usleep(1000);
}
-
- if ($count >= 50) {
- return false;
+
+ if ($count > 0) {
+ return $handle;
} else {
- return true;
+ fclose($handle);
+ return false;
}
}
-
- private function releaseLock () {
- flock ($this->file, LOCK_UN);
+
+ private function releaseLock($handle) {
+ flock($handle, LOCK_UN);
+ fclose($handle);
}
}
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php
index 9655539b2..831df13a2 100644
--- a/lib/Minz/ModelPdo.php
+++ b/lib/Minz/ModelPdo.php
@@ -1,5 +1,5 @@
<?php
-/**
+/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
@@ -23,7 +23,7 @@ class Minz_ModelPdo {
protected $bd;
protected $prefix;
-
+
/**
* Créé la connexion à la base de données à l'aide des variables
* HOST, BASE, USER et PASS définies dans le fichier de configuration
@@ -60,8 +60,7 @@ class Minz_ModelPdo {
);
self::$sharedBd = $this->bd;
- $userPrefix = Minz_Configuration::currentUser ();
- $this->prefix = $db['prefix'] . (empty($userPrefix) ? '' : ($userPrefix . '_'));
+ $this->prefix = $db['prefix'] . Minz_Session::param('currentUser', '_') . '_';
self::$sharedPrefix = $this->prefix;
} catch (Exception $e) {
throw new Minz_PDOConnectionException (
@@ -81,15 +80,24 @@ class Minz_ModelPdo {
$this->bd->rollBack();
}
- public function size() {
+ public function size($all = false) {
$db = Minz_Configuration::dataBase ();
$sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?';
- $stm = $this->bd->prepare ($sql);
$values = array ($db['base']);
+ if (!$all) {
+ $sql .= ' AND table_name LIKE ?';
+ $values[] = $this->prefix . '%';
+ }
+ $stm = $this->bd->prepare ($sql);
$stm->execute ($values);
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
}
+
+ public static function clean() {
+ self::$sharedBd = null;
+ self::$sharedPrefix = '';
+ }
}
class FreshPDO extends PDO {
diff --git a/lib/Minz/ModelTxt.php b/lib/Minz/ModelTxt.php
deleted file mode 100644
index 8c5973f4d..000000000
--- a/lib/Minz/ModelTxt.php
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
-*/
-
-/**
- * La classe Model_txt représente le modèle interragissant avec les fichiers de type texte
- */
-class Minz_ModelTxt {
- /**
- * $file représente le fichier à ouvrir
- */
- protected $file;
-
- /**
- * $filename est le nom du fichier
- */
- protected $filename;
-
- /**
- * Ouvre un fichier dans $file
- * @param $nameFile nom du fichier à ouvrir
- * @param $mode mode d'ouverture du fichier ('a+' par défaut)
- * @exception FileNotExistException si le fichier n'existe pas
- * > ou ne peux pas être ouvert
- */
- public function __construct ($nameFile, $mode = 'a+') {
- $this->filename = $nameFile;
- if (!file_exists($this->filename)) {
- throw new Minz_FileNotExistException (
- $this->filename,
- Minz_Exception::WARNING
- );
- }
-
- $this->file = @fopen ($this->filename, $mode);
-
- if (!$this->file) {
- throw new Minz_PermissionDeniedException (
- $this->filename,
- Minz_Exception::WARNING
- );
- }
- }
-
- /**
- * Lit une ligne de $file
- * @return une ligne du fichier
- */
- public function readLine () {
- return fgets ($this->file);
- }
-
- /**
- * Écrit une ligne dans $file
- * @param $line la ligne à écrire
- */
- public function writeLine ($line, $newLine = true) {
- $char = '';
- if ($newLine) {
- $char = "\n";
- }
-
- fwrite ($this->file, $line . $char);
- }
-
- /**
- * Efface le fichier $file
- * @return true en cas de succès, false sinon
- */
- public function erase () {
- return ftruncate ($this->file, 0);
- }
-
- /**
- * Ferme $file
- */
- public function __destruct () {
- if (isset ($this->file)) {
- fclose ($this->file);
- }
- }
-}
diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php
index fb48bd7a2..d4e1355d7 100644
--- a/lib/Minz/Request.php
+++ b/lib/Minz/Request.php
@@ -96,7 +96,14 @@ class Minz_Request {
* @return la base de l'url
*/
public static function getBaseUrl () {
- return Minz_Configuration::baseUrl ();
+ $defaultBaseUrl = Minz_Configuration::baseUrl();
+ if (!empty($defaultBaseUrl)) {
+ return $defaultBaseUrl;
+ } elseif (isset($_SERVER['REQUEST_URI'])) {
+ return dirname($_SERVER['REQUEST_URI']) . '/';
+ } else {
+ return '/';
+ }
}
/**
diff --git a/lib/Minz/Session.php b/lib/Minz/Session.php
index f527322f5..37faff0fb 100644
--- a/lib/Minz/Session.php
+++ b/lib/Minz/Session.php
@@ -8,7 +8,7 @@ class Minz_Session {
/**
* $session stocke les variables de session
*/
- private static $session = array ();
+ private static $session = array (); //TODO: Try to avoid having another local copy
/**
* Initialise la session, avec un nom
@@ -33,13 +33,7 @@ class Minz_Session {
* @return la valeur de la variable de session, false si n'existe pas
*/
public static function param ($p, $default = false) {
- if (isset (self::$session[$p])) {
- $return = self::$session[$p];
- } else {
- $return = $default;
- }
-
- return $return;
+ return isset(self::$session[$p]) ? self::$session[$p] : $default;
}
@@ -55,11 +49,6 @@ class Minz_Session {
} else {
$_SESSION[$p] = $v;
self::$session[$p] = $v;
-
- if($p == 'language') {
- // reset pour remettre à jour le fichier de langue à utiliser
- Minz_Translate::reset ();
- }
}
}
@@ -71,11 +60,12 @@ class Minz_Session {
public static function unset_session ($force = false) {
$language = self::param ('language');
- session_unset ();
+ session_destroy();
self::$session = array ();
if (!$force) {
self::_param ('language', $language);
+ Minz_Translate::reset ();
}
}
}
diff --git a/lib/Minz/View.php b/lib/Minz/View.php
index c8d0aefed..ba9555cd7 100644
--- a/lib/Minz/View.php
+++ b/lib/Minz/View.php
@@ -13,7 +13,7 @@ class Minz_View {
const LAYOUT_FILENAME = '/layout.phtml';
private $view_filename = '';
- private $use_layout = false;
+ private $use_layout = null;
private static $title = '';
private static $styles = array ();
@@ -31,12 +31,6 @@ class Minz_View {
. Minz_Request::controllerName () . '/'
. Minz_Request::actionName () . '.phtml';
- if (file_exists (APP_PATH
- . self::LAYOUT_PATH_NAME
- . self::LAYOUT_FILENAME)) {
- $this->use_layout = true;
- }
-
self::$title = Minz_Configuration::title ();
}
@@ -44,6 +38,9 @@ class Minz_View {
* Construit la vue
*/
public function build () {
+ if ($this->use_layout === null) { //TODO: avoid file_exists and require views to be explicit
+ $this->use_layout = file_exists (APP_PATH . self::LAYOUT_PATH_NAME . self::LAYOUT_FILENAME);
+ }
if ($this->use_layout) {
$this->buildLayout ();
} else {
@@ -66,10 +63,8 @@ class Minz_View {
* Affiche la Vue en elle-même
*/
public function render () {
- if (file_exists ($this->view_filename)) {
- include ($this->view_filename);
- } else {
- Minz_Log::record ('File doesn\'t exist : `'
+ if ((@include($this->view_filename)) === false) {
+ Minz_Log::record ('File not found: `'
. $this->view_filename . '`',
Minz_Log::NOTICE);
}
@@ -84,10 +79,8 @@ class Minz_View {
. self::LAYOUT_PATH_NAME . '/'
. $part . '.phtml';
- if (file_exists ($fic_partial)) {
- include ($fic_partial);
- } else {
- Minz_Log::record ('File doesn\'t exist : `'
+ if ((@include($fic_partial)) === false) {
+ Minz_Log::record ('File not found: `'
. $fic_partial . '`',
Minz_Log::WARNING);
}
@@ -102,10 +95,8 @@ class Minz_View {
. '/views/helpers/'
. $helper . '.phtml';
- if (file_exists ($fic_helper)) {
- include ($fic_helper);
- } else {
- Minz_Log::record ('File doesn\'t exist : `'
+ if ((@include($fic_helper)) === false) {;
+ Minz_Log::record ('File not found: `'
. $fic_helper . '`',
Minz_Log::WARNING);
}
diff --git a/lib/SimplePie/SimplePie/File.php b/lib/SimplePie/SimplePie/File.php
index 063ad955e..cf926cf5a 100644
--- a/lib/SimplePie/SimplePie/File.php
+++ b/lib/SimplePie/SimplePie/File.php
@@ -77,6 +77,7 @@ class SimplePie_File
$this->useragent = $useragent;
if (preg_match('/^http(s)?:\/\//i', $url))
{
+ syslog(LOG_INFO, 'SimplePie GET ' . $url); //FreshRSS
if ($useragent === null)
{
$useragent = ini_get('user_agent');
diff --git a/lib/lib_opml.php b/lib/lib_opml.php
new file mode 100644
index 000000000..1b5517d7f
--- /dev/null
+++ b/lib/lib_opml.php
@@ -0,0 +1,121 @@
+<?php
+function opml_export ($cats) {
+ $txt = '';
+
+ foreach ($cats as $cat) {
+ $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
+
+ foreach ($cat['feeds'] as $feed) {
+ $txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . htmlspecialchars($feed->description(), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
+ }
+
+ $txt .= '</outline>' . "\n";
+ }
+
+ return $txt;
+}
+
+function opml_import ($xml) {
+ $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
+
+ $dom = new DOMDocument();
+ $dom->recover = true;
+ $dom->strictErrorChecking = false;
+ $dom->loadXML($xml);
+ $dom->encoding = 'UTF-8';
+
+ $opml = simplexml_import_dom($dom);
+
+ if (!$opml) {
+ throw new FreshRSS_Opml_Exception ();
+ }
+
+ $catDAO = new FreshRSS_CategoryDAO();
+ $catDAO->checkDefault();
+ $defCat = $catDAO->getDefault();
+
+ $categories = array ();
+ $feeds = array ();
+
+ foreach ($opml->body->outline as $outline) {
+ if (!isset ($outline['xmlUrl'])) {
+ // Catégorie
+ $title = '';
+
+ if (isset ($outline['text'])) {
+ $title = (string) $outline['text'];
+ } elseif (isset ($outline['title'])) {
+ $title = (string) $outline['title'];
+ }
+
+ if ($title) {
+ // Permet d'éviter les soucis au niveau des id :
+ // ceux-ci sont générés en fonction de la date,
+ // un flux pourrait être dans une catégorie X avec l'id Y
+ // alors qu'il existe déjà la catégorie X mais avec l'id Z
+ // Y ne sera pas ajouté et le flux non plus vu que l'id
+ // de sa catégorie n'exisera pas
+ $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
+ $catDAO = new FreshRSS_CategoryDAO ();
+ $cat = $catDAO->searchByName ($title);
+ if ($cat === false) {
+ $cat = new FreshRSS_Category ($title);
+ $values = array (
+ 'name' => $cat->name (),
+ 'color' => $cat->color ()
+ );
+ $cat->_id ($catDAO->addCategory ($values));
+ }
+
+ $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
+ }
+ } else {
+ // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
+ $feeds[] = getFeed ($outline, $defCat->id());
+ }
+ }
+
+ return array ($categories, $feeds);
+}
+
+/**
+ * import all feeds of a given outline tag
+ */
+function getFeedsOutline ($outline, $cat_id) {
+ $feeds = array ();
+
+ foreach ($outline->children () as $child) {
+ if (isset ($child['xmlUrl'])) {
+ $feeds[] = getFeed ($child, $cat_id);
+ } else {
+ $feeds = array_merge(
+ $feeds,
+ getFeedsOutline ($child, $cat_id)
+ );
+ }
+ }
+
+ return $feeds;
+}
+
+function getFeed ($outline, $cat_id) {
+ $url = (string) $outline['xmlUrl'];
+ $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
+ $title = '';
+ if (isset ($outline['text'])) {
+ $title = (string) $outline['text'];
+ } elseif (isset ($outline['title'])) {
+ $title = (string) $outline['title'];
+ }
+ $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
+ $feed = new FreshRSS_Feed ($url);
+ $feed->_category ($cat_id);
+ $feed->_name ($title);
+ if (isset($outline['htmlUrl'])) {
+ $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
+ }
+ if (isset($outline['description'])) {
+ $feed->_description(sanitizeHTML((string)$outline['description']));
+ }
+ return $feed;
+}
diff --git a/lib/lib_rss.php b/lib/lib_rss.php
index a1fadcb24..4f98ed14a 100644
--- a/lib/lib_rss.php
+++ b/lib/lib_rss.php
@@ -56,16 +56,6 @@ function checkUrl($url) {
}
}
-// vérifie qu'on est connecté
-function is_logged () {
- return Minz_Session::param ('mail') != false;
-}
-
-// vérifie que le système d'authentification est configuré
-function login_is_conf ($conf) {
- return $conf->mailLogin () != false;
-}
-
// tiré de Shaarli de Seb Sauvage //Format RFC 4648 base64url
function small_hash ($txt) {
$t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
@@ -98,40 +88,20 @@ function timestamptodate ($t, $hour = true) {
return @date ($date, $t);
}
-function sortEntriesByDate ($entry1, $entry2) {
- return $entry2->date (true) - $entry1->date (true);
-}
-function sortReverseEntriesByDate ($entry1, $entry2) {
- return $entry1->date (true) - $entry2->date (true);
-}
-
-function get_domain ($url) {
- return parse_url($url, PHP_URL_HOST);
-}
-
-function opml_export ($cats) {
- $txt = '';
-
- foreach ($cats as $cat) {
- $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
-
- foreach ($cat['feeds'] as $feed) {
- $txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . htmlspecialchars($feed->description(), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
- }
-
- $txt .= '</outline>' . "\n";
- }
-
- return $txt;
-}
-
function html_only_entity_decode($text) {
static $htmlEntitiesOnly = null;
if ($htmlEntitiesOnly === null) {
- $htmlEntitiesOnly = array_flip(array_diff(
- get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
- get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
- ));
+ if (version_compare(PHP_VERSION, '5.3.4') >= 0) {
+ $htmlEntitiesOnly = array_flip(array_diff(
+ get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
+ get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
+ ));
+ } else {
+ $htmlEntitiesOnly = array_map('utf8_encode', array_flip(array_diff(
+ get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES), //Decode HTML entities
+ get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES) //Preserve XML entities
+ )));
+ }
}
return strtr($text, $htmlEntitiesOnly);
}
@@ -144,112 +114,6 @@ function sanitizeHTML($data) {
return html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_MAYBE_HTML));
}
-function opml_import ($xml) {
- $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
-
- $dom = new DOMDocument();
- $dom->recover = true;
- $dom->strictErrorChecking = false;
- $dom->loadXML($xml);
- $dom->encoding = 'UTF-8';
-
- $opml = simplexml_import_dom($dom);
-
- if (!$opml) {
- throw new FreshRSS_Opml_Exception ();
- }
-
- $catDAO = new FreshRSS_CategoryDAO();
- $catDAO->checkDefault();
- $defCat = $catDAO->getDefault();
-
- $categories = array ();
- $feeds = array ();
-
- foreach ($opml->body->outline as $outline) {
- if (!isset ($outline['xmlUrl'])) {
- // Catégorie
- $title = '';
-
- if (isset ($outline['text'])) {
- $title = (string) $outline['text'];
- } elseif (isset ($outline['title'])) {
- $title = (string) $outline['title'];
- }
-
- if ($title) {
- // Permet d'éviter les soucis au niveau des id :
- // ceux-ci sont générés en fonction de la date,
- // un flux pourrait être dans une catégorie X avec l'id Y
- // alors qu'il existe déjà la catégorie X mais avec l'id Z
- // Y ne sera pas ajouté et le flux non plus vu que l'id
- // de sa catégorie n'exisera pas
- $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
- $catDAO = new FreshRSS_CategoryDAO ();
- $cat = $catDAO->searchByName ($title);
- if ($cat === false) {
- $cat = new FreshRSS_Category ($title);
- $values = array (
- 'name' => $cat->name (),
- 'color' => $cat->color ()
- );
- $cat->_id ($catDAO->addCategory ($values));
- }
-
- $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
- }
- } else {
- // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
- $feeds[] = getFeed ($outline, $defCat->id());
- }
- }
-
- return array ($categories, $feeds);
-}
-
-/**
- * import all feeds of a given outline tag
- */
-function getFeedsOutline ($outline, $cat_id) {
- $feeds = array ();
-
- foreach ($outline->children () as $child) {
- if (isset ($child['xmlUrl'])) {
- $feeds[] = getFeed ($child, $cat_id);
- } else {
- $feeds = array_merge(
- $feeds,
- getFeedsOutline ($child, $cat_id)
- );
- }
- }
-
- return $feeds;
-}
-
-function getFeed ($outline, $cat_id) {
- $url = (string) $outline['xmlUrl'];
- $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
- $title = '';
- if (isset ($outline['text'])) {
- $title = (string) $outline['text'];
- } elseif (isset ($outline['title'])) {
- $title = (string) $outline['title'];
- }
- $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
- $feed = new FreshRSS_Feed ($url);
- $feed->_category ($cat_id);
- $feed->_name ($title);
- if (isset($outline['htmlUrl'])) {
- $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
- }
- if (isset($outline['description'])) {
- $feed->_description(sanitizeHTML((string)$outline['description']));
- }
- return $feed;
-}
-
-
/* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
function get_content_by_parsing ($url, $path) {
require_once (LIB_PATH . '/lib_phpQuery.php');
@@ -307,5 +171,22 @@ function uSecString() {
}
function invalidateHttpCache() {
- file_put_contents(DATA_PATH . '/touch.txt', uTimeString());
+ touch(LOG_PATH . '/' . Minz_Session::param('currentUser', '_') . '.log');
+ Minz_Session::_param('touch', uTimeString());
+}
+
+function usernameFromPath($userPath) {
+ if (preg_match('%/([a-z0-9]{1,16})_user\.php$%', $userPath, $matches)) {
+ return $matches[1];
+ } else {
+ return '';
+ }
+}
+
+function listUsers() {
+ return array_map('usernameFromPath', glob(DATA_PATH . '/*_user.php'));
+}
+
+function httpAuthUser() {
+ return isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '';
}