79 lines
1.8 KiB
PHP
79 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* @Author: printempw
|
|
* @Date: 2016-01-16 23:01:33
|
|
* @Last Modified by: prpr
|
|
* @Last Modified time: 2016-02-03 10:41:31
|
|
*/
|
|
|
|
class utils
|
|
{
|
|
/**
|
|
* Custom error handler
|
|
*
|
|
* @param int $errno
|
|
* @param string $msg, message to show
|
|
* @return void
|
|
*/
|
|
public static function raise($errno = -1, $msg = "Error occured.") {
|
|
$exception['errno'] = $errno;
|
|
$exception['msg'] = $msg;
|
|
die(json_encode($exception));
|
|
}
|
|
|
|
/**
|
|
* Rename uploaded file
|
|
*
|
|
* @param array $file, files uploaded via HTTP POST
|
|
* @return string $hash, sha256 hash of file
|
|
*/
|
|
public static function upload($file) {
|
|
move_uploaded_file($file["tmp_name"], "./textures/tmp.png");
|
|
$hash = hash_file('sha256', "./textures/tmp.png");
|
|
rename("./textures/tmp.png", "./textures/".$hash);
|
|
return $hash;
|
|
}
|
|
|
|
/**
|
|
* Remove a file
|
|
*
|
|
* @param $filename
|
|
* @return $bool
|
|
*/
|
|
public static function remove($filename) {
|
|
if(file_exists($filename)) {
|
|
if (!unlink($filename)) {
|
|
self::raise(-1, "Uncaught error when deleting $filename");
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Simple SQL injection protection
|
|
*
|
|
* @param string $string
|
|
* @return string
|
|
*/
|
|
public static function convertString($string) {
|
|
return stripslashes(trim($string));
|
|
}
|
|
|
|
/**
|
|
* Get the value of key in an array if index exist
|
|
*
|
|
* @param string $key
|
|
* @param array $array
|
|
* @return object
|
|
*/
|
|
public static function getValue($key, $array) {
|
|
if (array_key_exists($key, $array)) {
|
|
return $array[$key];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
?>
|