Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Dcp_WSHMailError.php
Go to the documentation of this file.
1 <?php
2 namespace Dcp;
3 
5 {
6  public $expand = array();
7  /**
8  * @var \Action
9  */
10  public $action = null;
11  public $msg = '';
12  public $from = '';
13  public $mailto = '';
14  public $subject = '';
15  public $prefix = false;
16  /**
17  * WSHError constructor.
18  * @param \Action $action
19  * @param $msg
20  */
21  public function __construct(\Action & $action, $msg)
22  {
23  $this->reset($action, $msg);
24  }
25  /**
26  * @param \Action $action
27  * @param $msg
28  */
29  public function reset(\Action & $action, $msg)
30  {
31  $this->expand = array(
32  'h' => php_uname('n') ,
33  'c' => \ApplicationParameterManager::getParameterValue('CORE', 'CORE_CLIENT')
34  );
35  $this->action = $action;
36  $this->msg = $msg;
37  $this->from = '';
38  $this->mailto = '';
39  $this->subject = '';
40  $this->prefix = false;
41  }
42  /**
43  * @param $str
44  * @return string
45  */
46  public function expand($str)
47  {
48  $res = '';
49  $expandNextChar = false;
50  $p = 0;
51  while ($p < mb_strlen($str)) {
52  $c = mb_substr($str, $p, 1);
53  if ($expandNextChar) {
54  if ($c == '%') {
55  $res.= '%';
56  } elseif (isset($this->expand[$c])) {
57  $res.= $this->expand[$c];
58  } else {
59  $res.= $c;
60  }
61  $expandNextChar = false;
62  } else {
63  if ($c == '%') {
64  $expandNextChar = true;
65  } else {
66  $res.= $c;
67  }
68  }
69  $p++;
70  }
71  return $res;
72  }
73  /**
74  * @return string
75  */
76  public function autosend()
77  {
78  $from = getMailAddr($this->action->user->id);
79  if ($from == '') {
81  }
82  if ($from == '') {
83  $from = $this->action->user->login . '@' . php_uname('n');
84  }
85  $this->from = $from;
86 
87  $mailto = trim(\ApplicationParameterManager::getParameterValue('CORE', 'CORE_WSH_MAILTO'));
88  if ($mailto == '') {
89  return '';
90  }
91  $this->mailto = $mailto;
92 
93  $subject = \ApplicationParameterManager::getParameterValue('CORE', 'CORE_WSH_MAILSUBJECT');
94  if ($subject == '') {
95  $subject = 'Script error';
96  }
97  $this->subject = $this->expand($subject);
98 
99  $msg = $this->msg;
100  if (is_string($this->prefix) && strlen($this->prefix) > 0) {
101  $msg = $this->prefixize($this->prefix, $msg);
102  }
103 
104  $htmlBody = sprintf('<pre>%s</pre>', htmlspecialchars($msg, ENT_QUOTES));
105 
106  return $this->send($this->from, $this->mailto, $this->subject, $htmlBody, $msg);
107  }
108  /**
109  * Add a prefix at beginning of each lines
110  * @param string $prefix
111  * @param string $msg
112  * @return string
113  */
114  public function prefixize($prefix, $msg)
115  {
116  return $prefix . str_replace("\n", "\n" . $prefix, $msg);
117  }
118  /**
119  * @param string $from Sender's email address (e.g. 'john.doe@example.net')
120  * @param string|string[] $mailto Recipient(s) address(es) as a comma-separated list of mail addresses, or and array og mail addresses
121  * @param string $subject Subject
122  * @param string $htmlBody The main body in HTML format
123  * @param string $altTextBody Optional text variant of the main HTML body
124  * @param array[] $attachments Optional attachments (e.g. array( array('file' => '/path/to/att.dat', 'name' => 'icon.png', 'mime' => 'image/png' [, 'cid' => 'CIDidentifier'] ), [...] )))
125  * @return string Non-empty string with error message on failure, or empty string on success
126  */
127  public function send($from, $mailto, $subject, $htmlBody, $altTextBody = '', $attachments = array())
128  {
129  $message = new \Dcp\Mail\Message();
130  $message->setFrom($from);
131  $message->setSubject($subject);
132  $recipients = array();
133  if (is_string($mailto)) {
134  foreach (preg_split('/\s*,\s*/', $mailto) as $to) {
135  if ($to == '') {
136  continue;
137  }
138  $recipients[] = $to;
139  }
140  } elseif (is_array($mailto)) {
141  foreach ($mailto as $to) {
142  $recipients[] = $to;
143  }
144  }
145  foreach ($recipients as $to) {
146  $message->addTo($to);
147  }
148  $message->setBody(new \Dcp\Mail\Body($htmlBody, 'text/html'));
149  if (is_string($altTextBody) && strlen($altTextBody) > 0) {
150  $message->setAltBody(new \Dcp\Mail\Body($altTextBody, 'text/plain'));
151  }
152  if (is_array($attachments)) {
153  foreach ($attachments as $att) {
154  if (!is_array($att) || !isset($att['file']) || !isset($att['name']) || !isset($att['mime'])) {
155  continue;
156  }
157  if (isset($att['cid'])) {
158  $message->addBodyRelatedAttachment(new \Dcp\Mail\RelatedAttachment($att['file'], $att['name'], $att['mime']));
159  } else {
160  $message->addAttachment(new \Dcp\Mail\Attachment($att['file'], $att['name'], $att['mime']));
161  }
162  }
163  }
164  if (($err = $message->send()) !== '') {
165  error_log(__METHOD__ . " " . sprintf("Error sending mail: %s", $err));
166  }
167  return $err;
168  }
169  /**
170  * @param string[] $expand
171  */
172  public function addExpand($expand = array())
173  {
174  if (!is_array($expand)) {
175  return;
176  }
177  foreach ($expand as $k => $v) {
178  if (!is_string($v)) {
179  continue;
180  }
181  $this->expand[$k] = $v;
182  }
183  }
184 }
addExpand($expand=array())
global $action
getMailAddr($userid, $full=false)
Definition: Lib.Common.php:133
$message
static getParameterValue($application, $parameterName)
$to
send($from, $mailto, $subject, $htmlBody, $altTextBody= '', $attachments=array())
$subject
reset(\Action &$action, $msg)
$from
if($file) if($subject==""&&$file) if($subject=="") $err
__construct(\Action &$action, $msg)
← centre documentaire © anakeen