CMSimple_XH 開発者ドキュメント
Mailform.php
1 <?php
2 
16 namespace XH;
17 
28 class Mailform
29 {
35  private $embedded;
36 
42  private $sendername;
43 
49  private $senderphone;
50 
56  private $sender;
57 
63  private $getlast;
64 
70  private $cap;
71 
77  public $subject;
78 
84  private $mailform;
85 
91  private $mail;
92 
104  public function __construct($embedded = false, $subject = null, $mail = null)
105  {
106  global $tx;
107 
108  $this->embedded = $embedded;
109  $this->sendername = isset($_POST['sendername'])
110  ? stsl($_POST['sendername']) : '';
111  $this->senderphone = isset($_POST['senderphone'])
112  ? stsl($_POST['senderphone']) : '';
113  $this->sender = isset($_POST['sender'])
114  ? stsl($_POST['sender']) : '';
115  $this->getlast = isset($_POST['getlast'])
116  ? stsl($_POST['getlast']) : '';
117  $this->cap = isset($_POST['cap'])
118  ? stsl($_POST['cap']) : '';
119 
120  if (isset($_POST['subject'])) {
121  $this->subject = stsl($_POST['subject']);
122  } elseif (isset($_GET['xh_mailform_subject'])) {
123  $this->subject = stsl($_GET['xh_mailform_subject']);
124  } elseif (isset($subject)) {
125  $this->subject = $subject;
126  } else {
127  $this->subject = sprintf($tx['mailform']['subject_default'], sv('SERVER_NAME'));
128  }
129 
130  if ($embedded) {
131  $this->mailform = isset($_POST['xh_mailform'])
132  ? stsl($_POST['xh_mailform']) : '';
133  } else {
134  $this->mailform = isset($_POST['mailform'])
135  ? stsl($_POST['mailform']) : '';
136  }
137  $this->mail = isset($mail) ? $mail : new Mail();
138  }
139 
148  public function check()
149  {
150  global $cf, $tx;
151 
152  $o = '';
153  if ($this->getlast != $this->cap
154  && trim($cf['mailform']['captcha']) == 'true'
155  ) {
156  $o .= XH_message('warning', $tx['mailform']['captchafalse']);
157  }
158  if ($this->mailform == '') {
159  $o .= XH_message('warning', $tx['mailform']['mustwritemessage']);
160  }
161  if (!$this->mail->isValidAddress($this->sender) || $this->subject == '') {
162  $o .= XH_message('warning', $tx['mailform']['notaccepted']);
163  }
164  return $o;
165  }
166 
175  public function submit()
176  {
177  global $cf, $tx;
178 
179  $this->mail->setTo($cf['mailform']['email']);
180  $this->mail->addHeader('From', $this->sender);
181  $this->mail->addHeader('X-Remote', sv('REMOTE_ADDR'));
182  $this->mail->setSubject($this->subject);
183  $this->mail->setMessage(
184  $tx['mailform']['sendername'] . $this->sendername . "\n"
185  . $tx['mailform']['senderphone'] . $this->senderphone . "\n\n"
186  . $this->mailform
187  );
188  $sent = $this->mail->send();
189  if (!$sent) {
190  XH_logMessage('error', 'XH', 'mailform', $this->sender);
191  }
192  return $sent;
193  }
194 
207  public function process()
208  {
209  global $action, $tx;
210  static $again = false;
211 
212  if ($again) {
213  return false;
214  }
215  $again = true;
216 
217  $anchor = '<div id="xh_mailform"></div>';
218  if ($action == 'send') {
219  $o = $this->check();
220  if (!$o && $this->submit()) {
221  $o .= $anchor . XH_message('success', $tx['mailform']['send']);
222  } else {
223  $o .= $anchor . XH_message('fail', $tx['mailform']['notsend'])
224  . $this->render();
225  }
226  } else {
227  $o = $anchor . $this->render();
228  }
229  return $o;
230  }
231 
242  public function render()
243  {
244  global $sn, $cf, $tx, $su;
245 
246  $random = rand(10000, 99999);
247  $url = $sn . ($this->embedded ? '?' . $su : '');
248  $o = '<form class="xh_mailform" action="' . $url
249  . '#xh_mailform" method="post">' . "\n";
250  if (!$this->embedded) {
251  $o .= '<input type="hidden" name="function" value="mailform">' . "\n";
252  }
253  if (isset($cf['mailform']['captcha'])
254  && trim($cf['mailform']['captcha']) == 'true'
255  ) {
256  $o .= '<input type="hidden" name="getlast" value="' . $random . '">'
257  . "\n";
258  }
259  $o .= '<input type="hidden" name="action" value="send">' . "\n";
260 
261  // fields before textarea
262  $o .= '<div>' . "\n" . '<label for="xh_mailform_sendername">'
263  . $tx['mailform']['sendername'] . '</label>' . '<br>' . "\n"
264  . '<input type="text" class="text" size="35" name="sendername"'
265  . ' id="xh_mailform_sendername" value="'
266  . XH_hsc($this->sendername).'">' . "\n"
267  . '</div>' . "\n"
268  . '<div>' . "\n" . '<label for="xh_mailform_senderphone">'
269  . $tx['mailform']['senderphone'] . '</label>' . '<br>' . "\n"
270  . '<input type="tel" class="text" size="35" name="senderphone"'
271  . ' id="xh_mailform_senderphone" value="'
272  . XH_hsc($this->senderphone).'">' . "\n"
273  . '</div>' . "\n"
274  . '<div>' . "\n" . '<label for="xh_mailform_sender">'
275  . $tx['mailform']['sender'] . '</label>' . '<br>' . "\n"
276  . '<input type="email" class="text" size="35" name="sender"'
277  . ' id="xh_mailform_sender" value="'
278  . XH_hsc($this->sender).'" required="required">' . "\n"
279  . '</div>' . "\n"
280  . '<div>' . "\n" . '<label for="xh_mailform_subject">'
281  . $tx['mailform']['subject'] . '</label>'. '<br>' . "\n"
282  . '<input type="text" class="text" size="35" name="subject"'
283  . ' id="xh_mailform_subject" value="'
284  . XH_hsc($this->subject).'" required="required">' . "\n"
285  . '</div>' . "\n"
286  . '<br>' . "\n";
287 
288  // textarea
289  $name = $this->embedded ? 'xh_mailform' : 'mailform';
290  $o .= '<textarea rows="12" cols="40" name="' . $name
291  . '" required="required" title="' . $tx['mailform']['message'] . '">'
292  . XH_hsc($this->mailform) . '</textarea>';
293 
294  // captcha
295  if (isset($cf['mailform']['captcha'])
296  && trim($cf['mailform']['captcha']) == 'true'
297  ) {
298  $o .= '<p>' . $tx['mailform']['captcha'] . '</p>' . "\n"
299  . '<input type="text" name="cap" class="xh_captcha_input"'
300  . ' required="required">'
301  . "\n" . '<span class="xh_captcha_code">' . "\n"
302  . $random . '</span>' . "\n";
303  }
304 
305  // send button
306  $o .= '<div class="xh_break">' . "\n"
307  . '<input type="submit" class="submit" value="'
308  . $tx['mailform']['sendbutton'] . '">'
309  . "\n" . '</div>' . "\n" . '</form>' . "\n";
310 
311  return $o;
312  }
313 }
$action
Definition: cms.php:460
XH_logMessage($type, $module, $category, $description)
Definition: functions.php:1523
$su
Definition: cms.php:778
sv($s)
Definition: functions.php:543
__construct($embedded=false, $subject=null, $mail=null)
Definition: Mailform.php:104
$cf
Definition: cms.php:272
$o
Definition: cms.php:113
$sn
Definition: cms.php:434
$tx
Definition: cms.php:363
stsl($t)
Definition: functions.php:606
XH_hsc($string)
Definition: functions.php:2204
$mailform
Definition: cms.php:524
XH_message($type, $message)
Definition: functions.php:1806
Definition: Mail.php:28