-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-w.php
More file actions
148 lines (129 loc) · 5.58 KB
/
index-w.php
File metadata and controls
148 lines (129 loc) · 5.58 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
//start session
session_start();
//Include Twitter config file && User class
//include_once 'twConfig.php';
include 'EpiTwitter/EpiCurl.php';
include 'EpiTwitter/EpiOAuth.php';
include 'EpiTwitter/EpiTwitter.php';
include 'EpiTwitter/TwitterConfig.php';
include_once 'User.php';
//echo $redirectURL;die;
//If OAuth token not matched
if(isset($_REQUEST['oauth_token']) && $_SESSION['token'] !== $_REQUEST['oauth_token']){
//Remove token from session
unset($_SESSION['token']);
unset($_SESSION['token_secret']);
}
//If user already verified
if(isset($_SESSION['status']) && $_SESSION['status'] == 'verified' && !empty($_SESSION['request_vars'])){
//Retrive variables from session
$username = $_SESSION['request_vars']['screen_name'];
$twitterId = $_SESSION['request_vars']['user_id'];
$oauthToken = $_SESSION['request_vars']['oauth_token'];
$oauthTokenSecret = $_SESSION['request_vars']['oauth_token_secret'];
$profilePicture = $_SESSION['userData']['picture'];
/*
* Prepare output to show to the user
*/
$twClient = new EpiTwitter($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
//If user submits a tweet to post to twitter
if(isset($_POST["updateme"])){
$my_update = $twClient->post('statuses/update', array('status' => $_POST["updateme"]));
}
//Display username and logout link
$output = '<div class="welcome_txt">Welcome <strong>'.$username.'</strong> (Twitter ID : '.$twitterId.'). <a href="logout.php">Logout</a>!</div>';
//Display profile iamge and tweet form
$output .= '<div class="tweet_box">';
$output .= '<img src="'.$profilePicture.'" width="120" height="110"/>';
$output .= '<form method="post" action=""><table width="200" border="0" cellpadding="3">';
$output .= '<tr>';
$output .= '<td><textarea name="updateme" cols="60" rows="4"></textarea></td>';
$output .= '</tr>';
$output .= '<tr>';
$output .= '<td><input type="submit" value="Tweet" /></td>';
$output .= '</tr></table></form>';
$output .= '</div>';
//Get latest tweets
$myTweets = $twClient->get('statuses/user_timeline', array('screen_name' => $username, 'count' => 5));
//Display the latest tweets
$output .= '<div class="tweet_list"><strong>Latest Tweets : </strong>';
$output .= '<ul>';
foreach($myTweets as $tweet){
$output .= '<li>'.$tweet->text.' <br />-<i>'.$tweet->created_at.'</i></li>';
}
$output .= '</ul></div>';
}elseif(isset($_REQUEST['oauth_token']) && $_SESSION['token'] == $_REQUEST['oauth_token']){
//Call Twitter API
$twClient = new EpiTwitter($consumerKey, $consumerSecret, $_SESSION['token'] , $_SESSION['token_secret']);
//Get OAuth token
$access_token = $twClient->getAccessToken($_REQUEST['oauth_verifier']);
//If returns success
if($twClient->http_code == '200'){
//Storing access token data into session
$_SESSION['status'] = 'verified';
$_SESSION['request_vars'] = $access_token;
//Get user profile data from twitter
$userInfo = $twClient->get('account/verify_credentials');
//Initialize User class
$user = new User();
//Insert or update user data to the database
$name = explode(" ",$userInfo->name);
$fname = isset($name[0])?$name[0]:'';
$lname = isset($name[1])?$name[1]:'';
$profileLink = 'https://twitter.com/'.$userInfo->screen_name;
$twUserData = array(
'oauth_provider'=> 'twitter',
'oauth_uid' => $userInfo->id,
'first_name' => $fname,
'last_name' => $lname,
'email' => '',
'gender' => '',
'locale' => $userInfo->lang,
'picture' => $userInfo->profile_image_url,
'link' => $profileLink,
'username' => $userInfo->screen_name
);
$userData = $user->checkUser($twUserData);
//Storing user data into session
$_SESSION['userData'] = $userData;
//Remove oauth token and secret from session
unset($_SESSION['token']);
unset($_SESSION['token_secret']);
//Redirect the user back to the same page
header('Location: ./');
}else{
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
}
}else{
//Fresh authentication
$twClient = new EpiTwitter($consumerKey, $consumerSecret);
$request_token = $twClient->getRequestToken($redirectURL);
//Received token info from twitter
$_SESSION['token'] = $request_token->oauth_token;
$_SESSION['token_secret'] = $request_token->oauth_token_secret;
//If authentication returns success
if($twClient->http_code == '200'){
//Get twitter oauth url
$authUrl = $twClient->getAuthorizeURL($request_token['oauth_token']);
//Display twitter login button
$output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="images/TwitterButton.png" width="151" height="24" border="0" /></a>';
}else{
$output = '<h3 style="color:red">Error connecting to twitter! try again later!</h3>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login with Twitter using PHP by CodexWorld</title>
<link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<!-- Display login button / profile information -->
<?php echo $output; ?>
</body>
</html>