]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/poche/Poche.class.php
Merge branch 'extraction-with-basic-auth' of git://github.com/aaa2000/poche into...
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
index d415dd03b6cd3820a7a1a62a041027657eeccd7d..0be1668d08f1649401cdcb89a606b1fdc20cfcb4 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /**
- * poche, a read it later open source system
+ * wallabag, self hostable application allowing you to not miss any content anymore
  *
- * @category   poche
- * @author     Nicolas LÅ“uillet <support@inthepoche.com>
+ * @category   wallabag
+ * @author     Nicolas LÅ“uillet <nicolas@loeuillet.org>
  * @copyright  2013
  * @license    http://www.wtfpl.net/ see COPYING file
  */
@@ -29,7 +29,8 @@ class Poche
         'dark' => array('requires' => array('default')),
         'dmagenta' => array('requires' => array('default')),
         'solarized' => array('requires' => array('default')),
-        'solarized-dark' => array('requires' => array('default'))
+        'solarized-dark' => array('requires' => array('default')),
+        'courgette' => array('requires' => array())
     );
 
     public function __construct()
@@ -49,6 +50,7 @@ class Poche
             if (! $this->store->isInstalled()) {
                 $this->install();
             }
+            $this->store->checkTags();
         }
     }
     
@@ -109,7 +111,7 @@ class Poche
         $passTheme = TRUE;
         # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
         if (! self::$canRenderTemplates) {
-            $this->notInstalledMessage[] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://doc.inthepoche.com/doku.php?id=users:begin:install">the documentation.</a>';
+            $this->notInstalledMessage[] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://doc.wallabag.org/doku.php?id=users:begin:install">the documentation.</a>';
             $passTheme = FALSE;
         }
 
@@ -324,6 +326,22 @@ class Poche
         );
     }
 
+    protected function getPageContent(Url $url)
+    {
+        $options = array('http' => array('user_agent' => 'poche'));
+        if (isset($_SERVER['AUTH_TYPE']) && "basic" === strtolower($_SERVER['AUTH_TYPE'])) {
+            $options['http']['header'] = sprintf(
+                "Authorization: Basic %s", 
+                base64_encode(
+                    sprintf('%s:%s', $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
+                )
+            );
+        }
+        $context = stream_context_create($options);
+        $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed', false, $context);
+        return json_decode($json, true);
+    }
+
     /**
      * Call action (mark as fav, archive, delete, etc.)
      */
@@ -332,8 +350,7 @@ class Poche
         switch ($action)
         {
             case 'add':
-                $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed');
-                $content = json_decode($json, true);
+                $content = $this->getPageContent($url);
                 $title = $content['rss']['channel']['item']['title'];
                 $body = $content['rss']['channel']['item']['description'];
 
@@ -446,7 +463,7 @@ class Poche
                 $themes = $this->getInstalledThemes();
                 $languages = $this->getInstalledLanguages();
                 $token = $this->user->getConfigValue('token');
-                $http_auth = (isset($_SERVER['PHP_AUTH_USER']))?true:false;
+                $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false;
                 $tpl_vars = array(
                     'themes' => $themes,
                     'languages' => $languages,
@@ -649,14 +666,18 @@ class Poche
      * it redirects the user to the $referer link
      * @return array
      */
-     private function credentials() {
-         if(isset($_SERVER['PHP_AUTH_USER'])) {
-             return array($_SERVER['PHP_AUTH_USER'],'php_auth');
-         }
-         if(!empty($_POST['login']) && !empty($_POST['password'])) {
-             return array($_POST['login'],$_POST['password']);
-         }
-         return array(false,false);
+    private function credentials() {
+        if(isset($_SERVER['PHP_AUTH_USER'])) {
+            return array($_SERVER['PHP_AUTH_USER'],'php_auth');
+        }
+        if(!empty($_POST['login']) && !empty($_POST['password'])) {
+            return array($_POST['login'],$_POST['password']);
+        }
+        if(isset($_SERVER['REMOTE_USER'])) {
+            return array($_SERVER['REMOTE_USER'],'http_auth');
+        }
+
+        return array(false,false);
      }
 
     /**
@@ -678,7 +699,8 @@ class Poche
             $user = $this->store->login($login, Tools::encodeString($password . $login));
             if ($user != array()) {
                 # Save login into Session
-                Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), array('poche_user' => new User($user)));
+               $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'));
                 Tools::logm('login successful');
                 Tools::redirect($referer);
@@ -800,26 +822,74 @@ class Poche
             $url = NULL;
             $favorite = FALSE;
             $archive = FALSE;
-            foreach ($value as $attr => $attr_value) {
-                if ($attr == 'article__url') {
-                    $url = new Url(base64_encode($attr_value));
-                }
-                $sequence = '';
-                if (STORAGE == 'postgres') {
-                    $sequence = 'entries_id_seq';
+            foreach ($value as $item) {
+                foreach ($item as $attr => $value) {
+                    if ($attr == 'article__url') {
+                        $url = new Url(base64_encode($value));
+                    }
+                    $sequence = '';
+                    if (STORAGE == 'postgres') {
+                        $sequence = 'entries_id_seq';
+                    }
+                    if ($value == 'true') {
+                        if ($attr == 'favorite') {
+                            $favorite = TRUE;
+                        }
+                        if ($attr == 'archive') {
+                            $archive = TRUE;
+                        }
+                    }
                 }
-                if ($attr_value == 'true') {
-                    if ($attr == 'favorite') {
-                        $favorite = TRUE;
+
+                # we can add the url
+                if (!is_null($url) && $url->isCorrect()) {
+                    $this->action('add', $url, 0, TRUE);
+                    $count++;
+                    if ($favorite) {
+                        $last_id = $this->store->getLastId($sequence);
+                        $this->action('toggle_fav', $url, $last_id, TRUE);
                     }
-                    if ($attr == 'archive') {
-                        $archive = TRUE;
+                    if ($archive) {
+                        $last_id = $this->store->getLastId($sequence);
+                        $this->action('toggle_archive', $url, $last_id, TRUE);
                     }
                 }
             }
+        }
+        $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.'));
+        Tools::logm('import from Readability completed');
+        Tools::redirect();
+    }
+
+    /**
+     * import from Poche exported file
+     * @param string $targetFile the file used for importing
+     * @return boolean 
+     */
+    private function importFromPoche($targetFile)
+    {
+        $str_data = file_get_contents($targetFile);
+        $data = json_decode($str_data,true);
+        Tools::logm('starting import from Poche');
+
+
+        $sequence = '';
+        if (STORAGE == 'postgres') {
+            $sequence = 'entries_id_seq';
+        }
+
+        $count = 0;
+        foreach ($data as $value) {
+
+            $url = new Url(base64_encode($value['url']));
+            $favorite = ($value['is_fav'] == -1);
+            $archive = ($value['is_read'] == -1);
+    
             # we can add the url
             if (!is_null($url) && $url->isCorrect()) {
+                
                 $this->action('add', $url, 0, TRUE);
+                
                 $count++;
                 if ($favorite) {
                     $last_id = $this->store->getLastId($sequence);
@@ -830,9 +900,10 @@ class Poche
                     $this->action('toggle_archive', $url, $last_id, TRUE);
                 }
             }
+            
         }
-        $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.'));
-        Tools::logm('import from Readability completed');
+        $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.'));
+        Tools::logm('import from Poche completed');
         Tools::redirect();
     }
 
@@ -847,7 +918,8 @@ class Poche
         $providers = array(
             'pocket' => 'importFromPocket',
             'readability' => 'importFromReadability',
-            'instapaper' => 'importFromInstapaper'
+            'instapaper' => 'importFromInstapaper',
+            'poche' => 'importFromPoche',
         );
         
         if (! isset($providers[$from])) {
@@ -897,7 +969,7 @@ class Poche
         if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
            $version = file_get_contents($cache_file);
         } else {
-           $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
+           $version = file_get_contents('http://static.wallabag.org/versions/' . $which);
            file_put_contents($cache_file, $version, LOCK_EX);
         }
         return $version;
@@ -945,7 +1017,7 @@ class Poche
         if (count($entries) > 0) {
             foreach ($entries as $entry) {
                 $newItem = $feed->createNewItem();
-                $newItem->setTitle(htmlentities($entry['title']));
+                $newItem->setTitle($entry['title']);
                 $newItem->setLink(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']);
                 $newItem->setDate(time());
                 $newItem->setDescription($entry['content']);