fix ported from 6.x branch.
[project/fb.git] / fb_url_rewrite.inc
CommitLineData
fc3080a3 1<?php
fc3080a3
DC
2/**
3 * @file
4 * Performs custom url rewriting for Drupal for Facebook.
5 *
0b3a0b45
DC
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
14 * one is hosted.
fc3080a3 15 *
0b3a0b45
DC
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.
19092d2f 20 *
0b3a0b45
DC
21 * If your application supports canvas pages and/or profile tabs, include this
22 * file in your settings.php.
fc3080a3
DC
23 * I.e. the end of your settings.php might look like:
24 *
0b3a0b45 25 * include 'sites/all/modules/fb/fb_url_rewrite.inc'; // For canvas pages support.
fc3080a3
DC
26 * include 'sites/all/modules/fb/fb_settings.inc'; // Always include this.
27 */
626faa71 28
fc3080a3
DC
29/**
30 * Returns a list of the values which we prepend to paths when rewriting urls.
31 */
32function _fb_settings_url_rewrite_prefixes() {
3cf45120 33 $prefixes = &drupal_static(__FUNCTION__);
fc3080a3
DC
34 if (!isset($prefixes)) {
35 $prefixes = array(
36 FB_SETTINGS_CB,
b389f367 37 FB_SETTINGS_CB_PAGE,
626faa71 38 FB_SETTINGS_CB_TYPE,
fc3080a3
DC
39 FB_SETTINGS_CB_SESSION,
40 );
41 }
42 return $prefixes;
43}
44
45/**
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).
49 */
50function _fb_settings_parse($key) {
a732fef6 51 // XXX will this $_GET[q] stuff still work in D7???
fc3080a3
DC
52 if (isset($_GET['q'])) {
53 $path = $_GET['q'];
54 $pos = strpos($path, $key . '/');
55 if ($pos !== FALSE) {
56 // Too soon for arg() function.
57 $args = explode('/', $path);
58 $i = 0;
59 while (isset($args[$i]) && isset($args[$i+1])) {
60 if ($args[$i] == $key)
61 // Found the value we're interested in.
62 return $args[$i+1];
63 $i = $i + 2;
64 }
65 }
66 }
67}
68
69
fc3080a3
DC
70//// URL Management
71
fc3080a3
DC
72
73/**
3511345c
DC
74 * Implements hook_url_outbound_alter().
75 *
76 * @param $options
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.
fc3080a3
DC
80 */
81function fb_url_outbound_alter(&$path, &$options, $original_path) {
4f7ade71 82 if ((isset($options['external']) && $options['external']) ||
b7dbf81c 83 (isset($options['fb_url_alter']) && $options['fb_url_alter'] === FALSE)) {
39677b31
DC
84 return;
85 }
626faa71 86
39677b31
DC
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);
90 }
91
92 $pre = '';
93
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 . '/';
3511345c 99 }
18e16e53 100 }
49c58876 101
18e16e53
DC
102 if ($pre) {
103 if ($path == '<front>') {
104 // Do we really have to do this here?
105 $path = '';
106 }
49c58876 107
18e16e53
DC
108 $path =
109 $pre .
110 (!empty($options['prefix']) ? $options['prefix'] : '') .
111 $path;
49c58876
DC
112
113 // We have manually set the prefix, so remove the options prefix.
114 $options['prefix'] = '';
fc3080a3 115 }
626faa71 116
39677b31
DC
117 // Since we called fb_canvas_url_alter already, supress it from acting again.
118 $options['fb_url_alter'] = FALSE;
fc3080a3
DC
119}
120
121/**
3cf45120 122 * Implements hook_url_inbound_alter().
fc3080a3
DC
123 *
124 * Rewrite URLs for facebook canvas pages, and connect callbacks.
19092d2f 125 *
fc3080a3 126 */
18e16e53 127function fb_url_inbound_alter(&$path, $original_path, $path_language) {
49c58876
DC
128 global $language;
129
fc3080a3
DC
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);
626faa71
DC
137 if (fb_settings($key) === NULL) // defer to previously set values
138 fb_settings($key, $value); // Store for use later.
fc3080a3 139 }
2df03b58 140
18e16e53
DC
141 // Remove our prefixes
142 $path = implode('/', $args);
143
144 // Because Drupal calls hook_url_inbound_alter() later than it should, we have to repeat this work, which path.inc has already done.
145 if (!$path) {
146 $path = variable_get('site_frontpage', 'node');
147 }
148 $alias = drupal_lookup_path('source', $path, $path_language);
149 if ($alias) {
fc3080a3 150 $path = $alias;
18e16e53 151 }
fc3080a3
DC
152 }
153
154 $result = $path;
155}
156