]> git.mxchange.org Git - friendica-addons.git/blob - dwpost/dwpost.php
Merge pull request 'Bluesky: Provide the correct user id while fetching content'...
[friendica-addons.git] / dwpost / dwpost.php
1 <?php
2 /**
3  * Name: Dreamwidth Post Connector
4  * Description: Post to dreamwidth
5  * Version: 1.0
6  * Author: Tony Baldwin <https://free-haven.org/profile/tony>
7  * Author: Michael Johnston
8  * Author: Cat Gray <https://free-haven.org/profile/catness>
9  */
10
11 use Friendica\App;
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\DI;
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;
22
23 function dwpost_install()
24 {
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');
30 }
31
32 function dwpost_jot_nets(array &$jotnets_fields)
33 {
34         if (!DI::userSession()->getLocalUserId()) {
35                 return;
36         }
37
38         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post')) {
39                 $jotnets_fields[] = [
40                         'type' => 'checkbox',
41                         'field' => [
42                                 'dwpost_enable',
43                                 DI::l10n()->t('Post to Dreamwidth'),
44                                 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'dwpost', 'post_by_default')
45                         ]
46                 ];
47         }
48 }
49
50
51 function dwpost_settings(array &$data)
52 {
53         if (!DI::userSession()->getLocalUserId()) {
54                 return;
55         }
56
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');
60
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],
67         ]);
68
69         $data = [
70                 'connector' => 'dwpost',
71                 'title'     => DI::l10n()->t('Dreamwidth Export'),
72                 'image'     => 'images/dreamwidth.png',
73                 'enabled'   => $enabled,
74                 'html'      => $html,
75         ];
76 }
77
78
79 function dwpost_settings_post(array &$b)
80 {
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']));
86         }
87 }
88
89 function dwpost_post_local(array &$b)
90 {
91         // This can probably be changed to allow editing by pointing to a different API endpoint
92         if ($b['edit']) {
93                 return;
94         }
95
96         if ((!DI::userSession()->getLocalUserId()) || (DI::userSession()->getLocalUserId() != $b['uid'])) {
97                 return;
98         }
99
100         if ($b['private'] || $b['parent']) {
101                 return;
102         }
103
104         $dw_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'dwpost','post'));
105
106         $dw_enable = (($dw_post && !empty($_REQUEST['dwpost_enable'])) ? intval($_REQUEST['dwpost_enable']) : 0);
107
108         if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'dwpost','post_by_default'))) {
109                 $dw_enable = 1;
110         }
111
112         if (!$dw_enable) {
113                 return;
114         }
115
116         if (strlen($b['postopts'])) {
117                 $b['postopts'] .= ',';
118         }
119
120         $b['postopts'] .= 'dwpost';
121 }
122
123 function dwpost_send(array &$b)
124 {
125         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
126                 return;
127         }
128
129         if (strpos($b['postopts'] ?? '', 'dwpost') === false) {
130                 return;
131         }
132
133         if ($b['parent'] != $b['id']) {
134                 return;
135         }
136
137         $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
138
139         /*
140          * dreamwidth post in the LJ user's timezone.
141          * Hopefully the person's Friendica account
142          * will be set to the same thing.
143          */
144
145         $user = User::getById($b['uid']);
146         $tz = $user['timezone'] ?: 'UTC';
147
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';
151
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]);
157
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));
164
165                 $xml = <<< EOT
166 <?xml version="1.0" encoding="utf-8"?>
167 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
168 <params><param>
169 <value><struct>
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>
182 <value><struct>
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>
186 </struct></value>
187 </param></params>
188 </methodCall>
189
190 EOT;
191
192                 Logger::debug('dwpost: data: ' . $xml);
193
194                 if ($dw_blog !== 'test') {
195                         $x = DI::httpClient()->post($dw_blog, $xml, ['Content-Type' => 'text/xml'])->getBody();
196                 }
197
198                 Logger::info('posted to dreamwidth: ' . ($x) ? $x : '');
199         }
200 }