]> git.mxchange.org Git - friendica-addons.git/blob - dwpost/dwpost.php
Rename "fetchUrl" and "fetchUrlFull" to "fetch" and "fetchFull"
[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\Database\DBA;
16 use Friendica\DI;
17 use Friendica\Model\Tag;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\XML;
20
21 function dwpost_install()
22 {
23         Hook::register('post_local',              'addon/dwpost/dwpost.php', 'dwpost_post_local');
24         Hook::register('notifier_normal',         'addon/dwpost/dwpost.php', 'dwpost_send');
25         Hook::register('jot_networks',            'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
26         Hook::register('connector_settings',      'addon/dwpost/dwpost.php', 'dwpost_settings');
27         Hook::register('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
28 }
29
30 function dwpost_uninstall()
31 {
32         Hook::unregister('post_local',              'addon/dwpost/dwpost.php', 'dwpost_post_local');
33         Hook::unregister('notifier_normal',         'addon/dwpost/dwpost.php', 'dwpost_send');
34         Hook::unregister('jot_networks',            'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
35         Hook::unregister('connector_settings',      'addon/dwpost/dwpost.php', 'dwpost_settings');
36         Hook::unregister('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
37 }
38
39 function dwpost_jot_nets(App $a, array &$jotnets_fields)
40 {
41         if (!local_user()) {
42                 return;
43         }
44
45         if (DI::pConfig()->get(local_user(), 'dwpost', 'post')) {
46                 $jotnets_fields[] = [
47                         'type' => 'checkbox',
48                         'field' => [
49                                 'dwpost_enable',
50                                 DI::l10n()->t('Post to Dreamwidth'),
51                                 DI::pConfig()->get(local_user(), 'dwpost', 'post_by_default')
52                         ]
53                 ];
54         }
55 }
56
57
58 function dwpost_settings(App $a, &$s)
59 {
60         if (!local_user()) {
61                 return;
62         }
63
64         /* Add our stylesheet to the page so we can make our settings look nice */
65         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/dwpost/dwpost.css' . '" media="all" />' . "\r\n";
66
67         /* Get the current state of our config variables */
68         $enabled = DI::pConfig()->get(local_user(), 'dwpost', 'post');
69
70         $checked = (($enabled) ? ' checked="checked" ' : '');
71
72         $def_enabled = DI::pConfig()->get(local_user(), 'dwpost', 'post_by_default');
73
74         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
75
76         $dw_username = DI::pConfig()->get(local_user(), 'dwpost', 'dw_username');
77         $dw_password = DI::pConfig()->get(local_user(), 'dwpost', 'dw_password');
78
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">'. DI::l10n()->t("Dreamwidth Export").'</h3>';
82         $s .= '</span>';
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">'. DI::l10n()->t("Dreamwidth Export").'</h3>';
86         $s .= '</span>';
87
88         $s .= '<div id="dwpost-enable-wrapper">';
89         $s .= '<label id="dwpost-enable-label" for="dwpost-checkbox">' . DI::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>';
92
93         $s .= '<div id="dwpost-username-wrapper">';
94         $s .= '<label id="dwpost-username-label" for="dwpost-username">' . DI::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>';
97
98         $s .= '<div id="dwpost-password-wrapper">';
99         $s .= '<label id="dwpost-password-label" for="dwpost-password">' . DI::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>';
102
103         $s .= '<div id="dwpost-bydefault-wrapper">';
104         $s .= '<label id="dwpost-bydefault-label" for="dwpost-bydefault">' . DI::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>';
107
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="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
110 }
111
112
113 function dwpost_settings_post(App $a, array &$b)
114 {
115         if (!empty($_POST['dwpost-submit'])) {
116                 DI::pConfig()->set(local_user(), 'dwpost', 'post',            intval($_POST['dwpost']));
117                 DI::pConfig()->set(local_user(), 'dwpost', 'post_by_default', intval($_POST['dw_bydefault']));
118                 DI::pConfig()->set(local_user(), 'dwpost', 'dw_username',     trim($_POST['dw_username']));
119                 DI::pConfig()->set(local_user(), 'dwpost', 'dw_password',     trim($_POST['dw_password']));
120         }
121 }
122
123 function dwpost_post_local(App $a, array &$b)
124 {
125         // This can probably be changed to allow editing by pointing to a different API endpoint
126         if ($b['edit']) {
127                 return;
128         }
129
130         if ((!local_user()) || (local_user() != $b['uid'])) {
131                 return;
132         }
133
134         if ($b['private'] || $b['parent']) {
135                 return;
136         }
137
138         $dw_post = intval(DI::pConfig()->get(local_user(),'dwpost','post'));
139
140         $dw_enable = (($dw_post && !empty($_REQUEST['dwpost_enable'])) ? intval($_REQUEST['dwpost_enable']) : 0);
141
142         if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'dwpost','post_by_default'))) {
143                 $dw_enable = 1;
144         }
145
146         if (!$dw_enable) {
147                 return;
148         }
149
150         if (strlen($b['postopts'])) {
151                 $b['postopts'] .= ',';
152         }
153
154         $b['postopts'] .= 'dwpost';
155 }
156
157 function dwpost_send(App $a, array &$b)
158 {
159         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
160                 return;
161         }
162
163         if (!strstr($b['postopts'],'dwpost')) {
164                 return;
165         }
166
167         if ($b['parent'] != $b['id']) {
168                 return;
169         }
170
171         /*
172          * dreamwidth post in the LJ user's timezone.
173          * Hopefully the person's Friendica account
174          * will be set to the same thing.
175          */
176         $tz = 'UTC';
177
178         $x = q("SELECT `timezone` FROM `user` WHERE `uid` = %d LIMIT 1",
179                 intval($b['uid'])
180         );
181
182         if (DBA::isResult($x) && !empty($x[0]['timezone'])) {
183                 $tz = $x[0]['timezone'];
184         }
185
186         $dw_username = DI::pConfig()->get($b['uid'],'dwpost','dw_username');
187         $dw_password = DI::pConfig()->get($b['uid'],'dwpost','dw_password');
188         $dw_blog = 'http://www.dreamwidth.org/interface/xmlrpc';
189
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 = Tag::getCSVByURIId($b['uri-id'], [Tag::HASHTAG]);
195
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));
202
203                 $xml = <<< EOT
204 <?xml version="1.0" encoding="utf-8"?>
205 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
206 <params><param>
207 <value><struct>
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>
220 <value><struct>
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>
224 </struct></value>
225 </param></params>
226 </methodCall>
227
228 EOT;
229
230                 Logger::log('dwpost: data: ' . $xml, Logger::DATA);
231
232                 if ($dw_blog !== 'test') {
233                         $x = DI::httpRequest()->post($dw_blog, $xml, ["Content-Type: text/xml"])->getBody();
234                 }
235
236                 Logger::log('posted to dreamwidth: ' . ($x) ? $x : '', Logger::DEBUG);
237         }
238 }