1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\components; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Object; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Yii2 Rest Component |
10
|
|
|
* @class YRC |
11
|
|
|
*/ |
12
|
|
|
class YRC extends Object |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The user class |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $userClass; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The email address that emails should be sent from |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $fromEmail; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Whether or not we should really send emails |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
public $realSend = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The access header |
34
|
|
|
* If set access to controller actions is granted if and only if the HTTP header value |
35
|
|
|
* identified by this parameters equals the $accessHeaderSecret property |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public $accessHeader; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The access header secret value |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public $accessHeaderSecret; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Helper method to check the access header |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
public function checkAccessHeader($request) |
51
|
|
|
{ |
52
|
|
|
// Both the access header and access header secret must be set for this check to validate |
53
|
|
|
if ($this->accessHeader === null || $this->accessHeaderSecret === null) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Fetch the access header from the request |
58
|
|
|
$header = $request->getHeaders()->get($this->accessHeader); |
59
|
|
|
|
60
|
|
|
// If the header isn't set, deny |
61
|
|
|
if ($header === null) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Allow if the header values match |
66
|
|
|
if (\hash_equals($this->accessHeaderSecret, $header)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Deny by default |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Send an email using the provided viewfile and parameters |
76
|
|
|
* @param string $viewFile |
77
|
|
|
* @param string $subject |
78
|
|
|
* @param string $email |
79
|
|
|
* @param array $params |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
public function sendEmail($viewFile, $subject, $email, array $params = []) |
83
|
|
|
{ |
84
|
|
|
// Default to this view file |
85
|
|
|
$viewFilePath = '@app/views/mail/en-US/' . $viewFile . '.twig'; |
86
|
|
|
|
87
|
|
|
// Scan the "Accept-Language" header to see if we have a better viewfile |
88
|
|
|
$languages = Yii::$app->request->getAcceptableLanguages(); |
89
|
|
|
foreach ($languages as $language) { |
90
|
|
|
$vfp = '@app/views/mail/' . $language . '/' . $viewFile . '.twig'; |
91
|
|
|
if (\file_exists(Yii::getAlias($vfp))) { |
92
|
|
|
$viewFilePath = $vfp; |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!\file_exists(Yii::getAlias($viewFilePath))) { |
98
|
|
|
Yii::warning(sprintf('The requested view (%s) file does not exist', $viewFilePath)); |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$view = Yii::$app->view->renderFile($viewFilePath, $params); |
103
|
|
|
|
104
|
|
|
if ($this->realSend === true) { |
105
|
|
|
return Yii::$app->mailer->compose() |
106
|
|
|
->setFrom($this->fromEmail) |
107
|
|
|
->setTo($email) |
108
|
|
|
->setSubject($subject) |
109
|
|
|
->setHtmlBody($view) |
110
|
|
|
->send(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
} |