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