예제 #1
0
/**
 * This function will generate the values that are required to complete
 * the "Edit routine" form given the name of a routine.
 *
 * @param string $name    The name of the routine.
 * @param string $type    Type of routine (ROUTINE|PROCEDURE)
 * @param bool   $all     Whether to return all data or just
 *                        the info about parameters.
 * @param bool   $control Where to use Controllink to query for
 *                        routine information
 *
 * @return array    Data necessary to create the routine editor.
 */
function PMA_RTN_getDataFromName($name, $type, $all = true, $control = false)
{
    global $db;
    $link = null;
    if ($control) {
        $link = $GLOBALS['controllink'];
    }
    $retval = array();
    // Build and execute the query
    $fields = "SPECIFIC_NAME, ROUTINE_TYPE, DTD_IDENTIFIER, " . "ROUTINE_DEFINITION, IS_DETERMINISTIC, SQL_DATA_ACCESS, " . "ROUTINE_COMMENT, SECURITY_TYPE";
    $where = "ROUTINE_SCHEMA " . PMA\libraries\Util::getCollateForIS() . "=" . "'" . $GLOBALS['dbi']->escapeString($db) . "' " . "AND SPECIFIC_NAME='" . $GLOBALS['dbi']->escapeString($name) . "'" . "AND ROUTINE_TYPE='" . $GLOBALS['dbi']->escapeString($type) . "'";
    $query = "SELECT {$fields} FROM INFORMATION_SCHEMA.ROUTINES WHERE {$where};";
    $routine = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $link);
    if (!$routine) {
        return false;
    }
    // Get required data
    $retval['item_name'] = $routine['SPECIFIC_NAME'];
    $retval['item_type'] = $routine['ROUTINE_TYPE'];
    $definition = $GLOBALS['dbi']->getDefinition($db, $routine['ROUTINE_TYPE'], $routine['SPECIFIC_NAME'], $link);
    if ($definition == NULL) {
        return false;
    }
    $parser = new SqlParser\Parser($definition);
    /**
     * @var CreateStatement $stmt
     */
    $stmt = $parser->statements[0];
    $params = SqlParser\Utils\Routine::getParameters($stmt);
    $retval['item_num_params'] = $params['num'];
    $retval['item_param_dir'] = $params['dir'];
    $retval['item_param_name'] = $params['name'];
    $retval['item_param_type'] = $params['type'];
    $retval['item_param_length'] = $params['length'];
    $retval['item_param_length_arr'] = $params['length_arr'];
    $retval['item_param_opts_num'] = $params['opts'];
    $retval['item_param_opts_text'] = $params['opts'];
    // Get extra data
    if (!$all) {
        return $retval;
    }
    if ($retval['item_type'] == 'FUNCTION') {
        $retval['item_type_toggle'] = 'PROCEDURE';
    } else {
        $retval['item_type_toggle'] = 'FUNCTION';
    }
    $retval['item_returntype'] = '';
    $retval['item_returnlength'] = '';
    $retval['item_returnopts_num'] = '';
    $retval['item_returnopts_text'] = '';
    if (!empty($routine['DTD_IDENTIFIER'])) {
        $options = array();
        foreach ($stmt->return->options->options as $opt) {
            $options[] = is_string($opt) ? $opt : $opt['value'];
        }
        $retval['item_returntype'] = $stmt->return->name;
        $retval['item_returnlength'] = implode(',', $stmt->return->parameters);
        $retval['item_returnopts_num'] = implode(' ', $options);
        $retval['item_returnopts_text'] = implode(' ', $options);
    }
    $retval['item_definer'] = $stmt->options->has('DEFINER');
    $retval['item_definition'] = $routine['ROUTINE_DEFINITION'];
    $retval['item_isdeterministic'] = '';
    if ($routine['IS_DETERMINISTIC'] == 'YES') {
        $retval['item_isdeterministic'] = " checked='checked'";
    }
    $retval['item_securitytype_definer'] = '';
    $retval['item_securitytype_invoker'] = '';
    if ($routine['SECURITY_TYPE'] == 'DEFINER') {
        $retval['item_securitytype_definer'] = " selected='selected'";
    } else {
        if ($routine['SECURITY_TYPE'] == 'INVOKER') {
            $retval['item_securitytype_invoker'] = " selected='selected'";
        }
    }
    $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS'];
    $retval['item_comment'] = $routine['ROUTINE_COMMENT'];
    return $retval;
}
/**
 * Creates the contents for a row in the list of routines
 *
 * @param array  $routine  An array of routine data
 * @param string $rowclass Empty or one of ['even'|'odd']
 *
 * @return string HTML code of a row for the list of routines
 */
