block bots
[clinton/MarylandElectronicPetitionSignature.git] / api / maryland_voter.php
1 <?PHP
2 /*
3 ini_set('display_errors', 1);
4 ini_set('display_startup_errors', 1);
5 error_reporting(E_ALL);
6 */
7 include_once('/var/www/secure.php'); //outside webserver
8
9
10 function cut_part_out($start,$end,$whole){
11 $parts = explode($start,$whole);
12 $subparts = explode($end,$parts[1]);
13 $out = $subparts[0];
14 return $out;
15 }
16
17 function MVgetPage($url,$cookie,$post){
18 $url = str_replace('[month]',date('F'),$url); // replace month January through December
19 $url = str_replace('[day]',date('j'),$url); // replace day 1 to 31
20 $url = str_replace('[yesterday]',date('j',strtotime('yesterday')),$url); // replace day 1 to 31
21 $url = str_replace('[year]',date('Y'),$url); // replace year Examples: 1999 or 2003
22 $curl = curl_init();
23 curl_setopt ($curl, CURLOPT_URL, $url);
24 curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("McGuire MEPS https://www.md-petition.com/ /%d.0",rand(4,50)));
25 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
26 curl_setopt ($curl, CURLOPT_HEADER, 1);
27 curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
28 curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
29 if ($cookie != ''){
30 curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Cookie: ASP.NET_SessionId=$cookie")); // use cookies
31 }
32 /*
33 $postfields = array(
34 'upload_file' => '@file_to_upload.png',
35 'upload_text' => '@text_to_upload'
36 );
37 */
38 if ($post != ''){
39 curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
40 }
41 $html = curl_exec ($curl);
42 curl_close ($curl);
43 return $html;
44 }
45
46 function md_voter_lookup($SearchFirstName,$SearchLastName,$DOBMonth,$DOBDay,$DOBYear,$SearchZipCode,$SearchHouseNumber,$SearchMiddleInitial){
47 global $petition;
48 $post['ctl00$MainContent$btnSearch'] = "Search";
49 $post['btnSearch'] = "Search";
50 $post['ctl00$MainContent$listLanguages'] = "en";
51 $post['listLanguages'] = "en";
52 if ($SearchFirstName == ''){
53 die();
54 }
55 $post['ctl00$MainContent$txtSearchFirstName'] = $SearchFirstName;
56 $post['txtSearchFirstName'] = $SearchFirstName;
57 $post['ctl00$MainContent$txtSearchLastName'] = $SearchLastName;
58 $post['txtSearchLastName'] = $SearchLastName;
59 $post['ctl00$MainContent$txtDOBMonth'] = $DOBMonth;
60 $post['txtDOBMonth'] = $DOBMonth;
61 $post['ctl00$MainContent$txtDOBDay'] = $DOBDay;
62 $post['txtDOBDay'] = $DOBDay;
63 $post['ctl00$MainContent$txtDOBYear'] = $DOBYear;
64 $post['txtDOBYear'] = $DOBYear;
65 $post['ctl00$MainContent$txtSearchZipCode'] = $SearchZipCode;
66 $post['txtSearchZipCode'] = $SearchZipCode;
67 $post['ctl00$MainContent$txtSearchHouseNumber'] = $SearchHouseNumber;
68 $post['txtSearchHouseNumber'] = $SearchHouseNumber;
69 $post['ctl00$MainContent$txtSearchMiddleInitial'] = $SearchMiddleInitial;
70 $post['txtSearchMiddleInitial'] = $SearchMiddleInitial;
71 // start a session to get a cookie
72 $form['url'] = 'https://voterservices.elections.maryland.gov/VoterSearch';
73 $form['html'] = MVgetPage($form['url'],'','');
74 // extract the cookie from the header (see CURLOPT_HEADER)
75 $cookie = cut_part_out('ASP.NET_SessionId=',';',$form['html']);
76 // extract the form elements we will need to post with our data
77 $post['__VIEWSTATE'] = cut_part_out('id="__VIEWSTATE" value="','"',$form['html']);
78 $post['__VIEWSTATEGENERATOR'] = cut_part_out('id="__VIEWSTATEGENERATOR" value="','"',$form['html']);
79 $post['__VIEWSTATEENCRYPTED'] = cut_part_out('id="__VIEWSTATEENCRYPTED" value="','"',$form['html']);
80 $post['__EVENTVALIDATION'] = cut_part_out('id="__EVENTVALIDATION" value="','"',$form['html']);
81 $result['html'] = MVgetPage($form['url'],$cookie,$post);
82 //echo "<h1>STEP 2: SBE RESULTS</h1>";
83 $return['debug'] = htmlspecialchars($result['html']);
84 $return['html'] = $result['html'];
85 $html = $petition->real_escape_string($result['html']);
86 $petition->query("INSERT INTO `RemoteVoterList` (`date_validated`, `txtSearchFirstName`, `txtSearchLastName`, `txtDOBMonth`, `txtDOBDay`, `txtDOBYear`, `txtSearchZipCode`, `txtSearchHouseNumber`, `txtSearchMiddleInitial`, `sbe_response`)
87 VALUES (now(), '$SearchFirstName', '$SearchLastName', '$DOBMonth', '$DOBDay', '$DOBYear', '$SearchZipCode', '$SearchHouseNumber', '$SearchMiddleInitial', '$html')");
88 return $result['html'];
89 }
90 /*
91 if (isset($_POST['SearchFirstName'])){
92 $voter = md_voter_lookup($_POST['SearchFirstName'],$_POST['SearchLastName'],$_POST['DOBMonth'],$_POST['DOBDay'],$_POST['DOBYear'],$_POST['SearchZipCode'],$_POST['SearchHouseNumber'],$_POST['SearchMiddleInitial']);
93 echo $voter['html'];
94 }
95 ?>
96 <form method='POST'>
97 <table>
98 <tr>
99 <td>SearchFirstName</td><td><input name='SearchFirstName'></td>
100 </tr>
101 <tr>
102 <td>SearchLastName</td><td><input name='SearchLastName'></td>
103 </tr>
104 <tr>
105 <td>DOBMonth</td><td><input name='DOBMonth'></td>
106 </tr>
107 <tr>
108 <td>DOBDay</td><td><input name='DOBDay'></td>
109 </tr>
110 <tr>
111 <td>DOBYear</td><td><input name='DOBYear'></td>
112 </tr>
113 <tr>
114 <td>SearchZipCode</td><td><input name='SearchZipCode'></td>
115 </tr>
116 <tr>
117 <td>SearchHouseNumber*</td><td><input name='SearchHouseNumber'></td>
118 </tr>
119 <tr>
120 <td>SearchMiddleInitial*</td><td><input name='SearchMiddleInitial'></td>
121 </tr>
122 <tr>
123 <td>*Optional</td><td><input type='submit'></td>
124 </tr>
125 </table>
126 </form>
127 */
128