From 327b937b67255e5aef807e70aff412a49c99999b Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 16:25:50 +0530 Subject: [PATCH 01/41] added namespace autoloader --- .gitignore | 1 + composer.json | 15 +++++++++++++++ composer.lock | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49ce3c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6f27eee --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "PDO Library", + "description": "The PDO 5.6 Framework.", + "keywords": ["Library", "PHP Library"], + "license": "MIT", + "type": "project", + "require": { + "php": ">=5.5.9" + }, + "autoload": { + "psr-4": { + "App\\": "app/" + } + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..ba44e52 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "bba7759a56d368604e6c6d254203c7f2", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.5.9" + }, + "platform-dev": [] +} From 967c3f82c229f5f0c514a21ea4658fa8adb012d5 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 16:30:09 +0530 Subject: [PATCH 02/41] PSR-4 Autoloader Added --- lib/Auth.class.php | 2 +- lib/DB.class.php | 2 +- lib/Encryption.class.php | 2 +- lib/Image.class.php | 2 +- lib/Pagination.class.php | 1 + lib/Password.class.php | 2 +- lib/config.php | 34 +++++++++++++++++++--------------- lib/functions.php | 2 +- 8 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/Auth.class.php b/lib/Auth.class.php index 9d22bbd..94468de 100644 --- a/lib/Auth.class.php +++ b/lib/Auth.class.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/DB.class.php b/lib/DB.class.php index 73af4fd..85c3ec6 100644 --- a/lib/DB.class.php +++ b/lib/DB.class.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/Encryption.class.php b/lib/Encryption.class.php index 15be961..d5792ad 100644 --- a/lib/Encryption.class.php +++ b/lib/Encryption.class.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/Image.class.php b/lib/Image.class.php index 7c1cd1c..3bd097e 100644 --- a/lib/Image.class.php +++ b/lib/Image.class.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/Pagination.class.php b/lib/Pagination.class.php index 8d5b4b6..3878db4 100644 --- a/lib/Pagination.class.php +++ b/lib/Pagination.class.php @@ -1,4 +1,5 @@ diff --git a/lib/Password.class.php b/lib/Password.class.php index 47a5bd1..57b3c31 100644 --- a/lib/Password.class.php +++ b/lib/Password.class.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/config.php b/lib/config.php index 5b2ffa0..5f743e9 100644 --- a/lib/config.php +++ b/lib/config.php @@ -46,22 +46,26 @@ * ========== I N C L U D E N E C E S S A R Y F I L E S =========== * */ +if( file_exists(BASEPATH . 'vendor') ){ + require_once BASEPATH ."vendor/autoload.php"; +} +else{ + spl_autoload_register( function ($class) { + if( file_exists(BASEPATH . 'lib' . DS . $class . '.class.php') ){ + require_once BASEPATH . 'lib' . DS . $class . '.class.php'; + } + } ); -spl_autoload_register( function ($class) { - if( file_exists(BASEPATH . 'lib' . DS . $class . '.class.php') ){ - require_once BASEPATH . 'lib' . DS . $class . '.class.php'; - } -} ); - -/* - * L O A D I N G T A B L E F I L E S - */ + /* + * L O A D I N G T A B L E F I L E S + */ -spl_autoload_register( function ($class) { - if( file_exists(BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php') ){ - require_once BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php'; - } -} ); -require_once BASEPATH . "lib" . DS . "functions.php"; + spl_autoload_register( function ($class) { + if( file_exists(BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php') ){ + require_once BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php'; + } + } ); + require_once BASEPATH . "lib" . DS . "functions.php"; +} /** =========== F I L E L O A D I N G E N D S H E R E =========== **/ \ No newline at end of file diff --git a/lib/functions.php b/lib/functions.php index 6650502..363266e 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -1,5 +1,5 @@ * @package : Admin Panel From 0cf5e0ed2317798db8003f9c2a4acd12ea54ff87 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 16:43:18 +0530 Subject: [PATCH 03/41] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 49ce3c1..8b7ef35 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/vendor \ No newline at end of file +/vendor +composer.lock From 99621572514b0b2979ae37013510cf52437f277e Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 16:44:10 +0530 Subject: [PATCH 04/41] Delete composer.lock --- composer.lock | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 composer.lock diff --git a/composer.lock b/composer.lock deleted file mode 100644 index ba44e52..0000000 --- a/composer.lock +++ /dev/null @@ -1,19 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "hash": "bba7759a56d368604e6c6d254203c7f2", - "packages": [], - "packages-dev": [], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": { - "php": ">=5.5.9" - }, - "platform-dev": [] -} From 7b73f9c080f6a7b76a60fdf6a5c17e5fc6e4a5fe Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 16:57:43 +0530 Subject: [PATCH 05/41] Modified Files - Akhtar --- lib/Auth.class.php | 2 ++ lib/DB.class.php | 6 +++--- lib/tables/AdminUser.class.php | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/Auth.class.php b/lib/Auth.class.php index 94468de..3b1c0a9 100644 --- a/lib/Auth.class.php +++ b/lib/Auth.class.php @@ -1,5 +1,7 @@ * @package : Admin Panel diff --git a/lib/DB.class.php b/lib/DB.class.php index 85c3ec6..497924b 100644 --- a/lib/DB.class.php +++ b/lib/DB.class.php @@ -14,12 +14,12 @@ class DB public $offset; // @integer public $limit; // @integer public $orderBy; // @Array as ['ID', 'ASC'] - public $where; // WHERE Query + public $where; // WHERE Query public $whereVal; public $arrValues = array(); // @Array to bind with @params - /*public $innerJoin; // Array as ['table'=>{tabl_name}, 'ON'=>'id'] - public $outerJoin; + public $innerJoin; // Array as ['table'=>{tabl_name}, 'ON'=>'id'] + /*public $outerJoin; public $leftJoin; public $rightJoin;*/ // Will use later. diff --git a/lib/tables/AdminUser.class.php b/lib/tables/AdminUser.class.php index b93b6df..138ff39 100644 --- a/lib/tables/AdminUser.class.php +++ b/lib/tables/AdminUser.class.php @@ -1,5 +1,7 @@ * @package : Admin Panel From 0291c2681427e8cc4f56b58ca5cea4d779a13346 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 17:25:40 +0530 Subject: [PATCH 06/41] cganged - Akhtar --- lib/Auth.class.php | 4 ++-- lib/tables/AdminUser.class.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Auth.class.php b/lib/Auth.class.php index 3b1c0a9..84d7eb3 100644 --- a/lib/Auth.class.php +++ b/lib/Auth.class.php @@ -1,7 +1,7 @@ * @package : Admin Panel diff --git a/lib/tables/AdminUser.class.php b/lib/tables/AdminUser.class.php index 138ff39..e642a95 100644 --- a/lib/tables/AdminUser.class.php +++ b/lib/tables/AdminUser.class.php @@ -1,7 +1,7 @@ * @package : Admin Panel From 652c9dd137312c171499661d83835105d831c4f1 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 17:38:34 +0530 Subject: [PATCH 07/41] Changed config - Akhtar --- composer.json | 2 +- lib/config.php | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 6f27eee..eaea0f3 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "type": "project", "require": { - "php": ">=5.5.9" + "php": ">=5.4" }, "autoload": { "psr-4": { diff --git a/lib/config.php b/lib/config.php index 5f743e9..34f0eb6 100644 --- a/lib/config.php +++ b/lib/config.php @@ -14,18 +14,18 @@ } const HOSTNAME = 'localhost'; -const DBNAME = 'admin_panel'; -const USERNAME = 'root'; -const PASSWORD = '467936'; -const DS = DIRECTORY_SEPARATOR; +const DBNAME = 'YourDBName'; +const USERNAME = 'YourUserName'; +const PASSWORD = 'YourPassword'; +const DS = DIRECTORY_SEPARATOR; const ENVIRONMENT = 'development'; // OR production => live if( ENVIRONMENT == 'development' ){ error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE); } else{ - error_reporting(-1); + error_reporting(0); } $basepath = realpath( dirname( dirname(__FILE__) ) ); @@ -55,11 +55,9 @@ require_once BASEPATH . 'lib' . DS . $class . '.class.php'; } } ); - /* * L O A D I N G T A B L E F I L E S */ - spl_autoload_register( function ($class) { if( file_exists(BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php') ){ require_once BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php'; From e1fecae0e61fd95660d80ed769173eb2b79e2a6e Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 17:41:37 +0530 Subject: [PATCH 08/41] changed - Akhtar --- lib/Auth.class.php | 2 +- lib/tables/AdminUser.class.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Auth.class.php b/lib/Auth.class.php index 84d7eb3..c59ad7a 100644 --- a/lib/Auth.class.php +++ b/lib/Auth.class.php @@ -1,6 +1,6 @@ diff --git a/lib/tables/AdminUser.class.php b/lib/tables/AdminUser.class.php index e642a95..26f91cf 100644 --- a/lib/tables/AdminUser.class.php +++ b/lib/tables/AdminUser.class.php @@ -1,6 +1,7 @@ From 3da1952181d49ceb324fdeb47399fcb628bdf851 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 29 Oct 2015 17:45:41 +0530 Subject: [PATCH 09/41] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4022d1..407c154 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # PDO-Library -Simple PHP library with PDO and PHP 5.6.0 +Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 autoloading is used. To use this library, you need to only include config.php file from 'lib' directory. From 07b7356266538d59d5956755b36b9037417eb4da Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 30 Oct 2015 12:59:27 +0530 Subject: [PATCH 10/41] New features added - Akhtar --- composer.json | 4 +- lib/Auth.class.php | 41 ----- lib/DB.class.php | 252 -------------------------- lib/Encryption.class.php | 23 --- lib/Image.class.php | 201 --------------------- lib/Pagination.class.php | 89 ---------- lib/Password.class.php | 52 ------ lib/config.php | 138 ++++++++++++--- lib/functions.php | 313 ++++++++++++++++----------------- lib/tables/AdminUser.class.php | 125 ------------- 10 files changed, 266 insertions(+), 972 deletions(-) delete mode 100644 lib/Auth.class.php delete mode 100644 lib/DB.class.php delete mode 100644 lib/Encryption.class.php delete mode 100644 lib/Image.class.php delete mode 100644 lib/Pagination.class.php delete mode 100644 lib/Password.class.php delete mode 100644 lib/tables/AdminUser.class.php diff --git a/composer.json b/composer.json index eaea0f3..6d9b418 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,9 @@ "require": { "php": ">=5.4" }, - "autoload": { + "autoload": { "psr-4": { - "App\\": "app/" + "App\\": "lib/" } } } \ No newline at end of file diff --git a/lib/Auth.class.php b/lib/Auth.class.php deleted file mode 100644 index c59ad7a..0000000 --- a/lib/Auth.class.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -public abstract class Auth -{ - //protected function initialize(); - public function login(){ - $pass = new Password( $this->password ); - $db = new DB(); - $db->where( ['email' => $this->email, 'password' => $this->password] ); - $data = $db->getRow($this->table); - if( count($res) > 0 && $pass->verifyPassword() ){ - if( $pass->needRehash() ){ - $newHash = $pass->reHashPassword(); - return $newHash; - $db->where( ['email' => $this->email, 'password' => $this->password] ); - $res = $db->update($this->table,['hash' => $newHash]); - } - - /************ SET SESSION VARIABLES HERE **************/ - - $_SESSEION['logged'] = TRUE; - $_SESSEION['userid'] = $data->id; - $_SESSEION['username'] = $data->username ? $data->username : ""; - - /****************** END SESSION SETTINGS **************/ - } - } - public function logout(){ - unset( $_SESSEION ); - header("Location:".BASEURL."?action=logout"); - } - -} \ No newline at end of file diff --git a/lib/DB.class.php b/lib/DB.class.php deleted file mode 100644 index 497924b..0000000 --- a/lib/DB.class.php +++ /dev/null @@ -1,252 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -class DB -{ - private $dbh; - public $query; - public $fieldList; // @Array in key=>pair format - public $offset; // @integer - public $limit; // @integer - public $orderBy; // @Array as ['ID', 'ASC'] - public $where; // WHERE Query - public $whereVal; - public $arrValues = array(); // @Array to bind with @params - - public $innerJoin; // Array as ['table'=>{tabl_name}, 'ON'=>'id'] - /*public $outerJoin; - public $leftJoin; - public $rightJoin;*/ // Will use later. - - function __construct(){ - $dsn = "mysql:host=".HOSTNAME.";dbname=".DBNAME; - try - { - $this->dbh = new PDO($dsn, USERNAME, PASSWORD); - if( ENVIRONMENT == 'development' ){ - $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); - } - } - catch(PDO_Exception $e){ - echo $e->getMessage(); - } - } - - /* - * - * ============= D A T A B A S E R E L A T E D F U N C T I O N S ============ - * - */ - - public function getResult($table, $fields) - { - if( empty($table) ) - return; - - $field = is_array($fields) ? implode(', ', $fields) : "*"; - - $sql = "SELECT ".$field." FROM `".$table."` "; - $this->query = self::buildQuery($sql); - - $stmt = $this->dbh->prepare($this->query); - $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); - $data = $stmt->fetchAll(PDO::FETCH_OBJ); - //print_r($data); - self::reset(); - return $data; - } - - public function getRow($table, $fields) - { - if( empty($table) ) - return; - - $field = is_array($fields) ? implode(', ', $fields) : "*"; - - $sql = "SELECT ".$field." FROM `".$table."` "; - $this->query = self::buildQuery($sql); - - $stmt = $this->dbh->prepare($this->query); - $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); - $data = $stmt->fetch(PDO::FETCH_OBJ); - - self::reset(); - return $data; - } - - /************************ E N D ********************************/ - public function insert($table, $fieldVal) - { - /* - * $fieldVal is an associative array containing as - * $key=>$val , Where key = column name of table - */ - - if( !is_array($fieldVal) || empty($table) ) - return; - - $arrValues = array(); - $fields = array(); - $sql = "INSERT INTO `".$table."` SET "; - foreach( $fieldVal as $key=>$val){ - $fields[] = $key."=?"; - $arrValues[] = $val; - } - - $sql .= implode(", ", $fields); - $stmt = $this->dbh->prepare($sql); - return $stmt->execute($arrValues) ? $this->dbh->lastInsertId() : FALSE; - } - - public function update($table, $fieldVal) - { - /* - * $fieldVal is an associative array containing as - * $key=>$val , Where key = column name of table - */ - if( !is_array($fieldVal) || empty($table) ) - return; - - $fields = array(); - $sql = "UPDATE `".$table."` SET "; - foreach( $fieldVal as $key=>$val){ - $fields[] = $key."=?"; - $this->arrValues[] = $val; - } - - $sql .= implode(", ", $fields); - $this->query = self::buildQuery($sql); - - $stmt = $this->dbh->prepare($this->query); - $res = $stmt->execute($this->arrValues); - - self::reset(); - return $res; - } - - public function delete( $table ) - { - if( empty($table) ) - return; - - $sql = "DELETE FROM `".$table."`"; - $this->query = self::buildQuery($sql); - - $stmt = $this->dbh->prepare($this->query); - $res = $stmt->execute($this->arrValues); - - self::reset(); - return $res; - } - - public function getCount( $table, $field ) - { - if( empty($table) ) - return; - - if( empty($field) ) - $field = "*"; - - $sql = "SELECT COUNT(".$field.") FROM `".$table."` "; - $this->query = self::buildQuery($sql); - - $stmt = $this->dbh->prepare($this->query); - $stmt->execute($this->arrValues); - $res = $stmt->fetch(PDO::FETCH_NUM); - - self::reset(); - return $res[0]; - } - - public function where( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr = "AND" ){ - if( empty($this->where) ){ - $this->where = "WHERE "; - } - else{ - $this->where .= " ".$beforeOpr." "; - } - $param = array(); - if( count($whereQuery) >= 1 ){ - foreach($whereQuery as $key => $val){ - $param[] = $key."=?"; - $this->whereVal[] = $val; - } - } - - $this->where .= implode(" ".$afterOpr." ", $param); - } - - public function likeWhere( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr = "AND" ){ - if( empty($this->where) ){ - $this->where = "WHERE "; - } - else{ - $this->where .= " ".$beforeOpr." "; - } - $param = array(); - if( count($whereQuery) >= 1 ){ - foreach($whereQuery as $key => $val){ - $param[] = $key." LIKE '%?%'"; - $this->whereVal[] = $val; - } - } - - $this->where .= implode(" ".$afterOpr." ", $param); - } - public function inWhere( $whereQuery = array(), $beforeOpr = 'AND' ){ - if( empty($this->where) ){ - $this->where = "WHERE "; - } - else{ - $this->where .= " ".$beforeOpr." "; - } - //$ar = [ 'id' => [1,2,3,4,5] ]; - $param = array(); - if( count($whereQuery) == 1 ){ - foreach($whereQuery as $key => $val){ - $param[] = $key." IN '%?%'"; - $this->whereVal[] = "(".implode(',', $val).")"; - } - } - - $this->where .= implode(" ".$afterOpr." ", $param); - } - - private function buildQuery( $sql ) - { - if( empty($sql) ) - return FALSE; - - $query = $sql; - if( $this->where ){ - $query .= " ".$this->where." "; - $this->arrValues = array_merge($this->arrValues, $this->whereVal); - } - $query .= !empty($this->orderBy) ? implode(' ', $this->orderBy)." " : ""; - $query .= !empty($this->limit) ? " LIMIT ".$this->limit." " : ""; - $query .= !empty($this->offset) ? " OFFSET ".$this->offset." " : ""; - - showQuery($query,$this->arrValues); - return $query; - } - - private function reset() - { - unset($this->query); - unset($this->fieldList); - unset($this->arrValues); - unset($this->limit); - unset($this->orderBy); - unset($this->where); - unset($this->whereVal); - unset($this->innerJoin); - /*unset($this->outerJoin); - unset($this->leftJoin); - unset($this->rightJoin);*/ - } -} diff --git a/lib/Encryption.class.php b/lib/Encryption.class.php deleted file mode 100644 index d5792ad..0000000 --- a/lib/Encryption.class.php +++ /dev/null @@ -1,23 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -final class Encryption { - private $key; - - public function __construct($key) { - $this->key = hash('sha256', $key, true); - } - - public function encrypt($value) { - return strtr(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), $value, MCRYPT_MODE_ECB)), '+/=', '-_,'); - } - - public function decrypt($value) { - return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), base64_decode(strtr($value, '-_,', '+/=')), MCRYPT_MODE_ECB)); - } -} \ No newline at end of file diff --git a/lib/Image.class.php b/lib/Image.class.php deleted file mode 100644 index 3bd097e..0000000 --- a/lib/Image.class.php +++ /dev/null @@ -1,201 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -class Image { - private $file; - private $image; - private $info; - - public function __construct($file) { - if (file_exists($file)) { - $this->file = $file; - - $info = getimagesize($file); - - $this->info = array( - 'width' => $info[0], - 'height' => $info[1], - 'bits' => isset($info['bits']) ? $info['bits'] : '', - 'mime' => isset($info['mime']) ? $info['mime'] : '' - ); - - $this->image = $this->create($file); - } else { - exit('Error: Could not load image ' . $file . '!'); - } - } - - private function create($image) { - $mime = $this->info['mime']; - - if ($mime == 'image/gif') { - return imagecreatefromgif ($image); - } elseif ($mime == 'image/png') { - return imagecreatefrompng($image); - } elseif ($mime == 'image/jpeg') { - return imagecreatefromjpeg($image); - } - } - - public function save($file, $quality = 90) { - $info = pathinfo($file); - - $extension = strtolower($info['extension']); - - if (is_resource($this->image)) { - if ($extension == 'jpeg' || $extension == 'jpg') { - imagejpeg($this->image, $file, $quality); - } elseif ($extension == 'png') { - imagepng($this->image, $file); - } elseif ($extension == 'gif') { - imagegif ($this->image, $file); - } - - imagedestroy($this->image); - } - } - - public function resize($width = 0, $height = 0, $default = '') { - if (!$this->info['width'] || !$this->info['height']) { - return; - } - - $xpos = 0; - $ypos = 0; - $scale = 1; - - $scale_w = $width / $this->info['width']; - $scale_h = $height / $this->info['height']; - - if ($default == 'w') { - $scale = $scale_w; - } elseif ($default == 'h') { - $scale = $scale_h; - } else { - $scale = min($scale_w, $scale_h); - } - - if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { - return; - } - - $new_width = (int)($this->info['width'] * $scale); - $new_height = (int)($this->info['height'] * $scale); - $xpos = (int)(($width - $new_width) / 2); - $ypos = (int)(($height - $new_height) / 2); - - $image_old = $this->image; - $this->image = imagecreatetruecolor($width, $height); - - if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { - imagealphablending($this->image, false); - imagesavealpha($this->image, true); - $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); - imagecolortransparent($this->image, $background); - } else { - $background = imagecolorallocate($this->image, 255, 255, 255); - } - - imagefilledrectangle($this->image, 0, 0, $width, $height, $background); - - imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); - imagedestroy($image_old); - - $this->info['width'] = $width; - $this->info['height'] = $height; - } - - public function watermark($file, $position = 'bottomright') { - $watermark = $this->create($file); - - $watermark_width = imagesx($watermark); - $watermark_height = imagesy($watermark); - - switch($position) { - case 'topleft': - $watermark_pos_x = 0; - $watermark_pos_y = 0; - break; - case 'topright': - $watermark_pos_x = $this->info['width'] - $watermark_width; - $watermark_pos_y = 0; - break; - case 'bottomleft': - $watermark_pos_x = 0; - $watermark_pos_y = $this->info['height'] - $watermark_height; - break; - case 'bottomright': - $watermark_pos_x = $this->info['width'] - $watermark_width; - $watermark_pos_y = $this->info['height'] - $watermark_height; - break; - } - - imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40); - - imagedestroy($watermark); - } - - public function crop($top_x, $top_y, $bottom_x, $bottom_y) { - $image_old = $this->image; - $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); - - imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']); - imagedestroy($image_old); - - $this->info['width'] = $bottom_x - $top_x; - $this->info['height'] = $bottom_y - $top_y; - } - - public function rotate($degree, $color = 'FFFFFF') { - $rgb = $this->html2rgb($color); - - $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); - - $this->info['width'] = imagesx($this->image); - $this->info['height'] = imagesy($this->image); - } - - private function filter($filter) { - imagefilter($this->image, $filter); - } - - private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') { - $rgb = $this->html2rgb($color); - - imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); - } - - private function merge($file, $x = 0, $y = 0, $opacity = 100) { - $merge = $this->create($file); - - $merge_width = imagesx($merge); - $merge_height = imagesy($merge); - - imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity); - } - - private function html2rgb($color) { - if ($color[0] == '#') { - $color = substr($color, 1); - } - - if (strlen($color) == 6) { - list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); - } elseif (strlen($color) == 3) { - list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); - } else { - return false; - } - - $r = hexdec($r); - $g = hexdec($g); - $b = hexdec($b); - - return array($r, $g, $b); - } -} diff --git a/lib/Pagination.class.php b/lib/Pagination.class.php deleted file mode 100644 index 3878db4..0000000 --- a/lib/Pagination.class.php +++ /dev/null @@ -1,89 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -class Pagination { - public $total = 0; - public $page = 1; - public $limit = 10; - public $num_links = 8; - public $url = ''; - public $text_first = '|<'; - public $text_last = '>|'; - public $text_next = '>'; - public $text_prev = '<'; - - public function render() { - $total = $this->total; - - if ($this->page < 1) { - $page = 1; - } else { - $page = $this->page; - } - - if (!(int)$this->limit) { - $limit = 10; - } else { - $limit = $this->limit; - } - - $num_links = $this->num_links; - $num_pages = ceil($total / $limit); - - $this->url = str_replace('%7Bpage%7D', '{page}', $this->url); - - $output = '
    '; - - if ($page > 1) { - $output .= '
  • ' . $this->text_first . '
  • '; - $output .= '
  • ' . $this->text_prev . '
  • '; - } - - if ($num_pages > 1) { - if ($num_pages <= $num_links) { - $start = 1; - $end = $num_pages; - } else { - $start = $page - floor($num_links / 2); - $end = $page + floor($num_links / 2); - - if ($start < 1) { - $end += abs($start) + 1; - $start = 1; - } - - if ($end > $num_pages) { - $start -= ($end - $num_pages); - $end = $num_pages; - } - } - - for ($i = $start; $i <= $end; $i++) { - if ($page == $i) { - $output .= '
  • ' . $i . '
  • '; - } else { - $output .= '
  • ' . $i . '
  • '; - } - } - } - - if ($page < $num_pages) { - $output .= '
  • ' . $this->text_next . '
  • '; - $output .= '
  • ' . $this->text_last . '
  • '; - } - - $output .= '
