~ubuntu-branches/debian/sid/ampache/sid

« back to all changes in this revision

Viewing changes to modules/twitter/twitter_works.php

  • Committer: Package Import Robot
  • Author(s): Charlie Smotherman
  • Date: 2013-08-27 13:19:48 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20130827131948-1czew0zxn6u70dtv
Tags: 3.6-rzb2752+dfsg-1
* New upsteam snapshot.  Contains important bug fixes to the installer.
* Correct typo in ampache-common.postrm.
* Remove courtousy copy of php-getid3, during repack.  Closes: #701526
* Update package to use dh_linktree to make the needed sym links to the
  needed system libs that were removed during repack.
* Update debian/rules to reflect upstreams removing/moving of modules.
* Update debian/ampache-common.install to reflect upstreams removal of files.
* Updated to use new apache2.4 API. Closes: #669756
* Updated /debian/po/de.po thx David Prévot for the patch.  Closes:  #691963
* M3U import is now ordered, fixed upstream.  Closes: #684984
* Text input area has been resized so IPv6 addresses will now fit, fixed
  upstream.  Closes:  #716230
* Added ampache-common.preinst to make sure that the courtousy copies of code
  dirs are empty so dh_linktree can do it's magic on upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
 
2
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
2
3
/**
3
 
 * Twitter Works
4
4
 *
5
5
 * Adapted for Ampache by Chris Slamar
6
 
 *
 
6
 * FIXME: V. suspicious about this whole "Adapted" thing
7
7
 *
8
8
 * LICENSE: GNU General Public License, version 2 (GPLv2)
9
 
 * Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
 
9
 * Copyright 2001 - 2013 Ampache.org
10
10
 *
11
11
 * This program is free software; you can redistribute it and/or
12
12
 * modify it under the terms of the GNU General Public License v2
21
21
 * along with this program; if not, write to the Free Software
22
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
23
 *
24
 
 * @author      Chris Slamar
25
 
 * @copyright   2001 - 2011 Ampache.org
26
 
 * @license     http://opensource.org/licenses/gpl-2.0 GPLv2
27
 
 * @link        http://www.ampache.org/
28
24
 */
29
25
 
30
 
        require_once '../../lib/init.php';
31
 
        require_once( Config::get('prefix') . "/modules/twitter/twitteroauth/twitteroauth.php");
32
 
 
33
 
        session_start();
34
 
        
35
 
        if(!empty($_SESSION['twitterusername'])) {
36
 
                header('Location: ' . Config::Get('web_path') . '/modules/twitter/twitter_update.php');
37
 
                debug_event("Twitter", "Twitter user has logged in this session.", "5");
38
 
        }
39
 
 
40
 
        if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){
41
 
                debug_event("Twitter", "Got all 3 pieces for auth", "5");
42
 
        } else {
43
 
                if( $_SESSION['twitterCount'] < 4 ) {
44
 
                        debug_event("Twitter", "Didn't get all 3 auth pieces, going to try again.  Try #" . $_SESSION['twitterCount'], "5");
45
 
                        $_SESSION['twitterCount']++;
46
 
                        header('Location: ' . Config::Get('web_path') . '/modules/twitter/twitter_login.php');
47
 
                } else {
48
 
                        debug_event("Twitter", "Failed to auth too many times.  Giving up.", "5");
49
 
                        header('Location: ' . Config::Get('web_path') );
50
 
                }
51
 
        }
52
 
 
53
 
        // TwitterOAuth instance, with two new parameters we got in twitter_login.php
