20/04/2012 | 09:50 | Code
No idea if it’s possible what I want, but I’m working on an application that requires custom classes to override the core functionality, if these files exist. So as an example, this is my current file structure:
- app
- core
- custom
- index.php (class Bootstrap with autoLoader)
Now I want to check if “custom/User.php” exists and accordingly include and use this. It should extend the core User class.
My scripts looks like this at this moment:
core/User.php
<?php
namespace Core;
class User
{
public function getName()
{
return 'Pieter';
}
}
custom/User.php
<?php
namespace Custom;
class User extends \Core\User
{
public function getName()
{
return 'Mr. '.parent::getName();
}
}
index.php
<?php
class Bootstrap
{
public function __construct()
{
spl_autoload_register(array('Bootstrap', 'autoLoader'));
$user = new \Custom\User();
print_r($user->getName());
}
private function autoLoader($className)
{
$fileName = str_replace("\\", "/", strtolower($className)).".php";
if (file_exists($fileName)) {
require_once $fileName;
}
}
}
new Bootstrap();
The example above works fine, but when I delete the Custom\User class it stops working. This is correct behaviour because I’m trying to make an instance of the \Custom\User class which not exists. But what is the best way to change the autoloader to load the correct Core class?
I think there are 2 possibilities:
- Working with className variables. First check if the file exists and attach the correct namespace+classpath to the variable and keep working with that variable to load the class.
//I know the file_exists example won't work because of
//the slashes but this is just an example
$className = '\Custom\User.php';
if (!file_exists($className)) {
$className = '\Core\User.php';
}
$user = new $className();
- Create a static function like getService(‘User’) that handles all discussed matter above and returns the correct class.
public static function getService($classObj) {
$className = '\Custom\\'.$classObj.'.php';
if (!file_exists($className)) {
$className = '\Core\\'.$classObj.'.php';
}
return new $className();
}
$user = Register::getService('User');
I think the second option is the best one but is there some easier, more proper way to do this?
Thanks in advance.
Comments (8)
19/04/2012 | 07:10 | Code
Script before
/* Example script */
DROP PROCEDURE IF EXISTS calculateLength $$
CREATE PROCEDURE calculateLength ()
BEGIN
SELECT value FROM `table` WHERE 1=1
END;
/* End script */
Solution
- Go to any SQL tab in phpMyAdmin.
- Directly below the SQL text area you’ll see a text input labeled “Delimiter.” In that field, enter “$$”.
- I can then paste in this slightly-modifed SQL script. Notice, I’ve removed the DELIMITER statements. phpMyAdmin handles that for me.
/* Example script */
DROP PROCEDURE IF EXISTS calculateLength $$
CREATE PROCEDURE calculateLength ()
BEGIN
SELECT value FROM `table` WHERE 1=1
END $$
/* End script */
Comments (0)
14/03/2011 | 20:14 | Code, Me, Zend
Hoera! Vandaag (14/03/2011) heb ik het Zend Certified Engineer PHP5.3 examen gedaan en ik ben geslaagd!
In detail gaan over de vragen kan ik niet want bij het begin van het examen moet je een papier ondertekenen dat je niets over de vragen zult rond vertellen. Ik kan wel vertellen hoe ik begonnen ben en wat de hoogtepunten (zie als pijnpunten) van mijn examen waren. Het examen zelf heb ik gedaan bij OPNS te Brussel.
Qua studies heb ik me eigenlijk vooral gebaseerd op the Zend Certification Study Guide en de PHP manual (specifiek de wijzigingen in PHP5.3). Tijdens het examen zelf krijg je dan 70 vragen die je moet beantwoorden binnen 90minuten. Op het einde van het examen weet je onmiddellijk uw resultaat. Die laatste klik (show result) is eventjes keihard sterven…
Goed, waarover ging het:
En dan nog wel paar eenvoudige dingen die je dagelijks tegenkomt wanneer je PHP gebruikt:
Must read articles:
Bij deze ben ik dus geslaagd en mag ik dit icoontje ook gaan gebruiken

