Update maryland_voter.php
[clinton/MarylandElectronicPetitionSignature.git] / api / maryland_voter.php
CommitLineData
183344e4 1<?PHP
e20f7315
PM
2ini_set('display_errors', 1);
3ini_set('display_startup_errors', 1);
4error_reporting(E_ALL);
5
a40d2263
PM
6function cut_part_out($start,$end,$whole){
7 $parts = explode($start,$whole);
8 $subparts = explode($end,$parts[1]);
9 $out = $subparts[0];
10 return $out;
11}
12
6a5ac174 13function getPage($url,$cookie,$post){
183344e4
PM
14 $url = str_replace('[month]',date('F'),$url); // replace month January through December
15 $url = str_replace('[day]',date('j'),$url); // replace day 1 to 31
16 $url = str_replace('[yesterday]',date('j',strtotime('yesterday')),$url); // replace day 1 to 31
17 $url = str_replace('[year]',date('Y'),$url); // replace year Examples: 1999 or 2003
18 $curl = curl_init();
19 curl_setopt ($curl, CURLOPT_URL, $url);
20 curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("McGuire MEPS https://www.md-petition.com/ /%d.0",rand(4,50)));
21 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
876e678d 22 curl_setopt ($curl, CURLOPT_HEADER, 1);
183344e4
PM
23 curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
24 curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
a40d2263
PM
25 if ($cookie != ''){
26 curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Cookie: ASP.NET_SessionId=$cookie")); // use cookies
27 }
28 /*
29 $postfields = array(
30 'upload_file' => '@file_to_upload.png',
31 'upload_text' => '@text_to_upload'
32 );
a40d2263 33 */
6a5ac174
PM
34 if ($post != ''){
35 curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
36 }
183344e4
PM
37 $html = curl_exec ($curl);
38 curl_close ($curl);
39 return $html;
40}
e20f7315 41
651ce823 42// start a session to get a cookie
183344e4 43$form['url'] = 'https://voterservices.elections.maryland.gov/VoterSearch';
6a5ac174 44$form['html'] = getPage($form['url'],'','');
e20f7315 45
a40d2263
PM
46// extract the cookie from the header (see CURLOPT_HEADER)
47$cookie = cut_part_out('ASP.NET_SessionId=',';',$form['html']);
5ba87f85 48// extract the form elements we will need to post with our data
6a5ac174
PM
49$post['__VIEWSTATE'] = cut_part_out('id="__VIEWSTATE" value="','"',$form['html']);
50$post['__VIEWSTATEGENERATOR'] = cut_part_out('id="__VIEWSTATEGENERATOR" value="','"',$form['html']);
51$post['__VIEWSTATEENCRYPTED'] = cut_part_out('id="__VIEWSTATEENCRYPTED" value="','"',$form['html']);
52$post['__EVENTVALIDATION'] = cut_part_out('id="__EVENTVALIDATION" value="','"',$form['html']);
e8d6484d 53// Voter Info Form Elements
6a5ac174 54//$post['Languages_field'] = '$ctl00$MainContent$listLanguages';
19089eab 55$post['Languages'] = "en";
7e435904 56if (isset($_POST['SearchFirstName'])){
6a5ac174 57 //$post['SearchFirstName_field'] = 'ctl00$MainContent$txtSearchFirstName';
19089eab 58 $post['SearchFirstName'] = $_POST['SearchFirstName'];
c58825ae 59}else{
19089eab 60 $post['SearchFirstName'] = '';
e8d6484d 61}
7e435904 62if (isset($_POST['SearchLastName'])){
6a5ac174 63 //$post['SearchLastName_field'] = 'ctl00$MainContent$txtSearchLastName';
19089eab 64 $post['SearchLastName'] = $_POST['SearchLastName'];
c58825ae 65}else{
19089eab 66 $post['SearchLastName'] = '';
e8d6484d 67}
7e435904 68if (isset($_POST['DOBMonth'])){
6a5ac174 69 //$post['DOBMonth_field'] = 'ctl00$MainContent$txtDOBMonth';
19089eab 70 $post['DOBMonth'] = $_POST['DOBMonth'];
c58825ae 71}else{
19089eab 72 $post['DOBMonth'] = '';
e8d6484d 73}
7e435904 74if (isset($_POST['DOBDay'])){
6a5ac174 75 //$post['DOBDay_field'] = 'ctl00$MainContent$txtDOBDay';
19089eab 76 $post['DOBDay'] = $_POST['DOBDay'];
c58825ae 77}else{
19089eab 78 $post['DOBDay'] = '';
e8d6484d 79}
7e435904 80if (isset($_POST['DOBYear'])){
6a5ac174 81 //$post['DOBYear_field'] = 'ctl00$MainContent$txtDOBYear';
19089eab 82 $post['DOBYear'] = $_POST['DOBYear'];
c58825ae 83}else{
19089eab 84 $post['DOBYear'] = '';
e8d6484d 85}
7e435904 86if (isset($_POST['SearchZipCode'])){
6a5ac174 87 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
19089eab 88 $post['SearchZipCode'] = $_POST['SearchZipCode'];
c58825ae 89}else{
19089eab 90 $post['SearchZipCode'] = '';
e8d6484d 91}
651ce823 92// debug - show full response make sure we have the cookie
a40d2263 93echo "<li>COOKIE ASP.NET_SessionId $cookie</li>";
6a5ac174
PM
94echo "<li>POST VIEWSTATE $post[__VIEWSTATE]</li>";
95echo "<li>POST VIEWSTATEGENERATOR $post[__VIEWSTATEGENERATOR]</li>";
96echo "<li>POST VIEWSTATEENCRYPTED $post[__VIEWSTATEENCRYPTED]</li>";
97echo "<li>POST EVENTVALIDATION $post[__EVENTVALIDATION]</li>";
c1c726fd
PM
98echo "<pre>";
99echo print_r($post);
100echo "</pre>";
e990a75d 101echo "<hr><hr><hr><h1>Voter Form</h1>";
a40d2263 102echo htmlspecialchars($form['html']);
6a5ac174 103echo '<hr><hr><hr>';
e8d6484d
PM
104//echo $form['html'];
105?>
106<form method='POST'>
107<table>
108 <tr>
109 <td>SearchFirstName</td><td><input name='SearchFirstName'></td>
110 </tr>
111 <tr>
112 <td>SearchLastName</td><td><input name='SearchLastName'></td>
113 </tr>
114 <tr>
115 <td>DOBMonth</td><td><input name='DOBMonth'></td>
116 </tr>
117 <tr>
118 <td>DOBDay</td><td><input name='DOBDay'></td>
119 </tr>
120 <tr>
121 <td>DOBYear</td><td><input name='DOBYear'></td>
122 </tr>
123 <tr>
124 <td>SearchZipCode</td><td><input name='SearchZipCode'></td>
125 </tr>
126 <tr>
127 <td>SearchHouseNumber</td><td>n/a</td>
128 </tr>
129 <tr>
130 <td>SearchMiddleInitial</td><td>n/a</td>
131 </tr>
132 <tr>
133 <td></td><td><input type='submit'></td>
134 </tr>
135</table>
136</form>
6a5ac174 137<?PHP
19089eab 138if ($post['SearchFirstName'] != ''){
6a5ac174
PM
139 $result['html'] = getPage($form['url'],$cookie,$post);
140 echo "<hr><hr><hr><h1>SBE API RESULTS</h1>";
c1fa38f8
PM
141 echo htmlspecialchars($result['html']);
142 echo '<hr><hr><hr>';
143 echo $result['html'];
6a5ac174
PM
144}
145?>