Thread: PgSQL and integration between 2 PHP programs and a Java program

PgSQL and integration between 2 PHP programs and a Java program

From
"Servers24 Network"
Date:
Hi,
 
I'm a newby so please bare with me :
 
I want to integrate 2 PHP scripts and a Java program that all have the capability of using PgSQL as their databases.
The point is that all the programs have their own DB and table structure.
What I want to do is to have a single DB for all of them and have them authenticate the users somehow that a registered user in PHP script can login to Java program too!
suppose that PHP1 scripts have a table called members with 2 fields "username" and "password". PHP2 has its own table called logins and have 2 fields "login_name" & "login_password". and my Java program have a table called profiles with "plogin" and "ppass". suppose that the password field in all the programs are MD5. Now I want to integrate them. I have read some PgSQL help books and it seems that there are many options that PgSQL offers for this, like inheritance, views and join. but I don't know exactly what to do. Can anyone help please?


--

Warm Regards,
Amir

Re: PgSQL and integration between 2 PHP programs and a Java program

From
Brew
Date:
Amir......

It seems to me you have greater issues than simply combining the three username and password tables!!!

What if there are existing users on the three different systems with the same user name?  Are they garanteed to be the same person?  Maybe so, if they have the same password, but what will you do if they have different passwords?

But beyond that, from what you say, you have to convert three different systems from whatever database programs you are using now to postgreSQL.  The current databases are doing more than just keeping track of users and passwords, right?  Possibly you plan to keep the existing database program and just use postgreSQL to log in?

And do you hope your users will log in once and go back and forth between your aplications without logging in again, or not?

At any rate, unless the applications are very simple, there is a lot more work to be done than simply joining the three password tables together!!!!!

brew

On 11/9/06, Servers24 Network <servers24@gmail.com> wrote:
Hi,
 
I'm a newby so please bare with me :
 
I want to integrate 2 PHP scripts and a Java program that all have the capability of using PgSQL as their databases.
The point is that all the programs have their own DB and table structure.
What I want to do is to have a single DB for all of them and have them authenticate the users somehow that a registered user in PHP script can login to Java program too!
suppose that PHP1 scripts have a table called members with 2 fields "username" and "password". PHP2 has its own table called logins and have 2 fields "login_name" & "login_password". and my Java program have a table called profiles with "plogin" and "ppass". suppose that the password field in all the programs are MD5. Now I want to integrate them. I have read some PgSQL help books and it seems that there are many options that PgSQL offers for this, like inheritance, views and join. but I don't know exactly what to do. Can anyone help please?

--

Strange Brew (brew@theMode.com)

Check out my Stock Option website http://www.callpix.com
and my Musicians Free Classified http://www.TheMode.com

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Servers24 Network"
Date:
Hi Brew,
 
Well this is a new project and we will import our users to this system after the project is done.
yes, the same usernames in different applications are the same user and they should be able to login with their single username and password in each application. From those applications I'm going to use, half has built-in PgSQL capabilities and half have MySQL as their databases.
I also have CAS ( Centeral Authentication Service ) in mind for SSO ( Single Sign On ). They also have a phpCAS class that authenticates the users against the CAS server and this way the user won't need to login again in each application as phpCAS will check their ticket to see if they're logged in or not.
Also I can do the project in another way, not using PgSQL and using their native databases. This way I will have duplication of data in each application as each application will store its username & password in its own database. But a modification should be done in all the applications to prevent the users from signing up and editing their profile inside the application, and then write a central signup & profile editing section where users can update their passwords for example, and this central profile editing will update the password stored in all databased ( suppose 10 databases ! ).
But I think this way is really silly :( As it will cause the server to handle too many queries!
This was the description of my project. If anyone have a better solution please let me know. I also have researched about using LDAP as the main DB server but seems it can only be used as the CAS and not the main DB for applications as it's main use is as a directory and can not be used in a high volume of changing data environment. Also very much modification should be done in each application to be able to talk to LDAP server!!
Also I have another question : do I need to edit MySQL queries very much in my PHP applications to interact with a PgSQL or not? Because most of the queries are the same and maybe very few should be changed ( as far as I know! ).

 
--

Warm Regards,
Amir
 

 
On 11/9/06, Brew <brew@themode.com> wrote:
Amir......

It seems to me you have greater issues than simply combining the three username and password tables!!!

What if there are existing users on the three different systems with the same user name?  Are they garanteed to be the same person?  Maybe so, if they have the same password, but what will you do if they have different passwords?

But beyond that, from what you say, you have to convert three different systems from whatever database programs you are using now to postgreSQL.  The current databases are doing more than just keeping track of users and passwords, right?  Possibly you plan to keep the existing database program and just use postgreSQL to log in?

And do you hope your users will log in once and go back and forth between your aplications without logging in again, or not?

At any rate, unless the applications are very simple, there is a lot more work to be done than simply joining the three password tables together!!!!!

brew


On 11/9/06, Servers24 Network <servers24@gmail.com > wrote:
Hi,
 
I'm a newby so please bare with me :
 
I want to integrate 2 PHP scripts and a Java program that all have the capability of using PgSQL as their databases.
The point is that all the programs have their own DB and table structure.
What I want to do is to have a single DB for all of them and have them authenticate the users somehow that a registered user in PHP script can login to Java program too!
suppose that PHP1 scripts have a table called members with 2 fields "username" and "password". PHP2 has its own table called logins and have 2 fields "login_name" & "login_password". and my Java program have a table called profiles with "plogin" and "ppass". suppose that the password field in all the programs are MD5. Now I want to integrate them. I have read some PgSQL help books and it seems that there are many options that PgSQL offers for this, like inheritance, views and join. but I don't know exactly what to do. Can anyone help please?

 
--

Strange Brew (brew@theMode.com)

Check out my Stock Option website http://www.callpix.com
and my Musicians Free Classified http://www.TheMode.com



Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Juan Miguel Paredes"
Date:
Hi, folks!

On 11/9/06, Servers24 Network <servers24@gmail.com> wrote:
> This was the description of my project. If anyone have a better solution
> please let me know. I also have researched about using LDAP as the main DB
> server but seems it can only be used as the CAS and not the main DB for
> applications as it's main use is as a directory and can not be used in a
> high volume of changing data environment. Also very much modification should
> be done in each application to be able to talk to LDAP server!!

PHP can take advantage of php4-ldap/php5-ldap to communicate with LDAP
server with no much trouble... Of course, that depends on your actual
class design, but with good OO design it should be quite simple to
replace/extend existing classes/methods for LDAP.  Same thing could be
said for Java.

> Also I have another question : do I need to edit MySQL queries very much in
> my PHP applications to interact with a PgSQL or not? Because most of the
> queries are the same and maybe very few should be changed ( as far as I
> know! ).

Again, if you're using some ORM or database abstraction layer
(PHP-ADODB, for example of the latter), it could be as simple as
changing your dsn configuration with maybe a very few noticeable
differences in sql-dialects.  If you're using just plain old php
database apis (e.g. mysql_connect, mysql_query), both mysql and pgsql
apis are very similar in php (there's mysql_connect for pg_connect,
mysql_query for pg_query and so on)...  Just watch out for things like
parameter ordering and so on (documentation is your friend), but of
course, database abstraction is highly recommended.

In any case, LDAP could be a good option for storing authentication
and authorization information in a centralized repository (most modern
frameworks, languages, technologies can integrate pretty well with
LDAP), and keep your application-specific data in whatever repository
seems appropiate.

Greetings!

Re: PgSQL and integration between 2 PHP programs and a Java program

From
Brew
Date:
Amir.......
 
I also have CAS ( Centeral Authentication Service ) in mind for SSO ( Single Sign On ). They also have a phpCAS class that authenticates the users against the CAS server and this way the user won't need to login again in each application as phpCAS will check their ticket to see if they're logged in or not.
 
It sounds like you've thought this through pretty well, more than you mentioned in your first email!
 
Combining the users table can be as simple as doing a dump of the tables in the other databases, editing the sql to make up for the slightly differing syntax of table declaration, etc. and inserting them into the new database.  The only snag is if you have duplicate user names, but then, if you make the username and unique index, it will create an error when you attempt to load in more than one user name and you can fix that some way.
 
>Also I have another question : do I need to edit MySQL queries very much in my PHP >applications to interact with a PgSQL or not? Because most of the queries are the same and >maybe very few should be changed ( as far as I know! ).
 
As Jaun mentioned, if you are using an abstraction layer already that would make it easy.  If I recall, the basic difference between a mysql select and a postgreSQL select is that in postgreSQL you need to manually check the row count and then call them out by number, while in mysql you do a fetch_next and it goes until the end, I think.
 
But that may have changed in later versions of PHP.
 
Of course, if there are triggers and transactions in the database that will complicate things, but it's not likely in standard mysql.
 
Others can probably give you advice I haven't thought of.  At any rate, ask on the list and you'll get advice!  (and with luck it will be good advice!)
 
later.....
 
brew
--

Strange Brew (brew@theMode.com)

Check out my Stock Option website http://www.callpix.com
and my Musicians Free Classified http://www.TheMode.com

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Servers24 Network"
Date:
Hi
 
First tI should thank Brew and Juan for their kind help.
Second it seems that the php applications doesn't use ADODB as here's a sample DB connection function of one of the PHP sctipts :
 
[code]
 
class DB {

 function DB(){

  $this->values = array();  

 }//DB

    function connect() {
  
        $this->conn_id = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS,1);
        mysql_select_db(SQL_DB,$this->conn_id);
        $version = $this->version();
       
        if($version >= 4.1){
         $encoding = extract_encoding("mysql");
         if($encoding){
          $sql_query = "SET NAMES '".$encoding."'";
          $this->execute($sql_query);
         }//if
        }//if

    }//connect
   
    function disconnect(){
     
     if($this->conn_id)
      @mysql_close($this->conn_id);
     
    }//disconnect
   
    function version(){
     
     $sql_query = "SELECT VERSION()";
     $version   = $this->single($sql_query);
     $version   = ereg_replace("[^0-9|\.]","",$version);
     return $version;
     
    }//version

    function execute($sql_query) {     
  
        if(!$this->conn_id){

            $this->connect();

        }//if
       
        preg_match_all("/\:[A-Za-z|0-9|_]{1,}\:/",$sql_query,$matches);
       
        $patterns = $matches[0];
       
        while(list(,$val) = each($patterns)){
         $key   = substr($val,1,strlen($val)-2);
         $value = stripslashes($this->values[$key]);
         $sql_query = hs_ereg_replace($val,mysql_escape_string($value),$sql_query);
        }//while
          
  reset($this->values);
  
        $this->sql_result = mysql_query($sql_query,$this->conn_id);

        if(!$this->sql_result && !hs_ereg("^LOCK TABLES",$sql_query) && $sql_query!="UNLOCK TABLES"){         
         die($sql_query.":".mysql_error($this->conn_id));
        }//if
       
        return $this->sql_result;

    }//execute

    function insert($sql_query){

     if((!$this->sql_result)||($sql_query != $this->sql_query)){

            $this->execute($sql_query);

        }//if

        $this->clean();
        return mysql_insert_id($this->conn_id);       

    }//insert
   
    function update($sql_query){

     if((!$this->sql_result)||($sql_query != $this->sql_query)){

            $this->execute($sql_query);

        }//if

        $this->clean();
        return mysql_affected_rows($this->conn_id);       

    }//update

    function fetch($sql_query){

        if((!$this->sql_result)||($sql_query != $this->sql_query)){

            $this->execute($sql_query);

        }//if

        $this->sql_query = $sql_query;
        $result = @mysql_fetch_object($this->sql_result);
        if(!$result)
         $this->clean();
         
        return $result;

    }//fetch

    function row($sql_query){

        if((!$this->sql_result)||($sql_query != $this->sql_query)){

            $this->execute($sql_query);

        }//if

        $this->sql_query = $sql_query;
        $result = @mysql_fetch_assoc($this->sql_result);
        if(!$result)
         $this->clean();
         
        return $result;

    }//row

    function result($sql_query){

     $ret_array = array();    

        while($this->res = $this->row($sql_query)){
         
            $ret_array[] = $this->res;

        }//while
        
        @mysql_free_result($this->res);      
        $this->clean();
        return $ret_array;

    }//result

    function single($sql_query){
     
        $this->execute($sql_query);
        $this->clean();
        return @mysql_result($this->sql_result,0);

    }//single
   
    function lock($table){
     
     $sql_query="LOCK TABLES `".$table."` WRITE";
     $this->execute($sql_query);
     
    }//lock
   
    function unlock(){
     
     $sql_query="UNLOCK TABLES";
     $this->execute($sql_query);
     
    }//unlock
   
    function clean(){
     
     $this->sql_query = "";
     
    }//clean

}//DB class

$DB = new DB;
 
[/code]
 
 
Well, this way I think I would need very much modification in source codes to make the PHP scripts talk to a PgSQL server :(
After all I have thought of 3 methods of integration. I would like everyone who have an experience to share it with me and vote for the best methd that comes to your mind :
 
1. change the PHP programs somehow that the registration and password modification ( and maybe profile editing ) won't be accessed. This way I will write a centralized registeration and editing script where users can register and update their profile. When a user is registered or updated all the new information including username, password, name, last_name, address an etc will be updated in all connected databased + LDAP server. This way for example user "sample" with pass "sample" will be stored in almost 10 databases + LDAP server. Then each application will read from its own database ( MySQL, PgSQL or LDAP ) and authenticates the user. Also CAS can be integrated using LDAP as the main directory service and phpCAS or JavaCas as the client ticketing system.
Pros : no change in any PHP or Java applications, but just integration of CAS for authentication. Centeral user managemet like what Yahoo offers ( Account Setting in Yahoo ). Seprate databased for each application that maybe increases security.
Cons : Huge amount of data duplication as each username. password, address and other related fields will be the same in many databases! , High database loads due to multiple UPDATE of several tables in different DBs.
 
2. Change all the applications to use PgSQL and share the same database.
Pros : No data duplication, fast database access ( no high loads like method no.1 )
Cons : Heavy source code editing
 
 
3. Change all the applications to use LDAP.
Pros : No data duplication, fast database access ( no high loads like method no.1 )
Cons : Heavy source code editing, I'm not sure if LDAP can handle this much insert & update queries as it's mostly a directory service and is not usefull to be used as a dabatase for other informations like profiles and etc.
 
 
--

Warm Regards,
Amir

Re: PgSQL and integration between 2 PHP programs and a Java program

From
Brew
Date:
Amir.....
 
After all I have thought of 3 methods of integration. I would like everyone who have an experience to share it with me and vote for the best methd that comes to your mind :
 
I vote for number 1. 
 
Unless you have some other reason to do a major rewrite, it seems the easiest to share the username and passwords across applications.  It's nice to make things perfect, but sometimes you have to look at how much effort versus the return.
 
> Cons : Huge amount of data duplication as each username. password, address and other
> related fields will be the same in many databases! , High database loads due to multiple
> UPDATE of several tables in different DBs.
 
Yes, but disk space is cheaper than the labor, and time, of all that rewriting.  And further, except for the added CAS table, it's no more than the space you are already using.  No wait, that's not so, each table will have ALL the users now, not just a subset.  Oh, well.
 
As far as updating, that only happens when a user makes a change, or maybe once when he logs in.  And if it's too slow you can find a way to do it in the background, let the user set it in the CAS and be done, then find another way to propagate the changes to the other database tables.  A trigger (maybe, not sure) or some recurring job that finds data that hasn't been propagated and does it, then sets a flag column for that user that it's been synced.
 
I'm sure others on the list can think of some more (maybe better) ways.
 
So I'm voting for number one.  On the other hand, I can think of times that I've cobbled something together and then realized I should have taken the time to do it right in the first place.  It's a hard call to make up front!!!
 
brew
 
--

Strange Brew (brew@theMode.com)

Check out my Stock Option website http://www.callpix.com
and my Musicians Free Classified http://www.TheMode.com

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Juan Miguel Paredes"
Date:
Hi, folks!

On 11/9/06, Brew <brew@themode.com> wrote:
> Amir.....
>
>
> >
> > After all I have thought of 3 methods of integration. I would like
> everyone who have an experience to share it with me and vote for the best
> methd that comes to your mind :
>
>
> I vote for number 1.
>
> Unless you have some other reason to do a major rewrite, it seems the
> easiest to share the username and passwords across applications.  It's nice
> to make things perfect, but sometimes you have to look at how much effort
> versus the return.
>
> > Cons : Huge amount of data duplication as each username. password, address
> and other
> > related fields will be the same in many databases! , High database loads
> due to multiple
> > UPDATE of several tables in different DBs.
>
> Yes, but disk space is cheaper than the labor, and time, of all that
> rewriting.  And further, except for the added CAS table, it's no more than
> the space you are already using.  No wait, that's not so, each table will
> have ALL the users now, not just a subset.  Oh, well.
>
> As far as updating, that only happens when a user makes a change, or maybe
> once when he logs in.  And if it's too slow you can find a way to do it in
> the background, let the user set it in the CAS and be done, then find
> another way to propagate the changes to the other database tables.  A
> trigger (maybe, not sure) or some recurring job that finds data that hasn't
> been propagated and does it, then sets a flag column for that user that it's
> been synced.
>
> I'm sure others on the list can think of some more (maybe better) ways.
>
> So I'm voting for number one.  On the other hand, I can think of times that
> I've cobbled something together and then realized I should have taken the
> time to do it right in the first place.  It's a hard call to make up
> front!!!
>
>
> brew
>
> --
>
> Strange Brew (brew@theMode.com)
>
> Check out my Stock Option website http://www.callpix.com
> and my Musicians Free Classified http://www.TheMode.com

Indeed, it's a hard call, but if you go for option 1 (keeping account
information in all your existing databases AND LDAP repository),
perhaps you could take some time and try something like PostgreSQL
DBI-Link (http://pgfoundry.org/projects/dbi-link/), in which
theoretically you could access any data source available for perl DBI
(and that includes of course pgsql, mysql, ldap, and many others) as
postgresql tables, so the overhead and complications of updating
several databases could be diminished.

Also, I'd dare to suggest a 4th option for consideration: centralize
account information only (perhaps in LDAP repository), keep
application-specific information in application-specific databases. Of
course, that would mean some code rewriting (but not for all
application queries, just for account information ones, and you could
take advantage of your CAS apis). I've seen this approach work in
several scenarios, but of course, there's no perfect solution for
every situation, and YMMV.

Thanks a lot to all you guys keeping up the good work in the
PostgreSQL community, and any other suggestions/experiences regarding
this case will surely be interesting.

Greetings!

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Servers24 Network"
Date:
Hi,
 
First I should thank Brew and Juan for their kind helps :)
 
