/**
 * Get HTML snippet for global privileges table with check boxes
 *
 * @param array $privTable       privileges table array
 * @param array $privTable_names names of the privilege tables
 *                               (Data, Structure, Administration)
 * @param array $row             first row from result or boolean false
 *
 * @return string $html_output
 */
function PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_names, $row)
{
    $html_output = '';
    foreach ($privTable as $i => $table) {
        $html_output .= '<fieldset>' . "\n" . '<legend>' . "\n" . '<input type="checkbox" class="sub_checkall_box"' . ' id="checkall_' . $privTable_names[$i] . '_priv"' . ' title="' . __('Check All') . '"/>' . '<label for="checkall_' . $privTable_names[$i] . '_priv">' . $privTable_names[$i] . '</label>' . "\n" . '</legend>' . "\n";
        foreach ($table as $priv) {
            $html_output .= '<div class="item">' . "\n" . '<input type="checkbox" class="checkall"' . ' name="' . $priv[0] . '_priv" ' . 'id="checkbox_' . $priv[0] . '_priv"' . ' value="Y" title="' . $priv[2] . '"' . (isset($row[$priv[0] . '_priv']) && $row[$priv[0] . '_priv'] == 'Y' ? ' checked="checked"' : '') . '/>' . "\n" . '<label for="checkbox_' . $priv[0] . '_priv">' . '<code>' . PMA_formatPrivilege($priv, true) . '</code></label>' . "\n" . '</div>' . "\n";
        }
        $html_output .= '</fieldset>' . "\n";
    }
    return $html_output;
}
/**
 * Extracts the privilege information of a priv table row
 *
 * @param array|null $row        the row
 * @param boolean    $enableHTML add <dfn> tag with tooltips
 * @param boolean    $tablePrivs whether row contains table privileges
 *
 * @global  resource $user_link the database connection
 *
 * @return array
 */
function PMA_extractPrivInfo($row = null, $enableHTML = false, $tablePrivs = false)
{
    if ($tablePrivs) {
        $grants = PMA_getTableGrantsArray();
    } else {
        $grants = PMA_getGrantsArray();
    }
    if (!is_null($row) && isset($row['Table_priv'])) {
        PMA_fillInTablePrivileges($row);
    }
    $privs = array();
    $allPrivileges = true;
    foreach ($grants as $current_grant) {
        if (!is_null($row) && isset($row[$current_grant[0]]) || is_null($row) && isset($GLOBALS[$current_grant[0]])) {
            if (!is_null($row) && $row[$current_grant[0]] == 'Y' || is_null($row) && ($GLOBALS[$current_grant[0]] == 'Y' || is_array($GLOBALS[$current_grant[0]]) && count($GLOBALS[$current_grant[0]]) == $_REQUEST['column_count'] && empty($GLOBALS[$current_grant[0] . '_none']))) {
                $privs[] = PMA_formatPrivilege($current_grant, $enableHTML);
            } elseif (!empty($GLOBALS[$current_grant[0]]) && is_array($GLOBALS[$current_grant[0]]) && empty($GLOBALS[$current_grant[0] . '_none'])) {
                // Required for proper escaping of ` (backtick) in a column name
                $grant_cols = array_map(function ($val) {
                    return Util::backquote($val);
                }, $GLOBALS[$current_grant[0]]);
                $privs[] = PMA_formatPrivilege($current_grant, $enableHTML) . ' (' . join(', ', $grant_cols) . ')';
            } else {
                $allPrivileges = false;
            }
        }
    }
    if (empty($privs)) {
        if ($enableHTML) {
            $privs[] = '<dfn title="' . __('No privileges.') . '">USAGE</dfn>';
        } else {
            $privs[] = 'USAGE';
        }
    } elseif ($allPrivileges && (!isset($_POST['grant_count']) || count($privs) == $_POST['grant_count'])) {
        if ($enableHTML) {
            $privs = array('<dfn title="' . __('Includes all privileges except GRANT.') . '">ALL PRIVILEGES</dfn>');
        } else {
            $privs = array('ALL PRIVILEGES');
        }
    }
    return $privs;
}