/** * Test for PMA_EVN_getQueryFromRequest * * @param array $request Request * @param string $query Query * @param array $num_err Error number * * @return void * * @dataProvider provider */ public function testgetQueryFromRequest($request, $query, $num_err) { global $_REQUEST, $errors; $errors = array(); PMA_EVN_setGlobals(); unset($_REQUEST); $_REQUEST = $request; $this->assertEquals($query, PMA_EVN_getQueryFromRequest()); $this->assertEquals($num_err, count($errors)); }
/** * Test for PMA_EVN_getQueryFromRequest * * @param array $request Request * @param string $query Query * @param array $num_err Error number * * @return void * * @dataProvider provider */ public function testgetQueryFromRequest($request, $query, $num_err) { global $_REQUEST, $errors; $errors = array(); PMA_EVN_setGlobals(); unset($_REQUEST); $_REQUEST = $request; $dbi = $this->getMockBuilder('PMA\\libraries\\DatabaseInterface')->disableOriginalConstructor()->getMock(); $dbi->expects($this->any())->method('escapeString')->will($this->returnArgument(0)); $GLOBALS['dbi'] = $dbi; $this->assertEquals($query, PMA_EVN_getQueryFromRequest()); $this->assertEquals($num_err, count($errors)); }
/** * Handles editor requests for adding or editing an item */ function PMA_EVN_handleEditor() { global $_REQUEST, $_POST, $errors, $db; if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) { $sql_query = ''; $item_query = PMA_EVN_getQueryFromRequest(); if (!count($errors)) { // set by PMA_RTN_getQueryFromRequest() // Execute the created query if (!empty($_REQUEST['editor_process_edit'])) { // Backup the old trigger, in case something goes wrong $create_item = PMA_DBI_get_definition($db, 'EVENT', $_REQUEST['item_original_name']); $drop_item = "DROP EVENT " . PMA_backquote($_REQUEST['item_original_name']) . ";\n"; $result = PMA_DBI_try_query($drop_item); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), $drop_item) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null); } else { $result = PMA_DBI_try_query($item_query); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), $item_query) . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null); // We dropped the old item, but were unable to create the new one // Try to restore the backup query $result = PMA_DBI_try_query($create_item); 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 event.') . '<br />' . __('The backed up query was:') . "\"{$create_item}\"" . '<br />' . __('MySQL said: ') . PMA_DBI_getError(null); } } else { $message = PMA_Message::success(__('Event %1$s has been modified.')); $message->addParam(PMA_backquote($_REQUEST['item_name'])); $sql_query = $drop_item . $item_query; } } } else { // 'Add a new item' mode $result = PMA_DBI_try_query($item_query); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), $item_query) . '<br /><br />' . __('MySQL said: ') . PMA_DBI_getError(null); } else { $message = PMA_Message::success(__('Event %1$s has been created.')); $message->addParam(PMA_backquote($_REQUEST['item_name'])); $sql_query = $item_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_showMessage($message, $sql_query); if ($GLOBALS['is_ajax_request']) { $extra_data = array(); if ($message->isSuccess()) { $columns = "`EVENT_NAME`, `EVENT_TYPE`, `STATUS`"; $where = "EVENT_SCHEMA='" . PMA_sqlAddSlashes($db) . "' " . "AND EVENT_NAME='" . PMA_sqlAddSlashes($_REQUEST['item_name']) . "'"; $query = "SELECT {$columns} FROM `INFORMATION_SCHEMA`.`EVENTS` WHERE {$where};"; $event = PMA_DBI_fetch_single_row($query); $extra_data['name'] = htmlspecialchars(strtoupper($_REQUEST['item_name'])); $extra_data['new_row'] = PMA_EVN_getRowForList($event); $extra_data['insert'] = !empty($event); $response = $output; } else { $response = $message; } PMA_ajaxResponse($response, $message->isSuccess(), $extra_data); } } /** * Display a form used to add/edit a trigger, 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['item_changetype']))) { // FIXME: this must be simpler than that $operation = ''; if (!empty($_REQUEST['item_changetype'])) { $operation = 'change'; } // Get the data for the form (if any) if (!empty($_REQUEST['add_item'])) { $title = PMA_RTE_getWord('add'); $item = PMA_EVN_getDataFromRequest(); $mode = 'add'; } else { if (!empty($_REQUEST['edit_item'])) { $title = __("Edit event"); if (!empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit']) && empty($_REQUEST['item_changetype'])) { $item = PMA_EVN_getDataFromName($_REQUEST['item_name']); if ($item !== false) { $item['item_original_name'] = $item['item_name']; } } else { $item = PMA_EVN_getDataFromRequest(); } $mode = 'edit'; } } if ($item !== false) { // Show form $editor = PMA_EVN_getEditorForm($mode, $operation, $item); if ($GLOBALS['is_ajax_request']) { $extra_data = array('title' => $title); PMA_ajaxResponse($editor, true, $extra_data); } else { echo "\n\n<h2>{$title}</h2>\n\n{$editor}"; unset($_POST); include './libraries/footer.inc.php'; } // exit; } else { $message = __('Error in processing request') . ' : '; $message .= sprintf(PMA_RTE_getWord('not_found'), htmlspecialchars(PMA_backquote($_REQUEST['item_name'])), htmlspecialchars(PMA_backquote($db))); $message = PMA_message::error($message); if ($GLOBALS['is_ajax_request']) { PMA_ajaxResponse($message, false); } else { $message->display(); } } } }
/** * Handles editor requests for adding or editing an item * * @return void */ function PMA_EVN_handleEditor() { global $_REQUEST, $_POST, $errors, $db; if (!empty($_REQUEST['editor_process_add']) || !empty($_REQUEST['editor_process_edit'])) { $sql_query = ''; $item_query = PMA_EVN_getQueryFromRequest(); if (!count($errors)) { // set by PMA_RTN_getQueryFromRequest() // Execute the created query if (!empty($_REQUEST['editor_process_edit'])) { // Backup the old trigger, in case something goes wrong $create_item = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $_REQUEST['item_original_name']); $drop_item = "DROP EVENT " . PMA\libraries\Util::backquote($_REQUEST['item_original_name']) . ";\n"; $result = $GLOBALS['dbi']->tryQuery($drop_item); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($drop_item)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null); } else { $result = $GLOBALS['dbi']->tryQuery($item_query); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($item_query)) . '<br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null); // We dropped the old item, but were unable to create // the new one. Try to restore the backup query $result = $GLOBALS['dbi']->tryQuery($create_item); $errors = checkResult($result, __('Sorry, we failed to restore the dropped event.'), $create_item, $errors); } else { $message = PMA\libraries\Message::success(__('Event %1$s has been modified.')); $message->addParam(PMA\libraries\Util::backquote($_REQUEST['item_name'])); $sql_query = $drop_item . $item_query; } } } else { // 'Add a new item' mode $result = $GLOBALS['dbi']->tryQuery($item_query); if (!$result) { $errors[] = sprintf(__('The following query has failed: "%s"'), htmlspecialchars($item_query)) . '<br /><br />' . __('MySQL said: ') . $GLOBALS['dbi']->getError(null); } else { $message = PMA\libraries\Message::success(__('Event %1$s has been created.')); $message->addParam(PMA\libraries\Util::backquote($_REQUEST['item_name'])); $sql_query = $item_query; } } } if (count($errors)) { $message = PMA\libraries\Message::error('<b>' . __('One or more errors have occurred while processing your request:') . '</b>'); $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']) { $response = PMA\libraries\Response::getInstance(); if ($message->isSuccess()) { $events = $GLOBALS['dbi']->getEvents($db, $_REQUEST['item_name']); $event = $events[0]; $response->addJSON('name', htmlspecialchars(mb_strtoupper($_REQUEST['item_name']))); $response->addJSON('new_row', PMA_EVN_getRowForList($event)); $response->addJSON('insert', !empty($event)); $response->addJSON('message', $output); } else { $response->setRequestStatus(false); $response->addJSON('message', $message); } exit; } } /** * Display a form used to add/edit a trigger, 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['item_changetype']))) { // FIXME: this must be simpler than that $operation = ''; if (!empty($_REQUEST['item_changetype'])) { $operation = 'change'; } // Get the data for the form (if any) if (!empty($_REQUEST['add_item'])) { $title = PMA_RTE_getWord('add'); $item = PMA_EVN_getDataFromRequest(); $mode = 'add'; } else { if (!empty($_REQUEST['edit_item'])) { $title = __("Edit event"); if (!empty($_REQUEST['item_name']) && empty($_REQUEST['editor_process_edit']) && empty($_REQUEST['item_changetype'])) { $item = PMA_EVN_getDataFromName($_REQUEST['item_name']); if ($item !== false) { $item['item_original_name'] = $item['item_name']; } } else { $item = PMA_EVN_getDataFromRequest(); } $mode = 'edit'; } } PMA_RTE_sendEditor('EVN', $mode, $item, $title, $db, $operation); } }