function PMA_RTN_getRowForList($routine, $rowclass = '')
{
    global $ajax_class, $url_query, $db, $titles;
    $sql_drop = sprintf('DROP %s IF EXISTS %s', $routine['type'], PMA_Util::backquote($routine['name']));
    $type_link = "item_type={$routine['type']}";
    $retval = "        <tr class='{$rowclass}'>\n";
    $retval .= "            <td>\n";
    $retval .= '                <input type="checkbox"' . ' class="checkall" name="item_name[]"' . ' value="' . htmlspecialchars($routine['name']) . '" />';
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    $retval .= "                <span class='drop_sql hide'>" . htmlspecialchars($sql_drop) . "</span>\n";
    $retval .= "                <strong>\n";
    $retval .= "                    " . htmlspecialchars($routine['name']) . "\n";
    $retval .= "                </strong>\n";
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    // Since editing a procedure involved dropping and recreating, check also for
    // CREATE ROUTINE privilege to avoid lost procedures.
    if (PMA_Util::currentUserHasPrivilege('CREATE ROUTINE', $db)) {
        $retval .= '                <a ' . $ajax_class['edit'] . ' href="db_routines.php' . $url_query . '&amp;edit_item=1' . '&amp;item_name=' . urlencode($routine['name']) . '&amp;' . $type_link . '">' . $titles['Edit'] . "</a>\n";
    } else {
        $retval .= "                {$titles['NoEdit']}\n";
    }
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    // There is a problem with PMA_Util::currentUserHasPrivilege():
    // it does not detect all kinds of privileges, for example
    // a direct privilege on a specific routine. So, at this point,
    // we show the Execute link, hoping that the user has the correct rights.
    // Also, information_schema might be hiding the ROUTINE_DEFINITION
    // but a routine with no input parameters can be nonetheless executed.
    // Check if the routine has any input parameters. If it does,
    // we will show a dialog to get values for these parameters,
    // otherwise we can execute it directly.
    $parser = new SqlParser\Parser($GLOBALS['dbi']->getDefinition($db, $routine['type'], $routine['name']));
    /**
     * @var CreateStatement $stmt
     */
    $stmt = $parser->statements[0];
    $params = SqlParser\Utils\Routine::getParameters($stmt);
    if ($routine !== false) {
        if (PMA_Util::currentUserHasPrivilege('EXECUTE', $db)) {
            $execute_action = 'execute_routine';
            for ($i = 0; $i < $params['num']; $i++) {
                if ($routine['type'] == 'PROCEDURE' && $params['dir'][$i] == 'OUT') {
                    continue;
                }
                $execute_action = 'execute_dialog';
                break;
            }
            $retval .= '                <a ' . $ajax_class['exec'] . ' href="db_routines.php' . $url_query . '&amp;' . $execute_action . '=1' . '&amp;item_name=' . urlencode($routine['name']) . '&amp;' . $type_link . '">' . $titles['Execute'] . "</a>\n";
        } else {
            $retval .= "                {$titles['NoExecute']}\n";
        }
    }
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    $retval .= '                <a ' . $ajax_class['export'] . ' href="db_routines.php' . $url_query . '&amp;export_item=1' . '&amp;item_name=' . urlencode($routine['name']) . '&amp;' . $type_link . '">' . $titles['Export'] . "</a>\n";
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    $retval .= '                <a ' . $ajax_class['drop'] . ' href="sql.php' . $url_query . '&amp;sql_query=' . urlencode($sql_drop) . '&amp;goto=db_routines.php' . urlencode("?db={$db}") . '" >' . $titles['Drop'] . "</a>\n";
    $retval .= "            </td>\n";
    $retval .= "            <td>\n";
    $retval .= "                 {$routine['type']}\n";
    $retval .= "            </td>\n";
    $retval .= "            <td dir=\"ltr\">\n";
    $retval .= "                " . htmlspecialchars($routine['returns']) . "\n";
    $retval .= "            </td>\n";
    $retval .= "        </tr>\n";
    return $retval;
}