]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/poche/Poche.class.php
Merge branch 'dev' of https://github.com/wallabag/wallabag into dev
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
index 0be1668d08f1649401cdcb89a606b1fdc20cfcb4..9345d1c1cd9a73bbbf4e863a7ce445e0be24fe0f 100644 (file)
@@ -22,16 +22,6 @@ class Poche
     private $currentTheme = '';
     private $currentLanguage = '';
     private $notInstalledMessage = array();
-    
-    # @todo make this dynamic (actually install themes and save them in the database including author information et cetera)
-    private $installedThemes = array(
-        'default' => array('requires' => array()),
-        'dark' => array('requires' => array('default')),
-        'dmagenta' => array('requires' => array('default')),
-        'solarized' => array('requires' => array('default')),
-        'solarized-dark' => array('requires' => array('default')),
-        'courgette' => array('requires' => array())
-    );
 
     public function __construct()
     {
@@ -124,21 +114,26 @@ class Poche
         } 
         
         # Check if the selected theme and its requirements are present
-        if ($this->getTheme() != '' && ! is_dir(THEME . '/' . $this->getTheme())) {
-            $this->notInstalledMessage[] = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')';
+        $theme = $this->getTheme();
+
+        if ($theme != '' && ! is_dir(THEME . '/' . $theme)) {
+            $this->notInstalledMessage[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $theme . ')';
             
             self::$canRenderTemplates = false;
             
             $passTheme = FALSE;
         }
         
-        foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
-            if (! is_dir(THEME . '/' . $requiredTheme)) {
-                $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')';
+        $themeInfo = $this->getThemeInfo($theme);
+        if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) {
+            foreach ($themeInfo['requirements'] as $requiredTheme) {
+                if (! is_dir(THEME . '/' . $requiredTheme)) {
+                    $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')';
                 
-                self::$canRenderTemplates = false;
+                    self::$canRenderTemplates = false;
                 
-                $passTheme = FALSE;
+                    $passTheme = FALSE;
+                }
             }
         }
 
@@ -194,32 +189,36 @@ class Poche
     private function initTpl()
     {
         $loaderChain = new Twig_Loader_Chain();
+        $theme = $this->getTheme();
        
         # add the current theme as first to the loader chain so Twig will look there first for overridden template files
         try {
-            $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme()));
+            $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme));
         } catch (Twig_Error_Loader $e) {
             # @todo isInstalled() should catch this, inject Twig later
-            die('The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (' . THEME . '/' . $this->getTheme() .' is missing)');
+            die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme .' is missing)');
         }
         
         # add all required themes to the loader chain
-        foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
-            try {
-                $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . DEFAULT_THEME));
-            } catch (Twig_Error_Loader $e) {
-                # @todo isInstalled() should catch this, inject Twig later
-                die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')');
+        $themeInfo = $this->getThemeInfo($theme);
+        if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) {
+            foreach ($themeInfo['requirements'] as $requiredTheme) {
+                try {
+                    $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $requiredTheme));
+                } catch (Twig_Error_Loader $e) {
+                    # @todo isInstalled() should catch this, inject Twig later
+                    die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')');
+                }
             }
         }
         
         if (DEBUG_POCHE) {
-            $twig_params = array();
+            $twigParams = array();
         } else {
-            $twig_params = array('cache' => CACHE);
+            $twigParams = array('cache' => CACHE);
         }
         
-        $this->tpl = new Twig_Environment($loaderChain, $twig_params);
+        $this->tpl = new Twig_Environment($loaderChain, $twigParams);
         $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
         
         # filter to display domain name of an url
@@ -229,13 +228,9 @@ class Poche
         # filter for reading time
         $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime');
         $this->tpl->addFilter($filter);
-        
-        # filter for simple filenames in config view
-        $filter = new Twig_SimpleFilter('getPrettyFilename', function($string) { return str_replace(ROOT, '', $string); });
-        $this->tpl->addFilter($filter);
     }
 
