-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
124 lines (97 loc) · 3.3 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
include("inc/config.php");
// Get basic endpoint that's being called
isset($_GET['url']) ? $url = $_GET['url'] : $url = $_GET['url'];
// Generate the actual XML API uri
$api_url = str_replace("json", "xml", $url);
// Get querystring
$request_uri = $_SERVER['REQUEST_URI'];
$qs = "/".str_replace($base_url, '', $request_uri);
$qs = str_replace($url, '', $qs);
// Merge the pre-redirect querystring back in
$parsed_uri = parse_url($request_uri);
if(isset($parsed_uri["query"])) {
parse_str($parsed_uri["query"], $qs_array);
$_GET = array_merge($_GET, $qs_array);
}
// Channel ID
isset($_GET['channel']) ? $channel = intval($_GET['channel']): $channel = 0;
// Content type
isset($_GET['return']) ? $return = "text" : $return = "json";
if($return=="text")
header("Content-type: text/plain; charset=UTF-8");
else
header("Content-type: application/json; charset=UTF-8");
// Callback (for jsonp)
if(isset($_GET['callback'])) {
$callback = addslashes($_GET['callback']);
$jsonp = true;
} else
$jsonp = false;
// Check the API url is allowed (in config.php)
if (array_key_exists($api_url, $allowed_fields)) {
$tourcms = new TourCMS($marketplace_account_id, $api_private_key, "simplexml");
$result = $tourcms->request($api_url.$qs, $channel);
$error = $result->error;
// Start building the output string
$string = '{
"tours" : [';
if ($error=="OK") {
/* fetch associative array */
foreach ($result->tour as $item) {
$string .= '
{
';
foreach ($allowed_fields[$api_url] as $field) {
// Handle images separately
if($field=="images") {
$string .= '
"images" : [';
foreach ($item->images->image as $image) {
$string .= '
"'.$image->url . '", ';
}
$string = substr($string, 0, strlen($string) - 2);
$string .= '
],';
// Also handle special offers separately
} else if($field=="soonest_special_offer" || $field=="recent_special_offer") {
if(isset($item->$field)) {
$string .= '
"'.$field.'" : {';
$offer_fields = array("start_date", "end_date", "date_code", "note", "min_booking_size", "spaces_remaining", "special_offer_type", "price_1", "price_1_display", "price_2", "price_2_display", "special_offer_datetime", "special_offer_note", "original_price_1", "original_price_1_display", "original_price_2", "original_price_2_display");
foreach ($offer_fields as $offer_field) {
$string .= '
"'.$offer_field.'" : "'.$item->$field->$offer_field.'", ';
}
$string = substr($string, 0, strlen($string) - 2);
$string .= '
},';
}
// All other fields handled simply
} else {
$exploded = explode("->", $field);
count($exploded) == 1 ? $value = $item->$exploded[0] : $value = $item->$exploded[0]->$exploded[1];
$string .= '
"'.$field.'" : '.json_encode((string)$value).',';
}
}
$string = substr($string, 0, strlen($string) - 1);
$string .= '
}, ';
}
$string = substr($string, 0, strlen($string) - 2);
/* free result set */
$string .= "
] }";
if($jsonp)
print $callback." (
".$string."
);";
else
print "$string";
} else {
printf("Error: %s\n", $error);
}
}
?>