-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitly.module
228 lines (210 loc) · 7.44 KB
/
bitly.module
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
* Basic module to provide access and storage of bit.ly shortened URLs.
*/
/**
* Implementation of hook_help().
*/
function bitly_help($path, $arg) {
switch ($path) {
case "admin/help#shorten":
return '<p>'. t("This module provides a framework for other Drupal " .
"modules to shorten URLs and display statistics using " .
"the bit.ly service.") .'</p>';
}
}
/**
* Implementation of hook_menu().
*/
function bitly_menu() {
$items = array();
$items['admin/settings/bitly'] = array(
'title' => 'Bit.ly',
'description' => 'Provide global options for the bit.ly module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('bitly_admin'),
'access arguments' => array('access administration pages'),
);
$items['user/%user/bitly'] = array(
'title' => 'Bit.ly',
'page callback' => 'bitly_preferences',
'page arguments' => array(1),
'access callback' => 'user_edit_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['bitly_oauth'] = array(
'title' => 'Validate bit.ly credentials',
'page callback' => '_bitly_oauth',
'access arguments' => array('use own bit.ly login'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function bitly_perm() {
return array('use own bit.ly login', 'use bit.ly');
}
/**
* Admin settings page.
*/
function bitly_admin($form_state) {
$form['bitly_keys'] = array(
'#type' => 'fieldset',
'#title' => t('Bit.ly API Credentials'),
'#description' => t('Please supply the bit.ly API credentials that will be used globally. These API credentials will be used in any case where a user either does not or cannot supply their own credentials. !here.', array('!here' => l('Find your credentials', 'http://bit.ly/a/your_api_key'))),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['bitly_keys']['bitly_login'] = array(
'#type' => 'textfield',
'#title' => t('Bit.ly Login'),
'#required' => TRUE,
'#default_value' => variable_get('bitly_login', ''),
'#description' => t('This field is case-sensitive.'),
);
$form['bitly_keys']['bitly_key'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Bit.ly API Key'),
'#default_value' => variable_get('bitly_key', ''),
'#description' => t('This field is case-sensitive.'),
);
$form['bitly_oauth'] = array(
'#type' => 'fieldset',
'#title' => t('Bit.ly OAuth Credentials'),
'#description' => t('Please supply OAuth to allow users to log into their bit.ly accounts by clicking a sign-in link rather than needing to enter an API key. Currently you must !request directly from bit.ly.', array('!request' => l('request OAuth access', 'http://code.google.com/p/bitly-api/wiki/ApiDocumentation#OAuth'))),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['bitly_oauth']['bitly_client_id'] = array(
'#type' => 'textfield',
'#title' => t('Bit.ly OAuth client_id'),
'#default_value' => variable_get('bitly_client_id', ''),
'#description' => t('This field is case-sensitive.'),
);
$form['bitly_oauth']['bitly_client_secret'] = array(
'#type' => 'textfield',
'#title' => t('Bit.ly OAuth client_secret'),
'#default_value' => variable_get('bitly_client_secret', ''),
'#description' => t('This field is case-sensitive.'),
);
return system_settings_form($form);
}
/**
* Provides a preferences and management page for bit.ly users.
*/
function bitly_preferences($account) {
$search = db_query("SELECT login FROM {bitly_auth} WHERE uid = %d", $account->uid);
if($result = db_result($search)) {
$output = t("You're currently linked to the %login bit.ly account.", array('%login' => $result));
$output .= drupal_get_form("_bitly_preferences_unlink", $account->uid);
}
else {
$output = t("You currently do not have a !bitly account configured. A bit.ly account is required for tracking statistics of shortened URLs.", array('!bitly' => l('bit.ly', 'http://bit.ly')));
$output .= drupal_get_form("_bitly_preferences_link");
}
return $output;
}
/**
* Provide the "Link bit.ly account" button.
*/
function _bitly_preferences_link($form_state) {
$form = array();
$form['bitly_link'] = array(
'#type' => 'submit',
'#value' => t('Link my bit.ly account')
);
return $form;
}
function _bitly_preferences_link_submit($form, &$form_state) {
_bitly_oauth();
}
/**
* Provide the "Un-link bit.ly account" button.
*/
function _bitly_preferences_unlink($form_state, $uid) {
$form = array();
$form['bitly_link'] = array(
'#type' => 'submit',
'#value' => t('Un-link this account')
);
$form['bitly_uid'] = array(
'#type' => 'value',
'#value' => $uid
);
return $form;
}
function _bitly_preferences_unlink_submit($form, &$form_state) {
$uid = $form_state['values']['bitly_uid'];
@db_query("DELETE FROM {bitly_auth} WHERE uid = %d", $uid);
drupal_goto('user/' . $uid . '/bitly');
}
/**
* Handles user OAuth interaction.
*/
function _bitly_oauth() {
global $user;
// Determine if the user already has a key stored.
$search = db_query("SELECT token FROM {bitly_auth} WHERE uid = %d", $user->uid);
if(db_result($search)) {
drupal_set_message(t("Your user account already has an active bit.ly account definated, please visit your !user_page to change it.", array('!user_page' => l('user page', 'user/' . $user->uid . '/bitly'))));
drupal_goto('user/' . $user->uid . '/bitly');
}
$client_id = variable_get('bitly_client_id', '');
global $base_root;
$redirect_uri = $base_root . base_path() . 'bitly_oauth';
if(empty($_REQUEST['code'])) {
drupal_goto("https://bit.ly/oauth/authorize?client_id=$client_id&redirect_uri=$redirect_uri");
}
else {
_bitly_inc();
$results = bitly_oauth_access_token($_REQUEST['code'], $redirect_uri);
if (!empty($results)) {
$array = array(
'uid' => $user->uid,
'login' => $results['login'],
'token' => $results['access_token'],
'apikey' => $results['apiKey'],
);
if (empty($array['token'])) {
drupal_set_message(t("We were unable to link to your bit.ly account, please try again later."));
} else {
drupal_write_record('bitly_auth', $array);
drupal_set_message(t("Your user account has been sucessfully linked to bit.ly!"));
}
}
}
drupal_goto('user/' . $user->uid . '/bitly');
}
/**
* Helper function to create proper defines before loading bitly.inc.
* Call this function before any of the other bitly_* functions in bitly.inc.
*/
function _bitly_inc() {
define('bitlyKey', variable_get('bitly_key', ''));
define('bitlyLogin', variable_get('bitly_login', ''));
define('bitly_clientid', variable_get('bitly_client_id', ''));
define('bitly_secret', variable_get('bitly_client_secret', ''));
module_load_include('inc', 'bitly');
}
/**
* Wrapper function to prefill bitly_v3_shorten with current user's login.
*/
function bitly_user_shorten($longUrl, $domain = '', $account = NULL) {
if (empty($account)) {
global $user;
if (empty($user->uid)) { // Anonymous user, 0 is also considered empty.
return bitly_v3_shorten($longUrl, $domain);
}
$account = $user->uid;
}
$search = db_query("SELECT * FROM {bitly_auth} WHERE uid = %d", $user->uid);
if ($auth = db_fetch_object($search)) {
return bitly_v3_shorten($longUrl, $domain, $auth->login, $auth->apikey);
}
return FALSE;
}