PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wall/php/functions.php

https://github.com/molokoloco/FRAMEWORK
PHP | 204 lines | 162 code | 15 blank | 27 comment | 26 complexity | 860d0db80b6e6aabd815b7406d25460c MD5 | raw file
  1. <?
  2. $local = (strpos($_SERVER['SERVER_ADDR'], '127.0.0.1') !== FALSE ? TRUE : FALSE);
  3. $WWW = ( $local ? 'http://localhost/FRAMEWORK/wall/' : 'http://www.b2bweb.fr/wall/' );
  4. $FEED = 'http://feeds.feedburner.com/b2bweb'; // http://www.google.com/reader/public/atom/user%2F11601043898330304613%2Fstate%2Fcom.google%2Fbroadcast
  5. function getFeed() {
  6. global $FEED;
  7. $feedUrl = '';
  8. //if (!empty($_GET['feed'])) $feedUrl = urldecode($_GET['feed']); // From form input index.php, HTML Form encoding
  9. if (!empty($_GET['url'])) $feedUrl = /*rawurldecode(*/$_GET['url']/*)*/; // From Bookmarklet, JS encodeURIComponent()
  10. if (empty($feedUrl) || !preg_match('/^https?:\/\/[A-Za-z0-9\-_%&\?\/.=:+ ]{5,}$/i', $feedUrl)) $feedUrl = $FEED;
  11. else {
  12. if (substr($feedUrl, -1, 1) == '#') $feedUrl = substr($feedUrl, 0, -1); // JQM
  13. $feedUrl = clean($feedUrl);
  14. }
  15. return $feedUrl;
  16. }
  17. // customPhpLibs... ---------------------------------------------
  18. function clean($string) {
  19. $bad = array('|</?\s*SCRIPT.*?>|si', '|</?\s*OBJECT.*?>|si', '|</?\s*META.*?>|si', '|</?\s*APPLET.*?>|si', '|</?\s*LINK.*?>|si', '|</?\s*FRAME.*?>|si', '|</?\s*IFRAME.*?>|si', '|</?\s*JAVASCRIPT.*?>|si', '|JAVASCRIPT:|si', '|</?\s*FORM.*?>|si', '|</?\s*INPUT.*?>|si', '|CHAR\(|si', '|INTO OUTFILE|si', '|LOAD DATA|si', '|EVAL\(|si', '|CONCAT\(|si');
  20. $string = preg_replace($bad, array(''), ' '.$string.' ');
  21. //$string = mysql_real_escape_string($string);
  22. $string = addslashes($string);
  23. $string = str_replace('\\n','\n',$string);
  24. $string = str_replace('\\r','\r',$string);
  25. return trim($string);
  26. }
  27. function cleanName($string) {
  28. if (empty($string)) return $string;
  29. elseif (is_numeric($string)) return $string;
  30. $string = strtolower(' '.clean($string).' ');
  31. $special = array('&', 'O', 'Z', '-', 'o', 'z', 'Y', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', '.', ' ', '+', '\'', '/');
  32. $normal = array('et', 'o', 'z', '-', 'o', 'z', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'd', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', '-', '-', '-', '-', '-');
  33. $string = str_replace($special, $normal, $string);
  34. $string = preg_replace('/[^a-z0-9_\-]/', '', $string);
  35. $string = preg_replace('/[\-]{2,}/', '-', $string);
  36. $string = preg_replace('/[\_]{2,}/', '_', $string);
  37. $string = substr($string, 1, -1);
  38. return $string;
  39. }
  40. function getExt($string) {
  41. $string = basename($string);
  42. if (strrpos($string,'.') === false) return '';
  43. $ext = strtolower(strrchr($string, '.'));
  44. $ext = substr($ext, 1);
  45. if ($ext == 'jpeg') $ext = 'jpg';
  46. if ($ext == 'mpeg') $ext = 'mpg';
  47. return $ext;
  48. }
  49. function makeName($path, $nb='60', $noExt=false) { // 070220155402_las-vegas-blvd.jpg
  50. list($filename) = explode('?', basename($path)); // some url param ?
  51. $ext = getExt($filename);
  52. $filename = substr(cleanName(preg_replace('|.'.$ext.'|si', '', $filename)), 0, $nb);
  53. return date(ymdHis).'_'.$filename.($noExt || empty($ext) ? '' : '.'.$ext);
  54. }
  55. function makeNameUrl($path, $nb='60', $noExt=false) { // XXXUrlHashXXX_las-vegas-blvd.jpg
  56. list($filename) = explode('?', basename($path)); // some url param ?
  57. $ext = getExt($filename);
  58. $filename = substr(cleanName(preg_replace('|.'.$ext.'|si', '', $filename)), 0, $nb);
  59. return $filename.'_'.md5($path).($noExt || empty($ext) ? '' : '.'.$ext);
  60. }
  61. function unHtmlEntities($string) {
  62. //return html_entity_decode(aff($string),ENT_COMPAT,'ISO-8859-15');
  63. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  64. $trans_tbl = array_flip($trans_tbl);
  65. return strtr($string, $trans_tbl);
  66. }
  67. function make_iso($str) {
  68. if (!$str) return;
  69. if (!function_exists('mb_detect_encoding')) return $str;
  70. $cod = mb_detect_encoding($str, 'UTF-8, ISO-8859-1');
  71. if ($cod == 'UTF-8') return utf8_decode($str);
  72. else return $str;
  73. }
  74. function make_utf($str) {
  75. if (!$str) return;
  76. if (!function_exists('mb_detect_encoding')) return $str;
  77. $cod = mb_detect_encoding($str, 'UTF-8, ISO-8859-1');
  78. if ($cod != 'UTF-8') return utf8_encode($str);
  79. else return $str;
  80. }
  81. function stripTags($string) {
  82. $string = preg_replace('/<br(.*?)>/i', ' ', ' '.$string.' '); // Retour a la ligne avec espace
  83. $string = str_replace('\\n', ' ', $string);
  84. $string = preg_replace('/<\/(pre|ul|li|p|table|tr)>/i', ' ', $string);
  85. $string = strip_tags($string);
  86. //$string = preg_replace('/([<br \/>]{2,})/', '<br />', $string);
  87. return trim($string);
  88. }
  89. function cs($string, $max=0, $ponct='...', $isHtml=true) {
  90. $string = stripTags($string);
  91. if (strlen($string) > $max) {
  92. $chaine = substr($string, 0, $max);
  93. $espace = strrpos($chaine, ' ');
  94. $string = substr($string, 0, $espace).$ponct;
  95. }
  96. if ($isHtml) $string = str_replace('\\n', '<br />', $string);
  97. return $string;
  98. /* Cf. my wordpress addon... (Dependency with WP force_balance_tags() )
  99. function improved_trim_excerpt($text) {
  100. global $post;
  101. $excerpt_length = 80;
  102. if (empty($text)) $text = apply_filters('the_content', get_the_content(''));
  103. if (empty($text)) return '';
  104. $text = strip_tags($text, '<br><p><a><quote><b><u><i><strong><cite><ul><li><ol><img><pre><code><xmp><blockquote><embed><object><h1><h2><h3><h4>');
  105. $words = explode(' ', $text, $excerpt_length + 1);
  106. if (count($words) > $excerpt_length) {
  107. array_pop($words);
  108. $text = implode(' ', $words);
  109. // Sick bug with link... cut in the middle ? trash patch... // Cf. formatting.php > force_balance_tags($text)
  110. $diff = count(explode('<a' , ' '.$text.' ')) - count(explode('</a>' , ' '.$text.' '));
  111. if ($diff > 0) $text .= '"></a>'; //.str_repeat('</a>', $diff - 1);
  112. $text .= ' [...] ';
  113. $text = force_balance_tags($text);
  114. }
  115. return $text;
  116. }
  117. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  118. add_filter('get_the_excerpt', 'improved_trim_excerpt');
  119. */
  120. }
  121. function jsonClean($string) {
  122. $bad = array('’', '“', '”');
  123. $better = array("'", '"', '"');
  124. $string = str_replace($bad, $better, $string);
  125. $string = stripslashes(make_iso($string));
  126. $string = str_replace($bad, $better, $string); // twice...
  127. $string = clean(stripTags(html_entity_decode(make_iso($string), ENT_QUOTES, 'ISO-8859-1')));
  128. $string = str_replace(chr(10), ' ',$string); // \n
  129. $string = str_replace(chr(13), '',$string); // \r
  130. $string = str_replace(chr(9), ' ',$string); // \t
  131. $string = preg_replace('/( ){2,}/', ' ', $string);
  132. $string = str_replace('"', '\"',stripslashes($string));
  133. return $string;
  134. }
  135. function array2json($arrItems) {
  136. $jsonStr = '';
  137. foreach($arrItems as $item) {
  138. $itemStr = '';
  139. foreach((array)$item as $att=>$val) $itemStr .= '"'.$att.'":"'.jsonClean($val).'",';
  140. $jsonStr .= '{'.substr($itemStr, 0, -1).'},';
  141. }
  142. return '['.substr($jsonStr, 0, -1).']';
  143. }
  144. /*function yahooYql($feedUrl) {
  145. $path = 'http://query.yahooapis.com/v1/public/yql?q=';
  146. $path .= urlencode("SELECT * FROM feed WHERE url='$feedUrl'");
  147. $path .= "&format=json";
  148. return file_get_contents($path, true);
  149. }*/
  150. function generateId($prefix='obj_') {
  151. static $idObjects = 0;
  152. if ($prefix != 'obj_') $prefix = cleanName($prefix);
  153. return $prefix.$idObjects++;
  154. }
  155. function js($script, $echo=TRUE) {
  156. $JS = '<script type="text/javascript">'.chr(13).chr(10).'// <![CDATA['.chr(13).chr(10);
  157. $JSE = chr(13).chr(10).'// ]]>'.chr(13).chr(10).'</script>';
  158. $js = $JS.chr(13).chr(10).$script.chr(13).chr(10).$JSE;
  159. if ($echo) echo $js;
  160. else return $js;
  161. }
  162. function db($var='') {
  163. $args = func_get_args();
  164. if (count($args) > 1) {
  165. foreach ($args as $arg) db($arg);
  166. return;
  167. }
  168. $t_id = generateId('db_');
  169. echo '<textarea id="'.$t_id.'" style="width:100%;height:250px;font:11px courier;color:#FFFFFF;background:#FF66CC;text-align:left;white-space:pre;padding:4px" rows="3" cols="7">';
  170. if (is_bool($var)) echo ($var ? 'TRUE' : 'FALSE');
  171. elseif ($var === '0' || $var === 0) echo $var;
  172. elseif (!empty($var)) var_export($var);
  173. else echo '*** No Value ***';
  174. echo '</textarea><br />';
  175. js("var lignes = document.getElementById('".$t_id."').value.split('\\n');
  176. document.getElementById('".$t_id."').style.height = (lignes.length*18+30)+'px';");
  177. }
  178. function d($var='<< PHP says that you killing him softly >>') {
  179. db($var);
  180. die();
  181. }
  182. ?>