]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/facebook/facebook_mobile.php
Merge commit 'origin/0.9.x' into 0.9.x
[quix0rs-gnu-social.git] / plugins / Facebook / facebook / facebook_mobile.php
1 <?php
2 // Copyright 2004-2009 Facebook. All Rights Reserved.
3 //
4 // +---------------------------------------------------------------------------+
5 // | Facebook Platform PHP5 client                                             |
6 // +---------------------------------------------------------------------------+
7 // | Copyright (c) 2007 Facebook, Inc.                                         |
8 // | All rights reserved.                                                      |
9 // |                                                                           |
10 // | Redistribution and use in source and binary forms, with or without        |
11 // | modification, are permitted provided that the following conditions        |
12 // | are met:                                                                  |
13 // |                                                                           |
14 // | 1. Redistributions of source code must retain the above copyright         |
15 // |    notice, this list of conditions and the following disclaimer.          |
16 // | 2. Redistributions in binary form must reproduce the above copyright      |
17 // |    notice, this list of conditions and the following disclaimer in the    |
18 // |    documentation and/or other materials provided with the distribution.   |
19 // |                                                                           |
20 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |
21 // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
22 // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |
23 // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |
24 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |
25 // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
27 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
28 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |
29 // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |
30 // +---------------------------------------------------------------------------+
31 // | For help with this library, contact developers-help@facebook.com          |
32 // +---------------------------------------------------------------------------+
33 //
34 /**
35  *  This class extends and modifies the "Facebook" class to better suit wap
36  *  apps. Since there is no javascript support, we need to use server redirect
37  *  to implement Facebook connect functionalities such as authenticate,
38  *  authorize, feed form etc.. This library provide many helper functions for
39  *  wap developer to locate the right wap url. The url here is targed at
40  *  facebook wap site or wap-friendly url.
41  */
42 class FacebookMobile extends Facebook {
43   // the application secret, which differs from the session secret
44
45   public function __construct($api_key, $secret, $generate_session_secret=false) {
46     parent::__construct($api_key, $secret, $generate_session_secret);
47   }
48
49   public function redirect($url) {
50     header('Location: '. $url);
51   }
52
53   public function get_m_url($action, $params) {
54     $page = parent::get_facebook_url('m'). '/' .$action;
55     foreach($params as $key => $val) {
56       if (!$val) {
57         unset($params[$key]);
58       }
59     }
60     return $page . '?' . http_build_query($params);
61   }
62
63   public function get_www_url($action, $params) {
64     $page = parent::get_facebook_url('www'). '/' .$action;
65     foreach($params as $key => $val) {
66       if (!$val) {
67         unset($params[$key]);
68       }
69     }
70     return $page . '?' . http_build_query($params);
71   }
72
73   public function get_add_url($next=null) {
74
75     return $this->get_m_url('add.php', array('api_key' => $this->api_key,
76                                              'next'    => $next));
77   }
78
79   public function get_tos_url($next=null, $cancel = null, $canvas=null) {
80     return $this->get_m_url('tos.php', array('api_key' => $this->api_key,
81                                              'v'       => '1.0',
82                                              'next'    => $next,
83                                              'canvas'  => $canvas,
84                                              'cancel'   => $cancel));
85   }
86
87   public function get_logout_url($next=null) {
88     $params = array('api_key'     => $this->api_key,
89                     'session_key' => $this->api_client->session_key,
90                    );
91
92     if ($next) {
93       $params['connect_next'] = 1;
94       $params['next'] = $next;
95     }
96
97     return $this->get_m_url('logout.php', $params);
98   }
99   public function get_register_url($next=null, $cancel_url=null) {
100     return $this->get_m_url('r.php',
101       array('fbconnect' => 1,
102             'api_key' => $this->api_key,
103             'next' => $next ? $next : parent::current_url(),
104             'cancel_url' => $cancel_url ? $cancel_url : parent::current_url()));
105   }
106   /**
107    * These set of fbconnect style url redirect back to the application current
108    * page when the action is done. Developer can also use the non fbconnect
109    * style url and provide their own redirect link by giving the right parameter
110    * to $next and/or $cancel_url
111    */
112   public function get_fbconnect_register_url() {
113     return $this->get_register_url(parent::current_url(), parent::current_url());
114   }
115   public function get_fbconnect_tos_url() {
116     return $this->get_tos_url(parent::current_url(), parent::current_url(), $this->in_frame());
117   }
118
119   public function get_fbconnect_logout_url() {
120     return $this->get_logout_url(parent::current_url());
121   }
122
123   public function logout_user() {
124     $this->user = null;
125   }
126
127   public function get_prompt_permissions_url($ext_perm,
128                                              $next=null,
129                                              $cancel_url=null) {
130
131     return $this->get_www_url('connect/prompt_permissions.php',
132       array('api_key' => $this->api_key,
133             'ext_perm' => $ext_perm,
134             'next' => $next ? $next : parent::current_url(),
135             'cancel' => $cancel_url ? $cancel_url : parent::current_url(),
136             'display' => 'wap'));
137
138   }
139
140   /**
141    * support both prompt_permissions.php and authorize.php for now.
142    * authorized.php is to be deprecate though.
143    */
144   public function get_extended_permission_url($ext_perm,
145                                               $next=null,
146                                               $cancel_url=null) {
147     $next = $next ? $next : parent::current_url();
148     $cancel_url = $cancel_url ? $cancel_url : parent::current_url();
149
150     return $this->get_m_url('authorize.php',
151                       array('api_key' => $this->api_key,
152                             'ext_perm' => $ext_perm,
153                             'next' => $next,
154                             'cancel_url' => $cancel_url));
155
156   }
157
158   public function render_prompt_feed_url($action_links=NULL,
159                                          $target_id=NULL,
160                                          $message='',
161                                          $user_message_prompt='',
162                                          $caption=NULL,
163                                          $callback ='',
164                                          $cancel='',
165                                          $attachment=NULL,
166                                          $preview=true) {
167
168     $params = array('api_key'     => $this->api_key,
169                     'session_key' => $this->api_client->session_key,
170                    );
171     if (!empty($attachment)) {
172       $params['attachment'] = urlencode(json_encode($attachment));
173     } else {
174       $attachment = new stdClass();
175       $app_display_info = $this->api_client->admin_getAppProperties(array('application_name',
176                                                                           'callback_url',
177                                                                           'description',
178                                                                           'logo_url'));
179       $app_display_info = $app_display_info;
180       $attachment->name = $app_display_info['application_name'];
181       $attachment->caption = !empty($caption) ? $caption : 'Just see what\'s new!';
182       $attachment->description = $app_display_info['description'];
183       $attachment->href = $app_display_info['callback_url'];
184       if (!empty($app_display_info['logo_url'])) {
185         $logo = new stdClass();
186         $logo->type = 'image';
187         $logo->src = $app_display_info['logo_url'];
188         $logo->href = $app_display_info['callback_url'];
189         $attachment->media = array($logo);
190       }
191       $params['attachment'] = urlencode(json_encode($attachment));
192     }
193     $params['preview'] = $preview;
194     $params['message'] = $message;
195     $params['user_message_prompt'] = $user_message_prompt;
196     if (!empty($callback)) {
197       $params['callback'] = $callback;
198     } else {
199       $params['callback'] = $this->current_url();
200     }
201     if (!empty($cancel)) {
202       $params['cancel'] = $cancel;
203     } else {
204       $params['cancel'] = $this->current_url();
205     }
206
207     if (!empty($target_id)) {
208       $params['target_id'] = $target_id;
209     }
210     if (!empty($action_links)) {
211       $params['action_links'] = urlencode(json_encode($action_links));
212     }
213
214     $params['display'] = 'wap';
215     header('Location: '. $this->get_www_url('connect/prompt_feed.php', $params));
216   }
217
218 //use template_id
219   public function render_feed_form_url($template_id=NULL,
220                                        $template_data=NULL,
221                                        $user_message=NULL,
222                                        $body_general=NULL,
223                                        $user_message_prompt=NULL,
224                                        $target_id=NULL,
225                                        $callback=NULL,
226                                        $cancel=NULL,
227                                        $preview=true) {
228
229     $params = array('api_key' => $this->api_key);
230     $params['preview'] = $preview;
231     if (isset($template_id) && $template_id) {
232       $params['template_id'] = $template_id;
233     }
234     $params['message'] = $user_message ? $user_message['value'] : '';
235     if (isset($body_general) && $body_general) {
236       $params['body_general'] = $body_general;
237     }
238     if (isset($user_message_prompt) && $user_message_prompt) {
239       $params['user_message_prompt'] = $user_message_prompt;
240     }
241     if (isset($callback) && $callback) {
242       $params['callback'] = $callback;
243     } else {
244       $params['callback'] = $this->current_url();
245     }
246     if (isset($cancel) && $cancel) {
247       $params['cancel'] = $cancel;
248     } else {
249       $params['cancel'] = $this->current_url();
250     }
251     if (isset($template_data) && $template_data) {
252       $params['template_data'] = $template_data;
253     }
254     if (isset($target_id) && $target_id) {
255       $params['to_ids'] = $target_id;
256     }
257     $params['display'] = 'wap';
258     header('Location: '. $this->get_www_url('connect/prompt_feed.php', $params));
259   }
260 }