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