Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * $shimansky.biz
- *
- * Static web site core scripts
- * @category PHP
- * @access public
- * @copyright (c) 2012 Shimansky.biz
- * @author Serguei Shimansky <serguei@shimansky.biz>
- * @license http://opensource.org/licenses/bsd-license.php
- * @package shimansky.biz
- * @link https://bitbucket.org/englishextra/shimansky.biz
- * @link https://github.com/englishextra/shimansky.biz.git
- */
- /**
- * General purpose PHP class to work with strings and files
- *
- * PHP version 5.4+
- *
- * Copyright (c) 2012 Shimansky.biz
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the organization.
- * 4. Neither the name of the organizatio> nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ''AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Methods:
- *
- * 1. is_utf8
- * 2. safe_str
- * 3. get_post
- * 4. ensure_amp
- * 5. ensure_lt_gt
- * 6. ord_space
- * 7. ord_underscore
- * 8. ord_hypher
- * 9. ord_newline
- * 10. remove_tags
- * 11. remove_ents
- * 12. remove_comments
- * 13. has_http
- * 14. is_ip
- * 15. write_file
- * 16. clear_data
- * 17. remove_dir_content
- * 18. remove_bbcoded
- * 19. text_symbs_to_num_ents
- * 20. acc_text_to_num_ents
- * 21. safe_html
- * 22. random_anchor
- *
- * PHP version 5.4+
- *
- * @category PHP
- * @access public
- * @copyright (c) 2012 Shimansky.biz
- * @author Serguei Shimansky <serguei@shimansky.biz>
- * @license http://opensource.org/licenses/bsd-license.php
- * @package shimansky.biz
- * @version 0.1
- * @https://github.com/englishextra/shimansky.biz
- */
- class Swamper {
- /**
- * There is a difference between the two: If you write an empty __construct() function, you overwrite any inherited __construct() from a parent class.
- * So if you don't need it and you do not want to overwrite the parent constructor explicitly, don't write it at all.
- */
- function __construct() {
- }
- public function is_utf8($s) {
- // From http://w3.org/International/questions/qa-forms-utf-8.html
- return preg_match('%^(?:
- [\x09\x0A\x0D\x20-\x7E] # ASCII
- | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
- | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
- | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
- | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
- | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
- | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
- | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
- )*$%xs', $s);
- }
- public function safe_str($s) {
- return str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", "", trim(@strval($s))));
- }
- public function get_post($s, $v = '') {
- if (isset($_GET[$s]) || isset($_POST[$s])) {
- $v = isset($_GET[$s]) ? $_GET[$s] : (isset($_POST[$s]) ? urldecode($_POST[$s]) : '');
- if (is_array($v)) {
- foreach ($v as &$v1) {
- $v1 = strtr($v1, array_flip(get_html_translation_table(HTML_ENTITIES)));
- $v1 = trim($v1);
- $v1 = $this->safe_str($v1);
- }
- unset($v1);
- } else {
- $v = strtr($v, array_flip(get_html_translation_table(HTML_ENTITIES)));
- $v = trim($v);
- $v = $this->safe_str($v);
- }
- }
- return $v;
- }
- public function ensure_amp($s) {
- $s = str_replace('&', '&', $s);
- $s = str_replace(array('&amp;', '&#'), array('&', '&#'), $s);
- $s = preg_replace("/(\&\;)([a-z0-9]+)(\;)/", "&\\2;", $s);
- $s = preg_replace("/(\&\#)([a-z0-9]+)(\;)/", "&#\\2;", $s);
- return $s;
- }
- public function ensure_lt_gt($s) {
- $s = str_replace('<', '<', $s);
- $s = str_replace('>', '>', $s);
- $s = str_replace(array('&lt;', '&gt;'), array('<', '>'), $s);
- return $s;
- }
- public function ord_space($s) {
- return trim(preg_replace("/[\ ]+/", " ", $s));
- }
- public function ord_underscore($s) {
- return trim(preg_replace("/[\_]+/", "_", $s));
- }
- public function ord_hypher($s) {
- return trim(preg_replace("/[\-]+/", "-", $s));
- }
- public function ord_newline($s) {
- return preg_replace("/[\r]+/s", "\r", $s);
- return preg_replace("/[\n]+/s", "\n", $s);
- }
- public function remove_tags($s) {
- $s = stripslashes($s);
- $s = preg_replace("'<script[^>]*?>.*?</script>'si", ' ', $s);
- $s = preg_replace("'<style[^>]*?>.*?</style>'si", ' ', $s);
- $s = preg_replace("'<[\/\!]*?[^<>]*?>'si", ' ', $s);
- $s = $this->ord_space($s);
- return $s;
- }
- public function remove_ents($s) {
- $s = preg_replace("'(\&)([A-Za-z0-9\#]+)(\;)'si", ' ', $s);
- return $s;
- }
- public function remove_comments($s) {
- $s = preg_replace("'<!--.*?-->'si", ' ', $s);
- $s = preg_replace("'\/\*.*?\*\/'si", ' ', $s);
- return $s;
- }
- public function has_http($s, $r = false) {
- if (preg_match("/^(http|https|ftp)\:\/\//", $s) &&
- !preg_match("/^\//", $s)) {
- return $r = true;
- }
- }
- public function is_ip($ip) {
- //first of all the format of the ip address is matched
- if (preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $ip)) {
- //now all the intger values are separated
- $parts = explode(".", $ip);
- //now we need to check each part can range from 0-255
- foreach ($parts as $ip_parts) {
- if (intval($ip_parts) > 255 || intval($ip_parts) < 0) {
- return false; //if number is not within range of 0-255
- }
- }
- return true;
- } else {
- return false; //if format of ip address doesn't matches
- }
- }
- public function write_file($data, $p, $type) {
- if (!$fo = fopen($data, $type)) {
- die('Cannot open file: ' . $data);
- }
- if (!is_writable($data)) {
- die('Cannot write file: ' . $data);
- }
- flock($fo, LOCK_EX);
- fputs($fo, $p);
- fflush($fo);
- flock($fo, LOCK_UN);
- fclose($fo);
- }
- public function clear_data($data) {
- $p = '';
- if (file_exists($data)) {
- if (!$fo = fopen($data, "w+")) {
- die('Cannot open file: ' . $data);
- }
- flock($fo, LOCK_EX);
- fputs($fo, $p);
- fflush($fo);
- flock($fo, LOCK_UN);
- fclose($fo);
- }
- }
- public function remove_dir_content($dirname = '.') {
- if (is_dir($dirname)) {
- echo '<strong>' . $dirname . '</strong> is a directory.<br />';
- if ($handle = @opendir($dirname)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != "..") {
- echo '<strong>' . $file . '</strong> deleted<br />';
- $fullpath = $dirname . '/' . $file;
- if (is_dir($fullpath)) {
- $this->remove_dir_content($fullpath);
- @rmdir($fullpath);
- } else {
- @unlink($fullpath);
- }
- }
- }
- closedir($handle);
- }
- }
- }
- public function remove_bbcoded($s) {
- $a = array(
- '(\[img\])(.*?)(\[\/img\])' => ' ',
- '(\[img\=left\])(.*?)(\[\/img\])' => ' ',
- '(\[img\=right\])(.*?)(\[\/img\])' => ' ',
- '(\[color\=[a-zA-Z0-9\#]+\])(.*?)(\[\/color\])' => ' ',
- '(\[)(.*?)(\])' => ' '
- );
- foreach ($a as $k => $v) {
- $s = preg_replace("/${k}/", $v, $s);
- $s = $this->ord_space($s);
- }
- return $s;
- }
- public function text_symbs_to_dec_ents($s) {
- /*
- $a[] = null;
- dont include these:
- $a[] = array(' ', ' ');
- $a[] = array('&', '&');
- $a[] = array('#', '#');
- $a[] = array(';', ';');
- $a[] = array('0', '0');
- $a[] = array('1', '1');
- $a[] = array('2', '2');
- $a[] = array('3', '3');
- $a[] = array('4', '4');
- $a[] = array('5', '5');
- $a[] = array('6', '6');
- $a[] = array('7', '7');
- $a[] = array('8', '8');
- $a[] = array('9', '9');
- */
- $a = null;
- $a = array(
- '¡' => '¡', '¢' => '¢', '£' => '£', '¤' => '¤', '¥' => '¥', '¦' => '¦', '§' => '§', '¨' => '¨', '©' => '©', 'ª' => 'ª', '«' => '«', '¬' => '¬', '' => '­', '®' => '®', '¯' => '¯', '°' => '°', '±' => '±', '²' => '²', '³' => '³', '´' => '´', 'µ' => 'µ', '¶' => '¶', '·' => '·', '¸' => '¸', '¹' => '¹', 'º' => 'º', '»' => '»', '¼' => '¼', '½' => '½', '¾' => '¾', '¿' => '¿', 'À' => 'À', 'Á' => 'Á', 'Â' => 'Â', 'Ã' => 'Ã', 'Ä' => 'Ä', 'Å' => 'Å', 'Æ' => 'Æ', 'Ç' => 'Ç', 'È' => 'È', 'É' => 'É', 'Ê' => 'Ê', 'Ë' => 'Ë', 'Ì' => 'Ì', 'Í' => 'Í', 'Î' => 'Î', 'Ï' => 'Ï', 'Ð' => 'Ð', 'Ñ' => 'Ñ', 'Ò' => 'Ò', 'Ó' => 'Ó', 'Ô' => 'Ô', 'Õ' => 'Õ', 'Ö' => 'Ö', '×' => '×', 'Ø' => 'Ø', 'Ù' => 'Ù', 'Ú' => 'Ú', 'Û' => 'Û', 'Ü' => 'Ü', 'Ý' => 'Ý', 'Þ' => 'Þ', 'ß' => 'ß', 'à' => 'à', 'á' => 'á', 'â' => 'â', 'ã' => 'ã', 'ä' => 'ä', 'å' => 'å', 'æ' => 'æ', 'ç' => 'ç', 'è' => 'è', 'é' => 'é', 'ê' => 'ê', 'ë' => 'ë', 'ì' => 'ì', 'í' => 'í', 'î' => 'î', 'ï' => 'ï', 'ð' => 'ð', 'ñ' => 'ñ', 'ò' => 'ò', 'ó' => 'ó', 'ô' => 'ô', 'õ' => 'õ', 'ö' => 'ö', '÷' => '÷', 'ø' => 'ø', 'ù' => 'ù', 'ú' => 'ú', 'û' => 'û', 'ü' => 'ü', 'ý' => 'ý', 'þ' => 'þ', 'ÿ' => 'ÿ', 'Œ' => 'Œ', 'œ' => 'œ', 'Š' => 'Š', 'š' => 'š', 'Ÿ' => 'Ÿ', 'ƒ' => 'ƒ', 'ˆ' => 'ˆ', '˜' => '˜', 'Α' => 'Α', 'Β' => 'Β', 'Γ' => 'Γ', 'Δ' => 'Δ', 'Ε' => 'Ε', 'Ζ' => 'Ζ', 'Η' => 'Η', 'Θ' => 'Θ', 'Ι' => 'Ι', 'Κ' => 'Κ', 'Λ' => 'Λ', 'Μ' => 'Μ', 'Ν' => 'Ν', 'Ξ' => 'Ξ', 'Ο' => 'Ο', 'Π' => 'Π', 'Ρ' => 'Ρ', 'Σ' => 'Σ', 'Τ' => 'Τ', 'Υ' => 'Υ', 'Φ' => 'Φ', 'Χ' => 'Χ', 'Ψ' => 'Ψ', 'Ω' => 'Ω', 'α' => 'α', 'β' => 'β', 'γ' => 'γ', 'δ' => 'δ', 'ε' => 'ε', 'ζ' => 'ζ', 'η' => 'η', 'θ' => 'θ', 'ι' => 'ι', 'κ' => 'κ', 'λ' => 'λ', 'μ' => 'μ', 'ν' => 'ν', 'ξ' => 'ξ', 'ο' => 'ο', 'π' => 'π', 'ρ' => 'ρ', 'ς' => 'ς', 'σ' => 'σ', 'τ' => 'τ', 'υ' => 'υ', 'φ' => 'φ', 'χ' => 'χ', 'ψ' => 'ψ', 'ω' => 'ω', 'ϑ' => 'ϑ', 'ϒ' => 'ϒ', 'ϖ' => 'ϖ', ' ' => ' ', ' ' => ' ', ' ' => ' ', '' => '‌', '' => '‍', '' => '‎', '' => '‏', '–' => '–', '—' => '—', '‘' => '‘', '’' => '’', '‚' => '‚', '“' => '“', '”' => '”', '„' => '„', '†' => '†', '‡' => '‡', '•' => '•', '…' => '…', '‰' => '‰', '′' => '′', '″' => '″', '‹' => '‹', '›' => '›', '‾' => '‾', '⁄' => '⁄', '€' => '€', 'ℑ' => 'ℑ', '℘' => '℘', 'ℜ' => 'ℜ', '™' => '™', 'ℵ' => 'ℵ', '←' => '←', '↑' => '↑', '→' => '→', '↓' => '↓', '↔' => '↔', '↵' => '↵', '⇐' => '⇐', '⇑' => '⇑', '⇒' => '⇒', '⇓' => '⇓', '⇔' => '⇔', '∀' => '∀', '∂' => '∂', '∃' => '∃', '∅' => '∅', '∇' => '∇', '∈' => '∈', '∉' => '∉', '∋' => '∋', '∏' => '∏', '∑' => '∑', '−' => '−', '∗' => '∗', '√' => '√', '∝' => '∝', '∞' => '∞', '∠' => '∠', '∧' => '∧', '∨' => '∨', '∩' => '∩', '∪' => '∪', '∫' => '∫', '∴' => '∴', '∼' => '∼', '≅' => '≅', '≈' => '≈', '≠' => '≠', '≡' => '≡', '≤' => '≤', '≥' => '≥', '⊂' => '⊂', '⊃' => '⊃', '⊄' => '⊄', '⊆' => '⊆', '⊇' => '⊇', '⊕' => '⊕', '⊗' => '⊗', '⊥' => '⊥', '⋅' => '⋅', '⌈' => '⌈', '⌉' => '⌉', '⌊' => '⌊', '⌋' => '⌋', '◊' => '◊', '♠' => '♠', '♣' => '♣', '♥' => '♥', '♦' => '♦', '⟨' => '〈', '⟩' => '〉',
- /* '!' => '!', */
- /* '"' => '"', */
- /* '$' => '$', */
- /* '%' => '%', */
- /* '(' => '(', */
- /* ')' => ')', */
- /* '*' => '*', */
- /* '+' => '+', */
- /* ',' => ',', */
- /* '-' => '-', */
- /* '.' => '.', */
- /* '/' => '/', */
- /* ':' => ':', */
- /* '<' => '<', */
- /* '=' => '=', */
- /* '>' => '>', */
- /* '?' => '?', */
- '@' => '@',
- /* '[' => '[', */
- /* '\'' => ''', */
- /* '\\' => '\', */
- /* ']' => ']', */
- '^' => '^',
- /* '_' => '_', */
- '`' => '`',
- /* '{' => '{', */
- /* '|' => '|', */
- /* '}' => '}', */
- /* '~' => '~', */
- '✁' => '✁', '✂' => '✂', '✃' => '✃', '✄' => '✄', '✆' => '✆', '✇' => '✇', '✈' => '✈', '✉' => '✉', '✌' => '✌', '✍' => '✍', '✎' => '✎', '✏' => '✏', '✐' => '✐', '✑' => '✑', '✒' => '✒', '✓' => '✓', '✔' => '✔', '✕' => '✕', '✖' => '✖', '✗' => '✗', '✘' => '✘', '✙' => '✙', '✚' => '✚', '✛' => '✛', '✜' => '✜', '✝' => '✝', '✞' => '✞', '✟' => '✟', '✠' => '✠', '✡' => '✡', '✢' => '✢', '✣' => '✣', '✤' => '✤', '✥' => '✥', '✦' => '✦', '✧' => '✧', '✩' => '✩', '✪' => '✪', '✫' => '✫', '✬' => '✬', '✭' => '✭', '✮' => '✮', '✯' => '✯', '✰' => '✰', '✱' => '✱', '✲' => '✲', '✳' => '✳', '✴' => '✴', '✵' => '✵', '✶' => '✶', '✷' => '✷', '✸' => '✸', '✹' => '✹', '✺' => '✺', '✻' => '✻', '✼' => '✼', '✽' => '✽', '✾' => '✾', '✿' => '✿', '❀' => '❀', '❁' => '❁', '❂' => '❂', '❃' => '❃', '❄' => '❄', '❅' => '❅', '❆' => '❆', '❇' => '❇', '❈' => '❈', '❉' => '❉', '❊' => '❊', '❋' => '❋', '❍' => '❍', '❏' => '❏', '❐' => '❐', '❑' => '❑', '❒' => '❒', '❖' => '❖', '❘' => '❘', '❙' => '❙', '❚' => '❚', '❛' => '❛', '❜' => '❜', '❝' => '❝', '❞' => '❞', '❡' => '❡', '❢' => '❢', '❣' => '❣', '❤' => '❤', '❥' => '❥', '❦' => '❦', '❧' => '❧', '❶' => '❶', '❷' => '❷', '❸' => '❸', '❹' => '❹', '❺' => '❺', '❻' => '❻', '❼' => '❼', '❽' => '❽', '❾' => '❾', '❿' => '❿', '➀' => '➀', '➁' => '➁', '➂' => '➂', '➃' => '➃', '➄' => '➄', '➅' => '➅', '➆' => '➆', '➇' => '➇', '➈' => '➈', '➉' => '➉', '➊' => '➊', '➋' => '➋', '➌' => '➌', '➍' => '➍', '➎' => '➎', '➏' => '➏', '➐' => '➐', '➑' => '➑', '➒' => '➒', '➓' => '➓', '➔' => '➔', '➘' => '➘', '➙' => '➙', '➚' => '➚', '➛' => '➛', '➜' => '➜', '➝' => '➝', '➞' => '➞', '➟' => '➟', '➠' => '➠', '➡' => '➡', '➢' => '➢', '➣' => '➣', '➤' => '➤', '➥' => '➥', '➦' => '➦', '➧' => '➧', '➨' => '➨', '➩' => '➩', '➪' => '➪', '➫' => '➫', '➬' => '➬', '➭' => '➭', '➮' => '➮', '➯' => '➯', '➱' => '➱', '➲' => '➲', '➳' => '➳', '➴' => '➴', '➵' => '➵', '➶' => '➶', '➷' => '➷', '➸' => '➸', '➹' => '➹', '➺' => '➺', '➻' => '➻', '➼' => '➼', '➽' => '➽', '➾' => '➾');
- foreach($a as $k => $v) {
- $s = str_replace($k, $v, $s);
- }
- return $s;
- }
- public function acc_text_to_dec_ents($s) {
- $a = null;
- $a = array('Á' => 'Á', 'Â' => 'Â', 'Æ' => 'Æ', 'À' => 'À', 'Å' => 'Å', 'Ã' => 'Ã', 'Ä' => 'Ä', 'Ç' => 'Ç', 'É' => 'É', 'Ê' => 'Ê', 'È' => 'È', 'Ð' => 'Ð', 'Ë' => 'Ë', 'ƒ' => 'ƒ', 'Í' => 'Í', 'Î' => 'Î', 'Ì' => 'Ì', 'Ï' => 'Ï', 'Ñ' => 'Ñ', 'Ó' => 'Ó', 'Ô' => 'Ô', 'Œ' => 'Œ', 'Ò' => 'Ò', 'Ø' => 'Ø', 'Õ' => 'Õ', 'Ö' => 'Ö', 'Š' => 'Š', 'ß' => 'ß', 'Þ' => 'Þ', 'Ú' => 'Ú', 'Û' => 'Û', 'Ù' => 'Ù', 'ü' => 'ü', 'Ý' => 'Ý', 'Ÿ' => 'Ÿ', 'á' => 'á', 'â' => 'â', 'æ' => 'æ', 'à' => 'à', 'å' => 'å', 'ã' => 'ã', 'ä' => 'ä', 'ç' => 'ç', 'é' => 'é', 'ê' => 'ê', 'è' => 'è', 'ð' => 'ð', 'ë' => 'ë', 'ƒ' => 'ƒ', 'í' => 'í', 'î' => 'î', 'ì' => 'ì', 'ï' => 'ï', 'ñ' => 'ñ', 'ó' => 'ó', 'ô' => 'ô', 'œ' => 'œ', 'ò' => 'ò', 'ø' => 'ø', 'õ' => 'õ', 'ö' => 'ö', 'š' => 'š', 'ß' => 'ß', 'þ' => 'þ', 'ú' => 'ú', 'û' => 'û', 'ù' => 'ù', 'Ü' => 'Ü', 'ý' => 'ý', 'ÿ' => 'ÿ');
- foreach($a as $k => $v) {
- $s = str_replace($k, $v, $s);
- }
- return $s;
- }
- public function safe_html($s, $length = '') {
- $s = $this->safe_str($s);
- $s = $this->remove_comments($s);
- $s = $this->remove_tags($s);
- $s = $this->ensure_lt_gt($s);
- $s = $this->text_symbs_to_dec_ents($s);
- $s = $this->acc_text_to_dec_ents($s);
- if (!empty($length) && (mb_strlen($s, mb_detect_encoding($s)) > $length)) {
- $s = mb_substr($s, 0, ($length - 5), mb_detect_encoding($s)) . ' [...]';
- }
- $s = $this->ord_space($s);
- $s = $this->ord_newline($s);
- $s = $this->ensure_amp($s);
- return $s;
- }
- public function random_anchor() {
- $r = range(0, 9);
- shuffle($r);
- $ds = '';
- foreach ($r as $d) {
- $ds .= $d;
- }
- return $ds;
- }
- }
- /* if (!isset($Swamper) || empty($Swamper)) {
- $Swamper = new Swamper();
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement