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>
11 function posterous_install() {
12 register_hook('post_local', 'addon/posterous/posterous.php', 'posterous_post_local');
13 register_hook('notifier_normal', 'addon/posterous/posterous.php', 'posterous_send');
14 register_hook('jot_networks', 'addon/posterous/posterous.php', 'posterous_jot_nets');
15 register_hook('connector_settings', 'addon/posterous/posterous.php', 'posterous_settings');
16 register_hook('connector_settings_post', 'addon/posterous/posterous.php', 'posterous_settings_post');
19 function posterous_uninstall() {
20 unregister_hook('post_local', 'addon/posterous/posterous.php', 'posterous_post_local');
21 unregister_hook('notifier_normal', 'addon/posterous/posterous.php', 'posterous_send');
22 unregister_hook('jot_networks', 'addon/posterous/posterous.php', 'posterous_jot_nets');
23 unregister_hook('connector_settings', 'addon/posterous/posterous.php', 'posterous_settings');
24 unregister_hook('connector_settings_post', 'addon/posterous/posterous.php', 'posterous_settings_post');
28 function posterous_jot_nets(&$a,&$b) {
32 $pstr_post = get_pconfig(local_user(),'posterous','post');
33 if(intval($pstr_post) == 1) {
34 $pstr_defpost = get_pconfig(local_user(),'posterous','post_by_default');
35 $selected = ((intval($pstr_defpost) == 1) ? ' checked="checked" ' : '');
36 $b .= '<div class="profile-jot-net"><input type="checkbox" name="posterous_enable"' . $selected . 'value="1" /> '
37 . t('Post to Posterous') . '</div>';
42 function posterous_settings(&$a,&$s) {
47 /* Add our stylesheet to the page so we can make our settings look nice */
49 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/posterous/posterous.css' . '" media="all" />' . "\r\n";
51 /* Get the current state of our config variables */
53 $enabled = get_pconfig(local_user(),'posterous','post');
55 $checked = (($enabled) ? ' checked="checked" ' : '');
57 $def_enabled = get_pconfig(local_user(),'posterous','post_by_default');
59 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
61 $pstr_username = get_pconfig(local_user(), 'posterous', 'posterous_username');
62 $pstr_password = get_pconfig(local_user(), 'posterous', 'posterous_password');
63 $pstr_site_id = get_pconfig(local_user(), 'posterous', 'posterous_site_id');
64 $pstr_api_token = get_pconfig(local_user(), 'posterous', 'posterous_api_token');
67 /* Add some HTML to the existing form */
69 $s .= '<div class="settings-block">';
70 $s .= '<h3>' . t('Posterous Post Settings') . '</h3>';
71 $s .= '<div id="posterous-enable-wrapper">';
72 $s .= '<label id="posterous-enable-label" for="posterous-checkbox">' . t('Enable Posterous Post Plugin') . '</label>';
73 $s .= '<input id="posterous-checkbox" type="checkbox" name="posterous" value="1" ' . $checked . '/>';
74 $s .= '</div><div class="clear"></div>';
76 $s .= '<div id="posterous-username-wrapper">';
77 $s .= '<label id="posterous-username-label" for="posterous-username">' . t('Posterous login') . '</label>';
78 $s .= '<input id="posterous-username" type="text" name="posterous_username" value="' . $pstr_username . '" />';
79 $s .= '</div><div class="clear"></div>';
81 $s .= '<div id="posterous-password-wrapper">';
82 $s .= '<label id="posterous-password-label" for="posterous-password">' . t('Posterous password') . '</label>';
83 $s .= '<input id="posterous-password" type="password" name="posterous_password" value="' . $pstr_password . '" />';
84 $s .= '</div><div class="clear"></div>';
86 $s .= '<div id="posterous-site_id-wrapper">';
87 $s .= '<label id="posterous-site_id-label" for="posterous-site_id">' . t('Posterous site ID') . '</label>';
88 $s .= '<input id="posterous-site_id" type="text" name="posterous_site_id" value="' . $pstr_site_id . '" />';
89 $s .= '</div><div class="clear"></div>';
91 $s .= '<div id="posterous-api_token-wrapper">';
92 $s .= '<label id="posterous-api_token-label" for="posterous-api_token">' . t('Posterous API token') . '</label>';
93 $s .= '<input id="posterous-api_token" type="text" name="posterous_api_token" value="' . $pstr_api_token . '" />';
94 $s .= '</div><div class="clear"></div>';
96 $s .= '<div id="posterous-bydefault-wrapper">';
97 $s .= '<label id="posterous-bydefault-label" for="posterous-bydefault">' . t('Post to Posterous by default') . '</label>';
98 $s .= '<input id="posterous-bydefault" type="checkbox" name="posterous_bydefault" value="1" ' . $def_checked . '/>';
99 $s .= '</div><div class="clear"></div>';
101 /* provide a submit button */
103 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="posterous-submit" name="posterous-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
108 function posterous_settings_post(&$a,&$b) {
110 if(x($_POST,'posterous-submit')) {
112 set_pconfig(local_user(),'posterous','post',intval($_POST['posterous']));
113 set_pconfig(local_user(),'posterous','post_by_default',intval($_POST['posterous_bydefault']));
114 set_pconfig(local_user(),'posterous','posterous_username',trim($_POST['posterous_username']));
115 set_pconfig(local_user(),'posterous','posterous_password',trim($_POST['posterous_password']));
116 set_pconfig(local_user(),'posterous','posterous_site_id',trim($_POST['posterous_site_id']));
117 set_pconfig(local_user(),'posterous','posterous_api_token',trim($_POST['posterous_api_token']));
123 function posterous_post_local(&$a,&$b) {
125 // This can probably be changed to allow editing by pointing to a different API endpoint
130 if((! local_user()) || (local_user() != $b['uid']))
133 if($b['private'] || $b['parent'])
136 $pstr_post = intval(get_pconfig(local_user(),'posterous','post'));
138 $pstr_enable = (($pstr_post && x($_REQUEST,'posterous_enable')) ? intval($_REQUEST['posterous_enable']) : 0);
140 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'posterous','post_by_default')))
146 if(strlen($b['postopts']))
147 $b['postopts'] .= ',';
148 $b['postopts'] .= 'posterous';
154 function posterous_send(&$a,&$b) {
156 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
159 if(! strstr($b['postopts'],'posterous'))
162 if($b['parent'] != $b['id'])
166 $pstr_username = get_pconfig($b['uid'],'posterous','posterous_username');
167 $pstr_password = get_pconfig($b['uid'],'posterous','posterous_password');
168 $pstr_site_id = get_pconfig($b['uid'],'posterous','posterous_site_id');
169 $pstr_blog = "http://posterous.com/api/2/sites/$pstr_site_id/posts";
170 $pstr_api_token = get_pconfig($b['uid'],'posterous','posterous_api_token');
172 if($pstr_username && $pstr_password && $pstr_blog) {
174 require_once('include/bbcode.php');
177 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
180 foreach($matches as $mtch) {
181 $tag_arr[] = $mtch[2];
185 $tags = implode(',',$tag_arr);
189 'post[title]' => (($b['title']) ? $b['title'] : t('Post from Friendica')),
190 'post[source]' => 'Friendica',
191 'post[tags]' => $tags,
192 'post[body]' => bbcode($b['body']),
193 'api_token' => $pstr_api_token,
194 'site_id' => $pstr_site_id
198 curl_setopt($ch, CURLOPT_URL, $pstr_blog);
199 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
200 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
201 curl_setopt($ch, CURLOPT_HEADER, false);
202 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
203 curl_setopt($ch, CURLOPT_USERPWD, $pstr_username . ':' . $pstr_password);
204 curl_setopt($ch, CURLOPT_POST, true);
205 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
207 $data = curl_exec($ch);
208 $result = curl_multi_getcontent($ch);
211 logger('posterous_send: ' . $result);