]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | class Routing | |
12 | { | |
13 | protected $wallabag; | |
26b77483 NL |
14 | protected $referer; |
15 | protected $view; | |
3602405e NL |
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 | $_SESSION['sort'] = Tools::checkVar('sort', 'id'); | |
37 | $this->url = new Url((isset ($_GET['url'])) ? $_GET['url'] : ''); | |
38 | } | |
39 | ||
40 | public function run() | |
41 | { | |
42 | # vars to _always_ send to templates | |
43 | $this->defaultVars = array( | |
44 | 'referer' => $this->referer, | |
45 | 'view' => $this->view, | |
46 | 'poche_url' => Tools::getPocheUrl(), | |
47 | 'title' => _('wallabag, a read it later open source system'), | |
48 | 'token' => \Session::getToken(), | |
49 | 'theme' => $this->wallabag->tpl->getTheme() | |
50 | ); | |
51 | ||
52 | $this->_launchAction(); | |
53 | $this->_defineTplInformation(); | |
54 | ||
55 | # because messages can be added in $poche->action(), we have to add this entry now (we can add it before) | |
56 | $this->vars = array_merge($this->vars, array('messages' => $this->wallabag->messages->display('all', FALSE))); | |
57 | ||
26b77483 | 58 | $this->_render($this->file, $this->vars); |
3602405e NL |
59 | } |
60 | ||
61 | private function _defineTplInformation() | |
62 | { | |
63 | $tplFile = array(); | |
64 | $tplVars = array(); | |
65 | ||
66 | if (\Session::isLogged()) { | |
67 | $this->wallabag->action($this->action, $this->url, $this->id); | |
68 | $tplFile = Tools::getTplFile($this->view); | |
69 | $tplVars = array_merge($this->vars, $this->wallabag->displayView($this->view, $this->id)); | |
70 | } elseif(isset($_SERVER['PHP_AUTH_USER'])) { | |
71 | if($this->wallabag->store->userExists($_SERVER['PHP_AUTH_USER'])) { | |
72 | $this->wallabag->login($this->referer); | |
73 | } else { | |
74 | $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist')); | |
75 | Tools::logm('user doesn\'t exist'); | |
76 | $tplFile = Tools::getTplFile('login'); | |
77 | $tplVars['http_auth'] = 1; | |
78 | } | |
79 | } elseif(isset($_SERVER['REMOTE_USER'])) { | |
80 | if($this->wallabag->store->userExists($_SERVER['REMOTE_USER'])) { | |
81 | $this->wallabag->login($this->referer); | |
82 | } else { | |
83 | $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist')); | |
84 | Tools::logm('user doesn\'t exist'); | |
85 | $tplFile = Tools::getTplFile('login'); | |
86 | $tplVars['http_auth'] = 1; | |
87 | } | |
88 | } else { | |
89 | $tplFile = Tools::getTplFile('login'); | |
90 | $tplVars['http_auth'] = 0; | |
91 | \Session::logout(); | |
92 | } | |
93 | ||
94 | $this->file = $tplFile; | |
95 | $this->vars = array_merge($this->defaultVars, $tplVars); | |
96 | } | |
97 | ||
98 | private function _launchAction() | |
99 | { | |
830612f5 NL |
100 | if (isset($_GET['login'])) { |
101 | // hello to you | |
102 | $this->wallabag->login($this->referer); | |
7dd8b502 MR |
103 | } elseif (isset($_GET['feed']) && isset($_GET['user_id'])) { |
104 | $tag_id = (isset($_GET['tag_id']) ? intval($_GET['tag_id']) : 0); | |
7fe8a9ad VM |
105 | $limit = (isset($_GET['limit']) ? intval($_GET['limit']) : 0); |
106 | $this->wallabag->generateFeeds($_GET['token'], filter_var($_GET['user_id'],FILTER_SANITIZE_NUMBER_INT), $tag_id, $_GET['type'], $limit); | |
7dd8b502 MR |
107 | } |
108 | ||
109 | //allowed ONLY to logged in user | |
830612f5 | 110 | if (\Session::isLogged() === true) |
7dd8b502 MR |
111 | { |
112 | if (isset($_GET['logout'])) { | |
113 | // see you soon ! | |
114 | $this->wallabag->logout(); | |
115 | } elseif (isset($_GET['config'])) { | |
116 | // update password | |
117 | $this->wallabag->updatePassword($_POST['password'], $_POST['password_repeat']); | |
118 | } elseif (isset($_GET['newuser'])) { | |
119 | $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser']); | |
120 | } elseif (isset($_GET['deluser'])) { | |
121 | $this->wallabag->deleteUser($_POST['password4deletinguser']); | |
122 | } elseif (isset($_GET['epub'])) { | |
606bea72 | 123 | $epub = new WallabagEpub($this->wallabag, $_GET['method'], $_GET['value']); |
6fc2c29d | 124 | $epub->prepareData(); |
125 | $epub->produceEpub(); | |
126 | } elseif (isset($_GET['mobi'])) { | |
127 | $mobi = new WallabagMobi($this->wallabag, $_GET['method'], $_GET['value']); | |
128 | $mobi->prepareData(); | |
129 | $mobi->produceMobi(); | |
6fc2c29d | 130 | } elseif (isset($_GET['pdf'])) { |
131 | $pdf = new WallabagPDF($this->wallabag, $_GET['method'], $_GET['value']); | |
132 | $pdf->prepareData(); | |
133 | $pdf->producePDF(); | |
7dd8b502 MR |
134 | } elseif (isset($_GET['import'])) { |
135 | $import = $this->wallabag->import(); | |
136 | $tplVars = array_merge($this->vars, $import); | |
7dd8b502 MR |
137 | } elseif (isset($_GET['empty-cache'])) { |
138 | Tools::emptyCache(); | |
139 | } elseif (isset($_GET['export'])) { | |
140 | $this->wallabag->export(); | |
141 | } elseif (isset($_GET['updatetheme'])) { | |
142 | $this->wallabag->tpl->updateTheme($_POST['theme']); | |
143 | } elseif (isset($_GET['updatelanguage'])) { | |
144 | $this->wallabag->language->updateLanguage($_POST['language']); | |
145 | } elseif (isset($_GET['uploadfile'])) { | |
146 | $this->wallabag->uploadFile(); | |
147 | } elseif (isset($_GET['feed']) && isset($_GET['action']) && $_GET['action'] == 'generate') { | |
2f26729c | 148 | $this->wallabag->updateToken(); |
3602405e | 149 | } |
7dd8b502 MR |
150 | elseif (isset($_GET['plainurl']) && !empty($_GET['plainurl'])) { |
151 | $plainUrl = new Url(base64_encode($_GET['plainurl'])); | |
152 | $this->wallabag->action('add', $plainUrl); | |
3602405e NL |
153 | } |
154 | } | |
3602405e NL |
155 | } |
156 | ||
26b77483 | 157 | public function _render($file, $vars) |
3602405e NL |
158 | { |
159 | echo $this->wallabag->tpl->render($file, $vars); | |
160 | } | |
dc69d3e8 | 161 | } |