Skip to content
WJunction - Webmaster Forum

See real OOP (Snippet from Litewarez V2) Webmasters CP

Status
Not open for further replies.
PHP:
class xxxxxx{
	//Handle Registration
	function handle_registration(){
		$this->username 	= $this->registry->get('input')->POST['username'];
		$this->password 	= $this->registry->get('input')->POST['password'];
		$this->password2 	= $this->registry->get('input')->POST['password2'];
		$this->alias 		= $this->registry->get('input')->POST['alias'];
		$this->website 		= $this->registry->get('input')->POST['website'];
		$this->email 		= $this->registry->get('input')->POST['email'];
		$this->sitename 	= $this->registry->get('input')->POST['sitename'];
		$this->location 	= $this->registry->get('input')->POST['location'];
		
		//Check for isset() and strlen and regex char checks
		if(isset($this->username) || isset($this->password) || isset($this->password2) || isset($this->alias) || isset($this->website) || isset($this->email) ){
			//Form has been sent
			if(strlen($this->username)	== 0){$this->register_errors['no_username'] = true;}
			if(strlen($this->password)	== 0){$this->register_errors['no_password'] = true;}
			if(strlen($this->password2)	== 0){$this->register_errors['no_password2'] = true;}
			if(strlen($this->alias) 	== 0){$this->register_errors['no_alias']	= true;}
			if(strlen($this->website)	== 0){$this->register_errors['no_website']	= true;}
			if(strlen($this->email)		== 0){$this->register_errors['no_email']	= true;}
			if(strlen($this->sitename)	== 0){$this->register_errors['no_sitename']	= true;}
			
			//Min Len
			if(strlen($this->username)	!= 0 && strlen($this->username) < 6){$this->register_errors['username_too_short'] = true;}
			if(strlen($this->password)	!= 0 && strlen($this->password) < 6){$this->register_errors['password_too_short'] = true;}
			if(strlen($this->password2)	!= 0 && strlen($this->password2)< 6){$this->register_errors['password2_too_short'] = true;}
			if(strlen($this->alias) 	!= 0 && strlen($this->alias) 	< 4){$this->register_errors['alias_too_short'] = true;}
			if(strlen($this->sitename)	!= 0 && strlen($this->sitename) < 4){$this->register_errors['sitename_too_short'] = true;}
			
			//Max Len
			if(strlen($this->username)	> 32){$this->register_errors['username_too_long'] = true;}
			if(strlen($this->password)	> 32){$this->register_errors['password_too_long'] = true;}
			if(strlen($this->alias)		> 32){$this->register_errors['alias_too_long'] = true;}
			if(strlen($this->sitename)	> 32){$this->register_errors['sitename_too_long'] = true;}
			
			//Location can be defualtized
			$this->location = isset($this->location) ? $this->location : "GB"; // defualt to uk !
		
			if(count($this->register_errors) == 0){
				//everything is inputted and is the correct lenght
				//Check the webiste domain is valid!
				$this->website = str_replace('www.','',$this->website); // Remove www. for the below regex
				if(!strstr('http://',$this->website)){$this->website = 'http://' . $this->website;} //Adds http:// if not existsing
				if(!filter_var($this->website,FILTER_VALIDATE_URL)){$this->register_errors['invalid_website'] = true;}
				//Set email back to the domain no HTTP for database insert later
				$this->website = str_replace("http://","",$this->website);
				
				//Check the email regex! - Taken from phpBB get_preg_expression()
				if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)){$this->register_errors['email_invalid'] = true;}
				
				//Username and email validation
				if(!preg_match("/^[a-zA-Z0-9\_]+$/",$this->username)){$this->register_errors['username_invalid'] = true;}
				if(!preg_match("/^[a-zA-Z0-9\_]+$/",$this->password)){$this->register_errors['password_invalid'] = true;}
				if(!preg_match("/^[a-zA-Z0-9\_\s]+$/",$this->alias)){$this->register_errors['alias_invalid'] = true;}
				
				//Now check see if the passwords match.
				if(strcmp($this->password,$this->password2) != 0){$this->register_errors['passwords_no_match'] = true;}
				
				//Do  Location validate
				
				if(count($this->register_errors) == 0){
					//Proceed to the dabase recorde creation

					//Check if email or doman or username is in use !
					
					//EMAIL
					$this->sql = "SELECT webmaster_id FROM ".WEBMASTERS_TABLE." WHERE xxxx = '%s'";
					$this->sql = sprintf($this->sql,$this->email);
					$this->registry->get('database')->query($this->sql);
					if($this->registry->get('database')->num_rows() == 0){
						//ok so the email is not in use well check the username
						$this->sql = "SELECT xxxx FROM ".WEBMASTERS_TABLE." WHERE xxxxx = '%s'";
						$this->sql = sprintf($this->sql,$this->username);
						$this->registry->get('database')->query($this->sql);
						
						if($this->registry->get('database')->num_rows() == 0){
							//No user has got this username just have to check make sure the domain is available
							$this->sql = "SELECT xxxx FROM ".SITES_TABLE." WHERE xxxx = '%s'";
							$this->sql = sprintf($this->sql,$this->website);
							$this->registry->get('database')->query($this->sql);
							if($this->registry->get('database')->num_rows() == 0){
								//Insert new user and website
								/*
									Ready for registration
								*/
								//Buld the queryies
									//Need to insert the website first to get teh ID number
									$this->site_name_clean = preg_replace("/[^a-zA-Z0-9\_]+/","_",strtolower($this->sitename));
									$this->sql_site = "INSERT INTO ".SITES_TABLE." VALUES (NULL,'%s','%s','%s','%s',0,0,0)";
									$this->sql_site = sprintf($this->sql_site,$this->sitename,$this->site_name_clean,$this->website,$this->email);
									//Insert Sites
									$this->registry->get('database')->query($this->sql_site);
									$this->siteId = $this->registry->get('database')->getInsertId();
										
									//No we can add the user to the webmasters table !
									$this->sql_webmaster = "INSERT INTO ".WEBMASTERS_TABLE." VALUES(NULL,%d,'%s','%s','%s','%s','%s','%s')";
									$this->pass_enc = $this->saltString($this->password);
									$this->realIp = $this->registry->get('input')->getRealIp();
									$this->sql_webmaster = sprintf($this->sql_webmaster,$this->siteId,$this->alias,$this->username,$this->pass_enc,$this->email,$this->location,$this->realIp);
									//Insert webmasters
									$this->registry->get('database')->query($this->sql_webmaster);

									//Send heaers && add a session key for a registration note on the login page!
									$this->registry->get('session')->add('session_registered',time());
									$this->registry->get('output')->redirect(_WEBMASTERS_ROOT);
							}else{
								$this->register_errors['website_in_use'] = true;
							}
						}else{
							$this->register_errors['username_in_use'] = true;
						}
					}else{
						$this->register_errors['email_in_use'] = true;
					}
				}
			}
		}
	}
}