-    private function install() 
+    private function install()
     {
         Tools::logm('poche still not installed');
         echo $this->tpl->render('install.twig', array(
@@ -266,34 +261,59 @@ class Poche
         return $this->currentTheme;
     }
 
-    public function getLanguage() {
-        return $this->currentLanguage;
+    /**
+     * Provides theme information by parsing theme.ini file if present in the theme's root directory.
+     * In all cases, the following data will be returned:
+     * - name: theme's name, or key if the theme is unnamed,
+     * - current: boolean informing if the theme is the current user theme.
+     *
+     * @param string $theme Theme key (directory name)
+     * @return array|boolean Theme information, or false if the theme doesn't exist.
+     */
+    public function getThemeInfo($theme) {
+        if (!is_dir(THEME . '/' . $theme)) {
+            return false;
+        }
+
+        $themeIniFile = THEME . '/' . $theme . '/theme.ini';
+        $themeInfo = array();
+
+        if (is_file($themeIniFile) && is_readable($themeIniFile)) {
+            $themeInfo = parse_ini_file($themeIniFile);
+        }
+        
+        if ($themeInfo === false) {
+            $themeInfo = array();
+        }
+        if (!isset($themeInfo['name'])) {
+            $themeInfo['name'] = $theme;
+        }
+        $themeInfo['current'] = ($theme === $this->getTheme());
+
+        return $themeInfo;
     }
     
     public function getInstalledThemes() {
         $handle = opendir(THEME);
         $themes = array();
-        
+
         while (($theme = readdir($handle)) !== false) {
             # Themes are stored in a directory, so all directory names are themes
             # @todo move theme installation data to database
-            if (! is_dir(THEME . '/' . $theme) || in_array($theme, array('..', '.'))) {
+            if (!is_dir(THEME . '/' . $theme) || in_array($theme, array('.', '..'))) {
                 continue;
             }
-            
-            $current = false;
-            
-            if ($theme === $this->getTheme()) {
-                $current = true;
-            }
-            
-            $themes[] = array('name' => $theme, 'current' => $current);
+
+            $themes[$theme] = $this->getThemeInfo($theme);
         }
-        
-        sort($themes);
+
         return $themes;
     }
 
+    public function getLanguage() {
+        return $this->currentLanguage;
+    }
+
     public function getInstalledLanguages() {
         $handle = opendir(LOCALE);
         $languages = array();
@@ -351,7 +371,7 @@ class Poche
         {
             case 'add':
                 $content = $this->getPageContent($url);
-                $title = $content['rss']['channel']['item']['title'];
+                $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
                 $body = $content['rss']['channel']['item']['description'];
 
                 if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) {
@@ -456,8 +476,8 @@ class Poche
         switch ($view)
         {
             case 'config':
-                $dev = $this->getPocheVersion('dev');
-                $prod = $this->getPocheVersion('prod');
+                $dev = trim($this->getPocheVersion('dev'));
+                $prod = trim($this->getPocheVersion('prod'));
                 $compare_dev = version_compare(POCHE, $dev);
                 $compare_prod = version_compare(POCHE, $prod);
                 $themes = $this->getInstalledThemes();
@@ -541,7 +561,8 @@ class Poche
                 
                 if (count($entries) > 0) {
                     $this->pagination->set_total(count($entries));
-                    $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
+                    $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
+                        $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'));
                     $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
                     $tpl_vars['entries'] = $datas;
                     $tpl_vars['page_links'] = $page_links;
@@ -600,8 +621,8 @@ class Poche
         $themes = $this->getInstalledThemes();
         $actualTheme = false;
         
-        foreach ($themes as $theme) {
-            if ($theme['name'] == $_POST['theme']) {
+        foreach (array_keys($themes) as $theme) {
+            if ($theme == $_POST['theme']) {
                 $actualTheme = true;
                 break;
             }
@@ -668,17 +689,17 @@ class Poche
      */
     private function credentials() {
         if(isset($_SERVER['PHP_AUTH_USER'])) {
-            return array($_SERVER['PHP_AUTH_USER'],'php_auth');
+            return array($_SERVER['PHP_AUTH_USER'],'php_auth',true);
         }
         if(!empty($_POST['login']) && !empty($_POST['password'])) {
-            return array($_POST['login'],$_POST['password']);
+            return array($_POST['login'],$_POST['password'],false);
         }
         if(isset($_SERVER['REMOTE_USER'])) {
-            return array($_SERVER['REMOTE_USER'],'http_auth');
+            return array($_SERVER['REMOTE_USER'],'http_auth',true);
         }
 
-        return array(false,false);
-     }
+        return array(false,false,false);
+    }
 
     /**
      * checks if login & password are correct and save the user in session.
@@ -689,19 +710,20 @@ class Poche
      */
     public function login($referer)
     {
-        list($login,$password)=$this->credentials();
+        list($login,$password,$isauthenticated)=$this->credentials();
         if($login === false || $password === false) {
             $this->messages->add('e', _('login failed: you have to fill all fields'));
             Tools::logm('login failed');
             Tools::redirect();
         }
         if (!empty($login) && !empty($password)) {
-            $user = $this->store->login($login, Tools::encodeString($password . $login));
+            $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated);
             if ($user != array()) {
                 # Save login into Session
-               $longlastingsession = isset($_POST['longlastingsession']);
-                Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), $longlastingsession, array('poche_user' => new User($user)));
-                $this->messages->add('s', _('welcome to your poche'));
+                $longlastingsession = isset($_POST['longlastingsession']);
+                $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login);
+                Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user)));
+                $this->messages->add('s', _('welcome to your wallabag'));
                 Tools::logm('login successful');
                 Tools::redirect($referer);
             }
@@ -720,7 +742,6 @@ class Poche
     {
         $this->user = array();
         Session::logout();
-        $this->messages->add('s', _('see you soon!'));
         Tools::logm('logout');
         Tools::redirect();
     }
@@ -984,6 +1005,7 @@ class Poche
             $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
         }
 
+        $token = str_replace('+', '', $token);
         $this->store->updateUserConfig($this->user->getId(), 'token', $token);
         $currentConfig = $_SESSION['poche_user']->config;
         $currentConfig['token'] = $token;