|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Config; |
|
6
|
|
|
use App\User; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Swift_Mailer; |
|
9
|
|
|
use Swift_Message; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Base |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The site configuration. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \App\Config |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The database. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \App\Db |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $db; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The current user. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \App\User |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $user; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Every controller gets the configuration, database, and a user. If the user has a session |
|
37
|
|
|
* in progress, the user is loaded from that. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct() |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$this->config = new Config(); |
|
42
|
|
|
$this->db = new \App\Db(); |
|
43
|
|
|
|
|
44
|
|
|
// User. |
|
45
|
|
|
$this->user = new User($this->db); |
|
46
|
|
|
if (isset($_SESSION['userid'])) { |
|
47
|
|
|
$this->user->load($_SESSION['userid']); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get a parameter value out of a request's body. |
|
53
|
|
|
* @param ServerRequestInterface $request |
|
54
|
|
|
* @param string $param The name of the parameter to get. |
|
55
|
|
|
* @param mixed $default |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getBodyParam(ServerRequestInterface $request, $param, $default = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$params = $request->getParsedBody(); |
|
61
|
|
|
return (isset($params[$param])) ? $params[$param] : $default; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get a parameter from the query string of this request. |
|
66
|
|
|
* @param ServerRequestInterface $request |
|
67
|
|
|
* @param string $param The name of the parameter to get. |
|
68
|
|
|
* @param mixed $default |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getQueryParam(ServerRequestInterface $request, $param, $default = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$params = $request->getQueryParams(); |
|
74
|
|
|
return (isset($params[$param])) ? $params[$param] : $default; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Send an email message. |
|
79
|
|
|
* |
|
80
|
|
|
* @param Swift_Message $message |
|
81
|
|
|
*/ |
|
82
|
|
|
public function email(Swift_Message $message) |
|
83
|
|
|
{ |
|
84
|
|
|
$config = $this->config->mail(); |
|
85
|
|
|
$transport_classname = '\\Swift_' . ucfirst($config['transport']) . 'Transport'; |
|
86
|
|
|
$transport = $transport_classname::newInstance(); |
|
87
|
|
|
$mailer = Swift_Mailer::newInstance($transport); |
|
88
|
|
|
$mailer->send($message); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: