3e57610a3f9454eb1016ee4410bc20c1d69a0a7b
[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     $s .= '<span id="settings_ijpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
69     $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
70     $s .= '</span>';
71     $s .= '<div id="settings_ijpost_expanded" class="settings-block" style="display: none;">';
72     $s .= '<span class="fakelink" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
73     $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
74     $s .= '</span>';
75
76     $s .= '<div id="ijpost-enable-wrapper">';
77     $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . t('Enable InsaneJournal Post Plugin') . '</label>';
78     $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
79     $s .= '</div><div class="clear"></div>';
80
81     $s .= '<div id="ijpost-username-wrapper">';
82     $s .= '<label id="ijpost-username-label" for="ijpost-username">' . t('InsaneJournal username') . '</label>';
83     $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
84     $s .= '</div><div class="clear"></div>';
85
86     $s .= '<div id="ijpost-password-wrapper">';
87     $s .= '<label id="ijpost-password-label" for="ijpost-password">' . t('InsaneJournal password') . '</label>';
88     $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
89     $s .= '</div><div class="clear"></div>';
90
91     $s .= '<div id="ijpost-bydefault-wrapper">';
92     $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . t('Post to InsaneJournal by default') . '</label>';
93     $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_bydefault" value="1" ' . $def_checked . '/>';
94     $s .= '</div><div class="clear"></div>';
95
96     /* provide a submit button */
97
98     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ijpost-submit" name="ijpost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
99
100 }
101
102
103 function ijpost_settings_post(&$a,&$b) {
104
105         if(x($_POST,'ijpost-submit')) {
106
107                 set_pconfig(local_user(),'ijpost','post',intval($_POST['ijpost']));
108                 set_pconfig(local_user(),'ijpost','post_by_default',intval($_POST['ij_bydefault']));
109                 set_pconfig(local_user(),'ijpost','ij_username',trim($_POST['ij_username']));
110                 set_pconfig(local_user(),'ijpost','ij_password',trim($_POST['ij_password']));
111
112         }
113
114 }
115
116 function ijpost_post_local(&$a,&$b) {
117
118         // This can probably be changed to allow editing by pointing to a different API endpoint
119
120         if($b['edit'])
121                 return;
122
123         if((! local_user()) || (local_user() != $b['uid']))
124                 return;
125
126         if($b['private'] || $b['parent'])
127                 return;
128
129     $ij_post   = intval(get_pconfig(local_user(),'ijpost','post'));
130
131         $ij_enable = (($ij_post && x($_REQUEST,'ijpost_enable')) ? intval($_REQUEST['ijpost_enable']) : 0);
132
133         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ijpost','post_by_default')))
134                 $ij_enable = 1;
135
136     if(! $ij_enable)
137        return;
138
139     if(strlen($b['postopts']))
140        $b['postopts'] .= ',';
141      $b['postopts'] .= 'ijpost';
142 }
143
144
145
146
147 function ijpost_send(&$a,&$b) {
148
149     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
150         return;
151
152     if(! strstr($b['postopts'],'ijpost'))
153         return;
154
155     if($b['parent'] != $b['id'])
156         return;
157
158         // insanejournal post in the LJ user's timezone. 
159         // Hopefully the person's Friendica account
160         // will be set to the same thing.
161
162         $tz = 'UTC';
163
164         $x = q("select timezone from user where uid = %d limit 1",
165                 intval($b['uid'])
166         );
167         if($x && strlen($x[0]['timezone']))
168                 $tz = $x[0]['timezone'];        
169
170         $ij_username = get_pconfig($b['uid'],'ijpost','ij_username');
171         $ij_password = get_pconfig($b['uid'],'ijpost','ij_password');
172         $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
173
174         if($ij_username && $ij_password && $ij_blog) {
175
176                 require_once('include/bbcode.php');
177                 require_once('include/datetime.php');
178
179                 $title = $b['title'];
180                 $post = bbcode($b['body']);
181                 $post = xmlify($post);
182                 $tags = ijpost_get_tags($b['tag']);
183
184                 $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
185                 $year = intval(substr($date,0,4));
186                 $mon  = intval(substr($date,5,2));
187                 $day  = intval(substr($date,8,2));
188                 $hour = intval(substr($date,11,2));
189                 $min  = intval(substr($date,14,2));
190
191                 $xml = <<< EOT
192 <?xml version="1.0" encoding="utf-8"?>
193 <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
194 <params><param>
195 <value><struct>
196 <member><name>year</name><value><int>$year</int></value></member>
197 <member><name>mon</name><value><int>$mon</int></value></member>
198 <member><name>day</name><value><int>$day</int></value></member>
199 <member><name>hour</name><value><int>$hour</int></value></member>
200 <member><name>min</name><value><int>$min</int></value></member>
201 <member><name>event</name><value><string>$post</string></value></member>
202 <member><name>username</name><value><string>$ij_username</string></value></member>
203 <member><name>password</name><value><string>$ij_password</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>ver</name><value><int>1</int></value></member>
207 <member><name>props</name>
208 <value><struct>
209 <member><name>useragent</name><value><string>Friendica</string></value></member>
210 <member><name>taglist</name><value><string>$tags</string></value></member>
211 </struct></value></member>
212 </struct></value>
213 </param></params>
214 </methodCall>
215
216 EOT;
217
218                 logger('ijpost: data: ' . $xml, LOGGER_DATA);
219
220                 if($ij_blog !== 'test')
221                         $x = post_url($ij_blog,$xml,array("Content-Type: text/xml"));
222                 logger('posted to insanejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
223
224         }
225 }
226
227 function ijpost_get_tags($post)
228 {
229         preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
230         $tags = implode(', ',$matches[1]);
231         return $tags;
232 }