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