This is a snippet of the registration validation class for my new Webmasters CP and im posting this to show you at what OOP is all about !!!!!!
 

21 comments

Hmm as much as some of this is a good idea, lookup trigger_error and custom error handlers, much better really as they are built in to php (faster and means you can interprete php errors in the same code).

The only other issue I can see is the speed of the page, but thats your choice, another peice of OOP from my language class, Ill post it 2morro or later tonight.
 
i have custom error handler that logs all errors and sends emails with updates every 6 hours, also stops the error output to the users but not my IP, this way only i can see errors and uses will just get system offline message !.. thought ahead you see :P

and this is very throughout checking, its pretty fast to be honest, this and about 18 other classes including Db, User Tracker, Sucurity on all Inout requests GET,POST etc and stuff like Smarty system and custom config class loads in about 0.0280ms
 
hmm I have the same thing, tho I do show messages in a nice way. Only Its implemented with trigger_error :P

I need to add a load timer to my script so we can compare. Have you got filtering and validation in the whole WCDDL script?

I might have to implement something like your registry class, it does look nice to the eye really.
 
my system is all coded by myself lol, WCDDL is good but its just for average warez sites and thats not my idea, and the registry class is a piece of piss to do and the best thing is you just globalize in your constructs like

function __construct(){
global $registry;
$this->registry = $regsitry;
}

