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 if (isset($_POST['SearchFirstName'])){
57 //$post['SearchFirstName_field'] = 'ctl00$MainContent$txtSearchFirstName';
58 $post['ctl00$MainContent$txtSearchFirstName'] = $_POST['SearchFirstName'];
59 $post['txtSearchFirstName'] = $_POST['SearchFirstName'];
60 }else{
61 $post['ctl00$MainContent$txtSearchFirstName'] = '';
62 $post['txtSearchFirstName']
63 }
64 if (isset($_POST['SearchLastName'])){
65 //$post['SearchLastName_field'] = 'ctl00$MainContent$txtSearchLastName';
66 $post['ctl00$MainContent$txtSearchLastName'] = $_POST['SearchLastName'];
67 $post['ctl00$MainContent$txtSearchLastName'] = $_POST['SearchLastName'];
68 }else{
69 $post['ctl00$MainContent$txtSearchLastName'] = '';
70 $post['txtSearchLastName'] = '';
71 }
72 if (isset($_POST['DOBMonth'])){
73 //$post['DOBMonth_field'] = 'ctl00$MainContent$txtDOBMonth';
74 $post['ctl00$MainContent$txtDOBMonth'] = $_POST['DOBMonth'];
75 $post['txtDOBMonth'] = $_POST['DOBMonth'];
76 }else{
77 $post['ctl00$MainContent$txtDOBMonth'] = '';
78 $post['txtDOBMonth'] = '';
79 }
80 if (isset($_POST['DOBDay'])){
81 //$post['DOBDay_field'] = 'ctl00$MainContent$txtDOBDay';
82 $post['ctl00$MainContent$txtDOBDay'] = $_POST['DOBDay'];
83 $post['txtDOBDay'] = $_POST['DOBDay'];
84 }else{
85 $post['ctl00$MainContent$txtDOBDay'] = '';
86 $post['txtDOBDay'] = '';
87 }
88 if (isset($_POST['DOBYear'])){
89 //$post['DOBYear_field'] = 'ctl00$MainContent$txtDOBYear';
90 $post['ctl00$MainContent$txtDOBYear'] = $_POST['DOBYear'];
91 $post['txtDOBYear'] = $_POST['DOBYear'];
92 }else{
93 $post['ctl00$MainContent$txtDOBYear'] = '';
94 $post['txtDOBYear'] = '';
95 }
96 if (isset($_POST['SearchZipCode'])){
97 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
98 $post['ctl00$MainContent$txtSearchZipCode'] = $_POST['SearchZipCode'];
99 $post['txtSearchZipCode'] = $_POST['SearchZipCode'];
100 }else{
101 $post['ctl00$MainContent$txtSearchZipCode'] = '';
102 $post['txtSearchZipCode'] = '';
103 }
104 if (isset($_POST['SearchHouseNumber'])){
105 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
106 $post['ctl00$MainContent$txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
107 $post['txtSearchHouseNumber'] = $_POST['SearchHouseNumber'];
108 }else{
109 $post['ctl00$MainContent$txtSearchHouseNumber'] = '';
110 $post['txtSearchHouseNumber'] = '';
111 }
112 if (isset($_POST['SearchMiddleInitial'])){
113 //$post['SearchZipCode_field'] = 'ctl00$MainContent$txtSearchZipCode';
114 $post['ctl00$MainContent$txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
115 $post['txtSearchMiddleInitial'] = $_POST['SearchMiddleInitial'];
116 }else{
117 $post['ctl00$MainContent$txtSearchMiddleInitial'] = '';
118 $post['txtSearchMiddleInitial'] = '';
119 }
120 // debug - show full response make sure we have the cookie
121 echo "<li>COOKIE ASP.NET_SessionId $cookie</li>";
122 echo "<li>POST VIEWSTATE $post[__VIEWSTATE]</li>";
123 echo "<li>POST VIEWSTATEGENERATOR $post[__VIEWSTATEGENERATOR]</li>";
124 echo "<li>POST VIEWSTATEENCRYPTED $post[__VIEWSTATEENCRYPTED]</li>";
125 echo "<li>POST EVENTVALIDATION $post[__EVENTVALIDATION]</li>";
126 echo "<pre>";
127 echo print_r($post);
128 echo "</pre>";
129 echo "<hr><hr><hr><h1>Voter Form</h1>";
130 echo htmlspecialchars($form['html']);
131 echo '<hr><hr><hr>';
132 //echo $form['html'];
133 ?>
134 <form method='POST'>
135 <table>
136 <tr>
137 <td>SearchFirstName</td><td><input name='SearchFirstName'></td>
138 </tr>
139 <tr>
140 <td>SearchLastName</td><td><input name='SearchLastName'></td>
141 </tr>
142 <tr>
143 <td>DOBMonth</td><td><input name='DOBMonth'></td>
144 </tr>
145 <tr>
146 <td>DOBDay</td><td><input name='DOBDay'></td>
147 </tr>
148 <tr>
149 <td>DOBYear</td><td><input name='DOBYear'></td>
150 </tr>
151 <tr>
152 <td>SearchZipCode</td><td><input name='SearchZipCode'></td>
153 </tr>
154 <tr>
155 <td>SearchHouseNumber*</td><td><input name='SearchHouseNumber'></td>
156 </tr>
157 <tr>
158 <td>SearchMiddleInitial*</td><td><input name='SearchMiddleInitial'></td>
159 </tr>
160 <tr>
161 <td>*Optional</td><td><input type='submit'></td>
162 </tr>
163 </table>
164 </form>
165 <?PHP
166 if ($post['SearchFirstName'] != ''){
167 $result['html'] = getPage($form['url'],$cookie,$post);
168 echo "<hr><hr><hr><h1>SBE API RESULTS</h1>";
169 echo htmlspecialchars($result['html']);
170 echo '<hr><hr><hr>';
171 echo $result['html'];
172 }
173 ?>