View Single Post
  #7  
Old 5th November 2009, 18:24
osmoza osmoza is offline
Junior Member
 
Join Date: Nov 2009
Posts: 5
Thanks: 0
Thanked 4 Times in 3 Posts
Default

OK, done
Thanks for help Till.

The three methods you privided sets sys_datalog and that makes the whole magic!

As far as I see - all I had to do to write simple Ispconfig API is calling datalogSave() each time I change something with database (as you said). So...I've changed my Database::save() method to call this method and....that's it - everything works fine

I had to redefine some things in your db class though (the "global $app.." in methods - what's that for? some backward compatibility stuff?) - the code's included (with two or three calls to Database::query and some $modelObject->save() methods...- original versions in comments, so should work with 2-3 little changes).

I also included little script that creates client (for particular reseller), and sets website for that client - example of how and what for I'm using it for.

If you're not interested in the code - that's all I had to say - no futher reading needed
Thanks again
O.

THE CODE:

db_mysql.inc.php redefined as helper class:
PHP Code:
/* ISPCONFIG's EULA */
class Ispconfig
{
    
/** Returns the last mySQL insert_id() */
    
static public function insertID()
    {
        return 
mysql_insert_id();
    }


    
/** Escapes quotes in variable. mysql_real_escape_string() */
    
static public function quote($formfield)
    {
        return 
mysql_real_escape_string($formfield);
    }

    
/**
     *  Function to fill the datalog with a full differential record.
     * 
     * @param <type> $db_table  -   table name
     * @param <type> $action    -   INSERT/UPDATE/DELETE...
     * @param String $primary_field -   primary field's name (ex. client_id)
     * @param <type> $primary_id    -   primary fielr's value (ex. 21)
     * @param array $record_old    -   array representing record (ex. array('client_id'=>'21'))
     * @param array $record_new    -   array representing record (ex. array('client_id'=>'21'))
     * @return <Boolean>
     */
    
static public function datalogSave($db_table$action$primary_field$primary_id$record_old$record_new)
    {
        
// Insert backticks only for incomplete table names.
        
if(stristr($db_table,'.'))
        {
            
$escape '';
        } else
        {
            
$escape '`';
        }

        
$diffrec_full = array();
        
$diff_num 0;

        if(
is_array($record_old) && count($record_old) > 0)
        {
            foreach(
$record_old as $key => $val)
            {
                if(!isset(
$record_new[$key]) || $record_new[$key] != $val)
                {
                
// Record has changed
                    
$diffrec_full['old'][$key] = $val;
                    
$diffrec_full['new'][$key] = $record_new[$key];
                    
$diff_num++;
                } else
                {
                    
$diffrec_full['old'][$key] = $val;
                    
$diffrec_full['new'][$key] = $val;
                }
            }
        } elseif(
is_array($record_new))
        {
            foreach(
$record_new as $key => $val)
            {
                if(isset(
$record_new[$key]) && @$record_old[$key] != $val)
                {
                
// Record has changed
                    
$diffrec_full['new'][$key] = $val;
                    
$diffrec_full['old'][$key] = @$record_old[$key];
                    
$diff_num++;
                } else
                {
                    
$diffrec_full['new'][$key] = $val;
                    
$diffrec_full['old'][$key] = $val;
                }
            }
        }

        
// Insert the server_id, if the record has a server_id
        
$server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:SERVER_ID;
        if(isset(
$record_new["server_id"])) $server_id $record_new["server_id"];


        if(
$diff_num 0)
        {
            
$diffstr self::quote(serialize($diffrec_full));
            
$reseller Client::findByPk(CLIENTS_PARENT_CLIENT_ID); //get defined reseller instead of currently logged in user
            
$username self::quote($reseller['username']);
//            $username = self::quote($_SESSION["s"]["user"]["username"]);  //get currently loged in username
            
$dbidx $primary_field.":".$primary_id;

            if(
$action == 'INSERT'$action 'i';
            if(
$action == 'UPDATE'$action 'u';
            if(
$action == 'DELETE'$action 'd';
            
$sql "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$db_table."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')";
            
Database::query($sql);  //process query - similar to $app->db->query($sql);
        
}
        return 
true;
    }


Changed my Database::save() implementation so that it works with Ispconfig's sys_datalog:
PHP Code:
/* ...method definition.... */
//ISPCONFIG DALTA_LOG FIX
        
if($object->isNew())
        {
            
$old_rec = array(); //new object, so old_rec is empty
            
$query_result self::query($sql);
            
$index_value Ispconfig::insertID();
            
$object->fields[$object->getPrimaryKey()]['value'] = $index_value;  //XXX WANDER IF THIS WORKS :)
            
$new_recs self::query('SELECT * FROM '.$object->getTable().' WHERE '.$object->getPrimaryKey().' = \''.$index_value.'\';');
            
$new_rec $new_recs[0];
            if(
Ispconfig::datalogSave($object->getTable(), 'INSERT'$object->getPrimaryKey(), $index_value$old_rec$new_rec) == true)
            {
                return 
$query_result;
            }
            else
            {
                return 
false;
            }
        }
        else
        {
            
$old_recs self::query('SELECT * FROM '.$object->getTable().' WHERE '.$object->getPrimaryKey().'='.$object->fields[$object->getPrimaryKey()]['value'].';');
            
$old_rec $old_recs[0];
            
$query_result self::query($sql);
            
$new_recs self::query('SELECT * FROM '.$object->getTable().' WHERE '.$object->getPrimaryKey().'='.$object->fields[$object->getPrimaryKey()]['value'].';');
            
$new_rec $new_recs[0];
            if(
Ispconfig::datalogSave($object->getTable(), 'UPDATE'$object->getPrimaryKey(), $object->fields[$object->getPrimaryKey()]['value'], $old_rec$new_rec) == true)
            {
                return 
$query_result;
            }
            else
            {
                return 
false;
            }
        } 
And finally - simple use case (adds Client, creates required stuff like sys_user, sys_group, adds web_domain):
PHP Code:
<?php
include('/home/user/system/config/Constants.php');
include(
AUTOLOADER);
include(
DBINITER);

$paramNumber 4;
$errors = array();
$errors['no_required_params'] = <<<EOF
Provide this parameters:
email   -   users email - used for client's name nad contact's name.
username    - ispconfig's username
password    - username's password
domain  -   domain as xxx.yyyyy.tld

EOF;
if((
count($argv) -1) != $paramNumber)
{
    echo 
$errors['no_required_params'];
    exit;
}

//Filter, escape, validate provided parameters...then set them
$email $argv[1];
$username $argv[2];
$password $argv[3];
$domain $argv[4];

$company_name $email;
$contact_name $email;

//ClientsParent 
$clientsParent Client::findByPk(CLIENTS_PARENT_CLIENT_ID,true);    //client's parent object
$clientParentSysGroup SysGroup::findOneBy('client_id',CLIENTS_PARENT_CLIENT_ID,true);  //client's parent's sysgroup
$clientParentSysUser SysUser::findOneBy('client_id',CLIENTS_PARENT_CLIENT_ID,true);    //clients's pranent's sysuser

//new client
$client = new Client();
$client->fields['company_name']['value']   =   $company_name;
$client->fields['contact_name']['value'] =   $contact_name;
$client->fields['username']['value']   =   $username;
$client->fields['password']['value'] =   crypt($password);
$client->fields['parent_client_id']['value'] =   CLIENTS_PARENT_CLIENT_ID;

$status = ($client->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'CLIENT_OBJECT_SAVE: '.$status;
    exit;
}

//Creating sys_group for this client
$sysgroup = new SysGroup();
$sysgroup->fields['name']['value']  =   $client->fields['username']['value'];
$sysgroup->fields['client_id']['value'] = $client->fields['client_id']['value'];

$status = ($sysgroup->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'SYS_GROUP_OBJECT_SAVE: '.$status;
    exit;
}


//Creating sys_user with privided sysgroup's ID and client's ID
$sysuser = new SysUser();
$sysuser->fields['username']['value']    =   $username;
$sysuser->fields['passwort']['value']    =   md5($password);
$sysuser->fields['client_id']['value']   =   $client->fields['client_id']['value'];
$sysuser->fields['groups']['value'] = $sysgroup->fields['groupid']['value'];
$sysuser->fields['default_group']['value'] = $sysgroup->fields['groupid']['value'];

$status = ($sysuser->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'SYS_USER_OBJECT_SAVE: '.$status;
    exit;
}

//Updates client with sys_groupid and sys_userid fields with provided data
$client->fields['sys_userid']['value']   =   $clientParentSysUser->fields['userid']['value'];
$client->fields['sys_groupid']['value']  =   $clientParentSysGroup->fields['groupid']['value'];
$status = ($client->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'SYS_USER_OBJECT_SAVE: '.$status;
    exit;
}

//Updates Clients parent sys_user
$clientParentSysUser->fields['groups']['value']   .=  ','.$sysgroup->fields['groupid']['value'];
$status = ($clientParentSysUser->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'SYS_USER_PARENT_OBJECT_SAVE: '.$status;
    exit;
}

//WEB DOMAIN
$clientSysGroup SysGroup::findOneBy('client_id',$client->fields['client_id']['value']);

$webdomain = new WebDomain();
$webdomain->fields['sys_userid']['value'] = $clientParentSysUser->fields['userid']['value'];
$webdomain->fields['sys_groupid']['value'] = $clientSysGroup['groupid'];
$webdomain->fields['domain']['value'] = $domain;
$webdomain->fields['system_group']['value'] = 'client'.$client->fields['client_id']['value'];
$status = ($webdomain->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'WEB_DOMAIN_ADD: '.$status;
    exit;
}

$webdomain->fields['system_user']['value']  =   'web'.$webdomain->fields['domain_id']['value'];
$system_group $webdomain->fields['system_group']['value'];
$system_user $webdomain->fields['system_user']['value'];
$webdomain->fields['document_root']['value']  =   '/var/www/clients/'.$system_group.'/'.$system_user;
$status = ($webdomain->save() === true) ? 'SUCCESS' :   'FAILURE';
if(
$status == 'FAILURE')
{
    echo 
'WEB_DOMAIN_ADD_UPDATE: '.$status;
    exit;
}
echo 
'SUCCESS';
?>
Reply With Quote