Comments (2)
6/03/2011 | 14:15 | Code
Perhaps one of the most new and usefull features in PHP5 are the great new built-in iterators from the SPL package, more specific the DirectoryIterator and RecursiveDirectoryIterator classes. Using these classes and functions saves a lot of time coding for easy functions like listing all files in directories.
How we did it before!
<?php
$hdl = opendir('.');
while ($dirEntry = readdir($hdl)) {
if (substr($dirEntry, 0, 1) != '.') {
if(!is_file($dirEntry)) continue;
$listing[] = $dirEntry;
}
}
closedir($hdl);
foreach($listing as $file) {
echo $file.'<br />';
}
?>
A example of how to use the DirectoryIterator class can be seen below:
<?php
foreach (new DirectoryIterator('.') as $file) {
if (!$file->isDot()) {
echo $file->getFileName().'<br />';
}
}
?>
It’s getting even better. The class RecursiveDirectoryIterator loops recursives over all directories:
<?php
$file = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
while($file->valid()) {
if (!$file->isDot()) {
echo $file->getFileName().'<br />';
}
$file->next();
}
?>
Comments (0)
10/02/2011 | 20:36 | Code
This is a quick and easy fix to remove duplicate entries from a MySQL table.
1: Move the non duplicates entries into a temp table
CREATE TABLE new_table as SELECT * FROM wrong_table GROUP BY [column to remove duplicates by];
2: Delete the wrong table
DROP TABLE wrong_table;
3: Rename the new_table to the name of the wrong_table
RENAME TABLE new_table TO wrong_table;
And last but not least, don’t forget to fix your buggy code to stop inserting duplicates entries!
Comments (3)
14/10/2010 | 20:41 | Code
Sometimes it can happens when you install WordPress with the default permalink structure everything will work fine. But when you want to change the permalink to /%year%/%monthnum%/%postname%/ it creates the following url index.php/%year%/%monthnum%/%postname%/ (mind the index.php).
The reason of this problem is that WordPress doesn’t think it’s running on Apache so it doesn’t create/write to the .htaccess file. It looks at the $_SERVER['SERVER_SOFTWARE'] variable which comes back with “WebServerX” based on what phpinfo() says on your server.
So, if you edit the /wp-includes/vars.php file and change the server detection area to:
// Server detection
// $is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false)) ? true : false;
$is_apache = 1;
$is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) ? true : false;
It should work. Basically just bypassing the variable check and manually telling it that it’s running on Apache will remove the index.php from the url.
Comments (4)
9/10/2010 | 19:55 | Code, Zend
When creating some fancy user registration or login form you’ll likely need an easy way to compare the users chosen and confirmed passwords. Here’s a little example how this easily can be done with the Zend Framework validator Identical:
$password = new Zend_Form_Element_Password('password');
$password->setLabel('New Password:');
$password->setRequired(true);
$confirm = new Zend_Form_Element_Password('confirm');
$confirm->setLabel('Confirm New Password:');
$confirm->setRequired(true);
$confirm->addValidator('Identical', false, array('token' => 'password'));
More information can be found here.
Comments (0)
26/09/2010 | 13:02 | Code
This week I discovered something new in PHP. It’s possible to increment alphabet characters with PHP. How cool is that!
$char = "a";
$char++;
echo($char);
This will result in the letter “b”. The same applies to uppercase characters (like capitals).
What happens if you reach the last letter in the alphabet:
$char = "z";
$char++;
echo($char);
This will result in “aa”.
Comments (0)
30/08/2010 | 07:25 | Code
Deze week serieus gevloekt op Ogone. Ze hebben namelijk hun concatenatie van strings aangepast om die SHA-1 IN & SHA-1 OUT hash te maken. Zonder iets van deftige communicatie aan de webdevelopers hebben ze dit op 10 mei 2010 toegepast op alle hun sindsdien nieuw geregistreerde accounts – ik wist allesinds van niets
.
De reden waarom ze dit hebben aangepast is voor een betere beveiliging van de verstuurde data. Nu allemaal goed en wel zolang je geen automatisch systeem hebt die Ogone automatisch gaat configureren voor & door klanten en dus die hashes automatisch gaat opbouwen. Laat staan dat er dan nog eens een modulair platform is waar klanten Ogone kunnen activeren waardoor je nog eens een update mag gaan schrijven voor veel websites… nu het is nu zo… genoeg gejankt. Tijd voor de oplossing! Om jullie veel zoekwerk te besparen heb ik eens kort samengevat wat er nu precies gewijzigd is en hoe je dit eenvoudigst kunt gaan oplossen.
SHA-1 IN:
Vroeger:
$sha1In = sha1($orderID.$amount.$currency.$pspid.$YourOwnSecretString);
Nu:
Je neemt alle inputfields die je gebruikt bij het formulier naar Ogone. Zoals onderstaand formulier:
Nogmaals samengevat gaan we dus alles verzamelen die we naar Ogone gaan versturen. Een lijst van parameters die je moet toevoegen aan uw hash kan je hier vinden (zoek naar hoofdstuk 15.1). Om dan definitief uw string te maken gaan we alles concateneren op basis van NAME=value+(sha-1 in passwordkey). Opgepast de name moet uppercase zijn en de parameters moeten alfabetisch toegevoegd worden en parameters met een lege value mogen niet toegevoegd worden!
Als resultaat kom je dan bijvoorbeeld zoiets uit:
ACCEPTURL=http://project/ogone/accept/payment/74MySecretPassWord!!AMOUNT=3600MySecretPassWord!!CANCELURL=http://project/ogone/cancelled/payment/74MySecretPassWord!!CN=Pieter CappelleMySecretPassWord!!CURRENCY=EURMySecretPassWord!!DECLINEURL=http://project/ogone/declined/payment/74MySecretPassWord!!EMAIL=snakehit@gmail.com.MySecretPassWord!!EXCEPTIONURL=http://project/ogone/exception/payment/74MySecretPassWord!!LANGUAGE=nl_NLMySecretPassWord!!ORDERID=74MySecretPassWord!!OWNERADDRESS=Segersstraat 16MySecretPassWord!!OWNERCTY=BelgiëMySecretPassWord!!OWNERTOWN=IzegemMySecretPassWord!!OWNERZIP=8870MySecretPassWord!!PSPID=XXXXXMySecretPassWord!!TITLE=ProjectMySecretPassWord!!TP=http://project/ogoneMySecretPassWord!!
Om dan de SHA-1 string te bekomen de PHP functie sha1() met bovenstaande string aanroepen en dit meesturen naar Ogone in het veld SHASign.
- Krijg je de foutmelding ‘unknown order/0/s‘ wilt dit zeggen dat je geen SHA-1 string hebt meegestuurd. Wat je dus wel degelijk moet doen…
- Krijg je de melding ‘unknown order/1/s‘ dan wil dit zeggen dat de SHA-1 IN hash die Ogone gemaakt heeft niet overeenkomt met de hash die jij gemaakt hebt. Er dus ergens iets foutgelopen bij het maken van uw hash.
SHA-1 OUT:
Vroeger:
$sha1In = sha1($_REQUEST['orderID'].$_REQUEST['currency'].$_REQUEST['amount'].$_REQUEST['PM'].$_REQUEST['ACCEPTANCE'].$_REQUEST['STATUS'].$_REQUEST['CARDNO'].$_REQUEST['PAYID'].$_REQUEST['NCERROR'].$_REQUEST['BRAND'].$ogoneFeedbackPassword);
Nu:
Het nieuwe systeem is gebaseerd op hetzelfde systeem als de SHA-1 IN hash. Bij de return van Ogone (dus na de betaling) ontvang je een resum parameters. Die parameters kan je opvragen in de $_REQUEST[] . (Om de parameters terug te krijgen van Ogone is het noodzakelijk dat je in hun control panel gaat gaan aanvinken dat ze na de betaling de parameters moeten terugsturen.) Als resultaat krijg je dan zoiets:
Array
(
[controller] => ogone
[action] => accept
[payment] => 75
[orderID] => 75
[currency] => EUR
[amount] => 36
[PM] => CreditCard
[ACCEPTANCE] => test123
[STATUS] => 5
[CARDNO] => XXXXXXXXXXXX1111
[ED] => 0114
[CN] => Pieter Cappelle
[TRXDATE] => 08/27/10
[PAYID] => 8048783
[NCERROR] => 0
[BRAND] => VISA
[IPCTY] => BE
[CCCTY] => US
[ECI] => 7
[CVCCheck] => NO
[AAVCheck] => NO
[VC] => NO
[IP] => 81.82.XXX.220
[SHASIGN] => 375CB7CCFAC268E049020BC1D105DC0EBC0C5687
[PHPSESSID] => 418da012a96bc9bbbcc2e289ace555cf
)
Deze parameters moet je dan opnieuw alfabetisch gaan concateneren. Opgepast! Bovenstaande resultaten kunnen verschillend zijn van uw resultaten. Opnieuw ga naar de lijst van parameters die je moet toevoegen aan uw hash. Die lijst kan je hier vinden (zoek naar hoofdstuk 15.2). Deze parameters moeten allemaal toegevoegd worden aan uw hash. Als 2de opmerking – het password dat je hier gebruikt is uw SHA-1 OUT handtekening die je hebt ingegeven bij Ogone.
Als resultaat kom je dan bijvoorbeeld zoiets uit:
AAVCHECK=NOMySecretPassWord!!ACCEPTANCE=test123MySecretPassWord!!AMOUNT=36MySecretPassWord!!BRAND=VISAMySecretPassWord!!CARDNO=XXXXXXXXXXXX1111MySecretPassWord!!CCCTY=USMySecretPassWord!!CN=Pieter CappelleMySecretPassWord!!CURRENCY=EURMySecretPassWord!!CVCCHECK=NOMySecretPassWord!!ECI=7MySecretPassWord!!ED=0114MySecretPassWord!!IP=81.82.194.220MySecretPassWord!!IPCTY=BEMySecretPassWord!!NCERROR=0SecretPassWord!!ORDERID=75MySecretPassWord!!PAYID=8048783MySecretPassWord!!PM=CreditCardMySecretPassWord!!STATUS=5MySecretPassWord!!TRXDATE=08/25/10MySecretPassWord!!VC=NOMySecretPassWord!!
Om dan de SHA-1 string te bekomen de PHP functie sha1() met bovenstaande string aanroepen en dit vergelijken met de verkregen SHASIGN van Ogone. Als deze gelijk zijn is de transactie goed gelukt. Komen deze niet overeen is er een probleem!
Mocht je vragen (of opmerkingen) hebben stel ze gerust
Update (28/12/2010)
In bovenstaande blogpost ontbreekt nog het volgende: ik ben vergeten te vermelden dat de SHA-sign (dus de SHA-1 IN en SHA-1 OUT) die je genereert in hoofdletters moet worden doorgestuurd. Dus na het aanroepen van de sha1() functie moet je er nog eens strtoupper() bij doen.
Update (21/01/2011)
In de commentaren van dit artikel kan je lezen dat een veelgemaakte fout is om de SHA string niet te laten eindigen op de passwordkey. Je kan dan heel lang zitten zoeken op het probleem zonder dat je het echt ziet. Dus zeker en vast eens checken!
Update (02/09/2011)
1) Nog een vaak voorkomende fout is dat bij de SHA1-OUT enkel en alleen de ingevulde values moeten teruggestuurd worden. Het kan dus gebeuren dat je in uw REQUEST parameters ook lege values hebt. Deze moeten niet toegevoegd worden aan de string. Wel opletten met een getal zoals NUL/0. Dit is niet leeg, dus (not empty) !empty zal niet voldoende zijn voor deze controle!
2) Maak altijd gebruik van de template orderstandard_utf8.asp, dit zorgt ervoor dat alles doorgestuurd wordt in UTF-8. Indien je dit niet gaat dan dit problemen geven bij namen zoals Pierré. Test dus zeker en vast eens uw Ogone implementatie met speciale karakters.
Comments (26)
16/07/2009 | 12:45 | Code
This is the most important SEO mod for your SMF installation. This mod named Pretty URLs totally elivates the level of SEO u have, plus this is automatic, so no need to worry about anything, just install and forget about it.
So let’s see what this actually does, basically it rewrites the original non pretty URLs of SMF to pretty ones and SEO friendly too.
snakehit.be/index.php?topic=100 (original url)
now the new one would look like this
snakehit.be/board-name/topic-name/ (new url)
snakehit.be/profile/snakehit (new profile url)
snakehit.be/unreadreplies
Cool, isnt it? So, lets install it.
Pretty URLs will work with SMF 1.1 and 2.0. It requires an Apache webserver with support for mod_rewrite and .htaccess files.
To install:
- First check your server supports .htaccess per-directory config files and mod_rewrite
- UTF-8 is recommended, though not required. Certain functions will work unreliably with other encodings
- Download and install the base package from the mod site.
- Enable the filters in the new Pretty URLs page
Here is the mod link
Have fun!
Comments (1)
« Previous Entries Next Page »