Update maryland_voter.php
[clinton/MarylandElectronicPetitionSignature.git] / api / maryland_voter.php
1 <?PHP
2 ini_set('display_errors', 1);
3 ini_set('display_startup_errors', 1);
4 error_reporting(E_ALL);
5
6 function 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
13 function getPage($url,$cookie,$post){
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);
22 curl_setopt ($curl, CURLOPT_HEADER, 1);
23 curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
24 curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
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 );
33 */
34 if ($post != ''){
35 curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
36 }
37 $html = curl_exec ($curl);
38 curl_close ($curl);
39 return $html;
40 }
41
42 // start a session to get a cookie
43 $form['url'] = 'https://voterservices.elections.maryland.gov/VoterSearch';
44 $form['html'] = getPage($form['url'],'','');
45
46 // extract the cookie from the header (see CURLOPT_HEADER)
47 $cookie = cut_part_out('ASP.NET_SessionId=',';',$form['html']);
48 // extract the form elements we will need to post with our data
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']);
53 // Voter Info Form Elements
54 //$post['Languages_field'] = 'ctl00$MainContent$listLanguages';
55 $post['ctl00$MainContent$listLanguages'] = "en";
56 $post['listLanguages'] = "en";
57 if (isset($_POST['SearchFirstName'])){
58 //$post['SearchFirstName_field'] = 'ctl00$MainContent$txtSearchFirstName';
59 $post['ctl00$MainContent$txtSearchFirstName'] = $_POST['SearchFirstName'];
60 $post['txtSearchFirstName'] = $_POST['SearchFirstName'];
61 }else{
62 $post['ctl00$MainContent$txtSearchFirstName'] = '';
63 $post['txtSearchFirstName'] = '';
64 }
65 if (isset($_POST['SearchLastName'])){
66 //$post['SearchLastName_field'] = 'ctl00$MainContent$txtSearchLastName';
67 $post['ctl00$MainContent$txtSearchLastName'] = $_POST['SearchLastName'];
68 $post['ctl00$MainContent$txtSearchLastName'] = $_POST['SearchLastName'];
69 }else{
70 $post['ctl00$MainContent$txtSearchLastName'] = '';
71 $post['txtSearchLastName'] = '';
72 }
73 if (isset($_POST['DOBMonth'])){
74 //$post['DOBMonth_field'] = 'ctl00$MainContent$txtDOBMonth';
75 $post['ctl00$MainContent$txtDOBMonth'] = $_POST['DOBMonth'];
76 $post['txtDOBMonth'] = $_POST['DOBMonth'];
77 }else{
78 $post['ctl00$MainContent$txtDOBMonth'] = '';
79 $post['txtDOBMonth'] = '';
80 }
81 if (isset($_POST['DOBDay'])){
82 //$post['DOBDay_field'] = 'ctl00$MainContent$txtDOBDay';
83 $post['ctl00$MainContent$txtDOBDay'] = $_POST['DOBDay'];
84 $post['txtDOBDay'] = $_POST['DOBDay'];
85 }else{
86 $post['ctl00$MainContent$txtDOBDay'] = '';
87 $post['txtDOBDay'] = '';
88 }
89 if (isset($_POST['DOBYear'])){
90 //$post['DOBYear_field'] = 'ctl00$MainContent$txtDOBYear';
91 $post['ctl00$MainContent$txtDOBYear'] = $_POST['DOBYear'];
92 $post['txtDOBYear'] = $_POST['DOBYear'];
93 }else{
94 $post['ctl00$MainContent$txtDOBYear'] = '';
95 $post['txtDOBYear'] = '';
96 }
97 if (isset($_POST['SearchZipCode'])){
98 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
99 $post['ctl00$MainContent$txtSearchZipCode'] = $_POST['SearchZipCode'];
100 $post['txtSearchZipCode'] = $_POST['SearchZipCode'];
101 }else{
102 $post['ctl00$MainContent$txtSearchZipCode'] = '';
103 $post['txtSearchZipCode'] = '';
104 }
105 if (isset($_POST['SearchHouseNumber'])){
106 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
107 $post['ctl00$MainContent$txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
108 $post['txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
109 }else{
110 $post['ctl00$MainContent$txtSearchHouseNumber'] = '';
111 $post['txtSearchHouseNumber'] = '';
112 }
113 if (isset($_POST['SearchMiddleInitial'])){
114 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
115 $post['ctl00$MainContent$txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
116 $post['txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
117 }else{
118 $post['ctl00$MainContent$txtSearchMiddleInitial'] = '';
119 $post['txtSearchMiddleInitial'] = '';
120 }
121 // debug - show full response make sure we have the cookie
122 echo "<li>COOKIE ASP.NET_SessionId $cookie</li>";
123 echo "<li>POST VIEWSTATE $post[__VIEWSTATE]</li>";
124 echo "<li>POST VIEWSTATEGENERATOR $post[__VIEWSTATEGENERATOR]</li>";
125 echo "<li>POST VIEWSTATEENCRYPTED $post[__VIEWSTATEENCRYPTED]</li>";
126 echo "<li>POST EVENTVALIDATION $post[__EVENTVALIDATION]</li>";
127 echo "<pre>";
128 echo print_r($post);
129 echo "</pre>";
130 echo "<hr><hr><hr><h1>Voter Form</h1>";
131 echo htmlspecialchars($form['html']);
132 echo '<hr><hr><hr>';
133 //echo $form['html'];
134 ?>
135 <form method='POST'>
136 <table>
137 <tr>
138 <td>SearchFirstName</td><td><input name='SearchFirstName'></td>
139 </tr>
140 <tr>
141 <td>SearchLastName</td><td><input name='SearchLastName'></td>
142 </tr>
143 <tr>
144 <td>DOBMonth</td><td><input name='DOBMonth'></td>
145 </tr>
146 <tr>
147 <td>DOBDay</td><td><input name='DOBDay'></td>
148 </tr>
149 <tr>
150 <td>DOBYear</td><td><input name='DOBYear'></td>
151 </tr>
152 <tr>
153 <td>SearchZipCode</td><td><input name='SearchZipCode'></td>
154 </tr>
155 <tr>
156 <td>SearchHouseNumber*</td><td><input name='SearchHouseNumber'></td>
157 </tr>
158 <tr>
159 <td>SearchMiddleInitial*</td><td><input name='SearchMiddleInitial'></td>
160 </tr>
161 <tr>
162 <td>*Optional</td><td><input type='submit'></td>
163 </tr>
164 </table>
165 </form>
166 <?PHP
167 if ($post['txtSearchFirstName'] != ''){
168 $result['html'] = getPage($form['url'],$cookie,$post);
169 echo "<hr><hr><hr><h1>SBE API RESULTS</h1>";
170 echo htmlspecialchars($result['html']);
171 echo '<hr><hr><hr>';
172 echo $result['html'];
173 }
174 ?>