]> git.mxchange.org Git - friendica-addons.git/blob - ijpost/ijpost.php
a166e319d877c871acb301b9e1335aa0389a6684
[friendica-addons.git] / ijpost / ijpost.php
1 <?php
2
3 /**
4  * Name: Insanejournal Post Connector
5  * Description: Post to insanejournal
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 ijpost_install() {
13     register_hook('post_local',           'addon/ijpost/ijpost.php', 'ijpost_post_local');
14     register_hook('notifier_normal',      'addon/ijpost/ijpost.php', 'ijpost_send');
15     register_hook('jot_networks',         'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
16     register_hook('connector_settings',      'addon/ijpost/ijpost.php', 'ijpost_settings');
17     register_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
18
19 }
20 function ijpost_uninstall() {
21     unregister_hook('post_local',       'addon/ijpost/ijpost.php', 'ijpost_post_local');
22     unregister_hook('notifier_normal',  'addon/ijpost/ijpost.php', 'ijpost_send');
23     unregister_hook('jot_networks',     'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
24     unregister_hook('connector_settings',      'addon/ijpost/ijpost.php', 'ijpost_settings');
25     unregister_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
26
27 }
28
29
30 function ijpost_jot_nets(&$a,&$b) {
31     if(! local_user())
32         return;
33
34     $ij_post = get_pconfig(local_user(),'ijpost','post');
35     if(intval($ij_post) == 1) {
36         $ij_defpost = get_pconfig(local_user(),'ijpost','post_by_default');
37         $selected = ((intval($ij_defpost) == 1) ? ' checked="checked" ' : '');
38         $b .= '<div class="profile-jot-net"><input type="checkbox" name="ijpost_enable" ' . $selected . ' value="1" /> '
39             . t('Post to insanejournal') . '</div>';
40     }
41 }
42
43
44 function ijpost_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/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
52
53     /* Get the current state of our config variables */
54
55     $enabled = get_pconfig(local_user(),'ijpost','post');
56
57     $checked = (($enabled) ? ' checked="checked" ' : '');
58
59     $def_enabled = get_pconfig(local_user(),'ijpost','post_by_default');
60
61     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
62
63         $ij_username = get_pconfig(local_user(), 'ijpost', 'ij_username');
64         $ij_password = get_pconfig(local_user(), 'ijpost', 'ij_password');
65
66
67     /* Add some HTML to the existing form */
68
69     $s .= '<div class="settings-block">';
70     $s .= '<h3>' . t('insanejournal Post Settings') . '</h3>';
71     $s .= '<div id="ijpost-enable-wrapper">';
72     $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . t('Enable insanejournal Post Plugin') . '</label>';
73     $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
74     $s .= '</div><div class="clear"></div>';
75
76     $s .= '<div id="ijpost-username-wrapper">';
77     $s .= '<label id="ijpost-username-label" for="ijpost-username">' . t('insanejournal username') . '</label>';
78     $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
79     $s .= '</div><div class="clear"></div>';
80
81     $s .= '<div id="ijpost-password-wrapper">';
82     $s .= '<label id="ijpost-password-label" for="ijpost-password">' . t('insanejournal password') . '</label>';
83     $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
84     $s .= '</div><div class="clear"></div>';
85
86     $s .= '<div id="ijpost-bydefault-wrapper">';
87     $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . t('Post to insanejournal by default') . '</label>';
88     $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_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="ijpost-submit" name="ijpost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
94
95 }
96
97
98 function ijpost_settings_post(&$a,&$b) {
99
100         if(x($_POST,'ijpost-submit')) {
101
102                 set_pconfig(local_user(),'ijpost','post',intval($_POST['ijpost']));
103                 set_pconfig(local_user(),'ijpost','post_by_default',intval($_POST['ij_bydefault']));
104                 set_pconfig(local_user(),'ijpost','ij_username',trim($_POST['ij_username']));
105                 set_pconfig(local_user(),'ijpost','ij_password',trim($_POST['ij_password']));
106
107         }
108
109 }
110
111 function ijpost_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     $ij_post   = intval(get_pconfig(local_user(),'ijpost','post'));
125
126         $ij_enable = (($ij_post && x($_REQUEST,'ijpost_enable')) ? intval($_REQUEST['ijpost_enable']) : 0);
127
128         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ijpost','post_by_default')))
129                 $ij_enable = 1;
130
131     if(! $ij_enable)
132        return;
133
134     if(strlen($b['postopts']))
135        $b['postopts'] .= ',';
136      $b['postopts'] .= 'ijpost';
137 }
138
139
140
141
142 function ijpost_send(&$a,&$b) {
143
144     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
145         return;
146
147     if(! strstr($b['postopts'],'ijpost'))
148         return;
149
150     if($b['parent'] != $b['id'])
151         return;
152
153         // insanejournal 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         $ij_username = xmlify(get_pconfig($b['uid'],'ijpost','ij_username'));
166         $ij_password = xmlify(get_pconfig($b['uid'],'ijpost','ij_password'));
167         $ij_journal = xmlify(get_pconfig($b['uid'],'ijpost','ij_journal'));
168
169         $ij_blog = xmlify(get_pconfig($b['uid'],'ijpost','ij_blog'));
170         if(! strlen($ij_blog))
171                 $ij_blog = xmlify('http://www.insanejournal.com/interface/xmlrpc');
172
173         if($ij_username && $ij_password && $ij_blog) {
174
175                 require_once('include/bbcode.php');
176                 require_once('include/datetime.php');
177
178                 $title = xmlify($b['title']);
179                 $post = bbcode($b['body']);
180                 $post = xmlify($post);
181                 $tags = ijpost_get_tags($b['tag']);
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>$ij_username</string></value></member>
198         <member><name>password</name><value><string>$ij_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>$ij_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                                         <member>
217                                                 <name>taglist</name>
218                                                 <value><string>$tags</string></value>
219                                         </member>
220                                 </struct>
221                         </value>
222                 </member>
223         </struct>
224     </value></param>
225   </params>
226 </methodCall>
227
228 EOT;
229
230                 logger('ijpost: data: ' . $xml, LOGGER_DATA);
231
232                 if($ij_blog !== 'test')
233                         $x = post_url($ij_blog,$xml);
234                 logger('posted to insanejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
235
236         }
237 }
238
239 function ijpost_get_tags($post)
240 {
241         preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
242         $tags = implode(', ',$matches[1]);
243         return $tags;
244 }