]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Routing.class.php
just a reminder
[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(isset($_SERVER['PHP_AUTH_USER'])) {
72 if($this->wallabag->store->userExists($_SERVER['PHP_AUTH_USER'])) {
73 $this->wallabag->login($this->referer);
74 } else {
75 $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist'));
76 Tools::logm('user doesn\'t exist');
77 $tplFile = Tools::getTplFile('login');
78 $tplVars['http_auth'] = 1;
79 }
80 } elseif(isset($_SERVER['REMOTE_USER'])) {
81 if($this->wallabag->store->userExists($_SERVER['REMOTE_USER'])) {
82 $this->wallabag->login($this->referer);
83 } else {
84 $this->wallabag->messages->add('e', _('login failed: user doesn\'t exist'));
85 Tools::logm('user doesn\'t exist');
86 $tplFile = Tools::getTplFile('login');
87 $tplVars['http_auth'] = 1;
88 }
89 } else {
90 $tplFile = Tools::getTplFile('login');
91 $tplVars['http_auth'] = 0;
92 \Session::logout();
93 }
94
95 $this->file = $tplFile;
96 $this->vars = array_merge($this->defaultVars, $tplVars);
97 }
98
99 private function _launchAction()
100 {
101 if (isset($_GET['login'])) {
102 // hello to you
103 $this->wallabag->login($this->referer);
104 } elseif (isset($_GET['feed']) && isset($_GET['user_id'])) {
105 $tag_id = (isset($_GET['tag_id']) ? intval($_GET['tag_id']) : 0);
106 $limit = (isset($_GET['limit']) ? intval($_GET['limit']) : 0);
107 $this->wallabag->generateFeeds($_GET['token'], filter_var($_GET['user_id'],FILTER_SANITIZE_NUMBER_INT), $tag_id, $_GET['type'], $limit);
108 }
109
110 //allowed ONLY to logged in user
111 if (\Session::isLogged() === true)
112 {
113 if (isset($_GET['logout'])) {
114 // see you soon !
115 $this->wallabag->logout();
116 } elseif (isset($_GET['config'])) {
117 // update password
118 $this->wallabag->updatePassword($_POST['password'], $_POST['password_repeat']);
119 } elseif (isset($_GET['newuser'])) {
120 $this->wallabag->createNewUser($_POST['newusername'], $_POST['password4newuser'], $_POST['newuseremail'], true);
121 } elseif (isset($_GET['deluser'])) {
122 $this->wallabag->deleteUser($_POST['password4deletinguser']);
123 } elseif (isset($_GET['epub'])) {
124 $epub = new WallabagEpub($this->wallabag, $_GET['method'], $_GET['value']);
125 $epub->prepareData();
126 $epub->produceEpub();
127 } elseif (isset($_GET['mobi'])) {
128 $mobi = new WallabagMobi($this->wallabag, $_GET['method'], $_GET['value']);
129 $mobi->prepareData();
130 $mobi->produceMobi();
131 } elseif (isset($_GET['pdf'])) {
132 $pdf = new WallabagPDF($this->wallabag, $_GET['method'], $_GET['value']);
133 $pdf->prepareData();
134 $pdf->producePDF();
135 } elseif (isset($_GET['import'])) {
136 $import = $this->wallabag->import();
137 $tplVars = array_merge($this->vars, $import);
138 } elseif (isset($_GET['empty-cache'])) {
139 Tools::emptyCache();
140 } elseif (isset($_GET['export'])) {
141 $this->wallabag->export();
142 } elseif (isset($_GET['updatetheme'])) {
143 $this->wallabag->tpl->updateTheme($_POST['theme']);
144 } elseif (isset($_GET['updatelanguage'])) {
145 $this->wallabag->language->updateLanguage($_POST['language']);
146 } elseif (isset($_GET['uploadfile'])) {
147 $this->wallabag->uploadFile();
148 } elseif (isset($_GET['feed']) && isset($_GET['action']) && $_GET['action'] == 'generate') {
149 $this->wallabag->updateToken();
150 }
151 elseif (isset($_GET['plainurl']) && !empty($_GET['plainurl'])) {
152 $plainUrl = new Url(base64_encode($_GET['plainurl']));
153 $this->wallabag->action('add', $plainUrl);
154 }
155 }
156 }
157
158 public function _render($file, $vars)
159 {
160 echo $this->wallabag->tpl->render($file, $vars);
161 }
162 }