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