and i have an object file to load all objects like this

PHP:
<?php
if(!defined('IN_SITE')){die('Hacking Attempt!');}

//Error Handling Ssytem

//Instantate Registry objects
$registry = new registry();							//	Core Registry
//Instantate all other objects into the registry
$registry->put('database',		new $dbType());		//	Database
$registry->get('database')->connect();				//	Run the connection
$registry->put('session',		new session);		//	Session
$registry->put('input',			new input);			//	Input
$registry->put('output',		new output); 		//	Output handler
$registry->put('helpers',		new helpers);		//	Helpers
$registry->put('user_tracker',	new user_tracker);	//	users_online + ip tracking etc
$registry->put('smarty',		new Smarty);		//	Smarty Tempalte Engine
$registry->put('litewarez',		new Litewarez);		//	Litewarez Core
$registry->put('tagcloud',		new tagcloud);		//	Litewarez Tag Cloud Links
$registry->put('breadcrumb',	new breadcrumb);	//	Breadcrumbs
?>
 
Yeah I know, WCDDL isnt that great but its quite alot of work to make one from scratch and maintain it. I know the registry class is a piece of piss but I was thinking of making it even quicker and using php's magic methods :P

If I had the time I should really finnish of DDL2 (aka the new name of uDDL) but yeah, then again Im not even happy with that really, I made it a bit to complex in some areas.
 
yea i love the magic methods aswell lol, me and you should talk.. could do with another programmer for litewarez !.. seems like you know your shit more than most users around here.. nice to have sum on ya level ol
 
To add to that it doesn't support namespaces which I think is essential for writting good OOP based code. It also doesn't force the use of public, protected and private. And as said before it doesn't force OOP alltogether. Most will put a few methods and fields in a few classes while leaving a lot of global code and call their script OOP when infact the idea of OOP is that everything should be an object/class.

The best examples today of what "real" OOP is about are found in the ECMA-334 standard (AKA C#) and vb.net, both used in ASP.NET and the windows .NET platform. And let's not forget Java :).
 
well none of those languages are really that good in my oppionion. OOP can be overused alot, there are just some instances where simple is better. For example the users online counter litewarez posted, like 30 lines of OOP or 10 lines of procedural.
 
Why are they not good? I don't believe OOP can be overused, that's like saying global code can be overused. Putting every bit of functionality in an object makes perfect sense. Especially for bigger projects. And when you need something that behaves like global functions or properties sealed classes w/ static methods and fields does the job (better than global code).

Edit: if you're not comfortable with the idea of having something truly 100% OOP then I think you have a problem wrapping your head around that concept. When I went from PHP to C# I had the same issue.
 
Seems like they added namespace support indeed. It's been a few years sinds I did any real projects with PHP. At the time there were no namespaces. But they don't look much like the ones i'm used to.

I.E:
Code:
namespace MyAppName.SomeClasses
{
    public class a
    {
        // ...
    }
    
    public class b
    {
        // ...
    }
}

namespace MyAppName.MoreClasses
{
    public class c
    {
        // ...
    }
    
    public class d
    {
        // ...
    }
}
 
yea havent used namespaces that much myself but im sure its kinda like a directory layout

PHP:
namespace LiteWarez/Forntend/UserSystem;

class Frontend{ }
function LW_test()

For some reason this post wont accept backslashes so the forword slashes have to be backslashes !
 
I'm currently coding a (non-warez) script using OOP. Autoloading, scopes, singletons, static methods, etc.

Very clean and useful.
 
yes seeral of my casses of V2 have interface models etc. and all objects are placed into the regsitry for easy of use within other objects.

and goto love static methods where you dont have to instantate the object, i like to use static class types for error systems that run

PHP:
switch($leval){

    case 0:
         Error::strike(1011,"Level is to low");
    break;
    case 11:
         Error::strike(1011,"Level is too high")
    break;
}
 
Status
Not open for further replies.

About the author

L
Active Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom