@@ -12,10 +12,12 @@ A robust and flexible data sanitization component for PHP, part of the KaririCod
1212- [ Installation] ( #installation )
1313- [ Usage] ( #usage )
1414 - [ Basic Usage] ( #basic-usage )
15- - [ Advanced Usage] ( #advanced-usage )
15+ - [ Advanced Usage: Blog Post Sanitization ] ( #advanced-usage-blog-post-sanitization )
1616- [ Available Sanitizers] ( #available-sanitizers )
17+ - [ Configuration] ( #configuration )
1718- [ Integration with Other KaririCode Components] ( #integration-with-other-kariricode-components )
1819- [ Development and Testing] ( #development-and-testing )
20+ - [ Contributing] ( #contributing )
1921- [ License] ( #license )
2022- [ Support and Community] ( #support-and-community )
2123
@@ -27,6 +29,7 @@ A robust and flexible data sanitization component for PHP, part of the KaririCod
2729- Configurable processors for customized sanitization logic
2830- Support for fallback values in case of sanitization failures
2931- Extensible architecture allowing custom sanitizers
32+ - Robust error handling and reporting
3033
3134## Installation
3235
@@ -52,10 +55,10 @@ use KaririCode\Sanitizer\Attribute\Sanitize;
5255
5356class UserProfile
5457{
55- #[Sanitize(sanitizers : ['trim', 'html_special_chars'])]
58+ #[Sanitize(processors : ['trim', 'html_special_chars'])]
5659 private string $name = '';
5760
58- #[Sanitize(sanitizers : ['trim', 'normalize_line_breaks'])]
61+ #[Sanitize(processors : ['trim', 'normalize_line_breaks'])]
5962 private string $email = '';
6063
6164 // Getters and setters...
@@ -79,74 +82,184 @@ $registry->register('sanitizer', 'normalize_line_breaks', new NormalizeLineBreak
7982$sanitizer = new Sanitizer($registry);
8083
8184$userProfile = new UserProfile();
82- $userProfile->setName(" Walmir Silva ");
83- $userProfile->setEmail("walmir.silva @example.com\r\n");
85+ $userProfile->setName(" John Doe ");
86+ $userProfile->setEmail("john.doe @example.com\r\n");
8487
85- $sanitizer->sanitize($userProfile);
88+ $result = $ sanitizer->sanitize($userProfile);
8689
87- echo $userProfile->getName(); // Output: "Walmir Silva"
88- echo $userProfile->getEmail(); // Output: "walmir.silva@example.com\n"
90+ echo $userProfile->getName(); // Output: "John Doe"
91+ echo $userProfile->getEmail(); // Output: "john.doe@example.com\n"
92+
93+ // Access sanitization results
94+ print_r($result['sanitizedValues']);
95+ print_r($result['messages']);
96+ print_r($result['errors']);
8997```
9098
91- ### Advanced Usage
99+ ### Advanced Usage: Blog Post Sanitization
92100
93- You can create custom sanitizers by implementing the ` Processor ` or ` ConfigurableProcessor ` interfaces :
101+ Here's a more comprehensive example demonstrating how to use the KaririCode Sanitizer in a real-world scenario, such as sanitizing blog post content :
94102
95103``` php
96- use KaririCode\Contract\Processor\ConfigurableProcessor;
97- use KaririCode\Sanitizer\Processor\AbstractSanitizerProcessor;
104+ <?php
98105
99- class CustomSanitizer extends AbstractSanitizerProcessor implements ConfigurableProcessor
100- {
101- private $option;
102-
103- public function configure(array $options): void
104- {
105- $this->option = $options['custom_option'] ?? 'default';
106- }
107-
108- public function process(mixed $input): string
109- {
110- $input = $this->guardAgainstNonString($input);
111- // Custom sanitization logic here
112- return $input;
113- }
114- }
106+ declare(strict_types=1);
115107
116- // Register and use the custom sanitizer
117- $registry->register('sanitizer', 'custom', new CustomSanitizer());
108+ require_once __DIR__ . '/../vendor/autoload.php';
118109
119- class AdvancedProfile
110+ use KaririCode\ProcessorPipeline\ProcessorRegistry;
111+ use KaririCode\Sanitizer\Attribute\Sanitize;
112+ use KaririCode\Sanitizer\Processor\Domain\HtmlPurifierSanitizer;
113+ use KaririCode\Sanitizer\Processor\Domain\MarkdownSanitizer;
114+ use KaririCode\Sanitizer\Processor\Input\HtmlSpecialCharsSanitizer;
115+ use KaririCode\Sanitizer\Processor\Input\NormalizeLineBreaksSanitizer;
116+ use KaririCode\Sanitizer\Processor\Input\StripTagsSanitizer;
117+ use KaririCode\Sanitizer\Processor\Input\TrimSanitizer;
118+ use KaririCode\Sanitizer\Processor\Security\XssSanitizer;
119+ use KaririCode\Sanitizer\Sanitizer;
120+
121+ class BlogPost
120122{
121- #[Sanitize(sanitizers: ['custom' => ['custom_option' => 'value']])]
122- private string $customField = '';
123+ #[Sanitize(
124+ processors: ['trim', 'html_special_chars', 'xss_sanitizer'],
125+ messages: [
126+ 'trim' => 'Title was trimmed',
127+ 'html_special_chars' => 'Special characters in title were escaped',
128+ 'xss_sanitizer' => 'XSS attempt was removed from title',
129+ ]
130+ )]
131+ private string $title = '';
132+
133+ #[Sanitize(
134+ processors: ['trim', 'normalize_line_breaks'],
135+ messages: [
136+ 'trim' => 'Slug was trimmed',
137+ 'normalize_line_breaks' => 'Line breaks in slug were normalized',
138+ ]
139+ )]
140+ private string $slug = '';
141+
142+ #[Sanitize(
143+ processors: ['trim', 'markdown', 'html_purifier'],
144+ messages: [
145+ 'trim' => 'Content was trimmed',
146+ 'markdown' => 'Markdown in content was processed',
147+ 'html_purifier' => 'HTML in content was purified',
148+ ]
149+ )]
150+ private string $content = '';
151+
152+ #[Sanitize(
153+ processors: ['trim', 'strip_tags', 'html_special_chars'],
154+ messages: [
155+ 'trim' => 'Author name was trimmed',
156+ 'strip_tags' => 'HTML tags were removed from author name',
157+ 'html_special_chars' => 'Special characters in author name were escaped',
158+ ]
159+ )]
160+ private string $authorName = '';
161+
162+ // Getters and setters...
123163}
164+
165+ // Set up the sanitizer
166+ $registry = new ProcessorRegistry();
167+ $registry->register('sanitizer', 'trim', new TrimSanitizer());
168+ $registry->register('sanitizer', 'html_special_chars', new HtmlSpecialCharsSanitizer());
169+ $registry->register('sanitizer', 'normalize_line_breaks', new NormalizeLineBreaksSanitizer());
170+ $registry->register('sanitizer', 'strip_tags', new StripTagsSanitizer());
171+ $registry->register('sanitizer', 'markdown', new MarkdownSanitizer());
172+ $registry->register('sanitizer', 'xss_sanitizer', new XssSanitizer());
173+
174+ // Configure HTML Purifier with specific settings for blog content
175+ $htmlPurifier = new HtmlPurifierSanitizer();
176+ $htmlPurifier->configure([
177+ 'allowedTags' => ['p', 'br', 'strong', 'em', 'u', 'ol', 'ul', 'li', 'a', 'img', 'h2', 'h3', 'blockquote'],
178+ 'allowedAttributes' => ['href' => ['a'], 'src' => ['img'], 'alt' => ['img']],
179+ ]);
180+ $registry->register('sanitizer', 'html_purifier', $htmlPurifier);
181+
182+ $sanitizer = new Sanitizer($registry);
183+
184+ // Simulating form submission with potentially unsafe data
185+ $blogPost = new BlogPost();
186+ $blogPost->setTitle(" Exploring KaririCode: A Modern PHP Framework <script >alert (' xss' ) </script > ");
187+ $blogPost->setSlug(" exploring-kariricode-a-modern-php-framework \r\n");
188+ $blogPost->setContent("
189+ # Introduction
190+
191+ KaririCode is a **powerful** and _flexible_ PHP framework designed for modern web development.
192+
193+ <script >alert (' malicious code' ); </script >
194+
195+ ## Key Features
196+
197+ 1. Robust sanitization
198+ 2. Efficient routing
199+ 3. Powerful ORM
200+
201+ Check out our [official website](https://kariricode.org) for more information!
202+
203+ <img src =\ " harmful.jpg\" onerror =\ " alert('xss')\" />
204+ ");
205+ $blogPost->setAuthorName("<b >John Doe</b > <script >alert (' xss' ) </script >");
206+
207+ $result = $sanitizer->sanitize($blogPost);
208+
209+ // Access sanitized data
210+ echo $blogPost->getTitle(); // Sanitized title
211+ echo $blogPost->getContent(); // Sanitized content
212+
213+ // Access sanitization details
214+ print_r($result['sanitizedValues']);
215+ print_r($result['messages']);
216+ print_r($result['errors']);
124217```
125218
219+ This example demonstrates how to use the KaririCode Sanitizer to clean and secure blog post data, including handling of Markdown content, HTML purification, and protection against XSS attacks.
220+
126221## Available Sanitizers
127222
128223The Sanitizer component provides various built-in sanitizers:
129224
130225### Input Sanitizers
131226
132- - TrimSanitizer
133- - HtmlSpecialCharsSanitizer
134- - NormalizeLineBreaksSanitizer
135- - StripTagsSanitizer
227+ - TrimSanitizer: Removes whitespace from the beginning and end of a string
228+ - HtmlSpecialCharsSanitizer: Converts special characters to HTML entities
229+ - NormalizeLineBreaksSanitizer: Standardizes line breaks across different operating systems
230+ - StripTagsSanitizer: Removes HTML and PHP tags from a string
136231
137232### Domain Sanitizers
138233
139- - HtmlPurifierSanitizer
140- - JsonSanitizer
141- - MarkdownSanitizer
234+ - HtmlPurifierSanitizer: Sanitizes HTML content using the HTML Purifier library
235+ - JsonSanitizer: Validates and prettifies JSON strings
236+ - MarkdownSanitizer: Sanitizes Markdown content
142237
143238### Security Sanitizers
144239
145- - FilenameSanitizer
146- - SqlInjectionSanitizer
147- - XssSanitizer
240+ - FilenameSanitizer: Ensures filenames are safe for use in file systems
241+ - SqlInjectionSanitizer: Protects against SQL injection attacks
242+ - XssSanitizer: Prevents Cross-Site Scripting (XSS) attacks
243+
244+ For detailed information on each sanitizer, including configuration options and usage examples, please refer to the [ documentation] ( https://kariricode.org/docs/sanitizer ) .
245+
246+ ## Configuration
148247
149- Each sanitizer is designed to handle specific types of data and security concerns. For detailed information on each sanitizer, please refer to the [ documentation] ( https://kariricode.org/docs/sanitizer ) .
248+ The Sanitizer component can be configured globally or per-sanitizer basis. Here's an example of how to configure the ` HtmlPurifierSanitizer ` :
249+
250+ ``` php
251+ use KaririCode\Sanitizer\Processor\Domain\HtmlPurifierSanitizer;
252+
253+ $htmlPurifier = new HtmlPurifierSanitizer();
254+ $htmlPurifier->configure([
255+ 'allowedTags' => ['p', 'br', 'strong', 'em'],
256+ 'allowedAttributes' => ['href' => ['a'], 'src' => ['img']],
257+ ]);
258+
259+ $registry->register('sanitizer', 'html_purifier', $htmlPurifier);
260+ ```
261+
262+ For global configuration options, refer to the ` Sanitizer ` class constructor.
150263
151264## Integration with Other KaririCode Components
152265
@@ -229,6 +342,19 @@ For a full list of available commands, run:
229342make help
230343```
231344
345+ ## Contributing
346+
347+ We welcome contributions to the KaririCode Sanitizer component! Here's how you can contribute:
348+
349+ 1 . Fork the repository
350+ 2 . Create a new branch for your feature or bug fix
351+ 3 . Write tests for your changes
352+ 4 . Implement your changes
353+ 5 . Run the test suite and ensure all tests pass
354+ 6 . Submit a pull request with a clear description of your changes
355+
356+ Please read our [ Contributing Guide] ( CONTRIBUTING.md ) for more details on our code of conduct and development process.
357+
232358## License
233359
234360This project is licensed under the MIT License - see the [ LICENSE] ( LICENSE ) file for details.
@@ -237,7 +363,8 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
237363
238364- ** Documentation** : [ https://kariricode.org/docs/sanitizer ] ( https://kariricode.org/docs/sanitizer )
239365- ** Issue Tracker** : [ GitHub Issues] ( https://github.com/KaririCode-Framework/kariricode-sanitizer/issues )
240- - ** Community** : [ KaririCode Club Community] ( https://kariricode.club )
366+ - ** Community Forum** : [ KaririCode Club Community] ( https://kariricode.club )
367+ - ** Stack Overflow** : Tag your questions with ` kariricode-sanitizer `
241368
242369---
243370
0 commit comments