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\Core\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Util\Network;
15 use Friendica\Util\Temporal;
17 function ijpost_install() {
18 Addon::registerHook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
19 Addon::registerHook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
20 Addon::registerHook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
21 Addon::registerHook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
22 Addon::registerHook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
25 function ijpost_uninstall() {
26 Addon::unregisterHook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
27 Addon::unregisterHook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
28 Addon::unregisterHook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
29 Addon::unregisterHook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
30 Addon::unregisterHook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
35 function ijpost_jot_nets(&$a,&$b) {
39 $ij_post = PConfig::get(local_user(),'ijpost','post');
40 if(intval($ij_post) == 1) {
41 $ij_defpost = PConfig::get(local_user(),'ijpost','post_by_default');
42 $selected = ((intval($ij_defpost) == 1) ? ' checked="checked" ' : '');
43 $b .= '<div class="profile-jot-net"><input type="checkbox" name="ijpost_enable" ' . $selected . ' value="1" /> '
44 . L10n::t('Post to Insanejournal') . '</div>';
49 function ijpost_settings(&$a,&$s) {
54 /* Add our stylesheet to the page so we can make our settings look nice */
56 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
58 /* Get the current state of our config variables */
60 $enabled = PConfig::get(local_user(),'ijpost','post');
62 $checked = (($enabled) ? ' checked="checked" ' : '');
64 $def_enabled = PConfig::get(local_user(),'ijpost','post_by_default');
66 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
68 $ij_username = PConfig::get(local_user(), 'ijpost', 'ij_username');
69 $ij_password = PConfig::get(local_user(), 'ijpost', 'ij_password');
72 /* Add some HTML to the existing form */
73 $s .= '<span id="settings_ijpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
74 $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. L10n::t("InsaneJournal Export").'</h3>';
76 $s .= '<div id="settings_ijpost_expanded" class="settings-block" style="display: none;">';
77 $s .= '<span class="fakelink" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
78 $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. L10n::t("InsaneJournal Export").'</h3>';
81 $s .= '<div id="ijpost-enable-wrapper">';
82 $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . L10n::t('Enable InsaneJournal Post Addon') . '</label>';
83 $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
84 $s .= '</div><div class="clear"></div>';
86 $s .= '<div id="ijpost-username-wrapper">';
87 $s .= '<label id="ijpost-username-label" for="ijpost-username">' . L10n::t('InsaneJournal username') . '</label>';
88 $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
89 $s .= '</div><div class="clear"></div>';
91 $s .= '<div id="ijpost-password-wrapper">';
92 $s .= '<label id="ijpost-password-label" for="ijpost-password">' . L10n::t('InsaneJournal password') . '</label>';
93 $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
94 $s .= '</div><div class="clear"></div>';
96 $s .= '<div id="ijpost-bydefault-wrapper">';
97 $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . L10n::t('Post to InsaneJournal by default') . '</label>';
98 $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_bydefault" value="1" ' . $def_checked . '/>';
99 $s .= '</div><div class="clear"></div>';
101 /* provide a submit button */
103 $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>';
108 function ijpost_settings_post(&$a,&$b) {
110 if(x($_POST,'ijpost-submit')) {
112 PConfig::set(local_user(),'ijpost','post',intval($_POST['ijpost']));
113 PConfig::set(local_user(),'ijpost','post_by_default',intval($_POST['ij_bydefault']));
114 PConfig::set(local_user(),'ijpost','ij_username',trim($_POST['ij_username']));
115 PConfig::set(local_user(),'ijpost','ij_password',trim($_POST['ij_password']));
121 function ijpost_post_local(&$a,&$b) {
123 // This can probably be changed to allow editing by pointing to a different API endpoint
128 if((! local_user()) || (local_user() != $b['uid']))
131 if($b['private'] || $b['parent'])
134 $ij_post = intval(PConfig::get(local_user(),'ijpost','post'));
136 $ij_enable = (($ij_post && x($_REQUEST,'ijpost_enable')) ? intval($_REQUEST['ijpost_enable']) : 0);
138 if($_REQUEST['api_source'] && intval(PConfig::get(local_user(),'ijpost','post_by_default')))
144 if(strlen($b['postopts']))
145 $b['postopts'] .= ',';
146 $b['postopts'] .= 'ijpost';
152 function ijpost_send(&$a,&$b) {
154 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
157 if(! strstr($b['postopts'],'ijpost'))
160 if($b['parent'] != $b['id'])
163 // insanejournal post in the LJ user's timezone.
164 // Hopefully the person's Friendica account
165 // will be set to the same thing.
169 $x = q("select timezone from user where uid = %d limit 1",
172 if($x && strlen($x[0]['timezone']))
173 $tz = $x[0]['timezone'];
175 $ij_username = PConfig::get($b['uid'],'ijpost','ij_username');
176 $ij_password = PConfig::get($b['uid'],'ijpost','ij_password');
177 $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
179 if($ij_username && $ij_password && $ij_blog) {
181 require_once('include/bbcode.php');
182 require_once('include/datetime.php');
184 $title = $b['title'];
185 $post = bbcode($b['body']);
186 $post = xmlify($post);
187 $tags = ijpost_get_tags($b['tag']);
189 $date = Temporal::convert($b['created'], $tz);
190 $year = intval(substr($date,0,4));
191 $mon = intval(substr($date,5,2));
192 $day = intval(substr($date,8,2));
193 $hour = intval(substr($date,11,2));
194 $min = intval(substr($date,14,2));
197 <?xml version="1.0" encoding="utf-8"?>
198 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
201 <member><name>year</name><value><int>$year</int></value></member>
202 <member><name>mon</name><value><int>$mon</int></value></member>
203 <member><name>day</name><value><int>$day</int></value></member>
204 <member><name>hour</name><value><int>$hour</int></value></member>
205 <member><name>min</name><value><int>$min</int></value></member>
206 <member><name>event</name><value><string>$post</string></value></member>
207 <member><name>username</name><value><string>$ij_username</string></value></member>
208 <member><name>password</name><value><string>$ij_password</string></value></member>
209 <member><name>subject</name><value><string>$title</string></value></member>
210 <member><name>lineendings</name><value><string>unix</string></value></member>
211 <member><name>ver</name><value><int>1</int></value></member>
212 <member><name>props</name>
214 <member><name>useragent</name><value><string>Friendica</string></value></member>
215 <member><name>taglist</name><value><string>$tags</string></value></member>
216 </struct></value></member>
223 logger('ijpost: data: ' . $xml, LOGGER_DATA);
225 if($ij_blog !== 'test') {
226 $x = Network::post($ij_blog, $xml, ["Content-Type: text/xml"]);
228 logger('posted to insanejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
233 function ijpost_get_tags($post)
235 preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
236 $tags = implode(', ',$matches[1]);