/**
  * Test for PMA_RTN_getQueryFromRequest
  *
  * @param array  $request Request
  * @param string $query   Query
  * @param int    $num_err Error number
  *
  * @return void
  *
  * @dataProvider provider
  */
 public function testgetQueryFromRequest($request, $query, $num_err)
 {
     global $_REQUEST, $errors, $cfg;
     $cfg['ShowFunctionFields'] = false;
     $GLOBALS['PMA_Types'] = new PMA_Types_MySQL();
     $errors = array();
     PMA_RTN_setGlobals();
     unset($_REQUEST);
     $_REQUEST = $request;
     $this->assertEquals($query, PMA_RTN_getQueryFromRequest());
     $this->assertEquals($num_err, count($errors));
 }
Пример #2
0
    /**
     * @dataProvider provider
     */
    public function testgetQueryFromRequest($request, $query, $num_err)
    {
        global $_REQUEST, $errors, $cfg;

        $cfg['ShowFunctionFields'] = false;

        include 'libraries/data_mysql.inc.php';

        $errors = array();
        PMA_RTN_setGlobals();

        unset($_REQUEST);
        $_REQUEST = $request;
        $this->assertEquals($query, PMA_RTN_getQueryFromRequest());
        $this->assertEquals($num_err, count($errors));
    }
 /**
  * Test for PMA_RTN_getQueryFromRequest
  *
  * @param array  $request Request
  * @param string $query   Query
  * @param int    $num_err Error number
  *
  * @return void
  *
  * @dataProvider provider
  */
 public function testgetQueryFromRequest($request, $query, $num_err)
 {
     global $_REQUEST, $errors, $cfg;
     $cfg['ShowFunctionFields'] = false;
     $GLOBALS['PMA_Types'] = new TypesMySQL();
     $errors = array();
     PMA_RTN_setGlobals();
     $old_dbi = isset($GLOBALS['dbi']) ? $GLOBALS['dbi'] : null;
     $dbi = $this->getMockBuilder('PMA\\libraries\\DatabaseInterface')->disableOriginalConstructor()->getMock();
     $dbi->expects($this->any())->method('escapeString')->will($this->returnValueMap(array(array('foo', null, 'foo'), array("foo's bar", null, "foo\\'s bar"), array('', null, ''))));
     $GLOBALS['dbi'] = $dbi;
     unset($_REQUEST);
     $_REQUEST = $request;
     $this->assertEquals($query, PMA_RTN_getQueryFromRequest());
     $this->assertEquals($num_err, count($errors));
     // reset
     $GLOBALS['dbi'] = $old_dbi;
 }
Пример #4
0
/**
 * Handle request to create or edit a routine
 *
 * @param array  $errors Errors
 * @param string $db     DB name
 *
 * @return array
 */
function PMA_RTN_handleRequestCreateOrEdit($errors, $db)
{
    if (empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit'])) {
        return $errors;
    }
    $sql_query = '';
    $routine_query = PMA_RTN_getQueryFromRequest();
    if (!count($errors)) {
        // set by PMA_RTN_getQueryFromRequest()
        // Execute the created query
        if (!empty($_REQUEST['editor_process_edit'])) {
            $isProcOrFunc = in_array($_REQUEST['item_original_type'], array('PROCEDURE', 'FUNCTION'));
            if (!$isProcOrFunc) {
                $errors[] = sprintf(__('Invalid routine type: "%s"'), htmlspecialchars($_REQUEST['item_original_type']));
            } else {
                // Backup the old routine, in case something goes wrong
                $create_routine = $GLOBALS['dbi']->getDefinition($db, $_REQUEST['item_original_type'], $_REQUEST['item_original_name']);
                $privilegesBackup = PMA_RTN_backupPrivileges();
                $drop_routine = "DROP {$_REQUEST['item_original_type']} " . PMA\libraries\Util::backquote($_REQUEST['item_original_name']) . ";\n";
                $result = $GLOBALS['dbi']->tryQuery($drop_routine);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_routine)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                } else {
                    list($newErrors, $message) = PMA_RTN_createRoutine($routine_query, $create_routine, $privilegesBackup);
                    if (empty($newErrors)) {
                        $sql_query = $drop_routine . $routine_query;
                    } else {
                        $errors = array_merge($errors, $newErrors);
                    }
                    unset($newErrors);
                    if (null === $message) {
                        unset($message);
                    }
                }
            }
        } else {
            // 'Add a new routine' mode
            $result = $GLOBALS['dbi']->tryQuery($routine_query);
            if (!$result) {
                $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br /><br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
            } else {
                $message = PMA\libraries\Message::success(__('Routine %1$s has been created.'));
                $message->addParam(PMA\libraries\Util::backquote($_REQUEST['item_name']));
                $sql_query = $routine_query;
            }
        }
    }
    if (count($errors)) {
        $message = PMA\libraries\Message::error(__('One or more errors have occurred while' . ' processing your request:'));
        $message->addString('<ul>');
        foreach ($errors as $string) {
            $message->addString('<li>' . $string . '</li>');
        }
        $message->addString('</ul>');
    }
    $output = PMA\libraries\Util::getMessage($message, $sql_query);
    if (!$GLOBALS['is_ajax_request']) {
        return $errors;
    }
    $response = PMA\libraries\Response::getInstance();
    if (!$message->isSuccess()) {
        $response->setRequestStatus(false);
        $response->addJSON('message', $output);
        exit;
    }
    $routines = $GLOBALS['dbi']->getRoutines($db, $_REQUEST['item_type'], $_REQUEST['item_name']);
    $routine = $routines[0];
    $response->addJSON('name', htmlspecialchars(mb_strtoupper($_REQUEST['item_name'])));
    $response->addJSON('new_row', PMA_RTN_getRowForList($routine));
    $response->addJSON('insert', !empty($routine));
    $response->addJSON('message', $output);
    exit;
}
Пример #5
0
/**
 * Handles editor requests for adding or editing an item
 *
 * @return Does not return
 */
