]> git.mxchange.org Git - friendica-addons.git/blob - posterous/posterous.php
Add an INSTALL.txt as an alternative solution to bug #520
[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  */
10
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');
17
18 }
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');
25 }
26
27
28 function posterous_jot_nets(&$a,&$b) {
29     if(! local_user())
30         return;
31
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>';
38     }
39 }
40
41
42 function posterous_settings(&$a,&$s) {
43
44     if(! local_user())
45         return;
46
47     /* Add our stylesheet to the page so we can make our settings look nice */
48
49     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/posterous/posterous.css' . '" media="all" />' . "\r\n";
50
51     /* Get the current state of our config variables */
52
53     $enabled = get_pconfig(local_user(),'posterous','post');
54
55     $checked = (($enabled) ? ' checked="checked" ' : '');
56
57     $def_enabled = get_pconfig(local_user(),'posterous','post_by_default');
58
59     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
60
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');
65
66
67     /* Add some HTML to the existing form */
68
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>';
75
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>';
80
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>';
85
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>';
90
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>';
95
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>';
100
101     /* provide a submit button */
102
103     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="posterous-submit" name="posterous-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
104
105 }
106
107
108 function posterous_settings_post(&$a,&$b) {
109
110         if(x($_POST,'posterous-submit')) {
111
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']));
118
119         }
120
121 }
122
123 function posterous_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     $pstr_post   = intval(get_pconfig(local_user(),'posterous','post'));
137
138         $pstr_enable = (($pstr_post && x($_REQUEST,'posterous_enable')) ? intval($_REQUEST['posterous_enable']) : 0);
139
140         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'posterous','post_by_default')))
141                 $pstr_enable = 1;
142
143     if(! $pstr_enable)
144        return;
145
146     if(strlen($b['postopts']))
147        $b['postopts'] .= ',';
148      $b['postopts'] .= 'posterous';
149 }
150
151
152
153
154 function posterous_send(&$a,&$b) {
155
156     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
157         return;
158
159     if(! strstr($b['postopts'],'posterous'))
160         return;
161
162     if($b['parent'] != $b['id'])
163         return;
164
165
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');
171
172         if($pstr_username && $pstr_password && $pstr_blog) {
173
174                 require_once('include/bbcode.php');
175                 $tag_arr = array();
176                 $tags = '';
177                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
178
179                 if($x) {
180                         foreach($matches as $mtch) {
181                                 $tag_arr[] = $mtch[2];
182                         }
183                 }
184                 if(count($tag_arr))
185                         $tags = implode(',',$tag_arr);          
186
187
188                 $params = array(
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
195                 );
196         
197                 $ch = curl_init();
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);
206
207                 $data = curl_exec($ch);
208                 $result = curl_multi_getcontent($ch);
209                 curl_close($ch);
210
211                 logger('posterous_send: ' . $result);
212         }
213 }
214