0
<?php

/*
		+-----------------+------------------------------------------------------------+
		|  Class          | setup                                                      |
                |  Author         | Brian Onang\'o                                             |
		|  Version        | 1.0                                                        |
		|  Last Modified  | 10:16 AM 10/2/2010 GMT+3                                   |
		+-----------------+------------------------------------------------------------+
		|  This program is free software; you can redistribute it and/or               |
		|  modify it under the terms of the GNU General Public License                 |
		|  as published by the Free Software Foundation; either version 2              |
		|  of the License, or (at your option) any later version.                      |
		|                                                                              |
		|  This program is distributed in the hope that it will be useful,             |
		|  but WITHOUT ANY WARRANTY; without even the implied warranty of              |
		|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
		|  GNU General Public License for more details.                                |
		+------------------------------------------------------------------------------+
  		|Description::								       |  
		|A minute class for creating mysql tables without much hassle	               |
		+------------------------------------------------------------------------------+
*/

Class setup{

	var $tables = array();
	
/**************************************************************************************************
This function may or may not take any arguments. Passing arguments to here is equivalent to passing
the arguements to $this->setopt();
***************************************************************************************************/
	function __construct(){
		$args = func_get_args();
		if($args)$this->setopt($args);
	}
	
/******************************************************************************************************
This is the brain of our class. Arguments can be passed directly to this function, or they are passed
from $this->__construct();
The accepted arguments formats are:
# The first argument is forever(unless you modify the code) a string that denotes the tablename for 
which the options are being set.
#The available options are:
1. Field passed as
	a). setopt('tablename',field,'field|datatype') OR
	b). setopt('tablename',array(field,'field|datatype')) OR
	c). setopt('tablename','field',array('field|datatype','field1|datatype'))
2. mysql server passed as
	a). setopt('tablename','server':your mysql server')
In the same way that the server argument is passed, so is the mysql username(user),
password(pass),database(db) passed
And that is almost all the documentation there is for anyone who doesn't intend to modify this program.
*******************************************************************************************************/
	function setopt(){
	$num = func_num_args();
	$args = func_get_args();
	
	#if the arguments are passed from $this->__construct()	
	#args[0] is the array of arguments from __construct()
	#args[0][0] is the first arguement from __construct()
	if(isset($args[0][0])){
		if($num==1){
		#arguments passed to $this->__construct()
			
			$bibi = $args[0];
		}else{
		#arguments passed to $this->setopt();
		
			$bibi=$args;
		}
	}
		
	#if the first argument passed to $this->__construct() is not an array
	if(!is_array($bibi[0])){
	$count = count($bibi);
	
		#if only one string arguement is passed to $this->__construct()
		#$obj=new Setup('tablename');
		if($count==1){
			$tmp=array('db'=>NULL,
							'pass'=>NULL,
							'field'=>array(NULL));
							
			$this->tables[$bibi[0]] = $tmp;
		
		
		
		#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>datatype)
		#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>array(datatypes)

		#$obj=new setup('tablename',array('db'=>password'));
		}else{
			$temp=array();
			for($i=1;$i<$count;$i+=2){
				#obj=new setup('tablename','database','password','field','dataype');
				if(!is_Array($bibi[$i])){
					#obj=new setup('tablename','field','dataype');
					if($bibi[$i]=='field'){
						#obj=new setup('tablename','field','dataype');
						if(!is_Array($bibi[$i+1])){
							$temp['field'][] = $bibi[$i+1];
						#obj=new setup('tablename','field',array('dataype'));	
						}else{
							foreach($bibi[$i+1] as $bri=>$an){
							$temp['field'][] = $an;
							}
						}
					}else{
											
					$temp[$bibi[$i]] = $bibi[$i+1];
					}
#obj=new setup('tablename','database','password',array('field'=>'dataype'),'field','datatype');
#$obj=new setup('tablename','database','password',array('field','dataype','field','field1','field','field2'));

			
				}else{
					$cnt=count($bibi[$i]);
					for($j=0;$j<$cnt;$j+=2){
					if(isset($bibi[$i][$j])){
						$me=$bibi[$i][$j];
						if($me=='field'){
						$temp['field'][] = $bibi[$i][$j+1];
						}else{
						$temp[$bibi[$i][$j]] = $bibi[$i][$j+1];
						}
						unset($bibi[$i][$j]);
						unset($bibi[$i][$j+1]);
						
					}
					}
					
					foreach($bibi[$i] as $bri=>$an){#fields are not over written.
							if(is_Array($an)){
								foreach($an as $me=>$she){
									if($me=='field'){
									$temp['field'][] = $she;
									}else{
									$temp[$me] = $she;
									}
								}
							}else{
								if($bri=='field'){
								$temp['field'][] = $an;
								}else{
								$temp[$bri] = $an;
								}
							}
					}
					$i--;
				}
			}
			if(isset($this->tables[$bibi[0]])){
			$tmp=$this->__combine($this->tables[$bibi[0]],$temp);
			$this->tables[$bibi[0]]=$tmp;
			}else{
			$this->tables[$bibi[0]]=$temp;
			}
		}	
	}
	}
	
	#adding elements to an array=>combining two arrays
	function __combine($x,$y){
	$tmp=array();
	foreach($x as $bri=>$an){
	$tmp[$bri]=$an;
	}
	
	foreach($y as $bri=>$an){
	$tmp[$bri]=$an;
	}
	
	return $tmp;
	
	}
	
	#Executing our class
	function exec($tb=null){
	$tables=array();
	if(isset($tb)){$tables[$tb]=0;}else{$tables=$this->tables;}
	
	foreach($tables as $bibi=>$ana){
		try{
		$this->__con($bibi);
		$this->__setfields($bibi);
		$this->__create($bibi);
		}catch(Exception $e){
		echo $e->getmessage();
	}
	}	
	}
	
	#Creating a connection to mysql_server 
	function __con($tb){
	if(isset($this->tables[$tb])){$tables=$this->tables[$tb];}else{throw new Exception("You are trying to create a table that has no been defined");}
	if(isset($tables['server'])){$server=$tables['server'];}else {throw new exception("No server name given");}
	if(isset($tables['user'])){$user=$tables['user'];}else {throw new exception("No username given");}
	if(isset($tables['pass'])){$pass=$tables['pass'];}else {throw new exception("No password given");}
	if(isset($tables['db'])){$db=$tables['db'];}else {throw new exception("No database given");}
	
	$con = mysql_connect($server,$user,$pass);
	if(!$con){throw new Exception("Unable to connect to SQL server because: ".mysql_error());}
	mysql_select_db($db,$con)or die(mysql_error());
	}
	
	function __setfields($tb){
	$tables=$this->tables[$tb];
	if(isset($tables['field'])){$fields = $tables['field'];}else{throw new Exception("You haven't provided any fields");}
	
	foreach($fields as $bibi=>$ana){
	$this->__split($ana);
	}
	}
	
	#Extracting field and datatype from the fields passed f.e from ('fieldname| CHAR(128)')
	function __split($x){
	$parts = explode("|",$x);
	$this->fields[$parts[0]]=$parts[1];
	}
	
	function __create($tb){
	$drop= "drop table $tb";
	$create="";#state VARCHAR(128),
	$fields=array();
	
	foreach($this->fields as $bibi=>$ana){
	$fields[]="$bibi $ana";
	}
	foreach($fields as $bibi){
	$create .="$bibi,";
	}
	#remove last comma in $create. I haven't found as yet a more efficient way to do it.
	$create=substr($create,0,(strlen($create)-1));
	$create="Create table $tb($create)";
	echo $create;
	
	$result=mysql_query($drop);
	$result=mysql_query($create);	

	if ($result){mysql_close();Return "sucess - created table.";}
	else
	{
	$Error =  "Mysql ERROR: ".mysql_errno().".  ".mysql_error();
	mysql_close();
	Return "$Error";
	}
	}
}

