4 * Performs custom url rewriting for Drupal for Facebook.
6 * These function prefix urls for canvas pages and profile tabs. This allows
7 * modules/fb to detect when a canvas page is requested. This is necessary
8 * because the facebook platform does not send us sufficient information to
9 * detect this in some cases, particularly when the user is not logged into
10 * facebook or has not authorized the app. In those case, drupal could not
11 * tell the difference between a canvas request and a regular HTML page
12 * request without this code. The url prefix allows us to detect that the
13 * request is for a canvas page, and which specific application if more than
16 * We define our custom_url rewrite function here, and not in
17 * fb_canvas.module, because custom_url_rewrite_inbound() must be defined
18 * *before* drupal_init_path(), which is called during DRUPAL_BOOTSTRAP_PATH,
19 * before modules are loaded.
21 * If your application supports canvas pages and/or profile tabs, include this
22 * file in your settings.php.
23 * I.e. the end of your settings.php might look like:
25 * include 'sites/all/modules/fb/fb_url_rewrite.inc'; // For canvas pages support.
26 * include 'sites/all/modules/fb/fb_settings.inc'; // Always include this.
30 * Returns a list of the values which we prepend to paths when rewriting urls.
32 function _fb_settings_url_rewrite_prefixes() {
33 $prefixes = &drupal_static(__FUNCTION__
);
34 if (!isset($prefixes)) {
39 FB_SETTINGS_CB_SESSION
,
46 * Parse a setting from the URL. This may be called before
47 * custom_url_rewrite, so we can't count on fb_settings() to return the value.
48 * For internal use only (see fb_session.inc).
50 function _fb_settings_parse($key) {
51 // XXX will this $_GET[q] stuff still work in D7???
52 if (isset($_GET['q'])) {
54 $pos = strpos($path, $key .
'/');
56 // Too soon for arg() function.
57 $args = explode('/', $path);
59 while (isset($args[$i]) && isset($args[$i+1])) {
60 if ($args[$i] == $key)
61 // Found the value we're interested in.
74 * Implements hook_url_outbound_alter().
77 * If $options['fb_url_alter'] == FALSE, this function will not alter the
78 * URL. If used with $options['absolute'] == TRUE, this will generate a
79 * link from a canvas page out to the server's URL.
81 function fb_url_outbound_alter(&$path, &$options, $original_path) {
82 if ((isset($options['external']) && $options['external']) ||
83 (isset($options['fb_url_alter']) && $options['fb_url_alter'] === FALSE
)) {
87 // For most hooks, fb should come before fb_.... modules. But in this case we want fb_canvas to act first.
88 if (function_exists('fb_canvas_url_outbound_alter')) {
89 fb_canvas_url_outbound_alter($path, $options, $original_path);
94 // Prefix each known value to the URL
95 foreach (_fb_settings_url_rewrite_prefixes() as
$prefix) {
96 if (!isset($options[$prefix]) || $options[$prefix] !== FALSE
) {
97 if ($value = fb_settings($prefix))
98 $pre .
= $prefix .
'/' .
$value .
'/';
103 if ($path == '<front>') {
104 // Do we really have to do this here?
110 (!empty($options['prefix']) ?
$options['prefix'] : '') .
113 // We have manually set the prefix, so remove the options prefix.
114 $options['prefix'] = '';
117 // Since we called fb_canvas_url_alter already, supress it from acting again.
118 $options['fb_url_alter'] = FALSE
;
122 * Implements hook_url_inbound_alter().
124 * Rewrite URLs for facebook canvas pages, and connect callbacks.
127 function fb_url_inbound_alter(&$path, $original_path, $path_language) {
130 // See if this is a request for us.
131 if (strpos($path, FB_SETTINGS_CB .
'/') === 0) {
132 // Too soon for arg() function.
133 $args = explode('/', $path);
134 while (count($args) && in_array($args[0], _fb_settings_url_rewrite_prefixes())) {
135 $key = array_shift($args);
136 $value = array_shift($args);
137 if (fb_settings($key) === NULL
) // defer to previously set values
138 fb_settings($key, $value); // Store for use later.
141 // Remove our prefixes
142 $path = implode('/', $args);
144 // Because Drupal calls hook_url_inbound_alter() later than it should, we have to repeat this work, which path.inc has already done.
146 $path = variable_get('site_frontpage', 'node');
148 $alias = drupal_lookup_path('source', $path, $path_language);