PluginProbe
Two Factor Authentication / 1.14.17
Two Factor Authentication v1.14.17
1.12.2 1.13.0 1.14.10 1.14.11 1.14.14 1.14.15 1.14.16 1.14.17 1.14.23 1.14.24 1.14.26 1.14.27 1.14.3 1.14.4 1.14.5 1.14.7 1.14.8 1.15.5 1.16.0 1.2.10 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 All 98 releases
two-factor-authentication / simba-tfa / includes / tfa-encryption-muplugin.php

tfa-encryption-muplugin.php in Two Factor Authentication 1.14.17, at simba-tfa/includes/tfa-encryption-muplugin.php

111 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('Access denied.');
4
5 class Simba_TFA_Encryption_Muplugin {
6
7 /**
8 * The simba tfa object
9 *
10 * @var object
11 */
12 private $simba_tfa;
13
14 /**
15 * Full path to the file we're managing
16 *
17 * @var string
18 */
19 private $file_path;
20
21 /**
22 * The class constructor
23 *
24 * @param object $simba_tfa - the simba tfa object
25 */
26 public function __construct($simba_tfa) {
27 $this->simba_tfa = $simba_tfa;
28 $this->file_path = path_join($this->get_mu_plugin_dir(), 'simba-tfa-encryption-key.php');
29 }
30
31 /**
32 * Returns full path to mu-plugin directory
33 *
34 * @return string - the mu-plugin directory path
35 */
36 public function get_mu_plugin_dir() {
37 return WPMU_PLUGIN_DIR;
38 }
39
40 /**
41 * Returns the full path to the mu-plugin file
42 *
43 * @return string - the mu-plugin path
44 */
45 public function get_file_path() {
46 return $this->file_path;
47 }
48
49 /**
50 * This function checks if our mu-plugin exists
51 *
52 * @return boolean - true if the file exists otherwise false
53 */
54 public function muplugin_exists() {
55 return file_exists($this->file_path);
56 }
57
58 /**
59 * Inserts our code into our mu-plugin.
60 *
61 * The mu-plugin and the mu-plugin directory will be created if they don't already exists
62 *
63 * @return boolean|WP_Error - true on success or WP_Error on failure
64 */
65 public function insert_contents() {
66
67 $info = pathinfo($this->file_path);
68
69 if (!isset($info['dirname'])) {
70 return new WP_Error(
71 'file_no_directory',
72 __('Encrypt secrets feature not enabled: no directory has been set.', 'two-factor-authentication') . ' ' . sprintf(__('Please check your %s constant is valid', 'two-factor-authentication'), 'WPMU_PLUGIN_DIR'),
73 $this->file_path
74 );
75 }
76
77 if (false === wp_mkdir_p($info['dirname'])) {
78 return new WP_Error(
79 'file_no_directory_created',
80 sprintf(__('The encrypt secrets feature was not enabled: your mu-plugins directory could not be automatically created; therefore, please use your web hosting file manager or FTP to manually create this folder and then try again: %s', 'two-factor-authentication'), $this->get_mu_plugin_dir()),
81 $info['dirname']
82 );
83 }
84
85 if (false === @file_put_contents($this->file_path, $this->get_contents())) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- ignore this as it is handled by the caller
86 return new WP_Error(
87 'file_no_contents',
88 __('The encrypt secrets feature was not enabled: attempting to write the mu-plugin file contents failed; therefore, please create the file manually.', 'two-factor-authentication') . "<br><br>" . sprintf(__('Add the following code to the file %s'), $this->get_file_path()) . "\n" . '<br><br><code>' . nl2br(esc_html($this->get_contents())) . '</code><br><br>' . __('Once you have added the above code then press the button to turn on encryption again', 'two-factor-authentication'),
89 $info['dirname']
90 );
91 }
92
93 return true;
94 }
95
96 /**
97 * This function creates the contents for the mu-plugin
98 *
99 * @return string - the contents of the mu-plugin
100 */
101 public function get_contents() {
102 $encryption_key = base64_encode($this->simba_tfa->random_bytes(16));
103 $code = "<?php\n";
104 $code .= "// Simba TFA Database encryption key. Do not change this unless you wish all your existing encrypted keys to become unusable (e.g. if you intend to then replace them all).\n";
105 $code .= "if (!defined('SIMBA_TFA_DB_ENCRYPTION_KEY')) define('SIMBA_TFA_DB_ENCRYPTION_KEY', '{$encryption_key}');";
106
107 return $code;
108 }
109 }
110
111