123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /*
- @nom: Configuration
- @auteur: Idleman (http://blog.idleman.fr)
- @description: Classe de gestion des préférences, fonctionne sur un simple système clé=>valeur avec un cache session pour eviter les requête inutiles
- */
- class Configuration extends MysqlEntity{
- protected $id,$key,$value,$confTab;
- protected $TABLE_NAME = 'configuration';
- protected $object_fields =
- array(
- 'id'=>'key',
- 'key'=>'string',
- 'value'=>'longstring'
- );
- protected $object_fields_uniques =
- array(
- 'key'
- );
- protected $options = array(
- 'articleDisplayAnonymous' => '0',
- 'articleDisplayAuthor' => '1',
- 'articleDisplayDate' => '1',
- 'articleDisplayFolderSort' => '1',
- 'articleDisplayHomeSort' => '1',
- 'articleDisplayLink' => '1',
- 'articleDisplayMode' => 'summary',
- 'articlePerPages' => '5',
- 'displayOnlyUnreadFeedFolder' => 'false',
- 'feedMaxEvents' => '50',
- 'language' => 'en',
- 'optionFeedIsVerbose' => 1,
- 'paginationScale' => 5,
- 'syncGradCount' => '10',
- 'synchronisationCode' => '',
- 'synchronisationEnableCache' => '0',
- 'synchronisationForceFeed' => '0',
- 'synchronisationType' => 'auto',
- 'theme' => 'marigolds',
- 'root' => '',
- 'cryptographicSalt' => '',
- 'otpEnabled' => 0
- );
- function __construct(){
- parent::__construct();
- }
- public function getAll(){
- if(!isset($_SESSION['configuration'])){
- $configurationManager = new Configuration();
- $configs = $configurationManager->populate();
- $confTab = array();
- foreach($configs as $config){
- $this->confTab[$config->getKey()] = $config->getValue();
- }
- $_SESSION['configuration'] = serialize($this->confTab);
- }else{
- $this->confTab = unserialize($_SESSION['configuration']);
- }
- }
- public function get($key){
- return (isset($this->confTab[$key])?$this->confTab[$key]:'');
- }
- public function put($key,$value){
- $configurationManager = new Configuration();
- if (isset($this->confTab[$key])){
- $configurationManager->change(array('value'=>$value),array('key'=>$key));
- } else {
- $configurationManager->add($key,$value);
- }
- $this->confTab[$key] = $value;
- unset($_SESSION['configuration']);
- }
- protected function createSynchronisationCode() {
- return substr(sha1(rand(0,30).time().rand(0,30)),0,10);
- }
- public function add($key,$value){
- $config = new Configuration();
- $config->setKey($key);
- $config->setValue($value);
- $config->save();
- $this->confTab[$key] = $value;
- unset($_SESSION['configuration']);
- }
- public function setDefaults() {
- foreach($this->options as $option => $defaultValue) {
- switch($option) {
- case 'language':
- $value = isset($_POST['install_changeLngLeed']) ? $_POST['install_changeLngLeed'] : $defaultValue;
- break;
- case 'theme':
- $value = isset($_POST['template']) ? $_POST['template'] : $defaultValue;
- break;
- case 'synchronisationCode':
- $value = $this->createSynchronisationCode();
- break;
- case 'root':
- $root = $_POST['root'];
- $value = (substr($root, strlen($root)-1)=='/'?$root:$root.'/');
- break;
- case 'cryptographicSalt':
- $value = $this->generateSalt();
- break;
- default:
- $value = $defaultValue;
- break;
- }
- $this->add($option, $value);
- }
- }
- protected function generateSalt() {
- return ''.mt_rand().mt_rand();
- }
- function getId(){
- return $this->id;
- }
- function getKey(){
- return $this->key;
- }
- function setKey($key){
- $this->key = $key;
- }
- function getValue(){
- return $this->value;
- }
- function setValue($value){
- $this->value = $value;
- }
- }
- ?>
|