3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2009-02-09
38 namespace Galette\Core
;
41 use Galette\Filters\HistoryList
;
42 use Laminas\Db\Sql\Expression
;
43 use Laminas\Db\Adapter\Adapter
;
44 use Galette\Core\Preferences
;
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2009-2014 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.7dev - 2009-02-09
72 protected $with_lists = true;
77 * @param Db $zdb Database
78 * @param Login $login Login
79 * @param HistoryList $filters Filtering
81 public function __construct(Db
$zdb, Login
$login, $filters = null)
84 $this->login
= $login;
86 if ($filters === null) {
87 $this->filters
= new HistoryList();
89 $this->filters
= $filters;
94 * Helper function to find the user IP address
96 * This function uses the client address or the appropriate part of
97 * X-Forwarded-For, if present and the configuration specifies it.
98 * (blindly trusting X-Forwarded-For would make the IP address logging
99 * very easy to deveive.
103 public static function findUserIPAddress()
106 defined('GALETTE_X_FORWARDED_FOR_INDEX')
107 && isset($_SERVER['HTTP_X_FORWARDED_FOR'])
109 $split_xff = preg_split('/,\s*/', $_SERVER['HTTP_X_FORWARDED_FOR']);
110 return $split_xff[count($split_xff) - GALETTE_X_FORWARDED_FOR_INDEX
];
112 return $_SERVER['REMOTE_ADDR'];
118 * @param string $action the action to log
119 * @param string $argument the argument
120 * @param string $query the query (if relevant)
122 * @return bool true if entry was successfully added, false otherwise
124 public function add($action, $argument = '', $query = '')
128 if ($preferences->pref_log
== Preferences
::LOG_DISABLED
) {
134 if (PHP_SAPI
=== 'cli') {
137 $ip = self
::findUserIpAddress();
142 'date_log' => date('Y-m-d H:i:s'),
144 'adh_log' => $this->login
->login
,
145 'action_log' => $action,
146 'text_log' => $argument,
150 $insert = $this->zdb
->insert($this->getTableName());
151 $insert->values($values);
152 $this->zdb
->execute($insert);
153 } catch (\Exception
$e) {
155 "An error occurred trying to add log entry. " . $e->getMessage(),
169 public function clean()
172 $result = $this->zdb
->db
->query(
173 'TRUNCATE TABLE ' . $this->getTableName(true),
174 Adapter
::QUERY_MODE_EXECUTE
179 'An error occurred cleaning history. ',
182 $this->add('Error flushing logs');
185 $this->add('Logs flushed');
186 $this->filters
= new HistoryList();
188 } catch (\Exception
$e) {
190 'Unable to flush logs. | ' . $e->getMessage(),
198 * Get the entire history list
202 public function getHistory()
205 $select = $this->zdb
->select($this->getTableName());
206 $this->buildWhereClause($select);
207 $select->order($this->buildOrderClause());
208 if ($this->with_lists
=== true) {
209 $this->buildLists($select);
211 $this->proceedCount($select);
212 //add limits to retrieve only relavant rows
213 $this->filters
->setLimits($select);
214 $results = $this->zdb
->execute($select);
217 foreach ($results as $result) {
218 $entries[] = $result;
222 } catch (\Exception
$e) {
224 'Unable to get history. | ' . $e->getMessage(),
232 * Builds users and actions lists
234 * @param Select $select Original select
238 private function buildLists($select)
241 $usersSelect = clone $select;
242 $usersSelect->reset($usersSelect::COLUMNS
);
243 $usersSelect->reset($usersSelect::ORDER
);
244 $usersSelect->quantifier('DISTINCT')->columns(['adh_log']);
245 $usersSelect->order(['adh_log ASC']);
247 $results = $this->zdb
->execute($usersSelect);
250 foreach ($results as $result) {
251 $this->users
[] = $result->adh_log
;
253 } catch (\Exception
$e) {
255 'Cannot list members from history! | ' . $e->getMessage(),
261 $actionsSelect = clone $select;
262 $actionsSelect->reset($actionsSelect::COLUMNS
);
263 $actionsSelect->reset($actionsSelect::ORDER
);
264 $actionsSelect->quantifier('DISTINCT')->columns(['action_log']);
265 $actionsSelect->order(['action_log ASC']);
267 $results = $this->zdb
->execute($actionsSelect);
270 foreach ($results as $result) {
271 $this->actions
[] = $result->action_log
;
273 } catch (\Exception
$e) {
275 'Cannot list actions from history! | ' . $e->getMessage(),
282 * Builds the order clause
284 * @return string SQL ORDER clause
286 protected function buildOrderClause()
290 switch ($this->filters
->orderby
) {
291 case HistoryList
::ORDERBY_DATE
:
292 $order[] = 'date_log ' . $this->filters
->ordered
;
294 case HistoryList
::ORDERBY_IP
:
295 $order[] = 'ip_log ' . $this->filters
->ordered
;
297 case HistoryList
::ORDERBY_USER
:
298 $order[] = 'adh_log ' . $this->filters
->ordered
;
300 case HistoryList
::ORDERBY_ACTION
:
301 $order[] = 'action_log ' . $this->filters
->ordered
;
309 * Builds where clause, for filtering on simple list mode
311 * @param Select $select Original select
313 * @return string SQL WHERE clause
315 private function buildWhereClause($select)
318 if ($this->filters
->start_date_filter
!= null) {
319 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
320 $d->setTime(0, 0, 0);
321 $select->where
->greaterThanOrEqualTo(
323 $d->format('Y-m-d H:i:s')
327 if ($this->filters
->end_date_filter
!= null) {
328 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
329 $d->setTime(23, 59, 59);
330 $select->where
->lessThanOrEqualTo(
332 $d->format('Y-m-d H:i:s')
336 if ($this->filters
->user_filter
!= null && $this->filters
->user_filter
!= '0') {
337 $select->where
->equalTo(
339 $this->filters
->user_filter
343 if ($this->filters
->action_filter
!= null && $this->filters
->action_filter
!= '0') {
344 $select->where
->equalTo(
346 $this->filters
->action_filter
349 } catch (\Exception
$e) {
351 __METHOD__
. ' | ' . $e->getMessage(),
358 * Count history entries from the query
360 * @param Select $select Original select
364 private function proceedCount($select)
367 $countSelect = clone $select;
368 $countSelect->reset($countSelect::COLUMNS
);
369 $countSelect->reset($countSelect::JOINS
);
370 $countSelect->reset($countSelect::ORDER
);
371 $countSelect->columns(
373 $this->getPk() => new Expression('COUNT(' . $this->getPk() . ')')
377 $results = $this->zdb
->execute($countSelect);
378 $result = $results->current();
381 $this->count
= $result->$k;
382 if ($this->count
> 0) {
383 $this->filters
->setCounter($this->count
);
385 } catch (\Exception
$e) {
387 'Cannot count history | ' . $e->getMessage(),
395 * Global getter method
397 * @param string $name name of the property we want to retrive
399 * @return false|object the called property
401 public function __get($name)
404 '[History] Getting property `' . $name . '`',
408 $forbidden = array();
409 if (!in_array($name, $forbidden)) {
412 //return formatted datemime
414 $d = new \
DateTime($this->$name);
415 return $d->format(__("Y-m-d H:i:s"));
416 } catch (\Exception
$e) {
417 //oops, we've got a bad date :/
419 'Bad date (' . $this->$name . ') | ' .
432 '[History] Unable to get proprety `' . $name . '`',
439 * Global setter method
441 * @param string $name name of the property we want to assign a value to
442 * @param object $value a relevant value for the property
446 public function __set($name, $value)
449 '[History] Setting property `' . $name . '`',
453 $forbidden = array();
454 if (!in_array($name, $forbidden)) {
457 $this->$name = $value;
462 '[History] Unable to set proprety `' . $name . '`',
471 * @param boolean $prefixed Whether table name should be prefixed
475 protected function getTableName($prefixed = false)
477 if ($prefixed === true) {
478 return PREFIX_DB
. self
::TABLE
;
489 protected function getPk()
497 * @param HistoryList $filters Filters
501 public function setFilters(HistoryList
$filters)
503 $this->filters
= $filters;
508 * Get count for current query
512 public function getCount()
522 public function getUsersList()
532 public function getActionsList()
534 return $this->actions
;