configure.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. class configure {
  3. # INPUT:
  4. # Param 1: Config file to read. Throws on failure to read.
  5. # Param 2: undef or ARRAY of keys you wanna fetch. undef/invalid assumes we'll be wanting all of them.
  6. # OUTPUT: ARRAY of the requested KEY => VALUE pairs.
  7. public static function get_config_values( $file = null, $desired_configs = null ) {
  8. if( !file_exists( $file ) ) throw new Exception( "$file doesn't exist." );
  9. $config = file_get_contents( $file );
  10. if( empty( $config ) ) throw new Exception( "$file couldn't be opened or is empty" );
  11. $config = json_decode( $config, true );
  12. if( empty( $config ) ) throw new Exception( "$file is not valid JSON" );
  13. if( !is_array( $config) ) throw new Exception( "Decoded $file's JSON string is not an array" );
  14. return $config;
  15. }
  16. # INPUT: ARRAY, preferably the one returned by get_config_values.
  17. # OUTPUT: ARRAY [ result => BOOL, reason => STRING ].
  18. public static function validate_config( $config = array() ) {
  19. #OK, so here we have to start making some 'assumptions' RE mutli-user environments (cPanel, etc.)
  20. $caller_info = posix_getpwuid();
  21. $basedir = $caller_info['dir'] . "/.tCMS";
  22. if( !file_exists( $basedir) || !is_dir( $basedir ) ) throw new Exception( "~/.tCMS doesn't exist." );
  23. $model_file = "$basedir/model.json";
  24. $model = self::get_config_values( $model_file );
  25. foreach ( $model as $key => $val ) {
  26. if( !array_key_exists( $key ) ) {
  27. return [ 'result' => 0, 'reason' => "Config file was missing required key '$key'" ];
  28. }
  29. # TODO check various directories here, make sure we can read/write as appropriate
  30. }
  31. return 1;
  32. }
  33. # INPUT:
  34. # Param 1: Config file to write. Throws if it fails to write.
  35. # Param 1: ARRAY of KEY => VALUE pairs you wanna set. Throws on invalid input.
  36. # OUTPUT: BOOL regarding success/failure.
  37. public static function set_config_values( $file = null, $desired_configs = null ) {
  38. return;
  39. }
  40. }
  41. ?>