diff options
author | Malf Furious <m@lfurio.us> | 2018-10-27 18:28:22 -0400 |
---|---|---|
committer | Malf Furious <m@lfurio.us> | 2018-10-27 18:28:22 -0400 |
commit | 0421aa1b60f4fe6bf140888159c58059c1013588 (patch) | |
tree | c3285276f6c53b6789e2f6dc82cb3b0fd17b38a4 /app/class/settings.class.php | |
parent | 495157341d60522084dcc9f6219877b6ba497312 (diff) | |
parent | 6512655aee73d3d295daa4de0e4ef25c08cfec9e (diff) | |
download | scrott-0421aa1b60f4fe6bf140888159c58059c1013588.tar.gz scrott-0421aa1b60f4fe6bf140888159c58059c1013588.zip |
Merge branch 'rel/v0.1'v0.1
Diffstat (limited to 'app/class/settings.class.php')
-rw-r--r-- | app/class/settings.class.php | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/app/class/settings.class.php b/app/class/settings.class.php new file mode 100644 index 0000000..d936a62 --- /dev/null +++ b/app/class/settings.class.php @@ -0,0 +1,160 @@ +<?php + +/* + * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * For more information, please refer to UNLICENSE + */ + +require_once "class/database.class.php"; + +/* + * Interface for accessing and updating Scrott administrative settings + */ +abstract class settings +{ + /* + * Get the value stored for the given key, or NULL if key is not + * set. + */ + private static function getValue(string $key) : ?string + { + $key = database::esc($key); + + $query = "SELECT value FROM settings WHERE name = '" . $key . "'"; + $res = database::query($query); + + if (count($res) == 0) + return NULL; + + return $res[0]['value']; + } + + /* + * Store the given (key, value) pair, overwriting key in the + * database if already present. + */ + private static function setValue(string $key, string $value) : void + { + $key = database::esc($key); + $value = database::esc($value); + + if (self::getValue($key) === NULL) + $query = "INSERT INTO settings (name, value) VALUES ('" . $key . "', '" . $value . "')"; + else + $query = "UPDATE settings SET value = '" . $value . "' WHERE name = '" . $key . "'"; + + database::query($query); + } + + /* + * Get the value for an option and optionally update it. Returns + * the stored value for this option, or the given default value + * if not found. If a value update takes place, the new value + * is returned. + */ + private static function option(string $key, string $deflt, ?string $value) : string + { + if ($value !== NULL) + self::setValue($key, $value); + + if (($res = self::getValue($key)) !== NULL) + return $res; + + return $deflt; + } + + /* + * GETTERS AND SETTERS FOR GLOBAL OPTIONS ... + */ + + /* + * Do we force connections to use HTTPS? + */ + public static function sslOnly(?bool $value = NULL) : bool + { + return self::option("sslOnly", false, $value); + } + + /* + * Do we allow anybody to signup their own account? + */ + public static function allowPublicSignup(?bool $value = NULL) : bool + { + return self::option("allowPublicSignup", false, $value); + } + + /* + * SMTP email address + */ + public static function smtpEmailAddress(?string $value = NULL) : string + { + return self::option("smtpEmailAddress", "", $value); + } + + /* + * SMTP FROM name + */ + public static function smtpFrom(?string $value = NULL) : string + { + return self::option("smtpFrom", "", $value); + } + + /* + * SMTP server address + */ + public static function smtpServer(?string $value = NULL) : string + { + return self::option("smtpServer", "", $value); + } + + /* + * SMTP port number + */ + public static function smtpPort(?int $value = NULL) : int + { + return self::option("smtpPort", 25, $value); + } + + /* + * SMTP security + * Should be '', 'ssl', or 'tls'. See the 'SMTPSecure' property + * from PHP Mailer module. + */ + public static function smtpSecurity(?string $value = NULL) : string + { + return self::option("smtpSecurity", "", $value); + } + + /* + * SMTP username + */ + public static function smtpUname(?string $value = NULL) : string + { + return self::option("smtpUname", "", $value); + } + + /* + * SMTP password + */ + public static function smtpPasswd(?string $value = NULL) : string + { + return self::option("smtpPasswd", "", $value); + } + + /* + * SMTP result of last attempted send - 'true', 'false', 'NULL' + */ + public static function smtpResult(?string $value = NULL) : string + { + return self::option("smtpResult", "NULL", $value); + } +} + +?> |