* @version $Id$ * @access public * @license http://opensource.org/licenses/gpl-3.0.html */ /** * convertBase64Images() * get all img src from html and convert them * @param string $content * @param string $prefix * @param string $folder * @return string */ function convertBase64Images($content, $prefix = '', $folder = 'autosave') { // $flag = false; // flag base64 encoded image // if disabled, we return given content if(!defined('BASE64IMAGE_AUTOSAVE') || BASE64IMAGE_AUTOSAVE == 0) return $content; $html = new simple_html_dom(); $html->load($content); if($html->find('img', 0)) { foreach($html->find('img') as $element) { // base64 encoded images -> we convert them and replace source if(Stringy\Stringy::create($element->src, CHARSET)->startsWith('data:image/')) { $flag = true; // we update flag // echo $element->src . "\n
"; // logfile(LOG_MAINFILE, array('[DEBUG] '. $element->src)); // we save image to file $r = saveImage($element->src, $prefix, $folder); if($r !== false) { // we change source $element->src = str_replace(SITE_PATH, SITE_ROOT_URL, $r); } } } } // @todo remove debug - if everything allright with data ! lots of errors due to addslashes / stripslashes // $id = generateRandomString(3); // file_put_contents('../tmp/__'.$id.'.before.txt', $content); // file_put_contents('../tmp/__'.$id.'.after.txt', $html); // if no change, we return original content, else modified content // if($flag == false) return $content; // else return addslashes($html); return addslashes($html); } /** * set_flash_msg() * @param string $str * @param string $scope * @return boolean */ function set_flash_msg($str, $scope = true) { $_SESSION['message'] = $str; $_SESSION['message-scope'] = $scope; return true; } /** * display_flash_msg() * @param string $wrap * @param string $class * @param string $scope * @return boolean */ function display_flash_msg( $wrap = false, $class = '', $scope = true) { if(isset($_SESSION['message']) && $scope == $_SESSION['message-scope']) { $str = $_SESSION['message']; unset($_SESSION['message']); unset($_SESSION['message-scope']); if($wrap) echo '
'.$str.'
'; else echo $str; } return true; } /** * removeDomElement() * remove a given element such as 'img' in given html * @param string $element * @param string $content * @param boolean $removeEmptyTags * @return string */ function removeDomElement($element, $content, $removeEmptyTags = 'p, div') { $html = new simple_html_dom(); $html->load($content); foreach ($html->find('img') as $el) { $images = $el->outertext = ''; } // we also remove empy p if($removeEmptyTags != '' && is_string($removeEmptyTags)) { $tags = $html->find($removeEmptyTags); foreach($tags as $t) { if(trim($t->plaintext) == '') { // Remove an element, set it's outertext as an empty string $t->outertext = ''; } } } $content = $html; unset($html); return $content; } /** * saveImage() * save base64 image to flat file * @param string $img * @param string $prefix * @param string $folder * @return string | false */ function saveImage($img, $prefix, $folder) { $target = SITE_PATH . 'library/userfiles/' . $folder. '/'; if(!file_exists($target)) mkdir($target, 0777); // split the string on commas // $data[0] == "data:image/png;base64" or "data:image/jpeg;base64" // $data[1] == $data = explode( ',', $img ); $filename = $target . $prefix . generateRandomString(12) . '.png'; if(strlen($data[1]) > 1) { logfile(LOG_MAINFILE, array('based64 image encoded saved into file '. $filename)); $r = file_put_contents($filename, base64_decode($data[1])); chmod($filename, 0777); if($r == false) logfile(LOG_MAINFILE, array('[ERROR] when writing based64 image into file '. $filename)); return $filename; } return false; } /** * implode_with_keys() * implode avec clefs associées renvoyées * sous forme de chaîne de caractères * @param string $glue * @param string $array * @return string */ function implode_with_keys($glue, $array) { $output = array(); foreach( $array as $key => $item ) $output[] = $key . "=" . $item; return implode($glue, $output); } /** * is_filled() * check if given field is filled up in array * @param array $array * @param string $fieldname * @return boolean */ function is_filled($array, $fieldname) { foreach ($array as $el) { if(!empty($el[$fieldname])) return true; } return false; } /** * safe_redirect() * check if given url is safe * if not, return CURRENT_APP_URL * @param string $url * @return string */ function safe_redirect( $url ) { if(Stringy\Stringy::create($url, CHARSET)->startsWith(SITE_ROOT_URL)) return $url; if(Stringy\Stringy::create($url, CHARSET)->startsWith('index.php')) return $url; if(Stringy\Stringy::create($url, CHARSET)->startsWith('login.php')) return $url; if(Stringy\Stringy::create($url, CHARSET)->startsWith('logout.php')) return $url; if(Stringy\Stringy::create($url, CHARSET)->startsWith('confirm.php')) return $url; // provided url is not safe, we route to default CURRENT_APP_URL return CURRENT_APP_URL; } /** * getHttpParameters() * Renvoie les paramètres HTTP * sous forme de chaîne de caractères * @return string */ function getHttpParameters($prefix = '?') { return $prefix. (string) implode_with_keys('&', $_REQUEST); } /** * is_module() * Test if the current page matches the given module and action (optionnal) * examples : (1) is_module('system'), (2) is_module('system/edit_files') * interpreted as : (1) rub=system, (2) rub=system and todo=edit_files * @param string $str * @return boolean */ // @todo simo function is_module($str) { $a = explode('/', $str); _debug('is_module() values : ' .join(', ', $a). ' '. count($a)); // handle simple case : is_module('system') if(!isset($_REQUEST['rub']) || strpos($a[0], $_REQUEST['rub']) === false) return false; // handle strict case : is_module('system$') $ - return false is $todo is set if(strpos($a[0], '$') !== false && isset($_REQUEST['todo'])) return false; // handle case is_module('system/list') if(isset($a[1]) && !empty($a[1])) { if(!isset($_REQUEST['todo']) || $_REQUEST['todo'] != $a[1]) return false; } return true; } /** * Remove DOM Nodes * @param string $html * @param string $tag * @return string */ function removeDomNodes($html, $tag) { if(empty($html)) return $html; $html = str_get_html($html); foreach($html->find($tag) as $element) { $scripts = $element->outertext = ''; } return $html; } function cleanString($string, $allowed = array()) { $allow = null; if (!empty($allowed)) { foreach ($allowed as $value) { $allow .= "\\$value"; } } $mapping = array( 'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ő'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ű'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ő'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'ű'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r' ); if (is_array($string)) { $cleaned = array(); foreach ($string as $key => $clean) { $clean = strtr($clean, $mapping); } } else { $cleaned = strtr($string, $mapping); } return $cleaned; } /** * sanitize_string() * Convert html special chars * @param string $str * @return string */ function sanitize_string($str) { if(is_string($str)) $str = htmlspecialchars($str, ENT_COMPAT, CHARSET); return $str; } /** * strip_input() * Remove PHP and HTML code * @param string $str * @param string $exceptions * @return string */ function strip_input($str, $exceptions = false) { if(CURRENT_APP != 'admin') { $str = removeDomNodes($str, 'script'); $str = strip_tags($str, '