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