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