Now I have good news for you two :
CAS supports authenticating the users from a non-LDAP DB server such as MySQL or PgSQL. This way there would be no need to implement a LDAP server for authentication as it can simply read from my main application database tables where it stores users information ( http://developer.ja-sig.org/projects/cas/multiproject/cas-server-jdbc/apidocs/index.html)
 
So now everything seems to be OK with the first method. Also with the help of DBI-Link I can have my main application write user information to all the connected DBs, tables and fields.
The main application will be Liferay Portal ( www.liferay.com ). It's a Java program and it can also be implemented using PgSQL database. I don't really know how this DBI-Link works, but the way Juan said, maybe I can have Liferay write user information to a PgSQL DB and have DBI-Link propagates the data (username, password, first_name, last_name, city, state, country, zipcode and ... ) inside Liferay's DB to all appropriate DBs.
Also I will disable ANY modification to the above mentioned fields in other applications so that a user need to change for e.g his name in Liferay, and then it will take effect in other applications as well.
So in this method, I will use a PgSQL server with 2 databases ( one for DBI-Link and one for Liferay ) and a MySQL server with maybe 10 databases for other applications. In this case, CAS will read authentication information from Liferay DB in PgSQL.
Any comments or ideas ?!


--

Warm Regards,
Amir

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Juan Miguel Paredes"
Date:
Hi, Amir!

On 11/10/06, Servers24 Network <servers24@gmail.com> wrote:
 I don't really
> know how this DBI-Link works, but the way Juan said, maybe I can have
> Liferay write user information to a PgSQL DB and have DBI-Link propagates
> the data (username, password, first_name, last_name, city, state, country,
> zipcode and ... ) inside Liferay's DB to all appropriate DBs.
> Also I will disable ANY modification to the above mentioned fields in other
> applications so that a user need to change for e.g his name in Liferay, and
> then it will take effect in other applications as well.
> So in this method, I will use a PgSQL server with 2 databases ( one for
> DBI-Link and one for Liferay ) and a MySQL server with maybe 10 databases
> for other applications. In this case, CAS will read authentication
> information from Liferay DB in PgSQL.
> Any comments or ideas ?!

For how DBI-Link works, maybe pgfoundry docs (like
http://pgfoundry.org/docman/view.php/1000045/48/IMPLEMENTATION.txt)
can give a good big picture. Reading the mentioned document, it seems
like there could be better performance when reading through DBI-Link
from other datasources than writing to several other datasources from
PostgreSQL "DBI-linked" tables (just guessing, there can be some other
factors involved, and, as always, experiences and benchmarks are
welcome). If this is true, maybe it would be better to have the other
10 DBs in PostgreSQL reading from the centralized datasource, but of
course that might not be practical in your case, if it means a lot of
code rewriting.  Anyway, I hope this helps and any feedback will be
followed with interest! ;)

Greetings!

Re: PgSQL and integration between 2 PHP programs and a Java program

From
"Servers24 Network"
Date:
Juan,
 
Well in the case of using PgSQL for all my application I will have to rewrite a lot of codes, so I think it's much better to use their native MySQL databases instead.
Also it seems to me that DBI-Link can not do what I want, as it seems, it updates ALL the fields of table in remotre database & not just the changed fields. If this is true, it will rewrite some user setting that is specific for an application that was previously updated by the application itself. So this way it will cause a roll back to these settings that user is made in a specific application. Although I'm not sure about this, I will ask it in their mailing list to see if it only updates the fields I want, or all the fields..
Anyhow, after all, I can go ahead with a simple transaction code in Liferay that triggers a series of connections to different MySQL DBs, update special user information tables and then close connections. Also I can use LOCK table method to lock the tables that Liferay is writing to, so that other applications can not write to the same table at that moment & show a "temporary out of service" message to the user. Although this will not occure much, as this incidence is very rare to happen.
Also I should ask about a feature in MySQL. Is it possible to LOCK a row in a table in MySQL? I mean for e.g if I lock the table "members" , then no member related functions can be done by ANY of my members. But if for example, I can LOCK the 1st row only that contains the data about my first user, then his account information will be updated and meanwhile others can use the system with no problem.
I'm not that familiar with this LOCK method. If anyone can help me and show me how to use it, I will appreciate.
 
 
 
 
--

Warm Regards,
Amir