Skip to content

Commit 356f03e

Browse files
fixed some stuff
1 parent 4d8deb7 commit 356f03e

File tree

11 files changed

+184
-62
lines changed

11 files changed

+184
-62
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,5 +422,9 @@ public function checkIfTheTokenIsValid($token){
422422
public function setLoginToken($id){
423423
$this->adminModel->setLoginToken($id);
424424
}
425+
426+
public function getLoginToken($id){
427+
return $this->adminModel->getLoginToken($id);
428+
}
425429

426430
}

core/Model/AdminModel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ public function setLoginToken($id){
9898
return false;
9999
}
100100

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+
101110
public function validateLogin($plainPass, $username){
102111
$admin = new Admin($this, $plainPass);
103112
return $admin->validateLogin($plainPass, $username);

core/Pass.php

Lines changed: 3 additions & 1 deletion
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

Lines changed: 16 additions & 58 deletions
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

Lines changed: 29 additions & 0 deletions
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

Lines changed: 36 additions & 0 deletions
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+
}

core/helper/Logger/LoggerWriter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace phpList\helper\Logger;
4+
5+
6+
interface LoggerWriter
7+
{
8+
9+
public function log($level, $message, array $context = array());
10+
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace phpList\helper\Logger;
4+
5+
6+
use phpList\Config;
7+
8+
class LoggerWriterAbstractFactory
9+
{
10+
11+
private $config;
12+
13+
private $fileLogger = "file";
14+
private $databaseLogger = "database";
15+
16+
/**
17+
* LoggerWriterAbstractFactory constructor.
18+
* @param Config $config
19+
*/
20+
public function __construct(Config $config)
21+
{
22+
$this->config = $config;
23+
}
24+
25+
public function getLoggerWriter()
26+
{
27+
switch ($this->config->get("LOG_WRITER")){
28+
29+
case $this->fileLogger:
30+
return $this->makeFileLoggerWriter();
31+
break;
32+
33+
case $this->databaseLogger:
34+
return $this->makeDatabaseLoggerWriter();
35+
break;
36+
37+
default:
38+
throw new NotSuchWriterException();
39+
break;
40+
41+
}
42+
}
43+
44+
private function makeFileLoggerWriter()
45+
{
46+
return new FileWriter($this->config);
47+
}
48+
49+
private function makeDatabaseLoggerWriter()
50+
{
51+
return new DatabaseWriter($this->config);
52+
}
53+
54+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by IntelliJ IDEA.
4+
* User: aleksanderkoko
5+
* Date: 11/7/16
6+
* Time: 4:01 PM
7+
*/
8+
9+
namespace phpList\helper\Logger;
10+
11+
12+
class NotSuchWriterException extends \Exception
13+
{
14+
15+
}

0 commit comments

Comments
 (0)