Find A Life Group2018-11-27T20:25:09+00:00
"; // suppress error messages so API key won't accidentally be exposed if(!$mydbg) libxml_use_internal_errors(true); // set these to appropriate API credentials & hostname $api_user = 'groups'; $api_pass = 'Ki53m3dC5n2sb'; $api_host = 'rockfamily.ccbchurch.com'; $api = "https://$api_host/api.php"; $campuses = array( "H" => "Huntsville Campus", "M" => "Madison Campus", "F" => "Fayetteville Campus", "SL" => "Smith Lake Campus", "SH" => "Shoals Campus", ); global $apos,$rn; // avoid using backslash escapes when embedding in wordpress $apos = chr(39); // apostrophe $rn = chr(13) . chr(10); // newline (carriage-return + linefeed) global $logo; $logo = "http://therockfamily.org/wp-content/uploads/2018/07/rock_logo.jpg"; function ccbapi($srv,$postdata=false) { global $script_url,$api_user,$api_pass,$api_host,$api,$mydbg; if($mydbg) echo '
ccbapi('.$srv.')

'; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERPWD, $api_user.':'.$api_pass); if($postdata) { if($mydbg) echo "
POST mode:
".$postdata."

"; curl_setopt($ch, CURLOPT_POST, 4); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); } curl_setopt($ch, CURLOPT_URL, $api."?srv=".$srv); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); if(!$data) { if($mydbg) echo "(retry 1)
"; $data = curl_exec($ch); } if(!$data) { if($mydbg) echo "(retry 2)
"; sleep(1); $data = curl_exec($ch); } if(!$data) { if($mydbg) echo "(retry 3)
"; sleep(3); $data = curl_exec($ch); } curl_close($ch); if($data) { list($hdata, $rdata) = explode("\r\n\r\n", $data, 2); $hdata = explode("\n",$hdata); $headers = []; $headers['status'] = $hdata[0]; array_shift($hdata); foreach($hdata as $part){ $middle=explode(":",$part); $headers[trim($middle[0])] = trim($middle[1]); } if($headers["Retry-After"] > 0) { if($mydbg) echo "waiting for API retry-after(" .$headers["Retry-After"] .")...
"; sleep($headers["Retry-After"]); return ccbapi($srv,$postdata); // todo: need to protect against infinite recursion // todo: generate log/email to notify limit was exceeded } else if($headers["X-RateLimit-Remaining"] / $headers["X-RateLimit-Limit"] < 0.5) { if($mydbg) echo "waiting for API rate reset(" .($headers["X-RateLimit-Reset"] - time()) .")...
"; sleep($headers["X-RateLimit-Reset"] - time()); /* todo: possibly more robust approach: Each time an API call is made, write an XML file to a folder in /tmp with service name as filename containing limit, remaining, reset, retry data. On next API call, check for presence of XML file with that filename. Check remaining vs limit and reset/retry values. sleep before next call if needed. */ } if($mydbg) { echo "
";
echo "current time()=".time()."\n";
print_r($headers);
echo htmlentities($rdata);
echo "
"; } return simplexml_load_string($rdata); } else { echo "An unexpected database error occured.
"; if($mydbg) { echo "curl_exec(): " .curl_error($ch) ." (".curl_errno($ch).")
"; } } } function my_campus_code($gname) { if(preg_match("/^LG[A-Z] /",$gname)) return(substr($gname,2,1)); if(preg_match("/^LG[A-Z][A-Z] /",$gname)) return(substr($gname,2,2)); return(""); } function my_group_name($gname) { if(preg_match("/^LG[A-Z] /",$gname)) return(substr($gname,4)); if(preg_match("/^LG[A-Z][A-Z] /",$gname)) return(substr($gname,5)); return($gname); } function my_header() { global $logo; echo '



Life Groups are one of the primary ways we make friends and grow in our faith here at The Rock. From monthly activity groups to weekly bible studies, come connect into community and join a Life Group!

'; } function my_search() { global $script_url,$campuses,$mydbg; if($mydbg) echo '
my_search()...
'; my_header(); echo '
STEP 1
STEP 2
STEP 3

Want to dig deeper? Advanced Search

'; } function my_adv_search() { global $campuses,$mydbg; if($mydbg) echo '
my_adv_search()...
'; my_header(); echo '

ADVANCED SEARCH




'; /* by request, hard-coding this to just dept 8 - Life Groups $xml = ccbapi("group_grouping_list"); //echo "
".htmlentities($xml->asXML())."
"; echo ""; echo "
"; //foreach($xml->response->items->item as $item) { // echo "".$item->name."
"; //} echo "
"; */ //echo ""; /* (by request, hard-coding type list to just 4 selected values...) $xml = ccbapi("group_type_list"); //echo "
".htmlentities($xml->asXML())."
"; echo ""; echo "
"; //foreach($xml->response->items->item as $item) { // echo "".$item->name."
"; //} echo "
"; */ echo '








'; /* (use simpler checkboxes for days, then parse in find script) $xml = ccbapi("meet_day_list"); echo ""; echo "
"; //foreach($xml->response->items->item as $item) { // echo "".$item->name."
"; //} echo "
"; */ echo '

Mon
Tue
Wed
Thu
Fri
Sat
Sun



Childcare available




'; } function my_find() { global $script_url,$api_host,$campuses,$mydbg; if(!$mydbg) $searchparms = "department_id=8"; //if($_GET['type_id']) // $searchparms .= "&type_id=".$_GET['type_id']; if($_GET['area_id']) $searchparms .= "&area_id=".preg_replace("/[^0-9]/", "", $_GET['area_id']); if($_GET['udf_pulldown_1_id']) $searchparms .= "&udf_pulldown_1_id=".preg_replace("/[^0-9]/", "", $_GET['udf_pulldown_1_id']); if($_GET['udf_pulldown_2_id']) { $g = preg_replace("/[^0-9]/", "", $_GET['udf_pulldown_2_id']); if($g == '3') $g = 0; $searchparms .= "&udf_pulldown_2_id=".$g; } if($_GET['childcare']) $searchparms .= "&childcare=".preg_replace("/[^0-9]/", "", ($_GET['childcare'])); if( $_GET['meet_mon'] || $_GET['meet_tue'] || $_GET['meet_wed'] || $_GET['meet_thu'] || $_GET['meet_fri'] || $_GET['meet_sat'] || $_GET['meet_sun'] ) $search_days = true; $xml = ccbapi("group_search&".$searchparms); if($xml->response->items['count'] > 0) { echo "
"; echo "

SEARCH RESULTS

"; foreach($xml->response->items->item as $item) { $name = my_group_name($item->name); $campus = my_campus_code($item->name); if($_GET['campus']) { if(strlen($campus)==0) continue; if($campus!=preg_replace("/[^A-Za-z0-9]/", "", $_GET['campus'])) continue; } if($_GET['type_id']) { $type_id = preg_replace("/[^0-9]/", "", $_GET['type_id']); $type = $item->group_type_name; if($type_id==3 && $type != "Study") continue; if($type_id==8 && $type != "Support") continue; if($type_id==1 && $type != "Connect" && $type != "Activity") continue; } if($search_days) { if(!strlen($item->meet_day_name)) continue; if(strpos($item->meet_day_name,'Monday') !== false && $_GET['meet_mon'] != 1) continue; if(strpos($item->meet_day_name,'Tuesday') !== false && $_GET['meet_tue'] != 1) continue; if(strpos($item->meet_day_name,'Wednesday') !== false && $_GET['meet_wed'] != 1) continue; if(strpos($item->meet_day_name,'Thursday') !== false && $_GET['meet_thu'] != 1) continue; if(strpos($item->meet_day_name,'Friday') !== false && $_GET['meet_fri'] != 1) continue; if(strpos($item->meet_day_name,'Saturday') !== false && $_GET['meet_sat'] != 1) continue; if(strpos($item->meet_day_name,'Sunday') !== false && $_GET['meet_sun'] != 1) continue; } echo "
"; echo "Request to Join
"; echo "
"; echo "
"; echo "".$name.""; if(strlen($campus) != 0) { echo ""; echo " ($campuses[$campus])"; echo ""; } echo "
"; echo ""; if(strlen($item->meet_day_name)) { echo $item->meet_day_name; if(strlen($item->meet_time_name)) echo " @ ".$item->meet_time_name; if(strlen($item->area_name)) echo " - "; } if(strlen($item->area_name)) echo $item->area_name; echo ""; echo "
"; echo "
"; echo $item->description; echo "
"; echo ""; echo "Contact Leader: " .$item->owner_name.""; echo ""; echo "
"; echo "
"; echo "

"; $found_count++; } echo "
"; } if(!$found_count) { echo ""; echo "No matches were found, please try again"; echo ""; my_search(); } } function my_details() { global $script_url,$api_host,$campuses; $xml = ccbapi("group_profile_from_id&id=".preg_replace("/[^0-9]/", "", $_GET['group_id'])."&include_image_link=true"); //echo "
".htmlentities($xml->asXML())."
"; echo "

GROUP DETAILS

"; foreach($xml->response->groups->group as $group) { // todo: check public_search_listed & inactive flags $name = my_group_name($group->name); $campus = $campuses[my_campus_code($group->name)]; echo ""; echo $name; echo ""; if(isset($campus)) { echo ""; echo " ($campus)"; echo ""; } echo "
"; echo ""; if(strlen($group->meeting_day)) { echo $group->meeting_day; if(strlen($group->meeting_time)) echo " @ ".$group->meeting_time; if(strlen($group->area)) echo " - "; } if(strlen($group->area)) echo $group->area; echo ""; echo "
"; echo "
"; echo "
"; echo ""; echo ""; echo "
"; echo $group->description; echo "
"; echo "
"; echo "Contact Leader: " .$group->main_leader->full_name.""; echo "
"; echo "
"; foreach($group->user_defined_fields->user_defined_field as $udf) { echo $udf->label; echo ": "; echo $udf->selection; echo "
"; } if($group->childcare_provided=="true") { echo "Childcare available"; } echo "
"; echo "
"; } echo "
"; echo "Request to Join
"; //$class = ($class=='even') ? 'odd' : 'even'; //echo ""; } function my_join_form() { global $mydbg; if($mydbg) echo '
my_join_form()...
'; if(isset($_GET['group_id'])) $gid=preg_replace("/[^0-9]/", "",$_GET['group_id']); if(isset($_POST['group_id'])) $gid=preg_replace("/[^0-9]/", "",$_POST['group_id']); if(!isset($gid)) echo "Error: no group specified"; else { $xml = ccbapi("group_profile_from_id&id=".$gid); if($mydbg) echo '
my_join_form(): after ccbapi
'; foreach($xml->response->groups->group as $group) { if($mydbg) echo '
my_join_form(): in foreach
'; $name = my_group_name($group->name); echo '

REQUEST TO JOIN GROUP:
'.$name.'

