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