Merge pull request #976 from annando/tags
[friendica-addons.git/.git] / ijpost / ijpost.php
1 <?php
2 /**
3  * Name: Insanejournal Post Connector
4  * Description: Post to Insanejournal
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\Content\Text\BBCode;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
14 use Friendica\DI;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Network;
17 use Friendica\Util\XML;
18
19 function ijpost_install()
20 {
21         Hook::register('post_local',           'addon/ijpost/ijpost.php', 'ijpost_post_local');
22         Hook::register('notifier_normal',      'addon/ijpost/ijpost.php', 'ijpost_send');
23         Hook::register('jot_networks',         'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
24         Hook::register('connector_settings',      'addon/ijpost/ijpost.php', 'ijpost_settings');
25         Hook::register('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
26 }
27
28 function ijpost_uninstall()
29 {
30         Hook::unregister('post_local',       'addon/ijpost/ijpost.php', 'ijpost_post_local');
31         Hook::unregister('notifier_normal',  'addon/ijpost/ijpost.php', 'ijpost_send');
32         Hook::unregister('jot_networks',     'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
33         Hook::unregister('connector_settings',      'addon/ijpost/ijpost.php', 'ijpost_settings');
34         Hook::unregister('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
35 }
36
37 function ijpost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
38 {
39         if (!local_user()) {
40                 return;
41         }
42
43         if (DI::pConfig()->get(local_user(), 'ijpost', 'post')) {
44                 $jotnets_fields[] = [
45                         'type' => 'checkbox',
46                         'field' => [
47                                 'ijpost_enable',
48                                 DI::l10n()->t('Post to Insanejournal'),
49                                 DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default')
50                         ]
51                 ];
52         }
53 }
54
55 function ijpost_settings(&$a, &$s)
56 {
57         if (!local_user()) {
58                 return;
59         }
60
61         /* Add our stylesheet to the page so we can make our settings look nice */
62
63         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
64
65         /* Get the current state of our config variables */
66
67         $enabled = DI::pConfig()->get(local_user(), 'ijpost', 'post');
68
69         $checked = (($enabled) ? ' checked="checked" ' : '');
70
71         $def_enabled = DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default');
72
73         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
74
75         $ij_username = DI::pConfig()->get(local_user(), 'ijpost', 'ij_username');
76         $ij_password = DI::pConfig()->get(local_user(), 'ijpost', 'ij_password');
77
78         /* Add some HTML to the existing form */
79         $s .= '<span id="settings_ijpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
80         $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. DI::l10n()->t("InsaneJournal Export").'</h3>';
81         $s .= '</span>';
82         $s .= '<div id="settings_ijpost_expanded" class="settings-block" style="display: none;">';
83         $s .= '<span class="fakelink" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
84         $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. DI::l10n()->t("InsaneJournal Export").'</h3>';
85         $s .= '</span>';
86
87         $s .= '<div id="ijpost-enable-wrapper">';
88         $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . DI::l10n()->t('Enable InsaneJournal Post Addon') . '</label>';
89         $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
90         $s .= '</div><div class="clear"></div>';
91
92         $s .= '<div id="ijpost-username-wrapper">';
93         $s .= '<label id="ijpost-username-label" for="ijpost-username">' . DI::l10n()->t('InsaneJournal username') . '</label>';
94         $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
95         $s .= '</div><div class="clear"></div>';
96
97         $s .= '<div id="ijpost-password-wrapper">';
98         $s .= '<label id="ijpost-password-label" for="ijpost-password">' . DI::l10n()->t('InsaneJournal password') . '</label>';
99         $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
100         $s .= '</div><div class="clear"></div>';
101
102         $s .= '<div id="ijpost-bydefault-wrapper">';
103         $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . DI::l10n()->t('Post to InsaneJournal by default') . '</label>';
104         $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_bydefault" value="1" ' . $def_checked . '/>';
105         $s .= '</div><div class="clear"></div>';
106
107         /* provide a submit button */
108         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ijpost-submit" name="ijpost-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
109 }
110
111 function ijpost_settings_post(&$a, &$b)
112 {
113         if (!empty($_POST['ijpost-submit'])) {
114                 DI::pConfig()->set(local_user(), 'ijpost', 'post', intval($_POST['ijpost']));
115                 DI::pConfig()->set(local_user(), 'ijpost', 'post_by_default', intval($_POST['ij_bydefault']));
116                 DI::pConfig()->set(local_user(), 'ijpost', 'ij_username', trim($_POST['ij_username']));
117                 DI::pConfig()->set(local_user(), 'ijpost', 'ij_password', trim($_POST['ij_password']));
118         }
119 }
120
121 function ijpost_post_local(&$a, &$b)
122 {
123         // This can probably be changed to allow editing by pointing to a different API endpoint
124
125         if ($b['edit']) {
126                 return;
127         }
128
129         if (!local_user() || (local_user() != $b['uid'])) {
130                 return;
131         }
132
133         if ($b['private'] || $b['parent']) {
134                 return;
135         }
136
137         $ij_post   = intval(DI::pConfig()->get(local_user(), 'ijpost', 'post'));
138
139         $ij_enable = (($ij_post && !empty($_REQUEST['ijpost_enable'])) ? intval($_REQUEST['ijpost_enable']) : 0);
140
141         if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default'))) {
142                 $ij_enable = 1;
143         }
144
145         if (!$ij_enable) {
146                 return;
147         }
148
149         if (strlen($b['postopts'])) {
150                 $b['postopts'] .= ',';
151         }
152
153         $b['postopts'] .= 'ijpost';
154 }
155
156 function ijpost_send(&$a, &$b)
157 {
158         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
159                 return;
160         }
161
162         if (!strstr($b['postopts'], 'ijpost')) {
163                 return;
164         }
165
166         if ($b['parent'] != $b['id']) {
167                 return;
168         }
169
170         // insanejournal post in the LJ user's timezone.
171         // Hopefully the person's Friendica account
172         // will be set to the same thing.
173
174         $tz = 'UTC';
175
176         $x = q("select timezone from user where uid = %d limit 1",
177                 intval($b['uid'])
178         );
179
180         if ($x && strlen($x[0]['timezone'])) {
181                 $tz = $x[0]['timezone'];
182         }
183
184         $ij_username = DI::pConfig()->get($b['uid'], 'ijpost', 'ij_username');
185         $ij_password = DI::pConfig()->get($b['uid'], 'ijpost', 'ij_password');
186         $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
187
188         if ($ij_username && $ij_password && $ij_blog) {
189                 $title = $b['title'];
190                 $post = BBCode::convert($b['body']);
191                 $post = XML::escape($post);
192                 $tags = ijpost_get_tags($b['tag']);
193
194                 $date = DateTimeFormat::convert($b['created'], $tz);
195                 $year = intval(substr($date,0,4));
196                 $mon  = intval(substr($date,5,2));
197                 $day  = intval(substr($date,8,2));
198                 $hour = intval(substr($date,11,2));
199                 $min  = intval(substr($date,14,2));
200
201                 $xml = <<< EOT
202 <?xml version="1.0" encoding="utf-8"?>
203 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
204 <params><param>
205 <value><struct>
206 <member><name>year</name><value><int>$year</int></value></member>
207 <member><name>mon</name><value><int>$mon</int></value></member>
208 <member><name>day</name><value><int>$day</int></value></member>
209 <member><name>hour</name><value><int>$hour</int></value></member>
210 <member><name>min</name><value><int>$min</int></value></member>
211 <member><name>event</name><value><string>$post</string></value></member>
212 <member><name>username</name><value><string>$ij_username</string></value></member>
213 <member><name>password</name><value><string>$ij_password</string></value></member>
214 <member><name>subject</name><value><string>$title</string></value></member>
215 <member><name>lineendings</name><value><string>unix</string></value></member>
216 <member><name>ver</name><value><int>1</int></value></member>
217 <member><name>props</name>
218 <value><struct>
219 <member><name>useragent</name><value><string>Friendica</string></value></member>
220 <member><name>taglist</name><value><string>$tags</string></value></member>
221 </struct></value></member>
222 </struct></value>
223 </param></params>
224 </methodCall>
225
226 EOT;
227
228                 Logger::log('ijpost: data: ' . $xml, Logger::DATA);
229
230                 if ($ij_blog !== 'test') {
231                         $x = Network::post($ij_blog, $xml, ["Content-Type: text/xml"])->getBody();
232                 }
233                 Logger::log('posted to insanejournal: ' . $x ? $x : '', Logger::DEBUG);
234         }
235 }
236
237 function ijpost_get_tags($post)
238 {
239         preg_match_all("/\]([^\[#]+)\[/", $post, $matches);
240         $tags = implode(', ', $matches[1]);
241         return $tags;
242 }