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