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