PHP Classes

File: sql/class.mysql.php

Recommend this page to a friend!
  Classes of giuseppe lucarelli   dbuser   sql/class.mysql.php   Download  
File: sql/class.mysql.php
Role: Class source
Content type: text/plain
Description: php class for mysql
Class: dbuser
Manage MySQL or PostgreSQL users using reconfig
Author: By
Last change: changed visibility
Date: 9 years ago
Size: 3,449 bytes
 

Contents

Class file image Download
<?php

class REConfigUser Extends REConfig
{
    function
BuildQuery(&$data) {
       
$privs='';
       
$grant='N';

        if(!
strcasecmp('yes',$data[5]['value'][0])) {
           
$privs='ALL PRIVILEGES';
        } else {
            for(
$i=6; $i < count(@$data); $i++) {
                if(!
strcasecmp('radio',@$data[$i]['type'])
                && !
strcasecmp('y',@$data[$i]['value'][0])) {
                    if(!
strcasecmp('grant',@$data[$i]['name'])) {
                       
$grant=@$data[$i]['value'][0];
                    } else {
                       
$privs.=','.@$data[$i]['name'];
                    }
                }
            }
           
$privs=ltrim($privs,',');
        }
        if(@
in_array('insert',$this->action)) {
           
$this->sources['building']['out']['query']=$this->SqlGrantPrivs(
               
$data[2]['value'][0],$data[0]['value'][0],$data[3]['value'][0],$data[4]['value'][0],$privs,$grant);
        } else if(@
in_array('update',$this->action)) {
           
$this->sources['building']['out']['query']=$this->SqlUpdatePrivs(
               
$data[2]['value'][0],$data[0]['value'][0],$data[3]['value'][0],$data[4]['value'][0],$privs,$grant);
        } else if(@
in_array('delete',$this->action)) {
           
$this->sources['building']['out']['query']=$this->SqlRevokePrivs(
                  
$data[2]['value'][0],$data[0]['value'][0],$data[3]['value'][0],$grant);
        }
    }
    function
SqlListPrivs($host,$user) {
        return
'SELECT Host,User,password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Grant_priv FROM user WHERE User=\''.$user.'\' AND Host=\''.$host.'\';';
    }
    function
SqlGrantPrivs($host,$db,$user,$password,$privs,$grant) {
       
$grantoption=($grant=='Y' ? ' WITH GRANT OPTION' : '');

        if(!
strcasecmp('all',$db)) $db='*';
       
$sql = array(
           
"CREATE USER '$user'@'$host' IDENTIFIED BY '$password';",
           
"GRANT $privs ON $db.* TO '$user'@'$host'$grantoption;",
           
'FLUSH PRIVILEGES;');
        return
$sql;
    }

    function
SqlUpdatePrivs($host,$db,$user,$password,$privs,$grant) {
       
$grantoption=($grant=='Y' ? ' WITH GRANT OPTION' : '');

        if(!
strcasecmp('all',$db)) $db='*';
       
$sql = array(
           
//'REVOKE ALL PRIVILEGES'.($grant=='Y'?', GRANT OPTION':'')." ON $db.* FROM '$user'@'$host';",
            //"REVOKE GRANT OPTION FROM '$user';", //.'\'@\''.$host.'\'',
           
"REVOKE ALL PRIVILEGES ON $db.* FROM '$user'@'$host';",
           
"UPDATE user SET Grant_priv='N' WHERE User='$user' AND Host='$host';",
           
"GRANT $privs ON $db.* TO '$user'@'$host'$grantoption;");
        if(
$password!='*')
           
$sql[] = "SET PASSWORD FOR '$user'@'$host' = PASSWORD('$password');";
       
$sql[] = 'FLUSH PRIVILEGES;';
        return
$sql;
    }

    function
SqlRevokePrivs($host,$db,$user,$grant) {
        if(!
strcasecmp('all',$db)) $db='*';
       
$grantoption=($grant=='Y' ? ', GRANT OPTION' : '');
       
$sql = array("REVOKE ALL PRIVILEGES ON $db.* FROM $user'@'$host';");
        if(
$grant=='Y')
           
$sql[] = "UPDATE user SET Grant_priv='N' WHERE User='$user' AND Host='$host';";
           
//"REVOKE GRANT OPTION FROM '$user'@'$host';";
       
$sql[] = "DROP USER '$user'@'$host';";
       
$sql[] = 'FLUSH PRIVILEGES;';
        return
$sql;
    }
};