54
 
        $twitteroauth = new TwitterOAuth( Config::get('twitter_consumer_key'), Config::get('twitter_consumer_secret'), $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
55
 
        if( !isset($twitteroauth) ) {
56
 
                debug_event("Twitter", "Couldn't create OAuth object.", "5");
57
 
                header('Location: ' . Config::get('web_path'));
58
 
        }
59
 
        // Let's request the access token
60
 
        $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
61
 
        if( !isset($access_token) ) {
62
 
                debug_event("Twitter", "Couldn't get access token", "5");
63
 
                header('Location: ' . Config::get('web_path'));
64
 
        }
65
 
        // Save it in a session var
66
 
        $_SESSION['access_token'] = $access_token;
67
 
        
68
 
 
69
 
        // Let's get the user's info
70
 
        $user_info = $twitteroauth->get('account/verify_credentials');
71
 
        
72
 
        debug_event("Twttier", "User ID:{$user_info->id}. ScreenName:{$user_info->screen_name}.", "5");
73
 
        debug_event("Twitter", "access token:" . $access_token['oauth_token'], "5");
74
 
        debug_event("Twitter", "access token secret:" .  $access_token['oauth_token_secret'], "5");
75
 
 
76
 
        if( isset($user_info->error)) {
77
 
                debug_event("Twitter", "Error verifying credentials", "5");
78
 
                session_destroy();
79
 
                header('Location: ' . Config::get('web_path'));
80
 
        } else {
81
 
                
82
 
                // Let's find the user by its twitterid and ampacheid
83
 
                $idselectquery = "SELECT * FROM twitter_users WHERE oauth_provider = 'twitter' AND oauth_uid = ". $user_info->id . " AND ampache_id = " . $_SESSION['userdata']['uid'];
84
 
                debug_event("Twitter", "Id query: " . $idselectquery, "6");
85
 
 
86
 
                $idselectrun = Dba::read($idselectquery);
87
 
                $result = Dba::fetch_assoc($idselectrun);
88
 
 
89
 
                debug_event("Twitter", "ampache_id: {$_SESSION['userdata']['uid']}", "5");
90
 
                debug_event("Twitter", "oauth_uid: {$user_info->id}", "5");
91
 
                debug_event("Twitter", "oauth_token: {$access_token['oauth_token']}", "5");
92
 
                debug_event("Twitter", "oauth_secret: {$access_token['oauth_token_secret']}", "5");
93
 
                debug_event("Twitter", "username: {$user_info->screen_name}", "5");
94
 
 
95
 
                // If not, let's add it to the database
96
 
                if(empty($result)){
97
 
                        debug_event("Twitter", "First time user.  Add them to the DB.", "5");
98
 
                        $insert_query ="INSERT INTO twitter_users (ampache_id, oauth_provider, oauth_uid, oauth_token, oauth_secret, username) VALUES ( '{$_SESSION['userdata']['uid']}', 'twitter', '{$user_info->id}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}', '{$user_info->screen_name}')";
99
 
 
100
 
                        debug_event("Twitter", "Insert query: " . $insert_query, "6");
101
 
                        $insert_run = Dba::write($insert_query);
102
 
 
103
 
                        $select_query = "SELECT * FROM twitter_users WHERE username = '" . $user_info->screen_name . "' AND ampache_id = " . $_SESSION['userdata']['uid']; 
104
 
                        debug_event("Twitter", "Select query: {$query}", "6");
105
 
                        $select_run = Dba::read( $select_query );
106
 
                        $result = Dba::fetch_assoc($select_run);
107
 
                } else {
108
 
                        debug_event("Twitter", "Update the DB to hold current tokens", "5");
109
 
 
110
 
                        $update_query = "UPDATE twitter_users SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_uid = {$user_info->id} AND ampache_id = {$_SESSION['userdata']['uid']}";
111
 
                        debug_event("Twitter", "update query: " . $update_query, "6");
112
 
 
113
 
                        $update_run = Dba::write($update_query);
114
 
 
115
 
                        $select_query = "SELECT * FROM twitter_users WHERE username = '" . $user_info->screen_name . "'";
116
 
                        debug_event("Twitter", "select query: " . $select_query, "6");
117
 
 
118
 
                        $select_run = Dba::read($select_query);
119
 
                        $result = Dba::fetch_assoc($select_run);
120
 
                }
121
 
 
122
 
                $_SESSION['id'] = $result['id'];
123
 
                $_SESSION['twitterusername'] = $result['username'];
124
 
                $_SESSION['oauth_uid'] = $result['oauth_uid'];
125
 
                $_SESSION['oauth_provider'] = $result['oauth_provider'];
126
 
                $_SESSION['oauth_token'] = $result['oauth_token'];
127
 
                $_SESSION['oauth_secret'] = $result['oauth_secret'];
128
 
 
129
 
                header('Location: ' . Config::get('web_path') . '/modules/twitter/twitter_update.php');
130
 
        }
 
26
require_once '../../lib/init.php';
 
27
    require_once( Config::get('prefix') . "/modules/twitter/twitteroauth/twitteroauth.php");
 
28
 
 
29
session_start();
 
30
 
 
31
if(!empty($_SESSION['twitterusername'])) {
 
32
    header('Location: ' . Config::Get('web_path') . '/modules/twitter/twitter_update.php');
 
33
    debug_event("Twitter", "Twitter user has logged in this session.", "5");
 
34
}
 
35
 
 
36
if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){
 
37
    debug_event("Twitter", "Got all 3 pieces for auth", "5");
 
38
} else {
 
39
    if( $_SESSION['twitterCount'] < 4 ) {
 
40
        debug_event("Twitter", "Didn't get all 3 auth pieces, going to try again.  Try #" . $_SESSION['twitterCount'], "5");
 
41
        $_SESSION['twitterCount']++;
 
42
        header('Location: ' . Config::Get('web_path') . '/modules/twitter/twitter_login.php');
 
43
    } else {
 
44
        debug_event("Twitter", "Failed to auth too many times.  Giving up.", "5");
 
45
        header('Location: ' . Config::Get('web_path') );
 
46
    }
 
47
}
 
48
 
 
49
// TwitterOAuth instance, with two new parameters we got in twitter_login.php
 
50
$twitteroauth = new TwitterOAuth( Config::get('twitter_consumer_key'), Config::get('twitter_consumer_secret'), $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
 
51
if( !isset($twitteroauth) ) {
 
52
    debug_event("Twitter", "Couldn't create OAuth object.", "5");
 
53
    header('Location: ' . Config::get('web_path'));
 
54
}
 
55
// Let's request the access token
 
56
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
 
57
if( !isset($access_token) ) {
 
58
    debug_event("Twitter", "Couldn't get access token", "5");
 
59
    header('Location: ' . Config::get('web_path'));
 
60
}
 
61
// Save it in a session var
 
62
$_SESSION['access_token'] = $access_token;
 
63
 
 
64
 
 
65
// Let's get the user's info
 
66
$user_info = $twitteroauth->get('account/verify_credentials');
 
67
 
 
68
debug_event("Twttier", "User ID:{$user_info->id}. ScreenName:{$user_info->screen_name}.", "5");
 
69
debug_event("Twitter", "access token:" . $access_token['oauth_token'], "5");
 
70
debug_event("Twitter", "access token secret:" .  $access_token['oauth_token_secret'], "5");
 
71
 
 
72
if( isset($user_info->error)) {
 
73
    debug_event("Twitter", "Error verifying credentials", "5");
 
74
    session_destroy();
 
75
    header('Location: ' . Config::get('web_path'));
 
76
}
 
77
else {
 
78
    
 
79
    // Let's find the user by its twitterid and ampacheid
 
80
    $idselectquery = "SELECT * FROM twitter_users WHERE oauth_provider = 'twitter' AND oauth_uid = ". $user_info->id . " AND ampache_id = " . $_SESSION['userdata']['uid'];
 
81
    debug_event("Twitter", "Id query: " . $idselectquery, "6");
 
82
 
 
83
    $idselectrun = Dba::read($idselectquery);
 
84
    $result = Dba::fetch_assoc($idselectrun);
 
85
 
 
86
    debug_event("Twitter", "ampache_id: {$_SESSION['userdata']['uid']}", "5");
 
87
    debug_event("Twitter", "oauth_uid: {$user_info->id}", "5");
 
88
    debug_event("Twitter", "oauth_token: {$access_token['oauth_token']}", "5");
 
89
    debug_event("Twitter", "oauth_secret: {$access_token['oauth_token_secret']}", "5");
 
90
    debug_event("Twitter", "username: {$user_info->screen_name}", "5");
 
91
 
 
92
    // If not, let's add it to the database
 
93
    if(empty($result)){
 
94
        debug_event("Twitter", "First time user.  Add them to the DB.", "5");
 
95
        $insert_query ="INSERT INTO twitter_users (ampache_id, oauth_provider, oauth_uid, oauth_token, oauth_secret, username) VALUES ( '{$_SESSION['userdata']['uid']}', 'twitter', '{$user_info->id}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}', '{$user_info->screen_name}')";
 
96
 
 
97
        debug_event("Twitter", "Insert query: " . $insert_query, "6");
 
98
        $insert_run = Dba::write($insert_query);
 
99
 
 
100
        $select_query = "SELECT * FROM twitter_users WHERE username = '" . $user_info->screen_name . "' AND ampache_id = " . $_SESSION['userdata']['uid']; 
 
101
        debug_event("Twitter", "Select query: {$query}", "6");
 
102
                    $select_run = Dba::read( $select_query );
 
103
        $result = Dba::fetch_assoc($select_run);
 
104
    }
 
105
    else {
 
106
        debug_event("Twitter", "Update the DB to hold current tokens", "5");
 
107
 
 
108
        $update_query = "UPDATE twitter_users SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_uid = {$user_info->id} AND ampache_id = {$_SESSION['userdata']['uid']}";
 
109
        debug_event("Twitter", "update query: " . $update_query, "6");
 
110
 
 
111
        $update_run = Dba::write($update_query);
 
112
 
 
113
        $select_query = "SELECT * FROM twitter_users WHERE username = '" . $user_info->screen_name . "'";
 
114
        debug_event("Twitter", "select query: " . $select_query, "6");
 
115
 
 
116
        $select_run = Dba::read($select_query);
 
117
                    $result = Dba::fetch_assoc($select_run);
 
118
            }
 
119
 
 
120
        $_SESSION['id'] = $result['id'];
 
121
        $_SESSION['twitterusername'] = $result['username'];
 
122
        $_SESSION['oauth_uid'] = $result['oauth_uid'];
 
123
        $_SESSION['oauth_provider'] = $result['oauth_provider'];
 
124
        $_SESSION['oauth_token'] = $result['oauth_token'];
 
125
        $_SESSION['oauth_secret'] = $result['oauth_secret'];
 
126
 
 
127
    header('Location: ' . Config::get('web_path') . '/modules/twitter/twitter_update.php');
 
128
    }
131
129
?>