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