aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/indexController.php
blob: 594d379fb85f1012482eabccd4f88be7b9a45247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php

class indexController extends ActionController {
	private $get = false;
	private $nb_not_read = 0;
	private $mode = 'all';

	public function indexAction () {
		$output = Request::param ('output');

		if ($output == 'rss') {
			$this->view->_useLayout (false);
		} else {
			View::appendScript (Url::display ('/scripts/shortcut.js'));
			View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'main')));
			View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'actualize')));
			View::appendScript (Url::display ('/scripts/endless_mode.js'));

			if(!$output) {
				Request::_param ('output', $this->view->conf->viewMode());
			}
		}

		$entryDAO = new EntryDAO ();
		$feedDAO = new FeedDAO ();
		$catDAO = new CategoryDAO ();

		$this->view->cat_aside = $catDAO->listCategories ();
		$this->view->nb_favorites = $entryDAO->countFavorites ();
		$this->view->nb_total = $entryDAO->count ();

		$this->view->get_c = '';
		$this->view->get_f = '';

		$type = $this->getType ();
		$error = $this->checkAndProcessType ($type);
		if (!$error) {
			// On récupère les différents éléments de filtrage
			$this->view->state = $state = Request::param ('state', $this->view->conf->defaultView ());
			$filter = Request::param ('search', '');
			$this->view->order = $order = Request::param ('order', $this->view->conf->sortOrder ());
			$nb = Request::param ('nb', $this->view->conf->postsPerPage ());
			$first = Request::param ('next', '');

			try {
				// EntriesGetter permet de déporter la complexité du filtrage
				$getter = new EntriesGetter ($type, $state, $filter, $order, $nb, $first);
				$getter->execute ();
				$entries = $getter->getPaginator ();

				// Si on a récupéré aucun article "non lus"
				// on essaye de récupérer tous les articles
				if ($state == 'not_read' && $entries->isEmpty ()) {
					$this->view->state = 'all';
					$getter->_state ('all');
					$getter->execute ();
					$entries = $getter->getPaginator ();
				}

				$this->view->entryPaginator = $entries;
			} catch(EntriesGetterException $e) {
				Log::record ($e->getMessage (), Log::NOTICE);
				Error::error (
					404,
					array ('error' => array (Translate::t ('page_not_found')))
				);
			}
		} else {
			Error::error (
				404,
				array ('error' => array (Translate::t ('page_not_found')))
			);
		}
	}

	/*
	 * Détermine le type d'article à récupérer :
	 * "tous", "favoris", "public", "catégorie" ou "flux"
	 */
	private function getType () {
		$get = Request::param ('get', 'all');
		$typeGet = $get[0];
		$id = substr ($get, 2);

		$type = null;
		if ($get == 'all' || $get == 'favoris' || $get == 'public') {
			$type = array (
				'type' => $get,
				'id' => $get
			);
		} elseif ($typeGet == 'f' || $typeGet == 'c') {
			$type = array (
				'type' => $typeGet,
				'id' => $id
			);
		}

		return $type;
	}
	/*
	 * Vérifie que la catégorie / flux sélectionné existe
	 * + Initialise correctement les variables de vue get_c et get_f
	 * + Initialise le titre
	 */
	private function checkAndProcessType ($type) {
		if ($type['type'] == 'all') {
			View::prependTitle (Translate::t ('your_rss_feeds') . ' - ');
			$this->view->get_c = $type['type'];
			return false;
		} elseif ($type['type'] == 'favoris') {
			View::prependTitle (Translate::t ('your_favorites') . ' - ');
			$this->view->get_c = $type['type'];
			return false;
		} elseif ($type['type'] == 'public') {
			View::prependTitle (Translate::t ('public') . ' - ');
			$this->view->get_c = $type['type'];
			return false;
		} elseif ($type['type'] == 'c') {
			$catDAO = new CategoryDAO ();
			$cat = $catDAO->searchById ($type['id']);
			if ($cat) {
				View::prependTitle ($cat->name () . ' - ');
				$this->view->get_c = $type['id'];
				return false;
			} else {
				return true;
			}
		} elseif ($type['type'] == 'f') {
			$feedDAO = new FeedDAO ();
			$feed = $feedDAO->searchById ($type['id']);
			if ($feed) {
				View::prependTitle ($feed->name () . ' - ');
				$this->view->get_f = $type['id'];
				$this->view->get_c = $feed->category ();
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	public function aboutAction () {
		View::prependTitle (Translate::t ('about') . ' - ');
	}

	public function logsAction () {
		if (login_is_conf ($this->view->conf) && !is_logged ()) {
			Error::error (
				403,
				array ('error' => array (Translate::t ('access_denied')))
			);
		}

		View::prependTitle (Translate::t ('logs') . ' - ');

		$logs = array();
		try {
			$logDAO = new LogDAO ();
			$logs = $logDAO->lister ();
			$logs = array_reverse ($logs);
		} catch(FileNotExistException $e) {

		}

		//gestion pagination
		$page = Request::param ('page', 1);
		$this->view->logsPaginator = new Paginator ($logs);
		$this->view->logsPaginator->_nbItemsPerPage (50);
		$this->view->logsPaginator->_currentPage ($page);
	}

	public function loginAction () {
		$this->view->_useLayout (false);

		$url = 'https://verifier.login.persona.org/verify';
		$assert = Request::param ('assertion');
		$params = 'assertion=' . $assert . '&audience=' .
			  urlencode (Url::display () . ':80');
		$ch = curl_init ();
		$options = array (
			CURLOPT_URL => $url,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_POST => 2,
			CURLOPT_POSTFIELDS => $params
		);
		curl_setopt_array ($ch, $options);
		$result = curl_exec ($ch);
		curl_close ($ch);

		$res = json_decode ($result, true);
		if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
			Session::_param ('mail', $res['email']);
		} else {
			$res = array ();
			$res['status'] = 'failure';
			$res['reason'] = Translate::t ('invalid_login');
		}

		$this->view->res = json_encode ($res);
	}

	public function logoutAction () {
		$this->view->_useLayout (false);
		Session::_param ('mail');
	}
}