Skip to content

Commit bc67744

Browse files
author
Fredrick Peter
committed
RemoveTags, Increment, Decrement Upgrade
1 parent 4dc17dc commit bc67744

File tree

7 files changed

+59
-55
lines changed

7 files changed

+59
-55
lines changed

README.md

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Having been introduced to learning Laravel Framework; Over the past yr(s), Comin
1212
was pretty tough. So i decided to create a much more easier way of communicating with Database, using native `PHP PDO:: Driver`.
1313

1414

15-
1615
* [Requirements](#requirements)
1716
* [Installation](#installation)
1817
* [Instantiate](#instantiate)
@@ -104,7 +103,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
104103
**Step 1** — update your `composer.json`:
105104
```composer.json
106105
"require": {
107-
"peterson/php-orm-database": "^3.1.4"
106+
"peterson/php-orm-database": "^3.1.5"
108107
}
109108
```
110109

@@ -183,8 +182,6 @@ $db->table('users')
183182
</details>
184183

185184
## More Database Connection Keys
186-
<details><summary>Read more...</summary>
187-
188185
- All available connection keys
189186
- The DRIVER_NAME uses only `mysql`
190187
- No other connection type is supported for now.
@@ -201,7 +198,6 @@ $db->table('users')
201198
| DB_PORT | int | `3306` |
202199
| DB_CHARSET | string | `utf8mb4_unicode_ci` |
203200
| DB_COLLATION | string | `utf8mb4` |
204-
</details>
205201

206202
## Usage
207203
- All Methods of usage
@@ -279,7 +275,6 @@ $db->table('users')
279275
```
280276

281277
### Increment
282-
<details><summary>Read more...</summary>
283278

284279
- Takes three parameter
285280
- Only the first param is required
@@ -301,25 +296,20 @@ $db->table('users')
301296
$db->table('users')
302297
->where('user_id', 10000001)
303298
->increment('wallet_bal', 10);
304-
305-
-- Query
306-
UPDATE `users`
307-
SET wallet_bal=wallet_bal+:10
308-
WHERE user_id=:user_id
309299
```
310300

311301
- You can also pass in a second or third parameter to update additional columns
312302
```
313303
$db->table('users')
314304
->where('user_id', 10000001)
315-
->increment('wallet_bal', 10, [
305+
->increment('wallet_bal', 100.23, [
316306
'first_name' => 'F. Peterson',
317307
'status' => 1,
318308
]);
319309
320310
-- Query
321311
UPDATE `users`
322-
SET wallet_bal=wallet_bal+:10, first_name=:first_name, status=:status
312+
SET wallet_bal=wallet_bal + :wallet_bal, first_name=:first_name, status=:status
323313
WHERE user_id=:user_id
324314
```
325315

@@ -332,7 +322,6 @@ $db->table('users')
332322
'status' => 1,
333323
]);
334324
```
335-
</details>
336325

337326
### Decrement
338327
- Same as Increment
@@ -368,26 +357,23 @@ SELECT count(*) FROM users WHERE status=:status
368357
</details>
369358

370359
### Remove Tags
371-
<details><summary>Read more...</summary>
372-
373-
- Helps against `XSS attacks`
374-
- By default we remove-prevention of `XSS attacks` as this should already been handled by Forms Validation before sending into the Database
375-
-> Applies to `insert` `update` `increment` `decrement` methods.
360+
- Takes one param as `bool` Default is `false`
361+
- Helps against `XSS attacks`
362+
- By default we did not handle `XSS attacks`. As we assume this should be done by `Forms Validation` before sending to Database
363+
-> Applies to `insert` `update` `increment` `decrement` methods.
376364

377365
- 1 usage
378366
```
379367
$db->table('post')
380-
->removeTags()
368+
->removeTags(true)
381369
->insert([
382-
'description' => '<script> alert(2); console.log('Blossom');</script>',
370+
'description' => "<script> alert(2); console.log('Blossom');</script>",
383371
'user_id' =>
384372
])
385373
386-
-- Query
387-
The value should be 'empty' if found as an attack
388-
Now the method automatically apply strict method of cleaning each values
374+
- If param set to true, then this will allow all possible tags
375+
- If false, it will allow few supported HTML5 tags
389376
```
390-
</details>
391377

392378
## Fetching Data
393379

@@ -454,8 +440,6 @@ SELECT * FROM `users`
454440
```
455441

456442
### Exists
457-
<details><summary>Read more...</summary>
458-
459443
```
460444
$db->table('users')
461445
->where('email', 'email@gmail.com')
@@ -465,7 +449,6 @@ $db->table('users')
465449
-- Query
466450
SELECT EXISTS(SELECT 1 FROM `users` WHERE email=:email OR name=:name) as `exists`
467451
```
468-
</details>
469452

470453
### Table Exist
471454
- Takes param as `string` `$table_name`
@@ -476,11 +459,11 @@ $db->tableExist('users');
476459
## Collections
477460
- You can directly use `methods` of `Collections Instance` on any of the below
478461
- All the below `methods` are received by Collection `class`
479-
1. get()
480-
2. first()
481-
3. firstOrFail()
482-
4. insert()
483-
5. insertOrIgnore()
462+
1. get()
463+
2. first()
464+
3. firstOrFail()
465+
4. insert()
466+
5. insertOrIgnore()
484467

485468

486469

@@ -660,7 +643,6 @@ $users->showing([
660643
- Multiple clause
661644

662645
### Raw
663-
<details><summary>Read more...</summary>
664646
- Allows you to use direct raw `SQL query syntax`
665647

666648
```
@@ -670,7 +652,7 @@ $date = strtotime('next week');
670652
$db->table("tb_wallet")
671653
->raw("date >= $date")
672654
->raw("NOW() > created_at")
673-
->raw("YEAR(created_at) = '2022'")
655+
->raw("YEAR(created_at) = 2022")
674656
->where('email', 'email@gmail.com')
675657
->limit(10)
676658
->random()
@@ -681,12 +663,10 @@ $db->table("tb_wallet")
681663
SELECT * FROM `tb_wallet`
682664
WHERE date >= 1681178855
683665
AND NOW() > created_at
684-
AND YEAR(created_at) = '2022'
666+
AND YEAR(created_at) = 2022
685667
AND email=:email
686668
ORDER BY RAND() LIMIT 10
687669
```
688-
</details>
689-
690670

691671
### Select
692672
- Used to select needed columns from database

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": ">=7.2",
2020
"vlucas/phpdotenv": "^5.3",
2121
"yidas/pagination": "^1.0",
22-
"symfony/var-dumper": "^6.2.8"
22+
"symfony/var-dumper": "^6.2.8",
23+
"ezyang/htmlpurifier": "^4.16.0"
2324
},
2425
"autoload": {
2526
"files": [
@@ -37,7 +38,7 @@
3738
},
3839
"extra": {
3940
"branch-alias": {
40-
"dev-main": "3.1.4-dev"
41+
"dev-main": "3.1.5-dev"
4142
}
4243
},
4344
"minimum-stability": "stable",

src/Capsule/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static public function saveTempIncrementQuery($data = [], $type = true)
353353
$sign = '-'; //decrement
354354
}
355355

356-
$tempIncrementQuery = "{$data['column']}={$data['column']}{$sign}:{$data['count']}";
356+
$tempIncrementQuery = "{$data['column']}={$data['column']} {$sign} :{$data['column']}";
357357
if(count($data['param']) > self::COUNT){
358358
$tempIncrementQuery .= ",";
359359
}

src/Query/Builder.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace builder\Database\Query;
66

7+
use Config;
8+
use Exception;
9+
use HTMLPurifier;
10+
711
class Builder extends MySqlExec{
812

913
/**
@@ -661,18 +665,25 @@ public function whitelistInput(mixed $input)
661665
}
662666

663667
// Convert input to string
664-
$filteredInput = (string) $input;
665-
666-
// Remove any script or style tags and their contents
667-
$filteredInput = preg_replace('/<(script|style)[^>]*?>.*?<\/\\1>/si', '', $filteredInput);
668-
669-
// Allow only letters, digits, spaces, and common punctuation marks
670-
$filteredInput = preg_replace('/[^\w\s.,!?():;\'"`-]/u', '', $filteredInput);
668+
$html = (string) $input;
669+
670+
$allowedTags = null;
671+
if ($this->allowAllTags) {
672+
// Allow all HTML tags except those seen as attacks
673+
$allowedTags = null;
674+
} else {
675+
// Allow only basic tags
676+
$allowedTags = '<a><abbr><address><area><article><aside><audio><b><base><bdi><bdo><blockquote><body><br><button><canvas><caption><cite><code><col><colgroup><data><datalist><dd><del><details><dfn><dialog><div><dl><dt><em><embed><fieldset><figcaption><figure><footer><form><h1><h2><h3><h4><h5><h6><head><header><hr><html><i><iframe><img><input><ins><kbd><label><legend><li><link><main><map><mark><meta><meter><nav><noscript><object><ol><optgroup><option><output><p><param><picture><pre><progress><q><rp><rt><ruby><s><samp><script><section><select><small><source><span><strong><style><sub><summary><sup><svg><table><tbody><td><template><textarea><tfoot><th><thead><time><title><tr><track><u><ul><var><video><wbr>';
677+
}
671678

672-
// Remove any extra whitespace
673-
$filteredInput = trim(preg_replace('/\s+/u', ' ', $filteredInput));
679+
// Use HTMLPurifier to remove any other potential XSS attacks
680+
$config = \HTMLPurifier_Config::createDefault();
681+
$config->set('HTML.Allowed', $allowedTags);
674682

675-
return $filteredInput;
683+
// purify html
684+
$purifier = new HTMLPurifier($config);
685+
$cleanHtml = $purifier->purify($html);
686+
return $cleanHtml;
676687
}
677688

678689
return $input;

src/Query/MySqlExec.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,19 @@ protected function allowCount()
367367

368368
/**
369369
* Remove Tags Found as an XSS-Attack
370+
* @param bool $tag\Default true
371+
* - If set to true, then this will allow all possible tags
372+
* - If false, it will allow few supported HTML5 tags
373+
* Apart from tags seen as an attack
370374
*
371375
* @return object\builder\Database\removeTags
372376
*/
373-
public function removeTags()
377+
public function removeTags(?bool $tag = true)
374378
{
375379
$this->removeTags = true;
376-
380+
if(!$tag){
381+
$this->allowAllTags = false;
382+
}
377383
return $this;
378384
}
379385

@@ -567,6 +573,7 @@ protected function closeQuery()
567573
$this->countQuery = false;
568574
$this->modelQuery = false;
569575
$this->removeTags = false;
576+
$this->allowAllTags = true;
570577
$this->runtime = 0.00;
571578
$this->timer = [
572579
'start' => 0.00,
@@ -575,7 +582,7 @@ protected function closeQuery()
575582
];
576583
}
577584

578-
/**
585+
/**
579586
* Get last insert ID
580587
* @param bool $type true or false
581588
* If true then it return an OBJECT data

src/Query/MySqlProperties.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ trait MySqlProperties{
153153
*/
154154
protected $removeTags = false;
155155

156+
/**
157+
* @var bool
158+
*/
159+
protected $allowAllTags = true;
160+
156161
/**
157162
* @var array
158163
*/

src/Traits/InsertionTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function incrementInsertionQuery(?array $temp = [])
117117
$this->query($this->query);
118118

119119
// bind increment data
120-
$this->bind(":{$temp['count']}", $temp['count']);
120+
$this->bind(":{$temp['column']}", $temp['count']);
121121

122122
// bind query for param
123123
foreach($temp['param'] as $key => $value){

0 commit comments

Comments
 (0)