'; - - if ($num_pages > 1) { - return $output; - } else { - return ''; - } - } -} \ No newline at end of file diff --git a/lib/Password.class.php b/lib/Password.class.php deleted file mode 100644 index 57b3c31..0000000 --- a/lib/Password.class.php +++ /dev/null @@ -1,52 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -final class Password -{ - public $hash; - public $password; - public $options; - - public function __construct( $password = '' ){ - if($password == ''){ - return; - } - $this->password = $password; - $this->options = ['salt' => self::uniqueSalt(), 'cost' => 10]; - if($password != ''){ - $this->hash = self::hashPassword(); - } - } - - public final function uniqueSalt() { - return substr(sha1(uniqid(mt_rand(), true)), 0, 22); - } - - public function hashPassword(){ - $hash = password_hash($this->password, PASSWORD_DEFAULT, $this->options); - return $hash; - } - public function needRehash(){ - if( password_needs_rehash($this->hash, PASSWORD_DEFAULT, $this->options) ){ - return TRUE; - } - } - public function reHashPassword(){ - $hash = password_hash($this->password, PASSWORD_DEFAULT, $this->options); - return $hash; - } - - public function getInfo(){ - $info = password_get_info($this->hash); - return $info; - } - - public function verifyPassword(){ - return password_verify($this->password, $this->hash) ? TRUE : FALSE; - } -} \ No newline at end of file diff --git a/lib/config.php b/lib/config.php index 34f0eb6..60f4653 100644 --- a/lib/config.php +++ b/lib/config.php @@ -9,14 +9,10 @@ ob_start(); session_start(); -if (version_compare(phpversion(), '5.4.0', '<') == true) { - exit('PHP 5.4+ Required'); -} - const HOSTNAME = 'localhost'; -const DBNAME = 'YourDBName'; -const USERNAME = 'YourUserName'; -const PASSWORD = 'YourPassword'; +const DBNAME = 'akhtar_test'; +const USERNAME = 'root'; +const PASSWORD = ''; const DS = DIRECTORY_SEPARATOR; const ENVIRONMENT = 'development'; // OR production => live @@ -39,6 +35,113 @@ define( 'BASEURL', $baseurl ); define( 'CURRENT_PAGE', $curPage ); +$mimes = array( 'hqx' => 'application/mac-binhex40', + 'cpt' => 'application/mac-compactpro', + 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), + 'bin' => 'application/macbinary', + 'dms' => 'application/octet-stream', + 'lha' => 'application/octet-stream', + 'lzh' => 'application/octet-stream', + 'exe' => array('application/octet-stream', 'application/x-msdownload'), + 'class' => 'application/octet-stream', + 'psd' => 'application/x-photoshop', + 'so' => 'application/octet-stream', + 'sea' => 'application/octet-stream', + 'dll' => 'application/octet-stream', + 'oda' => 'application/oda', + 'pdf' => array('application/pdf', 'application/x-download'), + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', + 'smi' => 'application/smil', + 'smil' => 'application/smil', + 'mif' => 'application/vnd.mif', + 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), + 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'), + 'wbxml' => 'application/wbxml', + 'wmlc' => 'application/wmlc', + 'dcr' => 'application/x-director', + 'dir' => 'application/x-director', + 'dxr' => 'application/x-director', + 'dvi' => 'application/x-dvi', + 'gtar' => 'application/x-gtar', + 'gz' => 'application/x-gzip', + 'php' => 'application/x-httpd-php', + 'php4' => 'application/x-httpd-php', + 'php3' => 'application/x-httpd-php', + 'phtml' => 'application/x-httpd-php', + 'phps' => 'application/x-httpd-php-source', + 'js' => 'application/x-javascript', + 'swf' => 'application/x-shockwave-flash', + 'sit' => 'application/x-stuffit', + 'tar' => 'application/x-tar', + 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), + 'xhtml' => 'application/xhtml+xml', + 'xht' => 'application/xhtml+xml', + 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), + 'mid' => 'audio/midi', + 'midi' => 'audio/midi', + 'mpga' => 'audio/mpeg', + 'mp2' => 'audio/mpeg', + 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), + 'aif' => 'audio/x-aiff', + 'aiff' => 'audio/x-aiff', + 'aifc' => 'audio/x-aiff', + 'ram' => 'audio/x-pn-realaudio', + 'rm' => 'audio/x-pn-realaudio', + 'rpm' => 'audio/x-pn-realaudio-plugin', + 'ra' => 'audio/x-realaudio', + 'rv' => 'video/vnd.rn-realvideo', + 'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'), + 'bmp' => array('image/bmp', 'image/x-windows-bmp'), + 'gif' => 'image/gif', + 'jpeg' => array('image/jpeg', 'image/pjpeg'), + 'jpg' => array('image/jpeg', 'image/pjpeg'), + 'jpe' => array('image/jpeg', 'image/pjpeg'), + 'png' => array('image/png', 'image/x-png'), + 'tiff' => 'image/tiff', + 'tif' => 'image/tiff', + 'css' => 'text/css', + 'html' => 'text/html', + 'htm' => 'text/html', + 'shtml' => 'text/html', + 'txt' => 'text/plain', + 'text' => 'text/plain', + 'log' => array('text/plain', 'text/x-log'), + 'rtx' => 'text/richtext', + 'rtf' => 'text/rtf', + 'xml' => 'text/xml', + 'xsl' => 'text/xml', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpe' => 'video/mpeg', + 'qt' => 'video/quicktime', + 'mov' => 'video/quicktime', + 'avi' => 'video/x-msvideo', + 'movie' => 'video/x-sgi-movie', + 'doc' => 'application/msword', + 'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), + 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), + 'word' => array('application/msword', 'application/octet-stream'), + 'xl' => 'application/excel', + 'eml' => 'message/rfc822', + 'json' => array('application/json', 'text/json') + ); + function _print_r($var) + { + if( empty($var) ) + return false; + + print "
"; + print ""; + print "PRINT RESULT"; + print ""; + print "
";
+		print_r($var);
+		print "
"; + print "
"; + } + /** ========== C O N S T A N T E N D S H E R E ============ **/ /** @@ -47,23 +150,10 @@ * */ if( file_exists(BASEPATH . 'vendor') ){ - require_once BASEPATH ."vendor/autoload.php"; -} -else{ - spl_autoload_register( function ($class) { - if( file_exists(BASEPATH . 'lib' . DS . $class . '.class.php') ){ - require_once BASEPATH . 'lib' . DS . $class . '.class.php'; - } - } ); - /* - * L O A D I N G T A B L E F I L E S - */ - spl_autoload_register( function ($class) { - if( file_exists(BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php') ){ - require_once BASEPATH . 'lib' . DS . 'tables' . DS . $class . '.class.php'; - } - } ); - require_once BASEPATH . "lib" . DS . "functions.php"; + //require BASEPATH ."vendor/autoload.php"; + require __DIR__.'/../vendor/autoload.php'; } +use App\Functions; +$func = new Functions(); /** =========== F I L E L O A D I N G E N D S H E R E =========== **/ \ No newline at end of file diff --git a/lib/functions.php b/lib/functions.php index 363266e..2e6f728 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -1,193 +1,180 @@ * @package : Admin Panel * @version : 1.0 */ +class Functions +{ + static function getUriSegment( $segment = 0 ) + { + $uri = $_SERVER['REQUEST_URI']; + $uri = explode('/', $uri); + return $uri[$segment]; + } + + static function encryptURL($string) + { + $data = base64_encode($string); + $data = str_replace(array('+','/','='),array('-','_',':'),$data); + return $data; + } -function getUriSegment( $segment = 0 ) -{ - $uri = $_SERVER['REQUEST_URI']; - $uri = explode('/', $uri); - return $uri[$segment]; -} - -function encryptURL($string) -{ - $data = base64_encode($string); - $data = str_replace(array('+','/','='),array('-','_',':'),$data); - return $data; -} - -function decryptURL($string) -{ - $data = str_replace(array('-','_',':'),array('+','/','='),$string); - $mod4 = strlen($data) % 4; - if ($mod4) { - $data .= substr('====', $mod4); - } - return base64_decode($data); -} - -function isValidURL($url) -{ - $regex = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^"; - if(preg_match($regex, $url)) + static function decryptURL($string) { - if(strstr($url,'www')) + $data = str_replace(array('-','_',':'),array('+','/','='),$string); + $mod4 = strlen($data) % 4; + if ($mod4) { + $data .= substr('====', $mod4); + } + return base64_decode($data); + } + + static function isValidURL($url) + { + $regex = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^"; + if(preg_match($regex, $url)) { - $url_str = substr($url,strpos($url, 'www')); - if( in_array(substr_count($url_str, '.'), array(2,3)) ) - return TRUE; + if(strstr($url,'www')) + { + $url_str = substr($url,strpos($url, 'www')); + if( in_array(substr_count($url_str, '.'), array(2,3)) ) + return TRUE; + else + return FALSE; + } else - return FALSE; + return TRUE; } else - return TRUE; + return FALSE; } - else - return FALSE; -} -/** - * Function to escape value. - **/ - -function escape($var) -{ - if( empty($var) ) - return FALSE; + /** + * Function to escape value. + **/ - $var = trim($var); - $var = strip_tags($var); - $var = addslashes($var); - $var = htmlentities($var, ENT_QUOTES); - return $var; -} + static function escape($var) + { + if( empty($var) ) + return FALSE; -/** - * Function to get file extension - */ -function getExtension($filename) { - if($filename){ - $info = pathinfo($filename); - return strtolower($info['extension']); - } - return FALSE; -} - -/** -* Function to get file type by given extension. -**/ - function getFileType( $ext ) { - $ext2type = array( - 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), - 'video' => array( 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), - 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'rtf', 'wp', 'wpd' ), - 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsb', 'xlsm' ), - 'interactive' => array( 'key', 'ppt', 'pptx', 'pptm', 'odp', 'swf' ), - 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), - 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip' ), - 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), - ); - foreach ( $ext2type as $type => $exts ) - if ( in_array( $ext, $exts ) ) - return $type; + $var = trim($var); + $var = strip_tags($var); + $var = addslashes($var); + $var = htmlentities($var, ENT_QUOTES); + return $var; } -/** - * Function to generates a random password drawn from the defined set of characters. - **/ -function generateKey( $length = 12, $special_chars = true, $extra_special_chars = false ) { - $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - if ( $special_chars ) - $chars .= '!@#$%^&*()'; - if ( $extra_special_chars ) - $chars .= '-_ []{}<>~`+=,.;:/?|'; - - $password = ''; - for ( $i = 0; $i < $length; $i++ ) { - $password .= substr($chars, self::_rand(0, strlen($chars) - 1), 1); + /** + * Function to get file extension + */ + static function getExtension($filename) { + if($filename){ + $info = pathinfo($filename); + return strtolower($info['extension']); + } + return FALSE; } - return $password; -} -/* - * Function to generate a random number. - */ -function _rand( $min = 0, $max = 0 ) { - $rnd_value=''; + /** + * Function to get file type by given extension. + **/ + static function getFileType( $ext ) { + $ext2type = array( + 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), + 'video' => array( 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), + 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'rtf', 'wp', 'wpd' ), + 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsb', 'xlsm' ), + 'interactive' => array( 'key', 'ppt', 'pptx', 'pptm', 'odp', 'swf' ), + 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), + 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip' ), + 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), + ); + foreach ( $ext2type as $type => $exts ) + if ( in_array( $ext, $exts ) ) + return $type; + } - if ( strlen($rnd_value) < 8 ) { - $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) ); - $rnd_value .= sha1($rnd_value); - $seed = md5($rnd_value); + /** + * Function to generates a random password drawn from the defined set of characters. + **/ + static function generateKey( $length = 12, $special_chars = true, $extra_special_chars = false ) { + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + if ( $special_chars ) + $chars .= '!@#$%^&*()'; + if ( $extra_special_chars ) + $chars .= '-_ []{}<>~`+=,.;:/?|'; + + $password = ''; + for ( $i = 0; $i < $length; $i++ ) { + $password .= substr($chars, self::_rand(0, strlen($chars) - 1), 1); + } + return $password; } - // Take the first 8 digits for our value - $value = substr($rnd_value, 0, 8); - $rnd_value = substr($rnd_value, 8); - $value = abs(hexdec($value)); - if ( $max != 0 ) - $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); + /* + * Function to generate a random number. + */ + static function _rand( $min = 0, $max = 0 ) { + $rnd_value=''; - return abs(intval($value)); -} + if ( strlen($rnd_value) < 8 ) { + $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) ); + $rnd_value .= sha1($rnd_value); + $seed = md5($rnd_value); + } + // Take the first 8 digits for our value + $value = substr($rnd_value, 0, 8); + $rnd_value = substr($rnd_value, 8); + $value = abs(hexdec($value)); -/** - * Function to get values from ($_POST or $_GET methods) otherwise set to empty. - */ -function getVars($vars=array()){ - for($i = 0; $i < count($vars); $i++){ - $var = $vars[$i]; - global $$var; - if( !isset($$var) ){ - if(empty($_REQUEST[$var])) - $$var = ""; - else - $$var = self::escape($_REQUEST[$var]); + if ( $max != 0 ) + $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); + + return abs(intval($value)); + } + + /** + * Function to get values from ($_POST or $_GET methods) otherwise set to empty. + */ + static function getVars($vars=array()){ + for($i = 0; $i < count($vars); $i++){ + $var = $vars[$i]; + global $$var; + if( !isset($$var) ){ + if(empty($_REQUEST[$var])) + $$var = ""; + else + $$var = self::escape($_REQUEST[$var]); + } } } -} - -function _print_r($var) -{ - if( empty($var) ) - return false; - - print "
"; - print ""; - print "PRINT RESULT"; - print ""; - print "
";
-	print_r($var);
-	print "
"; - print "
"; -} - -function showQuery($query, $params) -{ - $keys = array(); - $values = array(); - - # build a regular expression for each parameter - - foreach ($params as $key=>$value) + + static function showQuery($query, $params) { - if (is_string($key)){ - $keys[] = '/:'.$key.'/'; - } - else{ - $keys[] = '/[?]/'; - } - if(is_numeric($value)){ - $values[] = intval($value); - } - else{ - $values[] = '"'.$value .'"'; + $keys = array(); + $values = array(); + + # build a regular expression for each parameter + + foreach ($params as $key=>$value) + { + if (is_string($key)){ + $keys[] = '/:'.$key.'/'; + } + else{ + $keys[] = '/[?]/'; + } + if(is_numeric($value)){ + $values[] = intval($value); + } + else{ + $values[] = '"'.$value .'"'; + } } + $query = preg_replace($keys, $values, $query, 1, $count); + //echo "DSGFDGFHGJFDGSDDFG"; + print $query; } - $query = preg_replace($keys, $values, $query, 1, $count); - //echo "DSGFDGFHGJFDGSDDFG"; - print $query; } \ No newline at end of file diff --git a/lib/tables/AdminUser.class.php b/lib/tables/AdminUser.class.php deleted file mode 100644 index 26f91cf..0000000 --- a/lib/tables/AdminUser.class.php +++ /dev/null @@ -1,125 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -final public class AdminUser extends Auth -{ - ///////////////////////////////////////////////// - // PROPERTIES, PUBLIC - ///////////////////////////////////////////////// - - public $table = 'admin'; - - public $id; - - public $username; - - public $password; - - public $hash; - - public $email; - - public $display_name; - - public $updated_on; - - public $status; - - /** - * Constructor - * @return void - */ - function __construct($var=0) - { - if($var>0) - { - $db = new DB(); - $obj = $db->getRow( $this->table, '*' ); - - if(is_object($obj)) - { - $this->id = stripslashes($obj->id); - $this->username = stripslashes($obj->username); - $this->password = stripslashes($obj->password); - $this->hash = stripslashes($obj->hash); - $this->email = stripslashes($obj->email); - $this->display_name = stripslashes($obj->display_name); - $this->status = stripslashes($obj->status); - $this->updated_on = stripslashes($obj->updated_on); - } - } - - } - - /** - * Commit DB query. - * @return pointer - */ - function commit() - { - $db = new DB(); - - if($this->id>0) - { - //do update - $res=self::update(); - } - else - {//do insert - $res=self::add(); - } - return $res; - } - - /** - * Perform Update Query for User. - * @return pointer - */ - function update() - { - $db = new DB(); - $pass = new Password( md5($this->password) ); - - $fieldSet = ['display_name' => $this->display_name, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]; - $db->where( ['id'=>$this->id] ); - $res = $db->update( $this->table, $fieldSet ); - return $res; - } - - /** - * Perform Inser Query for User. - * @return inserted id - */ - function add() - { - $db = new DB(); - $pass = new Password( md5($this->password) ); - - $fieldSet = ['username' => $this->username, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'display_name' => $this->display_name, 'updated_on' => date('Y-m-d H:i:s'), 'status'=>'1']; - $id = $db->insert( $this->table, $fieldSet ); - $this->id = $id; - return $id; - } - - /** - * Delete user's record. - * @return pointer - */ - function remove() - { - $db = new DB(); - - $db->where( ['id' => $this->id] ); - $res = $db->delete( $this->table ); - return $res; - } - -} \ No newline at end of file From e036e1b663d4d8bc78f1bacf3a54152bf860e5a5 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 30 Oct 2015 13:03:13 +0530 Subject: [PATCH 11/41] Add new files --- .gitignore | 3 +- index.php | 19 +++ lib/Auth.php | 45 +++++++ lib/DB.php | 253 +++++++++++++++++++++++++++++++++++++++ lib/Encryption.php | 23 ++++ lib/Image.php | 201 +++++++++++++++++++++++++++++++ lib/Mimes.php | 107 +++++++++++++++++ lib/Pagination.php | 89 ++++++++++++++ lib/Password.php | 52 ++++++++ lib/Tables/AdminUser.php | 124 +++++++++++++++++++ lib/Upload.php | 84 +++++++++++++ 11 files changed, 999 insertions(+), 1 deletion(-) create mode 100644 index.php create mode 100644 lib/Auth.php create mode 100644 lib/DB.php create mode 100644 lib/Encryption.php create mode 100644 lib/Image.php create mode 100644 lib/Mimes.php create mode 100644 lib/Pagination.php create mode 100644 lib/Password.php create mode 100644 lib/Tables/AdminUser.php create mode 100644 lib/Upload.php diff --git a/.gitignore b/.gitignore index 8b7ef35..0f8e7c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor -composer.lock +/composer.lock +/test.php \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..a0cce70 --- /dev/null +++ b/index.php @@ -0,0 +1,19 @@ +".BASEPATH; + +$db = new DB(); + +echo "
Password: ".$func->generateKey(); +?> +
Admin + +
+ diff --git a/lib/Auth.php b/lib/Auth.php new file mode 100644 index 0000000..ceef773 --- /dev/null +++ b/lib/Auth.php @@ -0,0 +1,45 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +abstract class Auth +{ + //protected function initialize(); + public function login(){ + $pass = new Password( $this->password ); + $db = new DB(); + $db->where( ['email' => $this->email, 'username' => $this->username],'AND', "OR" ); + $db->where( ['password' => $this->password],'AND' ); + $data = $db->getRow($this->table); + //_print_r($data); + if( count($data) > 0 && $pass->verifyPassword() ){ + if( $n = $pass->needRehash() ){ + $newHash = $pass->reHashPassword(); + $db->where( ['email' => $this->email, 'username' => $this->username],'AND', "OR" ); + $db->where( ['password' => $this->password],'AND' ); + $res = $db->update($this->table,['hash' => $newHash]); + } + + /************ SET SESSION VARIABLES HERE **************/ + //session_start(); + $_SESSION['logged'] = TRUE; + $_SESSION['userid'] = $data->id; + $_SESSION['username'] = $data->username ? $data->username : ""; + + /****************** END SESSION SETTINGS **************/ + return $data; + } + } + public function logout(){ + unset( $_SESSEION ); + session_destroy(); + header("Location:".BASEURL."?action=logout"); + } + +} \ No newline at end of file diff --git a/lib/DB.php b/lib/DB.php new file mode 100644 index 0000000..49374b3 --- /dev/null +++ b/lib/DB.php @@ -0,0 +1,253 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +class DB +{ + private $dbh; + public $query; + public $fieldList; // @Array in key=>pair format + public $offset; // @integer + public $limit; // @integer + public $orderBy; // @Array as ['ID', 'ASC'] + public $where; // WHERE Query + public $whereVal; + public $arrValues = array(); // @Array to bind with @params + + public $innerJoin; // Array as ['table'=>{tabl_name}, 'ON'=>'id'] + /*public $outerJoin; + public $leftJoin; + public $rightJoin;*/ // Will use later. + + function __construct(){ + $dsn = "mysql:host=".HOSTNAME.";dbname=".DBNAME; + try + { + $this->dbh = new \PDO($dsn, USERNAME, PASSWORD); + if( ENVIRONMENT == 'development' ){ + $this->dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); + } + } + catch(PDO_Exception $e){ + echo $e->getMessage(); + } + } + + /* + * + * ============= D A T A B A S E R E L A T E D F U N C T I O N S ============ + * + */ + + public function getResult($table, $fields) + { + if( empty($table) ) + return; + + $field = is_array($fields) ? implode(', ', $fields) : "*"; + + $sql = "SELECT ".$field." FROM `".$table."` "; + $this->query = self::buildQuery($sql); + + $stmt = $this->dbh->prepare($this->query); + $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); + $data = $stmt->fetchAll(PDO::FETCH_OBJ); + //print_r($data); + self::reset(); + return $data; + } + + public function getRow($table, $fields) + { + if( empty($table) ) + return; + + $field = is_array($fields) ? implode(', ', $fields) : "*"; + + $sql = "SELECT ".$field." FROM `".$table."` "; + $this->query = self::buildQuery($sql); + + $stmt = $this->dbh->prepare($this->query); + $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); + $data = $stmt->fetch(PDO::FETCH_OBJ); + + self::reset(); + return $data; + } + + /************************ E N D ********************************/ + public function insert($table, $fieldVal) + { + /* + * $fieldVal is an associative array containing as + * $key=>$val , Where key = column name of table + */ + + if( !is_array($fieldVal) || empty($table) ) + return; + + $arrValues = array(); + $fields = array(); + $sql = "INSERT INTO `".$table."` SET "; + foreach( $fieldVal as $key=>$val){ + $fields[] = $key."=?"; + $arrValues[] = $val; + } + + $sql .= implode(", ", $fields); + $stmt = $this->dbh->prepare($sql); + return $stmt->execute($arrValues) ? $this->dbh->lastInsertId() : FALSE; + } + + public function update($table, $fieldVal) + { + /* + * $fieldVal is an associative array containing as + * $key=>$val , Where key = column name of table + */ + if( !is_array($fieldVal) || empty($table) ) + return; + + $fields = array(); + $sql = "UPDATE `".$table."` SET "; + foreach( $fieldVal as $key=>$val){ + $fields[] = $key."=?"; + $this->arrValues[] = $val; + } + + $sql .= implode(", ", $fields); + $this->query = self::buildQuery($sql); + + $stmt = $this->dbh->prepare($this->query); + $res = $stmt->execute($this->arrValues); + + self::reset(); + return $res; + } + + public function delete( $table ) + { + if( empty($table) ) + return; + + $sql = "DELETE FROM `".$table."`"; + $this->query = self::buildQuery($sql); + + $stmt = $this->dbh->prepare($this->query); + $res = $stmt->execute($this->arrValues); + + self::reset(); + return $res; + } + + public function getCount( $table, $field ) + { + if( empty($table) ) + return; + + if( empty($field) ) + $field = "*"; + + $sql = "SELECT COUNT(".$field.") FROM `".$table."` "; + $this->query = self::buildQuery($sql); + + $stmt = $this->dbh->prepare($this->query); + $stmt->execute($this->arrValues); + $res = $stmt->fetch(PDO::FETCH_NUM); + + self::reset(); + return $res[0]; + } + + public function where( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr = "AND" ){ + if( empty($this->where) ){ + $this->where = "WHERE "; + } + else{ + $this->where .= " ".$beforeOpr." "; + } + $param = array(); + if( count($whereQuery) >= 1 ){ + foreach($whereQuery as $key => $val){ + $param[] = $key."=?"; + $this->whereVal[] = $val; + } + } + + $this->where .= implode(" ".$afterOpr." ", $param); + } + + public function likeWhere( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr = "AND" ){ + if( empty($this->where) ){ + $this->where = "WHERE "; + } + else{ + $this->where .= " ".$beforeOpr." "; + } + $param = array(); + if( count($whereQuery) >= 1 ){ + foreach($whereQuery as $key => $val){ + $param[] = $key." LIKE ?"; + $this->whereVal[] = $val; + } + } + + $this->where .= implode(" ".$afterOpr." ", $param); + } + public function inWhere( $whereQuery = array(), $beforeOpr = 'AND' ){ + if( empty($this->where) ){ + $this->where = "WHERE "; + } + else{ + $this->where .= " ".$beforeOpr." "; + } + //$ar = [ 'id' => [1,2,3,4,5] ]; + $param = array(); + if( count($whereQuery) == 1 ){ + foreach($whereQuery as $key => $val){ + $param[] = $key." IN '%?%'"; + $this->whereVal[] = "(".implode(',', $val).")"; + } + } + + $this->where .= implode(" ".$afterOpr." ", $param); + } + + private function buildQuery( $sql ) + { + if( empty($sql) ) + return FALSE; + + $query = $sql; + if( $this->where ){ + $query .= " ".$this->where." "; + $this->arrValues = array_merge($this->arrValues, $this->whereVal); + } + $query .= !empty($this->orderBy) ? implode(' ', $this->orderBy)." " : ""; + $query .= !empty($this->limit) ? " LIMIT ".$this->limit." " : ""; + $query .= !empty($this->offset) ? " OFFSET ".$this->offset." " : ""; + + Functions::showQuery($query,$this->arrValues); + return $query; + } + + private function reset() + { + unset($this->query); + unset($this->fieldList); + unset($this->arrValues); + unset($this->limit); + unset($this->orderBy); + unset($this->where); + unset($this->whereVal); + unset($this->innerJoin); + /*unset($this->outerJoin); + unset($this->leftJoin); + unset($this->rightJoin);*/ + } +} diff --git a/lib/Encryption.php b/lib/Encryption.php new file mode 100644 index 0000000..fb7f7d2 --- /dev/null +++ b/lib/Encryption.php @@ -0,0 +1,23 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class Encryption { + private $key; + + public function __construct($key) { + $this->key = hash('sha256', $key, true); + } + + public function encrypt($value) { + return strtr(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), $value, MCRYPT_MODE_ECB)), '+/=', '-_,'); + } + + public function decrypt($value) { + return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, hash('sha256', $this->key, true), base64_decode(strtr($value, '-_,', '+/=')), MCRYPT_MODE_ECB)); + } +} \ No newline at end of file diff --git a/lib/Image.php b/lib/Image.php new file mode 100644 index 0000000..b5d7680 --- /dev/null +++ b/lib/Image.php @@ -0,0 +1,201 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +class Image { + private $file; + private $image; + private $info; + + public function __construct($file) { + if (file_exists($file)) { + $this->file = $file; + + $info = getimagesize($file); + + $this->info = array( + 'width' => $info[0], + 'height' => $info[1], + 'bits' => isset($info['bits']) ? $info['bits'] : '', + 'mime' => isset($info['mime']) ? $info['mime'] : '' + ); + + $this->image = $this->create($file); + } else { + exit('Error: Could not load image ' . $file . '!'); + } + } + + private function create($image) { + $mime = $this->info['mime']; + + if ($mime == 'image/gif') { + return imagecreatefromgif ($image); + } elseif ($mime == 'image/png') { + return imagecreatefrompng($image); + } elseif ($mime == 'image/jpeg') { + return imagecreatefromjpeg($image); + } + } + + public function save($file, $quality = 90) { + $info = pathinfo($file); + + $extension = strtolower($info['extension']); + + if (is_resource($this->image)) { + if ($extension == 'jpeg' || $extension == 'jpg') { + imagejpeg($this->image, $file, $quality); + } elseif ($extension == 'png') { + imagepng($this->image, $file); + } elseif ($extension == 'gif') { + imagegif ($this->image, $file); + } + + imagedestroy($this->image); + } + } + + public function resize($width = 0, $height = 0, $default = '') { + if (!$this->info['width'] || !$this->info['height']) { + return; + } + + $xpos = 0; + $ypos = 0; + $scale = 1; + + $scale_w = $width / $this->info['width']; + $scale_h = $height / $this->info['height']; + + if ($default == 'w') { + $scale = $scale_w; + } elseif ($default == 'h') { + $scale = $scale_h; + } else { + $scale = min($scale_w, $scale_h); + } + + if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { + return; + } + + $new_width = (int)($this->info['width'] * $scale); + $new_height = (int)($this->info['height'] * $scale); + $xpos = (int)(($width - $new_width) / 2); + $ypos = (int)(($height - $new_height) / 2); + + $image_old = $this->image; + $this->image = imagecreatetruecolor($width, $height); + + if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { + imagealphablending($this->image, false); + imagesavealpha($this->image, true); + $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); + imagecolortransparent($this->image, $background); + } else { + $background = imagecolorallocate($this->image, 255, 255, 255); + } + + imagefilledrectangle($this->image, 0, 0, $width, $height, $background); + + imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); + imagedestroy($image_old); + + $this->info['width'] = $width; + $this->info['height'] = $height; + } + + public function watermark($file, $position = 'bottomright') { + $watermark = $this->create($file); + + $watermark_width = imagesx($watermark); + $watermark_height = imagesy($watermark); + + switch($position) { + case 'topleft': + $watermark_pos_x = 0; + $watermark_pos_y = 0; + break; + case 'topright': + $watermark_pos_x = $this->info['width'] - $watermark_width; + $watermark_pos_y = 0; + break; + case 'bottomleft': + $watermark_pos_x = 0; + $watermark_pos_y = $this->info['height'] - $watermark_height; + break; + case 'bottomright': + $watermark_pos_x = $this->info['width'] - $watermark_width; + $watermark_pos_y = $this->info['height'] - $watermark_height; + break; + } + + imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40); + + imagedestroy($watermark); + } + + public function crop($top_x, $top_y, $bottom_x, $bottom_y) { + $image_old = $this->image; + $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); + + imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']); + imagedestroy($image_old); + + $this->info['width'] = $bottom_x - $top_x; + $this->info['height'] = $bottom_y - $top_y; + } + + public function rotate($degree, $color = 'FFFFFF') { + $rgb = $this->html2rgb($color); + + $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); + + $this->info['width'] = imagesx($this->image); + $this->info['height'] = imagesy($this->image); + } + + private function filter($filter) { + imagefilter($this->image, $filter); + } + + private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') { + $rgb = $this->html2rgb($color); + + imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); + } + + private function merge($file, $x = 0, $y = 0, $opacity = 100) { + $merge = $this->create($file); + + $merge_width = imagesx($merge); + $merge_height = imagesy($merge); + + imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity); + } + + private function html2rgb($color) { + if ($color[0] == '#') { + $color = substr($color, 1); + } + + if (strlen($color) == 6) { + list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); + } elseif (strlen($color) == 3) { + list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); + } else { + return false; + } + + $r = hexdec($r); + $g = hexdec($g); + $b = hexdec($b); + + return array($r, $g, $b); + } +} diff --git a/lib/Mimes.php b/lib/Mimes.php new file mode 100644 index 0000000..5b64226 --- /dev/null +++ b/lib/Mimes.php @@ -0,0 +1,107 @@ + + * @package : Admin Panel + * @version : 1.0 + */ +class Mimes +{ + $mimes = array( 'hqx' => 'application/mac-binhex40', + 'cpt' => 'application/mac-compactpro', + 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), + 'bin' => 'application/macbinary', + 'dms' => 'application/octet-stream', + 'lha' => 'application/octet-stream', + 'lzh' => 'application/octet-stream', + 'exe' => array('application/octet-stream', 'application/x-msdownload'), + 'class' => 'application/octet-stream', + 'psd' => 'application/x-photoshop', + 'so' => 'application/octet-stream', + 'sea' => 'application/octet-stream', + 'dll' => 'application/octet-stream', + 'oda' => 'application/oda', + 'pdf' => array('application/pdf', 'application/x-download'), + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', + 'smi' => 'application/smil', + 'smil' => 'application/smil', + 'mif' => 'application/vnd.mif', + 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), + 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'), + 'wbxml' => 'application/wbxml', + 'wmlc' => 'application/wmlc', + 'dcr' => 'application/x-director', + 'dir' => 'application/x-director', + 'dxr' => 'application/x-director', + 'dvi' => 'application/x-dvi', + 'gtar' => 'application/x-gtar', + 'gz' => 'application/x-gzip', + 'php' => 'application/x-httpd-php', + 'php4' => 'application/x-httpd-php', + 'php3' => 'application/x-httpd-php', + 'phtml' => 'application/x-httpd-php', + 'phps' => 'application/x-httpd-php-source', + 'js' => 'application/x-javascript', + 'swf' => 'application/x-shockwave-flash', + 'sit' => 'application/x-stuffit', + 'tar' => 'application/x-tar', + 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), + 'xhtml' => 'application/xhtml+xml', + 'xht' => 'application/xhtml+xml', + 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), + 'mid' => 'audio/midi', + 'midi' => 'audio/midi', + 'mpga' => 'audio/mpeg', + 'mp2' => 'audio/mpeg', + 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), + 'aif' => 'audio/x-aiff', + 'aiff' => 'audio/x-aiff', + 'aifc' => 'audio/x-aiff', + 'ram' => 'audio/x-pn-realaudio', + 'rm' => 'audio/x-pn-realaudio', + 'rpm' => 'audio/x-pn-realaudio-plugin', + 'ra' => 'audio/x-realaudio', + 'rv' => 'video/vnd.rn-realvideo', + 'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'), + 'bmp' => array('image/bmp', 'image/x-windows-bmp'), + 'gif' => 'image/gif', + 'jpeg' => array('image/jpeg', 'image/pjpeg'), + 'jpg' => array('image/jpeg', 'image/pjpeg'), + 'jpe' => array('image/jpeg', 'image/pjpeg'), + 'png' => array('image/png', 'image/x-png'), + 'tiff' => 'image/tiff', + 'tif' => 'image/tiff', + 'css' => 'text/css', + 'html' => 'text/html', + 'htm' => 'text/html', + 'shtml' => 'text/html', + 'txt' => 'text/plain', + 'text' => 'text/plain', + 'log' => array('text/plain', 'text/x-log'), + 'rtx' => 'text/richtext', + 'rtf' => 'text/rtf', + 'xml' => 'text/xml', + 'xsl' => 'text/xml', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpe' => 'video/mpeg', + 'qt' => 'video/quicktime', + 'mov' => 'video/quicktime', + 'avi' => 'video/x-msvideo', + 'movie' => 'video/x-sgi-movie', + 'doc' => 'application/msword', + 'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), + 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), + 'word' => array('application/msword', 'application/octet-stream'), + 'xl' => 'application/excel', + 'eml' => 'message/rfc822', + 'json' => array('application/json', 'text/json') + ); + + +/* End of file mimes.php */ +/* Location: ./application/config/mimes.php */ + +} \ No newline at end of file diff --git a/lib/Pagination.php b/lib/Pagination.php new file mode 100644 index 0000000..c6e1cfd --- /dev/null +++ b/lib/Pagination.php @@ -0,0 +1,89 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +class Pagination { + public $total = 0; + public $page = 1; + public $limit = 10; + public $num_links = 8; + public $url = ''; + public $text_first = '|<'; + public $text_last = '>|'; + public $text_next = '>'; + public $text_prev = '<'; + + public function render() { + $total = $this->total; + + if ($this->page < 1) { + $page = 1; + } else { + $page = $this->page; + } + + if (!(int)$this->limit) { + $limit = 10; + } else { + $limit = $this->limit; + } + + $num_links = $this->num_links; + $num_pages = ceil($total / $limit); + + $this->url = str_replace('%7Bpage%7D', '{page}', $this->url); + + $output = '
    '; + + if ($page > 1) { + $output .= '
  • ' . $this->text_first . '
  • '; + $output .= '
  • ' . $this->text_prev . '
  • '; + } + + if ($num_pages > 1) { + if ($num_pages <= $num_links) { + $start = 1; + $end = $num_pages; + } else { + $start = $page - floor($num_links / 2); + $end = $page + floor($num_links / 2); + + if ($start < 1) { + $end += abs($start) + 1; + $start = 1; + } + + if ($end > $num_pages) { + $start -= ($end - $num_pages); + $end = $num_pages; + } + } + + for ($i = $start; $i <= $end; $i++) { + if ($page == $i) { + $output .= '
  • ' . $i . '
  • '; + } else { + $output .= '
  • ' . $i . '
  • '; + } + } + } + + if ($page < $num_pages) { + $output .= '
  • ' . $this->text_next . '
  • '; + $output .= '
  • ' . $this->text_last . '
  • '; + } + + $output .= '
'; + + if ($num_pages > 1) { + return $output; + } else { + return ''; + } + } +} \ No newline at end of file diff --git a/lib/Password.php b/lib/Password.php new file mode 100644 index 0000000..c6eedd4 --- /dev/null +++ b/lib/Password.php @@ -0,0 +1,52 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class Password +{ + public $hash; + public $password; + public $options; + + public function __construct( $password = '' ){ + if($password == ''){ + return; + } + $this->password = $password; + $this->options = ['salt' => self::uniqueSalt(), 'cost' => 10]; + if($password != ''){ + $this->hash = self::hashPassword(); + } + } + + public final function uniqueSalt() { + return substr(sha1(uniqid(mt_rand(), true)), 0, 22); + } + + public function hashPassword(){ + $hash = password_hash($this->password, PASSWORD_DEFAULT, $this->options); + return $hash; + } + public function needRehash(){ + if( password_needs_rehash($this->hash, PASSWORD_DEFAULT, $this->options) ){ + return TRUE; + } + } + public function reHashPassword(){ + $hash = password_hash($this->password, PASSWORD_DEFAULT, $this->options); + return $hash; + } + + public function getInfo(){ + $info = password_get_info($this->hash); + return $info; + } + + public function verifyPassword(){ + return password_verify($this->password, $this->hash) ? TRUE : FALSE; + } +} \ No newline at end of file diff --git a/lib/Tables/AdminUser.php b/lib/Tables/AdminUser.php new file mode 100644 index 0000000..52f2d33 --- /dev/null +++ b/lib/Tables/AdminUser.php @@ -0,0 +1,124 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class AdminUser extends Auth +{ + ///////////////////////////////////////////////// + // PROPERTIES, PUBLIC + ///////////////////////////////////////////////// + + public $table = 'admin'; + + public $id; + + public $username; + + public $password; + + public $hash; + + public $email; + + public $display_name; + + public $updated_on; + + public $status; + + /** + * Constructor + * @return void + */ + function __construct($var=0) + { + if($var>0) + { + $db = new DB(); + $obj = $db->getRow( $this->table, '*' ); + + if(is_object($obj)) + { + $this->id = stripslashes($obj->id); + $this->username = stripslashes($obj->username); + $this->password = stripslashes($obj->password); + $this->hash = stripslashes($obj->hash); + $this->email = stripslashes($obj->email); + $this->display_name = stripslashes($obj->display_name); + $this->status = stripslashes($obj->status); + $this->updated_on = stripslashes($obj->updated_on); + } + } + + } + + /** + * Commit DB query. + * @return pointer + */ + function commit() + { + $db = new DB(); + + if($this->id>0) + { + //do update + $res=self::update(); + } + else + {//do insert + $res=self::add(); + } + return $res; + } + + /** + * Perform Update Query for User. + * @return pointer + */ + function update() + { + $db = new DB(); + $pass = new Password( md5($this->password) ); + + $fieldSet = ['display_name' => $this->display_name, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]; + $db->where( ['id'=>$this->id] ); + $res = $db->update( $this->table, $fieldSet ); + return $res; + } + + /** + * Perform Inser Query for User. + * @return inserted id + */ + function add() + { + $db = new DB(); + $pass = new Password( md5($this->password) ); + + $fieldSet = ['username' => $this->username, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'display_name' => $this->display_name, 'updated_on' => date('Y-m-d H:i:s'), 'status'=>'1']; + $id = $db->insert( $this->table, $fieldSet ); + $this->id = $id; + return $id; + } + + /** + * Delete user's record. + * @return pointer + */ + function remove() + { + $db = new DB(); + + $db->where( ['id' => $this->id] ); + $res = $db->delete( $this->table ); + return $res; + } + +} \ No newline at end of file diff --git a/lib/Upload.php b/lib/Upload.php new file mode 100644 index 0000000..94f308c --- /dev/null +++ b/lib/Upload.php @@ -0,0 +1,84 @@ +file = $file; + $info = getimagesize($file); + + $this->info = array( + 'width' => $info[0], + 'height' => $info[1], + 'bits' => isset($info['bits']) ? $info['bits'] : '', + 'mime' => isset($info['mime']) ? $info['mime'] : '' + ); + + if( !file_exists( BASEPATH.'uploads' )){ + mkdir( BASEPATH."uploads", 0755); + } + $this->fileType = strtolower(explode('/', $this->info['mime'])); + if( $this->fileType == "image" ) + { + if( !file_exists( BASEPATH.'uploads'.DS.'media' )){ + mkdir( BASEPATH.'uploads'.DS.'media'.DS.'thumb1', 0755, true); + mkdir( BASEPATH.'uploads'.DS.'media'.DS.'thumb2', 0755); + } + } + else + { + if( !file_exists( BASEPATH.'uploads'.DS.'document' )){ + mkdir( BASEPATH.'uploads'.DS.'document', 0755, true); + } + $this->dir = BASEPATH.'uploads'.DS.'document'.DS; + } + } + else{ + exit("Couldn't load the given file ".$file); + } + } + + public function doUpload() + { + $ext = pathinfo($this->file['name'], PATHINFO_EXTENSION); + if( !in_array($ext, $mimes) ){ + exit("Unsupported file type"); + } + + if( in_array( $this->fileType, ['image', 'audio', 'video'] ) ){ + $fileName = time().rand(0,10).".".$ext; + $targetFile = $this->dir.$fileName; + if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ + $image = new Image( $targetFile ); + + $image->load($targetPath); + $image->resizeToWidth(450); + $image->save($thumb1.$fileName); + + $image->load($targetPath); + $image->resizeToWidth(150); + $image->save($thumb2.$fileName); + } + return $fileName; + } + else{ + $fileName = time().rand(0,10).".".$ext; + $targetFile = $this->dir.$fileName; + if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ + return $fileName; + } + } + } + +} \ No newline at end of file From 740124288605057831b04092b3547cd0c0fce2ee Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 30 Oct 2015 15:54:53 +0530 Subject: [PATCH 12/41] Todays final commit 30-Oct --- .gitignore | 4 +- index.php | 14 +++ lib/Image.php | 280 +++++++++++++++---------------------------------- lib/Mimes.php | 53 ++++------ lib/Upload.php | 56 ++++++---- lib/config.php | 133 ++++------------------- test.php | 53 ++++------ 7 files changed, 201 insertions(+), 392 deletions(-) diff --git a/.gitignore b/.gitignore index 0f8e7c4..634e433 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /vendor +/uploads /composer.lock -/test.php \ No newline at end of file +/test.php +/index.php \ No newline at end of file diff --git a/index.php b/index.php index a0cce70..cefd793 100644 --- a/index.php +++ b/index.php @@ -3,6 +3,7 @@ //use App; use App\DB; use App\Tables\AdminUser; +use App\Upload; echo BASEURL."
".BASEPATH; @@ -17,3 +18,16 @@ ?> +
Upload +doUpload(); +?> +
+ diff --git a/lib/Image.php b/lib/Image.php index b5d7680..24425ab 100644 --- a/lib/Image.php +++ b/lib/Image.php @@ -6,196 +6,90 @@ * @version : 1.0 */ -class Image { - private $file; - private $image; - private $info; - - public function __construct($file) { - if (file_exists($file)) { - $this->file = $file; - - $info = getimagesize($file); - - $this->info = array( - 'width' => $info[0], - 'height' => $info[1], - 'bits' => isset($info['bits']) ? $info['bits'] : '', - 'mime' => isset($info['mime']) ? $info['mime'] : '' - ); - - $this->image = $this->create($file); - } else { - exit('Error: Could not load image ' . $file . '!'); - } - } - - private function create($image) { - $mime = $this->info['mime']; - - if ($mime == 'image/gif') { - return imagecreatefromgif ($image); - } elseif ($mime == 'image/png') { - return imagecreatefrompng($image); - } elseif ($mime == 'image/jpeg') { - return imagecreatefromjpeg($image); - } - } - - public function save($file, $quality = 90) { - $info = pathinfo($file); - - $extension = strtolower($info['extension']); - - if (is_resource($this->image)) { - if ($extension == 'jpeg' || $extension == 'jpg') { - imagejpeg($this->image, $file, $quality); - } elseif ($extension == 'png') { - imagepng($this->image, $file); - } elseif ($extension == 'gif') { - imagegif ($this->image, $file); - } - - imagedestroy($this->image); - } - } - - public function resize($width = 0, $height = 0, $default = '') { - if (!$this->info['width'] || !$this->info['height']) { - return; - } - - $xpos = 0; - $ypos = 0; - $scale = 1; - - $scale_w = $width / $this->info['width']; - $scale_h = $height / $this->info['height']; - - if ($default == 'w') { - $scale = $scale_w; - } elseif ($default == 'h') { - $scale = $scale_h; - } else { - $scale = min($scale_w, $scale_h); - } - - if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { - return; - } - - $new_width = (int)($this->info['width'] * $scale); - $new_height = (int)($this->info['height'] * $scale); - $xpos = (int)(($width - $new_width) / 2); - $ypos = (int)(($height - $new_height) / 2); - - $image_old = $this->image; - $this->image = imagecreatetruecolor($width, $height); - - if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { - imagealphablending($this->image, false); - imagesavealpha($this->image, true); - $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); - imagecolortransparent($this->image, $background); - } else { - $background = imagecolorallocate($this->image, 255, 255, 255); - } - - imagefilledrectangle($this->image, 0, 0, $width, $height, $background); - - imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); - imagedestroy($image_old); - - $this->info['width'] = $width; - $this->info['height'] = $height; - } - - public function watermark($file, $position = 'bottomright') { - $watermark = $this->create($file); - - $watermark_width = imagesx($watermark); - $watermark_height = imagesy($watermark); - - switch($position) { - case 'topleft': - $watermark_pos_x = 0; - $watermark_pos_y = 0; - break; - case 'topright': - $watermark_pos_x = $this->info['width'] - $watermark_width; - $watermark_pos_y = 0; - break; - case 'bottomleft': - $watermark_pos_x = 0; - $watermark_pos_y = $this->info['height'] - $watermark_height; - break; - case 'bottomright': - $watermark_pos_x = $this->info['width'] - $watermark_width; - $watermark_pos_y = $this->info['height'] - $watermark_height; - break; - } - - imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40); - - imagedestroy($watermark); - } - - public function crop($top_x, $top_y, $bottom_x, $bottom_y) { - $image_old = $this->image; - $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); - - imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']); - imagedestroy($image_old); - - $this->info['width'] = $bottom_x - $top_x; - $this->info['height'] = $bottom_y - $top_y; - } - - public function rotate($degree, $color = 'FFFFFF') { - $rgb = $this->html2rgb($color); - - $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); - - $this->info['width'] = imagesx($this->image); - $this->info['height'] = imagesy($this->image); - } - - private function filter($filter) { - imagefilter($this->image, $filter); - } - - private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') { - $rgb = $this->html2rgb($color); - - imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); - } - - private function merge($file, $x = 0, $y = 0, $opacity = 100) { - $merge = $this->create($file); - - $merge_width = imagesx($merge); - $merge_height = imagesy($merge); - - imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity); - } - - private function html2rgb($color) { - if ($color[0] == '#') { - $color = substr($color, 1); - } - - if (strlen($color) == 6) { - list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); - } elseif (strlen($color) == 3) { - list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); - } else { - return false; - } - - $r = hexdec($r); - $g = hexdec($g); - $b = hexdec($b); - - return array($r, $g, $b); - } -} +class Image +{ + var $image; + var $image_type; + + function load($filename) + { + $image_info = getimagesize($filename); + $this->image_type = $image_info[2]; + if ($this->image_type == IMAGETYPE_JPEG) { + $this->image = imagecreatefromjpeg($filename); + } + elseif ($this->image_type == IMAGETYPE_GIF) { + $this->image = imagecreatefromgif($filename); + } + elseif ($this->image_type == IMAGETYPE_PNG) { + $this->image = imagecreatefrompng($filename); + } + } + + function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null) + { + if ($image_type == IMAGETYPE_JPEG) { + imagejpeg($this->image, $filename, $compression); + } + elseif ($image_type == IMAGETYPE_GIF) { + imagegif($this->image, $filename); + } + elseif ($image_type == IMAGETYPE_PNG) { + imagepng($this->image, $filename); + } + if ($permissions != null) { + chmod($filename, $permissions); + } + } + + function output($image_type = IMAGETYPE_JPEG) + { + if ($image_type == IMAGETYPE_JPEG) { + imagejpeg($this->image); + } + elseif ($image_type == IMAGETYPE_GIF) { + imagegif($this->image); + } + elseif ($image_type == IMAGETYPE_PNG) { + imagepng($this->image); + } + } + + function getWidth() + { + return imagesx($this->image); + } + + function getHeight() + { + return imagesy($this->image); + } + + function resizeToHeight($height) + { + $ratio = $height / $this->getHeight(); + $width = $this->getWidth() * $ratio; + $this->resize($width, $height); + } + + function resizeToWidth($width) + { + $ratio = $width / $this->getWidth(); + $height = $this->getheight() * $ratio; + $this->resize($width, $height); + } + + function scale($scale) + { + $width = $this->getWidth() * $scale / 100; + $height = $this->getheight() * $scale / 100; + $this->resize($width, $height); + } + + function resize($width, $height) + { + $new_image = imagecreatetruecolor($width, $height); + imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); + $this->image = $new_image; + } +} \ No newline at end of file diff --git a/lib/Mimes.php b/lib/Mimes.php index 5b64226..f217244 100644 --- a/lib/Mimes.php +++ b/lib/Mimes.php @@ -1,35 +1,33 @@ * @package : Admin Panel * @version : 1.0 */ -class Mimes -{ - $mimes = array( 'hqx' => 'application/mac-binhex40', +$fileTypes = array( 'hqx' => 'application/mac-binhex40', 'cpt' => 'application/mac-compactpro', - 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), + 'csv' => 'text/csv', 'bin' => 'application/macbinary', 'dms' => 'application/octet-stream', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', - 'exe' => array('application/octet-stream', 'application/x-msdownload'), + 'exe' => 'application/octet-stream', 'class' => 'application/octet-stream', 'psd' => 'application/x-photoshop', 'so' => 'application/octet-stream', 'sea' => 'application/octet-stream', 'dll' => 'application/octet-stream', 'oda' => 'application/oda', - 'pdf' => array('application/pdf', 'application/x-download'), + 'pdf' => 'application/pdf', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'smi' => 'application/smil', 'smil' => 'application/smil', 'mif' => 'application/vnd.mif', - 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), - 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'), + 'xls' => 'application/vnd.ms-excel', + 'ppt' => 'application/vnd.ms-powerpoint', + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'wbxml' => 'application/wbxml', 'wmlc' => 'application/wmlc', 'dcr' => 'application/x-director', @@ -47,15 +45,15 @@ class Mimes 'swf' => 'application/x-shockwave-flash', 'sit' => 'application/x-stuffit', 'tar' => 'application/x-tar', - 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), + 'tgz' => 'application/x-tar', 'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml', - 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), + 'zip' => 'application/zip', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mpga' => 'audio/mpeg', 'mp2' => 'audio/mpeg', - 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), + 'mp3' => 'audio/mp3', 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', @@ -64,13 +62,13 @@ class Mimes 'rpm' => 'audio/x-pn-realaudio-plugin', 'ra' => 'audio/x-realaudio', 'rv' => 'video/vnd.rn-realvideo', - 'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'), - 'bmp' => array('image/bmp', 'image/x-windows-bmp'), + 'wav' => 'audio/wav', + 'bmp' => 'image/bmp', 'gif' => 'image/gif', - 'jpeg' => array('image/jpeg', 'image/pjpeg'), - 'jpg' => array('image/jpeg', 'image/pjpeg'), - 'jpe' => array('image/jpeg', 'image/pjpeg'), - 'png' => array('image/png', 'image/x-png'), + 'jpeg' => 'image/jpeg', + 'jpg' => 'image/jpeg', + 'jpe' => 'image/jpeg', + 'png' => 'image/png', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'css' => 'text/css', @@ -79,7 +77,7 @@ class Mimes 'shtml' => 'text/html', 'txt' => 'text/plain', 'text' => 'text/plain', - 'log' => array('text/plain', 'text/x-log'), + 'log' => 'text/x-log', 'rtx' => 'text/richtext', 'rtf' => 'text/rtf', 'xml' => 'text/xml', @@ -91,17 +89,12 @@ class Mimes 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', + 'webm' => 'video/webm', 'doc' => 'application/msword', - 'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), - 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), - 'word' => array('application/msword', 'application/octet-stream'), + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'word' => 'application/msword', 'xl' => 'application/excel', 'eml' => 'message/rfc822', - 'json' => array('application/json', 'text/json') - ); - - -/* End of file mimes.php */ -/* Location: ./application/config/mimes.php */ - -} \ No newline at end of file + 'json' => 'text/json' + ); \ No newline at end of file diff --git a/lib/Upload.php b/lib/Upload.php index 94f308c..a6246aa 100644 --- a/lib/Upload.php +++ b/lib/Upload.php @@ -1,36 +1,48 @@ file = $file; - $info = getimagesize($file); + $this->config = $config; + + $ext = pathinfo($file['name'], PATHINFO_EXTENSION); + $mime = $fileTypes[$ext]; + $size = isset($file['size']) ? $file['size'] : filesize($file['name']); + $this->fileTypes = $fileTypes; - $this->info = array( - 'width' => $info[0], - 'height' => $info[1], - 'bits' => isset($info['bits']) ? $info['bits'] : '', - 'mime' => isset($info['mime']) ? $info['mime'] : '' + $this->info = array( + 'bits' => $size, + 'mime' => $mime ); if( !file_exists( BASEPATH.'uploads' )){ mkdir( BASEPATH."uploads", 0755); } - $this->fileType = strtolower(explode('/', $this->info['mime'])); + $type = explode('/', $this->info['mime']); + $this->fileType = strtolower($type[0]); if( $this->fileType == "image" ) { + $info = getimagesize($file['tmp_name']); + $this->info['width'] = $info[0]; + $this->info['height'] = $info[1]; + if( !file_exists( BASEPATH.'uploads'.DS.'media' )){ mkdir( BASEPATH.'uploads'.DS.'media'.DS.'thumb1', 0755, true); mkdir( BASEPATH.'uploads'.DS.'media'.DS.'thumb2', 0755); @@ -52,23 +64,21 @@ public function __construct($file) public function doUpload() { $ext = pathinfo($this->file['name'], PATHINFO_EXTENSION); - if( !in_array($ext, $mimes) ){ - exit("Unsupported file type"); - } - if( in_array( $this->fileType, ['image', 'audio', 'video'] ) ){ $fileName = time().rand(0,10).".".$ext; $targetFile = $this->dir.$fileName; if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ - $image = new Image( $targetFile ); + if( $this->fileType == 'image' ){ + $image = new Image( $targetFile ); - $image->load($targetPath); - $image->resizeToWidth(450); - $image->save($thumb1.$fileName); + $image->load($targetFile); + $image->resizeToWidth(450); + $image->save($this->thumb1.$fileName); - $image->load($targetPath); - $image->resizeToWidth(150); - $image->save($thumb2.$fileName); + $image->load($targetFile); + $image->resizeToWidth(150); + $image->save($this->thumb2.$fileName); + } } return $fileName; } diff --git a/lib/config.php b/lib/config.php index 60f4653..3f75fd1 100644 --- a/lib/config.php +++ b/lib/config.php @@ -18,7 +18,7 @@ const ENVIRONMENT = 'development'; // OR production => live if( ENVIRONMENT == 'development' ){ - error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE); + error_reporting(-1); } else{ error_reporting(0); @@ -35,113 +35,6 @@ define( 'BASEURL', $baseurl ); define( 'CURRENT_PAGE', $curPage ); -$mimes = array( 'hqx' => 'application/mac-binhex40', - 'cpt' => 'application/mac-compactpro', - 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), - 'bin' => 'application/macbinary', - 'dms' => 'application/octet-stream', - 'lha' => 'application/octet-stream', - 'lzh' => 'application/octet-stream', - 'exe' => array('application/octet-stream', 'application/x-msdownload'), - 'class' => 'application/octet-stream', - 'psd' => 'application/x-photoshop', - 'so' => 'application/octet-stream', - 'sea' => 'application/octet-stream', - 'dll' => 'application/octet-stream', - 'oda' => 'application/oda', - 'pdf' => array('application/pdf', 'application/x-download'), - 'ai' => 'application/postscript', - 'eps' => 'application/postscript', - 'ps' => 'application/postscript', - 'smi' => 'application/smil', - 'smil' => 'application/smil', - 'mif' => 'application/vnd.mif', - 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), - 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'), - 'wbxml' => 'application/wbxml', - 'wmlc' => 'application/wmlc', - 'dcr' => 'application/x-director', - 'dir' => 'application/x-director', - 'dxr' => 'application/x-director', - 'dvi' => 'application/x-dvi', - 'gtar' => 'application/x-gtar', - 'gz' => 'application/x-gzip', - 'php' => 'application/x-httpd-php', - 'php4' => 'application/x-httpd-php', - 'php3' => 'application/x-httpd-php', - 'phtml' => 'application/x-httpd-php', - 'phps' => 'application/x-httpd-php-source', - 'js' => 'application/x-javascript', - 'swf' => 'application/x-shockwave-flash', - 'sit' => 'application/x-stuffit', - 'tar' => 'application/x-tar', - 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), - 'xhtml' => 'application/xhtml+xml', - 'xht' => 'application/xhtml+xml', - 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), - 'mid' => 'audio/midi', - 'midi' => 'audio/midi', - 'mpga' => 'audio/mpeg', - 'mp2' => 'audio/mpeg', - 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), - 'aif' => 'audio/x-aiff', - 'aiff' => 'audio/x-aiff', - 'aifc' => 'audio/x-aiff', - 'ram' => 'audio/x-pn-realaudio', - 'rm' => 'audio/x-pn-realaudio', - 'rpm' => 'audio/x-pn-realaudio-plugin', - 'ra' => 'audio/x-realaudio', - 'rv' => 'video/vnd.rn-realvideo', - 'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'), - 'bmp' => array('image/bmp', 'image/x-windows-bmp'), - 'gif' => 'image/gif', - 'jpeg' => array('image/jpeg', 'image/pjpeg'), - 'jpg' => array('image/jpeg', 'image/pjpeg'), - 'jpe' => array('image/jpeg', 'image/pjpeg'), - 'png' => array('image/png', 'image/x-png'), - 'tiff' => 'image/tiff', - 'tif' => 'image/tiff', - 'css' => 'text/css', - 'html' => 'text/html', - 'htm' => 'text/html', - 'shtml' => 'text/html', - 'txt' => 'text/plain', - 'text' => 'text/plain', - 'log' => array('text/plain', 'text/x-log'), - 'rtx' => 'text/richtext', - 'rtf' => 'text/rtf', - 'xml' => 'text/xml', - 'xsl' => 'text/xml', - 'mpeg' => 'video/mpeg', - 'mpg' => 'video/mpeg', - 'mpe' => 'video/mpeg', - 'qt' => 'video/quicktime', - 'mov' => 'video/quicktime', - 'avi' => 'video/x-msvideo', - 'movie' => 'video/x-sgi-movie', - 'doc' => 'application/msword', - 'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), - 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), - 'word' => array('application/msword', 'application/octet-stream'), - 'xl' => 'application/excel', - 'eml' => 'message/rfc822', - 'json' => array('application/json', 'text/json') - ); - function _print_r($var) - { - if( empty($var) ) - return false; - - print "
"; - print ""; - print "PRINT RESULT"; - print ""; - print "
";
-		print_r($var);
-		print "
"; - print "
"; - } - /** ========== C O N S T A N T E N D S H E R E ============ **/ /** @@ -149,11 +42,29 @@ function _print_r($var) * ========== I N C L U D E N E C E S S A R Y F I L E S =========== * */ -if( file_exists(BASEPATH . 'vendor') ){ +if( file_exists(BASEPATH . 'vendor'.DS.'autoload.php') ){ //require BASEPATH ."vendor/autoload.php"; - require __DIR__.'/../vendor/autoload.php'; + require BASEPATH . 'vendor'.DS.'autoload.php'; +} +else{ + exit("Autoload file does not exists. Please try to regenerate autoload file using command `composer dump-autoload`"); } use App\Functions; $func = new Functions(); -/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ \ No newline at end of file +/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ + +function _print_r($var) +{ + if( empty($var) ) + return false; + + print "
"; + print ""; + print "PRINT RESULT"; + print ""; + print "
";
+	print_r($var);
+	print "
"; + print "
"; +} \ No newline at end of file diff --git a/test.php b/test.php index 45744bb..89d4390 100644 --- a/test.php +++ b/test.php @@ -1,36 +1,21 @@ ".BASEPATH; - -$db = new DB(); - -//$db->where = "WHERE status='1'"; -$db->where( ['status'=>'1'], 'OR', 'AND' ); -//$db->where( ['id'=>'3'], 'OR', 'AND' ); -// $db->limit = '1'; -// $db->offset = '0'; -//$res = $db->getRow('admin'); - -$pass = new Password(md5('123456')); -/*$res = $db->update('admin',['password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]);*/ - -//$db->insert('admin', ['username'=>'akhtar', 'email'=>'abc@xyz.com', 'password'=>$pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]); -$res = $db->getCount('admin'); -_print_r($res); - -$admin = new AdminUser(1); // 1 = ID - -/*$admin->password = md5('123456'); -$pass = new Password(md5('123456')); -$admin->hash = $pass->hash; -$admin->commit();*/ - -_print_r($admin); - -$pass = new Password($admin->password); -_print_r($pass); - -echo $pass->verifyPassword() ? "SUCCESS" : "FAIL"; -/*$admin->password = "123456"; -$admin->commit();*/ \ No newline at end of file +use App\Upload; +if( isset($_POST['upload']) ) +{ + _print_r($_FILES); + $config['max_size'] = 1048576; + $config['max_width'] = 1024; + $config['max_height'] = 768; + + $upload = new Upload($_FILES['file1'],$config); + //_print_r($upload); + echo "File Name: ".$upload->doUpload(); +} +?> +
Upload file +
+ + +
+
\ No newline at end of file From f65739382b49bc7df2b011e1416d731506bd695d Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 11:05:45 +0530 Subject: [PATCH 13/41] New update 02-Nov-15 --- .gitignore | 5 +- .htaccess | 16 ++++ {lib => app}/Auth.php | 20 +++-- lib/config.php => app/Config.php | 18 ++-- {lib => app}/DB.php | 112 ++++++++++++++++++------- {lib => app}/Encryption.php | 0 lib/functions.php => app/Functions.php | 2 +- {lib => app}/Image.php | 0 {lib => app}/Mimes.php | 0 {lib => app}/Pagination.php | 0 {lib => app}/Password.php | 0 {lib => app}/Tables/AdminUser.php | 5 +- app/Tables/Options.php | 81 ++++++++++++++++++ {lib => app}/Upload.php | 8 +- app/Validation.php | 25 ++++++ app/index.html | 0 composer.json | 2 +- index.php | 47 +++++------ test.php | 21 ----- 19 files changed, 252 insertions(+), 110 deletions(-) create mode 100644 .htaccess rename {lib => app}/Auth.php (70%) rename lib/config.php => app/Config.php (88%) rename {lib => app}/DB.php (70%) rename {lib => app}/Encryption.php (100%) rename lib/functions.php => app/Functions.php (98%) rename {lib => app}/Image.php (100%) rename {lib => app}/Mimes.php (100%) rename {lib => app}/Pagination.php (100%) rename {lib => app}/Password.php (100%) rename {lib => app}/Tables/AdminUser.php (95%) create mode 100644 app/Tables/Options.php rename {lib => app}/Upload.php (90%) create mode 100644 app/Validation.php create mode 100644 app/index.html delete mode 100644 test.php diff --git a/.gitignore b/.gitignore index 634e433..464d473 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /vendor /uploads -/composer.lock -/test.php -/index.php \ No newline at end of file +/.idea +/composer.lock \ No newline at end of file diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..83159f5 --- /dev/null +++ b/.htaccess @@ -0,0 +1,16 @@ +RewriteEngine On +RewriteBase /admin-panel/ +ErrorDocument 404 http://localhost/admin-panel/?page=error +# + # + # Options -MultiViews + # + # RewriteEngine On + # RewriteBase /admin-panel/ + # Redirect Trailing Slashes... + # RewriteRule ^(.*)/$ /$1 [L,R=301] + # Handle Front Controller... + # RewriteCond %{REQUEST_FILENAME} !-d + # RewriteCond %{REQUEST_FILENAME} !-f + # RewriteRule ^(.*)$ $1.php [L] +# \ No newline at end of file diff --git a/lib/Auth.php b/app/Auth.php similarity index 70% rename from lib/Auth.php rename to app/Auth.php index ceef773..f0952b7 100644 --- a/lib/Auth.php +++ b/app/Auth.php @@ -8,7 +8,7 @@ * @version : 1.0 */ -abstract class Auth +class Auth { //protected function initialize(); public function login(){ @@ -18,28 +18,32 @@ public function login(){ $db->where( ['password' => $this->password],'AND' ); $data = $db->getRow($this->table); //_print_r($data); - if( count($data) > 0 && $pass->verifyPassword() ){ + if( $data->id > 0 && $pass->verifyPassword() ){ if( $n = $pass->needRehash() ){ $newHash = $pass->reHashPassword(); $db->where( ['email' => $this->email, 'username' => $this->username],'AND', "OR" ); $db->where( ['password' => $this->password],'AND' ); - $res = $db->update($this->table,['hash' => $newHash]); + $db->update($this->table,['hash' => $newHash]); } /************ SET SESSION VARIABLES HERE **************/ //session_start(); $_SESSION['logged'] = TRUE; $_SESSION['userid'] = $data->id; - $_SESSION['username'] = $data->username ? $data->username : ""; - + $_SESSION['username'] = $data->username; + $_SESSION['name'] = $data->display_name ? $data->display_name : ""; /****************** END SESSION SETTINGS **************/ return $data; } } - public function logout(){ + public static function isLoggedIn(){ + if( isset($_SESSION['logged']) ){ + return $_SESSION['logged']; + } + } + public static function logout(){ unset( $_SESSEION ); session_destroy(); - header("Location:".BASEURL."?action=logout"); + header("Location:".BASEURL); } - } \ No newline at end of file diff --git a/lib/config.php b/app/Config.php similarity index 88% rename from lib/config.php rename to app/Config.php index 3f75fd1..219f0cc 100644 --- a/lib/config.php +++ b/app/Config.php @@ -1,24 +1,24 @@ * @package : Admin Panel * @version : 1.0 */ - ob_start(); session_start(); const HOSTNAME = 'localhost'; -const DBNAME = 'akhtar_test'; +const DBNAME = 'admin_panel'; const USERNAME = 'root'; -const PASSWORD = ''; +const PASSWORD = '467936'; const DS = DIRECTORY_SEPARATOR; +const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; const ENVIRONMENT = 'development'; // OR production => live if( ENVIRONMENT == 'development' ){ - error_reporting(-1); + //error_reporting(-1); + error_reporting(E_ALL & E_WARNING & E_NOTICE); } else{ error_reporting(0); @@ -34,7 +34,6 @@ define( 'BASEPATH', $basepath ); define( 'BASEURL', $baseurl ); define( 'CURRENT_PAGE', $curPage ); - /** ========== C O N S T A N T E N D S H E R E ============ **/ /** @@ -49,20 +48,13 @@ else{ exit("Autoload file does not exists. Please try to regenerate autoload file using command `composer dump-autoload`"); } - -use App\Functions; -$func = new Functions(); /** =========== F I L E L O A D I N G E N D S H E R E =========== **/ - function _print_r($var) { if( empty($var) ) return false; print "
"; - print ""; - print "PRINT RESULT"; - print ""; print "
";
 	print_r($var);
 	print "
"; diff --git a/lib/DB.php b/app/DB.php similarity index 70% rename from lib/DB.php rename to app/DB.php index 49374b3..2593277 100644 --- a/lib/DB.php +++ b/app/DB.php @@ -44,37 +44,47 @@ function __construct(){ * */ - public function getResult($table, $fields) + public function getResult($table, $fields="*") { if( empty($table) ) return; - + $field = is_array($fields) ? implode(', ', $fields) : "*"; - + $sql = "SELECT ".$field." FROM `".$table."` "; $this->query = self::buildQuery($sql); - $stmt = $this->dbh->prepare($this->query); - $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); - $data = $stmt->fetchAll(PDO::FETCH_OBJ); + try{ + $stmt = $this->dbh->prepare($this->query); + $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); + $data = $stmt->fetchAll(PDO::FETCH_OBJ); + } + catch(PDO_Exception $e){ + $e->getMessage(); + } //print_r($data); self::reset(); return $data; } - - public function getRow($table, $fields) + + public function getRow($table, $fields="*") { if( empty($table) ) return; - + $field = is_array($fields) ? implode(', ', $fields) : "*"; - + $sql = "SELECT ".$field." FROM `".$table."` "; $this->query = self::buildQuery($sql); - $stmt = $this->dbh->prepare($this->query); - $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); - $data = $stmt->fetch(PDO::FETCH_OBJ); + try{ + $stmt = $this->dbh->prepare($this->query); + $stmt->execute( $this->arrValues ); // ? '' : "ERROR ".$this->dbh->errorInfo(); + $data = $stmt->fetch(PDO::FETCH_OBJ); + } + catch(PDO_Exception $e){ + $e->getMessage(); + } self::reset(); return $data; @@ -84,8 +94,8 @@ public function getRow($table, $fields) public function insert($table, $fieldVal) { /* - * $fieldVal is an associative array containing as - * $key=>$val , Where key = column name of table + * $fieldVal is an associative array containing as + * $key=>$val , Where key = column name of table */ if( !is_array($fieldVal) || empty($table) ) @@ -98,17 +108,47 @@ public function insert($table, $fieldVal) $fields[] = $key."=?"; $arrValues[] = $val; } - - $sql .= implode(", ", $fields); - $stmt = $this->dbh->prepare($sql); - return $stmt->execute($arrValues) ? $this->dbh->lastInsertId() : FALSE; + + try{ + $sql .= implode(", ", $fields); + $stmt = $this->dbh->prepare($sql); + return $stmt->execute($arrValues) ? $this->dbh->lastInsertId() : FALSE; + } + catch(PDO_Exception $e){ + $e->getMessage(); + } + } + public function batchInsert($table, $fieldNames=array(), $fieldValues=array()) + { + if( !is_array($fieldValues) || ! is_array($fieldNames) || empty($table) ) + return; + + $arrValues = array(); + $sql = "INSERT INTO `".$table."` ( ".implode(", ", $fieldNames)." ) VALUES "; + foreach( $fieldValues as $values){ + $val1 = array(); + foreach($values as $val){ + $val1[] = "'".$val."'"; + } + $arrValues[] = "( ".implode(", ", $val1)." )"; + } + + try{ + $sql .= implode(", ", $arrValues); + $stmt = $this->dbh->prepare($sql); + //_print_r($sql); + return $stmt->execute() ? TRUE : FALSE; + } + catch(PDO_Exception $e){ + $e->getMessage(); + } } public function update($table, $fieldVal) { /* - * $fieldVal is an associative array containing as - * $key=>$val , Where key = column name of table + * $fieldVal is an associative array containing as + * $key=>$val , Where key = column name of table */ if( !is_array($fieldVal) || empty($table) ) return; @@ -119,17 +159,22 @@ public function update($table, $fieldVal) $fields[] = $key."=?"; $this->arrValues[] = $val; } - + $sql .= implode(", ", $fields); $this->query = self::buildQuery($sql); - $stmt = $this->dbh->prepare($this->query); - $res = $stmt->execute($this->arrValues); - + try{ + $stmt = $this->dbh->prepare($this->query); + $res = $stmt->execute($this->arrValues); + } + catch(PDO_Exception $e){ + $e->getMessage(); + } + self::reset(); return $res; } - + public function delete( $table ) { if( empty($table) ) @@ -138,13 +183,18 @@ public function delete( $table ) $sql = "DELETE FROM `".$table."`"; $this->query = self::buildQuery($sql); - $stmt = $this->dbh->prepare($this->query); - $res = $stmt->execute($this->arrValues); + try{ + $stmt = $this->dbh->prepare($this->query); + $res = $stmt->execute($this->arrValues); + } + catch(PDO_Exception $e){ + $e->getMessage(); + } self::reset(); return $res; } - + public function getCount( $table, $field ) { if( empty($table) ) @@ -199,7 +249,7 @@ public function likeWhere( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr $this->where .= implode(" ".$afterOpr." ", $param); } - public function inWhere( $whereQuery = array(), $beforeOpr = 'AND' ){ + public function inWhere( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr="AND" ){ if( empty($this->where) ){ $this->where = "WHERE "; } @@ -232,7 +282,7 @@ private function buildQuery( $sql ) $query .= !empty($this->limit) ? " LIMIT ".$this->limit." " : ""; $query .= !empty($this->offset) ? " OFFSET ".$this->offset." " : ""; - Functions::showQuery($query,$this->arrValues); + //Functions::showQuery($query,$this->arrValues); return $query; } diff --git a/lib/Encryption.php b/app/Encryption.php similarity index 100% rename from lib/Encryption.php rename to app/Encryption.php diff --git a/lib/functions.php b/app/Functions.php similarity index 98% rename from lib/functions.php rename to app/Functions.php index 2e6f728..ec2cf2e 100644 --- a/lib/functions.php +++ b/app/Functions.php @@ -116,7 +116,7 @@ static function generateKey( $length = 12, $special_chars = true, $extra_special /* * Function to generate a random number. */ - static function _rand( $min = 0, $max = 0 ) { + static function _rand( $min = 111111, $max = 999999 ) { $rnd_value=''; if ( strlen($rnd_value) < 8 ) { diff --git a/lib/Image.php b/app/Image.php similarity index 100% rename from lib/Image.php rename to app/Image.php diff --git a/lib/Mimes.php b/app/Mimes.php similarity index 100% rename from lib/Mimes.php rename to app/Mimes.php diff --git a/lib/Pagination.php b/app/Pagination.php similarity index 100% rename from lib/Pagination.php rename to app/Pagination.php diff --git a/lib/Password.php b/app/Password.php similarity index 100% rename from lib/Password.php rename to app/Password.php diff --git a/lib/Tables/AdminUser.php b/app/Tables/AdminUser.php similarity index 95% rename from lib/Tables/AdminUser.php rename to app/Tables/AdminUser.php index 52f2d33..50fcfae 100644 --- a/lib/Tables/AdminUser.php +++ b/app/Tables/AdminUser.php @@ -41,6 +41,7 @@ function __construct($var=0) if($var>0) { $db = new DB(); + $db->where(['id' => $var]); $obj = $db->getRow( $this->table, '*' ); if(is_object($obj)) @@ -85,7 +86,7 @@ function commit() function update() { $db = new DB(); - $pass = new Password( md5($this->password) ); + $pass = new Password( $this->password ); $fieldSet = ['display_name' => $this->display_name, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]; $db->where( ['id'=>$this->id] ); @@ -100,7 +101,7 @@ function update() function add() { $db = new DB(); - $pass = new Password( md5($this->password) ); + $pass = new Password( $this->password ); $fieldSet = ['username' => $this->username, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'display_name' => $this->display_name, 'updated_on' => date('Y-m-d H:i:s'), 'status'=>'1']; $id = $db->insert( $this->table, $fieldSet ); diff --git a/app/Tables/Options.php b/app/Tables/Options.php new file mode 100644 index 0000000..27f27ac --- /dev/null +++ b/app/Tables/Options.php @@ -0,0 +1,81 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class Options +{ + public $table = "options"; + /** + * Perform Update Query for User. + * @return pointer + */ + function update($arrField = array()) + { + if(count($arrField) > 1){ + return FALSE; + } + $key = array_keys($arrField); + $val = $arrField[$key[0]]; + $db = new DB(); + $fieldSet = ['option_value' => $val]; + $db->where( ['option_key' => $key[0]] ); + return $db->update( $this->table, $fieldSet ); + } + + /** + * Perform Inser Query for Option. + * @return inserted id + */ + + /*----------- USED FOR FIRST TIME TO CONFIGE OPTION TABLE -----------*/ + function add($arrField = array()) + { + if(count($arrField) > 1){ + return FALSE; + } + $key = array_keys($arrField); + $val = $arrField[$key[0]]; + $db = new DB(); + $fieldSet = ['option_key' => $key[0], 'option_value' => $val]; + $id = $db->insert( $this->table, $fieldSet ); + $this->id = $id; + return $id; + } + + function getOption($key = "") + { + if($key != "") + { + $db = new DB(); + $db->where(['option_key' => $key]); + $obj = $db->getRow($this->table, ['option_value']); + + if(is_object($obj)) + { + return $obj->option_value; + } + } + else + return FALSE; + + } + + /** + * Delete user's record. + * @return pointer + */ + function remove($key) + { + $db = new DB(); + + $db->where( ['option_key' => $key] ); + return $db->delete( $this->table ); + } + +} \ No newline at end of file diff --git a/lib/Upload.php b/app/Upload.php similarity index 90% rename from lib/Upload.php rename to app/Upload.php index a6246aa..0a7e5ad 100644 --- a/lib/Upload.php +++ b/app/Upload.php @@ -1,6 +1,6 @@ file['name'], PATHINFO_EXTENSION); if( in_array( $this->fileType, ['image', 'audio', 'video'] ) ){ - $fileName = time().rand(0,10).".".$ext; + $fileName = date("Ymd_His").Functions::_rand().".".$ext; $targetFile = $this->dir.$fileName; if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ - if( $this->fileType == 'image' ){ + if( $this->fileType == 'image' && $this->file['name'] !== 'site_logo' ){ $image = new Image( $targetFile ); $image->load($targetFile); @@ -83,7 +83,7 @@ public function doUpload() return $fileName; } else{ - $fileName = time().rand(0,10).".".$ext; + $fileName = date("Ymd_His").Functions::_rand().".".$ext; $targetFile = $this->dir.$fileName; if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ return $fileName; diff --git a/app/Validation.php b/app/Validation.php new file mode 100644 index 0000000..b82c1c8 --- /dev/null +++ b/app/Validation.php @@ -0,0 +1,25 @@ +arr = $arr; + } + + public function validate(){ + foreach($this->arr as $key => $value){ + if($value == "" || empty(trim($this->arr[$key]))){ + $this->error[$key] = "This field can't be empty"; + } + } + if(count($this->error)){ + return $this->error; + } + else{ + return TRUE; + } + } +} diff --git a/app/index.html b/app/index.html new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json index 6d9b418..3315653 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ }, "autoload": { "psr-4": { - "App\\": "lib/" + "App\\": "app/" } } } \ No newline at end of file diff --git a/index.php b/index.php index cefd793..39f01eb 100644 --- a/index.php +++ b/index.php @@ -1,33 +1,28 @@ ".BASEPATH; +$page = isset($_REQUEST['page']) ? basename($_REQUEST['page'], ".php") : "index"; +$title = ($page != "index") ? ucwords($page) : "Admin"; -$db = new DB(); - -echo "
Password: ".$func->generateKey(); +if( ! in_array($page, $exceptPages) ){ + include_once BASEPATH."pages".DS."header.php"; + include_once BASEPATH."pages".DS."sidebar.php"; +} ?> -
Admin + -
- -
Upload + + doUpload(); -?> -
- +if( ! in_array($page, $exceptPages) ){ + include_once BASEPATH."pages".DS."footer.php"; +} +?> \ No newline at end of file diff --git a/test.php b/test.php deleted file mode 100644 index 89d4390..0000000 --- a/test.php +++ /dev/null @@ -1,21 +0,0 @@ -doUpload(); -} -?> -
Upload file -
- - -
-
\ No newline at end of file From 1f79cd66f270677a574c5f23940c0bdadcaef5fe Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 11:10:42 +0530 Subject: [PATCH 14/41] add html to table dir --- app/Tables/index.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/Tables/index.html diff --git a/app/Tables/index.html b/app/Tables/index.html new file mode 100644 index 0000000..e69de29 From 11130687d8d1923f26458f78d7b0fbf12d108bb6 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 11:27:23 +0530 Subject: [PATCH 15/41] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 407c154..e63ea06 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PDO-Library Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 autoloading is used. -To use this library, you need to only include config.php file from 'lib' directory. +To use this library, you need to only include config.php file from 'app' directory. -example : require_nce "lib/config.php"; +example : require_nce "app/config.php"; From 98188a0ab72ff859c160c71ea97e91f1e3eaf6ab Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 12:11:09 +0530 Subject: [PATCH 16/41] add html to table dir --- app/Config.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Config.php b/app/Config.php index 219f0cc..c3947a4 100644 --- a/app/Config.php +++ b/app/Config.php @@ -26,7 +26,10 @@ $basepath = realpath( dirname( dirname(__FILE__) ) ); $httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; -$baseurl = $httpProt.$_SERVER['HTTP_HOST'].str_replace(DS, '/', strrchr($basepath, DS)).'/'; +//$baseurl = $httpProt.$_SERVER['HTTP_HOST'].str_replace(DS, '/', strrchr($basepath, DS)).'/'; + +$baseurl = str_replace(basename($_SERVER['REQUEST_URI']), "", $_SERVER['REQUEST_URI']); +$baseurl = $httpProt.$_SERVER['HTTP_HOST'].$baseurl; $basepath = $basepath.DS; $curPage = basename($_SERVER['SCRIPT_NAME'], '.php'); From 441fc22701d6686490b846e98671d4a59718c3e1 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 12:38:56 +0530 Subject: [PATCH 17/41] config changed --- app/Config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Config.php b/app/Config.php index c3947a4..792ba80 100644 --- a/app/Config.php +++ b/app/Config.php @@ -10,7 +10,7 @@ const HOSTNAME = 'localhost'; const DBNAME = 'admin_panel'; const USERNAME = 'root'; -const PASSWORD = '467936'; +const PASSWORD = ''; const DS = DIRECTORY_SEPARATOR; const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; @@ -28,7 +28,7 @@ $httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; //$baseurl = $httpProt.$_SERVER['HTTP_HOST'].str_replace(DS, '/', strrchr($basepath, DS)).'/'; -$baseurl = str_replace(basename($_SERVER['REQUEST_URI']), "", $_SERVER['REQUEST_URI']); +$baseurl = strstr($_SERVER['REQUEST_URI'], '?') != "" ? str_replace(basename($_SERVER['REQUEST_URI']), "", $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; $baseurl = $httpProt.$_SERVER['HTTP_HOST'].$baseurl; $basepath = $basepath.DS; From d6f184e600df9be484c7f607cf691be01261cf2b Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 2 Nov 2015 17:48:58 +0530 Subject: [PATCH 18/41] final change on 02 Nov --- app/Config.php | 4 ++-- app/Image.php | 1 + app/Tables/index.html | 0 app/Upload.php | 5 +++-- 4 files changed, 6 insertions(+), 4 deletions(-) delete mode 100644 app/Tables/index.html diff --git a/app/Config.php b/app/Config.php index 792ba80..4825b5c 100644 --- a/app/Config.php +++ b/app/Config.php @@ -17,8 +17,8 @@ const ENVIRONMENT = 'development'; // OR production => live if( ENVIRONMENT == 'development' ){ - //error_reporting(-1); - error_reporting(E_ALL & E_WARNING & E_NOTICE); + error_reporting(-1); + //error_reporting(E_ALL & E_WARNING & E_NOTICE); } else{ error_reporting(0); diff --git a/app/Image.php b/app/Image.php index 24425ab..1939647 100644 --- a/app/Image.php +++ b/app/Image.php @@ -14,6 +14,7 @@ class Image function load($filename) { $image_info = getimagesize($filename); + //_print_r($image_info); $this->image_type = $image_info[2]; if ($this->image_type == IMAGETYPE_JPEG) { $this->image = imagecreatefromjpeg($filename); diff --git a/app/Tables/index.html b/app/Tables/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/app/Upload.php b/app/Upload.php index 0a7e5ad..c43edf0 100644 --- a/app/Upload.php +++ b/app/Upload.php @@ -17,7 +17,7 @@ class Upload public function __construct($file, $config="") { // 1MB = 1048576 KB. - include_once BASEPATH."lib".DS."Mimes.php"; + include_once BASEPATH."app".DS."Mimes.php"; if( !empty($file) ){ $this->file = $file; $this->config = $config; @@ -68,7 +68,7 @@ public function doUpload() $fileName = date("Ymd_His").Functions::_rand().".".$ext; $targetFile = $this->dir.$fileName; if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ - if( $this->fileType == 'image' && $this->file['name'] !== 'site_logo' ){ + if( $this->fileType == 'image' ){ $image = new Image( $targetFile ); $image->load($targetFile); @@ -91,4 +91,5 @@ public function doUpload() } } + } \ No newline at end of file From c8eb854bcfcbe98cb537f9d7509458e1820f2363 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Tue, 3 Nov 2015 17:10:28 +0530 Subject: [PATCH 19/41] composer modified --- composer.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 3315653..19f20f8 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,18 @@ { "name": "PDO Library", "description": "The PDO 5.6 Framework.", - "keywords": ["Library", "PHP Library"], - "license": "MIT", + "keywords": ["PHP Library", "PHP Library"], "type": "project", "require": { - "php": ">=5.4" + "php": ">=5.4", + "raveren/kint": "0.9" }, "autoload": { "psr-4": { "App\\": "app/" } + }, + "require-dev":{ + "phpmailer/phpmailer": "5.2.*" } } \ No newline at end of file From d504811c9f928e67e9539c98eaffbc5620fd2b66 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 12:17:06 +0530 Subject: [PATCH 20/41] twig template added --- .gitignore | 8 +++++++- app/Config.php | 5 +---- app/Controllers/index.html | 0 app/Models/index.html | 0 app/{ => System}/Auth.php | 4 ++-- app/System/Controller.php | 34 +++++++++++++++++++++++++++++++++ app/{ => System}/DB.php | 2 +- app/{ => System}/Encryption.php | 2 +- app/{ => System}/Functions.php | 2 +- app/{ => System}/Image.php | 2 +- app/{ => System}/Mimes.php | 0 app/{ => System}/Pagination.php | 2 +- app/{ => System}/Password.php | 2 +- app/{ => System}/Upload.php | 6 +++--- app/{ => System}/Validation.php | 2 +- app/System/index.html | 0 app/Tables/index.html | 0 app/Views/index.html | 0 composer.json | 7 ++++--- index-old.php | 27 ++++++++++++++++++++++++++ index.php | 28 ++------------------------- 21 files changed, 87 insertions(+), 46 deletions(-) create mode 100644 app/Controllers/index.html create mode 100644 app/Models/index.html rename app/{ => System}/Auth.php (95%) create mode 100644 app/System/Controller.php rename app/{ => System}/DB.php (99%) rename app/{ => System}/Encryption.php (96%) rename app/{ => System}/Functions.php (99%) rename app/{ => System}/Image.php (99%) rename app/{ => System}/Mimes.php (100%) rename app/{ => System}/Pagination.php (98%) rename app/{ => System}/Password.php (98%) rename app/{ => System}/Upload.php (95%) rename app/{ => System}/Validation.php (95%) create mode 100644 app/System/index.html create mode 100644 app/Tables/index.html create mode 100644 app/Views/index.html create mode 100644 index-old.php diff --git a/.gitignore b/.gitignore index 464d473..bc2bf15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,10 @@ /vendor /uploads /.idea -/composer.lock \ No newline at end of file +/templates +/cache + +/index.php +/composer.lock +/test.php +/index-old.php \ No newline at end of file diff --git a/app/Config.php b/app/Config.php index 4825b5c..2a37561 100644 --- a/app/Config.php +++ b/app/Config.php @@ -26,10 +26,7 @@ $basepath = realpath( dirname( dirname(__FILE__) ) ); $httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; -//$baseurl = $httpProt.$_SERVER['HTTP_HOST'].str_replace(DS, '/', strrchr($basepath, DS)).'/'; - -$baseurl = strstr($_SERVER['REQUEST_URI'], '?') != "" ? str_replace(basename($_SERVER['REQUEST_URI']), "", $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; -$baseurl = $httpProt.$_SERVER['HTTP_HOST'].$baseurl; +$baseurl = $httpProt.$_SERVER['HTTP_HOST'].'/'.substr( $_SERVER['REQUEST_URI'], strpos( $_SERVER['REQUEST_URI'], '/')+1, strrpos($_SERVER['REQUEST_URI'], '/') ); $basepath = $basepath.DS; $curPage = basename($_SERVER['SCRIPT_NAME'], '.php'); diff --git a/app/Controllers/index.html b/app/Controllers/index.html new file mode 100644 index 0000000..e69de29 diff --git a/app/Models/index.html b/app/Models/index.html new file mode 100644 index 0000000..e69de29 diff --git a/app/Auth.php b/app/System/Auth.php similarity index 95% rename from app/Auth.php rename to app/System/Auth.php index f0952b7..51cf1cb 100644 --- a/app/Auth.php +++ b/app/System/Auth.php @@ -1,6 +1,6 @@ diff --git a/app/System/Controller.php b/app/System/Controller.php new file mode 100644 index 0000000..461a85f --- /dev/null +++ b/app/System/Controller.php @@ -0,0 +1,34 @@ + 'cache') : array() ); + } + + /*-------------- FUNCTION TO LOAD VIEW -----------------*/ + public function loadView($template, $data=array()){ + if( ! is_array($data) ){ + return; + } + + $template = $this->twig->loadTemplate($template); + $template->display($data); + } + + /*-------------- FUNCTION TO LOAD MODEL -----------------*/ + public function loadModel($model){ + if( ! is_array($data) ){ + return; + } + + $template = $this->twig->loadTemplate($template); + $template->display($data); + } +} \ No newline at end of file diff --git a/app/DB.php b/app/System/DB.php similarity index 99% rename from app/DB.php rename to app/System/DB.php index 2593277..aa8712b 100644 --- a/app/DB.php +++ b/app/System/DB.php @@ -1,5 +1,5 @@ diff --git a/app/Encryption.php b/app/System/Encryption.php similarity index 96% rename from app/Encryption.php rename to app/System/Encryption.php index fb7f7d2..592debe 100644 --- a/app/Encryption.php +++ b/app/System/Encryption.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/app/Functions.php b/app/System/Functions.php similarity index 99% rename from app/Functions.php rename to app/System/Functions.php index ec2cf2e..4353bcd 100644 --- a/app/Functions.php +++ b/app/System/Functions.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/app/Image.php b/app/System/Image.php similarity index 99% rename from app/Image.php rename to app/System/Image.php index 1939647..f971e57 100644 --- a/app/Image.php +++ b/app/System/Image.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/app/Mimes.php b/app/System/Mimes.php similarity index 100% rename from app/Mimes.php rename to app/System/Mimes.php diff --git a/app/Pagination.php b/app/System/Pagination.php similarity index 98% rename from app/Pagination.php rename to app/System/Pagination.php index c6e1cfd..5fe1261 100644 --- a/app/Pagination.php +++ b/app/System/Pagination.php @@ -1,5 +1,5 @@ diff --git a/app/Password.php b/app/System/Password.php similarity index 98% rename from app/Password.php rename to app/System/Password.php index c6eedd4..4f33669 100644 --- a/app/Password.php +++ b/app/System/Password.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/app/Upload.php b/app/System/Upload.php similarity index 95% rename from app/Upload.php rename to app/System/Upload.php index c43edf0..dcefdfe 100644 --- a/app/Upload.php +++ b/app/System/Upload.php @@ -1,6 +1,6 @@ file = $file; $this->config = $config; diff --git a/app/Validation.php b/app/System/Validation.php similarity index 95% rename from app/Validation.php rename to app/System/Validation.php index b82c1c8..316f505 100644 --- a/app/Validation.php +++ b/app/System/Validation.php @@ -1,5 +1,5 @@ =5.4", - "raveren/kint": "0.9" + "php": ">=5.4" }, "autoload": { "psr-4": { @@ -13,6 +12,8 @@ } }, "require-dev":{ - "phpmailer/phpmailer": "5.2.*" + "phpmailer/phpmailer": "5.2.*", + "twig/twig":"~1.0", + "raveren/kint": "0.9" } } \ No newline at end of file diff --git a/index-old.php b/index-old.php new file mode 100644 index 0000000..a83a3a1 --- /dev/null +++ b/index-old.php @@ -0,0 +1,27 @@ + + + + + + - - - - - \ No newline at end of file +_print_r(BASEURL); +_print_r(BASEPATH); \ No newline at end of file From 6b3f5292c31aeff19d309b3c37ad6788c2e9e27c Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 12:20:36 +0530 Subject: [PATCH 21/41] Delete index-old.php --- index-old.php | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 index-old.php diff --git a/index-old.php b/index-old.php deleted file mode 100644 index a83a3a1..0000000 --- a/index-old.php +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - Date: Thu, 5 Nov 2015 12:20:50 +0530 Subject: [PATCH 22/41] Delete index.php --- index.php | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 index.php diff --git a/index.php b/index.php deleted file mode 100644 index a61f4b8..0000000 --- a/index.php +++ /dev/null @@ -1,4 +0,0 @@ - Date: Thu, 5 Nov 2015 12:23:25 +0530 Subject: [PATCH 23/41] changed --- .gitignore | 8 +-- app/Tables/AdminUser.php | 125 --------------------------------------- app/Tables/Options.php | 81 ------------------------- app/Tables/index.html | 0 4 files changed, 4 insertions(+), 210 deletions(-) delete mode 100644 app/Tables/AdminUser.php delete mode 100644 app/Tables/Options.php delete mode 100644 app/Tables/index.html diff --git a/.gitignore b/.gitignore index bc2bf15..af6618d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ /templates /cache -/index.php -/composer.lock -/test.php -/index-old.php \ No newline at end of file +index.php +index-old.php +composer.lock +test.php \ No newline at end of file diff --git a/app/Tables/AdminUser.php b/app/Tables/AdminUser.php deleted file mode 100644 index 50fcfae..0000000 --- a/app/Tables/AdminUser.php +++ /dev/null @@ -1,125 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -final class AdminUser extends Auth -{ - ///////////////////////////////////////////////// - // PROPERTIES, PUBLIC - ///////////////////////////////////////////////// - - public $table = 'admin'; - - public $id; - - public $username; - - public $password; - - public $hash; - - public $email; - - public $display_name; - - public $updated_on; - - public $status; - - /** - * Constructor - * @return void - */ - function __construct($var=0) - { - if($var>0) - { - $db = new DB(); - $db->where(['id' => $var]); - $obj = $db->getRow( $this->table, '*' ); - - if(is_object($obj)) - { - $this->id = stripslashes($obj->id); - $this->username = stripslashes($obj->username); - $this->password = stripslashes($obj->password); - $this->hash = stripslashes($obj->hash); - $this->email = stripslashes($obj->email); - $this->display_name = stripslashes($obj->display_name); - $this->status = stripslashes($obj->status); - $this->updated_on = stripslashes($obj->updated_on); - } - } - - } - - /** - * Commit DB query. - * @return pointer - */ - function commit() - { - $db = new DB(); - - if($this->id>0) - { - //do update - $res=self::update(); - } - else - {//do insert - $res=self::add(); - } - return $res; - } - - /** - * Perform Update Query for User. - * @return pointer - */ - function update() - { - $db = new DB(); - $pass = new Password( $this->password ); - - $fieldSet = ['display_name' => $this->display_name, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]; - $db->where( ['id'=>$this->id] ); - $res = $db->update( $this->table, $fieldSet ); - return $res; - } - - /** - * Perform Inser Query for User. - * @return inserted id - */ - function add() - { - $db = new DB(); - $pass = new Password( $this->password ); - - $fieldSet = ['username' => $this->username, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'display_name' => $this->display_name, 'updated_on' => date('Y-m-d H:i:s'), 'status'=>'1']; - $id = $db->insert( $this->table, $fieldSet ); - $this->id = $id; - return $id; - } - - /** - * Delete user's record. - * @return pointer - */ - function remove() - { - $db = new DB(); - - $db->where( ['id' => $this->id] ); - $res = $db->delete( $this->table ); - return $res; - } - -} \ No newline at end of file diff --git a/app/Tables/Options.php b/app/Tables/Options.php deleted file mode 100644 index 27f27ac..0000000 --- a/app/Tables/Options.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -final class Options -{ - public $table = "options"; - /** - * Perform Update Query for User. - * @return pointer - */ - function update($arrField = array()) - { - if(count($arrField) > 1){ - return FALSE; - } - $key = array_keys($arrField); - $val = $arrField[$key[0]]; - $db = new DB(); - $fieldSet = ['option_value' => $val]; - $db->where( ['option_key' => $key[0]] ); - return $db->update( $this->table, $fieldSet ); - } - - /** - * Perform Inser Query for Option. - * @return inserted id - */ - - /*----------- USED FOR FIRST TIME TO CONFIGE OPTION TABLE -----------*/ - function add($arrField = array()) - { - if(count($arrField) > 1){ - return FALSE; - } - $key = array_keys($arrField); - $val = $arrField[$key[0]]; - $db = new DB(); - $fieldSet = ['option_key' => $key[0], 'option_value' => $val]; - $id = $db->insert( $this->table, $fieldSet ); - $this->id = $id; - return $id; - } - - function getOption($key = "") - { - if($key != "") - { - $db = new DB(); - $db->where(['option_key' => $key]); - $obj = $db->getRow($this->table, ['option_value']); - - if(is_object($obj)) - { - return $obj->option_value; - } - } - else - return FALSE; - - } - - /** - * Delete user's record. - * @return pointer - */ - function remove($key) - { - $db = new DB(); - - $db->where( ['option_key' => $key] ); - return $db->delete( $this->table ); - } - -} \ No newline at end of file diff --git a/app/Tables/index.html b/app/Tables/index.html deleted file mode 100644 index e69de29..0000000 From 71d6e4f25a32cee6306ac46137d6a1b3616fa7be Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 12:30:43 +0530 Subject: [PATCH 24/41] change --- app/Models/AdminUser.php | 125 +++++++++++++++++++++++++++++++++++++++ app/Models/Options.php | 81 +++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 app/Models/AdminUser.php create mode 100644 app/Models/Options.php diff --git a/app/Models/AdminUser.php b/app/Models/AdminUser.php new file mode 100644 index 0000000..be5648a --- /dev/null +++ b/app/Models/AdminUser.php @@ -0,0 +1,125 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class AdminUser extends Auth +{ + ///////////////////////////////////////////////// + // PROPERTIES, PUBLIC + ///////////////////////////////////////////////// + + public $table = 'admin'; + + public $id; + + public $username; + + public $password; + + public $hash; + + public $email; + + public $display_name; + + public $updated_on; + + public $status; + + /** + * Constructor + * @return void + */ + function __construct($var=0) + { + if($var>0) + { + $db = new DB(); + $db->where(['id' => $var]); + $obj = $db->getRow( $this->table, '*' ); + + if(is_object($obj)) + { + $this->id = stripslashes($obj->id); + $this->username = stripslashes($obj->username); + $this->password = stripslashes($obj->password); + $this->hash = stripslashes($obj->hash); + $this->email = stripslashes($obj->email); + $this->display_name = stripslashes($obj->display_name); + $this->status = stripslashes($obj->status); + $this->updated_on = stripslashes($obj->updated_on); + } + } + + } + + /** + * Commit DB query. + * @return pointer + */ + function commit() + { + $db = new DB(); + + if($this->id>0) + { + //do update + $res=self::update(); + } + else + {//do insert + $res=self::add(); + } + return $res; + } + + /** + * Perform Update Query for User. + * @return pointer + */ + function update() + { + $db = new DB(); + $pass = new Password( $this->password ); + + $fieldSet = ['display_name' => $this->display_name, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'updated_on' => date('Y-m-d H:i:s')]; + $db->where( ['id'=>$this->id] ); + $res = $db->update( $this->table, $fieldSet ); + return $res; + } + + /** + * Perform Inser Query for User. + * @return inserted id + */ + function add() + { + $db = new DB(); + $pass = new Password( $this->password ); + + $fieldSet = ['username' => $this->username, 'email' => $this->email, 'password' => $pass->password, 'hash' => $pass->hash, 'display_name' => $this->display_name, 'updated_on' => date('Y-m-d H:i:s'), 'status'=>'1']; + $id = $db->insert( $this->table, $fieldSet ); + $this->id = $id; + return $id; + } + + /** + * Delete user's record. + * @return pointer + */ + function remove() + { + $db = new DB(); + + $db->where( ['id' => $this->id] ); + $res = $db->delete( $this->table ); + return $res; + } + +} \ No newline at end of file diff --git a/app/Models/Options.php b/app/Models/Options.php new file mode 100644 index 0000000..d3ef69a --- /dev/null +++ b/app/Models/Options.php @@ -0,0 +1,81 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +final class Options +{ + public $table = "options"; + /** + * Perform Update Query for User. + * @return pointer + */ + function update($arrField = array()) + { + if(count($arrField) > 1){ + return FALSE; + } + $key = array_keys($arrField); + $val = $arrField[$key[0]]; + $db = new DB(); + $fieldSet = ['option_value' => $val]; + $db->where( ['option_key' => $key[0]] ); + return $db->update( $this->table, $fieldSet ); + } + + /** + * Perform Inser Query for Option. + * @return inserted id + */ + + /*----------- USED FOR FIRST TIME TO CONFIGE OPTION TABLE -----------*/ + function add($arrField = array()) + { + if(count($arrField) > 1){ + return FALSE; + } + $key = array_keys($arrField); + $val = $arrField[$key[0]]; + $db = new DB(); + $fieldSet = ['option_key' => $key[0], 'option_value' => $val]; + $id = $db->insert( $this->table, $fieldSet ); + $this->id = $id; + return $id; + } + + function getOption($key = "") + { + if($key != "") + { + $db = new DB(); + $db->where(['option_key' => $key]); + $obj = $db->getRow($this->table, ['option_value']); + + if(is_object($obj)) + { + return $obj->option_value; + } + } + else + return FALSE; + + } + + /** + * Delete user's record. + * @return pointer + */ + function remove($key) + { + $db = new DB(); + + $db->where( ['option_key' => $key] ); + return $db->delete( $this->table ); + } + +} \ No newline at end of file From e9d63545fdfb8fdcccc9e8f95731a0dc1f4dea30 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 16:12:27 +0530 Subject: [PATCH 25/41] config changed --- app/Config.php | 62 --------------------------------------------- app/System/Auth.php | 2 +- 2 files changed, 1 insertion(+), 63 deletions(-) delete mode 100644 app/Config.php diff --git a/app/Config.php b/app/Config.php deleted file mode 100644 index 2a37561..0000000 --- a/app/Config.php +++ /dev/null @@ -1,62 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ -ob_start(); -session_start(); - -const HOSTNAME = 'localhost'; -const DBNAME = 'admin_panel'; -const USERNAME = 'root'; -const PASSWORD = ''; - -const DS = DIRECTORY_SEPARATOR; -const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; -const ENVIRONMENT = 'development'; // OR production => live - -if( ENVIRONMENT == 'development' ){ - error_reporting(-1); - //error_reporting(E_ALL & E_WARNING & E_NOTICE); -} -else{ - error_reporting(0); -} - -$basepath = realpath( dirname( dirname(__FILE__) ) ); -$httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; -$baseurl = $httpProt.$_SERVER['HTTP_HOST'].'/'.substr( $_SERVER['REQUEST_URI'], strpos( $_SERVER['REQUEST_URI'], '/')+1, strrpos($_SERVER['REQUEST_URI'], '/') ); - -$basepath = $basepath.DS; -$curPage = basename($_SERVER['SCRIPT_NAME'], '.php'); - -define( 'BASEPATH', $basepath ); -define( 'BASEURL', $baseurl ); -define( 'CURRENT_PAGE', $curPage ); -/** ========== C O N S T A N T E N D S H E R E ============ **/ - -/** - * - * ========== I N C L U D E N E C E S S A R Y F I L E S =========== - * - */ -if( file_exists(BASEPATH . 'vendor'.DS.'autoload.php') ){ - //require BASEPATH ."vendor/autoload.php"; - require BASEPATH . 'vendor'.DS.'autoload.php'; -} -else{ - exit("Autoload file does not exists. Please try to regenerate autoload file using command `composer dump-autoload`"); -} -/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ -function _print_r($var) -{ - if( empty($var) ) - return false; - - print "
"; - print "
";
-	print_r($var);
-	print "
"; - print "
"; -} \ No newline at end of file diff --git a/app/System/Auth.php b/app/System/Auth.php index 51cf1cb..f3de6c5 100644 --- a/app/System/Auth.php +++ b/app/System/Auth.php @@ -8,7 +8,7 @@ * @version : 1.0 */ -class Auth +abstract class Auth { //protected function initialize(); public function login(){ From 5260353367586325a6e2a99265641d63544207a3 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 16:13:26 +0530 Subject: [PATCH 26/41] config changed --- app/Bootstrap.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 app/Bootstrap.php diff --git a/app/Bootstrap.php b/app/Bootstrap.php new file mode 100644 index 0000000..2a37561 --- /dev/null +++ b/app/Bootstrap.php @@ -0,0 +1,62 @@ + + * @package : Admin Panel + * @version : 1.0 + */ +ob_start(); +session_start(); + +const HOSTNAME = 'localhost'; +const DBNAME = 'admin_panel'; +const USERNAME = 'root'; +const PASSWORD = ''; + +const DS = DIRECTORY_SEPARATOR; +const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; +const ENVIRONMENT = 'development'; // OR production => live + +if( ENVIRONMENT == 'development' ){ + error_reporting(-1); + //error_reporting(E_ALL & E_WARNING & E_NOTICE); +} +else{ + error_reporting(0); +} + +$basepath = realpath( dirname( dirname(__FILE__) ) ); +$httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; +$baseurl = $httpProt.$_SERVER['HTTP_HOST'].'/'.substr( $_SERVER['REQUEST_URI'], strpos( $_SERVER['REQUEST_URI'], '/')+1, strrpos($_SERVER['REQUEST_URI'], '/') ); + +$basepath = $basepath.DS; +$curPage = basename($_SERVER['SCRIPT_NAME'], '.php'); + +define( 'BASEPATH', $basepath ); +define( 'BASEURL', $baseurl ); +define( 'CURRENT_PAGE', $curPage ); +/** ========== C O N S T A N T E N D S H E R E ============ **/ + +/** + * + * ========== I N C L U D E N E C E S S A R Y F I L E S =========== + * + */ +if( file_exists(BASEPATH . 'vendor'.DS.'autoload.php') ){ + //require BASEPATH ."vendor/autoload.php"; + require BASEPATH . 'vendor'.DS.'autoload.php'; +} +else{ + exit("Autoload file does not exists. Please try to regenerate autoload file using command `composer dump-autoload`"); +} +/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ +function _print_r($var) +{ + if( empty($var) ) + return false; + + print "
"; + print "
";
+	print_r($var);
+	print "
"; + print "
"; +} \ No newline at end of file From 12e7e3cd30a8fddaa0ef827d21eba20d98afc4a5 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 16:14:37 +0530 Subject: [PATCH 27/41] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e63ea06..1da8819 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PDO-Library Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 autoloading is used. -To use this library, you need to only include config.php file from 'app' directory. +To use this library, you need to only include Bootstrap.php file from 'app' directory. -example : require_nce "app/config.php"; +example : require_nce "app/Bootstrap.php"; From a6d1a98155e81c2c48cf2247d494b995970c7bb7 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 16:14:51 +0530 Subject: [PATCH 28/41] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1da8819..c0c7b34 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 aut To use this library, you need to only include Bootstrap.php file from 'app' directory. -example : require_nce "app/Bootstrap.php"; +example : require_once "app/Bootstrap.php"; From c6474c0fbc486aac3cd53d871952299d2f713d78 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 16:42:41 +0530 Subject: [PATCH 29/41] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c0c7b34..c0b8961 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # PDO-Library -Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 autoloading is used. +Simple PHP Mini Framework with PDO and PHP 5.6.0. +-- To load files automatically PSR 4 autoloading is used. +-- For Templating Twig template engine. +-- For Debugging Kint debugger. +-- For SMTP mails phpMailer. To use this library, you need to only include Bootstrap.php file from 'app' directory. From d4fc0538deb9e89b87ad894f0fc6097ef68cf151 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 5 Nov 2015 17:12:44 +0530 Subject: [PATCH 30/41] composer changed --- README.md | 10 +- app/Bootstrap.php | 13 +- app/System/Controller.php | 3 +- app/System/Functions.php | 309 ++++++++++++++++++++------------------ composer.json | 5 +- 5 files changed, 173 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index e63ea06..254658f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # PDO-Library -Simple PHP library with PDO and PHP 5.6.0. To load files automatically PSR 4 autoloading is used. +Simple PHP Mini Framework with PDO and PHP 5.6.0. +-- To load files automatically PSR 4 autoloading is used. +-- For Templating Twig template engine. +-- For Debugging Kint debugger. +-- For SMTP mails phpMailer. -To use this library, you need to only include config.php file from 'app' directory. +To use this library, you need to only include Bootstrap.php file from 'app' directory. -example : require_nce "app/config.php"; +example : require_once "app/Bootstrap.php"; \ No newline at end of file diff --git a/app/Bootstrap.php b/app/Bootstrap.php index 2a37561..45c1001 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -48,15 +48,4 @@ else{ exit("Autoload file does not exists. Please try to regenerate autoload file using command `composer dump-autoload`"); } -/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ -function _print_r($var) -{ - if( empty($var) ) - return false; - - print "
"; - print "
";
-	print_r($var);
-	print "
"; - print "
"; -} \ No newline at end of file +/** =========== F I L E L O A D I N G E N D S H E R E =========== **/ \ No newline at end of file diff --git a/app/System/Controller.php b/app/System/Controller.php index 461a85f..836286f 100644 --- a/app/System/Controller.php +++ b/app/System/Controller.php @@ -8,8 +8,7 @@ function __construct(){ /*-------------- Initialize Twig Template Engine -----------------*/ Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem('Views'); - $twig = new Twig_Environment($loader); - $twig = new Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); + $this->twig = new Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); } /*-------------- FUNCTION TO LOAD VIEW -----------------*/ diff --git a/app/System/Functions.php b/app/System/Functions.php index 4353bcd..3d7db64 100644 --- a/app/System/Functions.php +++ b/app/System/Functions.php @@ -1,180 +1,191 @@ * @package : Admin Panel * @version : 1.0 */ -class Functions -{ - static function getUriSegment( $segment = 0 ) - { - $uri = $_SERVER['REQUEST_URI']; - $uri = explode('/', $uri); - return $uri[$segment]; - } - - static function encryptURL($string) - { - $data = base64_encode($string); - $data = str_replace(array('+','/','='),array('-','_',':'),$data); - return $data; - } - - static function decryptURL($string) - { - $data = str_replace(array('-','_',':'),array('+','/','='),$string); - $mod4 = strlen($data) % 4; - if ($mod4) { - $data .= substr('====', $mod4); - } - return base64_decode($data); - } - static function isValidURL($url) +function _print_r($var) +{ + if( empty($var) ) + return false; + + print "
"; + print "
";
+	print_r($var);
+	print "
"; + print "
"; +} + +function getUriSegment( $segment = 0 ) +{ + $uri = $_SERVER['REQUEST_URI']; + $uri = explode('/', $uri); + return $uri[$segment]; +} + +function encryptURL($string) +{ + $data = base64_encode($string); + $data = str_replace(array('+','/','='),array('-','_',':'),$data); + return $data; +} + +function decryptURL($string) +{ + $data = str_replace(array('-','_',':'),array('+','/','='),$string); + $mod4 = strlen($data) % 4; + if ($mod4) { + $data .= substr('====', $mod4); + } + return base64_decode($data); +} + +function isValidURL($url) +{ + $regex = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^"; + if(preg_match($regex, $url)) { - $regex = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^"; - if(preg_match($regex, $url)) + if(strstr($url,'www')) { - if(strstr($url,'www')) - { - $url_str = substr($url,strpos($url, 'www')); - if( in_array(substr_count($url_str, '.'), array(2,3)) ) - return TRUE; - else - return FALSE; - } + $url_str = substr($url,strpos($url, 'www')); + if( in_array(substr_count($url_str, '.'), array(2,3)) ) + return TRUE; else - return TRUE; + return FALSE; } else - return FALSE; + return TRUE; } - /** - * Function to escape value. - **/ + else + return FALSE; +} +/** + * Function to escape value. + **/ + +function escape($var) +{ + if( empty($var) ) + return FALSE; - static function escape($var) - { - if( empty($var) ) - return FALSE; + $var = trim($var); + $var = strip_tags($var); + $var = addslashes($var); + $var = htmlentities($var, ENT_QUOTES); + return $var; +} - $var = trim($var); - $var = strip_tags($var); - $var = addslashes($var); - $var = htmlentities($var, ENT_QUOTES); - return $var; +/** + * Function to get file extension + */ +function getExtension($filename) { + if($filename){ + $info = pathinfo($filename); + return strtolower($info['extension']); } - - /** - * Function to get file extension - */ - static function getExtension($filename) { - if($filename){ - $info = pathinfo($filename); - return strtolower($info['extension']); - } - return FALSE; + return FALSE; +} + +/** +* Function to get file type by given extension. +**/ + function getFileType( $ext ) { + $ext2type = array( + 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), + 'video' => array( 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), + 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'rtf', 'wp', 'wpd' ), + 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsb', 'xlsm' ), + 'interactive' => array( 'key', 'ppt', 'pptx', 'pptm', 'odp', 'swf' ), + 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), + 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip' ), + 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), + ); + foreach ( $ext2type as $type => $exts ) + if ( in_array( $ext, $exts ) ) + return $type; } - /** - * Function to get file type by given extension. - **/ - static function getFileType( $ext ) { - $ext2type = array( - 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), - 'video' => array( 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), - 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'rtf', 'wp', 'wpd' ), - 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsb', 'xlsm' ), - 'interactive' => array( 'key', 'ppt', 'pptx', 'pptm', 'odp', 'swf' ), - 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), - 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip' ), - 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), - ); - foreach ( $ext2type as $type => $exts ) - if ( in_array( $ext, $exts ) ) - return $type; - } - - /** - * Function to generates a random password drawn from the defined set of characters. - **/ - static function generateKey( $length = 12, $special_chars = true, $extra_special_chars = false ) { - $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - if ( $special_chars ) - $chars .= '!@#$%^&*()'; - if ( $extra_special_chars ) - $chars .= '-_ []{}<>~`+=,.;:/?|'; - - $password = ''; - for ( $i = 0; $i < $length; $i++ ) { - $password .= substr($chars, self::_rand(0, strlen($chars) - 1), 1); - } - return $password; +/** + * Function to generates a random password drawn from the defined set of characters. + **/ +function generateKey( $length = 12, $special_chars = true, $extra_special_chars = false ) { + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + if ( $special_chars ) + $chars .= '!@#$%^&*()'; + if ( $extra_special_chars ) + $chars .= '-_ []{}<>~`+=,.;:/?|'; + + $password = ''; + for ( $i = 0; $i < $length; $i++ ) { + $password .= substr($chars, self::_rand(0, strlen($chars) - 1), 1); } + return $password; +} - /* - * Function to generate a random number. - */ - static function _rand( $min = 111111, $max = 999999 ) { - $rnd_value=''; +/* + * Function to generate a random number. + */ +function _rand( $min = 111111, $max = 999999 ) { + $rnd_value=''; - if ( strlen($rnd_value) < 8 ) { - $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) ); - $rnd_value .= sha1($rnd_value); - $seed = md5($rnd_value); - } - // Take the first 8 digits for our value - $value = substr($rnd_value, 0, 8); - $rnd_value = substr($rnd_value, 8); - $value = abs(hexdec($value)); + if ( strlen($rnd_value) < 8 ) { + $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) ); + $rnd_value .= sha1($rnd_value); + $seed = md5($rnd_value); + } + // Take the first 8 digits for our value + $value = substr($rnd_value, 0, 8); + $rnd_value = substr($rnd_value, 8); + $value = abs(hexdec($value)); - if ( $max != 0 ) - $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); + if ( $max != 0 ) + $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); - return abs(intval($value)); - } + return abs(intval($value)); +} - /** - * Function to get values from ($_POST or $_GET methods) otherwise set to empty. - */ - static function getVars($vars=array()){ - for($i = 0; $i < count($vars); $i++){ - $var = $vars[$i]; - global $$var; - if( !isset($$var) ){ - if(empty($_REQUEST[$var])) - $$var = ""; - else - $$var = self::escape($_REQUEST[$var]); - } +/** + * Function to get values from ($_POST or $_GET methods) otherwise set to empty. + */ +function getVars($vars=array()){ + for($i = 0; $i < count($vars); $i++){ + $var = $vars[$i]; + global $$var; + if( !isset($$var) ){ + if(empty($_REQUEST[$var])) + $$var = ""; + else + $$var = self::escape($_REQUEST[$var]); } } +} - static function showQuery($query, $params) - { - $keys = array(); - $values = array(); - - # build a regular expression for each parameter +function showQuery($query, $params) +{ + $keys = array(); + $values = array(); + + # build a regular expression for each parameter - foreach ($params as $key=>$value) - { - if (is_string($key)){ - $keys[] = '/:'.$key.'/'; - } - else{ - $keys[] = '/[?]/'; - } - if(is_numeric($value)){ - $values[] = intval($value); - } - else{ - $values[] = '"'.$value .'"'; - } + foreach ($params as $key=>$value) + { + if (is_string($key)){ + $keys[] = '/:'.$key.'/'; + } + else{ + $keys[] = '/[?]/'; + } + if(is_numeric($value)){ + $values[] = intval($value); + } + else{ + $values[] = '"'.$value .'"'; } - $query = preg_replace($keys, $values, $query, 1, $count); - //echo "DSGFDGFHGJFDGSDDFG"; - print $query; } + $query = preg_replace($keys, $values, $query, 1, $count); + //echo "DSGFDGFHGJFDGSDDFG"; + print $query; } \ No newline at end of file diff --git a/composer.json b/composer.json index c6d2b28..152da75 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,10 @@ "require": { "php": ">=5.4" }, - "autoload": { + "autoload": { + "files": [ + "app/System/Functions.php" + ], "psr-4": { "App\\": "app/" } From 2a6b928b96b8e756a4be481a1d556ea80ef7d780 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 10:36:21 +0530 Subject: [PATCH 31/41] Update README.md --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dd1d942..0f2603e 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # PDO-Library Simple PHP Mini Framework with PDO and PHP 5.6.0. + -- To load files automatically PSR 4 autoloading is used. + -- For Templating Twig template engine. + -- For Debugging Kint debugger. + -- For SMTP mails phpMailer. To use this library, you need to only include Bootstrap.php file from 'app' directory. - -<<<<<<< HEAD -example : require_once "app/Bootstrap.php"; -======= -example : require_once "app/Bootstrap.php"; ->>>>>>> c6474c0fbc486aac3cd53d871952299d2f713d78 From c0f074de79faedaa563210b384b68b7d21ab611f Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 15:56:47 +0530 Subject: [PATCH 32/41] changed --- app/Bootstrap.php | 12 ++-- app/Controllers/Default_Controller.php | 20 +++++++ app/Controllers/Login.php | 77 ++++++++++++++++++++++++++ app/System/Controller.php | 10 +++- app/System/Router.php | 43 ++++++++++++++ 5 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 app/Controllers/Default_Controller.php create mode 100644 app/Controllers/Login.php create mode 100644 app/System/Router.php diff --git a/app/Bootstrap.php b/app/Bootstrap.php index 59c4e32..591c91c 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -13,8 +13,6 @@ const PASSWORD = ''; const DS = DIRECTORY_SEPARATOR; -const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; -const ENVIRONMENT = 'development'; // OR production => live if( ENVIRONMENT == 'development' ){ error_reporting(-1); @@ -26,18 +24,19 @@ $basepath = realpath( dirname( dirname(__FILE__) ) ); $httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; -$baseurl = $httpProt.$_SERVER['HTTP_HOST'].'/'.substr( $_SERVER['REQUEST_URI'], strpos( $_SERVER['REQUEST_URI'], '/')+1, strrpos($_SERVER['REQUEST_URI'], '/') ); +$baseurl = $httpProt.$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')). '/'; $basepath = $basepath.DS; $curPage = basename($_SERVER['SCRIPT_NAME'], '.php'); define( 'BASEPATH', $basepath ); +define( 'APP_PATH', BASEPATH . 'app' .DS ); define( 'BASEURL', $baseurl ); define( 'CURRENT_PAGE', $curPage ); -const DIR_CONTROLLER = BASEPATH . 'Controllers' . DS; -const DIR_MODEL = BASEPATH . 'Models' . DS; -const DIR_VIEW = BASEPATH . 'Views' . DS; +const DIR_CONTROLLER = APP_PATH . 'Controllers' . DS; +const DIR_MODEL = APP_PATH . 'Models' . DS; +const DIR_VIEW = APP_PATH . 'Views' . DS; /** ========== C O N S T A N T E N D S H E R E ============ **/ /** @@ -46,6 +45,7 @@ * */ if( file_exists(BASEPATH . 'vendor'.DS.'autoload.php') ){ + //require BASEPATH ."vendor/autoload.php"; require BASEPATH . 'vendor'.DS.'autoload.php'; } else{ diff --git a/app/Controllers/Default_Controller.php b/app/Controllers/Default_Controller.php new file mode 100644 index 0000000..89cd5c0 --- /dev/null +++ b/app/Controllers/Default_Controller.php @@ -0,0 +1,20 @@ +loadView("index.tpl", ['title'=>'Admin']); + } + else{ + $this->loadView("login.tpl", ['title'=>'Login']); + } + } +} \ No newline at end of file diff --git a/app/Controllers/Login.php b/app/Controllers/Login.php new file mode 100644 index 0000000..4a10e0b --- /dev/null +++ b/app/Controllers/Login.php @@ -0,0 +1,77 @@ +username = $_POST['username']; + $user->password = md5($_POST['password']); + $res = $user->login(); + if( (int)$res->id > 0){ + if(isset($_POST['keep_sign'])) + { + setcookie('username', $_POST['username'], time() + (30*86400), '/'); + setcookie('password', $_POST['password'], time() + (30*86400), '/'); + setcookie('keep_sign', TRUE, time() + (30*86400), '/'); + } + else{ + setcookie('username', '', time() - 3600); + setcookie('password', '', time() - 3600); + setcookie('keep_sign', '', time() - 3600); + } + header("Location:".BASEURL); + } + else{ + $error = TRUE; + } + } + } + + if(isset($_COOKIE['username'])){ + $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : ""; + }else if(isset($_POST['username'])){ + $username = isset($_POST['username']) ? $_POST['username'] : ""; + } + if(isset($_COOKIE['password'])){ + $password = isset($_COOKIE['password']) ? $_COOKIE['password'] : ""; + }else if(isset($_POST['password'])){ + $password = isset($_POST['password']) ? $_POST['password'] : ""; + } + if(isset($_COOKIE['keep_sign'])){ + $keep = isset($_COOKIE['keep_sign']) ? TRUE : FALSE; + }else if(isset($_POST['keep_sign'])){ + $keep = isset($_POST['keep_sign']) ? TRUE : FALSE;; + } + } + + /*-------------Login Function Ends ------------*/ + + +} \ No newline at end of file diff --git a/app/System/Controller.php b/app/System/Controller.php index 836286f..d748cc3 100644 --- a/app/System/Controller.php +++ b/app/System/Controller.php @@ -1,14 +1,16 @@ twig = new Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); + $loader = new \Twig_Loader_Filesystem(DIR_VIEW); + $this->twig = new \Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); + } /*-------------- FUNCTION TO LOAD VIEW -----------------*/ @@ -16,6 +18,8 @@ public function loadView($template, $data=array()){ if( ! is_array($data) ){ return; } + $data['BASEPATH'] = BASEPATH; + $data['BASEURL'] = BASEURL; $template = $this->twig->loadTemplate($template); $template->display($data); diff --git a/app/System/Router.php b/app/System/Router.php new file mode 100644 index 0000000..1212948 --- /dev/null +++ b/app/System/Router.php @@ -0,0 +1,43 @@ +route = isset($_GET['_route']) ? $_GET['_route'] : ''; + + self::parseRoute(); + self::_redirect(); + } + + public function parseRoute(){ + $urlArray = array(); + $urlArray = explode("/", $this->route); + + $this->controller = $urlArray[0] ? '\\App\Controllers\\' . ucfirst($urlArray[0]) : '\\App\Controllers\\'.DEFAULT_CONTROLLER; + array_shift($urlArray); + $this->action = $urlArray[0] ? $urlArray[0] : 'index'; + array_shift($urlArray); + $this->params = $urlArray; + } + + public function _redirect(){ + if( class_exists($this->controller) ){ + $controller = new $this->controller(); + $action = $this->action; + $controller->$action($this->params); + } + else{ + header("Location:" . BASEURL . ERROR_DOCUMENT); + } + } + +} \ No newline at end of file From dafa1a857bc45da5a5b9db47fb92eabbee250225 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 15:58:28 +0530 Subject: [PATCH 33/41] changed --- app/Controllers/Login.php | 77 --------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 app/Controllers/Login.php diff --git a/app/Controllers/Login.php b/app/Controllers/Login.php deleted file mode 100644 index 4a10e0b..0000000 --- a/app/Controllers/Login.php +++ /dev/null @@ -1,77 +0,0 @@ -username = $_POST['username']; - $user->password = md5($_POST['password']); - $res = $user->login(); - if( (int)$res->id > 0){ - if(isset($_POST['keep_sign'])) - { - setcookie('username', $_POST['username'], time() + (30*86400), '/'); - setcookie('password', $_POST['password'], time() + (30*86400), '/'); - setcookie('keep_sign', TRUE, time() + (30*86400), '/'); - } - else{ - setcookie('username', '', time() - 3600); - setcookie('password', '', time() - 3600); - setcookie('keep_sign', '', time() - 3600); - } - header("Location:".BASEURL); - } - else{ - $error = TRUE; - } - } - } - - if(isset($_COOKIE['username'])){ - $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : ""; - }else if(isset($_POST['username'])){ - $username = isset($_POST['username']) ? $_POST['username'] : ""; - } - if(isset($_COOKIE['password'])){ - $password = isset($_COOKIE['password']) ? $_COOKIE['password'] : ""; - }else if(isset($_POST['password'])){ - $password = isset($_POST['password']) ? $_POST['password'] : ""; - } - if(isset($_COOKIE['keep_sign'])){ - $keep = isset($_COOKIE['keep_sign']) ? TRUE : FALSE; - }else if(isset($_POST['keep_sign'])){ - $keep = isset($_POST['keep_sign']) ? TRUE : FALSE;; - } - } - - /*-------------Login Function Ends ------------*/ - - -} \ No newline at end of file From de701191df425c0a2875dbf2fbcce800cf630168 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 16:05:44 +0530 Subject: [PATCH 34/41] index added --- .gitignore | 1 - index.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 index.php diff --git a/.gitignore b/.gitignore index af6618d..300b4af 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ /templates /cache -index.php index-old.php composer.lock test.php \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..14230c1 --- /dev/null +++ b/index.php @@ -0,0 +1,29 @@ + live + +require_once "app/Bootstrap.php"; + +$RT = new \App\System\Router(); +$RT->parseRoute(); +$RT->_redirect(); \ No newline at end of file From 34dd2dbfa4641dbc801af425fff5401f43ed39fe Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 16:08:20 +0530 Subject: [PATCH 35/41] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f2603e..f337525 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# PDO-Library +# PHP-PDO-Mini Framework Simple PHP Mini Framework with PDO and PHP 5.6.0. -- To load files automatically PSR 4 autoloading is used. From 992d5d07f1202ab5f62b4280d80ede1e62a1190b Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 17:08:56 +0530 Subject: [PATCH 36/41] changed core --- app/Bootstrap.php | 5 ----- app/System/Controller.php | 10 ++++------ app/System/Upload.php | 34 +++++++++++++++++++++++++++------- composer.json | 8 ++++++++ index.php | 12 ++++++++++-- 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/app/Bootstrap.php b/app/Bootstrap.php index 591c91c..3dfd320 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -7,11 +7,6 @@ ob_start(); session_start(); -const HOSTNAME = 'localhost'; -const DBNAME = 'admin_panel'; -const USERNAME = 'root'; -const PASSWORD = ''; - const DS = DIRECTORY_SEPARATOR; if( ENVIRONMENT == 'development' ){ diff --git a/app/System/Controller.php b/app/System/Controller.php index d748cc3..1e34213 100644 --- a/app/System/Controller.php +++ b/app/System/Controller.php @@ -5,8 +5,9 @@ abstract class Controller { public $twig; function __construct(){ + /*-------------- Initialize Twig Template Engine -----------------*/ - $mail = new \PHPMailer(); + Twig_Autoloader::register(); $loader = new \Twig_Loader_Filesystem(DIR_VIEW); $this->twig = new \Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); @@ -27,11 +28,8 @@ public function loadView($template, $data=array()){ /*-------------- FUNCTION TO LOAD MODEL -----------------*/ public function loadModel($model){ - if( ! is_array($data) ){ - return; + if( file_exists(DIR_MODEL . $model . '.php') ){ + require_once DIR_MODEL . $model . '.php'; } - - $template = $this->twig->loadTemplate($template); - $template->display($data); } } \ No newline at end of file diff --git a/app/System/Upload.php b/app/System/Upload.php index dcefdfe..89b7d8a 100644 --- a/app/System/Upload.php +++ b/app/System/Upload.php @@ -1,6 +1,6 @@ file = $file; $this->config = $config; @@ -37,7 +38,7 @@ public function __construct($file, $config="") } $type = explode('/', $this->info['mime']); $this->fileType = strtolower($type[0]); - if( $this->fileType == "image" ) + if( $this->fileType == "image" || $this->fileType == 'video' ) { $info = getimagesize($file['tmp_name']); $this->info['width'] = $info[0]; @@ -64,9 +65,27 @@ public function __construct($file, $config="") public function doUpload() { $ext = pathinfo($this->file['name'], PATHINFO_EXTENSION); + if( $this->info['bits'] > $this->config['max_size'] ){ + return "File size exceeded"; + } + if( in_array( $this->fileType, ['image', 'audio', 'video'] ) ){ - $fileName = date("Ymd_His").Functions::_rand().".".$ext; + $fileName = date("Ymd_His")._rand().".".$ext; $targetFile = $this->dir.$fileName; + + if( is_array($this->config) ) + { + if( $this->fileType == 'image' || $this->fileType == 'video' ) + { + if( $this->info['width'] > $this->config['max_width'] ){ + return "File width exceeded"; + } + if( $this->info['height'] > $this->config['max_height'] ){ + return "File height exceeded"; + } + } + } + if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ if( $this->fileType == 'image' ){ $image = new Image( $targetFile ); @@ -79,11 +98,12 @@ public function doUpload() $image->resizeToWidth(150); $image->save($this->thumb2.$fileName); } + return $fileName; } - return $fileName; + } else{ - $fileName = date("Ymd_His").Functions::_rand().".".$ext; + $fileName = date("Ymd_His")._rand().".".$ext; $targetFile = $this->dir.$fileName; if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ return $fileName; diff --git a/composer.json b/composer.json index 49fb928..a856793 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,14 @@ "description": "The PDO 5.6 Framework.", "keywords": ["PHP Library", "PHP Library"], "type": "project", + "authors": [ + { + "name": "Akhtar Husain", + "email": "akhtar4660@gmail.com", + "homepage": "https://akhtar-husain.branded.me", + "role": "Lead Developer" + } + ], "require": { "php": ">=5.4" }, diff --git a/index.php b/index.php index 14230c1..0babc0d 100644 --- a/index.php +++ b/index.php @@ -2,7 +2,7 @@ /*---------- SET YOUR DEFAULT CONTROLLER WHEN NONE IS FOUND. * PLEASE MAKE IT AVAILABE, OTHERWISE WILL REDIRECTED TO 404 PAGE. - ---------------*/ + *---------------*/ const DEFAULT_CONTROLLER = "Default_Controller"; @@ -13,7 +13,7 @@ /*---------- SET ENCRYPTION KEY THAT WILL BE USED TO ENCRYPT CONFIDENTIAL DATA * ONCE STARTED THE SYSTEM PLEASE DON'T CHANGE IT. - ---------------*/ + *---------------*/ const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; @@ -22,6 +22,14 @@ const ENVIRONMENT = 'development'; // OR production => live +/*---------- SET DB DETAILS ---------------*/ + +const HOSTNAME = 'localhost'; +const DBNAME = 'admin_panel'; +const USERNAME = 'root'; +const PASSWORD = ''; + + require_once "app/Bootstrap.php"; $RT = new \App\System\Router(); From 4d64f335e214453b1895f8981790080052ca883d Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Fri, 6 Nov 2015 17:32:13 +0530 Subject: [PATCH 37/41] index changed --- app/System/Router.php | 2 -- index.php | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/System/Router.php b/app/System/Router.php index 1212948..6724e41 100644 --- a/app/System/Router.php +++ b/app/System/Router.php @@ -1,8 +1,6 @@ USE TWIG TEMPLATE ENGINE FOR VIEW FILES + * FALSE => USE .php EXTENSION FOR VIEW FILES + *---------------*/ + +const USE_TEMPLATE = TRUE; + + + /*---------- SET ENCRYPTION KEY THAT WILL BE USED TO ENCRYPT CONFIDENTIAL DATA * ONCE STARTED THE SYSTEM PLEASE DON'T CHANGE IT. *---------------*/ @@ -18,9 +30,15 @@ const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; -/*---------- SETTINGS WILL BE APPPLIED BASED ON THE ENVIRONMENT ---------------*/ -const ENVIRONMENT = 'development'; // OR production => live +/*---------- SETTINGS WILL BE APPPLIED BASED ON THE ENVIRONMENT + * ENVIRONMENT => development + * OR ENVIRONMENT => production + * ---------------*/ + +const ENVIRONMENT = ''; // OR production => live + + /*---------- SET DB DETAILS ---------------*/ From 72a86afd03f16192875a16009dde918e751b4fb9 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 7 Jan 2016 21:08:58 +0530 Subject: [PATCH 38/41] Update index.php --- index.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index c191888..d1ada6e 100644 --- a/index.php +++ b/index.php @@ -36,15 +36,15 @@ * OR ENVIRONMENT => production * ---------------*/ -const ENVIRONMENT = ''; // OR production => live +const ENVIRONMENT = 'development'; // OR production => live /*---------- SET DB DETAILS ---------------*/ -const HOSTNAME = 'localhost'; -const DBNAME = 'admin_panel'; -const USERNAME = 'root'; +const HOSTNAME = 'DBHOST'; +const DBNAME = 'DBNAME'; +const USERNAME = 'ROOT'; const PASSWORD = ''; @@ -52,4 +52,4 @@ $RT = new \App\System\Router(); $RT->parseRoute(); -$RT->_redirect(); \ No newline at end of file +$RT->_redirect(); From 1220e942d56445ea8eed41c04cdc438b828f97e7 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 7 Jan 2016 21:09:43 +0530 Subject: [PATCH 39/41] Update index.php --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index d1ada6e..1514ee1 100644 --- a/index.php +++ b/index.php @@ -44,8 +44,8 @@ const HOSTNAME = 'DBHOST'; const DBNAME = 'DBNAME'; -const USERNAME = 'ROOT'; -const PASSWORD = ''; +const USERNAME = 'DBUSER'; +const PASSWORD = 'DBPASSWORD'; require_once "app/Bootstrap.php"; From 9e202f042eca6c8616013aae10f20d8b2a21db93 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Thu, 7 Jan 2016 21:13:40 +0530 Subject: [PATCH 40/41] Update .htaccess --- .htaccess | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/.htaccess b/.htaccess index 83159f5..8b13789 100644 --- a/.htaccess +++ b/.htaccess @@ -1,16 +1 @@ -RewriteEngine On -RewriteBase /admin-panel/ -ErrorDocument 404 http://localhost/admin-panel/?page=error -# - # - # Options -MultiViews - # - # RewriteEngine On - # RewriteBase /admin-panel/ - # Redirect Trailing Slashes... - # RewriteRule ^(.*)/$ /$1 [L,R=301] - # Handle Front Controller... - # RewriteCond %{REQUEST_FILENAME} !-d - # RewriteCond %{REQUEST_FILENAME} !-f - # RewriteRule ^(.*)$ $1.php [L] -# \ No newline at end of file + From 7dc9d0fef4eff7c99b371784412255989c081f87 Mon Sep 17 00:00:00 2001 From: Akhtar Husain Date: Mon, 11 Jan 2016 14:39:16 +0530 Subject: [PATCH 41/41] Update .htaccess --- .htaccess | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.htaccess b/.htaccess index 8b13789..4f8c1f4 100644 --- a/.htaccess +++ b/.htaccess @@ -1 +1,6 @@ +RewriteEngine On +RewriteBase /admin-panel/ +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)$ index.php?_route=$1 [L]