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>
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
17 use Friendica\Model\Tag;
18 use Friendica\Model\User;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Util\XML;
22 function ijpost_install()
24 Hook::register('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
25 Hook::register('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
26 Hook::register('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
27 Hook::register('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
28 Hook::register('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
31 function ijpost_jot_nets(array &$jotnets_fields)
33 if (!DI::userSession()->getLocalUserId()) {
37 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post')) {
42 DI::l10n()->t('Post to Insanejournal'),
43 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default')
49 function ijpost_settings(array &$data)
51 if (!DI::userSession()->getLocalUserId()) {
55 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post', false);
56 $ij_username = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'ij_username');
57 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default');
59 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ijpost/');
60 $html = Renderer::replaceMacros($t, [
61 '$enabled' => ['ijpost', DI::l10n()->t('Enable InsaneJournal Post Addon'), $enabled],
62 '$username' => ['ij_username', DI::l10n()->t('InsaneJournal username'), $ij_username],
63 '$password' => ['ij_password', DI::l10n()->t('InsaneJournal password')],
64 '$bydefault' => ['ij_bydefault', DI::l10n()->t('Post to InsaneJournal by default'), $def_enabled],
68 'connector' => 'ijpost',
69 'title' => DI::l10n()->t('InsaneJournal Export'),
70 'image' => 'images/insanejournal.gif',
71 'enabled' => $enabled,
76 function ijpost_settings_post(array &$b)
78 if (!empty($_POST['ijpost-submit'])) {
79 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ijpost', 'post', intval($_POST['ijpost']));
80 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default', intval($_POST['ij_bydefault']));
81 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ijpost', 'ij_username', trim($_POST['ij_username']));
82 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ijpost', 'ij_password', trim($_POST['ij_password']));
86 function ijpost_post_local(array &$b)
88 // This can probably be changed to allow editing by pointing to a different API endpoint
94 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
98 if ($b['private'] || $b['parent']) {
102 $ij_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post'));
104 $ij_enable = (($ij_post && !empty($_REQUEST['ijpost_enable'])) ? intval($_REQUEST['ijpost_enable']) : 0);
106 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default'))) {
114 if (strlen($b['postopts'])) {
115 $b['postopts'] .= ',';
118 $b['postopts'] .= 'ijpost';
121 function ijpost_send(array &$b)
123 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
127 if (!strstr($b['postopts'], 'ijpost')) {
131 if ($b['parent'] != $b['id']) {
135 // insanejournal post in the LJ user's timezone.
136 // Hopefully the person's Friendica account
137 // will be set to the same thing.
139 $user = User::getById($b['uid']);
140 $tz = $user['timezone'] ?: 'UTC';
142 $ij_username = DI::pConfig()->get($b['uid'], 'ijpost', 'ij_username');
143 $ij_password = DI::pConfig()->get($b['uid'], 'ijpost', 'ij_password');
144 $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
146 if ($ij_username && $ij_password && $ij_blog) {
147 $title = $b['title'];
148 $post = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
149 $post = XML::escape($post);
150 $tags = Tag::getCSVByURIId($b['uri-id'], [Tag::HASHTAG]);
152 $date = DateTimeFormat::convert($b['created'], $tz);
153 $year = intval(substr($date,0,4));
154 $mon = intval(substr($date,5,2));
155 $day = intval(substr($date,8,2));
156 $hour = intval(substr($date,11,2));
157 $min = intval(substr($date,14,2));
160 <?xml version="1.0" encoding="utf-8"?>
161 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
164 <member><name>year</name><value><int>$year</int></value></member>
165 <member><name>mon</name><value><int>$mon</int></value></member>
166 <member><name>day</name><value><int>$day</int></value></member>
167 <member><name>hour</name><value><int>$hour</int></value></member>
168 <member><name>min</name><value><int>$min</int></value></member>
169 <member><name>event</name><value><string>$post</string></value></member>
170 <member><name>username</name><value><string>$ij_username</string></value></member>
171 <member><name>password</name><value><string>$ij_password</string></value></member>
172 <member><name>subject</name><value><string>$title</string></value></member>
173 <member><name>lineendings</name><value><string>unix</string></value></member>
174 <member><name>ver</name><value><int>1</int></value></member>
175 <member><name>props</name>
177 <member><name>useragent</name><value><string>Friendica</string></value></member>
178 <member><name>taglist</name><value><string>$tags</string></value></member>
179 </struct></value></member>
186 Logger::debug('ijpost: data: ' . $xml);
188 if ($ij_blog !== 'test') {
189 $x = DI::httpClient()->post($ij_blog, $xml, ['Content-Type' => 'text/xml'])->getBody();
191 Logger::info('posted to insanejournal: ' . $x ? $x : '');