]> git.mxchange.org Git - friendica-addons.git/blob - posterous/posterous.php
Additional work for PR 3778
[friendica-addons.git] / posterous / posterous.php
1 <?php
2
3 /**
4  * Name: Posterous Post Connector
5  * Description: Post to Posterous accounts
6  * Version: 1.0
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  * Author: Tony Baldwin <https://free-haven.org/u/tony>
9  * Status: Unsupported
10  */
11
12 function posterous_install() {
13     register_hook('post_local',           'addon/posterous/posterous.php', 'posterous_post_local');
14     register_hook('notifier_normal',      'addon/posterous/posterous.php', 'posterous_send');
15     register_hook('jot_networks',         'addon/posterous/posterous.php', 'posterous_jot_nets');
16     register_hook('connector_settings',      'addon/posterous/posterous.php', 'posterous_settings');
17     register_hook('connector_settings_post', 'addon/posterous/posterous.php', 'posterous_settings_post');
18
19 }
20 function posterous_uninstall() {
21     unregister_hook('post_local',       'addon/posterous/posterous.php', 'posterous_post_local');
22     unregister_hook('notifier_normal',  'addon/posterous/posterous.php', 'posterous_send');
23     unregister_hook('jot_networks',     'addon/posterous/posterous.php', 'posterous_jot_nets');
24     unregister_hook('connector_settings',      'addon/posterous/posterous.php', 'posterous_settings');
25     unregister_hook('connector_settings_post', 'addon/posterous/posterous.php', 'posterous_settings_post');
26 }
27
28
29 function posterous_jot_nets(&$a,&$b) {
30     if(! local_user())
31         return;
32
33     $pstr_post = get_pconfig(local_user(),'posterous','post');
34     if(intval($pstr_post) == 1) {
35         $pstr_defpost = get_pconfig(local_user(),'posterous','post_by_default');
36         $selected = ((intval($pstr_defpost) == 1) ? ' checked="checked" ' : '');
37         $b .= '<div class="profile-jot-net"><input type="checkbox" name="posterous_enable"' . $selected . ' value="1" /> '
38             . t('Post to Posterous') . '</div>';
39     }
40 }
41
42
43 function posterous_settings(&$a,&$s) {
44
45     if(! local_user())
46         return;
47
48     /* Add our stylesheet to the page so we can make our settings look nice */
49
50     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/posterous/posterous.css' . '" media="all" />' . "\r\n";
51
52     /* Get the current state of our config variables */
53
54     $enabled = get_pconfig(local_user(),'posterous','post');
55
56     $checked = (($enabled) ? ' checked="checked" ' : '');
57
58     $def_enabled = get_pconfig(local_user(),'posterous','post_by_default');
59
60     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
61
62         $pstr_username = get_pconfig(local_user(), 'posterous', 'posterous_username');
63         $pstr_password = get_pconfig(local_user(), 'posterous', 'posterous_password');
64         $pstr_site_id = get_pconfig(local_user(), 'posterous', 'posterous_site_id');
65         $pstr_api_token = get_pconfig(local_user(), 'posterous', 'posterous_api_token');
66
67
68     /* Add some HTML to the existing form */
69
70     $s .= '<div class="settings-block">';
71     $s .= '<h3>' . t('Posterous Post Settings') . '</h3>';
72     $s .= '<div id="posterous-enable-wrapper">';
73     $s .= '<label id="posterous-enable-label" for="posterous-checkbox">' . t('Enable Posterous Post Plugin') . '</label>';
74     $s .= '<input id="posterous-checkbox" type="checkbox" name="posterous" value="1" ' . $checked . '/>';
75     $s .= '</div><div class="clear"></div>';
76
77     $s .= '<div id="posterous-username-wrapper">';
78     $s .= '<label id="posterous-username-label" for="posterous-username">' . t('Posterous login') . '</label>';
79     $s .= '<input id="posterous-username" type="text" name="posterous_username" value="' . $pstr_username . '" />';
80     $s .= '</div><div class="clear"></div>';
81
82     $s .= '<div id="posterous-password-wrapper">';
83     $s .= '<label id="posterous-password-label" for="posterous-password">' . t('Posterous password') . '</label>';
84     $s .= '<input id="posterous-password" type="password" name="posterous_password" value="' . $pstr_password . '" />';
85     $s .= '</div><div class="clear"></div>';
86
87     $s .= '<div id="posterous-site_id-wrapper">';
88     $s .= '<label id="posterous-site_id-label" for="posterous-site_id">' . t('Posterous site ID') . '</label>';
89     $s .= '<input id="posterous-site_id" type="text" name="posterous_site_id" value="' . $pstr_site_id . '" />';
90     $s .= '</div><div class="clear"></div>';
91
92     $s .= '<div id="posterous-api_token-wrapper">';
93     $s .= '<label id="posterous-api_token-label" for="posterous-api_token">' . t('Posterous API token') . '</label>';
94     $s .= '<input id="posterous-api_token" type="text" name="posterous_api_token" value="' . $pstr_api_token . '" />';
95     $s .= '</div><div class="clear"></div>';
96
97     $s .= '<div id="posterous-bydefault-wrapper">';
98     $s .= '<label id="posterous-bydefault-label" for="posterous-bydefault">' . t('Post to Posterous by default') . '</label>';
99     $s .= '<input id="posterous-bydefault" type="checkbox" name="posterous_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="posterous-submit" name="posterous-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
105
106 }
107
108
109 function posterous_settings_post(&$a,&$b) {
110
111         if(x($_POST,'posterous-submit')) {
112
113                 set_pconfig(local_user(),'posterous','post',intval($_POST['posterous']));
114                 set_pconfig(local_user(),'posterous','post_by_default',intval($_POST['posterous_bydefault']));
115                 set_pconfig(local_user(),'posterous','posterous_username',trim($_POST['posterous_username']));
116                 set_pconfig(local_user(),'posterous','posterous_password',trim($_POST['posterous_password']));
117                 set_pconfig(local_user(),'posterous','posterous_site_id',trim($_POST['posterous_site_id']));
118                 set_pconfig(local_user(),'posterous','posterous_api_token',trim($_POST['posterous_api_token']));
119
120         }
121
122 }
123
124 function posterous_post_local(&$a,&$b) {
125
126         // This can probably be changed to allow editing by pointing to a different API endpoint
127
128         if($b['edit'])
129                 return;
130
131         if((! local_user()) || (local_user() != $b['uid']))
132                 return;
133
134         if($b['private'] || $b['parent'])
135                 return;
136
137     $pstr_post   = intval(get_pconfig(local_user(),'posterous','post'));
138
139         $pstr_enable = (($pstr_post && x($_REQUEST,'posterous_enable')) ? intval($_REQUEST['posterous_enable']) : 0);
140
141         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'posterous','post_by_default')))
142                 $pstr_enable = 1;
143
144     if(! $pstr_enable)
145        return;
146
147     if(strlen($b['postopts']))
148        $b['postopts'] .= ',';
149      $b['postopts'] .= 'posterous';
150 }
151
152
153
154
155 function posterous_send(&$a,&$b) {
156
157     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
158         return;
159
160     if(! strstr($b['postopts'],'posterous'))
161         return;
162
163     if($b['parent'] != $b['id'])
164         return;
165
166
167         $pstr_username = get_pconfig($b['uid'],'posterous','posterous_username');
168         $pstr_password = get_pconfig($b['uid'],'posterous','posterous_password');
169         $pstr_site_id = get_pconfig($b['uid'],'posterous','posterous_site_id');
170         $pstr_blog = "http://posterous.com/api/2/sites/$pstr_site_id/posts";
171         $pstr_api_token = get_pconfig($b['uid'],'posterous','posterous_api_token');
172
173         if($pstr_username && $pstr_password && $pstr_blog) {
174
175                 require_once('include/bbcode.php');
176                 $tag_arr = array();
177                 $tags = '';
178                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
179
180                 if($x) {
181                         foreach($matches as $mtch) {
182                                 $tag_arr[] = $mtch[2];
183                         }
184                 }
185                 if(count($tag_arr))
186                         $tags = implode(',',$tag_arr);          
187
188
189                 $params = array(
190                         'post[title]' => (($b['title']) ? $b['title'] : t('Post from Friendica')),
191                         'post[source]' => 'Friendica',
192                         'post[tags]' => $tags,
193                         'post[body]' => bbcode($b['body']),
194                         'api_token' => $pstr_api_token,
195                         'site_id' => $pstr_site_id
196                 );
197         
198                 $ch = curl_init();
199                 curl_setopt($ch, CURLOPT_URL, $pstr_blog);
200                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
201                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
202                 curl_setopt($ch, CURLOPT_HEADER, false);
203                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
204                 curl_setopt($ch, CURLOPT_USERPWD, $pstr_username . ':' . $pstr_password);
205                 curl_setopt($ch, CURLOPT_POST, true);
206                 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
207
208                 $data = curl_exec($ch);
209                 $result = curl_multi_getcontent($ch);
210                 curl_close($ch);
211
212                 logger('posterous_send: ' . $result);
213         }
214 }
215