]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Routing.class.php
autoclose was not declared
[github/wallabag/wallabag.git] / inc / poche / Routing.class.php
1 <?php
2 /**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
7 * @copyright 2013
8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */
10
11 class Routing
12 {
13 protected $wallabag;
14 protected $referer;
15 protected $view;
16 protected $action;
17 protected $id;
18 protected $autoclose;
19 protected $url;
20 protected $file;
21 protected $defaultVars = array();
22 protected $vars = array();
23
24 public function __construct(Poche $wallabag)
25 {
26 $this->wallabag = $wallabag;
27 $this->_init();
28 }
29
30 private function _init()
31 {
32 # Parse GET & REFERER vars
33 $this->referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
34 $this->view = Tools::checkVar('view', 'home');
35 $this->action = Tools::checkVar('action');
36 $this->id = Tools::checkVar('id');
37 $this->autoclose = Tools::checkVar('autoclose',FALSE);
38 $_SESSION['sort'] = Tools::checkVar('sort', 'id');
39 $this->url = new Url((isset ($_GET['url'])) ? $_GET['url'] : '');
40 }
41
42 public function run()
43 {
44 # vars to _always_ send to templates
45 $this->defaultVars = array(
46 'referer' => $this->referer,
47 'view' => $this->view,
48 'poche_url' => Tools::getPocheUrl(),
49 'title' => _('wallabag, a read it later open source system'),
50 'token' => \Session::getToken(),
51 'theme' => $this->wallabag->tpl->getTheme()
52 );
53
54 $this->_launchAction();
55 $this->_defineTplInformation();
56
57 # because messages can be added in $poche->action(), we have to add this entry now (we can add it before)
58 $this->vars = array_merge($this->vars, array('messages' => $this->wallabag->messages->display('all', FALSE)));
59
60 $this->_render($this->file, $this->vars);
61 }
62
63 private function _defineTplInformation()
64 {
65 $tplFile = array();
66 $tplVars = array();
67
68 if (\Session::isLogged()) {
69 $this->wallabag->action($this->action, $this->url, $this->id, FALSE, $this->autoclose);
70 $tplFile = Tools::getTplFile($this->view);
71 $tplVars = array_merge($this->vars, $this->wallabag->displayView($this->view, $this->id));
72 } elseif(ALLOW_REGISTER && isset($_GET['registerform'])) {
73 Tools::logm('register');
74 $tplFile = Tools::getTplFile('register');
75 } elseif (ALLOW_REGISTER && isset($_GET['register'])){
76 $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser'], $_POST['newuseremail']);
77 Tools::redirect();
78 } elseif(isset($_SERVER['PHP_AUTH_USER'])) {
79 if($this->wallabag->store->userExists($_SERVER['PHP_AUTH_USER'])) {
80 $this->wallabag->login($this->referer);
81 } else {
82 $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist'));
83 Tools::logm('user doesn\'t exist');
84 $tplFile = Tools::getTplFile('login');
85 $tplVars['http_auth'] = 1;
86 }
87 } elseif(isset($_SERVER['REMOTE_USER'])) {
88 if($this->wallabag->store->userExists($_SERVER['REMOTE_USER'])) {
89 $this->wallabag->login($this->referer);
90 } else {
91 $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist'));
92 Tools::logm('user doesn\'t exist');
93 $tplFile = Tools::getTplFile('login');
94 $tplVars['http_auth'] = 1;
95 }
96 } else {
97 $tplFile = Tools::getTplFile('login');
98 $tplVars['http_auth'] = 0;
99 \Session::logout();
100 }
101
102 $this->file = $tplFile;
103 $this->vars = array_merge($this->defaultVars, $tplVars);
104 }
105
106 private function _launchAction()
107 {
108 if (isset($_GET['login'])) {
109 // hello to you
110 $this->wallabag->login($this->referer);
111 } elseif (isset($_GET['feed']) && isset($_GET['user_id'])) {
112 $tag_id = (isset($_GET['tag_id']) ? intval($_GET['tag_id']) : 0);
113 $limit = (isset($_GET['limit']) ? intval($_GET['limit']) : 0);
114 $this->wallabag->generateFeeds($_GET['token'], filter_var($_GET['user_id'],FILTER_SANITIZE_NUMBER_INT), $tag_id, $_GET['type'], $limit);
115 }
116
117 //allowed ONLY to logged in user
118 if (\Session::isLogged() === true)
119 {
120 if (isset($_GET['logout'])) {
121 // see you soon !
122 $this->wallabag->logout();
123 } elseif (isset($_GET['config'])) {
124 // update password
125 $this->wallabag->updatePassword($_POST['password'], $_POST['password_repeat']);
126 } elseif (isset($_GET['newuser'])) {
127 $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser'], $_POST['newuseremail'], true);
128 } elseif (isset($_GET['deluser'])) {
129 $this->wallabag->deleteUser($_POST['password4deletinguser']);
130 } elseif (isset($_GET['epub'])) {
131 $epub = new WallabagEpub($this->wallabag, $_GET['method'], $_GET['value']);
132 $epub->prepareData();
133 $epub->produceEpub();
134 } elseif (isset($_GET['mobi'])) {
135 $mobi = new WallabagMobi($this->wallabag, $_GET['method'], $_GET['value']);
136 $mobi->prepareData();
137 $mobi->produceMobi();
138 } elseif (isset($_GET['pdf'])) {
139 $pdf = new WallabagPDF($this->wallabag, $_GET['method'], $_GET['value']);
140 $pdf->prepareData();
141 $pdf->producePDF();
142 } elseif (isset($_GET['import'])) {
143 $import = $this->wallabag->import();
144 $tplVars = array_merge($this->vars, $import);
145 } elseif (isset($_GET['empty-cache'])) {
146 Tools::emptyCache();
147 } elseif (isset($_GET['export'])) {
148 $this->wallabag->export();
149 } elseif (isset($_GET['updatetheme'])) {
150 $this->wallabag->tpl->updateTheme($_POST['theme']);
151 } elseif (isset($_GET['updatelanguage'])) {
152 $this->wallabag->language->updateLanguage($_POST['language']);
153 } elseif (isset($_GET['uploadfile'])) {
154 $this->wallabag->uploadFile();
155 } elseif (isset($_GET['feed']) && isset($_GET['action']) && $_GET['action'] == 'generate') {
156 $this->wallabag->updateToken();
157 }
158 elseif (isset($_GET['plainurl']) && !empty($_GET['plainurl'])) {
159 $plainUrl = new Url(base64_encode($_GET['plainurl']));
160 $this->wallabag->action('add', $plainUrl);
161 }
162 }
163 }
164
165 public function _render($file, $vars)
166 {
167 echo $this->wallabag->tpl->render($file, $vars);
168 }
169 }