Skip to content

Commit

Permalink
Add JSONP support. You must specify &output=jsonp?callback=<js_function>
Browse files Browse the repository at this point in the history
Fixes ticket #1205.
  • Loading branch information
bharat committed Aug 8, 2010
1 parent 9b5e058 commit d6f5a8a
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions modules/rest/helpers/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,27 @@ static function reply($data=array()) {
Session::instance()->abort_save();

header("X-Gallery-API-Version: " . rest::API_VERSION);
if (Input::instance()->get("output") == "html") {
switch (Input::instance()->get("output", "json")) {
case "json":
json::reply($data);
break;

case "jsonp":
if (!($callback = Input::instance()->get("callback", ""))) {
throw new Rest_Exception(
"Bad Request", 400, array("errors" => array("callback" => "missing")));
}

if (preg_match('/^[$A-Za-z_][0-9A-Za-z_]*$/', $callback) == 1) {
header("Content-type: application/javascript");
print "$callback(" . json_encode($data) . ")";
} else {
throw new Rest_Exception(
"Bad Request", 400, array("errors" => array("callback" => "invalid")));
}
break;

case "html":
header("Content-type: text/html");
if ($data) {
$html = preg_replace(
Expand All @@ -34,8 +54,10 @@ static function reply($data=array()) {
$html = t("Empty response");
}
print "<pre>$html</pre>";
} else {
json::reply($data);
break;

default:
throw new Rest_Exception("Bad Request", 400);
}
}

Expand Down

0 comments on commit d6f5a8a

Please sign in to comment.