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);
694b66cd
PM
5$post['ctl00$MainContent$btnSearch'] = "Search";
6$post['btnSearch'] = "Search";
425c643c 7$post['ctl00$MainContent$listLanguages'] = "en";
d44c2718 8$post['listLanguages'] = "en";
7e435904 9if (isset($_POST['SearchFirstName'])){
6a5ac174 10 //$post['SearchFirstName_field'] = 'ctl00$MainContent$txtSearchFirstName';
425c643c
PM
11 $post['ctl00$MainContent$txtSearchFirstName'] = $_POST['SearchFirstName'];
12 $post['txtSearchFirstName'] = $_POST['SearchFirstName'];
c58825ae 13}else{
425c643c 14 $post['ctl00$MainContent$txtSearchFirstName'] = '';
cfca1a0f 15 $post['txtSearchFirstName'] = '';
e8d6484d 16}
7e435904 17if (isset($_POST['SearchLastName'])){
6a5ac174 18 //$post['SearchLastName_field'] = 'ctl00$MainContent$txtSearchLastName';
425c643c 19 $post['ctl00$MainContent$txtSearchLastName'] = $_POST['SearchLastName'];
cf6a4bd9 20 $post['txtSearchLastName'] = $_POST['SearchLastName'];
c58825ae 21}else{
425c643c
PM
22 $post['ctl00$MainContent$txtSearchLastName'] = '';
23 $post['txtSearchLastName'] = '';
e8d6484d 24}
7e435904 25if (isset($_POST['DOBMonth'])){
6a5ac174 26 //$post['DOBMonth_field'] = 'ctl00$MainContent$txtDOBMonth';
425c643c
PM
27 $post['ctl00$MainContent$txtDOBMonth'] = $_POST['DOBMonth'];
28 $post['txtDOBMonth'] = $_POST['DOBMonth'];
c58825ae 29}else{
425c643c
PM
30 $post['ctl00$MainContent$txtDOBMonth'] = '';
31 $post['txtDOBMonth'] = '';
e8d6484d 32}
7e435904 33if (isset($_POST['DOBDay'])){
6a5ac174 34 //$post['DOBDay_field'] = 'ctl00$MainContent$txtDOBDay';
425c643c
PM
35 $post['ctl00$MainContent$txtDOBDay'] = $_POST['DOBDay'];
36 $post['txtDOBDay'] = $_POST['DOBDay'];
c58825ae 37}else{
425c643c
PM
38 $post['ctl00$MainContent$txtDOBDay'] = '';
39 $post['txtDOBDay'] = '';
e8d6484d 40}
7e435904 41if (isset($_POST['DOBYear'])){
6a5ac174 42 //$post['DOBYear_field'] = 'ctl00$MainContent$txtDOBYear';
425c643c
PM
43 $post['ctl00$MainContent$txtDOBYear'] = $_POST['DOBYear'];
44 $post['txtDOBYear'] = $_POST['DOBYear'];
c58825ae 45}else{
425c643c
PM
46 $post['ctl00$MainContent$txtDOBYear'] = '';
47 $post['txtDOBYear'] = '';
e8d6484d 48}
7e435904 49if (isset($_POST['SearchZipCode'])){
6a5ac174 50 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
425c643c
PM
51 $post['ctl00$MainContent$txtSearchZipCode'] = $_POST['SearchZipCode'];
52 $post['txtSearchZipCode'] = $_POST['SearchZipCode'];
c58825ae 53}else{
425c643c
PM
54 $post['ctl00$MainContent$txtSearchZipCode'] = '';
55 $post['txtSearchZipCode'] = '';
56}
57if (isset($_POST['SearchHouseNumber'])){
58 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
59 $post['ctl00$MainContent$txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
60 $post['txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
61}else{
62 $post['ctl00$MainContent$txtSearchHouseNumber'] = '';
63 $post['txtSearchHouseNumber'] = '';
64}
65if (isset($_POST['SearchMiddleInitial'])){
66 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
67 $post['ctl00$MainContent$txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
68 $post['txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
69}else{
70 $post['ctl00$MainContent$txtSearchMiddleInitial'] = '';
71 $post['txtSearchMiddleInitial'] = '';
e8d6484d 72}
9f5b3b38
PM
73
74
e8d6484d 75?>
9f5b3b38 76
e8d6484d
PM
77<form method='POST'>
78<table>
79 <tr>
9f5b3b38 80 <td>SearchFirstName</td><td><input name='SearchFirstName' value='<?PHP echo $post['txtSearchFirstName'];?>'></td>
e8d6484d
PM
81 </tr>
82 <tr>
9f5b3b38 83 <td>SearchLastName</td><td><input name='SearchLastName' value='<?PHP echo $post['txtSearchLastName'];?>'></td>
e8d6484d
PM
84 </tr>
85 <tr>
9f5b3b38 86 <td>DOBMonth</td><td><input name='DOBMonth' value='<?PHP echo $post['txtDOBMonth'];?>'></td>
e8d6484d
PM
87 </tr>
88 <tr>
9f5b3b38 89 <td>DOBDay</td><td><input name='DOBDay' value='<?PHP echo $post['txtDOBDay'];?>'></td>
e8d6484d
PM
90 </tr>
91 <tr>
9f5b3b38 92 <td>DOBYear</td><td><input name='DOBYear' value='<?PHP echo $post['txtDOBYear'];?>'></td>
e8d6484d
PM
93 </tr>
94 <tr>
9f5b3b38 95 <td>SearchZipCode</td><td><input name='SearchZipCode' value='<?PHP echo $post['txtSearchZipCode'];?>'></td>
e8d6484d
PM
96 </tr>
97 <tr>
9f5b3b38 98 <td>SearchHouseNumber*</td><td><input name='SearchHouseNumber' value='<?PHP echo $post['txtSearchHouseNumber'];?>'></td>
e8d6484d
PM
99 </tr>
100 <tr>
9f5b3b38 101 <td>SearchMiddleInitial*</td><td><input name='SearchMiddleInitial' value='<?PHP echo $post['txtSearchMiddleInitial'];?>'></td>
e8d6484d
PM
102 </tr>
103 <tr>
425c643c 104 <td>*Optional</td><td><input type='submit'></td>
e8d6484d
PM
105 </tr>
106</table>
107</form>
9f5b3b38
PM
108
109
6a5ac174 110<?PHP
9f5b3b38
PM
111function cut_part_out($start,$end,$whole){
112 $parts = explode($start,$whole);
113 $subparts = explode($end,$parts[1]);
114 $out = $subparts[0];
115 return $out;
116}
117
118function getPage($url,$cookie,$post){
119 $url = str_replace('[month]',date('F'),$url); // replace month January through December
120 $url = str_replace('[day]',date('j'),$url); // replace day 1 to 31
121 $url = str_replace('[yesterday]',date('j',strtotime('yesterday')),$url); // replace day 1 to 31
122 $url = str_replace('[year]',date('Y'),$url); // replace year Examples: 1999 or 2003
123 $curl = curl_init();
124 curl_setopt ($curl, CURLOPT_URL, $url);
125 curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("McGuire MEPS https://www.md-petition.com/ /%d.0",rand(4,50)));
126 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
127 curl_setopt ($curl, CURLOPT_HEADER, 1);
128 curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
129 curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
130 if ($cookie != ''){
131 curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Cookie: ASP.NET_SessionId=$cookie")); // use cookies
132 }
133 /*
134 $postfields = array(
135 'upload_file' => '@file_to_upload.png',
136 'upload_text' => '@text_to_upload'
137 );
138 */
139 if ($post != ''){
140 curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
141 }
142 $html = curl_exec ($curl);
143 curl_close ($curl);
144 return $html;
145}
146
147// start a session to get a cookie
148$form['url'] = 'https://voterservices.elections.maryland.gov/VoterSearch';
149$form['html'] = getPage($form['url'],'','');
150
151// extract the cookie from the header (see CURLOPT_HEADER)
152$cookie = cut_part_out('ASP.NET_SessionId=',';',$form['html']);
153// extract the form elements we will need to post with our data
154$post['__VIEWSTATE'] = cut_part_out('id="__VIEWSTATE" value="','"',$form['html']);
155$post['__VIEWSTATEGENERATOR'] = cut_part_out('id="__VIEWSTATEGENERATOR" value="','"',$form['html']);
156$post['__VIEWSTATEENCRYPTED'] = cut_part_out('id="__VIEWSTATEENCRYPTED" value="','"',$form['html']);
157$post['__EVENTVALIDATION'] = cut_part_out('id="__EVENTVALIDATION" value="','"',$form['html']);
158// Voter Info Form Elements
159//$post['Languages_field'] = 'ctl00$MainContent$listLanguages';
160
161// debug - show full response make sure we have the cookie
162echo "<li>COOKIE ASP.NET_SessionId $cookie</li>";
163echo "<li>POST VIEWSTATE $post[__VIEWSTATE]</li>";
164echo "<li>POST VIEWSTATEGENERATOR $post[__VIEWSTATEGENERATOR]</li>";
165echo "<li>POST VIEWSTATEENCRYPTED $post[__VIEWSTATEENCRYPTED]</li>";
166echo "<li>POST EVENTVALIDATION $post[__EVENTVALIDATION]</li>";
167echo "<pre>";
168echo print_r($post);
169echo "</pre>";
9f5b3b38 170
cfca1a0f 171if ($post['txtSearchFirstName'] != ''){
6a5ac174 172 $result['html'] = getPage($form['url'],$cookie,$post);
55a46cf7 173 echo "<h1>STEP 2: SBE RESULTS</h1>";
c1fa38f8 174 echo htmlspecialchars($result['html']);
c1fa38f8 175 echo $result['html'];
55a46cf7
PM
176}else{
177 echo "<h1>STEP 1: Input Form</h1>";
178 echo htmlspecialchars($form['html']);
179 echo $form['html'];
6a5ac174
PM
180}
181?>