partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

ConfigSetupService.php (6677B)


      1 <?php
      2 
      3 namespace PartKeepr\SetupBundle\Services;
      4 
      5 use PartKeepr\SetupBundle\Visitor\ConfigVisitor;
      6 use PartKeepr\SetupBundle\Visitor\LegacyConfigVisitor;
      7 use Symfony\Bundle\TwigBundle\TwigEngine;
      8 
      9 class ConfigSetupService
     10 {
     11     /**
     12      * The authentification key length.
     13      */
     14     const KEY_LENGTH = 32;
     15 
     16     /**
     17      * @var TwigEngine
     18      */
     19     private $twig;
     20 
     21     /**
     22      * ConfigSetupService constructor.
     23      *
     24      * @param TwigEngine $twig
     25      */
     26     public function __construct(TwigEngine $twig)
     27     {
     28         $this->twig = $twig;
     29     }
     30 
     31     public function getConfig($config)
     32     {
     33         // Parameter defaults to ensure they exist
     34         $parameters = [
     35             'database_driver'   => null,
     36             'database_host'     => null,
     37             'database_port'     => null,
     38             'database_name'     => null,
     39             'database_password' => null,
     40 
     41             'mailer_transport'  => null,
     42             'mailer_host'       => null,
     43             'mailer_port'       => null,
     44             'mailer_encryption' => null,
     45             'mailer_user'       => null,
     46             'mailer_password'   => null,
     47             'mailer_auth_mode'  => null,
     48 
     49             'authentication_provider' => 'PartKeepr.Auth.HTTPBasicAuthenticationProvider',
     50 
     51             'locale' => 'en',
     52 
     53             'secret' => $this->generateSecret(),
     54 
     55             'fr3d_ldap.driver.host'                   => '127.0.0.1',
     56             'fr3d_ldap.driver.port'                   => 389,
     57             'fr3d_ldap.driver.username'               => null,
     58             'fr3d_ldap.driver.password'               => null,
     59             'fr3d_ldap.driver.bindRequiresDn'         => false,
     60             'fr3d_ldap.driver.baseDn'                 => '',
     61             'fr3d_ldap.driver.accountFilterFormat'    => null,
     62             'fr3d_ldap.driver.optReferrals'           => null,
     63             'fr3d_ldap.driver.useSsl'                 => null,
     64             'fr3d_ldap.driver.useStartTls'            => null,
     65             'fr3d_ldap.driver.accountCanonicalForm'   => null,
     66             'fr3d_ldap.driver.accountDomainName'      => null,
     67             'fr3d_ldap.driver.accountDomainNameShort' => null,
     68             'fr3d_ldap.user.enabled'                  => false,
     69             'fr3d_ldap.user.baseDn'                   => 'dc=example,dc=com',
     70             'fr3d_ldap.user.filter'                   => null,
     71             'fr3d_ldap.user.attribute.username'       => 'samaccountname',
     72             'fr3d_ldap.user.attribute.email'          => 'email',
     73 
     74             'partkeepr.filesystem.data_directory'      => '%kernel.root_dir%/../data/',
     75             'partkeepr.cronjob.check'                  => true,
     76             'partkeepr.filesystem.quota'               => false,
     77             'partkeepr.auth.max_users'                 => 'unlimited',
     78             'partkeepr.category.path_separator'        => ' ➤ ',
     79             'partkeepr.maintenance'                    => false,
     80             'partkeepr.maintenance.title'              => '',
     81             'partkeepr.maintenance.message'            => '',
     82             'cache.dunglas'                            => false,
     83             'cache.doctrine'                           => 'array',
     84             'partkeepr.parts.limit'                    => false,
     85             'partkeepr.users.limit'                    => false,
     86             'partkeepr.parts.internalpartnumberunique' => false,
     87             'partkeepr.octopart.apikey'                => "",
     88             'partkeepr.octopart.limit'                 => "3",
     89         ];
     90 
     91         if (function_exists('apc_fetch')) {
     92             $parameters['cache.dunglas'] = 'api.mapping.cache.apc';
     93             $parameters['cache.doctrine'] = 'apc';
     94         }
     95 
     96         $this->applyIf($parameters, $config);
     97 
     98         $parameters = array_merge($parameters, $config);
     99 
    100         if ($parameters['fr3d_ldap.user.attribute.username'] === null) {
    101             $parameters['fr3d_ldap.user.attribute.username'] = 'samaccountname';
    102         }
    103 
    104         if ($parameters['fr3d_ldap.user.attribute.email'] === null) {
    105             $parameters['fr3d_ldap.user.attribute.email'] = 'email';
    106         }
    107 
    108         array_walk_recursive($parameters, function (&$item) {
    109             $item = var_export($item, true);
    110         });
    111 
    112         ksort($parameters);
    113 
    114         return $this->twig->render(
    115             'PartKeeprSetupBundle::parameters.php.twig',
    116             ['parameters' => $parameters]
    117         );
    118     }
    119 
    120     public function legacyConfigParser()
    121     {
    122         if (file_exists($this->getLegacyConfigPath())) {
    123             $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
    124             $traverser = new \PHPParser_NodeTraverser();
    125             $traverser->addVisitor(new LegacyConfigVisitor());
    126             $statements = $parser->parse(file_get_contents($this->getLegacyConfigPath()));
    127             $traverser->traverse($statements);
    128 
    129             return LegacyConfigVisitor::getConfigValues();
    130         }
    131 
    132         return [];
    133     }
    134 
    135     public function configParser()
    136     {
    137         if (file_exists($this->getConfigPath(false))) {
    138             $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
    139             $traverser = new \PHPParser_NodeTraverser();
    140             $traverser->addVisitor(new ConfigVisitor());
    141             $statements = $parser->parse(file_get_contents($this->getConfigPath(false)));
    142             $traverser->traverse($statements);
    143 
    144             return ConfigVisitor::getConfigValues();
    145         }
    146 
    147         return [];
    148     }
    149 
    150     public function getConfigPath($test)
    151     {
    152         if ($test) {
    153             $filename = 'parameters_setup.php';
    154         } else {
    155             $filename = 'parameters.php';
    156         }
    157 
    158         return dirname(__FILE__).'/../../../../app/config/'.$filename;
    159     }
    160 
    161     public function getLegacyConfigPath()
    162     {
    163         return dirname(__FILE__).'/../../../../config.php';
    164     }
    165 
    166     public function applyIf($target, $source)
    167     {
    168         foreach ($target as $key => $value) {
    169             if (array_key_exists($key, $source)) {
    170                 $target[$key] = $source[$key];
    171             }
    172         }
    173 
    174         return $target;
    175     }
    176 
    177     public function generateSecret()
    178     {
    179         $secret = '';
    180         for ($i = 0; $i < self::KEY_LENGTH; $i++) {
    181             $secret .= chr(65 + rand(0, 16));
    182         }
    183 
    184         return $secret;
    185     }
    186 
    187     public function getAuthKey()
    188     {
    189         $findText = 'Your auth key is: ';
    190 
    191         $data = file_get_contents($this->getAuthKeyPath());
    192         $position = strpos($data, $findText);
    193 
    194         return substr($data, $position + strlen($findText), self::KEY_LENGTH);
    195     }
    196 
    197     public function getAuthKeyPath()
    198     {
    199         return dirname(__FILE__).'/../../../../app/authkey.php';
    200     }
    201 }