functions.core.tfb.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /* $Id: functions.core.tfb.php 2853 2007-04-10 18:58:17Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * get Request Var
  17. *
  18. * @param $varName
  19. * @return string
  20. */
  21. function tfb_getRequestVar($varName) {
  22. $return = "";
  23. if(array_key_exists($varName, $_REQUEST)){
  24. // If magic quoting on, strip magic quotes:
  25. /**
  26. * TODO:
  27. * Codebase needs auditing to remove any unneeded stripslashes
  28. * calls before uncommenting this. Also using this really means
  29. * checking any addslashes() calls to see if they're really needed
  30. * when magic quotes is on.
  31. if(ini_get('magic_quotes_gpc')){
  32. tfb_strip_quotes($_REQUEST[$varName]);
  33. }
  34. */
  35. $return = htmlentities(trim($_REQUEST[$varName]), ENT_QUOTES);
  36. }
  37. return $return;
  38. }
  39. /**
  40. * Get Request Var, with no quoting or escaping (i.e. if
  41. * active on server, PHP's magic quoting is removed).
  42. *
  43. * Be careful what you do with the return value: it must not be output in HTML
  44. * without going thru htmlspecialchars, in a shell command without going thru
  45. * tfb_shellencode, in a DB without going thru addslashes or similar, ...
  46. *
  47. * @param $varName
  48. * @return string
  49. */
  50. function tfb_getRequestVarRaw($varName) {
  51. // Note: CANNOT use tfb_strip_quotes directly on $_REQUEST
  52. // here, because it works in-place, i.e. would break other
  53. // future uses of tfb_getRequestVarRaw on the same variables.
  54. $return = '';
  55. if (array_key_exists($varName, $_REQUEST)){
  56. $return = $_REQUEST[$varName];
  57. // Seems get_magic_quotes_gpc is deprecated
  58. // in PHP 6, use ini_get instead.
  59. if (ini_get('magic_quotes_gpc'))
  60. tfb_strip_quotes($return);
  61. }
  62. return $return;
  63. }
  64. /**
  65. * check if path is valid
  66. *
  67. * @param $path
  68. * @param $ext
  69. * @return boolean
  70. */
  71. function tfb_isValidPath($path, $ext = "") {
  72. if (preg_match("/\\\/", $path)) return false;
  73. if (preg_match("/\.\.\//", $path)) return false;
  74. if ($ext != "") {
  75. $extLength = strlen($ext);
  76. if (strlen($path) < $extLength) return false;
  77. if ((strtolower(substr($path, -($extLength)))) !== strtolower($ext)) return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * check if transfer is valid
  83. *
  84. * @param $transfer
  85. * @return boolean
  86. */
  87. function tfb_isValidTransfer($transfer) {
  88. global $cfg;
  89. return (preg_match('/^[0-9a-zA-Z._-]+('.$cfg["file_types_regexp"].')$/D', $transfer) == 1);
  90. }
  91. /**
  92. * clean file-name, validate extension and make it lower-case
  93. *
  94. * @param $inName
  95. * @return string or false
  96. */
  97. function tfb_cleanFileName($inName) {
  98. global $cfg;
  99. $outName = preg_replace("/[^0-9a-zA-Z.-]+/",'_', $inName);
  100. $stringLength = strlen($outName);
  101. foreach ($cfg['file_types_array'] as $ftype) {
  102. $extLength = strlen($ftype);
  103. $extIndex = 0 - $extLength;
  104. if (($stringLength > $extLength) && (strtolower(substr($outName, $extIndex)) === ($ftype)))
  105. return substr($outName, 0, $extIndex).$ftype;
  106. }
  107. return false;
  108. }
  109. /**
  110. * get name of transfer. name cleaned and extension removed.
  111. *
  112. * @param $transfer
  113. * @return string
  114. */
  115. function tfb_cleanTransferName($transfer) {
  116. global $cfg;
  117. return str_replace($cfg["file_types_array"], "", preg_replace("/[^0-9a-zA-Z.-]+/",'_', $transfer));
  118. }
  119. /**
  120. * split on the "*" coming from Varchar URL
  121. *
  122. * @param $url
  123. * @return string
  124. */
  125. function tfb_cleanURL($url) {
  126. $arURL = explode("*", $url);
  127. return ((is_array($arURL)) && (count($arURL)) > 1) ? $arURL[1] : $url;
  128. }
  129. /**
  130. * Avoid magic_quotes_gpc issues
  131. * courtesy of iliaa@php.net
  132. * @param ref &$var reference to a $_REQUEST variable
  133. * @return null
  134. */
  135. function tfb_strip_quotes(&$var){
  136. if (is_array($var)) {
  137. foreach ($var as $k => $v) {
  138. if (is_array($v))
  139. array_walk($var[$k], 'tfb_strip_quotes');
  140. else
  141. $var[$k] = stripslashes($v);
  142. }
  143. } else {
  144. $var = stripslashes($var);
  145. }
  146. }
  147. /**
  148. * HTML-encode a string.
  149. *
  150. * @param $str
  151. * @return string
  152. */
  153. function tfb_htmlencode($str) {
  154. return htmlspecialchars($str, ENT_QUOTES);
  155. }
  156. /**
  157. * HTML-encode a string, transforming spaces into '&nbsp;'.
  158. * Should be used on strings that might contain multiple spaces
  159. * (names, paths & filenames, ...), unless string will be output:
  160. * - in an HTML attribute,
  161. * - in a <pre> element,
  162. * since both of those do not ignore multiple spaces (in that
  163. * case, tfb_htmlencode is enough).
  164. *
  165. * @param $str
  166. * @return string
  167. */
  168. function tfb_htmlencodekeepspaces($str) {
  169. return str_replace(' ', '&nbsp;', htmlspecialchars($str, ENT_QUOTES));
  170. }
  171. /**
  172. * Shell-escape a string. The argument must be one whole (and only one) arg
  173. * (this function adds quotes around it so that the shell sees it as such).
  174. *
  175. * @param $str
  176. * @return string
  177. */
  178. function tfb_shellencode($str) {
  179. $str = (string)$str;
  180. return isset($str) && strlen($str) > 0 ? escapeshellarg($str) : "''";
  181. }
  182. ?>