This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.php
320 lines (272 loc) · 11.8 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/************************************************************************
*
* utils.php
*
* a collection of utilities used by the rest of the expungement generator
*
* Copyright 2011-2015 Community Legal Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************/
// checks to see if a person is logged in
// @return TRUE if the person is logged in and false if not
function isLoggedIn()
{
if(isset($_SESSION['loginUserID']))
return TRUE;
else
return FALSE;
}
// @return if the user is logged in, returns the logged in user; otherwise returns null
function getLoggedInUserName()
{
return ($_SESSION['loginUserFirst'] . " " . $_SESSION['loginUserLast']);
}
// print a key only if it is set in the "GET" variables
function printIfSet($key)
{
if(isset($_GET[$key]))
print $_GET[$key];
else if (isset($_POST[$key]))
print $_POST[$key];
}
// gets a "person" from the post vars; adds the person to the session
// @return a hash with each value from the get vars escaped etc... to be html and sql safe
function getPersonFromPostOrSession($data = FALSE)
{
if($data===FALSE)
$data = $_POST;
if ($GLOBALS['debug'])
{
print "POST VARS: <br />";
foreach ($_POST as $name=>$value)
{
print "$name: $value <br/>";
}
}
if (isset($data["personFirst"]))
$_SESSION['urlPerson']['First'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personFirst"])));
if (isset($data["personLast"]))
$_SESSION['urlPerson']['Last'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personLast"])));
if (isset($data["personDOB"]))
$_SESSION['urlPerson']['DOB'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personDOB"])));
if (isset($data["personStreet"]))
$_SESSION['urlPerson']['Street'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personStreet"])));
if (isset($data["personStreet"]))
$_SESSION['urlPerson']['City'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personCity"])));
if (isset($data["personStreet"]))
$_SESSION['urlPerson']['State'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personState"])));
if (isset($data["personStreet"]))
$_SESSION['urlPerson']['Zip'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personZip"])));
if (isset($data["personStreet"]))
$_SESSION['urlPerson']['SSN'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($data["personSSN"])));
return $_SESSION['urlPerson'];
}
// there is some informaiton that we want to get from getVars. The last page of the EG
// where the cases are displayed allows for a person to click on a result and
// output an Act 5 or pardon petition. The vars used to do that are stored here
function getInfoFromGetVars($data = FALSE)
{
if($data===FALSE)
$data = $_POST;
if (isset($_GET['docket']))
$_SESSION['docket'] = explode("|", $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_GET["docket"]))));
$_SESSION['act5Regardless'] = isset($_GET['act5Regardless']) || isset($data['act5Regardless']);
$_SESSION['expungeRegardless'] = isset($_GET['expungeRegardless']) || isset($data['expungeRegardless']);
$_SESSION['zipOnly'] = isset($data['zipOnly']) || isset($_GET['zipOnly']);
}
// gets vars passed in from LS
// @return a hash with each value from the get vars escaped etc... to be html and sql safe
function getLSVars()
{
//
if ($GLOBALS['debug'])
{
print "POST VARS: <br />";
foreach ($_POST as $name=>$value)
{
print "$name: $value <br/>";
}
}
$LSVars = array();
if (isSet($_POST["personFirst"])) $LSVars['First'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personFirst"])));
if (isSet($_POST["personLast"])) $LSVars['Last'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personLast"])));
if (isSet($_POST["personStreet"])) $LSVars['Street'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personStreet"])));
if (isSet($_POST["personCity"])) $LSVars['City'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personCity"])));
if (isSet($_POST["personState"])) $LSVars['State'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personState"])));
if (isSet($_POST["personZip"])) $LSVars['Zip'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personZip"])));
if (isSet($_POST["personSID"])) $LSVars['SID'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personSID"])));
if (isSet($_POST["personPP"])) $LSVars['PP'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personPP"])));
if (isSet($_POST["personSSN"])) $LSVars['SSN'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["personSSN"])));
if (isSet($_POST["personLSUser"])) $LSVars['LSUser'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["LSUser"])));
if (isSet($_POST["personLSPass"])) $LSVars['LSPass'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["LSPass"])));
if (isSet($_POST["Token"])) $LSVars['Token'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["Token"])));
if (isSet($_POST["debug"])) $LSVars['debug'] = $GLOBALS['db']->real_escape_string(htmlspecialchars(stripslashes($_POST["debug"])));
return $LSVars;
}
// resets all of the EG based session vars except login info
function resetSession()
{
unset($_SESSION['urlPerson']);
unset($_SESSION['docket']);
unset($_SESSION['scrapedDockets']);
}
// print index if exists
function printVar($array, $index)
{
if (isSet($array["$index"]))
print $array["$index"];
}
// zips all of the files in the array and returns the location of the zipfile.
// @return the name of the zipfile archive or null if there was a problem making the zipfile.
function zipFiles($files, $dataDir, $dockets, $fileName)
{
$zip = new ZipArchive();
$zipFileName = $dataDir . $fileName . ".zip";
if ($zip->open($zipFileName, ZipArchive::OVERWRITE|ZipArchive::CREATE)===TRUE )
{
foreach ($files as $index=>$file)
{
if($zip->addFile($file, basename($file)) && $GLOBALS['debug'])
print "added $file to archive <br />";
}
foreach ($dockets['userFile']['tmp_name'] as $key=>$docket)
{
$zip->addFile($docket, "dockets" . DIRECTORY_SEPARATOR . $dockets['userFile']['name'][$key]);
}
if ($zip->close())
return $zipFileName;
}
// if we couldn't open the zip file or save the zip file, return null
return NULL;
}
// removes all of the files in $files from the OS
function cleanupFiles($files)
{
foreach ($files as $file)
{
if (file_exists($file))
{
try
{
unlink($file);
}
catch (Exception $e) {}
}
}
}
// calculates date1-date2 in years
function dateDifference($date1, $date2)
{
$difference = ((int)$date1->format('Y')) - ((int)$date2->format('Y'));
// now check to see if date2 is later in the year than date1
// (z = number of days since jan 1)
if (((int)$date1->format('z')) > ((int)$date2->format('z')))
return $difference;
else
return $difference-1;
}
// @return a date in the form YYYY-MM-DD
// @param a date in the form MM/DD/YYYY
function dateConvert($docketDate)
{
if (preg_match("/\d{1,2}\/\d{1,2}\/\d{2,4}/",$docketDate))
{
$mysqlDate = new DateTime($docketDate);
return $mysqlDate->format('Y-m-d');
}
else
return ("0000-00-00");
}
// @return bool True if a file with the case id passed in exists in the pdf file dir, false if not
// @param id - the ID of the expungement that we are looking for a PDF for
function doesPDFExistForCaseId($id)
{
$filename = $GLOBALS['docketSheetsDir'] . $id;
if(file_exists($filename))
return TRUE;
else
return FALSE;
}
// @return serves a binary file from the data directory
// @param filename - the relative filename
function serveFile($filename)
{
# run basename on filename to make sure we aren't getting any directory input, like ../config.php or something
$file = $GLOBALS['dataDir'] . basename($filename);
if(!file_exists($file))
{
header ("HTTP/1.0 404 Not Found");
return;
}
else
{
$size=filesize($file);
header('HTTP/1.0 200 OK');
header('Content-Description: File Transfer');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Expires: 0');
header('Accept-Ranges: bytes');
header('Content-Length:' . $size);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile($file);
}
}
// takes a list of names in the form "('First', 'Last'), ('First', 'Last'),..." and runs a query to get all
// of the expungements prepared for those people
function createTrackingSpreadsheet($names, $programid)
{
// remove newlines from teh name list
$namelist = preg_split('/\r\n|[\r\n]/', $names);
// create a temporary file and open it; insert the headers for teh CSV
$filename = $GLOBALS['dataDir'] . time() . ".csv";
$csv = fopen($filename, 'w');
fputcsv($csv, array("First", "Last", "Case", "OTN", "Arrest Date", "E/R", "Order", "PSP", "Philly", "Docket", "Summary"));
// prepare the sql statement; this will be run through for each name.
if ($psql = $GLOBALS['db']->prepare("
SELECT d.firstname as 'First', d.lastname as 'Last', a.docketNumPrimary as 'Case', a.OTN as 'OTN',
DATE_FORMAT(a.arrestDate, '%m/%d/%Y') as 'Arrest Date',
if(e.isExpungement+e.isRedaction = 2, 'R', 'E') as 'E/R' FROM defendant as d left join
expungement as e on d.defendantID = e.defendantID left join arrest as a on e.arrestid = a.arrestid
LEFT JOIN userinfo on e.userid=userinfo.userid WHERE d.firstname=? AND d.lastname=?
AND e.isRedaction+e.isSummaryExpungement > 0 AND programid=" . $programid . "
ORDER BY d.lastName, d.firstname"))
{
// bind the first and lst name as the variables that will be inserted
$psql->bind_param("ss", $first, $last);
// run through each of the names in the list and run the query on each
foreach ($namelist as $name)
{
// be sure to first split the name into first and last, removing any whitespace
list($first, $last) = array_map('trim', explode(",", $name));
$psql->execute();
$psql->bind_result($f, $l, $docket, $otn, $arrest_date, $type);
// fetch each result for this person and put it into the CSV file
while($psql->fetch())
{
fputcsv($csv, array($f,$l,$docket,$otn,$arrest_date,$type,"","","","",""));
}
}
fclose($csv);
return $filename;
}
// if there was some sort of problem preparing the statement, send an error to the screen
else
return "aasdf" . $GLOBALS['db']->error;
}