3 * Name: Insanejournal Post Connector
4 * Description: Post to Insanejournal
6 * Author: Tony Baldwin <https://free-haven.org/profile/tony>
7 * Author: Michael Johnston
8 * Author: Cat Gray <https://free-haven.org/profile/catness>
11 use Friendica\Content\Text\BBCode;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
15 use Friendica\Model\Tag;
16 use Friendica\Model\User;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\XML;
20 function ijpost_install()
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');
29 function ijpost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
35 if (DI::pConfig()->get(local_user(), 'ijpost', 'post')) {
40 DI::l10n()->t('Post to Insanejournal'),
41 DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default')
47 function ijpost_settings(&$a, &$s)
53 /* Add our stylesheet to the page so we can make our settings look nice */
55 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
57 /* Get the current state of our config variables */
59 $enabled = DI::pConfig()->get(local_user(), 'ijpost', 'post');
61 $checked = (($enabled) ? ' checked="checked" ' : '');
63 $def_enabled = DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default');
65 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
67 $ij_username = DI::pConfig()->get(local_user(), 'ijpost', 'ij_username');
68 $ij_password = DI::pConfig()->get(local_user(), 'ijpost', 'ij_password');
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>';
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>';
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>';
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>';
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>';
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>';
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>';
103 function ijpost_settings_post(&$a, &$b)
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']));
113 function ijpost_post_local(&$a, &$b)
115 // This can probably be changed to allow editing by pointing to a different API endpoint
121 if (!local_user() || (local_user() != $b['uid'])) {
125 if ($b['private'] || $b['parent']) {
129 $ij_post = intval(DI::pConfig()->get(local_user(), 'ijpost', 'post'));
131 $ij_enable = (($ij_post && !empty($_REQUEST['ijpost_enable'])) ? intval($_REQUEST['ijpost_enable']) : 0);
133 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'ijpost', 'post_by_default'))) {
141 if (strlen($b['postopts'])) {
142 $b['postopts'] .= ',';
145 $b['postopts'] .= 'ijpost';
148 function ijpost_send(&$a, &$b)
150 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
154 if (!strstr($b['postopts'], 'ijpost')) {
158 if ($b['parent'] != $b['id']) {
162 // insanejournal post in the LJ user's timezone.
163 // Hopefully the person's Friendica account
164 // will be set to the same thing.
166 $user = User::getById($b['uid']);
167 $tz = $user['timezone'] ?: 'UTC';
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';
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]);
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));
187 <?xml version="1.0" encoding="utf-8"?>
188 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
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>
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>
213 Logger::debug('ijpost: data: ' . $xml);
215 if ($ij_blog !== 'test') {
216 $x = DI::httpClient()->post($ij_blog, $xml, ['Content-Type' => 'text/xml'])->getBody();
218 Logger::info('posted to insanejournal: ' . $x ? $x : '');