| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX; |
| 4 |
|
| 5 |
final class Config { |
| 6 |
private const DEFAULT_OPTIONS = [ |
| 7 |
'version' => null, |
| 8 |
'at' => ' [at] ', |
| 9 |
'dot' => ' [dot] ', |
| 10 |
'css_id' => '', |
| 11 |
'css_class' => '', |
| 12 |
'the_content' => 1, |
| 13 |
'the_meta_key' => 1, |
| 14 |
'the_excerpt' => 1, |
| 15 |
'comment_text' => 1, |
| 16 |
'widget_text' => 1, |
| 17 |
'java' => 1, |
| 18 |
'load_java' => 1, |
| 19 |
'opt_linktext' => 0, |
| 20 |
'autolink' => 1, |
| 21 |
'alt_linktext' => '', |
| 22 |
'alt_linkimage' => '', |
| 23 |
'http_linkimage_title' => '', |
| 24 |
'alt_linkimage_title' => '', |
| 25 |
'excludedIDs' => '', |
| 26 |
'metaBox' => 1, |
| 27 |
'alt_uploadedimage' => '0', |
| 28 |
'c2i_font' => null, |
| 29 |
'c2i_fontSize' => 10, |
| 30 |
'c2i_fontRGB' => '#000000', |
| 31 |
'echo' => 1, |
| 32 |
'filter' => ['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', 'widget_text'], |
| 33 |
'whiteList' => 'jpeg,jpg,png,gif', |
| 34 |
'disable_rss' => 1, // Disable CryptX in RSS feeds by default |
| 35 |
'encryption_mode' => 'secure', // Changed to 'secure' by default |
| 36 |
'encryption_password' => null, // Will be auto-generated if null |
| 37 |
'use_secure_encryption' => 1, // Enable secure encryption by default |
| 38 |
]; |
| 39 |
|
| 40 |
// Define the actual widget filters that will be used when widget_text is enabled |
| 41 |
private const WIDGET_FILTERS = [ |
| 42 |
'widget_text', // Legacy text widget (pre-4.9) |
| 43 |
'widget_text_content', // Modern text widget (4.9+) |
| 44 |
'widget_custom_html_content' // Custom HTML widget (4.8.1+) |
| 45 |
]; |
| 46 |
|
| 47 |
private array $options; |
| 48 |
private array $originalOptions; |
| 49 |
|
| 50 |
public function __construct(array $options = []) { |
| 51 |
$this->options = array_merge(self::DEFAULT_OPTIONS, $options); |
| 52 |
$this->originalOptions = $this->options; |
| 53 |
} |
| 54 |
|
| 55 |
public function getActiveFilters(): array { |
| 56 |
return array_filter($this->options['filter'], fn($filter) => |
| 57 |
isset($this->options[$filter]) && $this->options[$filter] |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function isMetaBoxEnabled(): bool { |
| 62 |
return (bool) ($this->options['metaBox'] ?? false); |
| 63 |
} |
| 64 |
|
| 65 |
public function isAutolinkEnabled(): bool { |
| 66 |
return (bool) ($this->options['autolink'] ?? false); |
| 67 |
} |
| 68 |
|
| 69 |
public function getLinkTextOption(): int { |
| 70 |
return (int) ($this->options['opt_linktext'] ?? 0); |
| 71 |
} |
| 72 |
|
| 73 |
public function getFontSettings(): array { |
| 74 |
return [ |
| 75 |
'font' => $this->options['c2i_font'], |
| 76 |
'size' => (int) $this->options['c2i_fontSize'], |
| 77 |
'color' => $this->options['c2i_fontRGB'] |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
public function getCssSettings(): array { |
| 82 |
return [ |
| 83 |
'id' => $this->options['css_id'], |
| 84 |
'class' => $this->options['css_class'] |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
public function getEmailReplacements(): array { |
| 89 |
return [ |
| 90 |
'at' => $this->options['at'], |
| 91 |
'dot' => $this->options['dot'] |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
public function getImageSettings(): array { |
| 96 |
return [ |
| 97 |
'url' => $this->options['alt_linkimage'], |
| 98 |
'title' => $this->options['http_linkimage_title'], |
| 99 |
'uploaded_id' => $this->options['alt_uploadedimage'], |
| 100 |
'uploaded_title' => $this->options['alt_linkimage_title'] |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
public function getExcludedIds(): array { |
| 105 |
$ids = $this->options['excludedIDs']; |
| 106 |
return $ids ? array_map('trim', explode(',', $ids)) : []; |
| 107 |
} |
| 108 |
|
| 109 |
public function getVersion(): ?string { |
| 110 |
return $this->options['version']; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the actual widget filters to be applied |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function getWidgetFilters(): array { |
| 119 |
return self::WIDGET_FILTERS; |
| 120 |
} |
| 121 |
|
| 122 |
public function updateFromShortcode(array $attributes, string $tag): void { |
| 123 |
$this->originalOptions = $this->options; |
| 124 |
$shortcodeOptions = shortcode_atts( |
| 125 |
$this->options, |
| 126 |
array_change_key_case($attributes, CASE_LOWER), |
| 127 |
$tag |
| 128 |
); |
| 129 |
$this->options = array_merge($this->options, $shortcodeOptions); |
| 130 |
} |
| 131 |
|
| 132 |
public function restoreOriginalOptions(): void { |
| 133 |
$this->options = $this->originalOptions; |
| 134 |
} |
| 135 |
|
| 136 |
public function save(): void { |
| 137 |
update_option('cryptX', $this->options); |
| 138 |
} |
| 139 |
|
| 140 |
public function update(array $newOptions): void |
| 141 |
{ |
| 142 |
// Convert checkbox values to integers |
| 143 |
foreach (['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', |
| 144 |
'widget_text', 'autolink', 'metaBox', 'disable_rss', 'use_secure_encryption'] as $key) { |
| 145 |
if (isset($newOptions[$key])) { |
| 146 |
$newOptions[$key] = (int)$newOptions[$key]; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$this->options = array_merge($this->options, $newOptions); |
| 151 |
$this->save(); // Save immediately after update |
| 152 |
} |
| 153 |
|
| 154 |
public function get(string $key, $default = null) { |
| 155 |
return $this->options[$key] ?? $default; |
| 156 |
} |
| 157 |
|
| 158 |
public function has(string $key): bool { |
| 159 |
return isset($this->options[$key]); |
| 160 |
} |
| 161 |
|
| 162 |
public function getAll(): array { |
| 163 |
return $this->options; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Resets all options to their default values |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function reset(): void |
| 172 |
{ |
| 173 |
$this->options = self::DEFAULT_OPTIONS; |
| 174 |
|
| 175 |
// Set version and default font |
| 176 |
$this->options['version'] = CRYPTX_VERSION; |
| 177 |
|
| 178 |
// Set default font if available |
| 179 |
$fontFiles = glob(CRYPTX_DIR_PATH . 'fonts/*.ttf'); |
| 180 |
if (!empty($fontFiles)) { |
| 181 |
$this->options['c2i_font'] = basename($fontFiles[0]); |
| 182 |
} |
| 183 |
|
| 184 |
$this->save(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Retrieves the encryption mode configured in the options. |
| 189 |
* |
| 190 |
* @return string Returns the encryption mode as a string. Defaults to 'secure' if not set. |
| 191 |
*/ |
| 192 |
public function getEncryptionMode(): string |
| 193 |
{ |
| 194 |
return $this->options['encryption_mode'] ?? 'secure'; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Checks if secure encryption is enabled in the options. |
| 199 |
* |
| 200 |
* @return bool Returns true if secure encryption is enabled, false otherwise. |
| 201 |
*/ |
| 202 |
public function isSecureEncryptionEnabled(): bool |
| 203 |
{ |
| 204 |
return (bool) ($this->options['use_secure_encryption'] ?? true); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Retrieves the encryption password configured in the options or generates a secure password if not set. |
| 209 |
* |
| 210 |
* @return string Returns the encryption password as a string. |
| 211 |
*/ |
| 212 |
public function getEncryptionPassword(): string |
| 213 |
{ |
| 214 |
if (empty($this->options['encryption_password'])) { |
| 215 |
// Generate a secure password based on WordPress keys |
| 216 |
$this->options['encryption_password'] = hash('sha256', |
| 217 |
(defined('AUTH_KEY') ? AUTH_KEY : '') . |
| 218 |
(defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : '') . |
| 219 |
get_site_url() |
| 220 |
); |
| 221 |
$this->save(); |
| 222 |
} |
| 223 |
return $this->options['encryption_password']; |
| 224 |
} |
| 225 |
} |