4 * Name: Tumblr Post Connector
5 * Description: Post to Tumblr
7 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
10 function tumblr_install() {
11 register_hook('post_local', 'addon/tumblr/tumblr.php', 'tumblr_post_local');
12 register_hook('notifier_normal', 'addon/tumblr/tumblr.php', 'tumblr_send');
13 register_hook('jot_networks', 'addon/tumblr/tumblr.php', 'tumblr_jot_nets');
14 register_hook('connector_settings', 'addon/tumblr/tumblr.php', 'tumblr_settings');
15 register_hook('connector_settings_post', 'addon/tumblr/tumblr.php', 'tumblr_settings_post');
18 function tumblr_uninstall() {
19 unregister_hook('post_local', 'addon/tumblr/tumblr.php', 'tumblr_post_local');
20 unregister_hook('notifier_normal', 'addon/tumblr/tumblr.php', 'tumblr_send');
21 unregister_hook('jot_networks', 'addon/tumblr/tumblr.php', 'tumblr_jot_nets');
22 unregister_hook('connector_settings', 'addon/tumblr/tumblr.php', 'tumblr_settings');
23 unregister_hook('connector_settings_post', 'addon/tumblr/tumblr.php', 'tumblr_settings_post');
27 function tumblr_jot_nets(&$a,&$b) {
31 $tmbl_post = get_pconfig(local_user(),'tumblr','post');
32 if(intval($tmbl_post) == 1) {
33 $tmbl_defpost = get_pconfig(local_user(),'tumblr','post_by_default');
34 $selected = ((intval($tmbl_defpost) == 1) ? ' checked="checked" ' : '');
35 $b .= '<div class="profile-jot-net"><input type="checkbox" name="tumblr_enable"' . $selected . 'value="1" /> '
36 . t('Post to Tumblr') . '</div>';
41 function tumblr_settings(&$a,&$s) {
46 /* Add our stylesheet to the page so we can make our settings look nice */
48 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/tumblr/tumblr.css' . '" media="all" />' . "\r\n";
50 /* Get the current state of our config variables */
52 $enabled = get_pconfig(local_user(),'tumblr','post');
54 $checked = (($enabled) ? ' checked="checked" ' : '');
56 $def_enabled = get_pconfig(local_user(),'tumblr','post_by_default');
58 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
60 $tmbl_username = get_pconfig(local_user(), 'tumblr', 'tumblr_username');
61 $tmbl_password = get_pconfig(local_user(), 'tumblr', 'tumblr_password');
64 /* Add some HTML to the existing form */
66 $s .= '<div class="settings-block">';
67 $s .= '<h3>' . t('Tumblr Post Settings') . '</h3>';
68 $s .= '<div id="tumblr-enable-wrapper">';
69 $s .= '<label id="tumblr-enable-label" for="tumblr-checkbox">' . t('Enable Tumblr Post Plugin') . '</label>';
70 $s .= '<input id="tumblr-checkbox" type="checkbox" name="tumblr" value="1" ' . $checked . '/>';
71 $s .= '</div><div class="clear"></div>';
73 $s .= '<div id="tumblr-username-wrapper">';
74 $s .= '<label id="tumblr-username-label" for="tumblr-username">' . t('Tumblr login') . '</label>';
75 $s .= '<input id="tumblr-username" type="text" name="tumblr_username" value="' . $tmbl_username . '" />';
76 $s .= '</div><div class="clear"></div>';
78 $s .= '<div id="tumblr-password-wrapper">';
79 $s .= '<label id="tumblr-password-label" for="tumblr-password">' . t('Tumblr password') . '</label>';
80 $s .= '<input id="tumblr-password" type="password" name="tumblr_password" value="' . $tmbl_password . '" />';
81 $s .= '</div><div class="clear"></div>';
83 $s .= '<div id="tumblr-bydefault-wrapper">';
84 $s .= '<label id="tumblr-bydefault-label" for="tumblr-bydefault">' . t('Post to Tumblr by default') . '</label>';
85 $s .= '<input id="tumblr-bydefault" type="checkbox" name="tumblr_bydefault" value="1" ' . $def_checked . '/>';
86 $s .= '</div><div class="clear"></div>';
88 /* provide a submit button */
90 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="tumblr-submit" name="tumblr-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
95 function tumblr_settings_post(&$a,&$b) {
97 if(x($_POST,'tumblr-submit')) {
99 set_pconfig(local_user(),'tumblr','post',intval($_POST['tumblr']));
100 set_pconfig(local_user(),'tumblr','post_by_default',intval($_POST['tumblr_bydefault']));
101 set_pconfig(local_user(),'tumblr','tumblr_username',trim($_POST['tumblr_username']));
102 set_pconfig(local_user(),'tumblr','tumblr_password',trim($_POST['tumblr_password']));
108 function tumblr_post_local(&$a,&$b) {
110 // This can probably be changed to allow editing by pointing to a different API endpoint
115 if((! local_user()) || (local_user() != $b['uid']))
118 if($b['private'] || $b['parent'])
121 $tmbl_post = intval(get_pconfig(local_user(),'tumblr','post'));
123 $tmbl_enable = (($tmbl_post && x($_REQUEST,'tumblr_enable')) ? intval($_REQUEST['tumblr_enable']) : 0);
125 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'tumblr','post_by_default')))
131 if(strlen($b['postopts']))
132 $b['postopts'] .= ',';
133 $b['postopts'] .= 'tumblr';
139 function tumblr_send(&$a,&$b) {
141 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
144 if(! strstr($b['postopts'],'tumblr'))
147 if($b['parent'] != $b['id'])
151 $tmbl_username = get_pconfig($b['uid'],'tumblr','tumblr_username');
152 $tmbl_password = get_pconfig($b['uid'],'tumblr','tumblr_password');
153 $tmbl_blog = 'http://www.tumblr.com/api/write';
155 if($tmbl_username && $tmbl_password && $tmbl_blog) {
157 require_once('include/bbcode.php');
161 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
164 foreach($matches as $mtch) {
165 $tag_arr[] = $mtch[2];
169 $tags = implode(',',$tag_arr);
173 'email' => $tmbl_username,
174 'password' => $tmbl_password,
175 'title' => (($b['title']) ? $b['title'] : t('Post from Friendica')),
178 'generator' => 'Friendica',
180 'body' => bbcode($b['body'])
183 $x = post_url($tmbl_blog,$params);
184 $ret_code = $a->get_curl_code();
186 logger('tumblr_send: success');
187 elseif($ret_code == 403)
188 logger('tumblr_send: authentication failure');
190 logger('tumblr_send: general error: ' . print_r($x,true));