diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..300b4af --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/vendor +/uploads +/.idea +/templates +/cache + +index-old.php +composer.lock +test.php \ No newline at end of file diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..4f8c1f4 --- /dev/null +++ b/.htaccess @@ -0,0 +1,6 @@ +RewriteEngine On +RewriteBase /admin-panel/ + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)$ index.php?_route=$1 [L] diff --git a/README.md b/README.md index f4022d1..f337525 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ -# PDO-Library -Simple PHP library with PDO and PHP 5.6.0 +# PHP-PDO-Mini Framework +Simple PHP Mini Framework with PDO and PHP 5.6.0. -To use this library, you need to only include config.php file from 'lib' directory. +-- To load files automatically PSR 4 autoloading is used. -example : require_nce "lib/config.php"; +-- 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. diff --git a/app/Bootstrap.php b/app/Bootstrap.php new file mode 100644 index 0000000..3dfd320 --- /dev/null +++ b/app/Bootstrap.php @@ -0,0 +1,49 @@ + + * @package : Admin Panel + * @version : 1.0 + */ +ob_start(); +session_start(); + +const DS = DIRECTORY_SEPARATOR; + +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['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 = 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 ============ **/ + +/** + * + * ========== 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 =========== **/ \ No newline at end of file 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/index.html b/app/Controllers/index.html new file mode 100644 index 0000000..e69de29 diff --git a/lib/tables/AdminUser.class.php b/app/Models/AdminUser.php similarity index 90% rename from lib/tables/AdminUser.class.php rename to app/Models/AdminUser.php index b93b6df..be5648a 100644 --- a/lib/tables/AdminUser.class.php +++ b/app/Models/AdminUser.php @@ -1,4 +1,6 @@ @@ -6,7 +8,7 @@ * @version : 1.0 */ -final public class AdminUser extends Auth +final class AdminUser extends Auth { ///////////////////////////////////////////////// // PROPERTIES, PUBLIC @@ -39,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)) @@ -83,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] ); @@ -98,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/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 diff --git a/app/Models/index.html b/app/Models/index.html new file mode 100644 index 0000000..e69de29 diff --git a/app/System/Auth.php b/app/System/Auth.php new file mode 100644 index 0000000..f3de6c5 --- /dev/null +++ b/app/System/Auth.php @@ -0,0 +1,49 @@ + + * @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( $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' ); + $db->update($this->table,['hash' => $newHash]); + } + + /************ SET SESSION VARIABLES HERE **************/ + //session_start(); + $_SESSION['logged'] = TRUE; + $_SESSION['userid'] = $data->id; + $_SESSION['username'] = $data->username; + $_SESSION['name'] = $data->display_name ? $data->display_name : ""; + /****************** END SESSION SETTINGS **************/ + return $data; + } + } + public static function isLoggedIn(){ + if( isset($_SESSION['logged']) ){ + return $_SESSION['logged']; + } + } + public static function logout(){ + unset( $_SESSEION ); + session_destroy(); + header("Location:".BASEURL); + } +} \ No newline at end of file diff --git a/app/System/Controller.php b/app/System/Controller.php new file mode 100644 index 0000000..1e34213 --- /dev/null +++ b/app/System/Controller.php @@ -0,0 +1,35 @@ +twig = new \Twig_Environment($loader, (ENVIRONMENT == 'production') ? array('cache' => 'cache') : array() ); + + } + + /*-------------- FUNCTION TO LOAD VIEW -----------------*/ + 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); + } + + /*-------------- FUNCTION TO LOAD MODEL -----------------*/ + public function loadModel($model){ + if( file_exists(DIR_MODEL . $model . '.php') ){ + require_once DIR_MODEL . $model . '.php'; + } + } +} \ No newline at end of file diff --git a/lib/DB.class.php b/app/System/DB.php similarity index 66% rename from lib/DB.class.php rename to app/System/DB.php index 73af4fd..aa8712b 100644 --- a/lib/DB.class.php +++ b/app/System/DB.php @@ -1,5 +1,6 @@ * @package : Admin Panel @@ -14,12 +15,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. @@ -27,14 +28,14 @@ function __construct(){ $dsn = "mysql:host=".HOSTNAME.";dbname=".DBNAME; try { - $this->dbh = new PDO($dsn, USERNAME, PASSWORD); + $this->dbh = new \PDO($dsn, USERNAME, PASSWORD); if( ENVIRONMENT == 'development' ){ - $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + $this->dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); } } catch(PDO_Exception $e){ echo $e->getMessage(); - } + } } /* @@ -43,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; @@ -83,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) ) @@ -97,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; @@ -118,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) ) @@ -137,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) ) @@ -191,14 +242,14 @@ public function likeWhere( $whereQuery = array(), $beforeOpr = 'AND', $afterOpr $param = array(); if( count($whereQuery) >= 1 ){ foreach($whereQuery as $key => $val){ - $param[] = $key." LIKE '%?%'"; + $param[] = $key." LIKE ?"; $this->whereVal[] = $val; } } $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 "; } @@ -231,7 +282,7 @@ private function buildQuery( $sql ) $query .= !empty($this->limit) ? " LIMIT ".$this->limit." " : ""; $query .= !empty($this->offset) ? " OFFSET ".$this->offset." " : ""; - showQuery($query,$this->arrValues); + //Functions::showQuery($query,$this->arrValues); return $query; } diff --git a/lib/Encryption.class.php b/app/System/Encryption.php similarity index 96% rename from lib/Encryption.class.php rename to app/System/Encryption.php index 15be961..592debe 100644 --- a/lib/Encryption.class.php +++ b/app/System/Encryption.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/lib/functions.php b/app/System/Helpers.php similarity index 97% rename from lib/functions.php rename to app/System/Helpers.php index 6650502..3d7db64 100644 --- a/lib/functions.php +++ b/app/System/Helpers.php @@ -1,11 +1,24 @@ * @package : Admin Panel * @version : 1.0 */ +function _print_r($var) +{ + if( empty($var) ) + return false; + + print "
"; +} + function getUriSegment( $segment = 0 ) { $uri = $_SERVER['REQUEST_URI']; @@ -115,7 +128,7 @@ function generateKey( $length = 12, $special_chars = true, $extra_special_chars /* * Function to generate a random number. */ -function _rand( $min = 0, $max = 0 ) { +function _rand( $min = 111111, $max = 999999 ) { $rnd_value=''; if ( strlen($rnd_value) < 8 ) { @@ -150,21 +163,6 @@ function getVars($vars=array()){ } } -function _print_r($var) -{ - if( empty($var) ) - return false; - - print ""; -} - function showQuery($query, $params) { $keys = array(); diff --git a/app/System/Image.php b/app/System/Image.php new file mode 100644 index 0000000..f971e57 --- /dev/null +++ b/app/System/Image.php @@ -0,0 +1,96 @@ + + * @package : Admin Panel + * @version : 1.0 + */ + +class Image +{ + var $image; + var $image_type; + + 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); + } + 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/app/System/Mimes.php b/app/System/Mimes.php new file mode 100644 index 0000000..f217244 --- /dev/null +++ b/app/System/Mimes.php @@ -0,0 +1,100 @@ + + * @package : Admin Panel + * @version : 1.0 + */ +$fileTypes = array( 'hqx' => 'application/mac-binhex40', + 'cpt' => 'application/mac-compactpro', + 'csv' => 'text/csv', + 'bin' => 'application/macbinary', + 'dms' => 'application/octet-stream', + 'lha' => 'application/octet-stream', + 'lzh' => 'application/octet-stream', + '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' => 'application/pdf', + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', + 'smi' => 'application/smil', + 'smil' => 'application/smil', + 'mif' => 'application/vnd.mif', + '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', + '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' => 'application/x-tar', + 'xhtml' => 'application/xhtml+xml', + 'xht' => 'application/xhtml+xml', + 'zip' => 'application/zip', + 'mid' => 'audio/midi', + 'midi' => 'audio/midi', + 'mpga' => 'audio/mpeg', + 'mp2' => 'audio/mpeg', + 'mp3' => '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' => 'audio/wav', + 'bmp' => 'image/bmp', + 'gif' => 'image/gif', + 'jpeg' => 'image/jpeg', + 'jpg' => 'image/jpeg', + 'jpe' => 'image/jpeg', + 'png' => 'image/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' => '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', + 'webm' => 'video/webm', + 'doc' => 'application/msword', + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'word' => 'application/msword', + 'xl' => 'application/excel', + 'eml' => 'message/rfc822', + 'json' => 'text/json' + ); \ No newline at end of file diff --git a/lib/Pagination.class.php b/app/System/Pagination.php similarity index 98% rename from lib/Pagination.class.php rename to app/System/Pagination.php index 8d5b4b6..5fe1261 100644 --- a/lib/Pagination.class.php +++ b/app/System/Pagination.php @@ -1,4 +1,5 @@ diff --git a/lib/Password.class.php b/app/System/Password.php similarity index 98% rename from lib/Password.class.php rename to app/System/Password.php index 47a5bd1..4f33669 100644 --- a/lib/Password.class.php +++ b/app/System/Password.php @@ -1,5 +1,5 @@ * @package : Admin Panel diff --git a/app/System/Router.php b/app/System/Router.php new file mode 100644 index 0000000..6724e41 --- /dev/null +++ b/app/System/Router.php @@ -0,0 +1,41 @@ +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 diff --git a/app/System/Upload.php b/app/System/Upload.php new file mode 100644 index 0000000..89b7d8a --- /dev/null +++ b/app/System/Upload.php @@ -0,0 +1,115 @@ +file = $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( + 'bits' => $size, + 'mime' => $mime + ); + + if( !file_exists( BASEPATH.'uploads' )){ + mkdir( BASEPATH."uploads", 0755); + } + $type = explode('/', $this->info['mime']); + $this->fileType = strtolower($type[0]); + if( $this->fileType == "image" || $this->fileType == 'video' ) + { + $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); + } + } + 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( $this->info['bits'] > $this->config['max_size'] ){ + return "File size exceeded"; + } + + if( in_array( $this->fileType, ['image', 'audio', 'video'] ) ){ + $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 ); + + $image->load($targetFile); + $image->resizeToWidth(450); + $image->save($this->thumb1.$fileName); + + $image->load($targetFile); + $image->resizeToWidth(150); + $image->save($this->thumb2.$fileName); + } + return $fileName; + } + + } + else{ + $fileName = date("Ymd_His")._rand().".".$ext; + $targetFile = $this->dir.$fileName; + if( move_uploaded_file($this->file['tmp_name'], $targetFile) ){ + return $fileName; + } + } + } + + +} \ No newline at end of file diff --git a/app/System/Validation.php b/app/System/Validation.php new file mode 100644 index 0000000..316f505 --- /dev/null +++ b/app/System/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/System/index.html b/app/System/index.html new file mode 100644 index 0000000..e69de29 diff --git a/app/Views/index.html b/app/Views/index.html new file mode 100644 index 0000000..e69de29 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 new file mode 100644 index 0000000..a856793 --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "PDO Library", + "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" + }, + "autoload": { + "files": [ + "app/System/Helpers.php" + ], + "psr-4": { + "App\\": "app/" + } + }, + "require-dev":{ + "phpmailer/phpmailer": "5.2.*", + "twig/twig":"~1.0", + "raveren/kint": "0.9" + } +} \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..1514ee1 --- /dev/null +++ b/index.php @@ -0,0 +1,55 @@ + 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. + *---------------*/ + +const KEY = 'qwertyuiopASDFGHJKLzxcvbnm!@#$%^&*87654321'; + + + +/*---------- SETTINGS WILL BE APPPLIED BASED ON THE ENVIRONMENT + * ENVIRONMENT => development + * OR ENVIRONMENT => production + * ---------------*/ + +const ENVIRONMENT = 'development'; // OR production => live + + + +/*---------- SET DB DETAILS ---------------*/ + +const HOSTNAME = 'DBHOST'; +const DBNAME = 'DBNAME'; +const USERNAME = 'DBUSER'; +const PASSWORD = 'DBPASSWORD'; + + +require_once "app/Bootstrap.php"; + +$RT = new \App\System\Router(); +$RT->parseRoute(); +$RT->_redirect(); diff --git a/lib/Auth.class.php b/lib/Auth.class.php deleted file mode 100644 index 9d22bbd..0000000 --- a/lib/Auth.class.php +++ /dev/null @@ -1,39 +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/Image.class.php b/lib/Image.class.php deleted file mode 100644 index 7c1cd1c..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/config.php b/lib/config.php deleted file mode 100644 index 5b2ffa0..0000000 --- a/lib/config.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @package : Admin Panel - * @version : 1.0 - */ - -ob_start(); -session_start(); - -if (version_compare(phpversion(), '5.4.0', '<') == true) { - exit('PHP 5.4+ Required'); -} - -const HOSTNAME = 'localhost'; -const DBNAME = 'admin_panel'; -const USERNAME = 'root'; -const PASSWORD = '467936'; -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); -} - -$basepath = realpath( dirname( dirname(__FILE__) ) ); -$httpProt = isset($_SERVER['https']) ? 'https://' : 'http://'; -$baseurl = $httpProt.$_SERVER['HTTP_HOST'].str_replace(DS, '/', strrchr($basepath, DS)).'/'; - -$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 =========== - * - */ - -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"; - -/** =========== 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/test.php b/test.php deleted file mode 100644 index 45744bb..0000000 --- a/test.php +++ /dev/null @@ -1,36 +0,0 @@ -".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