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