function PMA_RTN_handleEditor()
{
    global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;
    if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) {
        /**
         * Handle a request to create/edit a routine
         */
        $sql_query = '';
        $routine_query = PMA_RTN_getQueryFromRequest();
        if (!count($errors)) {
            // set by PMA_RTN_getQueryFromRequest()
            // Execute the created query
            if (!empty($_REQUEST['editor_process_edit'])) {
                if (!in_array($_REQUEST['item_original_type'], array('PROCEDURE', 'FUNCTION'))) {
                    $errors[] = sprintf(__('Invalid routine type: "%s"'), htmlspecialchars($_REQUEST['item_original_type']));
                } else {
                    // Backup the old routine, in case something goes wrong
                    $create_routine = PMA_DBI_get_definition($db, $_REQUEST['item_original_type'], $_REQUEST['item_original_name']);
                    $drop_routine = "DROP {$_REQUEST['item_original_type']} " . PMA_Util::backquote($_REQUEST['item_original_name']) . ";\n";
                    $result = PMA_DBI_try_query($drop_routine);
                    if (!$result) {
                        $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_routine)) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                    } else {
                        $result = PMA_DBI_try_query($routine_query);
                        if (!$result) {
                            $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                            // We dropped the old routine, but were unable to create the new one
                            // Try to restore the backup query
                            $result = PMA_DBI_try_query($create_routine);
                            if (!$result) {
                                // OMG, this is really bad! We dropped the query,
                                // failed to create a new one
                                // and now even the backup query does not execute!
                                // This should not happen, but we better handle
                                // this just in case.
                                $errors[] = __('Sorry, we failed to restore the dropped routine.') . '<br />' . __('The backed up query was:') . "\"" . htmlspecialchars($create_routine) . "\"" . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                            }
                        } else {
                            $message = PMA_Message::success(__('Routine %1$s has been modified.'));
                            $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                            $sql_query = $drop_routine . $routine_query;
                        }
                    }
                }
            } else {
                // 'Add a new routine' mode
                $result = PMA_DBI_try_query($routine_query);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br /><br />' . __('MySQL said: ') . PMA_DBI_getError(null);
                } else {
                    $message = PMA_Message::success(__('Routine %1$s has been created.'));
                    $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                    $sql_query = $routine_query;
                }
            }
        }
        if (count($errors)) {
            $message = PMA_Message::error(__('<b>One or more errors have occured while processing your request:</b>'));
            $message->addString('<ul>');
            foreach ($errors as $string) {
                $message->addString('<li>' . $string . '</li>');
            }
            $message->addString('</ul>');
        }
        $output = PMA_Util::getMessage($message, $sql_query);
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            if ($message->isSuccess()) {
                $columns = "`SPECIFIC_NAME`, `ROUTINE_NAME`, `ROUTINE_TYPE`, `DTD_IDENTIFIER`, `ROUTINE_DEFINITION`";
                $where = "ROUTINE_SCHEMA='" . PMA_Util::sqlAddSlashes($db) . "' " . "AND ROUTINE_NAME='" . PMA_Util::sqlAddSlashes($_REQUEST['item_name']) . "'" . "AND ROUTINE_TYPE='" . PMA_Util::sqlAddSlashes($_REQUEST['item_type']) . "'";
                $routine = PMA_DBI_fetch_single_row("SELECT {$columns} FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE {$where};");
                $response->addJSON('name', htmlspecialchars(strtoupper($_REQUEST['item_name'])));
                $response->addJSON('new_row', PMA_RTN_getRowForList($routine));
                $response->addJSON('insert', !empty($routine));
                $response->addJSON('message', $output);
            } else {
                $response->isSuccess(false);
                $response->addJSON('message', $output);
            }
            exit;
        }
    }
    /**
     * Display a form used to add/edit a routine, if necessary
     */
    if (count($errors) || empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit']) && (!empty($_REQUEST['add_item']) || !empty($_REQUEST['edit_item']) || !empty($_REQUEST['routine_addparameter']) || !empty($_REQUEST['routine_removeparameter']) || !empty($_REQUEST['routine_changetype']))) {
        // Handle requests to add/remove parameters and changing routine type
        // This is necessary when JS is disabled
        $operation = '';
        if (!empty($_REQUEST['routine_addparameter'])) {
            $operation = 'add';
        } else {
            if (!empty($_REQUEST['routine_removeparameter'])) {
                $operation = 'remove';
            } else {
                if (!empty($_REQUEST['routine_changetype'])) {
                    $operation = 'change';
                }
            }
        }
        // Get the data for the form (if any)
        if (!empty($_REQUEST['add_item'])) {
            $title = PMA_RTE_getWord('add');
            $routine = PMA_RTN_getDataFromRequest();
            $mode = 'add';
        } else {
            if (!empty($_REQUEST['edit_item'])) {
                $title = __("Edit routine");
                if (!$operation && !empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit'])) {
                    $routine = PMA_RTN_getDataFromName($_REQUEST['item_name'], $_REQUEST['item_type']);
                    if ($routine !== false) {
                        $routine['item_original_name'] = $routine['item_name'];
                        $routine['item_original_type'] = $routine['item_type'];
                    }
                } else {
                    $routine = PMA_RTN_getDataFromRequest();
                }
                $mode = 'edit';
            }
        }
        if ($routine !== false) {
            // Show form
            $editor = PMA_RTN_getEditorForm($mode, $operation, $routine);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->addJSON('message', $editor);
                $response->addJSON('title', $title);
                $response->addJSON('param_template', PMA_RTN_getParameterRow());
                $response->addJSON('type', $routine['item_type']);
            } else {
                echo "\n\n<h2>{$title}</h2>\n\n{$editor}";
            }
            exit;
        } else {
            $message = __('Error in processing request') . ' : ';
            $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_Util::backquote($db)));
            $message = PMA_message::error($message);
            if ($GLOBALS['is_ajax_request']) {
                $response->isSuccess(false);
                $response->addJSON('message', $message);
                exit;
            } else {
                $message->display();
            }
        }
    }
}
/**
 * Handles editor requests for adding or editing an item
 *
 * @return void
 */
