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