We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17412
    • 270 Posts
    Thanks Etal, really appreciate your quick reply. Got this to work using the following code

    <?php
    // ref: http://modxcms.com/forums/index.php?topic=63340.0
    // also on FACEBOOK after creating the app > EDIT PAGE > MANAGE PERMISSIONS > SET new page as landing.
    //your application secret goes here
    $app_secret = "00000000000000000000"; // change to your app secret
    
    $output = "General error";
    $signed_request = $_REQUEST['signed_request'];
    
    //read signed request
    if(!$signed_request === true){
    	$output = "Error no page requested";
    } else {
    	$data = parse_signed_request($signed_request, $app_secret);
    	
    	if(!$data === true){
    		$output = "Error invalid signature";
    	} else {
    		$page = $data['page'];
    		if($page == null){
    			$output = "Error no page requested";
    		} else {
    			$fbpageid = $page['id']; //the requested page id
    			$fbadmin = $page['admin']; //user is page admin
    			$fbfan = $page['liked']; //user is page fan
    			
    			if($fbfan == "1" && !isset($_SESSION['visited'])){
    				$_SESSION['visited'] = true;
    				$output = $modx->documentObject['content'];
    			} else {
    				if ($fbfan != "1")
    				{
    					unset($_SESSION['visited']);
    				}
    				
    				$docid = (isset($docid))? $docid: $modx->documentIdentifier;
    				$document_tvs=$modx->getTemplateVarOutput(array('fangate'), $docid);
    				$output = $document_tvs['fangate'];
    			}
    		}	
    	}
    }
    
    return $output;
    
    function parse_signed_request($signed_request, $secret) {
      list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
    
      // decode the data
      $sig = base64_url_decode($encoded_sig);
      $data = json_decode(base64_url_decode($payload), true);
    
      if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
        error_log('Unknown algorithm. Expected HMAC-SHA256');
        return null;
      }
    
      // check sig
      $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
      if ($sig !== $expected_sig) {
        error_log('Bad Signed JSON signature!');
        return null;
      }
    
      return $data;
    }
    
    function base64_url_decode($input) {
      return base64_decode(strtr($input, '-_', '+/'));
    }
    ?>