function PMA_RTN_handleEditor()
{
    global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;
    if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) {
        /**
         * Handle a request to create/edit a routine
         */
        $sql_query = '';
        $routine_query = PMA_RTN_getQueryFromRequest();
        if (!count($errors)) {
            // set by PMA_RTN_getQueryFromRequest()
            // Execute the created query
            if (!empty($_REQUEST['editor_process_edit'])) {
                $isProcOrFunc = in_array($_REQUEST['item_original_type'], array('PROCEDURE', 'FUNCTION'));
                if (!$isProcOrFunc) {
                    $errors[] = sprintf(__('Invalid routine type: "%s"'), htmlspecialchars($_REQUEST['item_original_type']));
                } else {
                    // Backup the old routine, in case something goes wrong
                    $create_routine = $GLOBALS['dbi']->getDefinition($db, $_REQUEST['item_original_type'], $_REQUEST['item_original_name']);
                    if (!defined('PMA_DRIZZLE') || !PMA_DRIZZLE) {
                        if (isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv'] && isset($GLOBALS['flush_priv']) && $GLOBALS['flush_priv']) {
                            // Backup the Old Privileges before dropping
                            // if $_REQUEST['item_adjust_privileges'] set
                            $privilegesBackup = array();
                            if (isset($_REQUEST['item_adjust_privileges']) && !empty($_REQUEST['item_adjust_privileges'])) {
                                $privilegesBackupQuery = 'SELECT * FROM ' . PMA_Util::backquote('mysql') . '.' . PMA_Util::backquote('procs_priv') . ' where Routine_name = "' . $_REQUEST['item_original_name'] . '" AND Routine_type = "' . $_REQUEST['item_original_type'] . '";';
                                $privilegesBackup = $GLOBALS['dbi']->fetchResult($privilegesBackupQuery, 0);
                            }
                        }
                    }
                    $drop_routine = "DROP {$_REQUEST['item_original_type']} " . PMA_Util::backquote($_REQUEST['item_original_name']) . ";\n";
                    $result = $GLOBALS['dbi']->tryQuery($drop_routine);
                    if (!$result) {
                        $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_routine)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                    } else {
                        $result = $GLOBALS['dbi']->tryQuery($routine_query);
                        if (!$result) {
                            $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                            // We dropped the old routine,
                            // but were unable to create the new one
                            // Try to restore the backup query
                            $result = $GLOBALS['dbi']->tryQuery($create_routine);
                            $errors = checkResult($result, __('Sorry, we failed to restore' . ' the dropped routine.'), $create_routine, $errors);
                        } else {
                            // Default value
                            $resultAdjust = false;
                            if (!defined('PMA_DRIZZLE') || !PMA_DRIZZLE) {
                                if (isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv'] && isset($GLOBALS['flush_priv']) && $GLOBALS['flush_priv']) {
                                    // Insert all the previous privileges
                                    // but with the new name and the new type
                                    foreach ($privilegesBackup as $priv) {
                                        $adjustProcPrivilege = 'INSERT INTO ' . PMA_Util::backquote('mysql') . '.' . PMA_Util::backquote('procs_priv') . ' VALUES("' . $priv[0] . '", "' . $priv[1] . '", "' . $priv[2] . '", "' . $_REQUEST['item_name'] . '", "' . $_REQUEST['item_type'] . '", "' . $priv[5] . '", "' . $priv[6] . '", "' . $priv[7] . '");';
                                        $resultAdjust = $GLOBALS['dbi']->query($adjustProcPrivilege);
                                    }
                                }
                            }
                            if ($resultAdjust) {
                                // Flush the Privileges
                                $flushPrivQuery = 'FLUSH PRIVILEGES;';
                                $GLOBALS['dbi']->query($flushPrivQuery);
                                $message = PMA_Message::success(__('Routine %1$s has been modified. Privileges have been adjusted.'));
                            } else {
                                $message = PMA_Message::success(__('Routine %1$s has been modified.'));
                            }
                            $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                            $sql_query = $drop_routine . $routine_query;
                        }
                    }
                }
            } else {
                // 'Add a new routine' mode
                $result = $GLOBALS['dbi']->tryQuery($routine_query);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br /><br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                } else {
                    $message = PMA_Message::success(__('Routine %1$s has been created.'));
                    $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                    $sql_query = $routine_query;
                }
            }
        }
        if (count($errors)) {
            $message = PMA_Message::error(__('One or more errors have occurred while' . ' processing your request:'));
            $message->addString('<ul>');
            foreach ($errors as $string) {
                $message->addString('<li>' . $string . '</li>');
            }
            $message->addString('</ul>');
        }
        $output = PMA_Util::getMessage($message, $sql_query);
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            if ($message->isSuccess()) {
                $routines = $GLOBALS['dbi']->getRoutines($db, $_REQUEST['item_type'], $_REQUEST['item_name']);
                $routine = $routines[0];
                $response->addJSON('name', htmlspecialchars(mb_strtoupper($_REQUEST['item_name'])));
                $response->addJSON('new_row', PMA_RTN_getRowForList($routine));
                $response->addJSON('insert', !empty($routine));
                $response->addJSON('message', $output);
            } else {
                $response->isSuccess(false);
                $response->addJSON('message', $output);
            }
            exit;
        }
    }
    /**
     * Display a form used to add/edit a routine, if necessary
     */
    // FIXME: this must be simpler than that
    if (count($errors) || empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit']) && (!empty($_REQUEST['add_item']) || !empty($_REQUEST['edit_item']) || !empty($_REQUEST['routine_addparameter']) || !empty($_REQUEST['routine_removeparameter']) || !empty($_REQUEST['routine_changetype']))) {
        // Handle requests to add/remove parameters and changing routine type
        // This is necessary when JS is disabled
        $operation = '';
        if (!empty($_REQUEST['routine_addparameter'])) {
            $operation = 'add';
        } else {
            if (!empty($_REQUEST['routine_removeparameter'])) {
                $operation = 'remove';
            } else {
                if (!empty($_REQUEST['routine_changetype'])) {
                    $operation = 'change';
                }
            }
        }
        // Get the data for the form (if any)
        if (!empty($_REQUEST['add_item'])) {
            $title = PMA_RTE_getWord('add');
            $routine = PMA_RTN_getDataFromRequest();
            $mode = 'add';
        } else {
            if (!empty($_REQUEST['edit_item'])) {
                $title = __("Edit routine");
                if (!$operation && !empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit'])) {
                    $routine = PMA_RTN_getDataFromName($_REQUEST['item_name'], $_REQUEST['item_type']);
                    if ($routine !== false) {
                        $routine['item_original_name'] = $routine['item_name'];
                        $routine['item_original_type'] = $routine['item_type'];
                    }
                } else {
                    $routine = PMA_RTN_getDataFromRequest();
                }
                $mode = 'edit';
            }
        }
        if ($routine !== false) {
            // Show form
            $editor = PMA_RTN_getEditorForm($mode, $operation, $routine);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->addJSON('message', $editor);
                $response->addJSON('title', $title);
                $response->addJSON('param_template', PMA_RTN_getParameterRow());
                $response->addJSON('type', $routine['item_type']);
            } else {
                echo "\n\n<h2>{$title}</h2>\n\n{$editor}";
            }
            exit;
        } else {
            $message = __('Error in processing request:') . ' ';
            $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_Util::backquote($db)));
            $message = PMA_message::error($message);
            if ($GLOBALS['is_ajax_request']) {
                $response->isSuccess(false);
                $response->addJSON('message', $message);
                exit;
            } else {
                $message->display();
            }
        }
    }
}
Пример #7
0
/**
 * Handles editor requests for adding or editing an item
 *
 * @return void
 */
