2 // Copyright 2004-2009 Facebook. All Rights Reserved.
4 // +---------------------------------------------------------------------------+
5 // | Facebook Platform PHP5 client |
6 // +---------------------------------------------------------------------------+
7 // | Copyright (c) 2007 Facebook, Inc. |
8 // | All rights reserved. |
10 // | Redistribution and use in source and binary forms, with or without |
11 // | modification, are permitted provided that the following conditions |
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. |
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 // +---------------------------------------------------------------------------+
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.
42 class FacebookMobile extends Facebook {
43 // the application secret, which differs from the session secret
45 public function __construct($api_key, $secret, $generate_session_secret=false) {
46 parent::__construct($api_key, $secret, $generate_session_secret);
49 public function redirect($url) {
50 header('Location: '. $url);
53 public function get_m_url($action, $params) {
54 $page = parent::get_facebook_url('m'). '/' .$action;
55 foreach($params as $key => $val) {
60 return $page . '?' . http_build_query($params);
63 public function get_www_url($action, $params) {
64 $page = parent::get_facebook_url('www'). '/' .$action;
65 foreach($params as $key => $val) {
70 return $page . '?' . http_build_query($params);
73 public function get_add_url($next=null) {
75 return $this->get_m_url('add.php', array('api_key' => $this->api_key,
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,
84 'cancel' => $cancel));
87 public function get_logout_url($next=null) {
88 $params = array('api_key' => $this->api_key,
89 'session_key' => $this->api_client->session_key,
93 $params['connect_next'] = 1;
94 $params['next'] = $next;
97 return $this->get_m_url('logout.php', $params);
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()));
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
112 public function get_fbconnect_register_url() {
113 return $this->get_register_url(parent::current_url(), parent::current_url());
115 public function get_fbconnect_tos_url() {
116 return $this->get_tos_url(parent::current_url(), parent::current_url(), $this->in_frame());
119 public function get_fbconnect_logout_url() {
120 return $this->get_logout_url(parent::current_url());
123 public function logout_user() {
127 public function get_prompt_permissions_url($ext_perm,
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'));
141 * support both prompt_permissions.php and authorize.php for now.
142 * authorized.php is to be deprecate though.
144 public function get_extended_permission_url($ext_perm,
147 $next = $next ? $next : parent::current_url();
148 $cancel_url = $cancel_url ? $cancel_url : parent::current_url();
150 return $this->get_m_url('authorize.php',
151 array('api_key' => $this->api_key,
152 'ext_perm' => $ext_perm,
154 'cancel_url' => $cancel_url));
158 public function render_prompt_feed_url($action_links=NULL,
161 $user_message_prompt='',
168 $params = array('api_key' => $this->api_key,
169 'session_key' => $this->api_client->session_key,
171 if (!empty($attachment)) {
172 $params['attachment'] = urlencode(json_encode($attachment));
174 $attachment = new stdClass();
175 $app_display_info = $this->api_client->admin_getAppProperties(array('application_name',
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);
191 $params['attachment'] = urlencode(json_encode($attachment));
193 $params['preview'] = $preview;
194 $params['message'] = $message;
195 $params['user_message_prompt'] = $user_message_prompt;
196 if (!empty($callback)) {
197 $params['callback'] = $callback;
199 $params['callback'] = $this->current_url();
201 if (!empty($cancel)) {
202 $params['cancel'] = $cancel;
204 $params['cancel'] = $this->current_url();
207 if (!empty($target_id)) {
208 $params['target_id'] = $target_id;
210 if (!empty($action_links)) {
211 $params['action_links'] = urlencode(json_encode($action_links));
214 $params['display'] = 'wap';
215 header('Location: '. $this->get_www_url('connect/prompt_feed.php', $params));
219 public function render_feed_form_url($template_id=NULL,
223 $user_message_prompt=NULL,
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;
234 $params['message'] = $user_message ? $user_message['value'] : '';
235 if (isset($body_general) && $body_general) {
236 $params['body_general'] = $body_general;
238 if (isset($user_message_prompt) && $user_message_prompt) {
239 $params['user_message_prompt'] = $user_message_prompt;
241 if (isset($callback) && $callback) {
242 $params['callback'] = $callback;
244 $params['callback'] = $this->current_url();
246 if (isset($cancel) && $cancel) {
247 $params['cancel'] = $cancel;
249 $params['cancel'] = $this->current_url();
251 if (isset($template_data) && $template_data) {
252 $params['template_data'] = $template_data;
254 if (isset($target_id) && $target_id) {
255 $params['to_ids'] = $target_id;
257 $params['display'] = 'wap';
258 header('Location: '. $this->get_www_url('connect/prompt_feed.php', $params));