4 * Name: Posterous Post Connector
5 * Description: Post to Posterous accounts
7 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8 * Author: Tony Baldwin <https://free-haven.org/u/tony>
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');
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');
29 function posterous_jot_nets(&$a,&$b) {
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>';
43 function posterous_settings(&$a,&$s) {
48 /* Add our stylesheet to the page so we can make our settings look nice */
50 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/posterous/posterous.css' . '" media="all" />' . "\r\n";
52 /* Get the current state of our config variables */
54 $enabled = get_pconfig(local_user(),'posterous','post');
56 $checked = (($enabled) ? ' checked="checked" ' : '');
58 $def_enabled = get_pconfig(local_user(),'posterous','post_by_default');
60 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
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');
68 /* Add some HTML to the existing form */
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>';
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>';
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>';
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>';
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>';
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>';
102 /* provide a submit button */
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>';
109 function posterous_settings_post(&$a,&$b) {
111 if(x($_POST,'posterous-submit')) {
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']));
124 function posterous_post_local(&$a,&$b) {
126 // This can probably be changed to allow editing by pointing to a different API endpoint
131 if((! local_user()) || (local_user() != $b['uid']))
134 if($b['private'] || $b['parent'])
137 $pstr_post = intval(get_pconfig(local_user(),'posterous','post'));
139 $pstr_enable = (($pstr_post && x($_REQUEST,'posterous_enable')) ? intval($_REQUEST['posterous_enable']) : 0);
141 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'posterous','post_by_default')))
147 if(strlen($b['postopts']))
148 $b['postopts'] .= ',';
149 $b['postopts'] .= 'posterous';
155 function posterous_send(&$a,&$b) {
157 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
160 if(! strstr($b['postopts'],'posterous'))
163 if($b['parent'] != $b['id'])
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');
173 if($pstr_username && $pstr_password && $pstr_blog) {
175 require_once('include/bbcode.php');
178 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
181 foreach($matches as $mtch) {
182 $tag_arr[] = $mtch[2];
186 $tags = implode(',',$tag_arr);
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
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);
208 $data = curl_exec($ch);
209 $result = curl_multi_getcontent($ch);
212 logger('posterous_send: ' . $result);