r4284 index.php: add "ajax" case to module dispatcher
authorDenis Ovsienko <infrastation@yandex.ru>
Thu, 3 Mar 2011 22:31:43 +0000 (22:31 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Thu, 3 Mar 2011 22:31:43 +0000 (22:31 +0000)
dispatchAJAXRequest(): new function to merge meaningful code from ajax.php
ajax.php: dismiss
renderRackCodeEditor(): update AJAX URI
portinfo.js: ditto

wwwroot/ajax.php [deleted file]
wwwroot/inc/ajax-interface.php
wwwroot/inc/interface.php
wwwroot/index.php
wwwroot/js/portinfo.js

diff --git a/wwwroot/ajax.php b/wwwroot/ajax.php
deleted file mode 100644 (file)
index e90af04..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-<?php
-ob_start();
-try {
-
-require 'inc/init.php';
-require_once 'inc/ajax-interface.php';
-assertStringArg ('ac');
-
-switch ($_REQUEST['ac'])
-{
-case 'verifyCode':
-       $pageno = 'perms';
-       $tabno = 'edit';
-       fixContext();
-       if (!permitted())
-       {
-               echo "NAK\nPermission denied";
-               exit();
-       }
-       assertStringArg ('code');
-       $result = getRackCode (dos2unix ($_REQUEST['code']));
-       if ($result['result'] == 'ACK')
-               echo "ACK\n";
-       else
-               echo "NAK\n" . $result['load'];
-       break;
-       case 'get-port-link': // returns JSON-encoded text
-               assertUIntArg ('object_id');
-               $object = spotEntity ('object', $_REQUEST['object_id']);
-               fixContext ($object);
-               if (! permitted ('object', 'liveports', 'get_link_status'))
-                       throw new RacktablesError ('Permission denied: $op_get_link_status check failed');
-               $data = formatPortLinkHints ($_REQUEST['object_id']);
-               echo json_encode ($data);
-       break;
-       case 'get-port-mac': // returns JSON-encoded text
-               assertUIntArg ('object_id');
-               $object = spotEntity ('object', $_REQUEST['object_id']);
-               fixContext ($object);
-               if (! permitted ('object', 'liveports', 'get_mac_list'))
-                       throw new RacktablesError ('Permission denied: $op_get_mac_list check failed');
-               $data = formatPortMacHints ($_REQUEST['object_id']);
-               echo json_encode ($data);
-       break;
-       case 'get-port-conf': // returns JSON-encoded text
-               assertUIntArg ('object_id');
-               $object = spotEntity ('object', $_REQUEST['object_id']);
-               fixContext ($object);
-               if (! permitted ('object', 'liveports', 'get_port_conf'))
-                       throw new RacktablesError ('Permission denied: $op_get_port_conf check failed');
-               $data = formatPortConfigHints ($_REQUEST['object_id']);
-               echo json_encode ($data);
-       break;
-default:
-       throw new InvalidRequestArgException ('ac', $_REQUEST['ac']);
-}
-ob_end_flush();
-}
-catch (InvalidRequestArgException $e)
-{
-       ob_end_clean();
-       echo "NAK\nMalformed request";
-}
-catch (Exception $e)
-{
-       ob_end_clean();
-       echo "NAK\nRuntime exception: ". $e->getMessage();
-}
-
-?>
index 0e8fe50a1b4eeefcf7a17830be3c2a99644d1fee..8db848b06782eeff90e09549d9258081cf050e5c 100644 (file)
@@ -88,4 +88,56 @@ function formatPortConfigHints ($object_id, $R = NULL)
        return $result;
 }
 
