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