| [ PHPXref.com ] | [ Generated: Sun Jul 20 19:12:48 2008 ] | [ OSC 2.0.5 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /******************************************************************************* 3 * 4 * filename : /Include/Functions.php 5 * last change : 2003-01-07 6 * 7 * http://osc.sourceforge.net 8 * 9 * This product is based upon work previously done by Infocentral (infocentral.org) 10 * on their PHP version Church Management Software that they discontinued 11 * and we have taken over. We continue to improve and build upon this product 12 * in the direction of excellence. 13 * 14 * OpenSourceChurch (OSC) is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * Any changes to the software must be submitted back to the OpenSourceChurch project 20 * for review and possible inclusion. 21 * 22 * Copyright 2001-2003 Deane Barker, Chris Gebhardt 23 ******************************************************************************/ 24 25 // Initialization common to all InfoCentral scripts 26 27 // Set error reporting 28 if ($debug == true) 29 Error_reporting ( E_ALL ^ E_NOTICE); 30 else 31 error_reporting(0); 32 33 // Establish the database connection 34 $cnInfoCentral = mysql_connect($sSERVERNAME,$sUSER,$sPASSWORD); 35 mysql_select_db($sDATABASE); 36 37 // Initialize the session 38 session_start(); 39 40 // 41 // Basic security checks: 42 // 43 if (!$bSuppressSessionTests) // This is used for the login page only. 44 { 45 // Basic security: If the UserID isn't set (no session), redirect to the login page 46 if (!isset($_SESSION['iUserID'])) 47 { 48 Redirect("Default.php"); 49 exit; 50 } 51 52 // Check for login timeout. If login has expired, redirect to login page 53 if ($sSessionTimeout > 0) 54 { 55 if ((time() - $_SESSION['tLastOperation']) > $sSessionTimeout) 56 { 57 Redirect("Default.php?timeout"); 58 exit; 59 } 60 else { 61 $_SESSION['tLastOperation'] = time(); 62 } 63 } 64 65 // If this user needs to change password, send to that page 66 if ($_SESSION['bNeedPasswordChange'] && !isset($bNoPasswordRedirect)) 67 { 68 Redirect("UserPasswordChange.php?PersonID=" . $_SESSION['iUserID']); 69 exit; 70 } 71 } 72 // End of basic security checks 73 74 // If Magic Quotes is turned off, do the same thing manually.. 75 if (!$_SESSION['bHasMagicQuotes']) 76 { 77 foreach ($_REQUEST as $key=>$value) $value = addslashes($value); 78 } 79 80 // Constants 81 $aPropTypes = array( 82 1 => gettext("True / False"), 83 2 => gettext("Date"), 84 3 => gettext("Text Field (50 char)"), 85 4 => gettext("Text Field (100 char)"), 86 5 => gettext("Text Field (long)"), 87 6 => gettext("Year"), 88 7 => gettext("Season"), 89 8 => gettext("Number"), 90 9 => gettext("Person from Group"), 91 10 => gettext("Money"), 92 11 => gettext("Phone Number"), 93 12 => gettext("Custom Drop-Down List") 94 ); 95 96 // Are they adding anything to the People Cart? 97 if (isset($_GET["AddToPeopleCart"])) { 98 AddToPeopleCart(FilterInput($_GET["AddToPeopleCart"],'int')); 99 $sGlobalMessage = gettext("Selected record successfully added to the Cart."); 100 } 101 102 // Are they removing anything from the People Cart? 103 if (isset($_GET["RemoveFromPeopleCart"])) { 104 RemoveFromPeopleCart(FilterInput($_GET["RemoveFromPeopleCart"],'int')); 105 $sGlobalMessage = gettext("Selected record successfully removed from the Cart."); 106 } 107 108 // Are they emptying their cart? 109 if ($_GET["Action"] == "EmptyCart") { 110 unset($_SESSION['aPeopleCart']); 111 $sGlobalMessage = gettext("Your cart has been successfully emptied."); 112 } 113 114 if (isset($_POST["BulkAddToCart"])) { 115 116 $aItemsToProcess = explode(",",$_POST["BulkAddToCart"]); 117 118 if (isset($_POST["AndToCartSubmit"])) 119 { 120 if (isset($_SESSION['aPeopleCart'])) 121 $_SESSION['aPeopleCart'] = array_intersect($_SESSION['aPeopleCart'],$aItemsToProcess); 122 } 123 elseif (isset($_POST["NotToCartSubmit"])) 124 { 125 if (isset($_SESSION['aPeopleCart'])) 126 $_SESSION['aPeopleCart'] = array_diff($_SESSION['aPeopleCart'],$aItemsToProcess); 127 } 128 else 129 { 130 for ($iCount = 0; $iCount < count($aItemsToProcess); $iCount++) { 131 AddToPeopleCart(str_replace(",","",$aItemsToProcess[$iCount])); 132 } 133 $sGlobalMessage = $iCount . " " . gettext("item(s) added to the Cart."); 134 } 135 } 136 137 // 138 // Some very basic functions that all scripts use 139 // 140 141 // Convert a relative URL into an absolute URL and redirect the browser there. 142 function Redirect($sRelativeURL) 143 { 144 global $sRootPath; 145 146 if (!$_SESSION['bSecureServer']) 147 { 148 $sProtocol = "http://"; 149 if ($_SESSION['iServerPort'] != 80) 150 $sPort = ":" . $_SESSION['iServerPort']; 151 else 152 $sPort = ""; 153 } 154 else 155 { 156 $sProtocol = "https://"; 157 if ($_SESSION['iServerPort'] != 443) 158 $sPort = ":" . $_SESSION['iServerPort']; 159 else 160 $sPort = ""; 161 } 162 163 header("Location: " . $sProtocol . $_SERVER['HTTP_HOST'] . $sPort . $sRootPath . "/" . $sRelativeURL); 164 } 165 166 // Runs an SQL query. Returns the result resource. 167 // By default stop on error, unless a second (optional) argument is passed as false. 168 function RunQuery($sSQL, $bStopOnError = true) 169 { 170 global $cnInfoCentral; 171 global $debug; 172 173 if ($result = mysql_query($sSQL, $cnInfoCentral)) 174 return $result; 175 elseif ($bStopOnError) 176 { 177 if ($debug) 178 die(gettext("Cannot execute query.") . "<p>$sSQL<p>" . mysql_error()); 179 else 180 die("Database error or invalid data"); 181 } 182 } 183 184 // Sanitizes user input as a security measure 185 // Optionally, a filtering type and size may be specified. By default, strip any tags from a string. 186 function FilterInput($sInput,$type = 'string',$size = 1) 187 { 188 if (strlen($sInput) > 0) 189 { 190 switch($type) { 191 case 'string': 192 // or use htmlspecialchars( stripslashes( )) 193 return strip_tags(trim($sInput)); 194 case 'htmltext': 195 return strip_tags(trim($sInput),'<a><b><i><u>'); 196 case 'char': 197 return substr(trim($sInput),0,$size); 198 case 'int': 199 return (int) trim($sInput); 200 case 'float': 201 return (float) trim($sInput); 202 } 203 } 204 else 205 { 206 return ""; 207 } 208 } 209 210 // 211 // Adds a person to a group with specified role. 212 // Returns false if the operation fails. (such as person already in group) 213 // 214 function AddToGroup($iPersonID, $iGroupID, $iRoleID) 215 { 216 global $cnInfoCentral; 217 218 // Was a RoleID passed in? 219 if ($iRoleID == 0) { 220 // No, get the Default Role for this Group 221 $sSQL = "SELECT grp_DefaultRole FROM group_grp WHERE grp_ID = " . $iGroupID; 222 $rsRoleID = RunQuery($sSQL); 223 $Row = mysql_fetch_row($rsRoleID); 224 $iRoleID = $Row[0]; 225 } 226 227 $sSQL = "INSERT INTO person2group2role_p2g2r (p2g2r_per_ID, p2g2r_grp_ID, p2g2r_rle_ID) VALUES (" . $iPersonID . ", " . $iGroupID . ", " . $iRoleID . ")"; 228 $result = RunQuery($sSQL,false); 229 230 if ($result) 231 { 232 // Check if this group has special properties 233 $sSQL = "SELECT grp_hasSpecialProps FROM group_grp WHERE grp_ID = " . $iGroupID; 234 $rsTemp = RunQuery($sSQL); 235 $rowTemp = mysql_fetch_row($rsTemp); 236 $bHasProp = $rowTemp[0]; 237 238 if ($bHasProp == 'true') 239 { 240 $sSQL = "INSERT INTO `groupprop_" . $iGroupID . "` (`per_ID`) VALUES ('" . $iPersonID . "')"; 241 RunQuery($sSQL); 242 } 243 } 244 245 return $result; 246 } 247 248 function RemoveFromGroup($iPersonID, $iGroupID) 249 { 250 $sSQL = "DELETE FROM person2group2role_p2g2r WHERE p2g2r_per_ID = " . $iPersonID . " AND p2g2r_grp_ID = " . $iGroupID; 251 RunQuery($sSQL); 252 253 // Check if this group has special properties 254 $sSQL = "SELECT grp_hasSpecialProps FROM group_grp WHERE grp_ID = " . $iGroupID; 255 $rsTemp = RunQuery($sSQL); 256 $rowTemp = mysql_fetch_row($rsTemp); 257 $bHasProp = $rowTemp[0]; 258 259 if ($bHasProp == 'true') 260 { 261 $sSQL = "DELETE FROM `groupprop_" . $iGroupID . "` WHERE `per_ID` = '" . $iPersonID . "'"; 262 RunQuery($sSQL); 263 } 264 265 // Reset any group specific property fields of type "Person from Group" with this person assigned 266 $sSQL = "SELECT grp_ID, prop_Field FROM groupprop_master WHERE type_ID = 9 AND prop_Special = " . $iGroupID; 267 $result = RunQuery($sSQL); 268 while ($aRow = mysql_fetch_array($result)) 269 { 270 $sSQL = "UPDATE groupprop_" . $aRow['grp_ID'] . " SET " . $aRow['prop_Field'] . " = NULL WHERE " . $aRow['prop_Field'] . " = " . $iPersonID; 271 RunQuery($sSQL); 272 } 273 274 // Reset any custom person fields of type "Person from Group" with this person assigned 275 $sSQL = "SELECT custom_Field FROM person_custom_master WHERE type_ID = 9 AND custom_Special = " . $iGroupID; 276 $result = RunQuery($sSQL); 277 while ($aRow = mysql_fetch_array($result)) 278 { 279 $sSQL = "UPDATE person_custom SET " . $aRow['custom_Field'] . " = NULL WHERE " . $aRow['custom_Field'] . " = " . $iPersonID; 280 RunQuery($sSQL); 281 } 282 } 283 284 function ConvertCartToString($aCartArray) 285 { 286 // Implode the array 287 $sCartString = implode(",", $aCartArray); 288 289 // Make sure the comma is chopped off the end 290 if (substr($sCartString, strlen($sCartString) - 1, 1) == ",") { 291 $sCartString = substr($sCartString, 0, strlen($sCartString) - 1); 292 } 293 294 // Make sure there are no duplicate commas 295 $sCartString = str_replace(",,", "", $sCartString); 296 297 return $sCartString; 298 } 299 300 301 /****************************************************************************** 302 * Returns the proper information to use for a field. 303 * Person info overrides Family info if they are different. 304 * If using family info and bFormat set, generate HTML tags for text color red. 305 * If neither family nor person info is available, return an empty string. 306 *****************************************************************************/ 307 308 function SelectWhichInfo($sPersonInfo, $sFamilyInfo, $bFormat = false) 309 { 310 global $bShowFamilyData; 311 312 if ($bShowFamilyData) { 313 314 if ($bFormat) { 315 $sFamilyInfoBegin = "<span style=\"color: red;\">"; 316 $sFamilyInfoEnd = "</span>"; 317 } 318 319 if ($sPersonInfo != "") { 320 return $sPersonInfo; 321 } elseif ($sFamilyInfo != "") { 322 if ($bFormat) { 323 return $sFamilyInfoBegin . $sFamilyInfo . $sFamilyInfoEnd; 324 } else { 325 return $sFamilyInfo; 326 } 327 } else { 328 return ""; 329 } 330 331 } else { 332 if ($sPersonInfo != "") 333 return $sPersonInfo; 334 else 335 return ""; 336 } 337 } 338 339 // 340 // Returns the correct address to use via the sReturnAddress arguments. 341 // Function value returns 0 if no info was given, 1 if person info was used, and 2 if family info was used. 342 // We do address lines 1 and 2 in together because seperately we might end up with half family address and half person address! 343 // 344 function SelectWhichAddress(&$sReturnAddress1, &$sReturnAddress2, $sPersonAddress1, $sPersonAddress2, $sFamilyAddress1, $sFamilyAddress2, $bFormat = false) 345 { 346 global $bShowFamilyData; 347 348 if ($bShowFamilyData) { 349 350 if ($bFormat) { 351 $sFamilyInfoBegin = "<span style=\"color: red;\">"; 352 $sFamilyInfoEnd = "</span>"; 353 } 354 355 if ($sPersonAddress1 || $sPersonAddress2) { 356 $sReturnAddress1 = $sPersonAddress1; 357 $sReturnAddress2 = $sPersonAddress2; 358 return 1; 359 } elseif ($sFamilyAddress1 || $sFamilyAddress2) { 360 if ($bFormat) { 361 if ($sFamilyAddress1) 362 $sReturnAddress1 = $sFamilyInfoBegin . $sFamilyAddress1 . $sFamilyInfoEnd; 363 else $sReturnAddress1 = ""; 364 if ($sFamilyAddress2) 365 $sReturnAddress2 = $sFamilyInfoBegin . $sFamilyAddress2 . $sFamilyInfoEnd; 366 else $sReturnAddress2 = ""; 367 return 2; 368 } else { 369 $sReturnAddress1 = $sFamilyAddress1; 370 $sReturnAddress2 = $sFamilyAddress2; 371 return 2; 372 } 373 } else { 374 $sReturnAddress1 = ""; 375 $sReturnAddress2 = ""; 376 return 0; 377 } 378 379 } else { 380 if ($sPersonAddress1 || $sPersonAddress2) { 381 $sReturnAddress1 = $sPersonAddress1; 382 $sReturnAddress2 = $sPersonAddress2; 383 return 1; 384 } else { 385 $sReturnAddress1 = ""; 386 $sReturnAddress2 = ""; 387 return 0; 388 } 389 } 390 } 391 392 function ConvertMySQLDate($datestr) 393 { 394 if (strlen($datestr)) { 395 list($year,$month,$day,$hour,$minute,$second) = split("([^0-9])",$datestr); 396 return date("U",mktime($hour,$minute,$second,$month,$day,$year)); 397 } else { 398 return ""; 399 } 400 } 401 402 function ChopLastCharacter($sText) 403 { 404 return substr($sText,0,strlen($sText) - 1); 405 } 406 407 408 function AddToPeopleCart($sID) 409 { 410 // make sure the cart array exists 411 if(isset($_SESSION['aPeopleCart'])) 412 { 413 if (!in_array($sID, $_SESSION['aPeopleCart'], false)) { 414 $_SESSION['aPeopleCart'][] = $sID; 415 } 416 } 417 else 418 $_SESSION['aPeopleCart'][] = $sID; 419 } 420 421 function RemoveFromPeopleCart($sID) 422 { 423 // make sure the cart array exists 424 if(isset($_SESSION['aPeopleCart'])) 425 { 426 while ($element = each($_SESSION['aPeopleCart'])) { 427 if ( $element[value] == $sID ) { 428 unset( $_SESSION['aPeopleCart'][$element[key]] ); 429 break; 430 } 431 } 432 } 433 } 434 435 // this might be cruft 436 function FormatDate($dDate, $bWithTime) 437 { 438 /*if (strlen($dDate) < 14) { 439 return $dDate; 440 } else { 441 //$dDate = mysql_to_epoch($dDate); 442 443 if ($bWithTime) { 444 return date("n/j/Y h:i:s a",$dDate); 445 } else { 446 return date("n/j/Y",$dDate); 447 } 448 } 449 */ 450 return $dDate; 451 } 452 453 // this might be cruft 454 function mysql_to_epoch($datestr) 455 { 456 list($year, $month, $day, $hour, $minute, $second) = split("([^0-9])", $datestr); 457 return date("U", mktime($hour, $minute, $second, $month, $day, $year)); 458 } 459 460 function AlternateRowStyle($sCurrentStyle) 461 { 462 if ($sCurrentStyle == "RowColorA") { 463 return "RowColorB"; 464 } else { 465 return "RowColorA"; 466 } 467 } 468 469 function ConvertToBoolean($sInput) 470 { 471 if (empty($sInput)) { 472 return False; 473 } else { 474 if (is_numeric($sInput)) { 475 if ($sInput == 1) { 476 return True; 477 } else { 478 return False; 479 } 480 } 481 else 482 { 483 $sInput = strtolower($sInput); 484 if (in_array($sInput,array("true","yes","si"))) { 485 return true; 486 } else { 487 return false; 488 } 489 } 490 } 491 } 492 493 function ConvertFromBoolean($sInput) 494 { 495 if ($sInput) { 496 return 1; 497 } else { 498 return 0; 499 } 500 } 501 502 // 503 // Collapses a formatted phone number as long as the Country is known 504 // Eg. for United States: 555-555-1212 Ext. 123 ==> 5555551212e123 505 // 506 // Need to add other countries besides the US... 507 // 508 function CollapsePhoneNumber($sPhoneNumber,$sPhoneCountry) 509 { 510 switch ($sPhoneCountry) { 511 512 case "United States": 513 $sCollapsedPhoneNumber = ""; 514 $bHasExtension = false; 515 516 // Loop through the input string 517 for ($iCount = 0; $iCount <= strlen($sPhoneNumber); $iCount++) { 518 519 // Take one character... 520 $sThisCharacter = substr($sPhoneNumber, $iCount, 1); 521 522 // Is it a number? 523 if (Ord($sThisCharacter) >= 48 && Ord($sThisCharacter) <= 57) { 524 // Yes, add it to the returned value. 525 $sCollapsedPhoneNumber .= $sThisCharacter; 526 } 527 // Is the user trying to add an extension? 528 else if (!$bHasExtension && ($sThisCharacter == "e" || $sThisCharacter == "E")) { 529 // Yes, add the extension identifier 'e' to the stored string. 530 $sCollapsedPhoneNumber .= "e"; 531 // From now on, ignore other non-digits and process normally 532 $bHasExtension = true; 533 } 534 } 535 break; 536 537 default: 538 $sCollapsedPhoneNumber = $sPhoneNumber; 539 break; 540 } 541 542 return $sCollapsedPhoneNumber; 543 } 544 545 546 // 547 // Expands a collapsed phone number into the proper format for a known country. 548 // 549 // If, during expansion, an unknown format is found, the original will be returned 550 // and the a boolean flag $bWeird will be set. Unfortunately, because PHP does not 551 // allow for pass-by-reference in conjunction with a variable-length argument list, 552 // a dummy variable will have to be passed even if this functionality is unneeded. 553 // 554 // Need to add other countries besides the US... 555 // 556 function ExpandPhoneNumber($sPhoneNumber,$sPhoneCountry,&$bWeird) 557 { 558 $bWeird = false; 559 $length = strlen($sPhoneNumber); 560 561 switch ($sPhoneCountry) { 562 563 case "United States": 564 565 if ($length == 0) 566 return ""; 567 568 // 7 digit phone # with extension 569 else if (substr($sPhoneNumber,7,1) == "e") 570 return substr($sPhoneNumber,0,3) . "-" . substr($sPhoneNumber,3,4) . " Ext." . substr($sPhoneNumber,8,6); 571 572 // 10 digit phone # with extension 573 else if (substr($sPhoneNumber,10,1) == "e") 574 return substr($sPhoneNumber,0,3) . "-" . substr($sPhoneNumber,3,3) . "-" . substr($sPhoneNumber,6,4) . " Ext." . substr($sPhoneNumber,11,6); 575 576 else if ($length == 7) 577 return substr($sPhoneNumber,0,3) . "-" . substr($sPhoneNumber,3,4); 578 579 else if ($length == 10) 580 return substr($sPhoneNumber,0,3) . "-" . substr($sPhoneNumber,3,3) . "-" . substr($sPhoneNumber,6,4); 581 582 // Otherwise, there is something weird stored, so just leave it untouched and set the flag 583 else 584 { 585 $bWeird = true; 586 return $sPhoneNumber; 587 } 588 589 break; 590 591 // If the country is unknown, we don't know how to format it, so leave it untouched 592 default: 593 return $sPhoneNumber; 594 } 595 } 596 597 // 598 // Prints age in years, or in months if less than one year old 599 // 600 function PrintAge($Month,$Day,$Year) 601 { 602 if ($Year > 0) 603 { 604 if ($Year == date("Y")) 605 { 606 $monthCount = date("m") - $Month; 607 if ($Day > date("d")) 608 $monthCou