Skip to content

Instantly share code, notes, and snippets.

@Firestorm-Graphics
Last active July 16, 2017 11:20
Show Gist options
  • Save Firestorm-Graphics/98f80b2b45c591d107c35c5f7e5c8632 to your computer and use it in GitHub Desktop.
Save Firestorm-Graphics/98f80b2b45c591d107c35c5f7e5c8632 to your computer and use it in GitHub Desktop.
userspice 4 autoload
class.autoloader.php will automaticly load classes in the directory it resides, this can be changed by changing the $pathTop variable
classes will be instantiated only when required, to check just place bold("<br><br>input class included")at the top of input class file
then visit a page with form fields, you'll get "input class included" displayed at the top of the page only on pages with input fields.
place class.autoloader.php into users/classes/ folder,
open init.php and find all the classes being required i.e:
/*
require_once $abs_us_root.$us_url_root.'users/classes/Config.php';
require_once $abs_us_root.$us_url_root.'users/classes/Cookie.php';
require_once $abs_us_root.$us_url_root.'users/classes/DB.php';
require_once $abs_us_root.$us_url_root.'users/classes/Hash.php';
require_once $abs_us_root.$us_url_root.'users/classes/Input.php';
require_once $abs_us_root.$us_url_root.'users/classes/Redirect.php';
require_once $abs_us_root.$us_url_root.'users/classes/Session.php';
require_once $abs_us_root.$us_url_root.'users/classes/Token.php';
require_once $abs_us_root.$us_url_root.'users/classes/User.php';
require_once $abs_us_root.$us_url_root.'users/classes/Validate.php';
require_once $abs_us_root.$us_url_root.'users/classes/phpmailer/PHPMailerAutoload.php';
require_once $abs_us_root.$us_url_root.'users/classes/Shuttle_Dumper.php';
*/
now either comment them out or remove completely, in their place you have to have one which must be above session_start();
require_once 'classes/class.autoloader.php';
Minor issue, autoloader requires filenames to be same as class names, the issue here is phpmailer, we need to delete PHPMailerAutoload.php, then rename class.phpmailer.php to PHPMailer.php and class.smtp.php to SMTP.php,
or just download the new folder and drop into the classes folder: https://www.dropbox.com/sh/pt0w6xqd0mnlgk4/AACDUrLb5RKiK70AnTTevTrNa?dl=0
<?php
/*
UserSpice 4
An Open Source PHP User Management System
by the UserSpice Team at http://UserSpice.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//if you are ever questioning if your classes are being included, uncomment the line below and the words "autoloader class included" should show at the top of your page.
//bold("<br><br>autoloader class included");
class Autoloader {
/**
* File extension as a string. Defaults to ".php".
*/
protected static $fileExt = '.php';
/**
* The top level directory where recursion will begin. Defaults to the current
* directory.
*/
protected static $pathTop = __DIR__;
/**
* A placeholder to hold the file iterator so that directory traversal is only
* performed once.
*/
protected static $fileIterator = null;
/**
* Autoload function for registration with spl_autoload_register
*
* Looks recursively through project directory and loads class files based on
* filename match.
*
* @param string $className
*/
public static function loader($className) {
//$directory = new RecursiveDirectoryIterator(static::$pathTop, RecursiveDirectoryIterator::SKIP_DOTS);
if (is_null(static::$fileIterator)) {
//static::$fileIterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::LEAVES_ONLY);
static::$fileIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(static::$pathTop, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
}
$filename = $className . static::$fileExt;
foreach (static::$fileIterator as $file) {
if (strtolower($file->getFilename()) === strtolower($filename)) {
if ($file->isReadable()) {
include_once $file->getPathname();
}
break;
}
}
}
/**
* Sets the $fileExt property
*
* @param string $fileExt The file extension used for class files. Default is "php".
*/
public static function setFileExt($fileExt) {
static::$fileExt = $fileExt;
}
/**
* Sets the $path property
*
* @param string $path The path representing the top level where recursion should
* begin. Defaults to the current directory.
*/
public static function setPath($path) {
static::$pathTop = $path;
}
}
Autoloader::setFileExt('.php');
spl_autoload_register('Autoloader::loader', true, true);
<?php
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
//::
//:: This will patch your init.php from v4.2.9
//::
//:: WARNING ! BACKUP YOUR INIT.PHP BEFORE RUNNING THIS PATCH
//::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
require_once 'init.php';
require_once 'includes/header.php';
require_once 'includes/navigation.php';
$init = 'init.php';
$AutoLoad = '<?php require_once \'classes/class.autoloader.php\';';
$contents = file_get_contents($init);
$line = '<?php';
$contents = str_replace($line, '', $contents);
file_put_contents($init, $contents);
$lines = file($init);
$fopen = fopen($init, "w+");
fwrite( $fopen, $AutoLoad );
foreach( $lines as $line ) {
fwrite( $fopen, "$line");
}
fclose ($fopen);
bold("<br><br>You're good to go. Delete patch file and bask in the glory.");
?>
@Firestorm-Graphics
Copy link
Author

Firestorm-Graphics commented Jul 7, 2017

No need for chunk6.php in this edit

@Firestorm-Graphics
Copy link
Author

use patch_init.php if you don't wish to edit your init.php file

@Firestorm-Graphics
Copy link
Author

updated to included sub folders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment