]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Routing.class.php
Merge branch 'autoclose-postmessage' into dev
[github/wallabag/wallabag.git] / inc / poche / Routing.class.php
CommitLineData
3602405e
NL
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
11class Routing
12{
13 protected $wallabag;
26b77483
NL
14 protected $referer;
15 protected $view;
3602405e
NL
16 protected $action;
17 protected $id;
e9540cad 18 protected $autoclose;
3602405e
NL
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');
1c911789 37 $this->autoclose = Tools::checkVar('autoclose',FALSE);
3602405e
NL
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
26b77483 60 $this->_render($this->file, $this->vars);
3602405e
NL
61 }
62
63 private function _defineTplInformation()
64 {
65 $tplFile = array();
66 $tplVars = array();
67
68 if (\Session::isLogged()) {
1c911789 69 $this->wallabag->action($this->action, $this->url, $this->id, FALSE, $this->autoclose);
3602405e
NL
70 $tplFile = Tools::getTplFile($this->view);
71 $tplVars = array_merge($this->vars, $this->wallabag->displayView($this->view, $this->id));
121691e9
TC
72 } elseif(ALLOW_REGISTER && isset($_GET['registerform'])) {
73 Tools::logm('register');
74 $tplFile = Tools::getTplFile('register');
75 } elseif (ALLOW_REGISTER && isset($_GET['register'])){
f2321633 76 $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser'], $_POST['newuseremail']);
121691e9 77 Tools::redirect();
3602405e
NL
78 } elseif(isset($_SERVER['REMOTE_USER'])) {
79 if($this->wallabag->store->userExists($_SERVER['REMOTE_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 } else {
88 $tplFile = Tools::getTplFile('login');
89 $tplVars['http_auth'] = 0;
90 \Session::logout();
91 }
92
93 $this->file = $tplFile;
94 $this->vars = array_merge($this->defaultVars, $tplVars);
95 }
96
97 private function _launchAction()
98 {
830612f5
NL
99 if (isset($_GET['login'])) {
100 // hello to you
101 $this->wallabag->login($this->referer);
7dd8b502
MR
102 } elseif (isset($_GET['feed']) && isset($_GET['user_id'])) {
103 $tag_id = (isset($_GET['tag_id']) ? intval($_GET['tag_id']) : 0);
7fe8a9ad
VM
104 $limit = (isset($_GET['limit']) ? intval($_GET['limit']) : 0);
105 $this->wallabag->generateFeeds($_GET['token'], filter_var($_GET['user_id'],FILTER_SANITIZE_NUMBER_INT), $tag_id, $_GET['type'], $limit);
056a3c6c 106 }
7dd8b502
MR
107
108 //allowed ONLY to logged in user
830612f5 109 if (\Session::isLogged() === true)
7dd8b502
MR
110 {
111 if (isset($_GET['logout'])) {
112 // see you soon !
113 $this->wallabag->logout();
114 } elseif (isset($_GET['config'])) {
115 // update password
116 $this->wallabag->updatePassword($_POST['password'], $_POST['password_repeat']);
117 } elseif (isset($_GET['newuser'])) {
3e1daa4c 118 $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser'], $_POST['newuseremail'], true);
7dd8b502
MR
119 } elseif (isset($_GET['deluser'])) {
120 $this->wallabag->deleteUser($_POST['password4deletinguser']);
121 } elseif (isset($_GET['epub'])) {
606bea72 122 $epub = new WallabagEpub($this->wallabag, $_GET['method'], $_GET['value']);
6fc2c29d 123 $epub->prepareData();
124 $epub->produceEpub();
125 } elseif (isset($_GET['mobi'])) {
126 $mobi = new WallabagMobi($this->wallabag, $_GET['method'], $_GET['value']);
127 $mobi->prepareData();
128 $mobi->produceMobi();
6fc2c29d 129 } elseif (isset($_GET['pdf'])) {
130 $pdf = new WallabagPDF($this->wallabag, $_GET['method'], $_GET['value']);
131 $pdf->prepareData();
132 $pdf->producePDF();
7dd8b502
MR
133 } elseif (isset($_GET['import'])) {
134 $import = $this->wallabag->import();
2c839402 135 $this->vars = array_merge($this->vars, $import);
7dd8b502
MR
136 } elseif (isset($_GET['empty-cache'])) {
137 Tools::emptyCache();
138 } elseif (isset($_GET['export'])) {
139 $this->wallabag->export();
140 } elseif (isset($_GET['updatetheme'])) {
141 $this->wallabag->tpl->updateTheme($_POST['theme']);
142 } elseif (isset($_GET['updatelanguage'])) {
143 $this->wallabag->language->updateLanguage($_POST['language']);
144 } elseif (isset($_GET['uploadfile'])) {
145 $this->wallabag->uploadFile();
146 } elseif (isset($_GET['feed']) && isset($_GET['action']) && $_GET['action'] == 'generate') {
2f26729c 147 $this->wallabag->updateToken();
3602405e 148 }
7dd8b502
MR
149 elseif (isset($_GET['plainurl']) && !empty($_GET['plainurl'])) {
150 $plainUrl = new Url(base64_encode($_GET['plainurl']));
151 $this->wallabag->action('add', $plainUrl);
3602405e
NL
152 }
153 }
3602405e
NL
154 }
155
26b77483 156 public function _render($file, $vars)
3602405e
NL
157 {
158 echo $this->wallabag->tpl->render($file, $vars);
159 }
dc69d3e8 160}