4 * Name: Tumblr Post Connector
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 function tumblr_install() {
10 register_hook('post_local', 'addon/tumblr/tumblr.php', 'tumblr_post_local');
11 register_hook('notifier_normal', 'addon/tumblr/tumblr.php', 'tumblr_send');
12 register_hook('jot_networks', 'addon/tumblr/tumblr.php', 'tumblr_jot_nets');
13 register_hook('connector_settings', 'addon/tumblr/tumblr.php', 'tumblr_settings');
14 register_hook('connector_settings_post', 'addon/tumblr/tumblr.php', 'tumblr_settings_post');
17 function tumblr_uninstall() {
18 unregister_hook('post_local', 'addon/tumblr/tumblr.php', 'tumblr_post_local');
19 unregister_hook('notifier_normal', 'addon/tumblr/tumblr.php', 'tumblr_send');
20 unregister_hook('jot_networks', 'addon/tumblr/tumblr.php', 'tumblr_jot_nets');
21 unregister_hook('connector_settings', 'addon/tumblr/tumblr.php', 'tumblr_settings');
22 unregister_hook('connector_settings_post', 'addon/tumblr/tumblr.php', 'tumblr_settings_post');
26 function tumblr_jot_nets(&$a,&$b) {
30 $tmbl_post = get_pconfig(local_user(),'tumblr','post');
31 if(intval($tmbl_post) == 1) {
32 $tmbl_defpost = get_pconfig(local_user(),'tumblr','post_by_default');
33 $selected = ((intval($tmbl_defpost) == 1) ? ' checked="checked" ' : '');
34 $b .= '<div class="profile-jot-net"><input type="checkbox" name="tumblr_enable"' . $selected . 'value="1" /> '
35 . t('Post to Tumblr') . '</div>';
40 function tumblr_settings(&$a,&$s) {
45 /* Add our stylesheet to the page so we can make our settings look nice */
47 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/tumblr/tumblr.css' . '" media="all" />' . "\r\n";
49 /* Get the current state of our config variables */
51 $enabled = get_pconfig(local_user(),'tumblr','post');
53 $checked = (($enabled) ? ' checked="checked" ' : '');
55 $def_enabled = get_pconfig(local_user(),'tumblr','post_by_default');
57 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
59 $tmbl_username = get_pconfig(local_user(), 'tumblr', 'tumblr_username');
60 $tmbl_password = get_pconfig(local_user(), 'tumblr', 'tumblr_password');
63 /* Add some HTML to the existing form */
65 $s .= '<div class="settings-block">';
66 $s .= '<h3>' . t('Tumblr Post Settings') . '</h3>';
67 $s .= '<div id="tumblr-enable-wrapper">';
68 $s .= '<label id="tumblr-enable-label" for="tumblr-checkbox">' . t('Enable Tumblr Post Plugin') . '</label>';
69 $s .= '<input id="tumblr-checkbox" type="checkbox" name="tumblr" value="1" ' . $checked . '/>';
70 $s .= '</div><div class="clear"></div>';
72 $s .= '<div id="tumblr-username-wrapper">';
73 $s .= '<label id="tumblr-username-label" for="tumblr-username">' . t('Tumblr login') . '</label>';
74 $s .= '<input id="tumblr-username" type="text" name="tumblr_username" value="' . $tmbl_username . '" />';
75 $s .= '</div><div class="clear"></div>';
77 $s .= '<div id="tumblr-password-wrapper">';
78 $s .= '<label id="tumblr-password-label" for="tumblr-password">' . t('Tumblr password') . '</label>';
79 $s .= '<input id="tumblr-password" type="password" name="tumblr_password" value="' . $tmbl_password . '" />';
80 $s .= '</div><div class="clear"></div>';
82 $s .= '<div id="tumblr-bydefault-wrapper">';
83 $s .= '<label id="tumblr-bydefault-label" for="tumblr-bydefault">' . t('Post to Tumblr by default') . '</label>';
84 $s .= '<input id="tumblr-bydefault" type="checkbox" name="tumblr_bydefault" value="1" ' . $def_checked . '/>';
85 $s .= '</div><div class="clear"></div>';
87 /* provide a submit button */
89 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="tumblr-submit" name="tumblr-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
94 function tumblr_settings_post(&$a,&$b) {
96 if(x($_POST,'tumblr-submit')) {
98 set_pconfig(local_user(),'tumblr','post',intval($_POST['tumblr']));
99 set_pconfig(local_user(),'tumblr','post_by_default',intval($_POST['tumblr_bydefault']));
100 set_pconfig(local_user(),'tumblr','tumblr_username',trim($_POST['tumblr_username']));
101 set_pconfig(local_user(),'tumblr','tumblr_password',trim($_POST['tumblr_password']));
107 function tumblr_post_local(&$a,&$b) {
109 // This can probably be changed to allow editing by pointing to a different API endpoint
114 if((! local_user()) || (local_user() != $b['uid']))
117 if($b['private'] || $b['parent'])
120 $tmbl_post = intval(get_pconfig(local_user(),'tumblr','post'));
122 $tmbl_enable = (($tmbl_post && x($_REQUEST,'tumblr_enable')) ? intval($_REQUEST['tumblr_enable']) : 0);
124 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'tumblr','post_by_default')))
130 if(strlen($b['postopts']))
131 $b['postopts'] .= ',';
132 $b['postopts'] .= 'tumblr';
138 function tumblr_send(&$a,&$b) {
140 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
143 if(! strstr($b['postopts'],'tumblr'))
146 if($b['parent'] != $b['id'])
150 $tmbl_username = get_pconfig($b['uid'],'tumblr','tumblr_username');
151 $tmbl_password = get_pconfig($b['uid'],'tumblr','tumblr_password');
152 $tmbl_blog = 'http://www.tumblr.com/api/write';
154 if($tmbl_username && $tmbl_password && $tmbl_blog) {
156 require_once('include/bbcode.php');
160 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
163 foreach($matches as $mtch) {
164 $tag_arr[] = $mtch[2];
168 $tags = implode(',',$tag_arr);
172 'email' => $tmbl_username,
173 'password' => $tmbl_password,
174 'title' => (($b['title']) ? $b['title'] : t('Post from Friendica')),
177 'generator' => 'Friendica',
179 'body' => bbcode($b['body'])
182 $x = post_url($tmbl_blog,$params);
183 $ret_code = $a->get_curl_code();
185 logger('tumblr_send: success');
186 elseif($ret_code == 403)
187 logger('tumblr_send: authentication failure');
189 logger('tumblr_send: general error: ' . print_r($x,true));