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
|
|
|
* Get either a body or a query parameter. |
79
|
|
|
* @param ServerRequestInterface $request |
80
|
|
|
* @param string $param The name of the parameter to get. |
81
|
|
|
* @param mixed $default |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getAnyParam(ServerRequestInterface $request, $param, $default = null) |
85
|
|
|
{ |
86
|
|
|
$bodyParam = $this->getBodyParam($request, $param, $default); |
87
|
|
|
if ($bodyParam !== null) { |
88
|
|
|
return $bodyParam; |
89
|
|
|
} |
90
|
|
|
return $this->getQueryParam($request, $param, $default); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Send an email message. |
95
|
|
|
* |
96
|
|
|
* @param Swift_Message $message |
97
|
|
|
*/ |
98
|
|
|
public function email(Swift_Message $message) |
99
|
|
|
{ |
100
|
|
|
$config = $this->config->mail(); |
101
|
|
|
$transport_classname = '\\Swift_' . ucfirst($config['transport']) . 'Transport'; |
102
|
|
|
$transport = $transport_classname::newInstance(); |
103
|
|
|
$mailer = Swift_Mailer::newInstance($transport); |
104
|
|
|
$mailer->send($message); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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: