]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
refactoring
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
1 <?php
2 /**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11 class Poche
12 {
13 public $store;
14 public $tpl;
15
16 function __construct($storage_type)
17 {
18 $this->store = new $storage_type();
19 $this->init();
20
21 # installation
22 if(!$this->store->isInstalled())
23 {
24 $this->install();
25 }
26
27 $this->saveUser();
28 }
29
30 private function init()
31 {
32 # l10n
33 putenv('LC_ALL=' . LANG);
34 setlocale(LC_ALL, LANG);
35 bindtextdomain(LANG, LOCALE);
36 textdomain(LANG);
37
38 # template engine
39 $loader = new Twig_Loader_Filesystem(TPL);
40 $this->tpl = new Twig_Environment($loader, array(
41 'cache' => CACHE,
42 ));
43 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
44
45 Tools::initPhp();
46 Session::init();
47 }
48
49 private function install()
50 {
51 Tools::logm('poche still not installed');
52 echo $this->tpl->render('install.twig', array(
53 'token' => Session::getToken(),
54 ));
55 if (isset($_GET['install'])) {
56 if (($_POST['password'] == $_POST['password_repeat'])
57 && $_POST['password'] != "" && $_POST['login'] != "") {
58 # let's rock, install poche baby !
59 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
60 Session::logout();
61 Tools::redirect();
62 }
63 }
64 exit();
65 }
66
67 private function saveUser()
68 {
69 $_SESSION['login'] = (isset ($_SESSION['login'])) ? $_SESSION['login'] : $this->store->getLogin();
70 $_SESSION['pass'] = (isset ($_SESSION['pass'])) ? $_SESSION['pass'] : $this->store->getPassword();
71 }
72
73 /**
74 * Call action (mark as fav, archive, delete, etc.)
75 */
76 public function action($action, Url $url, $id)
77 {
78 switch ($action)
79 {
80 case 'add':
81 if($parametres_url = $url->fetchContent()) {
82 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'])) {
83 Tools::logm('add link ' . $url->getUrl());
84 $last_id = $this->store->getLastId();
85 if (DOWNLOAD_PICTURES) {
86 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
87 }
88 #$msg->add('s', _('the link has been added successfully'));
89 }
90 else {
91 #$msg->add('e', _('error during insertion : the link wasn\'t added'));
92 Tools::logm('error during insertion : the link wasn\'t added');
93 }
94 }
95 else {
96 #$msg->add('e', _('error during url preparation : the link wasn\'t added'));
97 Tools::logm('error during content fetch');
98 }
99 break;
100 case 'delete':
101 if ($this->store->deleteById($id)) {
102 if (DOWNLOAD_PICTURES) {
103 remove_directory(ABS_PATH . $id);
104 }
105 #$msg->add('s', _('the link has been deleted successfully'));
106 Tools::logm('delete link #' . $id);
107 }
108 else {
109 #$msg->add('e', _('the link wasn\'t deleted'));
110 Tools::logm('error : can\'t delete link #' . $id);
111 }
112 break;
113 case 'toggle_fav' :
114 $this->store->favoriteById($id);
115 Tools::logm('mark as favorite link #' . $id);
116 break;
117 case 'toggle_archive' :
118 $this->store->archiveById($id);
119 Tools::logm('archive link #' . $id);
120 break;
121 default:
122 break;
123 }
124 }
125
126 function displayView($view, $id = 0)
127 {
128 $tpl_vars = array();
129
130 switch ($view)
131 {
132 case 'install':
133 Tools::logm('install mode');
134 break;
135 case 'import';
136 Tools::logm('import mode');
137 break;
138 case 'export':
139 $entries = $this->store->retrieveAll();
140 // $tpl->assign('export', Tools::renderJson($entries));
141 // $tpl->draw('export');
142 Tools::logm('export view');
143 break;
144 case 'config':
145 Tools::logm('config view');
146 break;
147 case 'view':
148 $entry = $this->store->retrieveOneById($id);
149 if ($entry != NULL) {
150 Tools::logm('view link #' . $id);
151 $content = $entry['content'];
152 if (function_exists('tidy_parse_string')) {
153 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
154 $tidy->cleanRepair();
155 $content = $tidy->value;
156 }
157 $tpl_vars = array(
158 'entry' => $entry,
159 'content' => $content,
160 );
161 }
162 else {
163 Tools::logm('error in view call : entry is NULL');
164 }
165 break;
166 default: # home view
167 $entries = $this->store->getEntriesByView($view);
168 $tpl_vars = array(
169 'entries' => $entries,
170 );
171 break;
172 }
173
174 return $tpl_vars;
175 }
176 }