c8690c26e18a1a1bfbdc352d7824e778d31278b2
[friendica-addons.git] / ljpost / ljpost.php
1 <?php
2
3 /**
4  * Name: LiveJournal Post Connector
5  * Description: Post to LiveJournal
6  * Version: 1.0
7  * Author: Tony Baldwin <https://free-haven.org/profile/tony>
8  * Author: Michael Johnston
9  * Author: Cat Gray <https://free-haven.org/profile/catness>
10  */
11
12 function ljpost_install() {
13     register_hook('post_local',           'addon/ljpost/ljpost.php', 'ljpost_post_local');
14     register_hook('notifier_normal',      'addon/ljpost/ljpost.php', 'ljpost_send');
15     register_hook('jot_networks',         'addon/ljpost/ljpost.php', 'ljpost_jot_nets');
16     register_hook('connector_settings',      'addon/ljpost/ljpost.php', 'ljpost_settings');
17     register_hook('connector_settings_post', 'addon/ljpost/ljpost.php', 'ljpost_settings_post');
18
19 }
20 function ljpost_uninstall() {
21     unregister_hook('post_local',       'addon/ljpost/ljpost.php', 'ljpost_post_local');
22     unregister_hook('notifier_normal',  'addon/ljpost/ljpost.php', 'ljpost_send');
23     unregister_hook('jot_networks',     'addon/ljpost/ljpost.php', 'ljpost_jot_nets');
24     unregister_hook('connector_settings',      'addon/ljpost/ljpost.php', 'ljpost_settings');
25     unregister_hook('connector_settings_post', 'addon/ljpost/ljpost.php', 'ljpost_settings_post');
26
27 }
28
29
30 function ljpost_jot_nets(&$a,&$b) {
31     if(! local_user())
32         return;
33
34     $lj_post = get_pconfig(local_user(),'ljpost','post');
35     if(intval($lj_post) == 1) {
36         $lj_defpost = get_pconfig(local_user(),'ljpost','post_by_default');
37         $selected = ((intval($lj_defpost) == 1) ? ' checked="checked" ' : '');
38         $b .= '<div class="profile-jot-net"><input type="checkbox" name="ljpost_enable" ' . $selected . ' value="1" /> '
39             . t('Post to LiveJournal') . '</div>';
40     }
41 }
42
43
44 function ljpost_settings(&$a,&$s) {
45
46     if(! local_user())
47         return;
48
49     /* Add our stylesheet to the page so we can make our settings look nice */
50
51     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/ljpost/ljpost.css' . '" media="all" />' . "\r\n";
52
53     /* Get the current state of our config variables */
54
55     $enabled = get_pconfig(local_user(),'ljpost','post');
56
57     $checked = (($enabled) ? ' checked="checked" ' : '');
58
59     $def_enabled = get_pconfig(local_user(),'ljpost','post_by_default');
60
61     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
62
63         $lj_username = get_pconfig(local_user(), 'ljpost', 'lj_username');
64         $lj_password = get_pconfig(local_user(), 'ljpost', 'lj_password');
65
66
67     /* Add some HTML to the existing form */
68
69     $s .= '<div class="settings-block">';
70     $s .= '<h3>' . t('LiveJournal Post Settings') . '</h3>';
71     $s .= '<div id="ljpost-enable-wrapper">';
72     $s .= '<label id="ljpost-enable-label" for="ljpost-checkbox">' . t('Enable LiveJournal Post Plugin') . '</label>';
73     $s .= '<input id="ljpost-checkbox" type="checkbox" name="ljpost" value="1" ' . $checked . '/>';
74     $s .= '</div><div class="clear"></div>';
75
76     $s .= '<div id="ljpost-username-wrapper">';
77     $s .= '<label id="ljpost-username-label" for="ljpost-username">' . t('LiveJournal username') . '</label>';
78     $s .= '<input id="ljpost-username" type="text" name="lj_username" value="' . $lj_username . '" />';
79     $s .= '</div><div class="clear"></div>';
80
81     $s .= '<div id="ljpost-password-wrapper">';
82     $s .= '<label id="ljpost-password-label" for="ljpost-password">' . t('LiveJournal password') . '</label>';
83     $s .= '<input id="ljpost-password" type="password" name="lj_password" value="' . $lj_password . '" />';
84     $s .= '</div><div class="clear"></div>';
85
86     $s .= '<div id="ljpost-bydefault-wrapper">';
87     $s .= '<label id="ljpost-bydefault-label" for="ljpost-bydefault">' . t('Post to LiveJournal by default') . '</label>';
88     $s .= '<input id="ljpost-bydefault" type="checkbox" name="lj_bydefault" value="1" ' . $def_checked . '/>';
89     $s .= '</div><div class="clear"></div>';
90
91     /* provide a submit button */
92
93     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ljpost-submit" name="ljpost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
94
95 }
96
97
98 function ljpost_settings_post(&$a,&$b) {
99
100         if(x($_POST,'ljpost-submit')) {
101
102                 set_pconfig(local_user(),'ljpost','post',intval($_POST['ljpost']));
103                 set_pconfig(local_user(),'ljpost','post_by_default',intval($_POST['lj_bydefault']));
104                 set_pconfig(local_user(),'ljpost','lj_username',trim($_POST['lj_username']));
105                 set_pconfig(local_user(),'ljpost','lj_password',trim($_POST['lj_password']));
106
107         }
108
109 }
110
111 function ljpost_post_local(&$a,&$b) {
112
113         // This can probably be changed to allow editing by pointing to a different API endpoint
114
115         if($b['edit'])
116                 return;
117
118         if((! local_user()) || (local_user() != $b['uid']))
119                 return;
120
121         if($b['private'] || $b['parent'])
122                 return;
123
124     $lj_post   = intval(get_pconfig(local_user(),'ljpost','post'));
125
126         $lj_enable = (($lj_post && x($_REQUEST,'ljpost_enable')) ? intval($_REQUEST['ljpost_enable']) : 0);
127
128         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ljpost','post_by_default')))
129                 $lj_enable = 1;
130
131     if(! $lj_enable)
132        return;
133
134     if(strlen($b['postopts']))
135        $b['postopts'] .= ',';
136      $b['postopts'] .= 'ljpost';
137 }
138
139
140
141
142 function ljpost_send(&$a,&$b) {
143
144     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
145         return;
146
147     if(! strstr($b['postopts'],'ljpost'))
148         return;
149
150     if($b['parent'] != $b['id'])
151         return;
152
153         // LiveJournal post in the LJ user's timezone. 
154         // Hopefully the person's Friendica account
155         // will be set to the same thing.
156
157         $tz = 'UTC';
158
159         $x = q("select timezone from user where uid = %d limit 1",
160                 intval($b['uid'])
161         );
162         if($x && strlen($x[0]['timezone']))
163                 $tz = $x[0]['timezone'];        
164
165         $lj_username = xmlify(get_pconfig($b['uid'],'ljpost','lj_username'));
166         $lj_password = xmlify(get_pconfig($b['uid'],'ljpost','lj_password'));
167         $lj_journal = xmlify(get_pconfig($b['uid'],'ljpost','lj_journal'));
168 //      if(! $lj_journal)
169 //              $lj_journal = $lj_username;
170
171         $lj_blog = xmlify(get_pconfig($b['uid'],'ljpost','lj_blog'));
172         if(! strlen($lj_blog))
173                 $lj_blog = xmlify('http://www.livejournal.com/interface/xmlrpc');
174
175         if($lj_username && $lj_password && $lj_blog) {
176
177                 require_once('include/bbcode.php');
178                 require_once('include/datetime.php');
179
180                 $title = xmlify($b['title']);
181                 $post = bbcode($b['body']);
182                 $post = xmlify($post);
183                 $tags = ljpost_get_tags($b['tag']);
184
185                 $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
186                 $year = intval(substr($date,0,4));
187                 $mon  = intval(substr($date,5,2));
188                 $day  = intval(substr($date,8,2));
189                 $hour = intval(substr($date,11,2));
190                 $min  = intval(substr($date,14,2));
191
192                 $xml = <<< EOT
193 <?xml version="1.0" encoding="utf-8"?>
194 <methodCall>
195   <methodName>LJ.XMLRPC.postevent</methodName>
196   <params>
197     <param><value>
198         <struct>
199         <member><name>username</name><value><string>$lj_username</string></value></member>
200         <member><name>password</name><value><string>$lj_password</string></value></member>
201         <member><name>event</name><value><string>$post</string></value></member>
202         <member><name>subject</name><value><string>$title</string></value></member>
203         <member><name>lineendings</name><value><string>unix</string></value></member>
204         <member><name>year</name><value><int>$year</int></value></member>
205         <member><name>mon</name><value><int>$mon</int></value></member>
206         <member><name>day</name><value><int>$day</int></value></member>
207         <member><name>hour</name><value><int>$hour</int></value></member>
208         <member><name>min</name><value><int>$min</int></value></member>
209                 <member><name>usejournal</name><value><string>$lj_username</string></value></member>
210                 <member>
211                         <name>props</name>
212                         <value>
213                                 <struct>
214                                         <member>
215                                                 <name>useragent</name>
216                                                 <value><string>Friendica</string></value>
217                                         </member>
218                                         <member>
219                                                 <name>taglist</name>
220                                                 <value><string>$tags</string></value>
221                                         </member>
222                                 </struct>
223                         </value>
224                 </member>
225         </struct>
226     </value></param>
227   </params>
228 </methodCall>
229
230 EOT;
231
232                 logger('ljpost: data: ' . $xml, LOGGER_DATA);
233
234                 if($lj_blog !== 'test')
235                         $x = post_url($lj_blog,$xml,array("Content-Type: text/xml"));
236                 logger('posted to livejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
237
238         }
239 }
240
241 function ljpost_get_tags($post)
242 {
243         preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
244         $tags = implode(', ',$matches[1]);
245         return $tags;
246 }