]> git.mxchange.org Git - friendica-addons.git/blob - tumblr/tumblr.php
eeb51348ba65e799113974b3a0f81c6926ead494
[friendica-addons.git] / tumblr / tumblr.php
1 <?php
2
3 /**
4  * Name: Tumblr Post Connector
5  * Description: Post to Tumblr
6  * Version: 1.0
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  */
9
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');
16
17 }
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');
24 }
25
26
27 function tumblr_jot_nets(&$a,&$b) {
28     if(! local_user())
29         return;
30
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>';
37     }
38 }
39
40
41 function tumblr_settings(&$a,&$s) {
42
43     if(! local_user())
44         return;
45
46     /* Add our stylesheet to the page so we can make our settings look nice */
47
48     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/tumblr/tumblr.css' . '" media="all" />' . "\r\n";
49
50     /* Get the current state of our config variables */
51
52     $enabled = get_pconfig(local_user(),'tumblr','post');
53
54     $checked = (($enabled) ? ' checked="checked" ' : '');
55
56     $def_enabled = get_pconfig(local_user(),'tumblr','post_by_default');
57
58     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
59
60         $tmbl_username = get_pconfig(local_user(), 'tumblr', 'tumblr_username');
61         $tmbl_password = get_pconfig(local_user(), 'tumblr', 'tumblr_password');
62
63
64     /* Add some HTML to the existing form */
65
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>';
72
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>';
77
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>';
82
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>';
87
88     /* provide a submit button */
89
90     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="tumblr-submit" name="tumblr-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
91
92 }
93
94
95 function tumblr_settings_post(&$a,&$b) {
96
97         if(x($_POST,'tumblr-submit')) {
98
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']));
103
104         }
105
106 }
107
108 function tumblr_post_local(&$a,&$b) {
109
110         // This can probably be changed to allow editing by pointing to a different API endpoint
111
112         if($b['edit'])
113                 return;
114
115         if((! local_user()) || (local_user() != $b['uid']))
116                 return;
117
118         if($b['private'] || $b['parent'])
119                 return;
120
121     $tmbl_post   = intval(get_pconfig(local_user(),'tumblr','post'));
122
123         $tmbl_enable = (($tmbl_post && x($_REQUEST,'tumblr_enable')) ? intval($_REQUEST['tumblr_enable']) : 0);
124
125         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'tumblr','post_by_default')))
126                 $tmbl_enable = 1;
127
128     if(! $tmbl_enable)
129        return;
130
131     if(strlen($b['postopts']))
132        $b['postopts'] .= ',';
133      $b['postopts'] .= 'tumblr';
134 }
135
136
137
138
139 function tumblr_send(&$a,&$b) {
140
141     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
142         return;
143
144     if(! strstr($b['postopts'],'tumblr'))
145         return;
146
147     if($b['parent'] != $b['id'])
148         return;
149
150
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';
154
155         if($tmbl_username && $tmbl_password && $tmbl_blog) {
156
157                 require_once('include/bbcode.php');
158
159                 $tag_arr = array();
160                 $tags = '';
161                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
162
163                 if($x) {
164                         foreach($matches as $mtch) {
165                                 $tag_arr[] = $mtch[2];
166                         }
167                 }
168                 if(count($tag_arr))
169                         $tags = implode(',',$tag_arr);
170
171                 $link = "";
172                 $video = false;
173                 $title = trim($b['title']);
174
175                 // Checking for a bookmark
176                 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) {
177                         $link = $matches[1];
178                         if ($title == '')
179                                 $title = html_entity_decode($matches[2],ENT_QUOTES,'UTF-8');
180
181                         $body = $b['body'];
182                         // splitting the text in two parts:
183                         // before and after the bookmark
184                         $pos = strpos($body, "[bookmark");
185                         $body1 = substr($body, 0, $pos);
186                         $body2 = substr($body, $pos);
187
188                         // Removing the bookmark
189                         $body2 = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'',$body2);
190                         $body = $body1.$body2;
191
192                         $video = ((stristr($link,'youtube')) || (stristr($link,'youtu.be')) || (stristr($mtch[1],'vimeo')));
193                 }
194
195                 $params = array(
196                         'email' => $tmbl_username,
197                         'password' => $tmbl_password,
198                         'format' => 'html',
199                         'generator' => 'Friendica',
200                         'tags' => $tags);
201
202                 if (($link != '') and $video) {
203                         $params['type'] = "video";
204                         $params['embed'] = $link;
205                         if ($title != '')
206                                 $params['caption'] = '<h1><a href="'.$link.'">'.$title.
207                                                         "</a></h1><p>".bbcode($body, false, false)."</p>";
208                         else
209                                 $params['caption'] = bbcode($body, false, false);
210                 } else if (($link != '') and !$video) {
211                         $params['type'] = "link";
212                         $params['name'] = $title;
213                         $params['url'] = $link;
214                         $params['description'] = bbcode($b["body"], false, false);
215                 } else {
216                         $params['type'] = "regular";
217                         $params['title'] = $title;
218                         $params['body'] = bbcode($b['body'], false, false);
219                 }
220
221                 $x = post_url($tmbl_blog,$params);
222                 $ret_code = $a->get_curl_code();
223                 if($ret_code == 201)
224                         logger('tumblr_send: success');
225                 elseif($ret_code == 403)
226                         logger('tumblr_send: authentication failure');
227                 else
228                         logger('tumblr_send: general error: ' . print_r($x,true));
229
230         }
231 }
232