aboutsummaryrefslogtreecommitdiffstats
path: root/mayor-orig/www/include/share/net/facebook.php
blob: 74721a0a3a1f7390606401afd239ea3d8d0679b0 (plain)
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
<?php

//__FBCONNECT_ENABLED===true 

function mayorFacebookAuth() {

	if (__FBCONNECT_ENABLED !== true ) return false;

	if (version_compare(PHP_VERSION, '5.4.0', '<')) {
	    $_SESSION['alert'][] = 'info::facebook:szerver konfigurációs hiba, legalább 5.4-es php verzió szükséges!';
	    return false;
	} else {
	    /* facebook login start */
	    require_once ('include/share/facebook/autoload.php');
	    $fb = new Facebook\Facebook(array(
    		'app_id' => __FB_APP_ID,
    		'app_secret' => __FB_APP_SECRET,
    		'default_graph_version' => 'v2.5',
	    ));
	    $helper = $fb->getJavaScriptHelper();
	    try {
    		$accessToken = $helper->getAccessToken();
	    } catch(Facebook\Exceptions\FacebookResponseException $e) {
    		// When Graph returns an error
    		$_SESSION['alert'][] = 'info::facebook gráf hiba: ' . $e->getMessage();
	    } catch(Facebook\Exceptions\FacebookSDKException $e) {
    		// When validation fails or other local issues
    		$_SESSION['alert'][] = 'info::facebook SDK hiba: ' . $e->getMessage();
	    }
	    if (isset($accessToken)) {
    		$_SESSION['facebook_access_token'] = (string) $accessToken;
    		$_SESSION['facebook_access_token_object'] = $accessToken;
		try {
    		    $response = $fb->get('/me?fields=id,name,email',$accessToken);
		    $userNode = $response->getGraphUser();
		    $fbUserId = $userNode->getField('id');
		    //$fbUserCn = $userNode->getField('name');
		    //$fbUserEmail = $userNode->getField('email');
		} catch (Facebook\Exceptions\FacebookSDKException $e) {
		    $_SESSION['alert'][] = 'info::facebook gráf hiba: ' . $e->getMessage();
		    return false;
		}
    		/* mayor auth start */
		$accountInformation=array();
		$toPolicy = 'public';
		$data = getUserByFbUserId($fbUserId);
		if ($data === false) {
		    $_SESSION['alert'][] = 'info:Nincs ilyen user (még) a MaYoR-ral összekötve, kérjük jelentkezz be jelszóval!';
		} elseif (is_array($data)) {
		    // Ha van, akkor ki az? Mert ő bemehet.
		    return array('userAccount'=>$data['userAccount'],'toPolicy'=>$data['policy'],'fbUserEmail'=>$data['fbUserEmail'],'studyId'=>$data['studyId'],'fbUserCn'=>$data['fbUserCn'],'accessToken'=>$accessToken);
		}
		/* mayor auth stop */
	    } else {
		$_SESSION['alert'][] = 'info::facebook:nem érvényes accessToken';
	    }
	    /* facebook login stop */
	}
        return false;
}

function getUserByFbUserId($fbUserId) {
        if ($fbUserId=='') return false;
        $q = "SELECT * FROM facebookConnect WHERE fbUserId='%s' ORDER BY policy LIMIT 1";
        $v = array('fbUserId'=>$fbUserId);
        $record = db_query($q,array('fv'=>'getUserByFbUserId','modul'=>'login','result'=>'record','values'=>$v));
        return $record;
}

?>