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\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
17 use Friendica\Model\Post;
18 use Friendica\Model\Tag;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\XML;
23 function dwpost_install()
25 Hook::register('post_local', 'addon/dwpost/dwpost.php', 'dwpost_post_local');
26 Hook::register('notifier_normal', 'addon/dwpost/dwpost.php', 'dwpost_send');
27 Hook::register('jot_networks', 'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
28 Hook::register('connector_settings', 'addon/dwpost/dwpost.php', 'dwpost_settings');
29 Hook::register('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
32 function dwpost_jot_nets(array &$jotnets_fields)
34 if (!DI::userSession()->getLocalUserId()) {
38 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post')) {
43 DI::l10n()->t('Post to Dreamwidth'),
44 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post_by_default')
51 function dwpost_settings(array &$data)
53 if (!DI::userSession()->getLocalUserId()) {
57 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post', false);
58 $dw_username = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'dw_username');
59 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post_by_default');
61 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/dwpost/');
62 $html = Renderer::replaceMacros($t, [
63 '$enabled' => ['dwpost', DI::l10n()->t('Enable Dreamwidth Post Addon'), $enabled],
64 '$username' => ['dw_username', DI::l10n()->t('Dreamwidth username'), $dw_username],
65 '$password' => ['dw_password', DI::l10n()->t('Dreamwidth password')],
66 '$bydefault' => ['dw_bydefault', DI::l10n()->t('Post to Dreamwidth by default'), $def_enabled],
70 'connector' => 'dwpost',
71 'title' => DI::l10n()->t('Dreamwidth Export'),
72 'image' => 'images/dreamwidth.png',
73 'enabled' => $enabled,
79 function dwpost_settings_post(array &$b)
81 if (!empty($_POST['dwpost-submit'])) {
82 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'dwpost', 'post', intval($_POST['dwpost']));
83 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'dwpost', 'post_by_default', intval($_POST['dw_bydefault']));
84 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'dwpost', 'dw_username', trim($_POST['dw_username']));
85 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'dwpost', 'dw_password', trim($_POST['dw_password']));
89 function dwpost_post_local(array &$b)
91 // This can probably be changed to allow editing by pointing to a different API endpoint
96 if ((!DI::userSession()->getLocalUserId()) || (DI::userSession()->getLocalUserId() != $b['uid'])) {
100 if ($b['private'] || $b['parent']) {
104 $dw_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'dwpost','post'));
106 $dw_enable = (($dw_post && !empty($_REQUEST['dwpost_enable'])) ? intval($_REQUEST['dwpost_enable']) : 0);
108 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'dwpost','post_by_default'))) {
116 if (strlen($b['postopts'])) {
117 $b['postopts'] .= ',';
120 $b['postopts'] .= 'dwpost';
123 function dwpost_send(array &$b)
125 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
129 if (strpos($b['postopts'] ?? '', 'dwpost') === false) {
133 if ($b['parent'] != $b['id']) {
137 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
140 * dreamwidth post in the LJ user's timezone.
141 * Hopefully the person's Friendica account
142 * will be set to the same thing.
145 $user = User::getById($b['uid']);
146 $tz = $user['timezone'] ?: 'UTC';
148 $dw_username = DI::pConfig()->get($b['uid'],'dwpost','dw_username');
149 $dw_password = DI::pConfig()->get($b['uid'],'dwpost','dw_password');
150 $dw_blog = 'http://www.dreamwidth.org/interface/xmlrpc';
152 if ($dw_username && $dw_password && $dw_blog) {
153 $title = $b['title'];
154 $post = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
155 $post = XML::escape($post);
156 $tags = Tag::getCSVByURIId($b['uri-id'], [Tag::HASHTAG]);
158 $date = DateTimeFormat::convert($b['created'], $tz);
159 $year = intval(substr($date,0,4));
160 $mon = intval(substr($date,5,2));
161 $day = intval(substr($date,8,2));
162 $hour = intval(substr($date,11,2));
163 $min = intval(substr($date,14,2));
166 <?xml version="1.0" encoding="utf-8"?>
167 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
170 <member><name>year</name><value><int>$year</int></value></member>
171 <member><name>mon</name><value><int>$mon</int></value></member>
172 <member><name>day</name><value><int>$day</int></value></member>
173 <member><name>hour</name><value><int>$hour</int></value></member>
174 <member><name>min</name><value><int>$min</int></value></member>
175 <member><name>event</name><value><string>$post</string></value></member>
176 <member><name>username</name><value><string>$dw_username</string></value></member>
177 <member><name>password</name><value><string>$dw_password</string></value></member>
178 <member><name>subject</name><value><string>$title</string></value></member>
179 <member><name>lineendings</name><value><string>unix</string></value></member>
180 <member><name>ver</name><value><int>1</int></value></member>
181 <member><name>props</name>
183 <member><name>useragent</name><value><string>Friendica</string></value></member>
184 <member><name>taglist</name><value><string>$tags</string></value></member>
185 </struct></value></member>
192 Logger::debug('dwpost: data: ' . $xml);
194 if ($dw_blog !== 'test') {
195 $x = DI::httpClient()->post($dw_blog, $xml, ['Content-Type' => 'text/xml'])->getBody();
198 Logger::info('posted to dreamwidth: ' . ($x) ? $x : '');