Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
accessible_captcha.php
1 <?php
21 require_once dirname(dirname(dirname(__FILE__))).'/include/init.inc';
22 
23 
33 function sendAccessibleCaptchaEmail($to_email_address, $key)
34 {
35  require_once 'Mail.php';
36  require_once 'Mail/mime.php';
37 
38  // Strip spaces from around the "To" address in case these are present
39  $to_email_address = trim($to_email_address);
40 
41  // Provide the name of the system as supplied in the main.inc configuration for use in the email (if it is set)
42  $from_address = '"Accessible CAPTCHA Form"';
43  if (SQ_CONF_SYSTEM_NAME != '') {
44  $from_system_name = 'from the '.SQ_CONF_SYSTEM_NAME.' website ';
45  $from_address = SQ_CONF_SYSTEM_NAME;
46  }
47 
48  // Quote the System Name as it could contain apos'rophes
49  $from_address = '"'.$from_address.'"';
50 
51  $current_url = current_url();
52  $body = 'This email has been generated '.$from_system_name."as part of a form submission which includes an Accessible CAPTCHA field.\n\n".
53  "Please visit the following page to validate your submission before submitting the form\n\n".
54  $current_url.'?key='.$key;
55 
56  $mime = new Mail_mime("\n");
57  $mime->setTXTBody($body);
58 
59  $from_address .= ' <'.SQ_CONF_DEFAULT_EMAIL.'>';
60 
61  $headers = Array(
62  'From' => $from_address,
63  'Subject' => 'Accessible CAPTCHA Form Verification',
64  );
65 
66  $param = Array(
67  'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
68  'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
69  'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
70  );
71  $body = @$mime->get($param);
72  $headers = @$mime->headers($headers);
73  $mail =& Mail::factory('mail');
74  $status = @$mail->send($to_email_address, $headers, $body);
75 
76 }//end sendAccessiblecaptchaEmail()
77 
78 
88 function validateAccessibleCaptcha($key)
89 {
90  $verified_captcha = FALSE;
91 
92  if ((isset($_SESSION['SQ_ACCESSIBLE_CAPTCHA_KEY'])) && ($_SESSION['SQ_ACCESSIBLE_CAPTCHA_KEY'] === $key)) {
93  // F.A.B - CAPTCHAs are go!
94  $verified_captcha = TRUE;
95  $_SESSION['SQ_ACCESSIBLE_CAPTCHA_PASSED'] = 1;
96 
97  // Clear the CAPTCHA key from the session and access to run this script
98  unset($_SESSION['SQ_ACCESSIBLE_CAPTCHA_KEY']);
99  unset($_SESSION['SQ_ACCESSIBLE_CAPTCHA_GENERATED']);
100  }
101 
102  if ($verified_captcha) {
103 ?>
104 <p>Thank you for verifying the Accessible CAPTCHA input.<br />
105 Please proceed to submit your form.</p>
106 <?php
107  } else {
108 ?>
109 <p>This Accessible CAPTCHA key has expired for this session.<br />
110 Please return to the form to generate a new key.</p>
111 <?php
112  }
113 
114 }//end validateAccessibleCaptcha()
115 
116 
117 
126 // Ensure that the request originated from *this* Matrix system, otherwise drop the request like some sort of heavy feather
127 if (!isset($_SESSION['SQ_ACCESSIBLE_CAPTCHA_GENERATED'])) exit;
128 
129 // Generate an Accessible CAPTCHA Key
130 if (isset($_GET['email'])) {
131 
132  $user_email = addslashes($_GET['email']);
133 
134  // Ensure that we have *one* valid email address
135  if ((strpos($user_email, ',') === FALSE) || (strpos($user_email, ';') === FALSE)) {
136  // Return a key to be used in an email message to clear this CAPTCHA hurdle
137  // The trinity of email address, timestamp, user ID and a locally-generated integer should be unique enough to generate a robust key
138  $local_megadice = rand(1, 1000000);
139 
140  $submission_time = time();
141  $key = md5($user_email.$submission_time.$local_megadice);
142 
143  // Log our generated key in the user's session and send a nice email to the user for CAPTCHA validation
144  $_SESSION['SQ_ACCESSIBLE_CAPTCHA_KEY'] = $key;
145  sendAccessibleCaptchaEmail($user_email, $key);
146  }
147 
148 } else if (isset($_GET['key'])) {
149 // Verify an Accessible CAPTCHA Key from an email message
150 
151  $key = addslashes($_GET['key']);
152  validateAccessibleCaptcha($key);
153 
154 }
155 
156 ?>