[ 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       * @since  1.7.3
  25       * @param string $config Optional user defined config path
  26       */
  27      public function __construct(string $config = '') {
  28          $this->set_root( $this->fix_win32_path(dirname(__DIR__, 2)) );
  29          $this->set_config($config);
  30      }
  31  
  32      /**
  33       * Convert backslashes to slashes
  34       *
  35       * @since  1.7.3
  36       * @param string $path
  37       * @return string  path with \ converted to /
  38       */
  39      public function fix_win32_path(string $path): string {
  40          return str_replace('\\', '/', $path);
  41      }
  42  
  43      /**
  44       * @since  1.7.3
  45       * @param string $config path to config file
  46       * @return void
  47       */
  48      public function set_config(string $config): void {
  49          $this->config = $config;
  50      }
  51  
  52      /**
  53       * @since  1.7.3
  54       * @param string $root path to YOURLS root directory
  55       * @return void
  56       */
  57      public function set_root(string $root): void {
  58          $this->root = $root;
  59      }
  60  
  61      /**
  62       * Find config.php, either user defined or from standard location
  63       *
  64       * @since  1.7.3
  65       * @return string         path to found config file
  66       * @throws ConfigException
  67       */
  68      public function find_config(): string {
  69  
  70          $config = $this->fix_win32_path($this->config);
  71  
  72          if (!empty($config) && is_readable($config)) {
  73              return $config;
  74          }
  75  
  76          if (!empty($config) && !is_readable($config)) {
  77              throw new ConfigException("User defined config not found at '$config'");
  78          }
  79  
  80          // config.php in /user/
  81          if (file_exists($this->root . '/user/config.php')) {
  82              return $this->root . '/user/config.php';
  83          }
  84  
  85          // config.php in /includes/
  86          if (file_exists($this->root . '/includes/config.php')) {
  87              return $this->root . '/includes/config.php';
  88          }
  89  
  90          // config.php not found :(
  91  
  92          throw new ConfigException('Cannot find config.php. Please read the readme.html to learn how to install YOURLS');
  93      }
  94  
  95      /**
  96       * Define core constants that have not been user defined in config.php
  97       *
  98       * @since  1.7.3
  99       * @return void
 100       * @throws ConfigException
 101       */
 102      public function define_core_constants(): void {
 103          // Check minimal config job has been properly done
 104          $must_haves = array('YOURLS_DB_USER', 'YOURLS_DB_PASS', 'YOURLS_DB_NAME', 'YOURLS_DB_HOST', 'YOURLS_DB_PREFIX', 'YOURLS_SITE');
 105          foreach($must_haves as $must_have) {
 106              if (!defined($must_have)) {
 107                  throw new ConfigException('Config is incomplete (missing at least '.$must_have.') Check config-sample.php and edit your config accordingly');
 108              }
 109          }
 110  
 111          /**
 112           * The following has an awful CRAP index and it would be much shorter reduced to something like
 113           * defining an array of ('YOURLS_SOMETHING' => 'default value') and then a simple loop over the
 114           * array, checking if $current is defined as a constant and otherwise define said constant with
 115           * its default value. I did not wrote it that way because that would make it difficult for code
 116           * parsers to identify which constants are defined and where. So, here it is, that long list of
 117           * if (!defined) define(). Ho and by the way, such beautiful comment, much right aligned, wow !
 118           */
 119  
 120          // physical path of YOURLS root
 121          if (!defined( 'YOURLS_ABSPATH' ))
 122              define('YOURLS_ABSPATH', $this->root);
 123  
 124          // physical path of includes directory
 125          if (!defined( 'YOURLS_INC' ))
 126              define('YOURLS_INC', YOURLS_ABSPATH.'/includes');
 127  
 128          // physical path of user directory
 129          if (!defined( 'YOURLS_USERDIR' ))
 130              define( 'YOURLS_USERDIR', YOURLS_ABSPATH.'/user' );
 131  
 132          // URL of user directory
 133          if (!defined( 'YOURLS_USERURL' ))
 134              define( 'YOURLS_USERURL', trim(YOURLS_SITE, '/').'/user' );
 135  
 136          // physical path of asset directory
 137          if( !defined( 'YOURLS_ASSETDIR' ) )
 138              define( 'YOURLS_ASSETDIR', YOURLS_ABSPATH.'/assets' );
 139  
 140          // URL of asset directory
 141          if( !defined( 'YOURLS_ASSETURL' ) )
 142              define( 'YOURLS_ASSETURL', trim(YOURLS_SITE, '/').'/assets' );
 143  
 144          // physical path of translations directory
 145          if (!defined( 'YOURLS_LANG_DIR' ))
 146              define( 'YOURLS_LANG_DIR', YOURLS_USERDIR.'/languages' );
 147  
 148          // physical path of plugins directory
 149          if (!defined( 'YOURLS_PLUGINDIR' ))
 150              define( 'YOURLS_PLUGINDIR', YOURLS_USERDIR.'/plugins' );
 151  
 152          // URL of plugins directory
 153          if (!defined( 'YOURLS_PLUGINURL' ))
 154              define( 'YOURLS_PLUGINURL', YOURLS_USERURL.'/plugins' );
 155  
 156          // physical path of themes directory
 157          if( !defined( 'YOURLS_THEMEDIR' ) )
 158              define( 'YOURLS_THEMEDIR', YOURLS_USERDIR.'/themes' );
 159  
 160          // URL of themes directory
 161          if( !defined( 'YOURLS_THEMEURL' ) )
 162              define( 'YOURLS_THEMEURL', YOURLS_USERURL.'/themes' );
 163  
 164          // physical path of pages directory
 165          if (!defined( 'YOURLS_PAGEDIR' ))
 166              define('YOURLS_PAGEDIR', YOURLS_USERDIR.'/pages' );
 167  
 168          // table to store URLs
 169          if (!defined( 'YOURLS_DB_TABLE_URL' ))
 170              define( 'YOURLS_DB_TABLE_URL', YOURLS_DB_PREFIX.'url' );
 171  
 172          // table to store options
 173          if (!defined( 'YOURLS_DB_TABLE_OPTIONS' ))
 174              define( 'YOURLS_DB_TABLE_OPTIONS', YOURLS_DB_PREFIX.'options' );
 175  
 176          // table to store hits, for stats
 177          if (!defined( 'YOURLS_DB_TABLE_LOG' ))
 178              define( 'YOURLS_DB_TABLE_LOG', YOURLS_DB_PREFIX.'log' );
 179  
 180          // if set to true, verbose debug infos
 181          if (!defined( 'YOURLS_DEBUG' )) {
 182              define('YOURLS_DEBUG', false);
 183          }
 184  
 185      }
 186  
 187  }


Generated: Sun Jul 12 05:10:03 2026 Cross-referenced by PHPXref 0.7.1