4 * Name: Dreamwidth Post Connector
5 * Description: Post to dreamwidth
7 * Author: Tony Baldwin <https://free-haven.org/profile/tony>
8 * Author: Michael Johnston
11 function dwpost_install() {
12 register_hook('post_local', 'addon/dwpost/dwpost.php', 'dwpost_post_local');
13 register_hook('notifier_normal', 'addon/dwpost/dwpost.php', 'dwpost_send');
14 register_hook('jot_networks', 'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
15 register_hook('connector_settings', 'addon/dwpost/dwpost.php', 'dwpost_settings');
16 register_hook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
19 function dwpost_uninstall() {
20 unregister_hook('post_local', 'addon/dwpost/dwpost.php', 'dwpost_post_local');
21 unregister_hook('notifier_normal', 'addon/dwpost/dwpost.php', 'dwpost_send');
22 unregister_hook('jot_networks', 'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
23 unregister_hook('connector_settings', 'addon/dwpost/dwpost.php', 'dwpost_settings');
24 unregister_hook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
29 function dwpost_jot_nets(&$a,&$b) {
33 $dw_post = get_pconfig(local_user(),'dwpost','post');
34 if(intval($dw_post) == 1) {
35 $dw_defpost = get_pconfig(local_user(),'dwpost','post_by_default');
36 $selected = ((intval($dw_defpost) == 1) ? ' checked="checked" ' : '');
37 $b .= '<div class="profile-jot-net"><input type="checkbox" name="dwpost_enable" ' . $selected . ' value="1" /> '
38 . t('Post to Dreamwidth') . '</div>';
43 function dwpost_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/dwpost/dwpost.css' . '" media="all" />' . "\r\n";
52 /* Get the current state of our config variables */
54 $enabled = get_pconfig(local_user(),'dwpost','post');
56 $checked = (($enabled) ? ' checked="checked" ' : '');
58 $def_enabled = get_pconfig(local_user(),'dwpost','post_by_default');
60 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
62 $dw_username = get_pconfig(local_user(), 'dwpost', 'dw_username');
63 $dw_password = get_pconfig(local_user(), 'dwpost', 'dw_password');
66 /* Add some HTML to the existing form */
68 $s .= '<div class="settings-block">';
69 $s .= '<h3>' . t('Dreamwidth Post Settings') . '</h3>';
70 $s .= '<div id="dwpost-enable-wrapper">';
71 $s .= '<label id="dwpost-enable-label" for="dwpost-checkbox">' . t('Enable dreamwidth Post Plugin') . '</label>';
72 $s .= '<input id="dwpost-checkbox" type="checkbox" name="dwpost" value="1" ' . $checked . '/>';
73 $s .= '</div><div class="clear"></div>';
75 $s .= '<div id="dwpost-username-wrapper">';
76 $s .= '<label id="dwpost-username-label" for="dwpost-username">' . t('dreamwidth username') . '</label>';
77 $s .= '<input id="dwpost-username" type="text" name="dw_username" value="' . $dw_username . '" />';
78 $s .= '</div><div class="clear"></div>';
80 $s .= '<div id="dwpost-password-wrapper">';
81 $s .= '<label id="dwpost-password-label" for="dwpost-password">' . t('dreamwidth password') . '</label>';
82 $s .= '<input id="dwpost-password" type="password" name="dw_password" value="' . $dw_password . '" />';
83 $s .= '</div><div class="clear"></div>';
85 $s .= '<div id="dwpost-bydefault-wrapper">';
86 $s .= '<label id="dwpost-bydefault-label" for="dwpost-bydefault">' . t('Post to dreamwidth by default') . '</label>';
87 $s .= '<input id="dwpost-bydefault" type="checkbox" name="dw_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="dwpost-submit" name="dwpost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
97 function dwpost_settings_post(&$a,&$b) {
99 if(x($_POST,'dwpost-submit')) {
101 set_pconfig(local_user(),'dwpost','post',intval($_POST['dwpost']));
102 set_pconfig(local_user(),'dwpost','post_by_default',intval($_POST['dw_bydefault']));
103 set_pconfig(local_user(),'dwpost','dw_username',trim($_POST['dw_username']));
104 set_pconfig(local_user(),'dwpost','dw_password',trim($_POST['dw_password']));
110 function dwpost_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 $dw_post = intval(get_pconfig(local_user(),'dwpost','post'));
125 $dw_enable = (($dw_post && x($_REQUEST,'dwpost_enable')) ? intval($_REQUEST['dwpost_enable']) : 0);
127 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'dwpost','post_by_default')))
133 if(strlen($b['postopts']))
134 $b['postopts'] .= ',';
135 $b['postopts'] .= 'dwpost';
141 function dwpost_send(&$a,&$b) {
143 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
146 if(! strstr($b['postopts'],'dwpost'))
149 if($b['parent'] != $b['id'])
152 // dreamwidth 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 $dw_username = get_pconfig($b['uid'],'dwpost','dw_username');
165 $dw_password = get_pconfig($b['uid'],'dwpost','dw_password');
166 $dw_blog = 'http://www.dreamwidth.org/interface/xmlrpc';
168 if($dw_username && $dw_password && $dw_blog) {
170 require_once('include/bbcode.php');
171 require_once('include/datetime.php');
173 $title = $b['title'];
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"?>
186 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
189 <member><name>year</name><value><int>$year</int></value></member>
190 <member><name>mon</name><value><int>$mon</int></value></member>
191 <member><name>day</name><value><int>$day</int></value></member>
192 <member><name>hour</name><value><int>$hour</int></value></member>
193 <member><name>min</name><value><int>$min</int></value></member>
194 <member><name>event</name><value><string>$post</string></value></member>
195 <member><name>username</name><value><string>$dw_username</string></value></member>
196 <member><name>password</name><value><string>$dw_password</string></value></member>
197 <member><name>subject</name><value><string>$title</string></value></member>
198 <member><name>lineendings</name><value><string>unix</string></value></member>
199 <member><name>ver</name><value><int>1</int></value></member>
200 <member><name>props</name>
202 <member><name>useragent</name><value><string>Friendica</string></value></member>
203 <member><name>taglist</name><value><string>friendica</string></value></member>
204 </struct></value></member>
211 logger('dwpost: data: ' . $xml, LOGGER_DATA);
213 if($dw_blog !== 'test')
214 $x = post_url($dw_blog,$xml);
215 logger('posted to dreamwidth: ' . ($x) ? $x : '', LOGGER_DEBUG);