3 * Name: Blogger Post Connector
4 * Description: Post to Blogger (or anything else which uses blogger XMLRPC API)
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
14 use Friendica\Util\Network;
15 use Friendica\Util\XML;
17 function blogger_install()
19 Hook::register('hook_fork', 'addon/blogger/blogger.php', 'blogger_hook_fork');
20 Hook::register('post_local', 'addon/blogger/blogger.php', 'blogger_post_local');
21 Hook::register('notifier_normal', 'addon/blogger/blogger.php', 'blogger_send');
22 Hook::register('jot_networks', 'addon/blogger/blogger.php', 'blogger_jot_nets');
23 Hook::register('connector_settings', 'addon/blogger/blogger.php', 'blogger_settings');
24 Hook::register('connector_settings_post', 'addon/blogger/blogger.php', 'blogger_settings_post');
27 function blogger_uninstall()
29 Hook::unregister('hook_fork', 'addon/blogger/blogger.php', 'blogger_hook_fork');
30 Hook::unregister('post_local', 'addon/blogger/blogger.php', 'blogger_post_local');
31 Hook::unregister('notifier_normal', 'addon/blogger/blogger.php', 'blogger_send');
32 Hook::unregister('jot_networks', 'addon/blogger/blogger.php', 'blogger_jot_nets');
33 Hook::unregister('connector_settings', 'addon/blogger/blogger.php', 'blogger_settings');
34 Hook::unregister('connector_settings_post', 'addon/blogger/blogger.php', 'blogger_settings_post');
37 Hook::unregister('post_local_end', 'addon/blogger/blogger.php', 'blogger_send');
38 Hook::unregister('addon_settings', 'addon/blogger/blogger.php', 'blogger_settings');
39 Hook::unregister('addon_settings_post', 'addon/blogger/blogger.php', 'blogger_settings_post');
43 function blogger_jot_nets(App $a, array &$jotnets_fields)
49 if (DI::pConfig()->get(local_user(), 'blogger', 'post')) {
54 DI::l10n()->t('Post to blogger'),
55 DI::pConfig()->get(local_user(), 'blogger', 'post_by_default')
62 function blogger_settings(App $a, &$s)
68 /* Add our stylesheet to the page so we can make our settings look nice */
70 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/blogger/blogger.css' . '" media="all" />' . "\r\n";
72 /* Get the current state of our config variables */
74 $enabled = DI::pConfig()->get(local_user(), 'blogger', 'post');
75 $checked = (($enabled) ? ' checked="checked" ' : '');
76 $css = (($enabled) ? '' : '-disabled');
78 $def_enabled = DI::pConfig()->get(local_user(), 'blogger', 'post_by_default');
80 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
82 $bl_username = DI::pConfig()->get(local_user(), 'blogger', 'bl_username');
83 $bl_password = DI::pConfig()->get(local_user(), 'blogger', 'bl_password');
84 $bl_blog = DI::pConfig()->get(local_user(), 'blogger', 'bl_blog');
86 /* Add some HTML to the existing form */
87 $s .= '<span id="settings_blogger_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_blogger_expanded\'); openClose(\'settings_blogger_inflated\');">';
88 $s .= '<img class="connector'.$css.'" src="images/blogger.png" /><h3 class="connector">'. DI::l10n()->t('Blogger Export').'</h3>';
90 $s .= '<div id="settings_blogger_expanded" class="settings-block" style="display: none;">';
91 $s .= '<span class="fakelink" onclick="openClose(\'settings_blogger_expanded\'); openClose(\'settings_blogger_inflated\');">';
92 $s .= '<img class="connector'.$css.'" src="images/blogger.png" /><h3 class="connector">'. DI::l10n()->t('Blogger Export').'</h3>';
95 $s .= '<div id="blogger-enable-wrapper">';
96 $s .= '<label id="blogger-enable-label" for="blogger-checkbox">' . DI::l10n()->t('Enable Blogger Post Addon') . '</label>';
97 $s .= '<input id="blogger-checkbox" type="checkbox" name="blogger" value="1" ' . $checked . '/>';
98 $s .= '</div><div class="clear"></div>';
100 $s .= '<div id="blogger-username-wrapper">';
101 $s .= '<label id="blogger-username-label" for="blogger-username">' . DI::l10n()->t('Blogger username') . '</label>';
102 $s .= '<input id="blogger-username" type="text" name="bl_username" value="' . $bl_username . '" />';
103 $s .= '</div><div class="clear"></div>';
105 $s .= '<div id="blogger-password-wrapper">';
106 $s .= '<label id="blogger-password-label" for="blogger-password">' . DI::l10n()->t('Blogger password') . '</label>';
107 $s .= '<input id="blogger-password" type="password" name="bl_password" value="' . $bl_password . '" />';
108 $s .= '</div><div class="clear"></div>';
110 $s .= '<div id="blogger-blog-wrapper">';
111 $s .= '<label id="blogger-blog-label" for="blogger-blog">' . DI::l10n()->t('Blogger API URL') . '</label>';
112 $s .= '<input id="blogger-blog" type="text" name="bl_blog" value="' . $bl_blog . '" />';
113 $s .= '</div><div class="clear"></div>';
115 $s .= '<div id="blogger-bydefault-wrapper">';
116 $s .= '<label id="blogger-bydefault-label" for="blogger-bydefault">' . DI::l10n()->t('Post to Blogger by default') . '</label>';
117 $s .= '<input id="blogger-bydefault" type="checkbox" name="bl_bydefault" value="1" ' . $def_checked . '/>';
118 $s .= '</div><div class="clear"></div>';
120 /* provide a submit button */
121 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="blogger-submit" name="blogger-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
125 function blogger_settings_post(App $a, array &$b)
127 if (!empty($_POST['blogger-submit'])) {
128 DI::pConfig()->set(local_user(), 'blogger', 'post', $_POST['blogger'] ?? false);
129 DI::pConfig()->set(local_user(), 'blogger', 'post_by_default', $_POST['bl_bydefault'] ?? false);
130 DI::pConfig()->set(local_user(), 'blogger', 'bl_username', trim($_POST['bl_username']));
131 DI::pConfig()->set(local_user(), 'blogger', 'bl_password', trim($_POST['bl_password']));
132 DI::pConfig()->set(local_user(), 'blogger', 'bl_blog', trim($_POST['bl_blog']));
136 function blogger_hook_fork(App &$a, array &$b)
138 if ($b['name'] != 'notifier_normal') {
144 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
145 !strstr($post['postopts'], 'blogger') || ($post['parent'] != $post['id'])) {
146 $b['execute'] = false;
151 function blogger_post_local(App $a, array &$b)
153 // This can probably be changed to allow editing by pointing to a different API endpoint
159 if (!local_user() || (local_user() != $b['uid'])) {
163 if ($b['private'] || $b['parent']) {
167 $bl_post = intval(DI::pConfig()->get(local_user(), 'blogger', 'post'));
169 $bl_enable = (($bl_post && !empty($_REQUEST['blogger_enable'])) ? intval($_REQUEST['blogger_enable']) : 0);
171 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'blogger', 'post_by_default'))) {
179 if (strlen($b['postopts'])) {
180 $b['postopts'] .= ',';
183 $b['postopts'] .= 'blogger';
186 function blogger_send(App $a, array &$b)
188 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
192 if (! strstr($b['postopts'], 'blogger')) {
196 if ($b['parent'] != $b['id']) {
200 $bl_username = XML::escape(DI::pConfig()->get($b['uid'], 'blogger', 'bl_username'));
201 $bl_password = XML::escape(DI::pConfig()->get($b['uid'], 'blogger', 'bl_password'));
202 $bl_blog = DI::pConfig()->get($b['uid'], 'blogger', 'bl_blog');
204 if ($bl_username && $bl_password && $bl_blog) {
205 $title = '<title>' . (($b['title']) ? $b['title'] : DI::l10n()->t('Post from Friendica')) . '</title>';
206 $post = $title . BBCode::convert($b['body']);
207 $post = XML::escape($post);
210 <?xml version=\"1.0\" encoding=\"utf-8\"?>
212 <methodName>blogger.newPost</methodName>
214 <param><value><string/></value></param>
215 <param><value><string/></value></param>
216 <param><value><string>$bl_username</string></value></param>
217 <param><value><string>$bl_password</string></value></param>
218 <param><value><string>$post</string></value></param>
219 <param><value><int>1</int></value></param>
225 Logger::log('blogger: data: ' . $xml, Logger::DATA);
227 if ($bl_blog !== 'test') {
228 $x = Network::post($bl_blog, $xml)->getBody();
231 Logger::log('posted to blogger: ' . (($x) ? $x : ''), Logger::DEBUG);