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
|
|
|
* The name to associate with the origin email |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $fromName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Whether or not we should really send emails |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
public $realSend = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The access header |
40
|
|
|
* If set access to controller actions is granted if and only if the HTTP header value |
41
|
|
|
* identified by this parameters equals the $accessHeaderSecret property |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public $accessHeader; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The access header secret value |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public $accessHeaderSecret; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Helper method to check the access header |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function checkAccessHeader($request) |
57
|
|
|
{ |
58
|
|
|
// Both the access header and access header secret must be set for this check to validate |
59
|
|
|
if ($this->accessHeader === null || $this->accessHeaderSecret === null) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Fetch the access header from the request |
64
|
|
|
$header = $request->getHeaders()->get($this->accessHeader); |
65
|
|
|
|
66
|
|
|
// Allow if the header values match |
67
|
|
|
if (\hash_equals($this->accessHeaderSecret, $header)) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Deny by default |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Send an email using the provided viewfile and parameters |
77
|
|
|
* @param string $viewFile |
78
|
|
|
* @param string $subject |
79
|
|
|
* @param string $email |
80
|
|
|
* @param array $params |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function sendEmail($viewFile, $subject, $email, array $params = []) |
84
|
|
|
{ |
85
|
|
|
// Default to this view file |
86
|
|
|
$viewFilePath = '@app/views/mail/en-US/' . $viewFile . '.twig'; |
87
|
|
|
|
88
|
|
|
// Scan the "Accept-Language" header to see if we have a better viewfile |
89
|
|
|
$languages = Yii::$app->request->getAcceptableLanguages(); |
90
|
|
|
foreach ($languages as $language) { |
91
|
|
|
$vfp = '@app/views/mail/' . $language . '/' . $viewFile . '.twig'; |
92
|
|
|
if (\file_exists(Yii::getAlias($vfp))) { |
93
|
|
|
$viewFilePath = $vfp; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!\file_exists(Yii::getAlias($viewFilePath))) { |
99
|
|
|
Yii::warning(sprintf('The requested view (%s) file does not exist', $viewFilePath)); |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$view = Yii::$app->view->renderFile($viewFilePath, $params); |
104
|
|
|
|
105
|
|
|
if ($this->realSend === true) { |
106
|
|
|
return Yii::$app->mailer->compose() |
107
|
|
|
->setFrom($this->fromEmail) |
108
|
|
|
->setTo($email) |
109
|
|
|
->setSubject($subject) |
110
|
|
|
->setHtmlBody($view) |
111
|
|
|
->send(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |