I have the following function for validating users facebook information against the rules setup in the database for users. Its working fine but i need to know if it can be more optimized. Few things that you need to know are:
- There is a possibility that particular rule would not exists in db.
- But if exists and does not match with user details it must redirect to error page.
- it is also possible that user has not set the information in their profile so in this case it should redirect to error page.
my dbdetails array has following structure is
Array( ['Demography'] => Array( ['Rule'] => Array( [0] => 'Some Value' [1] => 'Some Value'... ) ['Value'] => Array( [0] => 'Some Value' [1] => 'Some Value' ) ) )
And the fbdata is exactly same what facebook returns when user visits the page after allowing the application. Here is my function which validates/
private function validate_demographics($dbDetails, $fbData)
{
$this->load->library('facebook', array('appId' => APP_ID, 'secret' => APP_SECRET));
//Is page like restriction is there
if( in_array('People Who Like My Page', $dbDetails['Demography']['Value']) )
{
//Checks whether user has liked the page or not
if( !$fbData['page']['liked'] ) {
//If user has not liked the page set the network flag to 1.
redirect('viewer/demographics_not_match/1');
}
}
if( in_array('Age', $dbDetails['Demography']['Rule']) ) {
$ageInd = array_search('Age', $dbDetails['Demography']['Rule']);
$validAge = explode('-', $dbDetails['Demography']['Value'][$ageInd]);
$userBirthdate = explode('/',$this->viewer_fb_data['birthday']);
$userAge = date('Y') - $userBirthdate[2];
if( $userAge >= $validAge[0] && $userAge <= $validAge[1] ) {
return true;
} else {
redirect('viewer/demographics_not_match/2');
}
}
if( in_array('High-School', $dbDetails['Demography']['Rule']) or in_array('College', $dbDetails['Demography']['Rule']) ) {
//Checks whether user has set his educational details on facebook
if( array_key_exists('education', $this->viewer_fb_data) ) {
foreach( $this->viewer_fb_data['education'] as $userEducation )
{
if( in_array($userEducation['school']['name'], $dbDetails['Demography']['Value']) ) {
return true;
}
}
} else {
redirect('viewer/demographics_not_match/3');
}
}
if( in_array('City', $dbDetails['Demography']['Rule']) or in_array('State', $dbDetails['Demography']['Rule']) or in_array('Country', $dbDetails['Demography']['Rule']) )
{
//Checks whether user has set his location on facebook
if( array_key_exists('location', $this->viewer_fb_data) ) {
$userLocation = explode(',', $this->viewer_fb_data['location']['name']);
if( in_array($userLocation[0], $dbDetails['Demography']['Value']) ) {
return true;
}
} else {
redirect('viewer/demographics_not_match/5');
}
}
if( in_array('Work-Place', $dbDetails['Demography']['Rule']) )
{
//Checks whether user has set his work details on facebook
if( array_key_exists('work', $this->viewer_fb_data) ) {
$workInd = array_search('Work-Place', $dbDetails['Demography']['Rule']);
$workPlaces = explode(',', $dbDetails['Demography']['Value'][$workInd]);
foreach( $this->viewer_fb_data as $fbWork )
{
if( in_array($fbWork['employer']['name'], $workPlaces) ) {
return true;
}
}
} else {
redirect('viewer/demographics_not_match/4');
}
}
return false;
}