Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function filter_html($data, $tipe = 'input', $parameter = null) {
- if (!$data) return false;
- if (is_array($data)) {
- foreach ($data as $key => $value) {
- $output[$key] = filter_html($value, $tipe, $parameter);
- }
- return $output;
- }
- else {
- if (empty($data)) return '';
- if (strtolower($tipe) == 'input') {
- /*----- default filter untuk input -----*/
- //menhapus tanda slash ketika diinputkan dari form HTML
- if(isset($parameter['stripslashes_from_query']) && $parameter['stripslashes_from_query'] && get_magic_quotes_gpc()) {
- $data = stripslashes($data);
- //jika karakter quot distrip dan fitur anti sql injection tidak didefinisikan, maka aktifkan sebagai default
- if (!isset($parameter['anti_sql_injection'])) $parameter['anti_sql_injection'] = true;
- }
- // anti HTML injection
- if (isset($parameter['allowed_tag']) && !empty($parameter['allowed_tag']))
- $data = strip_tags($data, $parameter['allowed_tag']);
- else
- $data = strip_tags($data);
- /*----- filter input opsional -----*/
- //anti SQL injection
- if (isset($parameter['anti_sql_injection']) && $parameter['anti_sql_injection']) {
- $escape_character = array('\\', "'", '"', '\n', '\r');
- $replace_character = array('\\\\', "\'", '\"', '\\n', '\\r');
- $data = str_replace($escape_character, $replace_character, $data);
- }
- }
- else if (strtolower($tipe) == 'output') {
- //jika user memperbolehkan beberapa tags untuk ditampilkan sebagai HTML
- //maka hanya strip yang diperbolehkan
- if (isset($parameter['allowed_tag']) && !empty($parameter['allowed_tag']))
- $data = strip_tags($data, $parameter['allowed_tag']);
- //selain itu, maka semua tag dianggap sebagai string biasa (escape HTML tags)
- else {
- if (!isset($parameter['stripslashes_from_query'])) $parameter['stripslashes_from_query'] = true;
- if ($parameter['stripslashes_from_query'])
- $data = stripslashes($data);
- $data = strip_tags($data);
- //$data = htmlspecialchars($data, ENT_QUOTES);
- }
- }
- return $data;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment