Models
2 months ago
WPConfig
1 year ago
AdminAccessService.php
3 years ago
PermissionsService.php
3 years ago
PermissionsServiceProvider.php
1 year ago
RolesService.php
4 months ago
SureCartSaltService.php
1 year ago
SureCartSaltService.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Permissions; |
| 4 | |
| 5 | use SureCart\Permissions\WPConfig\WPConfigTransformService; |
| 6 | |
| 7 | /** |
| 8 | * Handles salts |
| 9 | */ |
| 10 | class SureCartSaltService { |
| 11 | /** |
| 12 | * Get the path to the wp-config.php file. |
| 13 | * |
| 14 | * @return string|null |
| 15 | */ |
| 16 | public function getWpConfigPath() { |
| 17 | // Default location within ABSPATH. |
| 18 | if ( file_exists( ABSPATH . 'wp-config.php' ) ) { |
| 19 | return ABSPATH . 'wp-config.php'; |
| 20 | } |
| 21 | |
| 22 | // One directory above ABSPATH. |
| 23 | elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) { |
| 24 | return dirname( ABSPATH ) . '/wp-config.php'; |
| 25 | } |
| 26 | |
| 27 | // can't find it. |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Write the salt to the wp-config.php file. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function write() { |
| 37 | // If the wp-config.php file does not exist, return. |
| 38 | if ( empty( $this->getWpConfigPath() ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // If the LOGGED_IN_KEY constant is not defined, return. |
| 43 | if ( ! defined( 'LOGGED_IN_KEY' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // If the SURECART_ENCRYPTION_KEY constant is defined, return. |
| 48 | if ( defined( 'SURECART_ENCRYPTION_KEY' ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // Create the service. |
| 53 | try { |
| 54 | $service = new WPConfigTransformService( $this->getWpConfigPath() ); |
| 55 | |
| 56 | // sanity check. |
| 57 | if ( $service->exists( 'constant', 'SURECART_ENCRYPTION_KEY' ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $service->add( 'constant', 'SURECART_ENCRYPTION_KEY', LOGGED_IN_KEY ); |
| 62 | } catch ( \Exception $e ) { |
| 63 | error_log( $e->getMessage() ); |
| 64 | // silently fail. |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 |