├── demo ├── README ├── ps │ ├── logout.php │ ├── login.php │ ├── register.php │ ├── reset_password.php │ ├── change_password.php │ └── update.php ├── .htaccess ├── static │ ├── css │ │ └── style.css │ └── js │ │ └── main.js ├── install │ ├── demo.sql │ ├── users_table.sql │ ├── config.tpl │ └── index.php ├── page │ ├── footer.php │ ├── home.php │ ├── resetPassword.php │ ├── account.php │ ├── users.php │ ├── login.php │ ├── account │ │ ├── update │ │ │ └── password.php │ │ └── update.php │ ├── user.php │ ├── header.php │ └── register.php ├── core │ ├── validations.php │ └── inc │ │ └── functions.php ├── index.php └── 404.html ├── .gitignore ├── tests ├── bootstrap.php ├── fixtures │ ├── README.md │ └── users.xml ├── src │ ├── CookieTest.php │ ├── LinkedCollectionTest.php │ ├── HashTest.php │ ├── SessionTest.php │ ├── DBTest.php │ ├── DBTableTest.php │ ├── LogTest.php │ ├── CollectionTest.php │ └── UserTest.php ├── phpunit.xml.dist └── Tests_Database_TestCase.php ├── db ├── README.md ├── users_table.sql └── users_table_upgrade.sql ├── .travis.yml ├── src ├── LinkedCollection.php ├── Session.php ├── DB.php ├── Cookie.php ├── Hash.php ├── Collection.php ├── DB_Table.php ├── UserBase.php ├── Log.php └── User.php ├── composer.json ├── LICENSE ├── autoload.php └── README.md /demo/README: -------------------------------------------------------------------------------- 1 | UPLOAD the 'demo' directory/folder and navigate your browser to it. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor 3 | composer.lock 4 | demo/core/config.php 5 | demo_old/core/config.php 6 | /index.php -------------------------------------------------------------------------------- /demo/ps/logout.php: -------------------------------------------------------------------------------- 1 | logout(); 5 | 6 | redirect("../"); 7 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | users.xml 4 | ``` -------------------------------------------------------------------------------- /demo/.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | 3 | RewriteCond %{REQUEST_FILENAME} !-l 4 | RewriteCond %{REQUEST_FILENAME} !-f 5 | RewriteCond %{REQUEST_FILENAME} !-d 6 | RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] -------------------------------------------------------------------------------- /db/README.md: -------------------------------------------------------------------------------- 1 | Database Scripts 2 | ====================== 3 | 4 | * users_table.sql - Is the minimum users table requirement for user class to worked as expected 5 | * users_table_upgrade.sql - Is an upgrade script to upgrade the previous format of the users table -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.3 4 | - 5.4 5 | - 5.5 6 | 7 | before_script: 8 | - composer update 9 | - mysql -e 'CREATE DATABASE uflex_ut;' 10 | - mysql uflex_ut < db/users_table.sql 11 | 12 | script: phpunit -c tests/phpunit.xml.dist -------------------------------------------------------------------------------- /demo/static/css/style.css: -------------------------------------------------------------------------------- 1 | .userBox { 2 | max-width: 90px; 3 | margin-bottom: 15px; 4 | } 5 | 6 | .userBox:hover img { 7 | box-shadow: 0 0 5px 7px; 8 | } 9 | 10 | .userBox .label { 11 | overflow: hidden; 12 | } 13 | 14 | form .error { 15 | padding-left: 5px; 16 | } -------------------------------------------------------------------------------- /demo/install/demo.sql: -------------------------------------------------------------------------------- 1 | -- Demo specific fields 2 | ALTER IGNORE TABLE `Users` ADD `website` VARCHAR( 50 ) NOT NULL AFTER `Username` ; 3 | ALTER IGNORE TABLE `Users` ADD `last_name` VARCHAR( 15 ) NOT NULL AFTER `Username` ; 4 | ALTER IGNORE TABLE `Users` ADD `first_name` VARCHAR( 15 ) NOT NULL AFTER `Username` ; 5 | -------------------------------------------------------------------------------- /src/LinkedCollection.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class LinkedCollection extends Collection 12 | { 13 | /** 14 | * Takes the reference of an array 15 | * 16 | * @param array $info 17 | */ 18 | public function __construct(array &$info = array()) 19 | { 20 | $this->_data =& $info; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /demo/page/footer.php: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 | Copyright Test © 2013 - 5 | uFlex Home - 6 | v 7 | 8 |

9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /demo/page/home.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | isSigned()): ?> 4 | My Account 5 | LogOut 6 | 7 | LogIn 8 | Register 9 | Forgot Password? 10 | 11 |
12 |
13 | 14 | -------------------------------------------------------------------------------- /db/users_table.sql: -------------------------------------------------------------------------------- 1 | -- v1.2 2 | CREATE TABLE IF NOT EXISTS `Users` ( 3 | `ID` INT(7) UNSIGNED NOT NULL AUTO_INCREMENT, 4 | `Username` VARCHAR(15) NOT NULL, 5 | `Password` VARCHAR (40) NOT NULL, 6 | `Email` VARCHAR (254) NOT NULL, 7 | `Activated` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', 8 | `Confirmation` CHAR(40) NOT NULL DEFAULT '', 9 | `RegDate` INT(11) UNSIGNED NOT NULL, 10 | `LastLogin` INT(11) UNSIGNED NOT NULL DEFAULT '0', 11 | `GroupID` INT(2) UNSIGNED NOT NULL DEFAULT '1', 12 | PRIMARY KEY (`ID`) 13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 14 | -------------------------------------------------------------------------------- /demo/install/users_table.sql: -------------------------------------------------------------------------------- 1 | -- v1.2 2 | CREATE TABLE IF NOT EXISTS `Users` ( 3 | `ID` INT(7) UNSIGNED NOT NULL AUTO_INCREMENT, 4 | `Username` VARCHAR(15) NOT NULL, 5 | `Password` VARCHAR (40) NOT NULL, 6 | `Email` VARCHAR (100) NOT NULL, 7 | `Activated` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', 8 | `Confirmation` CHAR(40) NOT NULL DEFAULT '', 9 | `RegDate` INT(11) UNSIGNED NOT NULL, 10 | `LastLogin` INT(11) UNSIGNED NOT NULL DEFAULT '0', 11 | `GroupID` INT(2) UNSIGNED NOT NULL DEFAULT '1', 12 | PRIMARY KEY (`ID`) 13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 14 | -------------------------------------------------------------------------------- /tests/fixtures/users.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 1 7 | pablo 8 | fb5a85964f44657d5f6ecbf618f9bcdf788ce56c 9 | pablo@ptejada.com 10 | 1 11 | 12 | 1361145707 13 | 0 14 | 1 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /demo/ps/login.php: -------------------------------------------------------------------------------- 1 | login($input->Username, $input->Password, $input->auto); 14 | 15 | $errMsg = ''; 16 | 17 | if($user->log->hasError()){ 18 | $errMsg = $user->log->getErrors(); 19 | $errMsg = $errMsg[0]; 20 | } 21 | 22 | echo json_encode(array( 23 | 'error' => $user->log->getErrors(), 24 | 'confirm' => "You are now login as $user->Username", 25 | 'form' => $user->log->getFormErrors(), 26 | )); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /tests/src/CookieTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($cookie->add(), 'Cookie was set correctly'); 23 | $output = ob_get_contents(); 24 | ob_end_clean(); 25 | 26 | $this->assertNotEmpty($output); 27 | $this->assertEquals(0, strpos($output, ' 70 | 71 | 72 | -------------------------------------------------------------------------------- /demo/static/js/main.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | $('form').on('submit',function () { 3 | var form = $(this); 4 | var button = $(':submit', form); 5 | 6 | button.button('loading'); 7 | 8 | 9 | $.post(form.attr('action'), form.serialize(), function (response) { 10 | 11 | form.find('.error').remove(); 12 | form.find('.has-error').removeClass('has-error'); 13 | 14 | if (response.form && ! $.isEmptyObject(response.form)) { 15 | // Display errors 16 | for (var name in response.form) { 17 | if (response.form.hasOwnProperty(name)) { 18 | form.find('[name=' + name + ']').focus().parent().addClass('has-error') 19 | .find('input') 20 | .before('' + response.form[name] + ''); 21 | } 22 | } 23 | 24 | // Re-Enables the button 25 | button.button('reset'); 26 | } 27 | else { 28 | // Success 29 | button.replaceWith('
' + response.confirm + '
'); 30 | 31 | if (form.data('success')) { 32 | setTimeout(function () { 33 | window.location = form.data('success'); 34 | }, 4000); 35 | } 36 | 37 | 38 | //form.find('fieldset').attr('disabled','disabled'); 39 | } 40 | }, 'json'); 41 | 42 | return false; 43 | }).on('change', 'input', function () { 44 | 45 | // Clears the error status 46 | 47 | var group = $(this).parents('.form-group:first'); 48 | 49 | group.find('.error').remove(); 50 | group.removeClass('has-error'); 51 | }) 52 | }); 53 | -------------------------------------------------------------------------------- /demo/page/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <?php echo $pageTitle?> | uFlex 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 | 49 |
50 | 51 |
52 | -------------------------------------------------------------------------------- /demo/page/register.php: -------------------------------------------------------------------------------- 1 | isSigned()) redirect("."); 3 | 4 | $d = @$_SESSION["regData"]; 5 | unset($_SESSION["regData"]); 6 | ?> 7 |
8 |
9 |

Register

10 | 11 |
12 | 13 |
14 |
15 | 16 | 17 |
18 | 19 |
20 | 21 | 22 |
23 | 24 |
25 | 26 | 27 |
28 | 29 |
30 | 31 | 32 |
33 | 34 |
35 | 36 | 37 |
38 | 39 |
40 | 41 | 42 |
43 | 44 |
45 | 46 | 47 |
48 | 49 |
50 | 51 | 56 |
57 | 58 | 59 |
60 | 61 |
62 | Login 63 |
64 |
65 | 66 |
67 |
-------------------------------------------------------------------------------- /demo/core/inc/functions.php: -------------------------------------------------------------------------------- 1 | {$txt}"; 15 | } 16 | 17 | 18 | /** 19 | * Prints an array in a readable form 20 | * @param array $a 21 | */ 22 | function aPrint(array $a) 23 | { 24 | echo "
";
25 |     print_r($a);
26 |     echo "
"; 27 | } 28 | 29 | /** 30 | * Redirects the user 31 | * 32 | * @param bool|string $url 33 | * @param int $time 34 | */ 35 | function redirect($url = false, $time = 0) 36 | { 37 | $url = $url ? $url : $_SERVER['HTTP_REFERER']; 38 | 39 | if (!headers_sent()) { 40 | if (!$time) { 41 | header("Location: {$url}"); 42 | } else { 43 | header("refresh: $time; {$url}"); 44 | } 45 | } else { 46 | echo ""; 47 | } 48 | } 49 | 50 | /** 51 | * Gets a content of a GET variable either by name or position in the path 52 | * @param $index 53 | * 54 | * @return mixed 55 | */ 56 | function getVar($index) 57 | { 58 | $tree = explode("/", @$_GET['path']); 59 | $tree = array_filter($tree); 60 | 61 | if (is_int($index)) { 62 | $res = @$tree[$index - 1]; 63 | } else { 64 | $res = @$_GET[$index]; 65 | } 66 | return $res; 67 | } 68 | 69 | /** 70 | * Triggers a 404 error 71 | */ 72 | function send404() 73 | { 74 | if (!headers_sent()) { 75 | header("HTTP/1.0 404 Not Found"); 76 | include("404.html"); 77 | die(); 78 | } else { 79 | redirect("404.html"); 80 | } 81 | } 82 | 83 | /** 84 | * Generates HTML for a gravatar icon 85 | * 86 | * @param string $email 87 | * @param int $size 88 | * 89 | * @return string 90 | */ 91 | function gravatar($email, $size = 80) 92 | { 93 | $hash = md5($email); 94 | return ""; 95 | } -------------------------------------------------------------------------------- /src/Session.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class Session extends LinkedCollection 15 | { 16 | /** @var Log - Log errors and report */ 17 | public $log; 18 | 19 | /** @var null|string Session index to manage */ 20 | protected $namespace; 21 | 22 | /** 23 | * Initialize a session handler by namespace 24 | * 25 | * @param string $namespace - Session namespace to manage 26 | * @param Log $log 27 | */ 28 | public function __construct($namespace = null, Log $log = null) 29 | { 30 | $this->log = $log instanceof Log ? $log : new Log('Session'); 31 | $this->namespace = $namespace; 32 | 33 | // Starts the session if it has not been started yet 34 | if (!isset($_SESSION) && !headers_sent()) { 35 | session_start(); 36 | $this->log->report('Session is been started...'); 37 | } elseif (isset($_SESSION)) { 38 | $this->log->report('Session has already been started'); 39 | } else { 40 | $this->log->error('Session could not be started'); 41 | } 42 | 43 | if (is_null($namespace)) { 44 | // Manage the whole session 45 | parent::__construct($_SESSION); 46 | } else { 47 | if (!isset($_SESSION[$namespace])) { 48 | // Initialize the session namespace if does not exists yet 49 | $_SESSION[$namespace] = array(); 50 | } 51 | 52 | // Link the SESSION namespace to the local $data variable 53 | parent::__construct($_SESSION[$namespace]); 54 | } 55 | 56 | $this->validate(); 57 | } 58 | 59 | /** 60 | * Validates the session 61 | */ 62 | private function validate() 63 | { 64 | /* 65 | * Get the correct IP 66 | */ 67 | $server = new Collection($_SERVER); 68 | $ip = $server->HTTP_X_FORWARDED_FOR; 69 | 70 | if (is_null($ip) && $server->REMOTE_ADDR) 71 | { 72 | $ip = $server->REMOTE_ADDR; 73 | } 74 | 75 | if (!is_null($this->_ip)) { 76 | if ($this->_ip != $ip) { 77 | /* 78 | * Destroy the session in the IP stored in the session is different 79 | * then the IP of the current request 80 | */ 81 | $this->destroy(); 82 | } 83 | } else { 84 | /* 85 | * Save the current request IP in the session 86 | */ 87 | $this->_ip = $ip; 88 | } 89 | } 90 | 91 | /** 92 | * Get current session ID identifier 93 | * 94 | * @return string 95 | */ 96 | public function getID() 97 | { 98 | return session_id(); 99 | } 100 | 101 | /** 102 | * Empty the session namespace 103 | */ 104 | public function destroy() 105 | { 106 | if (is_null($this->namespace)) { 107 | // Destroy the whole session 108 | session_destroy(); 109 | } else { 110 | // Just empty the current session namespace 111 | $_SESSION[$this->namespace] = array(); 112 | unset($_SESSION[$this->namespace]); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/DB.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class DB 12 | { 13 | /** @var Log - Log errors and report */ 14 | public $log; 15 | /** @var string - The server IP or host name */ 16 | private $host = 'localhost'; 17 | /** @var string - The server user to login as */ 18 | private $user = 'root'; 19 | /** @var string - The user password */ 20 | private $password = ''; 21 | /** @var string - The name of the database */ 22 | private $dbName = ''; 23 | /** @var string - Alternative DSN string */ 24 | private $dsn = ''; 25 | /** @var \PDO - The DB connection session */ 26 | private $connection; 27 | 28 | /** 29 | * Initializes the Database object 30 | * 31 | * @param string $hostOrDSN|\PDO - The domain/IP of the DB, the PDO DSN string or PDO connection 32 | * @param string $dbName - The name of the database 33 | */ 34 | public function __construct($hostOrDSN = '', $dbName = '') 35 | { 36 | if (!$dbName) { 37 | if ($hostOrDSN instanceof \PDO) { 38 | // Saves the PDO connection 39 | $this->setConnection($hostOrDSN); 40 | } else { 41 | // add full DSN string 42 | $this->dsn = $hostOrDSN; 43 | } 44 | } else { 45 | // Add the default DB credentials for MySQL 46 | $this->host = $hostOrDSN; 47 | $this->dbName = $dbName; 48 | } 49 | 50 | $this->log = new Log('DB'); 51 | } 52 | 53 | /** 54 | * Get table object 55 | * 56 | * @param $tableName 57 | * 58 | * @return DB_Table 59 | */ 60 | public function getTable($tableName) 61 | { 62 | return new DB_Table($this, $tableName); 63 | } 64 | 65 | /** 66 | * Set the database username 67 | * 68 | * @param string $user 69 | */ 70 | public function setUser($user) 71 | { 72 | $this->user = $user; 73 | } 74 | 75 | /** 76 | * Set the database user password 77 | * 78 | * @param string $password 79 | */ 80 | public function setPassword($password) 81 | { 82 | $this->password = $password; 83 | } 84 | 85 | /** 86 | * Set the name of the Database to connect to 87 | * @param string $dbName 88 | */ 89 | public function setDbName($dbName) 90 | { 91 | $this->dbName = $dbName; 92 | } 93 | 94 | /** 95 | * Get the record of the last inserted record 96 | * 97 | * @return int 98 | */ 99 | public function getLastInsertedID() 100 | { 101 | return $this->getConnection()->lastInsertId(); 102 | } 103 | 104 | /** 105 | * Gets the connecting to the database 106 | * Check if the database connection exists if not connects to the database 107 | * 108 | * @return \PDO | bool 109 | */ 110 | public function getConnection() 111 | { 112 | if (!($this->log instanceof Log)) { 113 | $this->log = new Log('DB'); 114 | } 115 | 116 | // Use cached connection if already connected to server 117 | if ($this->connection instanceof \PDO) { 118 | return $this->connection; 119 | } 120 | 121 | $this->log->report('Connecting to database...'); 122 | 123 | try{ 124 | $this->connection = new \PDO($this->generateDSN(), $this->user, $this->password); 125 | $this->log->report('Connected to database.'); 126 | } catch ( \PDOException $e ){ 127 | $this->log->error('Failed to connect to database, [SQLSTATE] ' . $e->getCode()); 128 | } 129 | 130 | // Check is the connection to server succeed 131 | if ($this->connection instanceof \PDO) { 132 | return $this->connection; 133 | } else { 134 | // There was an error connecting to the DB server 135 | return false; 136 | } 137 | } 138 | 139 | /** 140 | * Generate the DSN string for the connection 141 | * 142 | * @return string 143 | */ 144 | protected function generateDSN() 145 | { 146 | if (!$this->dsn) { 147 | $this->dsn = "mysql:dbname={$this->dbName};host={$this->host}"; 148 | } 149 | 150 | return $this->dsn; 151 | } 152 | 153 | /** 154 | * Set the connection 155 | * @param \PDO $connection 156 | */ 157 | public function setConnection(\PDO $connection) 158 | { 159 | $this->connection = $connection; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /tests/src/LogTest.php: -------------------------------------------------------------------------------- 1 | log = new Log('Test'); 21 | $this->log->channel('chan'.rand()); 22 | } 23 | 24 | public function testDefaultNamespace() 25 | { 26 | $this->assertEquals('Test', $this->log->getNamespace(), 'Default default namespace'); 27 | } 28 | 29 | public function testChangingChannel() 30 | { 31 | $this->log->error('Hello World'); 32 | $this->assertTrue($this->log->hasError(), 'The test default channel has error'); 33 | 34 | // Change channel 35 | $this->log->channel('chan'.rand()); 36 | $this->assertFalse($this->log->hasError(), 'The new channel should have no errors'); 37 | } 38 | 39 | 40 | 41 | public function testPredefinedErrors() 42 | { 43 | // The current channel error stack 44 | $errors = &$this->log->getErrors(); 45 | 46 | // Test pristine state 47 | $this->assertInternalType('array', $errors, 'Initially the error stack should be an array'); 48 | $this->assertEmpty($errors, 'Initially the error stack should also be an empty array'); 49 | 50 | $errorList = array( 51 | 404 => 'Not Found', 52 | 403 => 'Forbidden', 53 | 201 => 'Success', 54 | ); 55 | 56 | // Update the list for the first time 57 | $this->log->updateErrorList($errorList); 58 | 59 | $this->log->error(404); 60 | $this->assertEquals('Not Found', $errors[0], 'Error list first update'); 61 | 62 | // Update the list a second time 63 | $this->log->updateErrorList(array(101=>'Help')); 64 | 65 | $this->log->error(101); 66 | $this->assertEquals('Help', $errors[1], 'Error list second update'); 67 | 68 | // Test updating existing predefined errors 69 | $this->log->updateErrorList(array(404=>'Changed')); 70 | 71 | $this->log->error(404); 72 | $this->assertEquals('Changed', $errors[2], 'Error list third update replaces existing entry'); 73 | 74 | // Test original errors from the first update are still available 75 | $this->log->error(403); 76 | $this->assertEquals('Forbidden', $errors[3], 'Error list testing error from first update last'); 77 | } 78 | 79 | public function testFormErrors() 80 | { 81 | $this->assertFalse($this->log->hasError(), 'No initial errors'); 82 | 83 | $this->log->formError('name', 'The name is not valid'); 84 | $this->assertTrue($this->log->hasError(), 'Error should be present'); 85 | 86 | // The error for field 'name' should be present 87 | $this->assertArrayHasKey('name',$this->log->getFormErrors(), 'The field \'name\' should have an error'); 88 | 89 | $this->assertEquals(2, count($this->log->getReports()), 'There should only be two report entry'); 90 | $this->assertEquals(1, count($this->log->getErrors()), 'There should only be one error entry'); 91 | } 92 | 93 | public function testErrors() 94 | { 95 | $errors = &$this->log->getErrors(); 96 | $reports = &$this->log->getReports(); 97 | 98 | $this->assertEquals(0, count($errors), 'No initial errors'); 99 | $this->assertEquals(1, count($reports), 'Only The initial channel report'); 100 | 101 | for ($i=0; $i<10; $i++) 102 | { 103 | $this->log->error('Hello world ' . $i); 104 | } 105 | 106 | $this->assertEquals(10, count($errors), 'There should be errors'); 107 | $this->assertEquals(11, count($reports), 'There should be reports'); 108 | 109 | $this->assertEquals(count($reports), count($reports), 'There should be the same amount of errors and reports'); 110 | 111 | foreach (array_slice($reports,1) as $report) { 112 | $this->assertRegExp('/Error:/', $report, 'All reports should be errors'); 113 | } 114 | 115 | } 116 | 117 | public function testLinking() 118 | { 119 | $log1 = new Log('1'); 120 | $console = &$log1->getFullConsole(); 121 | $log1->error('Hello World'); 122 | $this->assertNotEmpty($console['errors']); 123 | $this->assertEquals(1, count($console['errors'])); 124 | 125 | $log2 = $log1->newChildLog('2'); 126 | $log2->error('Hello World 2'); 127 | $this->assertEquals(2, count($console['errors'])); 128 | 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/Cookie.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Cookie 13 | { 14 | /** @var Log - Log errors and report */ 15 | public $log; 16 | 17 | /** @var string The name of the cookie */ 18 | private $name; 19 | /** @var string The content of the cookie */ 20 | private $value; 21 | /** @var int The lifetime in days of the cookie */ 22 | private $lifetime; 23 | /** @var string The path of the cookie */ 24 | private $path; 25 | /** @var string The host for which the host belongs to */ 26 | private $host; 27 | 28 | /** 29 | * Initializes a cookie 30 | * 31 | * @param string $name The name of the cookie 32 | * @param string $value _(optional)_ The content of the cookie 33 | * @param int $lifetime _(optional)_ The lifetime in days of the cookie 34 | * @param string $path _(optional)_ The URL path of the cookie 35 | * @param null $host _(optional)_ The host for which the host belongs to 36 | */ 37 | public function __construct($name, $value = '', $lifetime = 15, $path = '/', $host = null) 38 | { 39 | $this->name = $name; 40 | 41 | //Defaults 42 | $this->value = $value; 43 | $this->setLifetime($lifetime); 44 | $this->setPath($path); 45 | if (!$host) { 46 | if (isset($_SERVER['SERVER_NAME'])) { 47 | $this->setHost($_SERVER['SERVER_NAME']); 48 | } 49 | } else { 50 | $this->setHost($host); 51 | } 52 | } 53 | 54 | /** 55 | * Set the lifetime of the cookie 56 | * 57 | * @param int $lifetime - The number of days to last 58 | */ 59 | public function setLifetime($lifetime) 60 | { 61 | $this->lifetime = $lifetime; 62 | } 63 | 64 | /** 65 | * Set the path of the cookie relative to the site domain 66 | * 67 | * @param string $path - The path of the cookie 68 | */ 69 | public function setPath($path) 70 | { 71 | $this->path = $path; 72 | } 73 | 74 | /** 75 | * Set the host to add the cookie for 76 | * 77 | * @param string $host 78 | */ 79 | public function setHost($host) 80 | { 81 | $this->host = $host; 82 | } 83 | 84 | /** 85 | * Sends the cookie to the browser 86 | * 87 | * @return bool 88 | */ 89 | public function add() 90 | { 91 | if (!headers_sent()) { 92 | // Set the cookie via PHP headers 93 | $added = setcookie( 94 | $this->name, 95 | $this->value, 96 | round(time() + 60 * 60 * 24 * $this->lifetime), 97 | $this->path, 98 | $this->host 99 | ); 100 | } else { 101 | //Headers have been sent use JavaScript to set the cookie 102 | echo ""; 112 | $added = true; 113 | } 114 | 115 | return $added; 116 | } 117 | 118 | /** 119 | * Destroys the cookie 120 | * 121 | * @return bool 122 | */ 123 | public function destroy() 124 | { 125 | if (!is_null($this->getValue())) { 126 | if (!headers_sent()) { 127 | return setcookie( 128 | $this->name, 129 | '', 130 | time() - 3600, 131 | $this->path, 132 | $this->host 133 | ); //Deletes Cookie 134 | } else { 135 | return false; 136 | } 137 | } else { 138 | // The cookie does not exists, there is nothing to destroy 139 | return true; 140 | } 141 | } 142 | 143 | /** 144 | * Get the value of the cookie 145 | * 146 | * @return null|mixed - Returns null if the cookie does not exists 147 | */ 148 | public function getValue() 149 | { 150 | if (isset($_COOKIE[$this->name])) { 151 | return $_COOKIE[$this->name]; 152 | } else { 153 | return null; 154 | } 155 | } 156 | 157 | /** 158 | * Sets the value for 159 | * 160 | * @param string $value 161 | */ 162 | public function setValue($value) 163 | { 164 | $this->value = $value; 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/Hash.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Hash 12 | { 13 | /** @var Log - Log errors and report */ 14 | public $log; 15 | 16 | /** 17 | * Required for the integer encoder and decoder functions 18 | * 19 | * @var array 20 | * @access protected 21 | * @ignore 22 | */ 23 | static protected $encoder = array( 24 | // @formatter:off 25 | 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 26 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 27 | 0,2,3,4,5,6,7,8,9 28 | // @formatter:on 29 | ); 30 | 31 | /** 32 | * Initializes the hash object 33 | */ 34 | public function __construct() 35 | { 36 | $this->log = new Log(); 37 | } 38 | 39 | /** 40 | * Generate a password for a user 41 | * 42 | * @param User $user 43 | * @param String $password - Clear text password 44 | * @param bool $generateOld 45 | * 46 | * @return string 47 | */ 48 | public function generateUserPassword(User $user, $password, $generateOld = false) 49 | { 50 | $registrationDate = $user->RegDate; 51 | 52 | $pre = $this->encode($registrationDate); 53 | $pos = substr($registrationDate, 5, 1); 54 | $post = $this->encode($registrationDate * (substr($registrationDate, $pos, 1))); 55 | 56 | $finalString = $pre . $password . $post; 57 | 58 | return $generateOld ? md5($finalString) : sha1($finalString); 59 | } 60 | 61 | /** 62 | * Encodes an integer 63 | * 64 | * @param int $number integer to encode 65 | * 66 | * @return string encoded integer string 67 | */ 68 | static protected function encode($number) 69 | { 70 | $k = self::$encoder; 71 | preg_match_all("/[1-9][0-9]|[0-9]/", $number, $a); 72 | $n = ''; 73 | $o = count($k); 74 | foreach ($a[0] as $i) { 75 | if ($i < $o) { 76 | $n .= $k[$i]; 77 | } else { 78 | $n .= '1' . $k[$i - $o]; 79 | } 80 | } 81 | return $n; 82 | } 83 | 84 | /** 85 | * Generates a unique hash 86 | * 87 | * @param int $uid user id 88 | * @param bool|string $hash optional hash to implement 89 | * 90 | * @return string 91 | */ 92 | static public function generate($uid = 0, $hash = false) 93 | { 94 | if ($uid) { 95 | $e_uid = self::encode($uid); 96 | $e_uid_length = strlen($e_uid); 97 | $e_uid_length = str_pad($e_uid_length, 2, 0, STR_PAD_LEFT); 98 | $e_uid_pos = rand(10, 32 - $e_uid_length - 1); 99 | 100 | if (!$hash) { 101 | $hash = sha1(uniqid(rand(), true)); 102 | } 103 | 104 | $code = $e_uid_pos . $e_uid_length; 105 | $code .= substr($hash, 0, $e_uid_pos - strlen($code)); 106 | $code .= $e_uid; 107 | $code .= substr($hash, strlen($code)); 108 | 109 | return $code; 110 | } else { 111 | return sha1(uniqid(rand(), true)); 112 | } 113 | } 114 | 115 | /** 116 | * Checks and validates a confirmation hash 117 | * 118 | * @param string $hash hashed string to check 119 | * 120 | * @return array 121 | */ 122 | static public function examine($hash) 123 | { 124 | if (strlen($hash) == 40 && preg_match("/^[0-9]{4}/", $hash)) { 125 | 126 | $e_uid_pos = substr($hash, 0, 2); 127 | $e_uid_length = substr($hash, 2, 2); 128 | $e_uid = substr($hash, $e_uid_pos, $e_uid_length); 129 | 130 | $uid = self::decode($e_uid); 131 | 132 | preg_match('/^([0-9]{4})(.{2,' . ($e_uid_pos - 4) . '})(' . $e_uid . ')/', $hash, $excerpt); 133 | $partial = $excerpt[2]; 134 | 135 | return array($uid, $partial); 136 | } 137 | else 138 | { 139 | /* 140 | * The hash is not valid 141 | */ 142 | return array(false, false); 143 | } 144 | } 145 | 146 | /** 147 | * Decodes a string into an integer 148 | * 149 | * @param string $number string to decode into an integer 150 | * 151 | * @return int 152 | */ 153 | static public function decode($number) 154 | { 155 | $k = self::$encoder; 156 | preg_match_all('/[1][a-zA-Z]|[2-9]|[a-zA-Z]|[0]/', $number, $a); 157 | $n = ''; 158 | $o = count($k); 159 | foreach ($a[0] as $i) { 160 | $f = preg_match('/1([a-zA-Z])/', $i, $v); 161 | if ($f == true) { 162 | $i = $o + array_search($v[1], $k); 163 | } else { 164 | $i = array_search($i, $k); 165 | } 166 | $n .= $i; 167 | } 168 | return $n; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /demo/install/index.php: -------------------------------------------------------------------------------- 1 | true, 14 | 'form' => array('error' => ''), 15 | ); 16 | 17 | try{ 18 | $dbh = new PDO("mysql:host=$db_host", $db_user, $db_pass); 19 | } catch ( PDOException $e ){ 20 | $response['form']['error'] = "Unable to connect to {$db_host} with username {$db_user} and password {$db_pass}"; 21 | $dbh = false; 22 | } 23 | 24 | $confirm = array(); 25 | 26 | if ($dbh) { 27 | if ($dbh->query("USE {$db_name};")) { 28 | $dbAccess = true; 29 | $confirm[] = "Using Existing Database!"; 30 | } else { 31 | if ($dbh->exec("CREATE DATABASE {$db_name}")) { 32 | // Database created 33 | $dbAccess = true; 34 | $confirm[] = "Database Created OK!"; 35 | } else { 36 | $dbAccess = false; 37 | $response['form']['error'] = "Database {$db_name} does not exists and we are unable to created. Make sure the user {$db_user} has permission to create the database or manually create it."; 38 | } 39 | } 40 | 41 | } 42 | 43 | if ($dbAccess) { 44 | //Import database 45 | $sql = file_get_contents("users_table.sql"); 46 | $import1 = $dbh->query($sql); 47 | 48 | $sql = file_get_contents("demo.sql"); 49 | $import2 = $dbh->query($sql); 50 | 51 | //Create Configuration file 52 | if ($import1) { 53 | $confirm[] = "Database Populated OK!"; 54 | 55 | $config = file_get_contents("config.tpl"); 56 | 57 | foreach ($_POST as $tag => $val) { 58 | $config = str_replace("#!db_" . $tag, $val, $config); 59 | } 60 | 61 | $file = fopen("../core/config.php", "w+"); 62 | 63 | if (fwrite($file, $config) == false) { 64 | $response['form']['error'] = "Could not generate config.php"; 65 | } else { 66 | $confirm[] = "Configuration File generated OK!"; 67 | $confirm[] = "CLICK HERE YOU ARE DONE!"; 68 | } 69 | 70 | fclose($file); 71 | } else { 72 | $response['form']['error'] = "Can not create tables in the {$db_name} database. Double check the user permission."; 73 | } 74 | } 75 | 76 | $response['error'] = $response['form']['error'] ? true : false; 77 | 78 | $response['confirm'] = '