/**
  * Test for PMA_getHtmlTableBodyForSpecificDbOrTablePrivs
  *
  * @return void
  */
 public function testPMAGetHtmlTableBodyForSpecificDbOrTablePrivss()
 {
     $privMap = null;
     $db = "pma_dbname";
     //$privMap = null
     $html = PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
     $this->assertContains(__('No user found.'), $html);
     //$privMap != null
     $privMap = array("user1" => array("hostname1" => array(array('Type' => 'g', 'Grant_priv' => 'Y'), array('Type' => 'd', 'Db' => "dbname", 'Grant_priv' => 'Y'), array('Type' => 't', 'Grant_priv' => 'N'))));
     $html = PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
     //validate 1: $current_privileges
     $current_privileges = $privMap["user1"]["hostname1"];
     $current_user = "******";
     $current_host = "hostname1";
     $this->assertContains(count($current_privileges) . "", $html);
     $this->assertContains(htmlspecialchars($current_user), $html);
     $this->assertContains(htmlspecialchars($current_host), $html);
     //validate 2: privileges[0]
     $this->assertContains(__('global'), $html);
     //validate 3: privileges[1]
     $current = $current_privileges[1];
     $this->assertContains(__('wildcard'), $html);
     $this->assertContains(htmlspecialchars($current['Db']), $html);
     //validate 4: privileges[2]
     $this->assertContains(__('table-specific'), $html);
 }
/**
 * Get the HTML for user form and check the privileges for a particular table.
 *
 * @param string $db    database name
 * @param string $table table name
 *
 * @return string $html_output
 */
function PMA_getHtmlForSpecificTablePrivileges($db, $table)
{
    $html_output = '';
    if ($GLOBALS['is_superuser']) {
        // check the privileges for a particular table.
        $html_output = '<form id="usersForm" action="server_privileges.php">';
        $html_output .= PMA_URL_getHiddenInputs($db, $table);
        $html_output .= '<fieldset>';
        $html_output .= '<legend>' . PMA_Util::getIcon('b_usrcheck.png') . sprintf(__('Users having access to "%s"'), '<a href="' . PMA_Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabTable'], 'table') . PMA_URL_getCommon(array('db' => $db, 'table' => $table)) . '">' . htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>') . '</legend>';
        $html_output .= '<table id="tablespecificuserrights" class="data">';
        $html_output .= PMA_getHtmlForPrivsTableHead();
        $privMap = PMA_getPrivMap($db);
        $sql_query = "SELECT `User`, `Host`, `Db`," . " 't' AS `Type`, `Table_name`, `Table_priv`" . " FROM `mysql`.`tables_priv`" . " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`" . "     AND '" . PMA_Util::sqlAddSlashes($table) . "' LIKE `Table_name`" . "     AND NOT (`Table_priv` = '' AND Column_priv = '')" . " ORDER BY `User` ASC, `Host` ASC, `Db` ASC, `Table_priv` ASC;";
        $res = $GLOBALS['dbi']->query($sql_query);
        PMA_mergePrivMapFromResult($privMap, $res);
        $html_output .= PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
        $html_output .= '</table>';
        $html_output .= '<div style="float:left;">';
        $html_output .= PMA_Util::getWithSelected($GLOBALS['pmaThemeImage'], $GLOBALS['text_dir'], "usersForm");
        $html_output .= PMA_Util::getButtonOrImage('submit_mult', 'mult_submit', 'submit_mult_export', __('Export'), 'b_tblexport.png', 'export');
        $html_output .= '</fieldset>';
        $html_output .= '</form>';
    } else {
        $html_output .= PMA_getHtmlForViewUsersError();
    }
    // Offer to create a new user for the current database
    $html_output .= PMA_getAddUserHtmlFieldset($db, $table);
    return $html_output;
}
/**
 * Get the HTML for user form and check the privileges for a particular table.
 *
 * @param string $db    database name
 * @param string $table table name
 *
 * @return string $html_output
 */
function PMA_getHtmlForSpecificTablePrivileges($db, $table)
{
    // check the privileges for a particular table.
    $html_output = '<form id="usersForm" action="server_privileges.php">';
    $html_output .= '<fieldset>';
    $html_output .= '<legend>' . PMA_Util::getIcon('b_usrcheck.png') . sprintf(__('Users having access to "%s"'), '<a href="' . $GLOBALS['cfg']['DefaultTabTable'] . PMA_URL_getCommon(array('db' => $db, 'table' => $table)) . '">' . htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>') . '</legend>';
    $html_output .= '<table id="tablespecificuserrights" class="data">';
    $html_output .= '<thead>' . '<tr><th>' . __('User') . '</th>' . '<th>' . __('Host') . '</th>' . '<th>' . __('Type') . '</th>' . '<th>' . __('Privileges') . '</th>' . '<th>' . __('Grant') . '</th>' . '<th>' . __('Action') . '</th>' . '</tr>' . '</thead>';
    list($listOfPrivs, $listOfComparedPrivs) = PMA_getListOfPrivilegesAndComparedPrivileges();
    $sql_query = "(" . " SELECT " . $listOfPrivs . ", '*' AS `Db`, 'g' AS `Type`" . " FROM `mysql`.`user`" . " WHERE NOT (" . $listOfComparedPrivs . ")" . ")" . " UNION " . "(" . " SELECT " . $listOfPrivs . ", `Db`, 'd' AS `Type`" . " FROM `mysql`.`db`" . " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`" . "     AND NOT (" . $listOfComparedPrivs . ")" . ")" . " ORDER BY `User` ASC, `Host` ASC, `Db` ASC;";
    $res = $GLOBALS['dbi']->query($sql_query);
    $privMap = array();
    while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
        $user = $row['User'];
        $host = $row['Host'];
        if (!isset($privMap[$user])) {
            $privMap[$user] = array();
        }
        if (!isset($privMap[$user][$host])) {
            $privMap[$user][$host] = array();
        }
        $privMap[$user][$host][] = $row;
    }
    $sql_query = "SELECT `User`, `Host`, `Db`," . " 't' AS `Type`, `Table_name`, `Table_priv`" . " FROM `mysql`.`tables_priv`" . " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`" . "     AND '" . PMA_Util::sqlAddSlashes($table) . "' LIKE `Table_name`" . "     AND NOT (`Table_priv` = '' AND Column_priv = '')" . " ORDER BY `User` ASC, `Host` ASC, `Db` ASC, `Table_priv` ASC;";
    $res = $GLOBALS['dbi']->query($sql_query);
    while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
        $user = $row['User'];
        $host = $row['Host'];
        if (!isset($privMap[$user])) {
            $privMap[$user] = array();
        }
        if (!isset($privMap[$user][$host])) {
            $privMap[$user][$host] = array();
        }
        $privMap[$user][$host][] = $row;
    }
    $html_output .= PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
    $html_output .= '</table>';
    $html_output .= '</fieldset>';
    $html_output .= '</form>';
    // Offer to create a new user for the current database
    $html_output .= '<fieldset id="fieldset_add_user">' . '<legend>' . _pgettext('Create new user', 'New') . '</legend>';
    $html_output .= '<a href="server_privileges.php' . PMA_URL_getCommon(array('adduser' => 1, 'dbname' => $db, 'tablename' => $table)) . '" rel="' . PMA_URL_getCommon(array('checkprivsdb' => $db, 'checkprivstable' => $table)) . '" class="ajax" name="table_specific">' . PMA_Util::getIcon('b_usradd.png') . __('Add user') . '</a>';
    $html_output .= '</fieldset>';
    return $html_output;
}