Skip to content

Commit 507b2f7

Browse files
authored
Merge pull request #17 from AleksanderKoko/master
Added logger, addes some stuff on login that were needed by the rest api plugin
2 parents cb0211a + 356f03e commit 507b2f7

11 files changed

+254
-65
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"minimum-stability": "dev",
1515
"require": {
1616
"php": ">=5.4.0",
17-
"symfony/dependency-injection": "~2.6",
18-
"symfony/yaml": "~2.6",
19-
"symfony/config": "~2.6",
17+
"symfony/dependency-injection": "~2.6",
18+
"symfony/yaml": "~2.6",
19+
"symfony/config": "~2.6",
2020
"phpmailer/phpmailer": "v5.2.7",
2121
"psr/log": "1.0.0"
2222
},

core/Admin.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use phpList\helper\Util;
66
use phpList\Entity\AdminEntity;
77

8+
89
class Admin
910
{
1011
public $id = 0;
@@ -277,12 +278,12 @@ function validateLogin( $plainPass, $username )
277278

278279
// If an admin was found with that username
279280
if( $result ) {
280-
281281
$adminEntity = $this->adminEntityFromArray( $result );
282282
}
283283

284284
/*
285285
* TODO: this should not happen imo, can this be removed
286+
* TODO: Aleksander Koko: I this should be removed
286287
#Password encryption verification.
287288
if(strlen($passwordDB)<$GLOBALS['hash_length']) { // Passwords are encrypted but the actual is not.
288289
#Encrypt the actual DB password before performing the validation below.
@@ -413,4 +414,17 @@ public function verifyToken($token)
413414
$result->execute(array(':token' => $token));
414415
return ($result->rowCount() > 0);
415416
}
417+
418+
public function checkIfTheTokenIsValid($token){
419+
return $this->adminModel->checkIfTheTokenIsValid($token);
420+
}
421+
422+
public function setLoginToken($id){
423+
$this->adminModel->setLoginToken($id);
424+
}
425+
426+
public function getLoginToken($id){
427+
return $this->adminModel->getLoginToken($id);
428+
}
429+
416430
}

core/Model/AdminModel.php

+68-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace phpList\Model;
33

4+
use phpList\Admin;
45
use phpList\Entity\SubscriberEntity;
56
use phpList\helper\StringClass;
67

@@ -33,12 +34,12 @@ public function getAdminByUsername( $username )
3334
{
3435
$result = $this->db->query(
3536
sprintf(
36-
'SELECT
37+
"SELECT
3738
*
3839
FROM
3940
%s
4041
WHERE
41-
loginname = "%s"'
42+
loginname = '%s'"
4243
, $this->config->getTableName( 'admin' )
4344
// FIXME: string->sqlEscape removed from here
4445
, $username
@@ -47,4 +48,69 @@ public function getAdminByUsername( $username )
4748

4849
return $result->fetch( \PDO::FETCH_ASSOC );
4950
}
51+
52+
53+
/**
54+
* Check if the given token is valid
55+
*
56+
* @param $token
57+
* @return bool
58+
*/
59+
public function checkIfTheTokenIsValid( $token )
60+
{
61+
62+
if (empty($token)) {
63+
return false;
64+
}
65+
66+
## @@@TODO for now ignore the error. This will cause a block on editing admins if the table doesn't exist.
67+
$result = $this->db->query(
68+
sprintf(
69+
"SELECT id FROM %s
70+
WHERE value = '%s'
71+
AND expires > CURRENT_TIMESTAMP",
72+
$this->config->getTableName('admintoken'),
73+
$token
74+
)
75+
);
76+
77+
if($result->fetch( \PDO::FETCH_ASSOC ) !== false)
78+
return true;
79+
80+
return false;
81+
}
82+
83+
public function setLoginToken($id){
84+
85+
$this->db->query(sprintf("delete from %s WHERE adminid = '%s'", $this->config->getTableName('admintoken'), $id));
86+
87+
$key = md5(time() . mt_rand(0, 10000));
88+
$tokenResult = $this->db->query(
89+
sprintf('insert into %s (adminid,value,entered,expires) values(%d,"%s",%d,date_add(now(),interval 1 hour))',
90+
$this->config->getTableName('admintoken'), $id, $key, time()), 1);
91+
92+
## keep the token table empty
93+
$result = $this->db->query(sprintf('delete from %s where expires < now()', $this->config->getTableName('admintoken')));
94+
95+
if(count($result->fetch( \PDO::FETCH_ASSOC )) > 0)
96+
return true;
97+
98+
return false;
99+
}
100+
101+
public function getLoginToken($id){
102+
$result = $this->db->query(sprintf("select * from %s WHERE adminid = '%s'", $this->config->getTableName('admintoken'), $id));
103+
$result = $result->fetch();
104+
if(count($result) > 0){
105+
return $result['value'];
106+
}
107+
108+
}
109+
110+
public function validateLogin($plainPass, $username){
111+
$admin = new Admin($this, $plainPass);
112+
return $admin->validateLogin($plainPass, $username);
113+
}
114+
115+
50116
}

core/Pass.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct( Config $config )
2323
* @param string $desiredAlgo Name of desiresd algo
2424
* @return string $encPass Encrypted password
2525
*/
26-
public function encrypt( $plainPass, $desiredAlgo = NULL )
26+
public function encrypt( $plainPass, $desiredAlgo = "sha256" )
2727
{
2828
// If no password was supplied, return empty
2929
// FIXME: Either log this event, or throw an exception, so client code
@@ -56,6 +56,8 @@ public function encrypt( $plainPass, $desiredAlgo = NULL )
5656
}
5757
// Hash the password using desired algo
5858
$encPass = hash( $algo, $plainPass );
59+
//var_dump($encPass);
60+
//die;
5961
} else {
6062
//. Hash the password using a fallback default
6163
$encPass = md5( $plainPass );

core/helper/Logger.php

+16-58
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,24 @@
11
<?php
22
namespace phpList\helper;
33

4+
use phpList\helper\Logger\LoggerWriterAbstractFactory;
5+
use phpList\helper\Logger\NotSuchWriterException;
46
use Psr\Log\LoggerInterface;
57
use Psr\Log\LogLevel;
68

79
class Logger implements LoggerInterface
810
{
9-
private $report;
1011

11-
public function __construct(){}
12+
private $logger;
1213

13-
private function logToDatabase($message, $page = 'unknown page')
14+
public function __construct(LoggerWriterAbstractFactory $factory)
1415
{
15-
$this->logToFile($message, $page);
16-
/* TODO: logger can't depend on database which depends on logger
17-
@$this->db->query(
18-
sprintf(
19-
'INSERT INTO %s (entered,page,entry)
20-
VALUES(CURRENT_TIMESTAMP, "%s", "%s")',
21-
$this->config->getTableName('eventlog', $page, $message)
22-
),
23-
1
24-
);*/
25-
}
26-
27-
private function logToFile($message, $page = 'unknown page')
28-
{
29-
//todo: change to config var?
30-
$logfile = './debug.log';
31-
$fp = @fopen($logfile, 'a');
32-
$line = '[' . date('d M Y, H:i:s') . '] ' . $page . ' - ' . $message . "\n";
33-
@fwrite($fp, $line);
34-
@fclose($fp);
35-
}
36-
37-
//todo: remove below functions
38-
public function addToReport($text)
39-
{
40-
$this->report .= "\n$text";
41-
}
16+
try{
17+
$this->logger = $factory->getLoggerWriter();
18+
}catch (NotSuchWriterException $e){}
4219

43-
public function getReport()
44-
{
45-
return $this->report;
4620
}
4721

48-
4922
/**
5023
* System is unusable.
5124
*
@@ -55,7 +28,7 @@ public function getReport()
5528
*/
5629
public function emergency($message, array $context = array())
5730
{
58-
$this->log(LogLevel::EMERGENCY, $context);
31+
$this->log(LogLevel::EMERGENCY, $message, $context);
5932
}
6033

6134
/**
@@ -70,7 +43,7 @@ public function emergency($message, array $context = array())
7043
*/
7144
public function alert($message, array $context = array())
7245
{
73-
$this->log(LogLevel::ALERT, $context);
46+
$this->log(LogLevel::ALERT, $message, $context);
7447
}
7548

7649
/**
@@ -84,7 +57,7 @@ public function alert($message, array $context = array())
8457
*/
8558
public function critical($message, array $context = array())
8659
{
87-
$this->log(LogLevel::CRITICAL, $context);
60+
$this->log(LogLevel::CRITICAL, $message, $context);
8861
}
8962

9063
/**
@@ -97,7 +70,7 @@ public function critical($message, array $context = array())
9770
*/
9871
public function error($message, array $context = array())
9972
{
100-
$this->log(LogLevel::ERROR, $context);
73+
$this->log(LogLevel::ERROR, $message, $context);
10174
}
10275

10376
/**
@@ -112,7 +85,7 @@ public function error($message, array $context = array())
11285
*/
11386
public function warning($message, array $context = array())
11487
{
115-
$this->log(LogLevel::WARNING, $context);
88+
$this->log(LogLevel::WARNING, $message, $context);
11689
}
11790

11891
/**
@@ -124,13 +97,7 @@ public function warning($message, array $context = array())
12497
*/
12598
public function notice($message, array $context = array())
12699
{
127-
if(isset($context['page'])){
128-
$this->logToDatabase($message, $context['page']);
129-
}else{
130-
$this->logToDatabase($message);
131-
}
132-
133-
//$this->log(LogLevel::NOTICE, $context);
100+
$this->log(LogLevel::NOTICE, $message, $context);
134101
}
135102

136103
/**
@@ -144,7 +111,7 @@ public function notice($message, array $context = array())
144111
*/
145112
public function info($message, array $context = array())
146113
{
147-
$this->log(LogLevel::INFO, $context);
114+
$this->log(LogLevel::INFO, $message, $context);
148115
}
149116

150117
/**
@@ -156,12 +123,7 @@ public function info($message, array $context = array())
156123
*/
157124
public function debug($message, array $context = array())
158125
{
159-
if(isset($context['page'])){
160-
$this->logToFile($message, $context['page']);
161-
}else{
162-
$this->logToFile($message);
163-
}
164-
//$this->log(LogLevel::DEBUG, $context);
126+
$this->log(LogLevel::DEBUG, $message, $context);
165127
}
166128

167129
/**
@@ -174,10 +136,6 @@ public function debug($message, array $context = array())
174136
*/
175137
public function log($level, $message, array $context = array())
176138
{
177-
if(isset($context['page'])){
178-
$this->logToFile($message, $context['page']);
179-
}else{
180-
$this->logToFile($message);
181-
}
139+
$this->logger->log($level, $message, $context);
182140
}
183141
}

core/helper/Logger/DatabaseWriter.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace phpList\helper\Logger;
4+
5+
6+
use phpList\Config;
7+
8+
class DatabaseWriter
9+
{
10+
11+
public function __construct(Config $config)
12+
{
13+
//
14+
}
15+
16+
public function log($level, $message, array $context = array())
17+
{
18+
@$fp = fopen($this->logfile, 'a');
19+
20+
$line = '[' . date('d M Y, H:i:s') . '] ' . $message;
21+
foreach ($context as $key => $item){
22+
$line = $line . " | {$key} - {$item} | ";
23+
}
24+
$line = $line . "\n";
25+
26+
@fwrite($fp, $line);
27+
@fclose($fp);
28+
}
29+
}

core/helper/Logger/FileWriter.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace phpList\helper\Logger;
4+
5+
6+
use phpList\Config;
7+
8+
class FileWriter implements LoggerWriter
9+
{
10+
11+
private $logfile = "/tmp/phplist.log";
12+
13+
/**
14+
* FileWriter constructor.
15+
* @param Config $config
16+
*/
17+
public function __construct(Config $config)
18+
{
19+
if($config->get("LOG_FOLDER") && $config->get("LOG_FILENAME"))
20+
$this->logfile = $config->get("LOG_FOLDER") . $config->get("LOG_FILENAME");
21+
}
22+
23+
public function log($level, $message, array $context = array())
24+
{
25+
@$fp = fopen($this->logfile, 'a');
26+
27+
$line = '[' . date('d M Y, H:i:s') . '] ' . $message;
28+
foreach ($context as $key => $item){
29+
$line = $line . " | {$key} - {$item} | ";
30+
}
31+
$line = $line . "\n";
32+
33+
@fwrite($fp, $line);
34+
@fclose($fp);
35+
}
36+
}

0 commit comments

Comments
 (0)