3 * Name: Dreamwidth Post Connector
4 * Description: Post to dreamwidth
6 * Author: Tony Baldwin <https://free-haven.org/profile/tony>
7 * Author: Michael Johnston
8 * Author: Cat Gray <https://free-haven.org/profile/catness>
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Addon;
14 use Friendica\Core\L10n;
15 use Friendica\Core\Logger;
16 use Friendica\Core\PConfig;
17 use Friendica\Database\DBA;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Network;
20 use Friendica\Util\XML;
22 function dwpost_install()
24 Addon::registerHook('post_local', 'addon/dwpost/dwpost.php', 'dwpost_post_local');
25 Addon::registerHook('notifier_normal', 'addon/dwpost/dwpost.php', 'dwpost_send');
26 Addon::registerHook('jot_networks', 'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
27 Addon::registerHook('connector_settings', 'addon/dwpost/dwpost.php', 'dwpost_settings');
28 Addon::registerHook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
31 function dwpost_uninstall()
33 Addon::unregisterHook('post_local', 'addon/dwpost/dwpost.php', 'dwpost_post_local');
34 Addon::unregisterHook('notifier_normal', 'addon/dwpost/dwpost.php', 'dwpost_send');
35 Addon::unregisterHook('jot_networks', 'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
36 Addon::unregisterHook('connector_settings', 'addon/dwpost/dwpost.php', 'dwpost_settings');
37 Addon::unregisterHook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
40 function dwpost_jot_nets(App $a, &$b)
46 $dw_post = PConfig::get(local_user(), 'dwpost', 'post');
48 if (intval($dw_post) == 1) {
49 $dw_defpost = PConfig::get(local_user(), 'dwpost', 'post_by_default');
50 $selected = ((intval($dw_defpost) == 1) ? ' checked="checked" ' : '');
52 $b .= '<div class="profile-jot-net"><input type="checkbox" name="dwpost_enable" ' . $selected . ' value="1" /> '
53 . L10n::t('Post to Dreamwidth') . '</div>';
58 function dwpost_settings(App $a, &$s)
64 /* Add our stylesheet to the page so we can make our settings look nice */
65 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/dwpost/dwpost.css' . '" media="all" />' . "\r\n";
67 /* Get the current state of our config variables */
68 $enabled = PConfig::get(local_user(), 'dwpost', 'post');
70 $checked = (($enabled) ? ' checked="checked" ' : '');
72 $def_enabled = PConfig::get(local_user(), 'dwpost', 'post_by_default');
74 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
76 $dw_username = PConfig::get(local_user(), 'dwpost', 'dw_username');
77 $dw_password = PConfig::get(local_user(), 'dwpost', 'dw_password');
79 /* Add some HTML to the existing form */
80 $s .= '<span id="settings_dwpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_dwpost_expanded\'); openClose(\'settings_dwpost_inflated\');">';
81 $s .= '<img class="connector" src="images/dreamwidth.png" /><h3 class="connector">'. L10n::t("Dreamwidth Export").'</h3>';
83 $s .= '<div id="settings_dwpost_expanded" class="settings-block" style="display: none;">';
84 $s .= '<span class="fakelink" onclick="openClose(\'settings_dwpost_expanded\'); openClose(\'settings_dwpost_inflated\');">';
85 $s .= '<img class="connector" src="images/dreamwidth.png" /><h3 class="connector">'. L10n::t("Dreamwidth Export").'</h3>';
88 $s .= '<div id="dwpost-enable-wrapper">';
89 $s .= '<label id="dwpost-enable-label" for="dwpost-checkbox">' . L10n::t('Enable dreamwidth Post Addon') . '</label>';
90 $s .= '<input id="dwpost-checkbox" type="checkbox" name="dwpost" value="1" ' . $checked . '/>';
91 $s .= '</div><div class="clear"></div>';
93 $s .= '<div id="dwpost-username-wrapper">';
94 $s .= '<label id="dwpost-username-label" for="dwpost-username">' . L10n::t('dreamwidth username') . '</label>';
95 $s .= '<input id="dwpost-username" type="text" name="dw_username" value="' . $dw_username . '" />';
96 $s .= '</div><div class="clear"></div>';
98 $s .= '<div id="dwpost-password-wrapper">';
99 $s .= '<label id="dwpost-password-label" for="dwpost-password">' . L10n::t('dreamwidth password') . '</label>';
100 $s .= '<input id="dwpost-password" type="password" name="dw_password" value="' . $dw_password . '" />';
101 $s .= '</div><div class="clear"></div>';
103 $s .= '<div id="dwpost-bydefault-wrapper">';
104 $s .= '<label id="dwpost-bydefault-label" for="dwpost-bydefault">' . L10n::t('Post to dreamwidth by default') . '</label>';
105 $s .= '<input id="dwpost-bydefault" type="checkbox" name="dw_bydefault" value="1" ' . $def_checked . '/>';
106 $s .= '</div><div class="clear"></div>';
108 /* provide a submit button */
109 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="dwpost-submit" name="dwpost-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
113 function dwpost_settings_post(App $a, array &$b)
115 if (!empty($_POST['dwpost-submit'])) {
116 PConfig::set(local_user(), 'dwpost', 'post', intval($_POST['dwpost']));
117 PConfig::set(local_user(), 'dwpost', 'post_by_default', intval($_POST['dw_bydefault']));
118 PConfig::set(local_user(), 'dwpost', 'dw_username', trim($_POST['dw_username']));
119 PConfig::set(local_user(), 'dwpost', 'dw_password', trim($_POST['dw_password']));
123 function dwpost_post_local(App $a, array &$b)
125 // This can probably be changed to allow editing by pointing to a different API endpoint
130 if ((!local_user()) || (local_user() != $b['uid'])) {
134 if ($b['private'] || $b['parent']) {
138 $dw_post = intval(PConfig::get(local_user(),'dwpost','post'));
140 $dw_enable = (($dw_post && x($_REQUEST,'dwpost_enable')) ? intval($_REQUEST['dwpost_enable']) : 0);
142 if ($b['api_source'] && intval(PConfig::get(local_user(),'dwpost','post_by_default'))) {
150 if (strlen($b['postopts'])) {
151 $b['postopts'] .= ',';
154 $b['postopts'] .= 'dwpost';
157 function dwpost_send(App $a, array &$b)
159 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
163 if (!strstr($b['postopts'],'dwpost')) {
167 if ($b['parent'] != $b['id']) {
172 * dreamwidth post in the LJ user's timezone.
173 * Hopefully the person's Friendica account
174 * will be set to the same thing.
178 $x = q("SELECT `timezone` FROM `user` WHERE `uid` = %d LIMIT 1",
182 if (DBA::isResult($x) && !empty($x[0]['timezone'])) {
183 $tz = $x[0]['timezone'];
186 $dw_username = PConfig::get($b['uid'],'dwpost','dw_username');
187 $dw_password = PConfig::get($b['uid'],'dwpost','dw_password');
188 $dw_blog = 'http://www.dreamwidth.org/interface/xmlrpc';
190 if ($dw_username && $dw_password && $dw_blog) {
191 $title = $b['title'];
192 $post = BBCode::convert($b['body']);
193 $post = XML::escape($post);
194 $tags = dwpost_get_tags($b['tag']);
196 $date = DateTimeFormat::convert($b['created'], $tz);
197 $year = intval(substr($date,0,4));
198 $mon = intval(substr($date,5,2));
199 $day = intval(substr($date,8,2));
200 $hour = intval(substr($date,11,2));
201 $min = intval(substr($date,14,2));
204 <?xml version="1.0" encoding="utf-8"?>
205 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
208 <member><name>year</name><value><int>$year</int></value></member>
209 <member><name>mon</name><value><int>$mon</int></value></member>
210 <member><name>day</name><value><int>$day</int></value></member>
211 <member><name>hour</name><value><int>$hour</int></value></member>
212 <member><name>min</name><value><int>$min</int></value></member>
213 <member><name>event</name><value><string>$post</string></value></member>
214 <member><name>username</name><value><string>$dw_username</string></value></member>
215 <member><name>password</name><value><string>$dw_password</string></value></member>
216 <member><name>subject</name><value><string>$title</string></value></member>
217 <member><name>lineendings</name><value><string>unix</string></value></member>
218 <member><name>ver</name><value><int>1</int></value></member>
219 <member><name>props</name>
221 <member><name>useragent</name><value><string>Friendica</string></value></member>
222 <member><name>taglist</name><value><string>$tags</string></value></member>
223 </struct></value></member>
230 Logger::log('dwpost: data: ' . $xml, Logger::DATA);
232 if ($dw_blog !== 'test') {
233 $x = Network::post($dw_blog, $xml, ["Content-Type: text/xml"])->getBody();
236 Logger::log('posted to dreamwidth: ' . ($x) ? $x : '', Logger::DEBUG);
240 function dwpost_get_tags($post)
242 preg_match_all("/\]([^\[#]+)\[/", $post, $matches);
244 $tags = implode(', ', $matches[1]);