]> git.mxchange.org Git - friendica-addons.git/blob - ljpost/ljpost.php
16863f6757963ddf59b16f4092f51db172e3ac99
[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 <http://tonybaldwin.me/friendica/profile/tony>
8  * Author: Michael Johnston
9  */
10
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');
17
18 }
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');
25
26 }
27
28
29 function ljpost_jot_nets(&$a,&$b) {
30     if(! local_user())
31         return;
32
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>';
39     }
40 }
41
42
43 function ljpost_settings(&$a,&$s) {
44
45     if(! local_user())
46         return;
47
48     /* Add our stylesheet to the page so we can make our settings look nice */
49
50     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/ljpost/ljpost.css' . '" media="all" />' . "\r\n";
51
52     /* Get the current state of our config variables */
53
54     $enabled = get_pconfig(local_user(),'ljpost','post');
55
56     $checked = (($enabled) ? ' checked="checked" ' : '');
57
58     $def_enabled = get_pconfig(local_user(),'ljpost','post_by_default');
59
60     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
61
62         $lj_username = get_pconfig(local_user(), 'ljpost', 'lj_username');
63         $lj_password = get_pconfig(local_user(), 'ljpost', 'lj_password');
64
65
66     /* Add some HTML to the existing form */
67
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>';
74
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>';
79
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>';
84
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>';
89
90     /* provide a submit button */
91
92     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ljpost-submit" name="ljpost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
93
94 }
95
96
97 function ljpost_settings_post(&$a,&$b) {
98
99         if(x($_POST,'ljpost-submit')) {
100
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']));
105
106         }
107
108 }
109
110 function ljpost_post_local(&$a,&$b) {
111
112         // This can probably be changed to allow editing by pointing to a different API endpoint
113
114         if($b['edit'])
115                 return;
116
117         if((! local_user()) || (local_user() != $b['uid']))
118                 return;
119
120         if($b['private'] || $b['parent'])
121                 return;
122
123     $lj_post   = intval(get_pconfig(local_user(),'ljpost','post'));
124
125         $lj_enable = (($lj_post && x($_REQUEST,'ljpost_enable')) ? intval($_REQUEST['ljpost_enable']) : 0);
126
127         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ljpost','post_by_default')))
128                 $lj_enable = 1;
129
130     if(! $lj_enable)
131        return;
132
133     if(strlen($b['postopts']))
134        $b['postopts'] .= ',';
135      $b['postopts'] .= 'ljpost';
136 }
137
138
139
140
141 function ljpost_send(&$a,&$b) {
142
143     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
144         return;
145
146     if(! strstr($b['postopts'],'ljpost'))
147         return;
148
149     if($b['parent'] != $b['id'])
150         return;
151
152         // LiveJournal post in the LJ user's timezone. 
153         // Hopefully the person's Friendica account
154         // will be set to the same thing.
155
156         $tz = 'UTC';
157
158         $x = q("select timezone from user where uid = %d limit 1",
159                 intval($b['uid'])
160         );
161         if($x && strlen($x[0]['timezone']))
162                 $tz = $x[0]['timezone'];        
163
164         $lj_username = xmlify(get_pconfig($b['uid'],'ljpost','lj_username'));
165         $lj_password = xmlify(get_pconfig($b['uid'],'ljpost','lj_password'));
166         $lj_journal = xmlify(get_pconfig($b['uid'],'ljpost','lj_journal'));
167 //      if(! $lj_journal)
168 //              $lj_journal = $lj_username;
169
170         $lj_blog = xmlify(get_pconfig($b['uid'],'ljpost','lj_blog'));
171         if(! strlen($lj_blog))
172                 $lj_blog = xmlify('http://www.livejournal.com/interface/xmlrpc');
173
174         if($lj_username && $lj_password && $lj_blog) {
175
176                 require_once('include/bbcode.php');
177                 require_once('include/datetime.php');
178
179                 $title = xmlify($b['title']);
180                 $post = bbcode($b['body']);
181                 $post = xmlify($post);
182
183                 $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
184                 $year = intval(substr($date,0,4));
185                 $mon  = intval(substr($date,5,2));
186                 $day  = intval(substr($date,8,2));
187                 $hour = intval(substr($date,11,2));
188                 $min  = intval(substr($date,14,2));
189
190                 $xml = <<< EOT
191 <?xml version="1.0" encoding="utf-8"?>
192 <methodCall>
193   <methodName>LJ.XMLRPC.postevent</methodName>
194   <params>
195     <param><value>
196         <struct>
197         <member><name>username</name><value><string>$lj_username</string></value></member>
198         <member><name>password</name><value><string>$lj_password</string></value></member>
199         <member><name>event</name><value><string>$post</string></value></member>
200         <member><name>subject</name><value><string>$title</string></value></member>
201         <member><name>lineendings</name><value><string>unix</string></value></member>
202         <member><name>year</name><value><int>$year</int></value></member>
203         <member><name>mon</name><value><int>$mon</int></value></member>
204         <member><name>day</name><value><int>$day</int></value></member>
205         <member><name>hour</name><value><int>$hour</int></value></member>
206         <member><name>min</name><value><int>$min</int></value></member>
207                 <member><name>usejournal</name><value><string>$lj_username</string></value></member>
208                 <member>
209                         <name>props</name>
210                         <value>
211                                 <struct>
212                                         <member>
213                                                 <name>useragent</name>
214                                                 <value><string>Friendica</string></value>
215                                         </member>
216                                 </struct>
217                         </value>
218                 </member>
219         </struct>
220     </value></param>
221   </params>
222 </methodCall>
223
224 EOT;
225
226                 logger('ljpost: data: ' . $xml, LOGGER_DATA);
227
228                 if($lj_blog !== 'test')
229                         $x = post_url($lj_blog,$xml);
230                 logger('posted to livejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
231
232         }
233 }
234