#sample usage;
$server='your mysql server';
$user='your mysql server username';
$pass='probaly your girlfriend\'s\wife\'s name ';
$db='your database name';
$table='your table name';
$field=array('your unindexed fields');

$obj=new setup($table,'server',$server,'db',$db,'pass',$pass,'user',$user,'field',$field);	
$exec=$obj->exec();
echo $exec;
?>

Edited by Brianbc: n/a

<?php

/*
		+-----------------+------------------------------------------------------------+
		|  Class          | setup                                                      |
		|  Author         | Brian Onang\'o                                             |
		|  Last Modified  | 10:16 AM 10/2/2010                                         |
		+-----------------+------------------------------------------------------------+
		|  This program is free software; you can redistribute it and/or               |
		|  modify it under the terms of the GNU General Public License                 |
		|  as published by the Free Software Foundation; either version 2              |
		|  of the License, or (at your option) any later version.                      |
		|                                                                              |
		|  This program is distributed in the hope that it will be useful,             |
		|  but WITHOUT ANY WARRANTY; without even the implied warranty of              |
		|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
		|  GNU General Public License for more details.                                |
		|                                                                              |
		|  																			   |
		+------------------------------------------------------------------------------+
		|Description:														    	   |  
		|A minute class for creating mysql tables without much hassle				   |
		+------------------------------------------------------------------------------+
*/

Class setup{

	var $tables = array();
	
/**************************************************************************************************
This function may or may not take any arguments. Passing arguments to here is equivalent to passing
the arguements to $this->setopt();
***************************************************************************************************/
	function __construct(){
		$args = func_get_args();
		if($args)$this->setopt($args);
	}
	
/******************************************************************************************************
This is the brain of our class. Arguments can be passed directly to this function, or they are passed
from $this->__construct();
The accepted arguments formats are:
# The first argument is forever(unless you modify the code) a string that denotes the tablename for 
which the options are being set.
#The available options are:
1. Field passed as
	a). setopt('tablename',field,'field|datatype') OR
	b). setopt('tablename',array(field,'field|datatype')) OR
	c). setopt('tablename','field',array('field|datatype','field1|datatype'))
2. mysql server passed as
	a). setopt('tablename','server':your mysql server')
In the same way that the server argument is passed, so is the mysql username(user),
password(pass),database(db) passed
And that is almost all the documentation there is for anyone who doesn't intend to modify this program.
*******************************************************************************************************/
	function setopt(){
	$num = func_num_args();
	$args = func_get_args();
	
	#if the arguments are passed from $this->__construct()	
	#args[0] is the array of arguments from __construct()
	#args[0][0] is the first arguement from __construct()
	if(isset($args[0][0])){
		if($num==1){
		#arguments passed to $this->__construct()
			
			$bibi = $args[0];
		}else{
		#arguments passed to $this->setopt();
		
			$bibi=$args;
		}
	}
		
	#if the first argument passed to $this->__construct() is not an array
	if(!is_array($bibi[0])){
	$count = count($bibi);
	
		#if only one string arguement is passed to $this->__construct()
		#$obj=new Setup('tablename');
		if($count==1){
			$tmp=array('db'=>NULL,
							'pass'=>NULL,
							'field'=>array(NULL));
							
			$this->tables[$bibi[0]] = $tmp;
		
		
		
		#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>datatype)
		#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>array(datatypes)

		#$obj=new setup('tablename',array('db'=>password'));
		}else{
			$temp=array();
			for($i=1;$i<$count;$i+=2){
				#obj=new setup('tablename','database','password','field','dataype');
				if(!is_Array($bibi[$i])){
					#obj=new setup('tablename','field','dataype');
					if($bibi[$i]=='field'){
						#obj=new setup('tablename','field','dataype');
						if(!is_Array($bibi[$i+1])){
							$temp['field'][] = $bibi[$i+1];
						#obj=new setup('tablename','field',array('dataype'));	
						}else{
							foreach($bibi[$i+1] as $bri=>$an){
							$temp['field'][] = $an;
							}
						}
					}else{
											
					$temp[$bibi[$i]] = $bibi[$i+1];
					}
#obj=new setup('tablename','database','password',array('field'=>'dataype'),'field','datatype');
#$obj=new setup('tablename','database','password',array('field','dataype','field','field1','field','field2'));

			
				}else{
					$cnt=count($bibi[$i]);
					for($j=0;$j<$cnt;$j+=2){
					if(isset($bibi[$i][$j])){
						$me=$bibi[$i][$j];
						if($me=='field'){
						$temp['field'][] = $bibi[$i][$j+1];
						}else{
						$temp[$bibi[$i][$j]] = $bibi[$i][$j+1];
						}
						unset($bibi[$i][$j]);
						unset($bibi[$i][$j+1]);
						
					}
					}
					
					foreach($bibi[$i] as $bri=>$an){#fields are not over written.
							if(is_Array($an)){
								foreach($an as $me=>$she){
									if($me=='field'){
									$temp['field'][] = $she;
									}else{
									$temp[$me] = $she;
									}
								}
							}else{
								if($bri=='field'){
								$temp['field'][] = $an;
								}else{
								$temp[$bri] = $an;
								}
							}
					}
					$i--;
				}
			}
			if(isset($this->tables[$bibi[0]])){
			$tmp=$this->__combine($this->tables[$bibi[0]],$temp);
			$this->tables[$bibi[0]]=$tmp;
			}else{
			$this->tables[$bibi[0]]=$temp;
			}
		}	
	}
	}
	
	#adding elements to an array=>combining two arrays
	function __combine($x,$y){
	$tmp=array();
	foreach($x as $bri=>$an){
	$tmp[$bri]=$an;
	}
	
	foreach($y as $bri=>$an){
	$tmp[$bri]=$an;
	}
	
	return $tmp;
	
	}
	
	#Executing our class
	function exec($tb=null){
	$tables=array();
	if(isset($tb)){$tables[$tb]=0;}else{$tables=$this->tables;}
	
	foreach($tables as $bibi=>$ana){
		try{
		$this->__con($bibi);
		$this->__setfields($bibi);
		$this->__create($bibi);
		}catch(Exception $e){
		echo $e->getmessage();
	}
	}	
	}
	
	#Creating a connection to mysql_server 
	function __con($tb){
	if(isset($this->tables[$tb])){$tables=$this->tables[$tb];}else{throw new Exception("You are trying to create a table that has no been defined");}
	if(isset($tables['server'])){$server=$tables['server'];}else {throw new exception("No server name given");}
	if(isset($tables['user'])){$user=$tables['user'];}else {throw new exception("No username given");}
	if(isset($tables['pass'])){$pass=$tables['pass'];}else {throw new exception("No password given");}
	if(isset($tables['db'])){$db=$tables['db'];}else {throw new exception("No database given");}
	
	$con = mysql_connect($server,$user,$pass);
	if(!$con){throw new Exception("Unable to connect to SQL server because: ".mysql_error());}
	mysql_select_db($db,$con)or die(mysql_error());
	}
	
	function __setfields($tb){
	$tables=$this->tables[$tb];
	if(isset($tables['field'])){$fields = $tables['field'];}else{throw new Exception("You haven't provided any fields");}
	
	foreach($fields as $bibi=>$ana){
	$this->__split($ana);
	}
	}
	
	#Extracting field and datatype from the fields passed f.e from ('fieldname| CHAR(128)')
	function __split($x){
	$parts = explode("|",$x);
	$this->fields[$parts[0]]=$parts[1];
	}
	
	function __create($tb){
	$drop= "drop table $tb";
	$create="";#state VARCHAR(128),
	$fields=array();
	
	foreach($this->fields as $bibi=>$ana){
	$fields[]="$bibi $ana";
	}
	foreach($fields as $bibi){
	$create .="$bibi,";
	}
	#remove last comma in $create. I haven't found as yet a more efficient way to do it.
	$create=substr($create,0,(strlen($create)-1));
	$create="Create table $tb($create)";
	echo $create;
	
	$result=mysql_query($drop);
	$result=mysql_query($create);	

	if ($result){mysql_close();Return "sucess - created table.";}
	else
	{
	$Error =  "Mysql ERROR: ".mysql_errno().".  ".mysql_error();
	mysql_close();
	Return "$Error";
	}
	}
}

#sample usage;
$server='your mysql server';
$user='your mysql server username';
$pass='probaly your girlfriend\'s\wife\'s name ';
$db='your database name';
$table='your table name';
$field=array('your unindexed fields');

$obj=new setup('tablename','server',$server,'db',$db,'pass',$pass,'user',$user,'field',$field);	
$exec=$obj->exec();
echo $exec;
?>
2
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by Brianbc
0

Smantscheff, I need. I'm designing a site where every registered user has their credentials in separate table. And I think it easier to have a class create those over 500 tables. And version 1.1 should be ready in some 14 hours. With version 1.1, you can do anything you would ever want to do with any database in a Mysql server.

0

500 tables of the same structure - I would say this is a design flaw. You will of course have good reason for it, presumably safety arguments. Would you mind telling us about them?

0

I don't mind, so I will try telling you about it. It is not the best of design's, but I haven't thought of any flaw in it as yet. You are welcome to tell me any improvements that I may need to make.

And it's all about safety and ...privacy.

I'm am thinking about a final product where everyone will have their own subdomain on my site. They, and not the webmaster, then will control everything on those subdomains of theirs. My website will then be just an interface to the data on their subdomains.

Now to log in, this interface makes a connection to a database in subdomain x or y or any of those 500 subdomains(assuming I have only 500 users of my site).

This system, in my view, will be govt. proof. And hacker-proof.

For now however, it is just the tables that are different for every user.(The tables are of the same design.) Each has the user password to the site and any other data we may need to store. My arrguement for this design is that if someone somehow happens to gain reading permission to any of the tables, then the damage caused will not be much. They will only spoof data for a single user!

And that brings me to my next question. It's not homework. How are subdomains created in php?

0

How are subdomains created in php?

The are not. You create subdomains in your webserver which has to be aware of them and configured to direct them to the correct directories scripts. Apache is capable of wildcard subdomains, so all subdomains can point to the same directory.
What you can do in PHP is to evaluate the server name ($_SERVER) which contains the subdomain name. If all your subdomains point to the same directory and script, the script can extract the subdomain from the server name variable and act on it.

I don't see how your design adds much to privacy. A hacker who is capable of hacking one account on a username/password level will have the rights of the hacked account. If your system is well designed, then only this account can be harmed.
A hacker who is capable of hacking a whole table will be capable of hacking the other tables, too. So there is no additional safety in them.
And for protection against government you need a complete encryption. Encrypt your server's hard disk and secure the connections via a VPN. Otherwise a government agency will be capable of laying hands on the hardware, and everything which is not encrypted will be compromised.

Edited by smantscheff: n/a

0
# A hacker who is capable of hacking a whole table will be capable of hacking the other tables, too

This is not the kind of hacking I'm not referring to. Not a username/password level hacking. I'm talking about what they recently did to sourceforce. http://sourceforge.net/blog/sourceforge-net-attack/ . If someone somehow manages to gain access to an entire table/db.

#Otherwise a government agency will be capable of laying hands on the hardware, and everything which is not encrypted will be compromised.

On this one also, it's more of govt policies than the fear of those stooges. If the user, and not the webmaster, has control over his data,(and it pathways between servers) then any arm of the govt. can not order us to give them any data that the user has entrusted us with to forever keep safe and secret.

#Encrypt your server's hard disk and secure the connections via a VPN

I have never thought of that. So much for the advice.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.