-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code cleanup, expand README, fix some minor bugs (#20)
* various fixes (read changelog) - Upgraded __get_request to match __write_data better and have less redundant code - Fixed __write_data overwriting keys sometimes - Failed GET requests now reject directly - Added collectionName to typescript - Return TypeErrors more consistently - Fixed various minor bugs
- Loading branch information
Showing
35 changed files
with
2,546 additions
and
2,314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
class FileAccess { | ||
public static function read($filepath, $waitLock = false, $default = null) { | ||
$fileObj = array('filepath' => $filepath, 'content' => ''); | ||
// open file as binary | ||
$file = fopen($filepath, 'rb'); | ||
|
||
// exit if couldn't find file | ||
if ($file === false) { | ||
if ($default == null) | ||
throw new Exception("Could not open file: $filepath"); | ||
|
||
// set default value | ||
$fileObj['content'] = $default; | ||
} | ||
|
||
// if no file, puts default value inside | ||
if ($file === false) { | ||
file_put_contents($fileObj['filepath'], $fileObj['content'], LOCK_EX); | ||
$file = fopen($filepath, 'rb'); | ||
} | ||
|
||
$fileObj['fd'] = $file; | ||
|
||
// if want the lock, we wait for the shared lock | ||
if ($waitLock) { | ||
$lock = flock($file, LOCK_SH); | ||
if (!$lock) { | ||
fclose($file); | ||
throw new Exception('Failed to lock file'); | ||
} | ||
} | ||
|
||
// read file content | ||
$string = ''; | ||
while (!feof($file)) { | ||
$string .= fread($file, 8192); | ||
} | ||
|
||
$fileObj['content'] = $string; | ||
|
||
// if no wait you can close the file | ||
if (!$waitLock) | ||
fclose($file); | ||
|
||
return $fileObj; | ||
} | ||
public static function write($fileObj) { | ||
// lock and close | ||
flock($fileObj['fd'], LOCK_UN); | ||
fclose($fileObj['fd']); | ||
|
||
if (!is_writable($fileObj['filepath'])) { | ||
throw new HTTPException("PHP script can't write to file. Check permission, group and owner.", 400); | ||
} | ||
|
||
$ret = file_put_contents($fileObj['filepath'], $fileObj['content'], LOCK_EX); | ||
return $ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
class HTTPException extends Exception { | ||
// custom message | ||
public function __construct($message, $code = 400, Throwable $previous = null) { | ||
$type_message = gettype($message); | ||
|
||
if ($type_message != 'string') | ||
throw new Exception("Incorrect message type for HTTPException constructor, expected string, got $type_message"); | ||
|
||
$type_code = gettype($code); | ||
if ($type_code != 'integer') | ||
throw new Exception("Incorrect code type for HTTPException constructor, expected string, got $type_code"); | ||
|
||
// assign everything | ||
parent::__construct($message, $code, $previous); | ||
} | ||
|
||
// prettier representation | ||
public function __toString(): string { | ||
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; | ||
} | ||
} |
Oops, something went wrong.