twitter.pages.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * Form builder; Twitter settings form.
  4. */
  5. function twitter_admin_form($form, &$form_state) {
  6. $form['twitter_import'] = array(
  7. '#type' => 'checkbox',
  8. '#title' => t('Import and display the Twitter statuses of site users who have entered their Twitter account information.'),
  9. '#default_value' => variable_get('twitter_import', 1),
  10. );
  11. $form['twitter_expire'] = array(
  12. '#type' => 'select',
  13. '#title' => t('Delete old statuses'),
  14. '#default_value' => variable_get('twitter_expire', 0),
  15. '#options' => array(0 => t('Never')) + drupal_map_assoc(array(604800, 2592000, 7776000, 31536000), 'format_interval'),
  16. '#states' => array(
  17. 'visible' => array(
  18. ':input[name=twitter_import]' => array('checked' => TRUE),
  19. ),
  20. ),
  21. );
  22. $form['oauth'] = array(
  23. '#type' => 'fieldset',
  24. '#title' => t('OAuth Settings'),
  25. '#access' => module_exists('oauth_common'),
  26. '#description' => t('To enable OAuth based access for twitter, you must <a href="@url">register your application</a> with Twitter and add the provided keys here.', array('@url' => 'https://dev.twitter.com/apps/new')),
  27. );
  28. $form['oauth']['callback_url'] = array(
  29. '#type' => 'item',
  30. '#title' => t('Callback URL'),
  31. '#markup' => url('twitter/oauth', array('absolute' => TRUE)),
  32. );
  33. $form['oauth']['twitter_consumer_key'] = array(
  34. '#type' => 'textfield',
  35. '#title' => t('OAuth Consumer key'),
  36. '#default_value' => variable_get('twitter_consumer_key', NULL),
  37. );
  38. $form['oauth']['twitter_consumer_secret'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('OAuth Consumer secret'),
  41. '#default_value' => variable_get('twitter_consumer_secret', NULL),
  42. );
  43. return system_settings_form($form);
  44. }
  45. /**
  46. * @todo Please document this function.
  47. * @see http://drupal.org/node/1354
  48. */
  49. function twitter_user_settings($account) {
  50. module_load_include('inc', 'twitter');
  51. $output = array();
  52. if (!empty($account->twitter_accounts)) {
  53. $output['list_form'] = drupal_get_form('twitter_account_list_form', $account->twitter_accounts);
  54. }
  55. $output['form'] = drupal_get_form('twitter_account_form', $account);
  56. return $output;
  57. }
  58. /**
  59. * @todo Please document this function.
  60. * @see http://drupal.org/node/1354
  61. */
  62. function twitter_account_list_form($form, $form_state, $twitter_accounts = array()) {
  63. $form['#tree'] = TRUE;
  64. $form['accounts'] = array();
  65. foreach ($twitter_accounts as $twitter_account) {
  66. $form['accounts'][] = _twitter_account_list_row($twitter_account);
  67. }
  68. if (!empty($twitter_accounts)) {
  69. $form['buttons']['submit'] = array(
  70. '#type' => 'submit',
  71. '#value' => t('Save changes'),
  72. );
  73. }
  74. return $form;
  75. }
  76. function _twitter_account_list_row($account) {
  77. $form['#account'] = $account;
  78. $form['id'] = array(
  79. '#type' => 'value',
  80. '#value' => $account->id,
  81. );
  82. $form['uid'] = array(
  83. '#type' => 'value',
  84. '#value' => $account->uid,
  85. );
  86. $form['screen_name'] = array(
  87. '#type' => 'value',
  88. '#value' => $account->screen_name,
  89. );
  90. $form['image'] = array(
  91. '#markup' => theme('image', array('path' => $account->profile_image_url)),
  92. );
  93. $form['visible_name'] = array(
  94. '#markup' => l($account->screen_name, 'http://www.twitter.com/' . $account->screen_name),
  95. );
  96. $form['description'] = array(
  97. '#markup' => filter_xss($account->description),
  98. );
  99. $form['protected'] = array(
  100. '#markup' => empty($account->protected) ? t('No') : t('Yes'),
  101. );
  102. // Here we use user_access('import own tweets') to check permission
  103. // instead of user_access('import own tweets', $account->uid)
  104. // because we allow roles with sufficient permission to overwrite
  105. // the user's import settings.
  106. if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
  107. $form['import'] = array(
  108. '#type' => 'checkbox',
  109. '#default_value' => user_access('import own tweets') ? $account->import : '',
  110. );
  111. }
  112. $form['delete'] = array(
  113. '#type' => 'checkbox',
  114. );
  115. return $form;
  116. }
  117. /**
  118. * @todo Please document this function.
  119. * @see http://drupal.org/node/1354
  120. */
  121. function theme_twitter_account_list_form($variables) {
  122. $form = $variables['form'];
  123. if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
  124. $header = array('', t('Name'), t('Description'), t('Private'), t('Import'), t('Delete'));
  125. }
  126. else {
  127. $header = array('', t('Name'), t('Description'), t('Private'), t('Delete'));
  128. }
  129. if (user_access('make twitter accounts global')) {
  130. $header[] = '';
  131. }
  132. $rows = array();
  133. foreach (element_children($form['accounts']) as $key) {
  134. $element = &$form['accounts'][$key];
  135. if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
  136. $row = array(
  137. drupal_render($element['image']),
  138. drupal_render($element['id']) . drupal_render($element['screen_name']) . drupal_render($element['visible_name']),
  139. drupal_render($element['description']),
  140. drupal_render($element['protected']),
  141. drupal_render($element['import']),
  142. drupal_render($element['delete']),
  143. );
  144. }
  145. else {
  146. $row = array(
  147. drupal_render($element['image']),
  148. drupal_render($element['id']) . drupal_render($element['screen_name']) . drupal_render($element['visible_name']),
  149. drupal_render($element['description']),
  150. drupal_render($element['protected']),
  151. drupal_render($element['delete']),
  152. );
  153. }
  154. if (user_access('make twitter accounts global')) {
  155. $label = ($element['#account']->is_global) ? t('remove global') : t('make global');
  156. $row[] = l($label, 'user/' . $element['#account']->uid . '/edit/twitter/global/' . $element['#account']->id);
  157. }
  158. $rows[] = $row;
  159. }
  160. $output = theme('table', array('header' => $header, 'rows' => $rows));
  161. $output .= drupal_render_children($form);
  162. return $output;
  163. }
  164. /**
  165. * @todo Please document this function.
  166. * @see http://drupal.org/node/1354
  167. */
  168. function twitter_account_list_form_submit($form, &$form_state) {
  169. $accounts = $form_state['values']['accounts'];
  170. foreach ($accounts as $account) {
  171. if (empty($account['delete'])) {
  172. twitter_account_save($account);
  173. drupal_set_message(t('The Twitter account settings were updated.'));
  174. }
  175. else {
  176. twitter_account_delete($account['id']);
  177. drupal_set_message(t('The Twitter account was deleted.'));
  178. }
  179. }
  180. }
  181. /**
  182. * @todo Please document this function.
  183. * @see http://drupal.org/node/1354
  184. */
  185. function twitter_user_make_global($form, $form_state, $account, $twitter_uid) {
  186. module_load_include('inc', 'twitter');
  187. $twitter_account = twitter_account_load($twitter_uid);
  188. $form = array();
  189. $form['uid'] = array(
  190. '#type' => 'value',
  191. '#value' => $account->uid,
  192. );
  193. $form['twitter_uid'] = array(
  194. '#type' => 'value',
  195. '#value' => $twitter_uid,
  196. );
  197. if ($twitter_account->is_global) {
  198. $text = t('Are you sure you want to remove %screen_name from the global accounts?', array('%screen_name' => $twitter_account->screen_name));
  199. $description = t('This means other users will no longer be allowed to post using this account.');
  200. }
  201. else {
  202. $text = t('Are you sure you want to allow other users to access the %screen_name account?', array('%screen_name' => $twitter_account->screen_name));
  203. $description = t('This will allow other users to post using this account.');
  204. }
  205. return confirm_form($form, $text, 'user/' . $account->uid . '/edit/twitter', $description);
  206. }
  207. /**
  208. * @todo Please document this function.
  209. * @see http://drupal.org/node/1354
  210. */
  211. function twitter_user_make_global_submit($form, &$form_state) {
  212. db_update('twitter_account')
  213. ->expression('is_global', '(1 - is_global)')
  214. ->condition('twitter_uid', $form_state['values']['twitter_uid'])
  215. ->execute();
  216. $form_state['redirect'] = 'user/' . $form_state['values']['uid'] . '/edit/twitter';
  217. }
  218. /**
  219. * Form to add a Twitter account
  220. *
  221. * If OAuth is not enabled, a text field lets users to add their
  222. * Twitter screen name. If it is, a submit button redirects to
  223. * Twitter.com asking for authorisation.
  224. */
  225. function twitter_account_form($form, $form_state, $account = NULL) {
  226. if (empty($account)) {
  227. global $user;
  228. $account = $user;
  229. }
  230. $form['uid'] = array(
  231. '#type' => 'value',
  232. '#value' => $account->uid,
  233. );
  234. if (_twitter_use_oauth()) {
  235. $form['#validate'] = array('twitter_account_oauth_validate');
  236. }
  237. else {
  238. $form['screen_name'] = array(
  239. '#type' => 'textfield',
  240. '#required' => TRUE,
  241. '#title' => t('Twitter user name'),
  242. );
  243. $form['import'] = array(
  244. '#type' => 'checkbox',
  245. '#title' => t('Import statuses from this account'),
  246. '#default_value' => TRUE,
  247. '#access' => FALSE,
  248. );
  249. }
  250. $form['submit'] = array(
  251. '#type' => 'submit',
  252. '#value' => t('Add account'),
  253. );
  254. return $form;
  255. }
  256. /**
  257. * Implements hook_FORM_ID_submit()
  258. *
  259. * Loads Twitter account details and adds them to the user account
  260. */
  261. function twitter_account_form_submit($form, &$form_state) {
  262. module_load_include('lib.php', 'twitter');
  263. module_load_include('inc', 'twitter');
  264. $name = $form_state['values']['screen_name'];
  265. $twitter = new Twitter($name);
  266. $account = $twitter->users_show($name, false);
  267. twitter_account_save($account, TRUE, user_load($form_state['values']['uid']));
  268. }
  269. /**
  270. * If OAuth is enabled, intercept submission of 'Add Account' form on
  271. * user/%/edit/twitter page and redirect to Twitter for auth.
  272. */
  273. function twitter_account_oauth_validate($form, &$form_state) {
  274. module_load_include('lib.php', 'oauth_common');
  275. module_load_include('lib.php', 'twitter');
  276. $key = variable_get('twitter_consumer_key', '');
  277. $secret = variable_get('twitter_consumer_secret', '');
  278. if ($key == '' || $secret == '') {
  279. form_set_error('', t('Please configure your Twitter consumer key and secret.'));
  280. }
  281. $twitter = new TwitterOAuth($key, $secret);
  282. $token = $twitter->get_request_token();
  283. $_SESSION['twitter_oauth']['account'] = user_load($form['uid']['#value']);
  284. $_SESSION['twitter_oauth']['token'] = $token;
  285. $_SESSION['twitter_oauth']['destination'] = $_GET['q'];
  286. drupal_goto($twitter->get_authorize_url($token));
  287. }
  288. /**
  289. * @TODO This code should probably be reviewed.
  290. *
  291. * Wrapper to call drupal_form_submit() which wasn't required in D6.
  292. */
  293. function twitter_oauth_callback() {
  294. $form_state['values']['oauth_token'] = $_GET['oauth_token'];
  295. drupal_form_submit('twitter_oauth_callback_form', $form_state);
  296. }
  297. /**
  298. * Form builder function. In D6 this form was built in response to the
  299. * oauth return request from Twitter, and the setting of
  300. * $form['#post'] seems to have caused the form to be validated and
  301. * processed.
  302. */
  303. function twitter_oauth_callback_form($form, &$form_state) {
  304. $form['#post']['oauth_token'] = $_GET['oauth_token'];
  305. $form['oauth_token'] = array(
  306. '#type' => 'hidden',
  307. '#default_value' => $_GET['oauth_token'],
  308. );
  309. return $form;
  310. }
  311. /**
  312. * Validate results from Twitter OAuth return request.
  313. */
  314. function twitter_oauth_callback_form_validate($form, &$form_state) {
  315. $key = variable_get('twitter_consumer_key', '');
  316. $secret = variable_get('twitter_consumer_secret', '');
  317. if ($key == '' || $secret == '') {
  318. form_set_error('', t('Please configure your Twitter consumer key and secret.'));
  319. }
  320. if (isset($_SESSION['twitter_oauth'])) {
  321. $form_state['twitter_oauth'] = $_SESSION['twitter_oauth'];
  322. unset($_SESSION['twitter_oauth']);
  323. }
  324. else {
  325. form_set_error('oauth_token', 'Invalid Twitter OAuth request');
  326. }
  327. if (isset($form_state['twitter_oauth']['token'])) {
  328. $token = $form_state['twitter_oauth']['token'];
  329. if (!is_array($token) || !$key || !$secret) {
  330. form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
  331. }
  332. if ($token['oauth_token'] != $form_state['values']['oauth_token']) {
  333. form_set_error('oauth_token', t('Invalid OAuth token.'));
  334. }
  335. }
  336. else {
  337. form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
  338. }
  339. module_load_include('lib.php', 'oauth_common');
  340. module_load_include('lib.php', 'twitter');
  341. module_load_include('inc', 'twitter');
  342. if ($twitter = new TwitterOAuth($key, $secret, $token['oauth_token'], $token['oauth_token_secret'])) {
  343. if ($response = $twitter->get_access_token()) {
  344. $form_state['twitter_oauth']['response'] = $response;
  345. }
  346. else {
  347. form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
  348. }
  349. }
  350. else {
  351. form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
  352. }
  353. }
  354. /**
  355. * Handle a Twitter OAuth return request and store the account creds
  356. * in the DB. Redirects to user/%/edit/twitter
  357. *
  358. * @TODO Redirect better.
  359. *
  360. * I don't much like the use of drupal_goto() here as it might
  361. * interfere with other modules trying to piggyback on the form
  362. * submission, but setting $form['redirect'] was leaving us at the
  363. * twitter/oauth URL.
  364. */
  365. function twitter_oauth_callback_form_submit(&$form, &$form_state) {
  366. $key = variable_get('twitter_consumer_key', '');
  367. $secret = variable_get('twitter_consumer_secret', '');
  368. $response = $form_state['twitter_oauth']['response'];
  369. $twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
  370. $twitter_account = $twitter->users_show($response['screen_name']);
  371. $twitter_account->set_auth($response);
  372. $account = $form_state['twitter_oauth']['account'];
  373. twitter_account_save($twitter_account, TRUE, $account);
  374. $form['#programmed'] = FALSE;
  375. $form_state['redirect'] = url('user/' . $account->uid . '/edit/twitter');
  376. // redirect isn't firing - because we're using drupal_submit_form()?
  377. // - so adding drupal_goto() here (but not liking it).
  378. drupal_goto('user/' . $account->uid . '/edit/twitter');
  379. }