New Addon Bot detection
[friendica-addons.git/.git] / wppost / wppost.php
1 <?php
2 /**
3  * Name: WordPress Post Connector
4  * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
5  * Version: 1.1
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Database\DBA;
16 use Friendica\Util\Network;
17 use Friendica\Util\Strings;
18 use Friendica\Util\XML;
19
20 function wppost_install()
21 {
22         Hook::register('hook_fork',            'addon/wppost/wppost.php', 'wppost_hook_fork');
23         Hook::register('post_local',           'addon/wppost/wppost.php', 'wppost_post_local');
24         Hook::register('notifier_normal',      'addon/wppost/wppost.php', 'wppost_send');
25         Hook::register('jot_networks',         'addon/wppost/wppost.php', 'wppost_jot_nets');
26         Hook::register('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
27         Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
28 }
29
30 function wppost_uninstall()
31 {
32         Hook::unregister('hook_fork',        'addon/wppost/wppost.php', 'wppost_hook_fork');
33         Hook::unregister('post_local',       'addon/wppost/wppost.php', 'wppost_post_local');
34         Hook::unregister('notifier_normal',  'addon/wppost/wppost.php', 'wppost_send');
35         Hook::unregister('jot_networks',     'addon/wppost/wppost.php', 'wppost_jot_nets');
36         Hook::unregister('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
37         Hook::unregister('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
38
39         // obsolete - remove
40         Hook::unregister('post_local_end',   'addon/wppost/wppost.php', 'wppost_send');
41         Hook::unregister('addon_settings',  'addon/wppost/wppost.php', 'wppost_settings');
42         Hook::unregister('addon_settings_post',  'addon/wppost/wppost.php', 'wppost_settings_post');
43 }
44
45
46 function wppost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
47 {
48         if (!local_user()) {
49                 return;
50         }
51
52         if (PConfig::get(local_user(),'wppost','post')) {
53                 $jotnets_fields[] = [
54                         'type' => 'checkbox',
55                         'field' => [
56                                 'wppost_enable',
57                                 L10n::t('Post to Wordpress'),
58                                 PConfig::get(local_user(),'wppost','post_by_default')
59                         ]
60                 ];
61         }
62 }
63
64
65 function wppost_settings(&$a,&$s) {
66
67         if(! local_user())
68                 return;
69
70         /* Add our stylesheet to the page so we can make our settings look nice */
71
72         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
73
74         /* Get the current state of our config variables */
75
76         $enabled = PConfig::get(local_user(),'wppost','post');
77         $checked = (($enabled) ? ' checked="checked" ' : '');
78
79         $css = (($enabled) ? '' : '-disabled');
80
81         $def_enabled = PConfig::get(local_user(),'wppost','post_by_default');
82         $back_enabled = PConfig::get(local_user(),'wppost','backlink');
83         $shortcheck_enabled = PConfig::get(local_user(),'wppost','shortcheck');
84
85         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
86         $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
87         $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
88
89         $wp_username = PConfig::get(local_user(), 'wppost', 'wp_username');
90         $wp_password = PConfig::get(local_user(), 'wppost', 'wp_password');
91         $wp_blog = PConfig::get(local_user(), 'wppost', 'wp_blog');
92         $wp_backlink_text = PConfig::get(local_user(), 'wppost', 'wp_backlink_text');
93
94
95     /* Add some HTML to the existing form */
96
97     $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
98     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
99     $s .= '</span>';
100     $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
101     $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
102     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
103     $s .= '</span>';
104     $s .= '<div id="wppost-enable-wrapper">';
105     $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . L10n::t('Enable WordPress Post Addon') . '</label>';
106     $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
107     $s .= '</div><div class="clear"></div>';
108
109     $s .= '<div id="wppost-username-wrapper">';
110     $s .= '<label id="wppost-username-label" for="wppost-username">' . L10n::t('WordPress username') . '</label>';
111     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
112     $s .= '</div><div class="clear"></div>';
113
114     $s .= '<div id="wppost-password-wrapper">';
115     $s .= '<label id="wppost-password-label" for="wppost-password">' . L10n::t('WordPress password') . '</label>';
116     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
117     $s .= '</div><div class="clear"></div>';
118
119     $s .= '<div id="wppost-blog-wrapper">';
120     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . L10n::t('WordPress API URL') . '</label>';
121     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
122     $s .= '</div><div class="clear"></div>';
123
124     $s .= '<div id="wppost-bydefault-wrapper">';
125     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . L10n::t('Post to WordPress by default') . '</label>';
126     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
127     $s .= '</div><div class="clear"></div>';
128
129     $s .= '<div id="wppost-backlink-wrapper">';
130     $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . L10n::t('Provide a backlink to the Friendica post') . '</label>';
131     $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
132     $s .= '</div><div class="clear"></div>';
133     $s .= '<div id="wppost-backlinktext-wrapper">';
134     $s .= '<label id="wppost-backlinktext-label" for="wp_backlink_text">' . L10n::t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.') . '</label>';
135     $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
136     $s .= '</div><div class="clear"></div>';
137
138     $s .= '<div id="wppost-shortcheck-wrapper">';
139     $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . L10n::t("Don't post messages that are too short") . '</label>';
140     $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
141     $s .= '</div><div class="clear"></div>';
142
143     /* provide a submit button */
144
145     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
146
147 }
148
149
150 function wppost_settings_post(&$a,&$b) {
151
152         if(!empty($_POST['wppost-submit'])) {
153                 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
154                 PConfig::set(local_user(),'wppost','post_by_default',intval(defaults($_POST, 'wp_bydefault', false)));
155                 PConfig::set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
156                 PConfig::set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
157                 PConfig::set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
158                 PConfig::set(local_user(),'wppost','backlink',trim(defaults($_POST, 'wp_backlink', '')));
159                 PConfig::set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
160                 $wp_backlink_text = Strings::escapeTags(trim($_POST['wp_backlink_text']));
161                 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, 8);
162                 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
163                 PConfig::set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
164         }
165
166 }
167
168 function wppost_hook_fork(&$a, &$b)
169 {
170         if ($b['name'] != 'notifier_normal') {
171                 return;
172         }
173
174         $post = $b['data'];
175
176         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
177                 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
178                 $b['execute'] = false;
179                 return;
180         }
181 }
182
183 function wppost_post_local(&$a, &$b) {
184
185         // This can probably be changed to allow editing by pointing to a different API endpoint
186
187         if ($b['edit']) {
188                 return;
189         }
190
191         if (!local_user() || (local_user() != $b['uid'])) {
192                 return;
193         }
194
195         if ($b['private'] || $b['parent']) {
196                 return;
197         }
198
199         $wp_post   = intval(PConfig::get(local_user(),'wppost','post'));
200
201         $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
202
203         if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
204                 $wp_enable = 1;
205         }
206
207         if (!$wp_enable) {
208                 return;
209         }
210
211         if (strlen($b['postopts'])) {
212                 $b['postopts'] .= ',';
213         }
214
215         $b['postopts'] .= 'wppost';
216 }
217
218
219
220
221 function wppost_send(&$a, &$b)
222 {
223         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
224                 return;
225         }
226
227         if(! strstr($b['postopts'],'wppost')) {
228                 return;
229         }
230
231         if($b['parent'] != $b['id']) {
232                 return;
233         }
234
235         // Dont't post if the post doesn't belong to us.
236         // This is a check for forum postings
237         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
238         if ($b['contact-id'] != $self['id']) {
239                 return;
240         }
241
242         $wp_username = XML::escape(PConfig::get($b['uid'], 'wppost', 'wp_username'));
243         $wp_password = XML::escape(PConfig::get($b['uid'], 'wppost', 'wp_password'));
244         $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
245         $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
246         if ($wp_backlink_text == '') {
247                 $wp_backlink_text = L10n::t('Read the orig­i­nal post and com­ment stream on Friendica');
248         }
249
250         if ($wp_username && $wp_password && $wp_blog) {
251                 $wptitle = trim($b['title']);
252
253                 if (intval(PConfig::get($b['uid'], 'wppost', 'shortcheck'))) {
254                         // Checking, if its a post that is worth a blog post
255                         $postentry = false;
256                         $siteinfo = BBCode::getAttachedData($b["body"]);
257
258                         // Is it a link to an aricle, a video or a photo?
259                         if (isset($siteinfo["type"])) {
260                                 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
261                                         $postentry = true;
262                                 }
263                         }
264
265                         // Does it have a title?
266                         if ($wptitle != "") {
267                                 $postentry = true;
268                         }
269
270                         // Is it larger than 500 characters?
271                         if (strlen($b['body']) > 500) {
272                                 $postentry = true;
273                         }
274
275                         if (!$postentry) {
276                                 return;
277                         }
278                 }
279
280                 // If the title is empty then try to guess
281                 if ($wptitle == '') {
282                         // Fetch information about the post
283                         $siteinfo = BBCode::getAttachedData($b["body"]);
284                         if (isset($siteinfo["title"])) {
285                                 $wptitle = $siteinfo["title"];
286                         }
287
288                         // If no bookmark is found then take the first line
289                         if ($wptitle == '') {
290                                 // Remove the share element before fetching the first line
291                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
292
293                                 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
294                                 $pos = strpos($title, "\n");
295                                 $trailer = "";
296                                 if (($pos == 0) || ($pos > 100)) {
297                                         $pos = 100;
298                                         $trailer = "...";
299                                 }
300
301                                 $wptitle = substr($title, 0, $pos).$trailer;
302                         }
303                 }
304
305                 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
306                 $post = BBCode::convert($b['body'], false, 4);
307
308                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
309                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
310                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
311
312                 $post = $title.$post;
313
314                 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
315                 if($wp_backlink && $b['plink']) {
316                         $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
317                                 . $wp_backlink_text . '</a>' . EOL . EOL;
318                 }
319
320                 $post = XML::escape($post);
321
322
323                 $xml = <<< EOT
324 <?xml version=\"1.0\" encoding=\"utf-8\"?>
325 <methodCall>
326   <methodName>blogger.newPost</methodName>
327   <params>
328     <param><value><string/></value></param>
329     <param><value><string/></value></param>
330     <param><value><string>$wp_username</string></value></param>
331     <param><value><string>$wp_password</string></value></param>
332     <param><value><string>$post</string></value></param>
333     <param><value><int>1</int></value></param>
334   </params>
335 </methodCall>
336
337 EOT;
338
339                 Logger::log('wppost: data: ' . $xml, Logger::DATA);
340
341                 if ($wp_blog !== 'test') {
342                         $x = Network::post($wp_blog, $xml)->getBody();
343                 }
344                 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);
345         }
346 }