'.($mydbg ? ' ' : '').'
Your First Name:(One person at a time)
Your Last Name:
email address:(xxxx@xxxxx.xxx)
confirm email:(xxxx@xxxxx.xxx)
mobile phone:(###-###-####)
'; if($mydbg) echo '
my_join_form(): end foreach
'; break; } } } function my_join_submit() { //sanitize/validate input global $script_url,$api_host,$apos,$rn,$logo,$mydbg; if($mydbg) echo '
my_join_submit()...
'; my_header(); echo '
'; if(!isset($_POST['group_id']) or strlen($_POST['group_id']) == 0 or $_POST['group_id'] == 0) $formErr[] = "no group specified"; if(!isset($_POST['first_name']) or strlen($_POST['first_name']) == 0 or !isset($_POST['last_name']) or strlen($_POST['last_name']) == 0) $formErr[] = "Please enter both your first and last name"; if(!preg_match("/^[-".$apos."a-z ]*$/i", stripslashes($_POST['first_name'].$_POST['last_name'])) ) $formErr[] = 'First and Last name may contain only letters, hyphens and apostrophes'; if(!isset($_POST['email']) or !preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $_POST['email']) ) $formErr[] = 'Please enter a valid email address'; if(!($_POST['email'] == $_POST['email2'])) $formErr[] = "email address confirmation does not match"; if(!isset($_POST['mobile_phone']) or !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $_POST['mobile_phone']) ) $formErr[] = 'Please enter a valid phone number (###-###-####)'; if(isset($formErr)) { echo ""; echo "Please correct the following error(s):
"; echo "
"; foreach($formErr as $e) echo $e."
"; my_join_form(); } else { if($mydbg) echo "(all input validated)"; //check for matching names $indiv_parms = "first_name=".urlencode(stripslashes($_POST['first_name']))."&last_name=".urlencode(stripslashes($_POST['last_name'])); $xml = ccbapi("individual_search&".$indiv_parms); //check email/phone for matches foreach($xml->response->individuals->individual as $item) { foreach($item->phones->phone as $ph) { if($ph['type'] == "mobile") { $phone = preg_replace("/[^0-9]/", "", $ph); break; } } if((preg_replace("/[^0-9]/", "",$_POST['mobile_phone']) == $phone) or (strtolower($_POST['email']) == strtolower($item->email))) { $id = $item['id']; $name = $_POST['first_name']." ".$_POST['last_name']; $email = $_POST['email']; //if matched but either phone or email are different, email db tech and update db if((preg_replace("/[^0-9]/", "",$_POST['mobile_phone']) != $phone) or ($_POST['email'] != $item->email)) { $from = "lifegroups@therockfwc.org"; $to = "lwillis@therockfwc.org"; if($mydbg) $to="jon@diercks.net"; $subject = "CCB database updated for ".$_POST['first_name']." ".$_POST['last_name']; $message = ' CCB database update requested for '.$_POST['first_name']." ".$_POST['last_name'].'

CCB database update is requested for '.$_POST['first_name']." ".$_POST['last_name'].'

While requesting to join group '.$gname.', '. $_POST['first_name']." ".$_POST['last_name'].' submitted an email address and/or phone number that did not match the existing data. This submitted data:

'.preg_replace("/[^0-9]/", "",$_POST['mobile_phone']).' '.$_POST['email'].'

would replace this previous database data:

'.$phone.' '.$item->email.'



Please update the database manually if appropriate.

'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . $rn; $headers .= 'Content-type: text/html; charset=iso-8859-1' . $rn; // Additional headers // $headers .= "To: $to" . $rn; $headers .= "From: $from" . $rn; // Mail it mail($to, $subject, $message, $headers); /* if($mydbg) echo "
updating individual...
"; $xml = ccbapi("update_individual&individual_id=".$id,"mobile_phone=".urlencode($_POST['mobile_phone'])."&email=".urlencode($_POST['email'])); */ } break; } } //create new individual if needed if(!isset($id)) { if($mydbg) echo "
creating new individual...
"; $xml = ccbapi("create_individual",$indiv_parms."&mobile_phone=".urlencode($_POST['mobile_phone'])."&email=".urlencode($_POST['email'])); $id = $xml->response->individuals->individual['id']; $name = stripslashes($xml->response->individuals->individual->full_name); $email = $xml->response->individuals->individual->email; } $gid = preg_replace("/[^0-9]/","",$_POST['group_id']); if($mydbg) echo "
id=$id
email=$email
"; //submit join request $xml = ccbapi("add_individual_to_group&id=$id&group_id=$gid&status=add"); if($xml->response->groups->group['id']==$gid) { $gname = my_group_name($xml->response->groups->group->name); $leader_id = $xml->response->groups->group->main_leader['id']; $leader_name = $xml->response->groups->group->main_leader->full_name; $leader_email = $xml->response->groups->group->main_leader->email; ccbapi("add_individual_to_queue&individual_id=$id&queue_id=3126"); //send confirmation email to requestor $from = "lifegroups@therockfwc.org"; $to = $email; $subject = "Your request to join $gname"; $message = ' Your request to join '.$gname.'

Thanks for signing up for the '.$gname.' group!

You will be contacted within a couple days by the Life Group leader to give you the details.

(If you don' . $apos . 't receive anything within 5 days, please ' .'click here to Contact Group leader.)



'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . $rn; $headers .= 'Content-type: text/html; charset=iso-8859-1' . $rn; // Additional headers // $headers .= "To: $to" . $rn; $headers .= "From: $from" . $rn; // Mail it mail($to, $subject, $message, $headers); //send notificattion email to group leader $to = $leader_email; if($mydbg) $to="jon@diercks.net"; $subject = "New request to join $gname"; $message = ' New request to join '.$gname.'

You have someone interested in joining your group: '.$gname.'

PLEASE TEXT AND CALL THEM WITHIN 48 HOURS of this email. Welcome them and let them know the day, time, and location of your next meeting.

' .''.$_POST['first_name'].' '.$_POST['last_name'].'
' .$_POST['mobile_phone'].'
' .''.$email.'

' .' CLICK HERE to go to your group page.


'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . $rn; $headers .= 'Content-type: text/html; charset=iso-8859-1' . $rn; // Additional headers // $headers .= "To: $to" . $rn; $headers .= "From: $from" . $rn; // Mail it mail($to, $subject, $message, $headers); //display confirmation echo '

Thanks for signing up for the '.$gname.' group!

You will be contacted within a couple days by your Life Group leader to give you the details.

(Please ' .'click here to contact your group leader with any questions.)


TO MAKE SURE YOUR LEADER CAN CONTACT YOU...

1. Please go to your email inbox
2. Add the domain @ccbchurch.com to your email "safe list"
3. If you need help, click here to "white-list" our emails


'; } else { echo "We're sorry, an unexpected error occured. We cannot process your request to join this group at this time.
"; } } echo '
'; } //main if(isset($_GET['submit'])) my_find(); else if(isset($_POST['join'])) my_join_submit(); else if(isset($_GET['join'])) my_join_form(); else if($_GET['group_id']) my_details(); else if(isset($_GET['advanced_search'])) my_adv_search(); else my_search(); if($mydbg) echo "
...end
"; ?>