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