Overview

Namespaces

  • Core
    • BaseClasses
    • Cookie
    • Crontab
    • DB
    • Input
    • Mailer
    • Middleware
      • Auth
      • CSRF
    • Route
    • Session
    • System
  • None

Classes

  • Bridge
  • Core\BaseClasses\BaseAuth
  • Core\BaseClasses\BaseController
  • Core\BaseClasses\BaseCSRF
  • Core\Cookie\Cookie
  • Core\Crontab\Crontab
  • Core\DB\DB
  • Core\Input\Input
  • Core\Mailer\Mailer
  • Core\Middleware\Auth\Auth
  • Core\Middleware\CSRF\CSRF
  • Core\Route\Route
  • Core\Session\Session
  • Core\System\System

Functions

  • back
  • ControllerAutoload
  • dbd
  • ErrorHandler
  • ExceptionHandler
  • json
  • loadConfig
  • MiddlewareAutoload
  • redirect
  • SPAutoload
  • view
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * @Author: Shakti Phartiyal
  5:  * Date: 11/24/16
  6:  * Time: 11:04 AM
  7:  */
  8: namespace Core\System;
  9: 
 10: use Core\BaseClasses\BaseController;
 11: 
 12: class System
 13: {
 14:     /**
 15:      * System constructor.
 16:      */
 17:     public function __construct()
 18:     {
 19: 
 20:     }
 21: 
 22:     /**
 23:      * Debug and die function prints debug data. If second param is true it terminates the script.
 24:      * @param $inData
 25:      * @param bool $die (terminates the script if true, defaults to false)
 26:      */
 27:     public static function DBD($inData, $die=false) //DEBUG AND DIE
 28:     {
 29:         if(is_array($inData))
 30:         {
 31:             echo "<pre><br/>\n";
 32:             print_r($inData);
 33:             echo "</pre><br/>\n";
 34:         }
 35:         else
 36:         {
 37:             if(gettype($inData) == 'object')
 38:             {
 39:                 echo "<br/>\n<pre>";
 40:                 var_dump($inData);
 41:                 echo "<br/>\n</pre>";
 42:             }
 43:             else
 44:             {
 45:                 echo "<br/>\n".$inData."<br/>\n";
 46:             }
 47:         }
 48:         if($die==1 || $die === true)
 49:         {
 50:             die;
 51:         }
 52:     }
 53: 
 54:     /**
 55:      * Make log Files
 56:      * @param $logdir
 57:      * @param $filename
 58:      * @param $logstr
 59:      */
 60:     public static function MakeLog($logdir, $filename, $logstr)
 61:     {
 62:         $logstr .= "\n";
 63:         if(is_dir($logdir.date('Y')))
 64:         {
 65:             if(is_dir($logdir.date('Y').'/'.date('m')))
 66:             {
 67:                 if(!is_dir($logdir.date('Y').'/'.date('m').'/'.date('d')))
 68:                 {
 69:                     mkdir($logdir.date('Y').'/'.date('m').'/'.date('d'),0777);
 70:                 }
 71:             }
 72:             else
 73:             {
 74:                 mkdir($logdir.date('Y').'/'.date('m').'/'.date('d'),0777,true);
 75:             }
 76:         }
 77:         else
 78:         {
 79:             mkdir($logdir.date('Y').'/'.date('m').'/'.date('d'),0777,true);
 80:         }
 81:         $fpd = fopen($logdir.date('Y').'/'.date('m').'/'.date('d').'/'.$filename,"a+");
 82:         fwrite($fpd,$logstr,strlen($logstr));
 83:         fclose($fpd);
 84:     }
 85: 
 86:     /**
 87:      * Method to Encrypt & Decrypt Data Second Parameter wid D required for decryption only.
 88:      * @param $inData
 89:      * @param string $opt
 90:      * @return string
 91:      */
 92:     public static function Crypto($inData, $opt="D")
 93:     {
 94:         $outData="";
 95:         $iv = "r@nD0mKey#osekj%^876ghjkjb5dDdf8";
 96:         if($opt=='E')
 97:         {
 98:             // Encrypt $string
 99:             $outData = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $_ENV['app_key'], $inData, MCRYPT_MODE_CBC, $iv));
100:         }
101:         else
102:         {
103:             // Decrypt $string
104:             $outData = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $_ENV['app_key'], base64_decode($inData), MCRYPT_MODE_CBC, $iv);
105:         }
106:         return $outData;
107:     }
108: 
109: 
110:     /**
111:      * Generate a non reversible secure hash
112:      * @param $plainPassword
113:      * @param string $saltKey (Optional)
114:      * @return string
115:      */
116:     public static function GenerateHash($plainPassword, $saltKey=null)
117:     {
118:         $salt = isset($_ENV['app_key'])?$_ENV['app_key']:"$#@kT!@p!7ram3w0rk";
119:         $salt = $saltKey == null ? $salt : $saltKey;
120:         $hash = hash_hmac('sha256', $plainPassword, $salt, false);
121:         return $hash;
122:     }
123: 
124: 
125:     /**
126:      * Generates a secure non reversible API Key
127:      * @param $digestString
128:      * @return string
129:      */
130:     public static function GenerateAPIKey($digestString)
131:     {
132:         $digestString.='|'.$_ENV['app_key'].'|'.microtime(true);
133:         $hash = hash_hmac('sha1', $digestString, microtime(true), false);
134:         return $hash;
135:     }
136: 
137:     /**
138:      * Generates a small salted CRC32B Secret
139:      * @param $digestString
140:      * @return string
141:      */
142:     public static function GenerateSecret($digestString)
143:     {
144:         $digestString.='|'.$_ENV['app_key'].'|'.microtime(true);
145:         $hash = hash_hmac('crc32b', $digestString, microtime(true), false);
146:         return $hash;
147:     }
148: 
149: 
150:     /**
151:      * Escape Strings
152:      * @param $string
153:      * @return mixed
154:      */
155:     public static function escapeString($string)
156:     {
157:         $search = array("\\",  "\x00", "\n",  "\r",  "'",  '"', "\x1a");
158:         $replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");
159:         return str_replace($search, $replace, $string);
160:     }
161: 
162:     /**
163:      * Filters user input
164:      * @param $inData
165:      * @param bool $keepHTML
166:      * @return array|mixed
167:      */
168:     public static function FilterInput($inData, $keepHTML=false)
169:     {
170:         if(is_array($inData))
171:         {
172:             $outData=array();
173:             foreach($inData as $key => $value)
174:             {
175:                 if(!$keepHTML)
176:                 {
177:                     $value=filter_var(trim($value), FILTER_SANITIZE_STRING);
178:                 }
179:                 else
180:                 {
181:                     $value=trim(htmlentities(strip_tags($value)));
182:                     if (get_magic_quotes_gpc())
183:                     {
184:                         $value = stripslashes($value);
185:                     }
186:                 }
187:                 $outData[$key]=System::escapeString($value);
188:             }
189:         }
190:         else
191:         {
192:             $inData=trim($inData);
193:             if(!$keepHTML)
194:             {
195:                 $inData=filter_var($inData, FILTER_SANITIZE_STRING);
196:             }
197:             else
198:             {
199:                 $inData=trim(htmlentities(strip_tags($inData)));
200:                 if (get_magic_quotes_gpc())
201:                 {
202:                     $inData = stripslashes($inData);
203:                 }
204:             }
205:             $inData = System::escapeString($inData);
206:             $outData=$inData;
207:         }
208:         return $outData;
209:     }
210: 
211:     /**
212:      * Give JSON formatted response suited for APIS
213:      * @param $inData
214:      */
215:     public static function GiveResponse($inData)
216:     {
217:         header('Content-Type: application/json');
218:         $outData=array('errStatus' =>0,
219:             'code'=>200,
220:             'message'=>'success',
221:             'data'=>$inData
222:         );
223:         $outData=json_encode($outData);
224:         echo $outData;
225:         exit();
226:     }
227: 
228:     /**
229:      * Give JSON response suited for APIS
230:      * @param $inData
231:      */
232:     public static function GiveJSON($inData)
233:     {
234:         header('Content-Type: application/json');
235:         $outData=json_encode($inData);
236:         echo $outData;
237:         exit();
238:     }
239: 
240:     /**
241:      * Give JSON formatted error response suited for APIS
242:      * @param $errorCode
243:      * @param $errorMessage
244:      */
245:     public static function GiveError($errorCode, $errorMessage)
246:     {
247:         if($errorCode==400)
248:         {
249:             header("HTTP/1.0 400 Bad Request");
250:         }
251:         else if($errorCode==401)
252:         {
253:             header("HTTP/1.0 401 Unauthorized");
254:         }
255:         else if($errorCode==403)
256:         {
257:             header("HTTP/1.0 403 Forbidden");
258:         }
259:         else if($errorCode==404)
260:         {
261:             header("HTTP/1.0 404 Not Found");
262:         }
263:         else if($errorCode==405)
264:         {
265:             header("HTTP/1.0 405 Method Not Allowed");
266:         }
267:         else if($errorCode==406)
268:         {
269:             header("HTTP/1.0 406 Not Acceptable");
270:         }
271:         else if($errorCode==503)
272:         {
273:             header("HTTP/1.0 503 Service Unavailable");
274:         }
275:         else if($errorCode==500)
276:         {
277:             header("HTTP/1.0 500 Server Error");
278:         }
279:         if(isset($_ENV['error_pages'][$errorCode]) && $_ENV['error_pages'][$errorCode] != "")
280:         {
281:             return BaseController::view($_ENV['error_pages'][$errorCode],[],true);
282:         }
283:         header('Content-Type: application/json');
284:         $outData=array('errStatus' =>1,
285:             'code'=>$errorCode,
286:             'message'=>$errorMessage);
287:         $outData=json_encode($outData);
288:         echo $outData;
289:         exit();
290:     }
291: }
API documentation generated by ApiGen