| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX; |
| 4 |
|
| 5 |
final class Config { |
| 6 |
/** |
| 7 |
* Default configuration options for the application. |
| 8 |
* |
| 9 |
* This array provides the default settings and values for various |
| 10 |
* features and behaviors of the application. These options can be |
| 11 |
* customized to suit specific implementation requirements. |
| 12 |
* |
| 13 |
* Keys and their purposes: |
| 14 |
* - 'version': The version of the application (default: null). |
| 15 |
* - 'at': Replacement string for the "@" symbol (default: ' [at] '). |
| 16 |
* - 'dot': Replacement string for the "." symbol (default: ' [dot] '). |
| 17 |
* - 'css_id': CSS ID to use for specific elements (default: ''). |
| 18 |
* - 'css_class': CSS class to use for specific elements (default: ''). |
| 19 |
* - 'the_content': Flag to enable processing on content (default: 1). |
| 20 |
* - 'render_block': Flag to control rendering of blocks (default: 1). |
| 21 |
* - 'the_meta_key': Flag to enable processing on meta keys (default: 1). |
| 22 |
* - 'the_excerpt': Flag to enable processing on excerpts (default: 1). |
| 23 |
* - 'comment_text': Flag to enable processing on comments (default: 1). |
| 24 |
* - 'widget_text': Flag to enable processing in widgets (default: 1). |
| 25 |
* - 'java': Flag indicating JavaScript-related configurations (default: 1). |
| 26 |
* - 'load_java': Flag to enable JavaScript loading (default: 1). |
| 27 |
* - 'opt_linktext': Option for link text settings (default: 0). |
| 28 |
* - 'autolink': Flag to enable auto-linking of content (default: 1). |
| 29 |
* - 'alt_linktext': Alternative text for links (default: ''). |
| 30 |
* - 'alt_linkimage': Alternative image for links (default: ''). |
| 31 |
* - 'http_linkimage_title': Link image title with HTTP reference (default: ''). |
| 32 |
* - 'alt_linkimage_title': Alternative title for the link image (default: ''). |
| 33 |
* - 'excludedIDs': IDs to exclude from processing (default: ''). |
| 34 |
* - 'metaBox': Flag to enable or disable meta box features (default: 1). |
| 35 |
* - 'alt_uploadedimage': Alternative uploaded image setting (default: '0'). |
| 36 |
* - 'c2i_font': Custom font setting (default: null). |
| 37 |
* - 'c2i_fontSize': Font size for configuration (default: 10). |
| 38 |
* - 'c2i_fontRGB': Font color in RGB format (default: '#000000'). |
| 39 |
* - 'echo': Flag to enable output directly to the browser (default: 1). |
| 40 |
* - 'whiteList': Comma-separated string of allowed file extensions (default: 'jpeg,jpg,png,gif'). |
| 41 |
* - 'disable_rss': Flag to disable CryptX in RSS feeds by default (default: 1). |
| 42 |
* - 'encryption_mode': Encryption mode setting (default: 'secure'). |
| 43 |
* - 'encryption_password': Password for encryption; auto-generated if null (default: null). |
| 44 |
* - 'use_secure_encryption': Flag to enable secure encryption by default (default: 1). |
| 45 |
*/ |
| 46 |
private const DEFAULT_OPTIONS = [ |
| 47 |
'version' => null, |
| 48 |
'at' => ' [at] ', |
| 49 |
'dot' => ' [dot] ', |
| 50 |
'css_id' => '', |
| 51 |
'css_class' => '', |
| 52 |
'the_content' => 1, |
| 53 |
'the_meta_key' => 1, |
| 54 |
'the_excerpt' => 1, |
| 55 |
'comment_text' => 1, |
| 56 |
'widget_text' => 1, |
| 57 |
'java' => 1, |
| 58 |
'load_java' => 1, |
| 59 |
'opt_linktext' => 0, |
| 60 |
'autolink' => 1, |
| 61 |
'alt_linktext' => '', |
| 62 |
'alt_linkimage' => '', |
| 63 |
'http_linkimage_title' => '', |
| 64 |
'alt_linkimage_title' => '', |
| 65 |
'excludedIDs' => '', |
| 66 |
'metaBox' => 1, |
| 67 |
'alt_uploadedimage' => '0', |
| 68 |
'c2i_font' => null, |
| 69 |
'c2i_fontSize' => 10, |
| 70 |
'c2i_fontRGB' => '#000000', |
| 71 |
'echo' => 1, |
| 72 |
'whiteList' => 'jpeg,jpg,png,gif', |
| 73 |
'disable_rss' => 1, |
| 74 |
'encryption_mode' => 'secure', |
| 75 |
'encryption_password' => null, |
| 76 |
'use_secure_encryption' => 1, |
| 77 |
]; |
| 78 |
|
| 79 |
/** |
| 80 |
* An array of filter names used within the application. |
| 81 |
* These filters are commonly applied to various types of content, |
| 82 |
* including posts, comments, and widgets. |
| 83 |
*/ |
| 84 |
private const FILTERS = ['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', 'widget_text']; |
| 85 |
|
| 86 |
// Define the actual widget filters that will be used when widget_text is enabled |
| 87 |
private const WIDGET_FILTERS = [ |
| 88 |
'widget_text', // Legacy text widget (pre-4.9) |
| 89 |
'widget_text_content', // Modern text widget (4.9+) |
| 90 |
'widget_custom_html_content' // Custom HTML widget (4.8.1+) |
| 91 |
]; |
| 92 |
|
| 93 |
private array $options; |
| 94 |
private array $originalOptions; |
| 95 |
|
| 96 |
public function __construct(array $options = []) { |
| 97 |
$this->options = array_merge(self::DEFAULT_OPTIONS, $options); |
| 98 |
$this->originalOptions = $this->options; |
| 99 |
} |
| 100 |
|
| 101 |
public function getActiveFilters(): array { |
| 102 |
return array_filter(self::FILTERS, fn($filter) => |
| 103 |
isset($this->options[$filter]) && $this->options[$filter] |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function isMetaBoxEnabled(): bool { |
| 108 |
return (bool) ($this->options['metaBox'] ?? false); |
| 109 |
} |
| 110 |
|
| 111 |
public function isAutolinkEnabled(): bool { |
| 112 |
return (bool) ($this->options['autolink'] ?? false); |
| 113 |
} |
| 114 |
|
| 115 |
public function getLinkTextOption(): int { |
| 116 |
return (int) ($this->options['opt_linktext'] ?? 0); |
| 117 |
} |
| 118 |
|
| 119 |
public function getFontSettings(): array { |
| 120 |
return [ |
| 121 |
'font' => $this->options['c2i_font'], |
| 122 |
'size' => (int) $this->options['c2i_fontSize'], |
| 123 |
'color' => $this->options['c2i_fontRGB'] |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
public function getCssSettings(): array { |
| 128 |
return [ |
| 129 |
'id' => $this->options['css_id'], |
| 130 |
'class' => $this->options['css_class'] |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
public function getEmailReplacements(): array { |
| 135 |
return [ |
| 136 |
'at' => $this->options['at'], |
| 137 |
'dot' => $this->options['dot'] |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
public function getImageSettings(): array { |
| 142 |
return [ |
| 143 |
'url' => $this->options['alt_linkimage'], |
| 144 |
'title' => $this->options['http_linkimage_title'], |
| 145 |
'uploaded_id' => $this->options['alt_uploadedimage'], |
| 146 |
'uploaded_title' => $this->options['alt_linkimage_title'] |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
public function getExcludedIds(): array { |
| 151 |
$ids = $this->options['excludedIDs']; |
| 152 |
return $ids ? array_map('trim', explode(',', $ids)) : []; |
| 153 |
} |
| 154 |
|
| 155 |
public function getVersion(): ?string { |
| 156 |
return $this->options['version']; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get the actual widget filters to be applied |
| 161 |
* |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function getWidgetFilters(): array { |
| 165 |
return self::WIDGET_FILTERS; |
| 166 |
} |
| 167 |
|
| 168 |
public function updateFromShortcode(array $attributes, string $tag): void { |
| 169 |
$this->originalOptions = $this->options; |
| 170 |
$shortcodeOptions = shortcode_atts( |
| 171 |
$this->options, |
| 172 |
array_change_key_case($attributes, CASE_LOWER), |
| 173 |
$tag |
| 174 |
); |
| 175 |
$this->options = array_merge($this->options, $shortcodeOptions); |
| 176 |
} |
| 177 |
|
| 178 |
public function restoreOriginalOptions(): void { |
| 179 |
$this->options = $this->originalOptions; |
| 180 |
} |
| 181 |
|
| 182 |
public function save(): void { |
| 183 |
update_option('cryptX', $this->options); |
| 184 |
} |
| 185 |
|
| 186 |
public function update(array $newOptions): void |
| 187 |
{ |
| 188 |
// Convert checkbox values to integers |
| 189 |
foreach (['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', |
| 190 |
'widget_text', 'autolink', 'metaBox', 'disable_rss', 'use_secure_encryption'] as $key) { |
| 191 |
if (isset($newOptions[$key])) { |
| 192 |
$newOptions[$key] = (int)$newOptions[$key]; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$this->options = array_merge($this->options, $newOptions); |
| 197 |
$this->save(); // Save immediately after update |
| 198 |
} |
| 199 |
|
| 200 |
public function get(string $key, $default = null) { |
| 201 |
return $this->options[$key] ?? $default; |
| 202 |
} |
| 203 |
|
| 204 |
public function has(string $key): bool { |
| 205 |
return isset($this->options[$key]); |
| 206 |
} |
| 207 |
|
| 208 |
public function getAll(): array { |
| 209 |
return $this->options; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Resets all options to their default values |
| 214 |
* |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
public function reset(): void |
| 218 |
{ |
| 219 |
$this->options = self::DEFAULT_OPTIONS; |
| 220 |
|
| 221 |
// Set version and default font |
| 222 |
$this->options['version'] = CRYPTX_VERSION; |
| 223 |
|
| 224 |
// Set default font if available |
| 225 |
$fontFiles = glob(CRYPTX_DIR_PATH . 'fonts/*.ttf'); |
| 226 |
if (!empty($fontFiles)) { |
| 227 |
$this->options['c2i_font'] = basename($fontFiles[0]); |
| 228 |
} |
| 229 |
|
| 230 |
$this->save(); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Retrieves the encryption mode configured in the options. |
| 235 |
* |
| 236 |
* @return string Returns the encryption mode as a string. Defaults to 'secure' if not set. |
| 237 |
*/ |
| 238 |
public function getEncryptionMode(): string |
| 239 |
{ |
| 240 |
return $this->options['encryption_mode'] ?? 'secure'; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Checks if secure encryption is enabled in the options. |
| 245 |
* |
| 246 |
* @return bool Returns true if secure encryption is enabled, false otherwise. |
| 247 |
*/ |
| 248 |
public function isSecureEncryptionEnabled(): bool |
| 249 |
{ |
| 250 |
return (bool) ($this->options['use_secure_encryption'] ?? true); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Retrieves the encryption password configured in the options or generates a secure password if not set. |
| 255 |
* |
| 256 |
* @return string Returns the encryption password as a string. |
| 257 |
*/ |
| 258 |
public function getEncryptionPassword(): string |
| 259 |
{ |
| 260 |
if (empty($this->options['encryption_password'])) { |
| 261 |
// Generate a secure password based on WordPress keys |
| 262 |
$this->options['encryption_password'] = hash('sha256', |
| 263 |
(defined('AUTH_KEY') ? AUTH_KEY : '') . |
| 264 |
(defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : '') . |
| 265 |
get_site_url() |
| 266 |
); |
| 267 |
$this->save(); |
| 268 |
} |
| 269 |
return $this->options['encryption_password']; |
| 270 |
} |
| 271 |
} |