Whats your style de codage?
- UPSGuy
- Lurker ಠ_ಠ


- Inscription: Juil 25, 2005
- Messages: 2735
- Loc: Nashville, TN
- Status: Offline
Si vous êtes le type plein commentaire? Pas de commentaires? tout à onglets en place? Condensée que possible?
Quelles sont ces règles que vous ne pouvez pas vivre sans?
Quels sont ces bêtes noires qui vous haïssent juste de trouver dans le code de quelqu'un d'autre?
Im curieux de voir où le populus général Ozzu Falls
Quelles sont ces règles que vous ne pouvez pas vivre sans?
Quels sont ces bêtes noires qui vous haïssent juste de trouver dans le code de quelqu'un d'autre?
Im curieux de voir où le populus général Ozzu Falls
I'd love to change the world, but they won't give me the source code.
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Septembre 28th, 2009, 11:22 am
- joebert
- Sledgehammer


- Inscription: Fév 10, 2004
- Messages: 13455
- Loc: Florida
- Status: Offline
J'aime accolades sur leurs propres lignes.
I like! (ne pas) avoir des espaces de chaque côté.
Je n'aime pas les classes singleton qui vérifient les instances d'eux-mêmes au sein d'un constructeur.
Je n'aime pas basés dans l'espace d'indentation, j'aime onglets.
J'aime bien les arguments pour des choses comme printf d'être sur leurs propres lignes et en retrait. J'aime bien des observations avec l'argument numéro placé périodiquement.
Je n'aime pas quand les numéros symboliques ne sont disponibles pour les fonctions de style printf et elles ne sont utilisées, peu importe la façon dont il existe peu d'arguments, ou si tous les arguments sont réutilisés dans le masque. Je ne me souviens pas combien de fois Ive a eu à revenir en arrière et retravailler un printf Lorsqu'un argument est ajoutée plus tard.
Je n'aime pas les commentaires qui ressemblent à de la documentation. Si le commentaire commence à prendre jusqu'à la moitié des éditeurs écran ce n'est pas un commentaire freakin, créez une page de documentation damn et remplacer whats dans le code avec un lien vers cette page déjà. Je ne veux pas entendre la merde pourtant de devoir accueillir une ou l'autre, vous n'avez pas besoin d'hébergement d'inclure un dossier Documents dans le paquet que vous pouvez lier à.
Je n'aime pas les multiples commentaires d'une ligne comme celle-ci quand des theres un multi-multi-syntaxe réelle commentaire disponible en ligne.
Je n'aime pas les commentaires qui ressemblent à de la documentation. Oui, cela est utile de mentionner deux fois.
Je n'aime pas l'espace entre la clôture pare nothesis. Je peux traiter avec des espaces après l'appel, la première fonction.
J'aime bien les guillemets quand il le faut.
I like this
Je n'aime pas cette
Mais je n'aime ce
Thats une question de comment fonctionne mon syntaxe surligneur.
Thats tout ce qui vient à l'esprit pour le moment.
Code: [ Select ]
if(...)
{
}
{
}
- if(...)
- {
- }
I like! (ne pas) avoir des espaces de chaque côté.
Code: [ Select ]
if( ! something)
if(something)
if(something)
- if( ! something)
- if(something)
Je n'aime pas les classes singleton qui vérifient les instances d'eux-mêmes au sein d'un constructeur.
Je n'aime pas basés dans l'espace d'indentation, j'aime onglets.
J'aime bien les arguments pour des choses comme printf d'être sur leurs propres lignes et en retrait. J'aime bien des observations avec l'argument numéro placé périodiquement.
Code: [ Select ]
printf(
'This %2$s how %1$s like it',
who,
whether // %2$s
);
'This %2$s how %1$s like it',
who,
whether // %2$s
);
- printf(
- 'This %2$s how %1$s like it',
- who,
- whether // %2$s
- );
Je n'aime pas quand les numéros symboliques ne sont disponibles pour les fonctions de style printf et elles ne sont utilisées, peu importe la façon dont il existe peu d'arguments, ou si tous les arguments sont réutilisés dans le masque. Je ne me souviens pas combien de fois Ive a eu à revenir en arrière et retravailler un printf Lorsqu'un argument est ajoutée plus tard.
Je n'aime pas les commentaires qui ressemblent à de la documentation. Si le commentaire commence à prendre jusqu'à la moitié des éditeurs écran ce n'est pas un commentaire freakin, créez une page de documentation damn et remplacer whats dans le code avec un lien vers cette page déjà. Je ne veux pas entendre la merde pourtant de devoir accueillir une ou l'autre, vous n'avez pas besoin d'hébergement d'inclure un dossier Documents dans le paquet que vous pouvez lier à.
Je n'aime pas les multiples commentaires d'une ligne comme celle-ci quand des theres un multi-multi-syntaxe réelle commentaire disponible en ligne.
Code: [ Select ]
// one
// two
// three
// two
// three
- // one
- // two
- // three
Je n'aime pas les commentaires qui ressemblent à de la documentation. Oui, cela est utile de mentionner deux fois.
Je n'aime pas l'espace entre la clôture pare nothesis. Je peux traiter avec des espaces après l'appel, la première fonction.
Code: [ Select ]
if(something( something2( something2())))
J'aime bien les guillemets quand il le faut.
Code: [ Select ]
$var = "My name is $name";
$var = 'My name is ' . $name;
$var = 'My name is ' . $name;
- $var = "My name is $name";
- $var = 'My name is ' . $name;
I like this
Code: [ Select ]
$var = "My name is {$obj->name}";
Je n'aime pas cette
Code: [ Select ]
$var = "My name is $obj->name";
Mais je n'aime ce
Code: [ Select ]
$var = "My name is $name";
Thats une question de comment fonctionne mon syntaxe surligneur.
Thats tout ce qui vient à l'esprit pour le moment.
Strong with this one, the sudo is.
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8211
- Loc: USA
- Status: Offline
Il serait peut-être plus facile si je vous montrer un exemple de mon morceau de codage et de vous montrer comment je fais:
Je suppose que je pourrais essayer de l'expliquer rapidement aussi bien...
Im un maniaque commentaire...Je commente à peu près tous seule ligne de code pour expliquer ce que je fais. J'ajoute en peu de rappels et des notes, des crédits si Im le rendant public, le commentaire de tout premier est le nom de fichier et fichier de description.
J'aime utiliser des variables et des noms de classe qui décrit ce qu'ils tiennent / fonction autant que possible avec le moins de mots possible...
J'ai tout tiret et tout garder en conformité avec ce qu'ils sont associés à...thats le mieux que j'ai pu l'expliquer.
Im un perfectionniste, donc si je copier une classe de quelqu'un, peu importe comment elle est grande, je commente tout et tout, ils tiret comme je le souhaite avant que je le télécharge sur mon site
Je hais aussi:
\
Qu'est-ce que j'aime, c'est:
J'aime utiliser des accolades à l'intérieur de guillemets pour accéder aux objets et les tableaux. J'aime bien utiliser des guillemets, point final.
PHP Code: [ Select ]
<?php
/*
* install.php
* A global installation class created to be used with any kind of application.
*
* Before doing anything with this class, I recommend you read the documentation to figure out
* what you are doing. That's so you won't accidentally mess something up.
*/
class install {
/*
* The stages that the script would have in it's overall installation process.
* This is to ensure that the user wouldn't just put anything random into the
* url and try to do something. Also for automation.
*/
var $stages = array('stage_1','stage_2','stage_3');
/*
* This determines if we would have anything to do with SQL
*/
var $use_sql = true;
/*
* This would store the SQL information once/if recieved (For the stage submitted on... later on, uses $_SESSION)
*/
var $sql = array('HOST' => null, // MySQL Host
'USER' => null, // MySQL Username
'PASS' => null, // MySQL Passowrd
'DBASE' => null, // MySQL Database Name
'TBL_PRE' => null); // MySQL Table Prefix
/*
* Determines if a global file would be created at the end.
*
* The global file would consist of the MySQL host, username, password and the database name
*/
var $create_global = true;
/*
* Determines the location that the configuration file would be saved to (Make sure it exists)
*/
var $gl_loc = 'includes/';
/*
* Determines the name for the global file
*/
var $globals = 'global';
/*
* Determines the global file type (also used as the extension for the global file)
*
* Possible types:
* XML, PHP
*
* NOTE: If you know PHP it would be easy for you to add more types here
*/
var $global_type = 'XML';
/*
* Set to true if you are installing and want to check if the application is already installed.
*
* If the global file exists, than the application is installed. Otherwise the application is not installed.
*/
var $check_installed = true;
/*
* location where the forms are located (Set the filename equal to their stage)
*/
var $forms = 'forms/';
/*
* Location of the SQL file holding the SQL queries to be made
*
* NOTE:
* Make sure that every CREATE query is set into an array called $create even if there is only one query.
* Make sure that every INSERT query is set into an array called $insert even if there is only one query.
*/
var $sqls = 'sql/queries.php';
/*
* The following forces the class to die on error and print the error
*/
var $force_errors = false;
/*
* Set the following true if you want the script to debug at each error
*/
var $debug = true;
/*
* Determines weather you want a HEADER redirect or one by JavaScript
*
* Possible values:
* HEADER (PHP Header redirection)
* JavaScript (Javascript Window redirection)
*/
var $redirect_type = 'JavaScript';
/*
* Redirection place where users would go to upon finishing installation
*/
var $redirect_loc = 'documentation.html';
/*
* The following variables are variables for this class. Configure them at your own risk
*/
var $error = array(); // Silent errors returned by this class
var $errno = 0; // Number of errors generated by the class
var $global_loc = null; // The location of the global file created
/*
* function __construct( VOID )
*
* Function that basically sets certain variables.
*/
function __construct()
{
// Creating the location where the global file would be and it's name with extension type
$this->global_loc = $this->gl_loc . $this->globals . '.' . strtolower($this->global_type);
}
/*
* function setup( [ string $stage ] )
* @string $stage - The current stage of the installation. (Includes the form)
*
* Includes the form for the current setup stage
*/
function setup()
{
// Making sure we've got the right stage
$stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
// Including the corrent stage's form
include_once($this->forms . $stage . '.php');
}
/*
* function cont( VOID )
*
* Checks the current stage and determines what the next stage should be
*/
function cont()
{
// Getting the current stage the user is on
$stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
// Advancing to the next stage
$stage = $this->advance($stage);
// Checking if the installation process was finished
if($stage == sizeof($this->stages) || $stage == false)
{
// Checking if we need to create the global file
if($this->create_global == true)
{
$this->create_global();
}
else
{
/*
* The redirect type here is set to JavaScript to over-ride HEADER.
* Reason being, is when the installation is done and the script
* is supposed to redirect the user to the page you specified,
* then the HEADER tries to look for a class that isn't there,
* but JavaScript does it correctly. Don't know why.
*/
$this->redirect_type = "JavaScript";
// Redirecting the user to the page you specified
$this->redirect($this->redirect_loc);
}
break;
}
// Getting the page the user is on (The installation page)
$page = $_SERVER['PHP_SELF'];
// Redirecting the user to the next stage
$this->redirect("{$page}?stage={$this->stages[$stage]}");
}
/*
* function advance( string $stage )
* @string $stage - Current stage that the function would determine the next from
*
* Redirects the user to the next stage after one is done
*/
function advance($stage)
{
// Going through the stages array and looking for the current stage
foreach($this->stages as $id => $st)
{
// Adding 1 to the current stage key
if($stage == $st)
{
return $id + 1;
}
}
// Getting the amount of stages there are
$stage_size = sizeof($this->stages);
// Checking if this is the last stage or if the stage doesn't exist
if($id == $stage_size)
{
return false;
}
else
{
// Neither found... we don't need to advance since the installation is done!
$this->error('STAGE_ADVANCE', "An unknown error occured in " . (__FILE__) . ' - (<strong>' . (__LINE__) . '</strong>)');
return $id;
}
}
/*
* function set_sql( string $host, string $user, string $pass, string $dbname, string $tbl_pre )
* @string $host - The MySQL Host
* @string $user - The MySQL User
* @string $pass - The MySQL Password
* @string $dbname - The Database Name
* @string $tbl_pre - The MySQL Table Prefix
*
* Function that sets the class sql array to their appropriate values
*/
function set_sql($host, $user, $pass, $dbname, $tbl_pre)
{
// Setting the SQL array to it's appropriate values
$this->sql = array('HOST' => $host,
'USER' => $user,
'PASS' => $pass,
'DBASE' => $dbname,
'TBL_PRE' => $tbl_pre);
// Setting the SQL session
$_SESSION['sql'] = $this->sql;
// Checking if the values submitted were valid
if($this->check_sql($host, $user, $pass, $dbname) == true)
{
// Values were correct
return true;
}
// Values were incorrect
$this->error('SETTING_SQL', "Values submitted weren't valid");
return false;
}
/*
* function create_global( VOID )
*
* Function that creates the global file.
*/
function create_global()
{
// Checking if we need to check if the application is already installed
if($this->check_installed == true)
{
// Checking if the global file exists
if(file_exists($this->global_loc) == true)
{
// Killing script execution with an error
$this->error('APP_INSTALLATION', "Application already installed.");
}
}
// Checking what kind of global file it would be... PHP or XML
switch($this->global_type)
{
case 'XML':
// The XML content
$content = <<<EOT
<?xml version="1.0" encoding="ISO-8859-1"?>
<database>
<host>{$_SESSION['sql']['HOST']}</host>
<user>{$_SESSION['sql']['USER']}</user>
<pass>{$_SESSION['sql']['PASS']}</pass>
<dbname>{$_SESSION['sql']['DBASE']}</dbname>
<table_prefix>{$_SESSION['sql']['TBL_PRE']}</table_prefix>
</database>
EOT;
break;
case 'PHP':
// The PHP content
$content = <<<EOT
<?php
/* The global configuration file */
// The MySQL Host
$host = "{$_SESSION['sql']['HOST']}";
// The MySQL User
$user = "{$_SESSION['sql']['USER']}";
// The MySQL password
$pass = "{$_SESSION['sql']['PASS']}";
// The MySQL Database Name
$dbname = "{$_SESSION['sql']['DBASE']}";
// The MySQL Table Prefix
$tbl_pre = "{$_SESSION['sql']['TBL_PRE']}";
?>
EOT;
break;
default:
// Neither of the correct options were given. Defaulting to XML
$this->global_type = 'XML';
$this->create_global();
break;
}
// Creating the Global File
if(touch($this->global_loc) != false)
{
// Putting the contents into the global file
if(file_put_contents($this->global_loc, $content) != false)
{
// Checking if we are to create SQL tables into the created database
if($this->use_sql == true)
{
// Doing the SQL portion of the installation
$this->do_sql();
}
else
{
// Redirecting the user to the finish page
$this->redirect($this->redirect_loc);
}
}
}
else
{
// The contents weren't put into the global file. Set the error and return false
$this->error('GLOBAL_FILE_CONTENT', "Contents were failed to be put into the global file {$this->global_loc}");
return false;
}
}
/*
* function do_sql( VOID )
*
* Function that carries out the available SQL queries provided in the SQL file
*/
function do_sql()
{
// Requiring the SQLs page
require $this->sqls;
// Connecting to MySQL
$link = mysql_connect($_SESSION['sql']['HOST'], $_SESSION['sql']['USER'], $_SESSION['sql']['PASS']) or die(mysql_error());
// Selecting the database
$db = mysql_select_db($_SESSION['sql']['DBASE']) or die(mysql_error());
// Checking if CREATE array exists
if(isset($create))
{
// Doing the SQL table creation
foreach($create as $query)
{
// executing the create queries
$result = mysql_query($query, $link);
// Checking of one of the queries failed
if($result == false)
{
$this->error('SQL_QUERY_CREATE',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
return false;
break;
}
}
}
// Checking if INSERT array exists
if(isset($insert))
{
// Looping through each insert query and doing them
foreach($insert as $query)
{
// Executing the queries
$result = mysql_query($query, $link);
// Checking if one of them failed.
if($result == false)
{
$this->error('SQL_QUERY_INSERT',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
return false;
break;
}
}
}
// Making sure there were no possible errors
if(!isset($create) && !isset($insert))
{
$this->error('DO_SQL_EXECUTION',"The SQL file provided doesn't have the required arrays set.");
return false;
}
// Everything carried out correctly. Redirect to the homepage
$this->redirect($this->redirect_loc);
}
function redirect($page)
{
// Redirecting the user to the next stage based on the preferable redirection type
switch($this->redirect_type)
{
case "HEADER":
// It's header... turning the output buffering on
ob_start();
// Redirecting the user
header("LOCATION: {$page}");
// Sending the content and turning the output buffer off
ob_end_flush();
break;
case "JavaScript":
// Redirecting the user with JavaScript
echo "<script language=\"JavaScript\">window.location=\"{$page}\";</script>";
break;
default:
// Not one possible redirection type is given... using JavaScript by default
$this->redirect_type = 'JavaScript';
$this->redirect($page);
break;
}
}
/*
* function error( string $key, string $msg )
* @string $key - The key to the error array
* @string $msg - The message to the error array associated to the given $key
*
* Sets the error array and checks on farther error/debugging options
*/
function error($key, $msg)
{
// Setting the error array
$this->error[$key] = $msg;
// Incrementing the error number value
$this->errno += 1;
// Checking if we should debug the error(s)
if($this->debug == true)
{
echo $this->debug();
// Checking if we have to force this error
if($this->force_errors == true)
{
exit;
}
}
// Checking if we should force the error to be shown
if($this->force_errors == true)
{
die("<strong>$key</strong> $msg");
}
}
/*
* function debug( VOID )
*
* Function that does some debugging options on produced/generated errors
*/
function debug()
{
// Going through each error recieved and generating the rest of the debugging text
$err_name = array_keys($this->error);
$err_msg = $this->retaval($this->error, $this->errno - 1, false, true);
$text = "<p><strong>{$err_name[$this->errno - 1]}</strong>: $err_msg<br />\n";
// Finishing off the debugging text
$text .= "</p>";
// Returning the generated text
return $text;
}
/************************************************************************************************
* The following functions are a functions repository *
* To what you may need for the installation *
* process. *
************************************************************************************************/
/*
* function check_sql( [ string $db ] )
* @string $db - Whether or not we should check the validicy of the database
*
* Checks if the MySQL credentials are correct
*/
function check_sql($db = true)
{
// Connecting to MySQL and capturing the resource handler
$link = @mysql_connect($this->sql['HOST'], $this->sql['USER'], $this->sql['PASS']);
// Checking if we are not connected
if($link == false)
{
$this->error('SQL_CONNECTION', mysql_errno() . ' - ' . mysql_error());
}
// Checking if we should select the database
if($db == true)
{
// Selecting the database and capturing the resource handler
$db = @mysql_select_db($this->sql['DBASE']);
// Checking if there were any errors in the connection
if($db == false)
{
$this->error('SQL_DB_SELECT', mysql_errno() . ' - ' . mysql_error());
}
}
// Closing the MySQL Connection if we are connected
if($link != false)
{
@mysql_close($link);
}
// Returning the result.
return (($link != false || $db != false) ? true : false);
}
/*
* function get_xml( string $what )
* @string $what - The variable to take from an XML File
*
* Retrieves a variable from the global file
*/
function get_xml($what)
{
// Loading the global XML file
$db = (array) simplexml_load_file($this->global_file);
// Returing the requested variable to the user
return (string) $db[$what];
}
/*
* function add( string $value [, mix $key] )
* @string $value - The value to add to the additional variable
* @mix $key - This variable could be set to the following possible values
* * 00 (2 zeros) - If you want a numeric, automatically-set keys
* * Anything else - If the key is not set to 00, then the key would be what you put it. (string value and key only)
*
* Creates an additional array for whatever needs you may need.
*/
function add($value, $key = 00)
{
// Checking if the key should be numeric
if($key == 00)
{
// Getting the next available numeric key so we wouldn't over ride a value
foreach($this->add as $id => $val)
{
// Checking if the key is numeric
if(is_numeric($id))
{
$keyed = $id;
}
}
// Incrementing the last numeric key by one
$key = $keyed + 1;
// Unsetting the useless $val
unset($val);
}
// Generating the session
$_SESSION['ADD'][$key] = $value;
// Returning true
return true;
}
/*
* function retaval( array $var, int $akey [, boolean $array [, boolean $value [, boolean $numeric]]])
* @array $var - The array to retrieve the value/key from
* @integer $akey - The key/value to retrieve in numeric terms (1, 2...)
* @boolean $array - Determines if the result should come as an array with the key and the
value.
* @boolean $value - Set true if you want the value, set false if you want the key...
* set to null if you want an array returned with the key and value
* @boolean $numeric - Set to true if you want the key to be numeric... otherwise set
* to false if you want the key to be what it previously was
*
* A function to retrieve a value/key from an array. Returns a string by default, or an
* array if you need the key to be numeric
*
*/
function retaval($var, $akey, $array = false, $value = false, $numeric = false)
{
// Setting some variables
$i = 0;
$num = count($var);
// Looping though the array and choosing the key/value that was asked for
foreach($var as $key => $val)
{
// Setting the return value to the current key/value
if($array == false)
{
$return = (($value == false) ? $key : $val);
}
// Checking if we need the result as an array or a string
if($array != false)
{
// Setting the key to be whatever it needs to be
$key = (($numeric == false) ? $key : 0);
// Setting the return array to be whatever it was meant to be
$return[$key] = (($value == false) ? ((is_null($value)) ? $value : $key) : $val);
}
// Making sure we stop when we have the correct key/value
if($i == $akey)
{
break;
}
// Incrementing the counter
++$i;
}
// Returning the return value if there is any
return (($i > $num) ? false : $return);
}
}
?>
/*
* install.php
* A global installation class created to be used with any kind of application.
*
* Before doing anything with this class, I recommend you read the documentation to figure out
* what you are doing. That's so you won't accidentally mess something up.
*/
class install {
/*
* The stages that the script would have in it's overall installation process.
* This is to ensure that the user wouldn't just put anything random into the
* url and try to do something. Also for automation.
*/
var $stages = array('stage_1','stage_2','stage_3');
/*
* This determines if we would have anything to do with SQL
*/
var $use_sql = true;
/*
* This would store the SQL information once/if recieved (For the stage submitted on... later on, uses $_SESSION)
*/
var $sql = array('HOST' => null, // MySQL Host
'USER' => null, // MySQL Username
'PASS' => null, // MySQL Passowrd
'DBASE' => null, // MySQL Database Name
'TBL_PRE' => null); // MySQL Table Prefix
/*
* Determines if a global file would be created at the end.
*
* The global file would consist of the MySQL host, username, password and the database name
*/
var $create_global = true;
/*
* Determines the location that the configuration file would be saved to (Make sure it exists)
*/
var $gl_loc = 'includes/';
/*
* Determines the name for the global file
*/
var $globals = 'global';
/*
* Determines the global file type (also used as the extension for the global file)
*
* Possible types:
* XML, PHP
*
* NOTE: If you know PHP it would be easy for you to add more types here
*/
var $global_type = 'XML';
/*
* Set to true if you are installing and want to check if the application is already installed.
*
* If the global file exists, than the application is installed. Otherwise the application is not installed.
*/
var $check_installed = true;
/*
* location where the forms are located (Set the filename equal to their stage)
*/
var $forms = 'forms/';
/*
* Location of the SQL file holding the SQL queries to be made
*
* NOTE:
* Make sure that every CREATE query is set into an array called $create even if there is only one query.
* Make sure that every INSERT query is set into an array called $insert even if there is only one query.
*/
var $sqls = 'sql/queries.php';
/*
* The following forces the class to die on error and print the error
*/
var $force_errors = false;
/*
* Set the following true if you want the script to debug at each error
*/
var $debug = true;
/*
* Determines weather you want a HEADER redirect or one by JavaScript
*
* Possible values:
* HEADER (PHP Header redirection)
* JavaScript (Javascript Window redirection)
*/
var $redirect_type = 'JavaScript';
/*
* Redirection place where users would go to upon finishing installation
*/
var $redirect_loc = 'documentation.html';
/*
* The following variables are variables for this class. Configure them at your own risk
*/
var $error = array(); // Silent errors returned by this class
var $errno = 0; // Number of errors generated by the class
var $global_loc = null; // The location of the global file created
/*
* function __construct( VOID )
*
* Function that basically sets certain variables.
*/
function __construct()
{
// Creating the location where the global file would be and it's name with extension type
$this->global_loc = $this->gl_loc . $this->globals . '.' . strtolower($this->global_type);
}
/*
* function setup( [ string $stage ] )
* @string $stage - The current stage of the installation. (Includes the form)
*
* Includes the form for the current setup stage
*/
function setup()
{
// Making sure we've got the right stage
$stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
// Including the corrent stage's form
include_once($this->forms . $stage . '.php');
}
/*
* function cont( VOID )
*
* Checks the current stage and determines what the next stage should be
*/
function cont()
{
// Getting the current stage the user is on
$stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
// Advancing to the next stage
$stage = $this->advance($stage);
// Checking if the installation process was finished
if($stage == sizeof($this->stages) || $stage == false)
{
// Checking if we need to create the global file
if($this->create_global == true)
{
$this->create_global();
}
else
{
/*
* The redirect type here is set to JavaScript to over-ride HEADER.
* Reason being, is when the installation is done and the script
* is supposed to redirect the user to the page you specified,
* then the HEADER tries to look for a class that isn't there,
* but JavaScript does it correctly. Don't know why.
*/
$this->redirect_type = "JavaScript";
// Redirecting the user to the page you specified
$this->redirect($this->redirect_loc);
}
break;
}
// Getting the page the user is on (The installation page)
$page = $_SERVER['PHP_SELF'];
// Redirecting the user to the next stage
$this->redirect("{$page}?stage={$this->stages[$stage]}");
}
/*
* function advance( string $stage )
* @string $stage - Current stage that the function would determine the next from
*
* Redirects the user to the next stage after one is done
*/
function advance($stage)
{
// Going through the stages array and looking for the current stage
foreach($this->stages as $id => $st)
{
// Adding 1 to the current stage key
if($stage == $st)
{
return $id + 1;
}
}
// Getting the amount of stages there are
$stage_size = sizeof($this->stages);
// Checking if this is the last stage or if the stage doesn't exist
if($id == $stage_size)
{
return false;
}
else
{
// Neither found... we don't need to advance since the installation is done!
$this->error('STAGE_ADVANCE', "An unknown error occured in " . (__FILE__) . ' - (<strong>' . (__LINE__) . '</strong>)');
return $id;
}
}
/*
* function set_sql( string $host, string $user, string $pass, string $dbname, string $tbl_pre )
* @string $host - The MySQL Host
* @string $user - The MySQL User
* @string $pass - The MySQL Password
* @string $dbname - The Database Name
* @string $tbl_pre - The MySQL Table Prefix
*
* Function that sets the class sql array to their appropriate values
*/
function set_sql($host, $user, $pass, $dbname, $tbl_pre)
{
// Setting the SQL array to it's appropriate values
$this->sql = array('HOST' => $host,
'USER' => $user,
'PASS' => $pass,
'DBASE' => $dbname,
'TBL_PRE' => $tbl_pre);
// Setting the SQL session
$_SESSION['sql'] = $this->sql;
// Checking if the values submitted were valid
if($this->check_sql($host, $user, $pass, $dbname) == true)
{
// Values were correct
return true;
}
// Values were incorrect
$this->error('SETTING_SQL', "Values submitted weren't valid");
return false;
}
/*
* function create_global( VOID )
*
* Function that creates the global file.
*/
function create_global()
{
// Checking if we need to check if the application is already installed
if($this->check_installed == true)
{
// Checking if the global file exists
if(file_exists($this->global_loc) == true)
{
// Killing script execution with an error
$this->error('APP_INSTALLATION', "Application already installed.");
}
}
// Checking what kind of global file it would be... PHP or XML
switch($this->global_type)
{
case 'XML':
// The XML content
$content = <<<EOT
<?xml version="1.0" encoding="ISO-8859-1"?>
<database>
<host>{$_SESSION['sql']['HOST']}</host>
<user>{$_SESSION['sql']['USER']}</user>
<pass>{$_SESSION['sql']['PASS']}</pass>
<dbname>{$_SESSION['sql']['DBASE']}</dbname>
<table_prefix>{$_SESSION['sql']['TBL_PRE']}</table_prefix>
</database>
EOT;
break;
case 'PHP':
// The PHP content
$content = <<<EOT
<?php
/* The global configuration file */
// The MySQL Host
$host = "{$_SESSION['sql']['HOST']}";
// The MySQL User
$user = "{$_SESSION['sql']['USER']}";
// The MySQL password
$pass = "{$_SESSION['sql']['PASS']}";
// The MySQL Database Name
$dbname = "{$_SESSION['sql']['DBASE']}";
// The MySQL Table Prefix
$tbl_pre = "{$_SESSION['sql']['TBL_PRE']}";
?>
EOT;
break;
default:
// Neither of the correct options were given. Defaulting to XML
$this->global_type = 'XML';
$this->create_global();
break;
}
// Creating the Global File
if(touch($this->global_loc) != false)
{
// Putting the contents into the global file
if(file_put_contents($this->global_loc, $content) != false)
{
// Checking if we are to create SQL tables into the created database
if($this->use_sql == true)
{
// Doing the SQL portion of the installation
$this->do_sql();
}
else
{
// Redirecting the user to the finish page
$this->redirect($this->redirect_loc);
}
}
}
else
{
// The contents weren't put into the global file. Set the error and return false
$this->error('GLOBAL_FILE_CONTENT', "Contents were failed to be put into the global file {$this->global_loc}");
return false;
}
}
/*
* function do_sql( VOID )
*
* Function that carries out the available SQL queries provided in the SQL file
*/
function do_sql()
{
// Requiring the SQLs page
require $this->sqls;
// Connecting to MySQL
$link = mysql_connect($_SESSION['sql']['HOST'], $_SESSION['sql']['USER'], $_SESSION['sql']['PASS']) or die(mysql_error());
// Selecting the database
$db = mysql_select_db($_SESSION['sql']['DBASE']) or die(mysql_error());
// Checking if CREATE array exists
if(isset($create))
{
// Doing the SQL table creation
foreach($create as $query)
{
// executing the create queries
$result = mysql_query($query, $link);
// Checking of one of the queries failed
if($result == false)
{
$this->error('SQL_QUERY_CREATE',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
return false;
break;
}
}
}
// Checking if INSERT array exists
if(isset($insert))
{
// Looping through each insert query and doing them
foreach($insert as $query)
{
// Executing the queries
$result = mysql_query($query, $link);
// Checking if one of them failed.
if($result == false)
{
$this->error('SQL_QUERY_INSERT',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
return false;
break;
}
}
}
// Making sure there were no possible errors
if(!isset($create) && !isset($insert))
{
$this->error('DO_SQL_EXECUTION',"The SQL file provided doesn't have the required arrays set.");
return false;
}
// Everything carried out correctly. Redirect to the homepage
$this->redirect($this->redirect_loc);
}
function redirect($page)
{
// Redirecting the user to the next stage based on the preferable redirection type
switch($this->redirect_type)
{
case "HEADER":
// It's header... turning the output buffering on
ob_start();
// Redirecting the user
header("LOCATION: {$page}");
// Sending the content and turning the output buffer off
ob_end_flush();
break;
case "JavaScript":
// Redirecting the user with JavaScript
echo "<script language=\"JavaScript\">window.location=\"{$page}\";</script>";
break;
default:
// Not one possible redirection type is given... using JavaScript by default
$this->redirect_type = 'JavaScript';
$this->redirect($page);
break;
}
}
/*
* function error( string $key, string $msg )
* @string $key - The key to the error array
* @string $msg - The message to the error array associated to the given $key
*
* Sets the error array and checks on farther error/debugging options
*/
function error($key, $msg)
{
// Setting the error array
$this->error[$key] = $msg;
// Incrementing the error number value
$this->errno += 1;
// Checking if we should debug the error(s)
if($this->debug == true)
{
echo $this->debug();
// Checking if we have to force this error
if($this->force_errors == true)
{
exit;
}
}
// Checking if we should force the error to be shown
if($this->force_errors == true)
{
die("<strong>$key</strong> $msg");
}
}
/*
* function debug( VOID )
*
* Function that does some debugging options on produced/generated errors
*/
function debug()
{
// Going through each error recieved and generating the rest of the debugging text
$err_name = array_keys($this->error);
$err_msg = $this->retaval($this->error, $this->errno - 1, false, true);
$text = "<p><strong>{$err_name[$this->errno - 1]}</strong>: $err_msg<br />\n";
// Finishing off the debugging text
$text .= "</p>";
// Returning the generated text
return $text;
}
/************************************************************************************************
* The following functions are a functions repository *
* To what you may need for the installation *
* process. *
************************************************************************************************/
/*
* function check_sql( [ string $db ] )
* @string $db - Whether or not we should check the validicy of the database
*
* Checks if the MySQL credentials are correct
*/
function check_sql($db = true)
{
// Connecting to MySQL and capturing the resource handler
$link = @mysql_connect($this->sql['HOST'], $this->sql['USER'], $this->sql['PASS']);
// Checking if we are not connected
if($link == false)
{
$this->error('SQL_CONNECTION', mysql_errno() . ' - ' . mysql_error());
}
// Checking if we should select the database
if($db == true)
{
// Selecting the database and capturing the resource handler
$db = @mysql_select_db($this->sql['DBASE']);
// Checking if there were any errors in the connection
if($db == false)
{
$this->error('SQL_DB_SELECT', mysql_errno() . ' - ' . mysql_error());
}
}
// Closing the MySQL Connection if we are connected
if($link != false)
{
@mysql_close($link);
}
// Returning the result.
return (($link != false || $db != false) ? true : false);
}
/*
* function get_xml( string $what )
* @string $what - The variable to take from an XML File
*
* Retrieves a variable from the global file
*/
function get_xml($what)
{
// Loading the global XML file
$db = (array) simplexml_load_file($this->global_file);
// Returing the requested variable to the user
return (string) $db[$what];
}
/*
* function add( string $value [, mix $key] )
* @string $value - The value to add to the additional variable
* @mix $key - This variable could be set to the following possible values
* * 00 (2 zeros) - If you want a numeric, automatically-set keys
* * Anything else - If the key is not set to 00, then the key would be what you put it. (string value and key only)
*
* Creates an additional array for whatever needs you may need.
*/
function add($value, $key = 00)
{
// Checking if the key should be numeric
if($key == 00)
{
// Getting the next available numeric key so we wouldn't over ride a value
foreach($this->add as $id => $val)
{
// Checking if the key is numeric
if(is_numeric($id))
{
$keyed = $id;
}
}
// Incrementing the last numeric key by one
$key = $keyed + 1;
// Unsetting the useless $val
unset($val);
}
// Generating the session
$_SESSION['ADD'][$key] = $value;
// Returning true
return true;
}
/*
* function retaval( array $var, int $akey [, boolean $array [, boolean $value [, boolean $numeric]]])
* @array $var - The array to retrieve the value/key from
* @integer $akey - The key/value to retrieve in numeric terms (1, 2...)
* @boolean $array - Determines if the result should come as an array with the key and the
value.
* @boolean $value - Set true if you want the value, set false if you want the key...
* set to null if you want an array returned with the key and value
* @boolean $numeric - Set to true if you want the key to be numeric... otherwise set
* to false if you want the key to be what it previously was
*
* A function to retrieve a value/key from an array. Returns a string by default, or an
* array if you need the key to be numeric
*
*/
function retaval($var, $akey, $array = false, $value = false, $numeric = false)
{
// Setting some variables
$i = 0;
$num = count($var);
// Looping though the array and choosing the key/value that was asked for
foreach($var as $key => $val)
{
// Setting the return value to the current key/value
if($array == false)
{
$return = (($value == false) ? $key : $val);
}
// Checking if we need the result as an array or a string
if($array != false)
{
// Setting the key to be whatever it needs to be
$key = (($numeric == false) ? $key : 0);
// Setting the return array to be whatever it was meant to be
$return[$key] = (($value == false) ? ((is_null($value)) ? $value : $key) : $val);
}
// Making sure we stop when we have the correct key/value
if($i == $akey)
{
break;
}
// Incrementing the counter
++$i;
}
// Returning the return value if there is any
return (($i > $num) ? false : $return);
}
}
?>
- <?php
- /*
- * install.php
- * A global installation class created to be used with any kind of application.
- *
- * Before doing anything with this class, I recommend you read the documentation to figure out
- * what you are doing. That's so you won't accidentally mess something up.
- */
- class install {
- /*
- * The stages that the script would have in it's overall installation process.
- * This is to ensure that the user wouldn't just put anything random into the
- * url and try to do something. Also for automation.
- */
- var $stages = array('stage_1','stage_2','stage_3');
- /*
- * This determines if we would have anything to do with SQL
- */
- var $use_sql = true;
- /*
- * This would store the SQL information once/if recieved (For the stage submitted on... later on, uses $_SESSION)
- */
- var $sql = array('HOST' => null, // MySQL Host
- 'USER' => null, // MySQL Username
- 'PASS' => null, // MySQL Passowrd
- 'DBASE' => null, // MySQL Database Name
- 'TBL_PRE' => null); // MySQL Table Prefix
- /*
- * Determines if a global file would be created at the end.
- *
- * The global file would consist of the MySQL host, username, password and the database name
- */
- var $create_global = true;
- /*
- * Determines the location that the configuration file would be saved to (Make sure it exists)
- */
- var $gl_loc = 'includes/';
- /*
- * Determines the name for the global file
- */
- var $globals = 'global';
- /*
- * Determines the global file type (also used as the extension for the global file)
- *
- * Possible types:
- * XML, PHP
- *
- * NOTE: If you know PHP it would be easy for you to add more types here
- */
- var $global_type = 'XML';
- /*
- * Set to true if you are installing and want to check if the application is already installed.
- *
- * If the global file exists, than the application is installed. Otherwise the application is not installed.
- */
- var $check_installed = true;
- /*
- * location where the forms are located (Set the filename equal to their stage)
- */
- var $forms = 'forms/';
- /*
- * Location of the SQL file holding the SQL queries to be made
- *
- * NOTE:
- * Make sure that every CREATE query is set into an array called $create even if there is only one query.
- * Make sure that every INSERT query is set into an array called $insert even if there is only one query.
- */
- var $sqls = 'sql/queries.php';
- /*
- * The following forces the class to die on error and print the error
- */
- var $force_errors = false;
- /*
- * Set the following true if you want the script to debug at each error
- */
- var $debug = true;
- /*
- * Determines weather you want a HEADER redirect or one by JavaScript
- *
- * Possible values:
- * HEADER (PHP Header redirection)
- * JavaScript (Javascript Window redirection)
- */
- var $redirect_type = 'JavaScript';
- /*
- * Redirection place where users would go to upon finishing installation
- */
- var $redirect_loc = 'documentation.html';
- /*
- * The following variables are variables for this class. Configure them at your own risk
- */
- var $error = array(); // Silent errors returned by this class
- var $errno = 0; // Number of errors generated by the class
- var $global_loc = null; // The location of the global file created
- /*
- * function __construct( VOID )
- *
- * Function that basically sets certain variables.
- */
- function __construct()
- {
- // Creating the location where the global file would be and it's name with extension type
- $this->global_loc = $this->gl_loc . $this->globals . '.' . strtolower($this->global_type);
- }
- /*
- * function setup( [ string $stage ] )
- * @string $stage - The current stage of the installation. (Includes the form)
- *
- * Includes the form for the current setup stage
- */
- function setup()
- {
- // Making sure we've got the right stage
- $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
- // Including the corrent stage's form
- include_once($this->forms . $stage . '.php');
- }
- /*
- * function cont( VOID )
- *
- * Checks the current stage and determines what the next stage should be
- */
- function cont()
- {
- // Getting the current stage the user is on
- $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
- // Advancing to the next stage
- $stage = $this->advance($stage);
- // Checking if the installation process was finished
- if($stage == sizeof($this->stages) || $stage == false)
- {
- // Checking if we need to create the global file
- if($this->create_global == true)
- {
- $this->create_global();
- }
- else
- {
- /*
- * The redirect type here is set to JavaScript to over-ride HEADER.
- * Reason being, is when the installation is done and the script
- * is supposed to redirect the user to the page you specified,
- * then the HEADER tries to look for a class that isn't there,
- * but JavaScript does it correctly. Don't know why.
- */
- $this->redirect_type = "JavaScript";
- // Redirecting the user to the page you specified
- $this->redirect($this->redirect_loc);
- }
- break;
- }
- // Getting the page the user is on (The installation page)
- $page = $_SERVER['PHP_SELF'];
- // Redirecting the user to the next stage
- $this->redirect("{$page}?stage={$this->stages[$stage]}");
- }
- /*
- * function advance( string $stage )
- * @string $stage - Current stage that the function would determine the next from
- *
- * Redirects the user to the next stage after one is done
- */
- function advance($stage)
- {
- // Going through the stages array and looking for the current stage
- foreach($this->stages as $id => $st)
- {
- // Adding 1 to the current stage key
- if($stage == $st)
- {
- return $id + 1;
- }
- }
- // Getting the amount of stages there are
- $stage_size = sizeof($this->stages);
- // Checking if this is the last stage or if the stage doesn't exist
- if($id == $stage_size)
- {
- return false;
- }
- else
- {
- // Neither found... we don't need to advance since the installation is done!
- $this->error('STAGE_ADVANCE', "An unknown error occured in " . (__FILE__) . ' - (<strong>' . (__LINE__) . '</strong>)');
- return $id;
- }
- }
- /*
- * function set_sql( string $host, string $user, string $pass, string $dbname, string $tbl_pre )
- * @string $host - The MySQL Host
- * @string $user - The MySQL User
- * @string $pass - The MySQL Password
- * @string $dbname - The Database Name
- * @string $tbl_pre - The MySQL Table Prefix
- *
- * Function that sets the class sql array to their appropriate values
- */
- function set_sql($host, $user, $pass, $dbname, $tbl_pre)
- {
- // Setting the SQL array to it's appropriate values
- $this->sql = array('HOST' => $host,
- 'USER' => $user,
- 'PASS' => $pass,
- 'DBASE' => $dbname,
- 'TBL_PRE' => $tbl_pre);
- // Setting the SQL session
- $_SESSION['sql'] = $this->sql;
- // Checking if the values submitted were valid
- if($this->check_sql($host, $user, $pass, $dbname) == true)
- {
- // Values were correct
- return true;
- }
- // Values were incorrect
- $this->error('SETTING_SQL', "Values submitted weren't valid");
- return false;
- }
- /*
- * function create_global( VOID )
- *
- * Function that creates the global file.
- */
- function create_global()
- {
- // Checking if we need to check if the application is already installed
- if($this->check_installed == true)
- {
- // Checking if the global file exists
- if(file_exists($this->global_loc) == true)
- {
- // Killing script execution with an error
- $this->error('APP_INSTALLATION', "Application already installed.");
- }
- }
- // Checking what kind of global file it would be... PHP or XML
- switch($this->global_type)
- {
- case 'XML':
- // The XML content
- $content = <<<EOT
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <database>
- <host>{$_SESSION['sql']['HOST']}</host>
- <user>{$_SESSION['sql']['USER']}</user>
- <pass>{$_SESSION['sql']['PASS']}</pass>
- <dbname>{$_SESSION['sql']['DBASE']}</dbname>
- <table_prefix>{$_SESSION['sql']['TBL_PRE']}</table_prefix>
- </database>
- EOT;
- break;
- case 'PHP':
- // The PHP content
- $content = <<<EOT
- <?php
- /* The global configuration file */
- // The MySQL Host
- $host = "{$_SESSION['sql']['HOST']}";
- // The MySQL User
- $user = "{$_SESSION['sql']['USER']}";
- // The MySQL password
- $pass = "{$_SESSION['sql']['PASS']}";
- // The MySQL Database Name
- $dbname = "{$_SESSION['sql']['DBASE']}";
- // The MySQL Table Prefix
- $tbl_pre = "{$_SESSION['sql']['TBL_PRE']}";
- ?>
- EOT;
- break;
- default:
- // Neither of the correct options were given. Defaulting to XML
- $this->global_type = 'XML';
- $this->create_global();
- break;
- }
- // Creating the Global File
- if(touch($this->global_loc) != false)
- {
- // Putting the contents into the global file
- if(file_put_contents($this->global_loc, $content) != false)
- {
- // Checking if we are to create SQL tables into the created database
- if($this->use_sql == true)
- {
- // Doing the SQL portion of the installation
- $this->do_sql();
- }
- else
- {
- // Redirecting the user to the finish page
- $this->redirect($this->redirect_loc);
- }
- }
- }
- else
- {
- // The contents weren't put into the global file. Set the error and return false
- $this->error('GLOBAL_FILE_CONTENT', "Contents were failed to be put into the global file {$this->global_loc}");
- return false;
- }
- }
- /*
- * function do_sql( VOID )
- *
- * Function that carries out the available SQL queries provided in the SQL file
- */
- function do_sql()
- {
- // Requiring the SQLs page
- require $this->sqls;
- // Connecting to MySQL
- $link = mysql_connect($_SESSION['sql']['HOST'], $_SESSION['sql']['USER'], $_SESSION['sql']['PASS']) or die(mysql_error());
- // Selecting the database
- $db = mysql_select_db($_SESSION['sql']['DBASE']) or die(mysql_error());
- // Checking if CREATE array exists
- if(isset($create))
- {
- // Doing the SQL table creation
- foreach($create as $query)
- {
- // executing the create queries
- $result = mysql_query($query, $link);
- // Checking of one of the queries failed
- if($result == false)
- {
- $this->error('SQL_QUERY_CREATE',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
- return false;
- break;
- }
- }
- }
- // Checking if INSERT array exists
- if(isset($insert))
- {
- // Looping through each insert query and doing them
- foreach($insert as $query)
- {
- // Executing the queries
- $result = mysql_query($query, $link);
- // Checking if one of them failed.
- if($result == false)
- {
- $this->error('SQL_QUERY_INSERT',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
- return false;
- break;
- }
- }
- }
- // Making sure there were no possible errors
- if(!isset($create) && !isset($insert))
- {
- $this->error('DO_SQL_EXECUTION',"The SQL file provided doesn't have the required arrays set.");
- return false;
- }
- // Everything carried out correctly. Redirect to the homepage
- $this->redirect($this->redirect_loc);
- }
- function redirect($page)
- {
- // Redirecting the user to the next stage based on the preferable redirection type
- switch($this->redirect_type)
- {
- case "HEADER":
- // It's header... turning the output buffering on
- ob_start();
- // Redirecting the user
- header("LOCATION: {$page}");
- // Sending the content and turning the output buffer off
- ob_end_flush();
- break;
- case "JavaScript":
- // Redirecting the user with JavaScript
- echo "<script language=\"JavaScript\">window.location=\"{$page}\";</script>";
- break;
- default:
- // Not one possible redirection type is given... using JavaScript by default
- $this->redirect_type = 'JavaScript';
- $this->redirect($page);
- break;
- }
- }
- /*
- * function error( string $key, string $msg )
- * @string $key - The key to the error array
- * @string $msg - The message to the error array associated to the given $key
- *
- * Sets the error array and checks on farther error/debugging options
- */
- function error($key, $msg)
- {
- // Setting the error array
- $this->error[$key] = $msg;
- // Incrementing the error number value
- $this->errno += 1;
- // Checking if we should debug the error(s)
- if($this->debug == true)
- {
- echo $this->debug();
- // Checking if we have to force this error
- if($this->force_errors == true)
- {
- exit;
- }
- }
- // Checking if we should force the error to be shown
- if($this->force_errors == true)
- {
- die("<strong>$key</strong> $msg");
- }
- }
- /*
- * function debug( VOID )
- *
- * Function that does some debugging options on produced/generated errors
- */
- function debug()
- {
- // Going through each error recieved and generating the rest of the debugging text
- $err_name = array_keys($this->error);
- $err_msg = $this->retaval($this->error, $this->errno - 1, false, true);
- $text = "<p><strong>{$err_name[$this->errno - 1]}</strong>: $err_msg<br />\n";
- // Finishing off the debugging text
- $text .= "</p>";
- // Returning the generated text
- return $text;
- }
- /************************************************************************************************
- * The following functions are a functions repository *
- * To what you may need for the installation *
- * process. *
- ************************************************************************************************/
- /*
- * function check_sql( [ string $db ] )
- * @string $db - Whether or not we should check the validicy of the database
- *
- * Checks if the MySQL credentials are correct
- */
- function check_sql($db = true)
- {
- // Connecting to MySQL and capturing the resource handler
- $link = @mysql_connect($this->sql['HOST'], $this->sql['USER'], $this->sql['PASS']);
- // Checking if we are not connected
- if($link == false)
- {
- $this->error('SQL_CONNECTION', mysql_errno() . ' - ' . mysql_error());
- }
- // Checking if we should select the database
- if($db == true)
- {
- // Selecting the database and capturing the resource handler
- $db = @mysql_select_db($this->sql['DBASE']);
- // Checking if there were any errors in the connection
- if($db == false)
- {
- $this->error('SQL_DB_SELECT', mysql_errno() . ' - ' . mysql_error());
- }
- }
- // Closing the MySQL Connection if we are connected
- if($link != false)
- {
- @mysql_close($link);
- }
- // Returning the result.
- return (($link != false || $db != false) ? true : false);
- }
- /*
- * function get_xml( string $what )
- * @string $what - The variable to take from an XML File
- *
- * Retrieves a variable from the global file
- */
- function get_xml($what)
- {
- // Loading the global XML file
- $db = (array) simplexml_load_file($this->global_file);
- // Returing the requested variable to the user
- return (string) $db[$what];
- }
- /*
- * function add( string $value [, mix $key] )
- * @string $value - The value to add to the additional variable
- * @mix $key - This variable could be set to the following possible values
- * * 00 (2 zeros) - If you want a numeric, automatically-set keys
- * * Anything else - If the key is not set to 00, then the key would be what you put it. (string value and key only)
- *
- * Creates an additional array for whatever needs you may need.
- */
- function add($value, $key = 00)
- {
- // Checking if the key should be numeric
- if($key == 00)
- {
- // Getting the next available numeric key so we wouldn't over ride a value
- foreach($this->add as $id => $val)
- {
- // Checking if the key is numeric
- if(is_numeric($id))
- {
- $keyed = $id;
- }
- }
- // Incrementing the last numeric key by one
- $key = $keyed + 1;
- // Unsetting the useless $val
- unset($val);
- }
- // Generating the session
- $_SESSION['ADD'][$key] = $value;
- // Returning true
- return true;
- }
- /*
- * function retaval( array $var, int $akey [, boolean $array [, boolean $value [, boolean $numeric]]])
- * @array $var - The array to retrieve the value/key from
- * @integer $akey - The key/value to retrieve in numeric terms (1, 2...)
- * @boolean $array - Determines if the result should come as an array with the key and the
- value.
- * @boolean $value - Set true if you want the value, set false if you want the key...
- * set to null if you want an array returned with the key and value
- * @boolean $numeric - Set to true if you want the key to be numeric... otherwise set
- * to false if you want the key to be what it previously was
- *
- * A function to retrieve a value/key from an array. Returns a string by default, or an
- * array if you need the key to be numeric
- *
- */
- function retaval($var, $akey, $array = false, $value = false, $numeric = false)
- {
- // Setting some variables
- $i = 0;
- $num = count($var);
- // Looping though the array and choosing the key/value that was asked for
- foreach($var as $key => $val)
- {
- // Setting the return value to the current key/value
- if($array == false)
- {
- $return = (($value == false) ? $key : $val);
- }
- // Checking if we need the result as an array or a string
- if($array != false)
- {
- // Setting the key to be whatever it needs to be
- $key = (($numeric == false) ? $key : 0);
- // Setting the return array to be whatever it was meant to be
- $return[$key] = (($value == false) ? ((is_null($value)) ? $value : $key) : $val);
- }
- // Making sure we stop when we have the correct key/value
- if($i == $akey)
- {
- break;
- }
- // Incrementing the counter
- ++$i;
- }
- // Returning the return value if there is any
- return (($i > $num) ? false : $return);
- }
- }
- ?>
Im un maniaque commentaire...Je commente à peu près tous seule ligne de code pour expliquer ce que je fais. J'ajoute en peu de rappels et des notes, des crédits si Im le rendant public, le commentaire de tout premier est le nom de fichier et fichier de description.
J'aime utiliser des variables et des noms de classe qui décrit ce qu'ils tiennent / fonction autant que possible avec le moins de mots possible...
J'ai tout tiret et tout garder en conformité avec ce qu'ils sont associés à...thats le mieux que j'ai pu l'expliquer.
Im un perfectionniste, donc si je copier une classe de quelqu'un, peu importe comment elle est grande, je commente tout et tout, ils tiret comme je le souhaite avant que je le télécharge sur mon site
PHP Code: [ Select ]
if ( $something )
Je hais aussi:
PHP Code: [ Select ]
if( $something) { /*code*/ }
if ($something) {
//code
}
if($something )
if ($something) {
//code
}
if($something )
- if( $something) { /*code*/ }
- if ($something) {
- //code
- }
- if($something )
Qu'est-ce que j'aime, c'est:
PHP Code: [ Select ]
if($cause == $effect && $cause2 == $effect2)
{
// Some code
}
else
{
// Some code
}
{
// Some code
}
else
{
// Some code
}
- if($cause == $effect && $cause2 == $effect2)
- {
- // Some code
- }
- else
- {
- // Some code
- }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- mk27
- Proficient


- Inscription: Juin 09, 2009
- Messages: 334
- Status: Offline
J'utilise le K & R style accolades
mais je suis obligé de tolérer les Allman (ou «tous les blancs»), comme j'aime à l'appeler:
La raison pour laquelle je n'aime pas c'est que je préfère le code compressé verticalement afin que je puisse afficher autant de lui comme possible à la fois, tout en conservant le caractère sacré de l'; ligne intacte.
Im court sur les commentaires, mais fort sur l'écriture de code clair avec les noms appropriés variable. J'ai horreur des espaces à la place des onglets et l'idiotie indentation autre.
Une autre chose aussi que je ne souhaite * I * tout le monde aurait est:
Toutes les fonctions des définitions par ordre alphabétique!
Parfois, je vais inclure un commentaire que des listes d'appels possibles, par exemple.
comme cela est souvent un indice important pour moi comme pour whats going on.
Code: [ Select ]
if (whatever) {
declare this, that;
...
}
declare this, that;
...
}
- if (whatever) {
- declare this, that;
- ...
- }
mais je suis obligé de tolérer les Allman (ou «tous les blancs»), comme j'aime à l'appeler:
Code: [ Select ]
sub example
(
arg 1
arg 2
)
{
declare this;
declare that;
wonder if there is a program in here?
}
(
arg 1
arg 2
)
{
declare this;
declare that;
wonder if there is a program in here?
}
- sub example
- (
- arg 1
- arg 2
- )
- {
- declare this;
- declare that;
- wonder if there is a program in here?
- }
La raison pour laquelle je n'aime pas c'est que je préfère le code compressé verticalement afin que je puisse afficher autant de lui comme possible à la fois, tout en conservant le caractère sacré de l'; ligne intacte.
Im court sur les commentaires, mais fort sur l'écriture de code clair avec les noms appropriés variable. J'ai horreur des espaces à la place des onglets et l'idiotie indentation autre.
Une autre chose aussi que je ne souhaite * I * tout le monde aurait est:
Toutes les fonctions des définitions par ordre alphabétique!
Parfois, je vais inclure un commentaire que des listes d'appels possibles, par exemple.
Code: [ Select ]
function test() { /* called from etc.html and otherfunc() */
comme cela est souvent un indice important pour moi comme pour whats going on.
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 4 messages
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 160 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