+function dispatchAJAXRequest()
+{
+       genericAssertion ('ac', 'string');
+       switch ($_REQUEST['ac'])
+       {
+       case 'verifyCode':
+               global $pageno, $tabno;
+               $pageno = 'perms';
+               $tabno = 'edit';
+               fixContext();
+               if (!permitted())
+               {
+                       echo "NAK\nPermission denied";
+                       return;
+               }
+               genericAssertion ('code', 'string');
+               $result = getRackCode (dos2unix ($_REQUEST['code']));
+               if ($result['result'] == 'ACK')
+                       echo "ACK\n";
+               else
+                       echo "NAK\n" . $result['load'];
+               break;
+       case 'get-port-link': // returns JSON-encoded text
+               genericAssertion ('object_id', 'uint');
+               $object = spotEntity ('object', $_REQUEST['object_id']);
+               fixContext ($object);
+               if (! permitted ('object', 'liveports', 'get_link_status'))
+                       throw new RacktablesError ('Permission denied: $op_get_link_status check failed');
+               $data = formatPortLinkHints ($_REQUEST['object_id']);
+               echo json_encode ($data);
+               break;
+       case 'get-port-mac': // returns JSON-encoded text
+               genericAssertion ('object_id', 'uint');
+               fixContext (spotEntity ('object', $_REQUEST['object_id']));
+               if (! permitted ('object', 'liveports', 'get_mac_list'))
+                       throw new RacktablesError ('Permission denied: $op_get_mac_list check failed');
+               $data = formatPortMacHints ($_REQUEST['object_id']);
+               echo json_encode ($data);
+               break;
+       case 'get-port-conf': // returns JSON-encoded text
+               genericAssertion ('object_id', 'uint');
+               fixContext (spotEntity ('object', $_REQUEST['object_id']));
+               if (! permitted ('object', 'liveports', 'get_port_conf'))
+                       throw new RacktablesError ('Permission denied: $op_get_port_conf check failed');
+               $data = formatPortConfigHints ($_REQUEST['object_id']);
+               echo json_encode ($data);
+               break;
+       default:
+               throw new InvalidRequestArgException ('ac', $_REQUEST['ac']);
+       }
+}
+
 ?>
index 9b77abb4f09e6466557b96264543a5a0fefcaed5..be0c10416a6bcb3aa6c7556db85596f5dc7ea429 100644 (file)
@@ -6454,8 +6454,8 @@ function verify()
 {
        $.ajax({
                type: "POST",
-               url: "ajax.php",
-               data: {'ac': 'verifyCode', 'code': RCTA.getCode()},
+               url: "index.php",
+               data: {'module': 'ajax', 'ac': 'verifyCode', 'code': RCTA.getCode()},
                success: function (data)
                {
                        arr = data.split("\\n");
index 00aed1a559ff39571cbcdd8ae3964a2854b77d94..a865222133b645ca06c677eb7c0216cc23bba2a8 100644 (file)
@@ -43,6 +43,24 @@ if (array_key_exists ('module', $_REQUEST))
                        renderError();
                }
                break;
+       case 'ajax':
+               require_once 'inc/ajax-interface.php';
+               require_once 'inc/init.php';
+               try
+               {
+                       dispatchAJAXRequest();
+               }
+               catch (InvalidRequestArgException $e)
+               {
+                       ob_clean();
+                       echo "NAK\nMalformed request";
+               }
+               catch (Exception $e)
+               {
+                       ob_clean();
+                       echo "NAK\nRuntime exception: ". $e->getMessage();
+               }
+               break;
        default:
                throw new InvalidRequestArgException ('module', $_REQUEST['module']);
        }
index c8962dd3b8f559285d702ed56e5d652a739e0815..c8ae3774a710c1b07775090a42984d6a7fd960ab 100644 (file)
@@ -99,8 +99,9 @@ function menuItemClicked(menuItem, menu) {
                                setItemIcon(menuItem, '');
                },
                type: 'GET',
-               url: 'ajax.php',
+               url: 'index.php',
                data: {
+                       'module': 'ajax',
                        'ac': 'get-port-' + type,
                        'object_id': getQueryString('object_id')
                },