4 * Name: LiveJournal Post Connector
5 * Description: Post to LiveJournal
7 * Author: Tony Baldwin <http://theshi.re/profile/tony>
8 * Author: Michael Johnston
11 function ljpost_install() {
12 register_hook('post_local', 'addon/ljpost/ljpost.php', 'ljpost_post_local');
13 register_hook('notifier_normal', 'addon/ljpost/ljpost.php', 'ljpost_send');
14 register_hook('jot_networks', 'addon/ljpost/ljpost.php', 'ljpost_jot_nets');
15 register_hook('connector_settings', 'addon/ljpost/ljpost.php', 'ljpost_settings');
16 register_hook('connector_settings_post', 'addon/ljpost/ljpost.php', 'ljpost_settings_post');
19 function ljpost_uninstall() {
20 unregister_hook('post_local', 'addon/ljpost/ljpost.php', 'ljpost_post_local');
21 unregister_hook('notifier_normal', 'addon/ljpost/ljpost.php', 'ljpost_send');
22 unregister_hook('jot_networks', 'addon/ljpost/ljpost.php', 'ljpost_jot_nets');
23 unregister_hook('connector_settings', 'addon/ljpost/ljpost.php', 'ljpost_settings');
24 unregister_hook('connector_settings_post', 'addon/ljpost/ljpost.php', 'ljpost_settings_post');
29 function ljpost_jot_nets(&$a,&$b) {
33 $lj_post = get_pconfig(local_user(),'ljpost','post');
34 if(intval($lj_post) == 1) {
35 $lj_defpost = get_pconfig(local_user(),'ljpost','post_by_default');
36 $selected = ((intval($lj_defpost) == 1) ? ' checked="checked" ' : '');
37 $b .= '<div class="profile-jot-net"><input type="checkbox" name="ljpost_enable" ' . $selected . ' value="1" /> '
38 . t('Post to LiveJournal') . '</div>';
43 function ljpost_settings(&$a,&$s) {
48 /* Add our stylesheet to the page so we can make our settings look nice */
50 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/ljpost/ljpost.css' . '" media="all" />' . "\r\n";
52 /* Get the current state of our config variables */
54 $enabled = get_pconfig(local_user(),'ljpost','post');
56 $checked = (($enabled) ? ' checked="checked" ' : '');
58 $def_enabled = get_pconfig(local_user(),'ljpost','post_by_default');
60 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
62 $lj_username = get_pconfig(local_user(), 'ljpost', 'lj_username');
63 $lj_password = get_pconfig(local_user(), 'ljpost', 'lj_password');
66 /* Add some HTML to the existing form */
68 $s .= '<div class="settings-block">';
69 $s .= '<h3>' . t('LiveJournal Post Settings') . '</h3>';
70 $s .= '<div id="ljpost-enable-wrapper">';
71 $s .= '<label id="ljpost-enable-label" for="ljpost-checkbox">' . t('Enable LiveJournal Post Plugin') . '</label>';
72 $s .= '<input id="ljpost-checkbox" type="checkbox" name="ljpost" value="1" ' . $checked . '/>';
73 $s .= '</div><div class="clear"></div>';
75 $s .= '<div id="ljpost-username-wrapper">';
76 $s .= '<label id="ljpost-username-label" for="ljpost-username">' . t('LiveJournal username') . '</label>';
77 $s .= '<input id="ljpost-username" type="text" name="lj_username" value="' . $lj_username . '" />';
78 $s .= '</div><div class="clear"></div>';
80 $s .= '<div id="ljpost-password-wrapper">';
81 $s .= '<label id="ljpost-password-label" for="ljpost-password">' . t('LiveJournal password') . '</label>';
82 $s .= '<input id="ljpost-password" type="password" name="lj_password" value="' . $lj_password . '" />';
83 $s .= '</div><div class="clear"></div>';
85 $s .= '<div id="ljpost-bydefault-wrapper">';
86 $s .= '<label id="ljpost-bydefault-label" for="ljpost-bydefault">' . t('Post to LiveJournal by default') . '</label>';
87 $s .= '<input id="ljpost-bydefault" type="checkbox" name="lj_bydefault" value="1" ' . $def_checked . '/>';
88 $s .= '</div><div class="clear"></div>';
90 /* provide a submit button */
92 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ljpost-submit" name="ljpost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
97 function ljpost_settings_post(&$a,&$b) {
99 if(x($_POST,'ljpost-submit')) {
101 set_pconfig(local_user(),'ljpost','post',intval($_POST['ljpost']));
102 set_pconfig(local_user(),'ljpost','post_by_default',intval($_POST['lj_bydefault']));
103 set_pconfig(local_user(),'ljpost','lj_username',trim($_POST['lj_username']));
104 set_pconfig(local_user(),'ljpost','lj_password',trim($_POST['lj_password']));
110 function ljpost_post_local(&$a,&$b) {
112 // This can probably be changed to allow editing by pointing to a different API endpoint
117 if((! local_user()) || (local_user() != $b['uid']))
120 if($b['private'] || $b['parent'])
123 $lj_post = intval(get_pconfig(local_user(),'ljpost','post'));
125 $lj_enable = (($lj_post && x($_REQUEST,'ljpost_enable')) ? intval($_REQUEST['ljpost_enable']) : 0);
127 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ljpost','post_by_default')))
133 if(strlen($b['postopts']))
134 $b['postopts'] .= ',';
135 $b['postopts'] .= 'ljpost';
141 function ljpost_send(&$a,&$b) {
143 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
146 if(! strstr($b['postopts'],'ljpost'))
149 if($b['parent'] != $b['id'])
152 // LiveJournal post in the LJ user's timezone.
153 // Hopefully the person's Friendica account
154 // will be set to the same thing.
158 $x = q("select timezone from user where uid = %d limit 1",
161 if($x && strlen($x[0]['timezone']))
162 $tz = $x[0]['timezone'];
164 $lj_username = get_pconfig($b['uid'],'ljpost','lj_username');
165 $lj_password = get_pconfig($b['uid'],'ljpost','lj_password');
166 $lj_blog = 'http://www.livejournal.com/interface/xmlrpc';
168 if($lj_username && $lj_password && $lj_blog) {
170 require_once('include/bbcode.php');
171 require_once('include/datetime.php');
173 $title = (($b['title']) ? $b['title'] : t('[no subject]'));
174 $post = bbcode($b['body']);
175 $post = xmlify($post);
177 $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
178 $year = intval(substr($date,0,4));
179 $mon = intval(substr($date,5,2));
180 $day = intval(substr($date,8,2));
181 $hour = intval(substr($date,11,2));
182 $min = intval(substr($date,14,2));
185 <?xml version="1.0" encoding="utf-8"?>
187 <methodName>LJ.XMLRPC.postevent</methodName>
191 <member><name>username</name><value><string>$lj_username</string></value></member>
192 <member><name>password</name><value><string>$lj_password</string></value></member>
193 <member><name>event</name><value><string>$post</string></value></member>
194 <member><name>subject</name><value><string>$title</string></value></member>
195 <member><name>lineendings</name><value><string>unix</string></value></member>
196 <member><name>year</name><value><int>$year</int></value></member>
197 <member><name>mon</name><value><int>$mon</int></value></member>
198 <member><name>day</name><value><int>$day</int></value></member>
199 <member><name>hour</name><value><int>$hour</int></value></member>
200 <member><name>min</name><value><int>$min</int></value></member>
208 logger('ljpost: data: ' . $xml, LOGGER_DATA);
210 if($lj_blog !== 'test')
211 $x = post_url($lj_blog,$xml);
212 logger('posted to livejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);