-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibe.php
More file actions
executable file
·313 lines (293 loc) · 9.09 KB
/
libe.php
File metadata and controls
executable file
·313 lines (293 loc) · 9.09 KB
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
<?php
require_once 'vendor/autoload.php';
/* PHP requires setting a timezone. This will be fine,
since the app doesn't require a time */
date_default_timezone_set('America/New_York');
$smarty = new Smarty();
$smarty->addTemplateDir(__DIR__ . '/templates');
$smarty->addPluginsDir(__DIR__ . '/plugins');
// $smarty->addPluginsDir(__DIR__ . '/plugins');
session_start();
if(isset($_SESSION['user_id'])) {
$user_id=$_SESSION['user_id'];
$smarty->assign('user_id',$user_id);
$smarty->assign('HelloName',$_SESSION['HelloName']);
}
else {
$user_id=0;
if(!isset($login)) {
header("Location: login.php");
}
}
// database connection
include 'config.php';
$msi = new mysqli($db_host, $db_user, $db_pw, $db_db);
/* Set permissions.
Viewer can see summary, Editor can see Details and Donations pages
*/
$rbac = new PhpRbac\Rbac();
/* not needed at this point - financial viewer can only see summary
$is_financial_viewer=
$rbac->Users->hasRole('Financial Information Viewer',$user_id);
$is_contact_editor=
$rbac->Users->hasRole('Financial Information Editor',$user_id);*/
$sitemenu=array();
if($rbac->Users->hasRole('Financial Information Editor',$user_id)) {
$sitemenu[]=array('d' => 'Summary','t' => 'summary', 'c' => 0);
$sitemenu[]=array('d' => 'Details', 't' => 'details', 'c' => 0);
$sitemenu[]=array('d' => 'Donations', 't' => 'donations', 'c' => 0);
$sitemenu[]=array('d' => 'Contact', 't' => 'contact', 'c' => 0);
$sitemenu[]=array('d' => 'Utility', 't' => 'utility', 'c' => 0);
$sitemenu[]=array('d' => 'Import', 't' => 'import', 'c' => 0);
foreach($sitemenu as &$sm) {
if (stripos(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH), $sm['t']))
{
$sm['c']=1;
}
else {
$sm['c']=0;
}
}
unset($sm);
}
$smarty->assign('sitemenu',$sitemenu);
function uSelect($msi,$query,$desc,&$ErrMsg) {
/* select query using mysqli->query */
$rv=array();
if(!$result=$msi->query($query)) {
buildErrorMessage($ErrMsg,$desc,$msi->error);
}
else {
while($tx=$result->fetch_assoc()) {
$rv[]=$tx;
}
$result->free();
}
return $rv;
}
function pSelect($msi,$query,$id,$desc,&$ErrMsg) {
/* select query with 1 id param, using mysqli->prepare,
stmt->bind_param, & stmt->execute */
$rv=array();
if(!$stmt=$msi->prepare($query)) {
buildErrorMessage($ErrMsg,"prep $desc",$msi->error);
}
else {
if(!$stmt->bind_param('i',$id)) {
buildErrorMessage($ErrMsg,"bind $desc",$msi->error);
}
else {
if(!$stmt->execute()) {
buildErrorMessage($ErrMsg,"exec $desc",$msi->error);
}
else {
$result=$stmt->get_result();
while($tx=$result->fetch_assoc()) {
$rv[]=$tx;
}
$result->free();
$stmt->close();
}
}
}
return $rv;
}
function array_matchfield($needle,$haystack,$field) {
foreach($haystack as $hx) {
if($hx[$field] == $needle) {
return true;
}
}
return false;
}
function getHouseholdFromContact($msi,$smarty,$cid) {
/* also gets contact name in case of error */
if($stmt=$msi->prepare(
"select h.household_id, c.first_name, c.primary_name
from contacts c
left join household_members hm
on hm.contact_id=c.contact_id
left join households h
on h.household_id=hm.household_id
where c.contact_id=?")) {
$stmt->bind_param('i',$cid);
$stmt->execute();
$result=$stmt->get_result();
$hx = $result->fetch_assoc();
$result->free();
$stmt->close();
}
else {
//$smarty->assign('footer',"getHouseholdFromContact: unable to create mysql statement object: ".$msi->error);
return 0;
}
if(is_null($hx['household_id'])) {
/*$smarty->assign('footer',
"No household for $cid ".$hx['first_name'].' '.$hx['primary_name']);*/
return 0;
}
return $hx['household_id'];
}
function isDupeHousehold($msi,$house_name,$notID=0,&$ErrMsg) {
// check if name is in use other than for household_id notID
if($stmt=$msi->prepare("select 0 from households h
where h.name=? and h.household_id!=?")) {
$stmt->bind_param('si',$house_name,$notID);
if($stmt->execute()) {
$result=$stmt->get_result();
$stmt->close;
$retval=$result->num_rows;
$result->free;
if($retval) {
return true;
}
else {
return false;
}
}
else {
buildErrorMessage($ErrMsg,
'exec dupe house query: ',$msi->error);
}
}
else {
buildErrorMessage($ErrMsg,
'prep dupe house query: ',$msi->error);
}
}
function buildErrorMessage(&$ErrMsg,$errortext,$sqlerror='') {
$ErrMsg[]=array('txt' => $errortext,'msg' => $sqlerror);
}
function displayFooter($smarty,$ErrMsg) {
/* footer will display if the smarty variable footer is set */
if(count($ErrMsg))$smarty->assign('footer',$ErrMsg);
}
function append_wc(&$list,$element) {
/* add comma if needed, then element */
$list .= (strlen($list) ? ', ' : '') . $element;
}
function trek_list($msi,$contact_id,&$ErrMsg) {
if($treksresult=$msi->query(
"select r.year,g.short_name,ifnull(ro.role,'') role
from roster_memberships rm
left join rosters r on r.roster_id=rm.roster_id
left join groups g on g.group_id=r.group_id
left join roles ro on ro.role_id=rm.role_id
where rm.contact_id=$contact_id
and g.group_id not in (61,77,86,58,11,25,54,62,53,55,57)
order by g.short_name, r.year")) {
//echo "trek count: ".$treksresult->num_rows."\n";
if(!$trx = $treksresult->fetch_assoc())return '';
$prev_group=$trx['short_name'];
$prev_role=$trx['role'];
$prev_year=$trx['year'];
$first_year=$prev_year;
$element='';
$le = array();
do {
$last_rec=is_null($trx = $treksresult->fetch_assoc());
$year=$trx['year'];
$group=$trx['short_name'];
$role=$trx['role'];
//echo "$year $group $role $first_year $prev_year $prev_group\n";
if($group == $prev_group && $role == $prev_role) {
if($year != ($prev_year+1)) {
append_wc($element,$first_year.'-'.substr($prev_year,2,2));
$first_year=$year;
}
}
else {
// different trek, write previous one
if($prev_year == $first_year) {
append_wc($element,$prev_year);
}
else {
if($first_year) {
append_wc($element,$first_year.'-'.substr($prev_year,2,2));
}
else {
append_wc($element,$prev_year);
}
}
$element .= " $prev_group";
if($prev_role != '')$element .= " $prev_role";
$le[]=$element;
$element='';
$first_year=$year;
}
$prev_year=$year;
$prev_group=$group;
$prev_role=$role;
} while(!$last_rec);
}
else {
// query error
buildErrorMessage($ErrMsg,'trek_list query: ',$msi->error);
}
sort($le);
foreach($le as $lx) append_wc($element,$lx);
return $element;
}
function email_list($msi,$household_id,&$ErrMsg) {
/* put all preferred emails in a string */
$elist='';
if($e_result=$msi->query(
'select et.email_type,e.email from preferred_emails pe '.
'inner join emails e on e.email_id=pe.email_id '.
'inner join email_types et on et.email_type_id=e.email_type_id '.
"where pe.household_id=$household_id")) {
$is_first=true;
while($ex=$e_result->fetch_assoc()) {
if(!$is_first)$elist.=', ';
$elist.=$ex['email_type'].': '.$ex['email'];
$is_first=false;
}
}
else {
// query error
buildErrorMessage($ErrMsg,'email_list query: ',$msi->error);
}
return $elist;
}
function phone_list($msi,$household_id,&$ErrMsg) {
/* put all household phone numbers in a string */
$plist='';
if($p_result=$msi->query(
'select pt.phone_type,p.number,p.formatted,c.first_name '.
'from household_members hm '.
'inner join contacts c on c.contact_id=hm.contact_id '.
'inner join phone_associations pa on pa.contact_id=hm.contact_id '.
'inner join phones p on p.phone_id=pa.phone_id '.
'inner join phone_types pt on pt.phone_type_id=p.phone_type_id '.
"where hm.household_id=$household_id")) {
$is_first=true;
while($px=$p_result->fetch_assoc()) {
if(!$is_first)$plist.=', ';
if($px['formatted']) {
$pn=$px['number'];
}
else {
$pn=substr($px['number'],0,3).'-'.substr($px['number'],3,3).'-'.
substr($px['number'],6);
}
$plist.=$px['phone_type'].": $pn (".$px['first_name'].')';
$is_first=false;
}
}
else {
// query error
buildErrorMessage($ErrMsg,'email_list query: ',$msi->error);
}
return $plist;
}
function distance ($lat1, $long1, $lat2, $long2) {
// this math is cribbed from 4 Guys From Rolla and is not verified
// http://www.4guysfromrolla.com/webtech/code/2points.asp.html
$x = (sin (deg2rad ($lat1)) * sin (deg2rad ($lat2)) + cos (deg2rad ($lat1)) * cos (deg2rad ($lat2)) * cos (abs ((deg2rad( $long2) - deg2rad ($long1)))));
$x = atan ((sqrt (1 - pow ($x, 2))) / $x);
return (1.852 * 60.0 * (($x / pi ()) * 180.0)) / 1.609344;
// statute miles: 1.609344
// nautical miles: 1.0
// kilometers: 1.852
}
?>