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