]> git.mxchange.org Git - friendica-addons.git/blob - ijpost/ijpost.php
Merge pull request 'Initial commit of url_replace addon.' (#1453) from toddy/friendic...
[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\App;
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\DI;
17 use Friendica\Model\Tag;
18 use Friendica\Model\User;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Util\XML;
21
22 function ijpost_install()
23 {
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');
29 }
30
31 function ijpost_jot_nets(array &$jotnets_fields)
32 {
33         if (!DI::userSession()->getLocalUserId()) {
34                 return;
35         }
36
37         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post')) {
38                 $jotnets_fields[] = [
39                         'type' => 'checkbox',
40                         'field' => [
41                                 'ijpost_enable',
42                                 DI::l10n()->t('Post to Insanejournal'),
43                                 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default')
44                         ]
45                 ];
46         }
47 }
48
49 function ijpost_settings(array &$data)
50 {
51         if (!DI::userSession()->getLocalUserId()) {
52                 return;
53         }
54
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');
58
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],
65         ]);
66
67         $data = [
68                 'connector' => 'ijpost',
69                 'title'     => DI::l10n()->t('InsaneJournal Export'),
70                 'image'     => 'images/insanejournal.gif',
71                 'enabled'   => $enabled,
72                 'html'      => $html,
73         ];
74 }
75
76 function ijpost_settings_post(array &$b)
77 {
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']));
83         }
84 }
85
86 function ijpost_post_local(array &$b)
87 {
88         // This can probably be changed to allow editing by pointing to a different API endpoint
89
90         if ($b['edit']) {
91                 return;
92         }
93
94         if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
95                 return;
96         }
97
98         if ($b['private'] || $b['parent']) {
99                 return;
100         }
101
102         $ij_post   = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post'));
103
104         $ij_enable = (($ij_post && !empty($_REQUEST['ijpost_enable'])) ? intval($_REQUEST['ijpost_enable']) : 0);
105
106         if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ijpost', 'post_by_default'))) {
107                 $ij_enable = 1;
108         }
109
110         if (!$ij_enable) {
111                 return;
112         }
113
114         if (strlen($b['postopts'])) {
115                 $b['postopts'] .= ',';
116         }
117
118         $b['postopts'] .= 'ijpost';
119 }
120
121 function ijpost_send(array &$b)
122 {
123         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
124                 return;
125         }
126
127         if (!strstr($b['postopts'], 'ijpost')) {
128                 return;
129         }
130
131         if ($b['parent'] != $b['id']) {
132                 return;
133         }
134
135         // insanejournal post in the LJ user's timezone.
136         // Hopefully the person's Friendica account
137         // will be set to the same thing.
138
139         $user = User::getById($b['uid']);
140         $tz = $user['timezone'] ?: 'UTC';
141
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';
145
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]);
151
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));
158
159                 $xml = <<< EOT
160 <?xml version="1.0" encoding="utf-8"?>
161 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
162 <params><param>
163 <value><struct>
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>
176 <value><struct>
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>
180 </struct></value>
181 </param></params>
182 </methodCall>
183
184 EOT;
185
186                 Logger::debug('ijpost: data: ' . $xml);
187
188                 if ($ij_blog !== 'test') {
189                         $x = DI::httpClient()->post($ij_blog, $xml, ['Content-Type' => 'text/xml'])->getBody();
190                 }
191                 Logger::info('posted to insanejournal: ' . $x ? $x : '');
192         }
193 }