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()
105 if (defined('GALETTE_X_FORWARDED_FOR_INDEX')
106 && isset($_SERVER['HTTP_X_FORWARDED_FOR'])
108 $split_xff = preg_split('/,\s*/', $_SERVER['HTTP_X_FORWARDED_FOR']);
109 return $split_xff[count($split_xff) - GALETTE_X_FORWARDED_FOR_INDEX
];
111 return $_SERVER['REMOTE_ADDR'];
117 * @param string $action the action to log
118 * @param string $argument the argument
119 * @param string $query the query (if relevant)
121 * @return bool true if entry was successfully added, false otherwise
123 public function add($action, $argument = '', $query = '')
127 if ($preferences->pref_log
== Preferences
::LOG_DISABLED
) {
133 if (PHP_SAPI
=== 'cli') {
136 $ip = self
::findUserIpAddress();
141 'date_log' => date('Y-m-d H:i:s'),
143 'adh_log' => $this->login
->login
,
144 'action_log' => $action,
145 'text_log' => $argument,
149 $insert = $this->zdb
->insert($this->getTableName());
150 $insert->values($values);
151 $this->zdb
->execute($insert);
152 } catch (\Exception
$e) {
154 "An error occurred trying to add log entry. " . $e->getMessage(),
168 public function clean()
171 $result = $this->zdb
->db
->query(
172 'TRUNCATE TABLE ' . $this->getTableName(true),
173 Adapter
::QUERY_MODE_EXECUTE
178 'An error occurred cleaning history. ',
181 $this->add('Error flushing logs');
184 $this->add('Logs flushed');
185 $this->filters
= new HistoryList();
187 } catch (\Exception
$e) {
189 'Unable to flush logs. | ' . $e->getMessage(),
197 * Get the entire history list
201 public function getHistory()
204 $select = $this->zdb
->select($this->getTableName());
205 $this->buildWhereClause($select);
206 $select->order($this->buildOrderClause());
207 if ($this->with_lists
=== true) {
208 $this->buildLists($select);
210 $this->proceedCount($select);
211 //add limits to retrieve only relavant rows
212 $this->filters
->setLimits($select);
213 $results = $this->zdb
->execute($select);
216 foreach ($results as $result) {
217 $entries[] = $result;
221 } catch (\Exception
$e) {
223 'Unable to get history. | ' . $e->getMessage(),
231 * Builds users and actions lists
233 * @param Select $select Original select
237 private function buildLists($select)
240 $usersSelect = clone $select;
241 $usersSelect->reset($usersSelect::COLUMNS
);
242 $usersSelect->reset($usersSelect::ORDER
);
243 $usersSelect->quantifier('DISTINCT')->columns(['adh_log']);
244 $usersSelect->order(['adh_log ASC']);
246 $results = $this->zdb
->execute($usersSelect);
249 foreach ($results as $result) {
250 $this->users
[] = $result->adh_log
;
252 } catch (\Exception
$e) {
254 'Cannot list members from history! | ' . $e->getMessage(),
260 $actionsSelect = clone $select;
261 $actionsSelect->reset($actionsSelect::COLUMNS
);
262 $actionsSelect->reset($actionsSelect::ORDER
);
263 $actionsSelect->quantifier('DISTINCT')->columns(['action_log']);
264 $actionsSelect->order(['action_log ASC']);
266 $results = $this->zdb
->execute($actionsSelect);
269 foreach ($results as $result) {
270 $this->actions
[] = $result->action_log
;
272 } catch (\Exception
$e) {
274 'Cannot list actions from history! | ' . $e->getMessage(),
281 * Builds the order clause
283 * @return string SQL ORDER clause
285 protected function buildOrderClause()
289 switch ($this->filters
->orderby
) {
290 case HistoryList
::ORDERBY_DATE
:
291 $order[] = 'date_log ' . $this->filters
->ordered
;
293 case HistoryList
::ORDERBY_IP
:
294 $order[] = 'ip_log ' . $this->filters
->ordered
;
296 case HistoryList
::ORDERBY_USER
:
297 $order[] = 'adh_log ' . $this->filters
->ordered
;
299 case HistoryList
::ORDERBY_ACTION
:
300 $order[] = 'action_log ' . $this->filters
->ordered
;
308 * Builds where clause, for filtering on simple list mode
310 * @param Select $select Original select
312 * @return string SQL WHERE clause
314 private function buildWhereClause($select)
317 if ($this->filters
->start_date_filter
!= null) {
318 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
319 $d->setTime(0, 0, 0);
320 $select->where
->greaterThanOrEqualTo(
322 $d->format('Y-m-d H:i:s')
326 if ($this->filters
->end_date_filter
!= null) {
327 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
328 $d->setTime(23, 59, 59);
329 $select->where
->lessThanOrEqualTo(
331 $d->format('Y-m-d H:i:s')
335 if ($this->filters
->user_filter
!= null && $this->filters
->user_filter
!= '0') {
336 $select->where
->equalTo(
338 $this->filters
->user_filter
342 if ($this->filters
->action_filter
!= null && $this->filters
->action_filter
!= '0') {
343 $select->where
->equalTo(
345 $this->filters
->action_filter
348 } catch (\Exception
$e) {
350 __METHOD__
. ' | ' . $e->getMessage(),
357 * Count history entries from the query
359 * @param Select $select Original select
363 private function proceedCount($select)
366 $countSelect = clone $select;
367 $countSelect->reset($countSelect::COLUMNS
);
368 $countSelect->reset($countSelect::JOINS
);
369 $countSelect->reset($countSelect::ORDER
);
370 $countSelect->columns(
372 $this->getPk() => new Expression('COUNT(' . $this->getPk() . ')')
376 $results = $this->zdb
->execute($countSelect);
377 $result = $results->current();
380 $this->count
= $result->$k;
381 if ($this->count
> 0) {
382 $this->filters
->setCounter($this->count
);
384 } catch (\Exception
$e) {
386 'Cannot count history | ' . $e->getMessage(),
394 * Global getter method
396 * @param string $name name of the property we want to retrive
398 * @return false|object the called property
400 public function __get($name)
403 '[History] Getting property `' . $name . '`',
407 $forbidden = array();
408 if (!in_array($name, $forbidden)) {
411 //return formatted datemime
413 $d = new \
DateTime($this->$name);
414 return $d->format(__("Y-m-d H:i:s"));
415 } catch (\Exception
$e) {
416 //oops, we've got a bad date :/
418 'Bad date (' . $this->$name . ') | ' .
431 '[History] Unable to get proprety `' .$name . '`',
438 * Global setter method
440 * @param string $name name of the property we want to assign a value to
441 * @param object $value a relevant value for the property
445 public function __set($name, $value)
448 '[History] Setting property `' . $name . '`',
452 $forbidden = array();
453 if (!in_array($name, $forbidden)) {
456 $this->$name = $value;
461 '[History] Unable to set proprety `' .$name . '`',
470 * @param boolean $prefixed Whether table name should be prefixed
474 protected function getTableName($prefixed = false)
476 if ($prefixed === true) {
477 return PREFIX_DB
. self
::TABLE
;
488 protected function getPk()
496 * @param HistoryList $filters Filters
500 public function setFilters(HistoryList
$filters)
502 $this->filters
= $filters;
507 * Get count for current query
511 public function getCount()
521 public function getUsersList()
531 public function getActionsList()
533 return $this->actions
;