Commit 6e9457c0 authored by Leigh B Stoller's avatar Leigh B Stoller

Cleaning up the ajax server code into per page modules, rather

then scattering around. Makes the php pages a lot cleaner, and
reduces code duplication. Its an approach, lets see how it goes.
parent 161415f3
This diff is collapsed.
......@@ -5,10 +5,12 @@ require(['jquery', 'js/quickvm_sup',
function ($, sup)
{
'use strict';
var ajaxurl = null;
function initialize()
{
window.APT_OPTIONS.initialize(sup);
ajaxurl = window.AJAXURL;
var table = $(".tablesorter")
.tablesorter({
......@@ -65,7 +67,8 @@ function ($, sup)
($("#showtopo_nopicker").outerWidth() - 2),
300, topo, null);
};
var $xmlthing = sup.CallMethod("getprofile", null, 0, profile);
var $xmlthing = sup.CallServerMethod(ajaxurl, "GetProfile",
{"uuid" : profile});
$xmlthing.done(callback);
}
......
......@@ -35,6 +35,30 @@ function CallMethod(method, callback, uuid, arg)
});
}
function CallServerMethod(url, method, args)
{
if (url == null) {
url = 'https://' + window.location.host + '/apt/server-ajax.php';
}
return $.ajax({
// the URL for the request
url: url,
// the data to send (will be converted to a query string)
data: {
ajax_request: 1,
ajax_method: method,
ajax_args: args,
},
// whether this is a POST or GET request
type: "POST",
// the type of data we expect back
dataType : "json",
});
}
function maketopmap(divname, width, height, json, sshcallback)
{
var ismousedown = false;
......@@ -352,6 +376,7 @@ return {
ShowModal: ShowModal,
HideModal: HideModal,
CallMethod: CallMethod,
CallServerMethod: CallServerMethod,
ConvertManifestToJSON: ConvertManifestToJSON,
maketopmap: maketopmap,
SpitOops: SpitOops,
......
<?php
#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
# Local Variables:
# mode:php
# End:
chdir("..");
include_once("webtask.php");
chdir("apt");
include_once("profile_defs.php");
#
# Return info about specific profile.
#
function Do_GetProfile()
{
global $this_idx;
global $ajax_args;
if (!isset($ajax_args["uuid"])) {
SPITAJAX_ERROR(1, "Missing profile uuid");
return;
}
$profile = Profile::Lookup($ajax_args["uuid"]);
if (!$profile) {
SPITAJAX_ERROR(1, "Unknown profile uuid");
return;
}
if ($this_idx != $profile->creator_idx() && !ISADMIN()) {
SPITAJAX_ERROR(1, "Not enough permission");
return;
}
SPITAJAX_RESPONSE(array('rspec' => $profile->rspec(),
'name' => $profile->name(),
'idx' => $profile->idx(),
'description' => $profile->description()));
}
?>
......@@ -31,10 +31,7 @@ $page_title = "My Profiles";
# Verify page arguments.
#
$optargs = OptionalPageArguments("target_user", PAGEARG_USER,
"all", PAGEARG_BOOLEAN,
"ajax_request", PAGEARG_BOOLEAN,
"ajax_method", PAGEARG_STRING,
"ajax_argument", PAGEARG_STRING);
"all", PAGEARG_BOOLEAN);
#
# Get current user.
......@@ -42,10 +39,6 @@ $optargs = OptionalPageArguments("target_user", PAGEARG_USER,
RedirectSecure();
$this_user = CheckLogin($check_status);
if (!$this_user) {
if (isset($ajax_request)) {
SPITAJAX_ERROR(1, "You are not logged in anymore");
exit();
}
RedirectLoginPage();
exit();
}
......@@ -54,42 +47,13 @@ if (!isset($target_user)) {
}
if (!$this_user->SameUser($target_user)) {
if (!ISADMIN()) {
if (isset($ajax_request)) {
SPITAJAX_ERROR(1, "You do not have permission to do this");
}
else {
SPITUSERERROR("You do not have permission to view ".
"target user's profiles");
}
SPITUSERERROR("You do not have permission to view ".
"target user's profiles");
exit();
}
}
$target_idx = $target_user->uid_idx();
#
# Deal with ajax requests.
#
if (isset($ajax_request)) {
if ($ajax_method == "getprofile") {
$profile_idx = addslashes($ajax_argument);
$query_result =
DBQueryWarn("select * from apt_profiles ".
"where idx='$profile_idx'");
if (!$query_result || !mysql_num_rows($query_result)) {
SPITAJAX_ERROR(1, "No such profile $profile_idx!");
exit();
}
$row = mysql_fetch_array($query_result);
SPITAJAX_RESPONSE(array('rspec' => $row['rspec'],
'name' => $row['name'],
'idx' => $row['idx'],
'description' => $row['description']));
}
exit();
}
SPITHEADER(1);
echo "<link rel='stylesheet'
......@@ -215,6 +179,10 @@ echo "<!-- This is the topology view modal -->
</div>
</div>\n";
echo "<script type='text/javascript'>\n";
echo " window.AJAXURL = 'server-ajax.php';\n";
echo "</script>\n";
echo "<script src='js/lib/require.js' data-main='js/myprofiles'></script>\n";
SPITFOOTER();
......
<?php
#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
chdir("..");
include("defs.php3");
chdir("apt");
include("quickvm_sup.php");
#
# Redefine this so we return XML instead of html for all errors.
#
$PAGEERROR_HANDLER = function($msg, $status_code = 0) {
if ($status_code == 0) {
$status_code = 1;
}
SPITAJAX_ERROR(1, $msg);
return;
};
#
# At this point, must always be a logged in user.
#
$this_user = CheckLogin($check_status);
if (!$this_user) {
SPITAJAX_ERROR(2, "Your login has timed out");
return;
}
$this_idx = $this_user->uid_idx();
#
# Verify page arguments.
#
$optargs = RequiredPageArguments("ajax_request", PAGEARG_BOOLEAN,
"ajax_method", PAGEARG_STRING,
"ajax_args", PAGEARG_ARRAY);
#
# If we get more then a few, this will be annyoing.
#
if ($ajax_method == "SnapShotStatus") {
include("manage_profile.ajax");
Do_SnapShotStatus();
}
if ($ajax_method == "GetProfile") {
include("myprofiles.ajax");
Do_GetProfile();
}
else {
SPITAJAX_ERROR(1, "Unknown request");
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment