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