]> git.mxchange.org Git - friendica-addons.git/blob - tumblr/tumblr.php
Merge branch 'apull'
[friendica-addons.git] / tumblr / tumblr.php
1 <?php
2
3 /**
4  * Name: Tumblr Post Connector
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8
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');
15
16 }
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');
23 }
24
25
26 function tumblr_jot_nets(&$a,&$b) {
27     if(! local_user())
28         return;
29
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>';
36     }
37 }
38
39
40 function tumblr_settings(&$a,&$s) {
41
42     if(! local_user())
43         return;
44
45     /* Add our stylesheet to the page so we can make our settings look nice */
46
47     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/tumblr/tumblr.css' . '" media="all" />' . "\r\n";
48
49     /* Get the current state of our config variables */
50
51     $enabled = get_pconfig(local_user(),'tumblr','post');
52
53     $checked = (($enabled) ? ' checked="checked" ' : '');
54
55     $def_enabled = get_pconfig(local_user(),'tumblr','post_by_default');
56
57     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
58
59         $tmbl_username = get_pconfig(local_user(), 'tumblr', 'tumblr_username');
60         $tmbl_password = get_pconfig(local_user(), 'tumblr', 'tumblr_password');
61
62
63     /* Add some HTML to the existing form */
64
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>';
71
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>';
76
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>';
81
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>';
86
87     /* provide a submit button */
88
89     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="tumblr-submit" name="tumblr-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
90
91 }
92
93
94 function tumblr_settings_post(&$a,&$b) {
95
96         if(x($_POST,'tumblr-submit')) {
97
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']));
102
103         }
104
105 }
106
107 function tumblr_post_local(&$a,&$b) {
108
109         // This can probably be changed to allow editing by pointing to a different API endpoint
110
111         if($b['edit'])
112                 return;
113
114         if((! local_user()) || (local_user() != $b['uid']))
115                 return;
116
117         if($b['private'] || $b['parent'])
118                 return;
119
120     $tmbl_post   = intval(get_pconfig(local_user(),'tumblr','post'));
121
122         $tmbl_enable = (($tmbl_post && x($_REQUEST,'tumblr_enable')) ? intval($_REQUEST['tumblr_enable']) : 0);
123
124         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'tumblr','post_by_default')))
125                 $tmbl_enable = 1;
126
127     if(! $tmbl_enable)
128        return;
129
130     if(strlen($b['postopts']))
131        $b['postopts'] .= ',';
132      $b['postopts'] .= 'tumblr';
133 }
134
135
136
137
138 function tumblr_send(&$a,&$b) {
139
140     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
141         return;
142
143     if(! strstr($b['postopts'],'tumblr'))
144         return;
145
146     if($b['parent'] != $b['id'])
147         return;
148
149
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';
153
154         if($tmbl_username && $tmbl_password && $tmbl_blog) {
155
156                 require_once('include/bbcode.php');
157
158                 $tag_arr = array();
159                 $tags = '';
160                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
161
162                 if($x) {
163                         foreach($matches as $mtch) {
164                                 $tag_arr[] = $mtch[2];
165                         }
166                 }
167                 if(count($tag_arr))
168                         $tags = implode(',',$tag_arr);          
169
170
171                 $params = array(
172                         'email' => $tmbl_username,
173                         'password' => $tmbl_password,
174                         'title' => (($b['title']) ? $b['title'] : t('Post from Friendica')),
175                         'type' => 'regular',
176                         'format' => 'html',
177                         'generator' => 'Friendica',
178                         'tags' => $tags,
179                         'body' => bbcode($b['body'])
180                 );
181
182                 $x = post_url($tmbl_blog,$params);
183                 $ret_code = $a->get_curl_code();
184                 if($ret_code == 201)
185                         logger('tumblr_send: success');
186                 elseif($ret_code == 403)
187                         logger('tumblr_send: authentication failure');
188                 else
189                         logger('tumblr_send: general error: ' . print_r($x,true)); 
190
191         }
192 }
193