[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/Config/ -> Config.php (source)

   1  <?php
   2  
   3  /**
   4   * Define the YOURLS config
   5   */
   6  
   7  namespace YOURLS\Config;
   8  
   9  use YOURLS\Exceptions\ConfigException;
  10  
  11  class Config {
  12  
  13      /**
  14       * @var string
  15       */
  16      protected string $root;
  17  
  18      /**
  19       * @var string
  20       */
  21      protected string $config;
  22  
  23      /**
  24       * Constants that must be defined in config.php
  25       */
  26      protected const MANDATORY_CONSTANTS = [
  27          'YOURLS_DB_USER',
  28          'YOURLS_DB_PASS',
  29          'YOURLS_DB_NAME',
  30          'YOURLS_DB_HOST',
  31          'YOURLS_DB_PREFIX',
  32          'YOURLS_SITE',
  33      ];
  34  
  35      /**
  36       * Publicly known values that are not acceptable for YOURLS_COOKIEKEY
  37       */
  38      protected const INVALID_COOKIEKEYS = [
  39          '',
  40          'modify this text with something random', // default value in config-sample.php
  41          'qQ4KhL_pu|s@Zm7n#%:b^{A[vhm',            // suggested value in the documentation
  42      ];
  43  
  44  
  45  
  46      /**
  47       * @since  1.7.3
  48       * @param string $config Optional user defined config path
  49       */
  50      public function __construct(string $config = '') {
  51          $this->set_root( $this->fix_win32_path(dirname(__DIR__, 2)) );
  52          $this->set_config($config);
  53      }
  54  
  55      /**
  56       * Convert backslashes to slashes
  57       *
  58       * @since  1.7.3
  59       * @param string $path
  60       * @return string  path with \ converted to /
  61       */
  62      public function fix_win32_path(string $path): string {
  63          return str_replace('\\', '/', $path);
  64      }
  65  
  66      /**
  67       * @since  1.7.3
  68       * @param string $config path to config file
  69       * @return void
  70       */
  71      public function set_config(string $config): void {
  72          $this->config = $config;
  73      }
  74  
  75      /**
  76       * @since  1.7.3
  77       * @param string $root path to YOURLS root directory
  78       * @return void
  79       */
  80      public function set_root(string $root): void {
  81          $this->root = $root;
  82      }
  83  
  84      /**
  85       * Find config.php, either user defined or from standard location
  86       *
  87       * @since  1.7.3
  88       * @return string         path to found config file
  89       * @throws ConfigException
  90       */
  91      public function find_config(): string {
  92  
  93          $config = $this->fix_win32_path($this->config);
  94  
  95          if (!empty($config) && is_readable($config)) {
  96              return $config;
  97          }
  98  
  99          if (!empty($config) && !is_readable($config)) {
 100              throw new ConfigException("User defined config not found at '$config'");
 101          }
 102  
 103          // config.php in /user/
 104          if (file_exists($this->root . '/user/config.php')) {
 105              return $this->root . '/user/config.php';
 106          }
 107  
 108          // config.php in /includes/
 109          if (file_exists($this->root . '/includes/config.php')) {
 110              return $this->root . '/includes/config.php';
 111          }
 112  
 113          // config.php not found :(
 114  
 115          throw new ConfigException('Cannot find config.php. Please read the readme.html to learn how to install YOURLS');
 116      }
 117  
 118      /**
 119       * Check that all mandatory constants are defined
 120       *
 121       * @since  1.10.5
 122       * @param string[] $must_haves Constant names to check, defaults to self::MANDATORY_CONSTANTS
 123       * @return void
 124       * @throws ConfigException
 125       */
 126      public function check_mandatory_constants(array $must_haves = self::MANDATORY_CONSTANTS): void {
 127          foreach ($must_haves as $must_have) {
 128              if (!defined($must_have)) {
 129                  throw new ConfigException('Config is incomplete (missing at least '.$must_have.') Check config-sample.php and edit your config accordingly');
 130              }
 131          }
 132      }
 133  
 134      /**
 135       * Check that the cookie key is defined and is not a publicly known value
 136       *
 137       * @since  1.10.5
 138       * @param mixed $key Cookie key value, or null if the constant is undefined
 139       * @return void
 140       * @throws ConfigException
 141       */
 142      public function check_cookie_key(mixed $key): void {
 143          if (!is_string($key) || in_array($key, self::INVALID_COOKIEKEYS, true)) {
 144              throw new ConfigException('YOURLS_COOKIEKEY is undefined or still set to the sample value. Set it to a long random string, see https://yourls.org/cookiedoc');
 145          }
 146      }
 147  
 148      /**
 149       * Define core constants that have not been user defined in config.php
 150       *
 151       * @since  1.7.3
 152       * @return void
 153       * @throws ConfigException
 154       */
 155      public function define_core_constants(): void {
 156          // Check minimal config job has been properly done
 157          $this->check_mandatory_constants();
 158          $this->check_cookie_key(defined('YOURLS_COOKIEKEY') ? YOURLS_COOKIEKEY : null);
 159  
 160          /**
 161           * The following has an awful CRAP index and it would be much shorter reduced to something like
 162           * defining an array of ('YOURLS_SOMETHING' => 'default value') and then a simple loop over the
 163           * array, checking if $current is defined as a constant and otherwise define said constant with
 164           * its default value. I did not write it that way because that would make it difficult for code
 165           * parsers to identify which constants are defined and where. So, here it is, that long list of
 166           * if (!defined) define(). Ho and by the way, such beautiful comment, much right aligned, wow !
 167           */
 168  
 169          // physical path of YOURLS root
 170          if (!defined( 'YOURLS_ABSPATH' ))
 171              define('YOURLS_ABSPATH', $this->root);
 172  
 173          // physical path of includes directory
 174          if (!defined( 'YOURLS_INC' ))
 175              define('YOURLS_INC', YOURLS_ABSPATH.'/includes');
 176  
 177          // physical path of user directory
 178          if (!defined( 'YOURLS_USERDIR' ))
 179              define( 'YOURLS_USERDIR', YOURLS_ABSPATH.'/user' );
 180  
 181          // URL of user directory
 182          if (!defined( 'YOURLS_USERURL' ))
 183              define( 'YOURLS_USERURL', trim(YOURLS_SITE, '/').'/user' );
 184  
 185          // physical path of asset directory
 186          if( !defined( 'YOURLS_ASSETDIR' ) )
 187              define( 'YOURLS_ASSETDIR', YOURLS_ABSPATH.'/assets' );
 188  
 189          // URL of asset directory
 190          if( !defined( 'YOURLS_ASSETURL' ) )
 191              define( 'YOURLS_ASSETURL', trim(YOURLS_SITE, '/').'/assets' );
 192  
 193          // physical path of translations directory
 194          if (!defined( 'YOURLS_LANG_DIR' ))
 195              define( 'YOURLS_LANG_DIR', YOURLS_USERDIR.'/languages' );
 196  
 197          // physical path of plugins directory
 198          if (!defined( 'YOURLS_PLUGINDIR' ))
 199              define( 'YOURLS_PLUGINDIR', YOURLS_USERDIR.'/plugins' );
 200  
 201          // URL of plugins directory
 202          if (!defined( 'YOURLS_PLUGINURL' ))
 203              define( 'YOURLS_PLUGINURL', YOURLS_USERURL.'/plugins' );
 204  
 205          // physical path of themes directory
 206          if( !defined( 'YOURLS_THEMEDIR' ) )
 207              define( 'YOURLS_THEMEDIR', YOURLS_USERDIR.'/themes' );
 208  
 209          // URL of themes directory
 210          if( !defined( 'YOURLS_THEMEURL' ) )
 211              define( 'YOURLS_THEMEURL', YOURLS_USERURL.'/themes' );
 212  
 213          // physical path of pages directory
 214          if (!defined( 'YOURLS_PAGEDIR' ))
 215              define('YOURLS_PAGEDIR', YOURLS_USERDIR.'/pages' );
 216  
 217          // table to store URLs
 218          if (!defined( 'YOURLS_DB_TABLE_URL' ))
 219              define( 'YOURLS_DB_TABLE_URL', YOURLS_DB_PREFIX.'url' );
 220  
 221          // table to store options
 222          if (!defined( 'YOURLS_DB_TABLE_OPTIONS' ))
 223              define( 'YOURLS_DB_TABLE_OPTIONS', YOURLS_DB_PREFIX.'options' );
 224  
 225          // table to store hits, for stats
 226          if (!defined( 'YOURLS_DB_TABLE_LOG' ))
 227              define( 'YOURLS_DB_TABLE_LOG', YOURLS_DB_PREFIX.'log' );
 228  
 229          // if set to true, verbose debug infos
 230          if (!defined( 'YOURLS_DEBUG' )) {
 231              define('YOURLS_DEBUG', false);
 232          }
 233  
 234      }
 235  
 236  }


Generated: Sat Aug 8 05:10:51 2026 Cross-referenced by PHPXref 0.7.1