]> git.mxchange.org Git - friendica-addons.git/blob - dwpost/dwpost.php
Merge pull request #778 from MrPetovan/task/move-config-to-php-array
[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\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;
21
22 function dwpost_install()
23 {
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');
29 }
30
31 function dwpost_uninstall()
32 {
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');
38 }
39
40 function dwpost_jot_nets(App $a, &$b)
41 {
42         if (!local_user()) {
43                 return;
44         }
45
46         $dw_post = PConfig::get(local_user(), 'dwpost', 'post');
47
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" ' : '');
51
52                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="dwpost_enable" ' . $selected . ' value="1" /> '
53                 . L10n::t('Post to Dreamwidth') . '</div>';
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         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/dwpost/dwpost.css' . '" media="all" />' . "\r\n";
66
67         /* Get the current state of our config variables */
68         $enabled = PConfig::get(local_user(), 'dwpost', 'post');
69
70         $checked = (($enabled) ? ' checked="checked" ' : '');
71
72         $def_enabled = PConfig::get(local_user(), 'dwpost', 'post_by_default');
73
74         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
75
76         $dw_username = PConfig::get(local_user(), 'dwpost', 'dw_username');
77         $dw_password = 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">'. 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">'. 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">' . 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">' . 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">' . 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">' . 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="' . 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                 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']));
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(PConfig::get(local_user(),'dwpost','post'));
139
140         $dw_enable = (($dw_post && x($_REQUEST,'dwpost_enable')) ? intval($_REQUEST['dwpost_enable']) : 0);
141
142         if ($b['api_source'] && intval(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 = 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';
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 = dwpost_get_tags($b['tag']);
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 = Network::post($dw_blog, $xml, ["Content-Type: text/xml"])->getBody();
234                 }
235
236                 Logger::log('posted to dreamwidth: ' . ($x) ? $x : '', Logger::DEBUG);
237         }
238 }
239
240 function dwpost_get_tags($post)
241 {
242         preg_match_all("/\]([^\[#]+)\[/", $post, $matches);
243
244         $tags = implode(', ', $matches[1]);
245
246         return $tags;
247 }