4 * Name: Insanejournal Post Connector
5 * Description: Post to Insanejournal
7 * Author: Tony Baldwin <https://free-haven.org/profile/tony>
8 * Author: Michael Johnston
9 * Author: Cat Gray <https://free-haven.org/profile/catness>
12 use Friendica\Core\PConfig;
14 function ijpost_install() {
15 register_hook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
16 register_hook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
17 register_hook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
18 register_hook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
19 register_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
22 function ijpost_uninstall() {
23 unregister_hook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
24 unregister_hook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
25 unregister_hook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
26 unregister_hook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
27 unregister_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
32 function ijpost_jot_nets(&$a,&$b) {
36 $ij_post = PConfig::get(local_user(),'ijpost','post');
37 if(intval($ij_post) == 1) {
38 $ij_defpost = PConfig::get(local_user(),'ijpost','post_by_default');
39 $selected = ((intval($ij_defpost) == 1) ? ' checked="checked" ' : '');
40 $b .= '<div class="profile-jot-net"><input type="checkbox" name="ijpost_enable" ' . $selected . ' value="1" /> '
41 . t('Post to Insanejournal') . '</div>';
46 function ijpost_settings(&$a,&$s) {
51 /* Add our stylesheet to the page so we can make our settings look nice */
53 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
55 /* Get the current state of our config variables */
57 $enabled = PConfig::get(local_user(),'ijpost','post');
59 $checked = (($enabled) ? ' checked="checked" ' : '');
61 $def_enabled = PConfig::get(local_user(),'ijpost','post_by_default');
63 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
65 $ij_username = PConfig::get(local_user(), 'ijpost', 'ij_username');
66 $ij_password = PConfig::get(local_user(), 'ijpost', 'ij_password');
69 /* Add some HTML to the existing form */
70 $s .= '<span id="settings_ijpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
71 $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
73 $s .= '<div id="settings_ijpost_expanded" class="settings-block" style="display: none;">';
74 $s .= '<span class="fakelink" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
75 $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
78 $s .= '<div id="ijpost-enable-wrapper">';
79 $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . t('Enable InsaneJournal Post Plugin') . '</label>';
80 $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
81 $s .= '</div><div class="clear"></div>';
83 $s .= '<div id="ijpost-username-wrapper">';
84 $s .= '<label id="ijpost-username-label" for="ijpost-username">' . t('InsaneJournal username') . '</label>';
85 $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
86 $s .= '</div><div class="clear"></div>';
88 $s .= '<div id="ijpost-password-wrapper">';
89 $s .= '<label id="ijpost-password-label" for="ijpost-password">' . t('InsaneJournal password') . '</label>';
90 $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
91 $s .= '</div><div class="clear"></div>';
93 $s .= '<div id="ijpost-bydefault-wrapper">';
94 $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . t('Post to InsaneJournal by default') . '</label>';
95 $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_bydefault" value="1" ' . $def_checked . '/>';
96 $s .= '</div><div class="clear"></div>';
98 /* provide a submit button */
100 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ijpost-submit" name="ijpost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
105 function ijpost_settings_post(&$a,&$b) {
107 if(x($_POST,'ijpost-submit')) {
109 PConfig::set(local_user(),'ijpost','post',intval($_POST['ijpost']));
110 PConfig::set(local_user(),'ijpost','post_by_default',intval($_POST['ij_bydefault']));
111 PConfig::set(local_user(),'ijpost','ij_username',trim($_POST['ij_username']));
112 PConfig::set(local_user(),'ijpost','ij_password',trim($_POST['ij_password']));
118 function ijpost_post_local(&$a,&$b) {
120 // This can probably be changed to allow editing by pointing to a different API endpoint
125 if((! local_user()) || (local_user() != $b['uid']))
128 if($b['private'] || $b['parent'])
131 $ij_post = intval(PConfig::get(local_user(),'ijpost','post'));
133 $ij_enable = (($ij_post && x($_REQUEST,'ijpost_enable')) ? intval($_REQUEST['ijpost_enable']) : 0);
135 if($_REQUEST['api_source'] && intval(PConfig::get(local_user(),'ijpost','post_by_default')))
141 if(strlen($b['postopts']))
142 $b['postopts'] .= ',';
143 $b['postopts'] .= 'ijpost';
149 function ijpost_send(&$a,&$b) {
151 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
154 if(! strstr($b['postopts'],'ijpost'))
157 if($b['parent'] != $b['id'])
160 // insanejournal post in the LJ user's timezone.
161 // Hopefully the person's Friendica account
162 // will be set to the same thing.
166 $x = q("select timezone from user where uid = %d limit 1",
169 if($x && strlen($x[0]['timezone']))
170 $tz = $x[0]['timezone'];
172 $ij_username = PConfig::get($b['uid'],'ijpost','ij_username');
173 $ij_password = PConfig::get($b['uid'],'ijpost','ij_password');
174 $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
176 if($ij_username && $ij_password && $ij_blog) {
178 require_once('include/bbcode.php');
179 require_once('include/datetime.php');
181 $title = $b['title'];
182 $post = bbcode($b['body']);
183 $post = xmlify($post);
184 $tags = ijpost_get_tags($b['tag']);
186 $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
187 $year = intval(substr($date,0,4));
188 $mon = intval(substr($date,5,2));
189 $day = intval(substr($date,8,2));
190 $hour = intval(substr($date,11,2));
191 $min = intval(substr($date,14,2));
194 <?xml version="1.0" encoding="utf-8"?>
195 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
198 <member><name>year</name><value><int>$year</int></value></member>
199 <member><name>mon</name><value><int>$mon</int></value></member>
200 <member><name>day</name><value><int>$day</int></value></member>
201 <member><name>hour</name><value><int>$hour</int></value></member>
202 <member><name>min</name><value><int>$min</int></value></member>
203 <member><name>event</name><value><string>$post</string></value></member>
204 <member><name>username</name><value><string>$ij_username</string></value></member>
205 <member><name>password</name><value><string>$ij_password</string></value></member>
206 <member><name>subject</name><value><string>$title</string></value></member>
207 <member><name>lineendings</name><value><string>unix</string></value></member>
208 <member><name>ver</name><value><int>1</int></value></member>
209 <member><name>props</name>
211 <member><name>useragent</name><value><string>Friendica</string></value></member>
212 <member><name>taglist</name><value><string>$tags</string></value></member>
213 </struct></value></member>
220 logger('ijpost: data: ' . $xml, LOGGER_DATA);
222 if($ij_blog !== 'test')
223 $x = post_url($ij_blog,$xml,array("Content-Type: text/xml"));
224 logger('posted to insanejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
229 function ijpost_get_tags($post)
231 preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
232 $tags = implode(', ',$matches[1]);