function PMA_RTN_handleEditor()
{
    global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;
    if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) {
        /**
         * Handle a request to create/edit a routine
         */
        $sql_query = '';
        $routine_query = PMA_RTN_getQueryFromRequest();
        if (!count($errors)) {
            // set by PMA_RTN_getQueryFromRequest()
            // Execute the created query
            if (!empty($_REQUEST['editor_process_edit'])) {
                $isProcOrFunc = in_array($_REQUEST['item_original_type'], array('PROCEDURE', 'FUNCTION'));
                if (!$isProcOrFunc) {
                    $errors[] = sprintf(__('Invalid routine type: "%s"'), htmlspecialchars($_REQUEST['item_original_type']));
                } else {
                    // Backup the old routine, in case something goes wrong
                    $create_routine = $GLOBALS['dbi']->getDefinition($db, $_REQUEST['item_original_type'], $_REQUEST['item_original_name']);
                    $privilegesBackup = PMA_RTN_backupPrivileges();
                    $drop_routine = "DROP {$_REQUEST['item_original_type']} " . PMA_Util::backquote($_REQUEST['item_original_name']) . ";\n";
                    $result = $GLOBALS['dbi']->tryQuery($drop_routine);
                    if (!$result) {
                        $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_routine)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                    } else {
                        list($newErrors, $message) = PMA_RTN_createRoutine($routine_query, $create_routine, $privilegesBackup);
                        if (empty($newErrors)) {
                            $sql_query = $drop_routine . $sql_query;
                        } else {
                            $errors = array_merge($errors, $newErrors);
                        }
                        unset($newErrors);
                        if (null === $message) {
                            unset($message);
                        }
                    }
                }
            } else {
                // 'Add a new routine' mode
                $result = $GLOBALS['dbi']->tryQuery($routine_query);
                if (!$result) {
                    $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($routine_query)) . '<br /><br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
                } else {
                    $message = PMA_Message::success(__('Routine %1$s has been created.'));
                    $message->addParam(PMA_Util::backquote($_REQUEST['item_name']));
                    $sql_query = $routine_query;
                }
            }
        }
        if (count($errors)) {
            $message = PMA_Message::error(__('One or more errors have occurred while' . ' processing your request:'));
            $message->addString('<ul>');
            foreach ($errors as $string) {
                $message->addString('<li>' . $string . '</li>');
            }
            $message->addString('</ul>');
        }
        $output = PMA_Util::getMessage($message, $sql_query);
        if ($GLOBALS['is_ajax_request']) {
            $response = PMA_Response::getInstance();
            if ($message->isSuccess()) {
                $routines = $GLOBALS['dbi']->getRoutines($db, $_REQUEST['item_type'], $_REQUEST['item_name']);
                $routine = $routines[0];
                $response->addJSON('name', htmlspecialchars(mb_strtoupper($_REQUEST['item_name'])));
                $response->addJSON('new_row', PMA_RTN_getRowForList($routine));
                $response->addJSON('insert', !empty($routine));
                $response->addJSON('message', $output);
            } else {
                $response->isSuccess(false);
                $response->addJSON('message', $output);
            }
            exit;
        }
    }
    /**
     * Display a form used to add/edit a routine, if necessary
     */
    // FIXME: this must be simpler than that
    if (count($errors) || empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit']) && (!empty($_REQUEST['add_item']) || !empty($_REQUEST['edit_item']) || !empty($_REQUEST['routine_addparameter']) || !empty($_REQUEST['routine_removeparameter']) || !empty($_REQUEST['routine_changetype']))) {
        // Handle requests to add/remove parameters and changing routine type
        // This is necessary when JS is disabled
        $operation = '';
        if (!empty($_REQUEST['routine_addparameter'])) {
            $operation = 'add';
        } else {
            if (!empty($_REQUEST['routine_removeparameter'])) {
                $operation = 'remove';
            } else {
                if (!empty($_REQUEST['routine_changetype'])) {
                    $operation = 'change';
                }
            }
        }
        // Get the data for the form (if any)
        if (!empty($_REQUEST['add_item'])) {
            $title = PMA_RTE_getWord('add');
            $routine = PMA_RTN_getDataFromRequest();
            $mode = 'add';
        } else {
            if (!empty($_REQUEST['edit_item'])) {
                $title = __("Edit routine");
                if (!$operation && !empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit'])) {
                    $routine = PMA_RTN_getDataFromName($_REQUEST['item_name'], $_REQUEST['item_type']);
                    if ($routine !== false) {
                        $routine['item_original_name'] = $routine['item_name'];
                        $routine['item_original_type'] = $routine['item_type'];
                    }
                } else {
                    $routine = PMA_RTN_getDataFromRequest();
                }
                $mode = 'edit';
            }
        }
        if ($routine !== false) {
            // Show form
            $editor = PMA_RTN_getEditorForm($mode, $operation, $routine);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->addJSON('message', $editor);
                $response->addJSON('title', $title);
                $response->addJSON('param_template', PMA_RTN_getParameterRow());
                $response->addJSON('type', $routine['item_type']);
            } else {
                echo "\n\n<h2>{$title}</h2>\n\n{$editor}";
            }
            exit;
        } else {
            $message = __('Error in processing request:') . ' ';
            $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_Util::backquote($db)));
            $message = PMA_message::error($message);
            if ($GLOBALS['is_ajax_request']) {
                $response = PMA_Response::getInstance();
                $response->isSuccess(false);
                $response->addJSON('message', $message);
                exit;
            } else {
                $message->display();
            }
        }
    }
}