--- twitter.module 2007-01-23 09:05:50.000000000 -0800 +++ twitter_220092.txt 2008-09-18 11:00:51.000000000 -0700 @@ -1,67 +1,257 @@ - 'fieldset', - '#title' => t('Twitter settings')); - $form['twitter']['twitter_user'] = array( - '#type' => 'textfield', - '#title' => t('Username'), - '#default_value' => $edit['twitter_user'], - '#description' => t('The username (email address) associated with your twitter.com account')); - $form['twitter']['twitter_pass'] = array( - '#type' => 'password', - '#title' => t('Password')); - $form['twitter']['twitter_text'] = array( - '#type' => 'textfield', - '#title' => t('Text format'), - '#default_value' => $edit['twitter_text'], - '#description' => t('Format of the text to submit. Use !title and !url for the post title and url respectively.')); - return $form; - case 'insert': - case 'update': - if (!empty($edit['twitter_user']) && !empty($edit['twitter_pass'])) { - $edit['twitter_encrypted'] = base64_encode($edit['twitter_user'] .':'. $edit['twitter_pass']); - } - unset($edit['twitter_pass']); - } -} - -function twitter_nodeapi(&$node, $op) { - switch ($op) { - case 'insert': - global $user; - if ($node->status == 1) { - twitter_post($node, $user); - } - } -} - -/** - * Implements the twitter posting API per: - * http://twitter.com/help/api - */ -function twitter_post($node, $account) { - if (empty($account->twitter_encrypted)) { - return false; - } - - $text = ($account->twitter_text) ? $account->twitter_text : 'New post: !title (!url)'; - $text = t($text, array('!title' => $node->title, - '!url' => url('node/' . $node->nid, NULL, NULL, TRUE))); - - $headers = array('Authorization' => 'Basic '. $account->twitter_encrypted, - 'Content-type' => 'application/x-www-form-urlencoded'); - $data = 'status='. urlencode($text); - - $result = drupal_http_request(TWITTER_URL, $headers, 'POST', $data); - drupal_set_message(t('Posted to twitter.com')); + 'admin/settings/twitter', + 'title' => t('Twitter configuration details'), + 'description' => t('Set details like the site-wide twitter account.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('twitter_admin_settings'), + 'access' => user_access('administer site configuration'), + ); + return $items; + } +} + + +/** + * Implementation of hook_perm + * + */ +function twitter_perm() { + return array( + 'broadcast to personal twitter', + 'broadcast to sitewide twitter', + ); +} + + +function twitter_admin_settings() { + + $form['twitter'] = array( + '#type' => 'fieldset', + '#title' => t('Twitter site-wide settings.'), + ); + $form['twitter']['twitter_site_user'] = array( + '#type' => 'textfield', + '#title' => t('Site-wide twitter username'), + '#default_value' => variable_get('twitter_site_user', ''), + ); + $form['twitter']['twitter_site_pass'] = array( + '#type' => 'password', + '#title' => t('Password'), + ); + $form['twitter']['twitter_site_text'] = array( + '#type' => 'textfield', + '#title' => t('Text content'), + '#default_value' => variable_get('twitter_site_text', ''), + '#description' => t('Content of the text to submit. Available replacement values are: !title, !url, !user, !userurl, for: the node title, node url, node author name, and node author profile url.') + ); + $form['twitter']['twitter_site_use_tinyurl'] = array( + '#type' => 'checkbox', + '#title' => t('Use tinyurl.com service to shorten links to posts?'), + '#default_value' => variable_get('twitter_site_use_tinyurl', FALSE), + ); + + // todo, is there a better way to do this ala FAPI? + $edit = $_POST; + if ($edit) { + $form['twitter']['twitter_site_encrypted'] = array( + '#type' => 'value', + '#value' => base64_encode($edit['twitter_site_user'] . ':' . $edit['twitter_site_pass']), + ); + } + return system_settings_form($form); +} + +/** + * Implementation of hook_form_alter + * Hook into node type forms. + */ +function twitter_form_alter($form_id, &$form) { + switch($form_id) { + case 'node_type_form': + $form['workflow']['twitter'] = array( + '#type' => 'radios', + '#title' => t('Twitter broadcasting'), + '#description' => t('Enable twitter broadcasting from the title field upon publishing of this node-type'), + '#default_value' => variable_get('twitter_'. $form['#node_type']->type, 0), + '#options' => array( + t('Disabled'), + t('Enabled')), + '#weight' => 20, + ); + break; + case $form['type']['#value'] .'_node_form': + if (variable_get('twitter_'. $form['type']['#value'], 0) && (user_access('broadcast to personal twitter') || user_access('broadcast to sitewide twitter'))) { + twitter_alter_node_form($form_id, $form); + } + break; + } +} + +/** + * Add twitter checkboxes on node-edit form. + * + */ + +function twitter_alter_node_form($form_id, &$form) { + $form['twitter'] = array( + '#type' => 'fieldset', + '#title' => t('Twitter settings'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#weight' => 10, + ); + + $options = array(); + if (user_access('broadcast to personal twitter')) { + $options['personal'] = t('Send to personal twitter'); + } + if (user_access('broadcast to sitewide twitter')) { + $options['sitewide'] = t('Send to sitewide twitter'); + } + $form['twitter']['twitter_options'] = array( + '#type' => 'checkboxes', + '#title' => t('Twitter broadcast options'), + '#default_value' => array(''), + '#options' => $options, + '#description' => t('Choose whether to send this node title / url to your personal and/or site-wide twitter feed.'), + ); +} + + +/** + * Implementation of hook_user() + */ +function twitter_user($op, &$edit, &$account, $category = NULL) { + switch ($op) { + case 'form': + if (user_access('broadcast to personal twitter')) { + $form['twitter'] = array( + '#type' => 'fieldset', + '#title' => t('Personal Twitter settings') + ); + $form['twitter']['twitter_user'] = array( + '#type' => 'textfield', + '#title' => t('Username'), + '#default_value' => $edit['twitter_user'], + '#description' => t('The email address associated with your twitter.com account') + ); + $form['twitter']['twitter_pass'] = array( + '#type' => 'password', + '#title' => t('Password') + ); + $form['twitter']['twitter_text'] = array( + '#type' => 'textfield', + '#title' => t('Text content'), + '#default_value' => $edit['twitter_text'], + '#description' => t('Content of the text to submit. Available replacement values are: !title, !url, !user, !userurl, for: the node title, node url, node author name, and node author profile url.') + ); + $form['twitter']['twitter_use_tinyurl'] = array( + '#type' => 'checkbox', + '#title' => t('Use tinyurl.com service to shorten links to posts?'), + '#default_value' => $edit['twitter_usetinyurl'] + ); + } + return $form; + case 'insert': + case 'update': + if (!empty($edit['twitter_user']) && !empty($edit['twitter_pass'])) { + $edit['twitter_encrypted'] = base64_encode($edit['twitter_user'] .':'. $edit['twitter_pass']); + } + unset($edit['twitter_pass']); + } +} + +function twitter_nodeapi(&$node, $op) { + switch ($op) { + case 'insert': + global $user; + if ($node->status == 1) { + if ($node->twitter_options['personal']) { + twitter_post($node, $user); + } + if ($node->twitter_options['sitewide']) { + twitter_post($node, $user, TRUE); + } + } + break; + case 'update': + global $user; + if ($node->status == 1) { + if ($node->twitter_options['personal']) { + twitter_post($node, $user); + } + if ($node->twitter_options['sitewide']) { + twitter_post($node, $user, TRUE); + } + } + break; + } +} + +/** + * Implements the twitter posting API per: + * http://twitter.com/help/api + */ +function twitter_post($node, $account, $sitewide = FALSE) { + // Check if key is set, in the case of posting to user account or sitewide account. + // If it's false in either case, return false. + if (($sitewide == 0 && empty($account->twitter_encrypted)) || ($sitewide == 1 && !variable_get('twitter_site_encrypted', ''))) { + print 'did not pass.'; + return false; + } + + if (!$sitewide) { + $text = ($account->twitter_text) ? $account->twitter_text : 'New post: !title (!url) !user (!userurl)'; + $auth = $account->twitter_encrypted; + $tinyUrls = $account->twitter_use_tinyurl; + } + elseif ($sitewide) { + $text = variable_get('twitter_site_text','') ? variable_get('twitter_site_text','') : 'New post: !title (!url) !user !userurl'; + $auth = variable_get('twitter_site_encrypted',''); + $tinyUrls = variable_get('twitter_site_use_tinyurl', FALSE); + } + $link_to_url = url('node/' . $node->nid, NULL, NULL, TRUE); + $link_to_userurl = url('user/' . $user->uid, NULL, NULL, TRUE); + + if ($tinyUrls) { + $headers = array('Content-type' => 'application/x-www-form-urlencoded'); + $tiny_urled = drupal_http_request('http://tinyurl.com/api-create.php?url='.urlencode($link_to_url), $headers, 'POST', NULL); + if ($tiny_urled->code == 200) { + $link_to_url = $tiny_urled->data; + } + } + + if ($tinyUrls) { + $headers = array('Content-type' => 'application/x-www-form-urlencoded'); + $tiny_userurled = drupal_http_request('http://tinyurl.com/api-create.php?url='.urlencode($link_to_userurl), $headers, 'POST', NULL); + if ($tiny_userurled->code == 200) { + $link_to_userurl = $tiny_userurled->data; + } + } + +global $user; + $text = t($text, array('!title' => $node->title, + '!user' => $user->name, + '!userurl' => $link_to_userurl, + '!url' => $link_to_url)); + + $headers = array('Authorization' => 'Basic '. $auth, + 'Content-type' => 'application/x-www-form-urlencoded'); + $data = 'status='. urlencode($text); + + $result = drupal_http_request(TWITTER_URL, $headers, 'POST', $data); + drupal_set_message(t('Posted to twitter.com')); } \ No newline at end of file