]> git.mxchange.org Git - friendica-addons.git/blob - dwpost/dwpost.php
Merge branch '3.6-rc'
[friendica-addons.git] / dwpost / dwpost.php
1 <?php
2 /**
3  * Name: Dreamwidth Post Connector
4  * Description: Post to dreamwidth
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 dwpost_install() {
19         Addon::registerHook('post_local',           'addon/dwpost/dwpost.php', 'dwpost_post_local');
20         Addon::registerHook('notifier_normal',      'addon/dwpost/dwpost.php', 'dwpost_send');
21         Addon::registerHook('jot_networks',         'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
22         Addon::registerHook('connector_settings',      'addon/dwpost/dwpost.php', 'dwpost_settings');
23         Addon::registerHook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
24
25 }
26 function dwpost_uninstall() {
27         Addon::unregisterHook('post_local',       'addon/dwpost/dwpost.php', 'dwpost_post_local');
28         Addon::unregisterHook('notifier_normal',  'addon/dwpost/dwpost.php', 'dwpost_send');
29         Addon::unregisterHook('jot_networks',     'addon/dwpost/dwpost.php', 'dwpost_jot_nets');
30         Addon::unregisterHook('connector_settings',      'addon/dwpost/dwpost.php', 'dwpost_settings');
31         Addon::unregisterHook('connector_settings_post', 'addon/dwpost/dwpost.php', 'dwpost_settings_post');
32
33 }
34
35
36 function dwpost_jot_nets(&$a,&$b) {
37     if(! local_user())
38         return;
39
40     $dw_post = get_pconfig(local_user(),'dwpost','post');
41     if(intval($dw_post) == 1) {
42         $dw_defpost = get_pconfig(local_user(),'dwpost','post_by_default');
43         $selected = ((intval($dw_defpost) == 1) ? ' checked="checked" ' : '');
44         $b .= '<div class="profile-jot-net"><input type="checkbox" name="dwpost_enable" ' . $selected . ' value="1" /> '
45             . L10n::t('Post to Dreamwidth') . '</div>';
46     }
47 }
48
49
50 function dwpost_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/dwpost/dwpost.css' . '" media="all" />' . "\r\n";
58
59     /* Get the current state of our config variables */
60
61     $enabled = get_pconfig(local_user(),'dwpost','post');
62
63     $checked = (($enabled) ? ' checked="checked" ' : '');
64
65     $def_enabled = get_pconfig(local_user(),'dwpost','post_by_default');
66
67     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
68
69         $dw_username = get_pconfig(local_user(), 'dwpost', 'dw_username');
70         $dw_password = get_pconfig(local_user(), 'dwpost', 'dw_password');
71
72
73     /* Add some HTML to the existing form */
74
75     $s .= '<span id="settings_dwpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_dwpost_expanded\'); openClose(\'settings_dwpost_inflated\');">';
76     $s .= '<img class="connector" src="images/dreamwidth.png" /><h3 class="connector">'. L10n::t("Dreamwidth Export").'</h3>';
77     $s .= '</span>';
78     $s .= '<div id="settings_dwpost_expanded" class="settings-block" style="display: none;">';
79     $s .= '<span class="fakelink" onclick="openClose(\'settings_dwpost_expanded\'); openClose(\'settings_dwpost_inflated\');">';
80     $s .= '<img class="connector" src="images/dreamwidth.png" /><h3 class="connector">'. L10n::t("Dreamwidth Export").'</h3>';
81     $s .= '</span>';
82
83     $s .= '<div id="dwpost-enable-wrapper">';
84     $s .= '<label id="dwpost-enable-label" for="dwpost-checkbox">' . L10n::t('Enable dreamwidth Post Addon') . '</label>';
85     $s .= '<input id="dwpost-checkbox" type="checkbox" name="dwpost" value="1" ' . $checked . '/>';
86     $s .= '</div><div class="clear"></div>';
87
88     $s .= '<div id="dwpost-username-wrapper">';
89     $s .= '<label id="dwpost-username-label" for="dwpost-username">' . L10n::t('dreamwidth username') . '</label>';
90     $s .= '<input id="dwpost-username" type="text" name="dw_username" value="' . $dw_username . '" />';
91     $s .= '</div><div class="clear"></div>';
92
93     $s .= '<div id="dwpost-password-wrapper">';
94     $s .= '<label id="dwpost-password-label" for="dwpost-password">' . L10n::t('dreamwidth password') . '</label>';
95     $s .= '<input id="dwpost-password" type="password" name="dw_password" value="' . $dw_password . '" />';
96     $s .= '</div><div class="clear"></div>';
97
98     $s .= '<div id="dwpost-bydefault-wrapper">';
99     $s .= '<label id="dwpost-bydefault-label" for="dwpost-bydefault">' . L10n::t('Post to dreamwidth by default') . '</label>';
100     $s .= '<input id="dwpost-bydefault" type="checkbox" name="dw_bydefault" value="1" ' . $def_checked . '/>';
101     $s .= '</div><div class="clear"></div>';
102
103     /* provide a submit button */
104
105     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="dwpost-submit" name="dwpost-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
106
107 }
108
109
110 function dwpost_settings_post(&$a,&$b) {
111
112         if(x($_POST,'dwpost-submit')) {
113
114                 set_pconfig(local_user(),'dwpost','post',intval($_POST['dwpost']));
115                 set_pconfig(local_user(),'dwpost','post_by_default',intval($_POST['dw_bydefault']));
116                 set_pconfig(local_user(),'dwpost','dw_username',trim($_POST['dw_username']));
117                 set_pconfig(local_user(),'dwpost','dw_password',trim($_POST['dw_password']));
118
119         }
120
121 }
122
123 function dwpost_post_local(&$a,&$b) {
124
125         // This can probably be changed to allow editing by pointing to a different API endpoint
126
127         if($b['edit'])
128                 return;
129
130         if((! local_user()) || (local_user() != $b['uid']))
131                 return;
132
133         if($b['private'] || $b['parent'])
134                 return;
135
136     $dw_post   = intval(get_pconfig(local_user(),'dwpost','post'));
137
138         $dw_enable = (($dw_post && x($_REQUEST,'dwpost_enable')) ? intval($_REQUEST['dwpost_enable']) : 0);
139
140         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'dwpost','post_by_default')))
141                 $dw_enable = 1;
142
143     if(! $dw_enable)
144        return;
145
146     if(strlen($b['postopts']))
147        $b['postopts'] .= ',';
148      $b['postopts'] .= 'dwpost';
149 }
150
151
152
153
154 function dwpost_send(&$a,&$b) {
155
156     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
157         return;
158
159     if(! strstr($b['postopts'],'dwpost'))
160         return;
161
162     if($b['parent'] != $b['id'])
163         return;
164
165         // dreamwidth post in the LJ user's timezone. 
166         // Hopefully the person's Friendica account
167         // will be set to the same thing.
168
169         $tz = 'UTC';
170
171         $x = q("select timezone from user where uid = %d limit 1",
172                 intval($b['uid'])
173         );
174         if($x && strlen($x[0]['timezone']))
175                 $tz = $x[0]['timezone'];        
176
177         $dw_username = get_pconfig($b['uid'],'dwpost','dw_username');
178         $dw_password = get_pconfig($b['uid'],'dwpost','dw_password');
179         $dw_blog = 'http://www.dreamwidth.org/interface/xmlrpc';
180
181         if($dw_username && $dw_password && $dw_blog) {
182                 $title = $b['title'];
183                 $post = BBCode::convert($b['body']);
184                 $post = xmlify($post);
185                 $tags = dwpost_get_tags($b['tag']);
186
187                 $date = DateTimeFormat::convert($b['created'], $tz);
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>$dw_username</string></value></member>
206 <member><name>password</name><value><string>$dw_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('dwpost: data: ' . $xml, LOGGER_DATA);
222
223                 if($dw_blog !== 'test') {
224                         $x = Network::post($dw_blog, $xml, ["Content-Type: text/xml"]);
225                 }
226                 logger('posted to dreamwidth: ' . ($x) ? $x : '', LOGGER_DEBUG);
227         }
228 }
229
230 function dwpost_get_tags($post)
231 {
232         preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
233         $tags = implode(', ',$matches[1]);
234         return $tags;
235 }