PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Suman Debnath   Password Checker   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script. Uses the other data files included.
Class: Password Checker
Generate and test safe passwords
Author: By
Last change: Changed the listing priority.
Date: 18 years ago
Size: 2,921 bytes
 

Contents

Class file image Download
<?PHP
//Initialize message
$message = '';
//If any of the options has been clicked/submitted
if ((!empty($_POST['submit'])) || ('genpassword' === $_GET['action'])) {
   
//Fetching selected options
   
$passCheck = htmlentities($_POST['D'] . $_POST['L'] . $_POST['H'] . $_POST['W'] . $_POST['C']);
   
$nameCheck = htmlentities($_POST['P'] . $_POST['T'] . $_POST['S']);

   
//Initializing various files
   
$dictionaryFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'words.txt';
   
$blackCharFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'blacklistchars.txt';
   
$blackWordFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'blacklistwords.txt';

   
//Fetching form elements
   
$userName = htmlentities($_POST['username']);
   
$userPassword = htmlentities($_POST['password']);

   
//Including class file
   
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.passwordChecker.php');
   
//Creating the object
   
$passwordCheckerObj = new passwordChecker();
   
//Setting the configuration
   
$passwordCheckerObj->setConfig($passCheck, $nameCheck, $dictionaryFile, $blackWordFile, $blackCharFile);

    if (!empty(
$_POST['submit'])) {
        if (!
$passwordCheckerObj->checkPassword($userPassword, $userName)) {
           
//If any error
           
$message .= '<STRONG>Error/s:</STRONG>';
            foreach (
$passwordCheckerObj->errorMsgArray as $error) {
               
$message .= '<BR/>' . $error;
            }
        } else {
           
//No error
           
$message .= '<STRONG>Strong Password</STRONG>';
        }
    }

   
//Generating a safe password. This example does not have any checks specified. But such can obviously be specified.
   
if ('genpassword' === $_GET['action']) {
       
$message .= 'Generated password : <STRONG>' . $passwordCheckerObj->getSafePassword() . '</STRONG>';
    }

   
//Removing the object. Not necessary really but still ...
   
unset($passwordCheckerObj);
}

//Showing the form
echo getForm($message);

/**
 * Gets the form to be displayed
 *
 * @param string $message
 * @return mixed
 */
function getForm($message = '') {
   
//Getting the template and return false if not successful
   
$contents = @file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'safe_password_template.html');
    if (
false === $contents) {
        return
$contents;
    }

   
//Initializing some values/inputs
   
$currentURL = basename($_SERVER['PHP_SELF']);
   
$userName = $_POST['username'];
   
$userPassword = $_POST['password'];

   
//Generating the replacement array
   
$replaceArr = array();
   
$replaceArr['GEN_PASSWORD'] = rawurlencode($currentURL) . '?action=genpassword';
   
$replaceArr['FORM_ACTION'] = rawurlencode($currentURL);
   
$replaceArr['FORM_USER'] = htmlentities($userName);
   
$replaceArr['FORM_PASSWORD'] = htmlentities($userPassword);
   
$replaceArr['FORM_MSG'] = $message;

   
//Replacing placeholders in template
   
foreach ($replaceArr as $key => $value) {
       
$contents = str_replace('###' . $key . '###', $value, $contents);
    }

    return
$contents;
}
?>