PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/osCommerce/OM/Core/Site/Admin/includes/functions/general.php

https://github.com/azouts/oscommerce
PHP | 321 lines | 192 code | 52 blank | 77 comment | 65 complexity | f086cff75cb75130d4802f2b800c5c1e MD5 | raw file
  1. <?php
  2. /*
  3. osCommerce Online Merchant $osCommerce-SIG$
  4. Copyright (c) 2010 osCommerce (http://www.oscommerce.com)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License v2 (1991)
  7. as published by the Free Software Foundation.
  8. */
  9. use osCommerce\OM\Core\OSCOM;
  10. /**
  11. * Wrapper function for set_time_limit(), which can't be used in safe_mode
  12. *
  13. * @param int $limit The limit to set the maximium execution time to
  14. * @access public
  15. */
  16. function osc_set_time_limit($limit) {
  17. if (!get_cfg_var('safe_mode')) {
  18. set_time_limit($limit);
  19. }
  20. }
  21. /**
  22. * Redirect to a URL address
  23. *
  24. * @param string $url The URL address to redirect to
  25. * @access public
  26. */
  27. function osc_redirect_admin($url) {
  28. if ( (strpos($url, "\n") !== false) || (strpos($url, "\r") !== false) ) {
  29. $url = osc_href_link_admin(FILENAME_DEFAULT);
  30. }
  31. if (strpos($url, '&amp;') !== false) {
  32. $url = str_replace('&amp;', '&', $url);
  33. }
  34. header('Location: ' . $url);
  35. exit;
  36. }
  37. /**
  38. * Parse file permissions to a human readable layout
  39. *
  40. * @param int $mode The file permission to parse
  41. * @access public
  42. */
  43. function osc_get_file_permissions($mode) {
  44. // determine type
  45. if ( ($mode & 0xC000) == 0xC000) { // unix domain socket
  46. $type = 's';
  47. } elseif ( ($mode & 0x4000) == 0x4000) { // directory
  48. $type = 'd';
  49. } elseif ( ($mode & 0xA000) == 0xA000) { // symbolic link
  50. $type = 'l';
  51. } elseif ( ($mode & 0x8000) == 0x8000) { // regular file
  52. $type = '-';
  53. } elseif ( ($mode & 0x6000) == 0x6000) { //bBlock special file
  54. $type = 'b';
  55. } elseif ( ($mode & 0x2000) == 0x2000) { // character special file
  56. $type = 'c';
  57. } elseif ( ($mode & 0x1000) == 0x1000) { // named pipe
  58. $type = 'p';
  59. } else { // unknown
  60. $type = '?';
  61. }
  62. // determine permissions
  63. $owner['read'] = ($mode & 00400) ? 'r' : '-';
  64. $owner['write'] = ($mode & 00200) ? 'w' : '-';
  65. $owner['execute'] = ($mode & 00100) ? 'x' : '-';
  66. $group['read'] = ($mode & 00040) ? 'r' : '-';
  67. $group['write'] = ($mode & 00020) ? 'w' : '-';
  68. $group['execute'] = ($mode & 00010) ? 'x' : '-';
  69. $world['read'] = ($mode & 00004) ? 'r' : '-';
  70. $world['write'] = ($mode & 00002) ? 'w' : '-';
  71. $world['execute'] = ($mode & 00001) ? 'x' : '-';
  72. // adjust for SUID, SGID and sticky bit
  73. if ($mode & 0x800 ) $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
  74. if ($mode & 0x400 ) $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
  75. if ($mode & 0x200 ) $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
  76. return $type .
  77. $owner['read'] . $owner['write'] . $owner['execute'] .
  78. $group['read'] . $group['write'] . $group['execute'] .
  79. $world['read'] . $world['write'] . $world['execute'];
  80. }
  81. /*
  82. * Recursively remove a directory or a single file
  83. *
  84. * @param string $source The source to remove
  85. * @access public
  86. */
  87. function osc_remove($source) {
  88. global $osC_Language, $osC_MessageStack;
  89. if (is_dir($source)) {
  90. $dir = dir($source);
  91. while ($file = $dir->read()) {
  92. if ( ($file != '.') && ($file != '..') ) {
  93. if (is_writeable($source . '/' . $file)) {
  94. osc_remove($source . '/' . $file);
  95. } else {
  96. $osC_MessageStack->add('header', sprintf($osC_Language->get('ms_error_file_not_removable'), $source . '/' . $file), 'error');
  97. }
  98. }
  99. }
  100. $dir->close();
  101. if (is_writeable($source)) {
  102. return rmdir($source);
  103. } else {
  104. $osC_MessageStack->add('header', sprintf($osC_Language->get('ms_error_directory_not_removable'), $source), 'error');
  105. }
  106. } else {
  107. if (is_writeable($source)) {
  108. return unlink($source);
  109. } else {
  110. $osC_MessageStack->add('header', sprintf($osC_Language->get('ms_error_file_not_removable'), $source), 'error');
  111. }
  112. }
  113. }
  114. /**
  115. * Return an image type that the server supports
  116. *
  117. * @access public
  118. */
  119. function osc_dynamic_image_extension() {
  120. static $extension;
  121. if (!isset($extension)) {
  122. if (function_exists('imagetypes')) {
  123. if (imagetypes() & IMG_PNG) {
  124. $extension = 'png';
  125. } elseif (imagetypes() & IMG_JPG) {
  126. $extension = 'jpeg';
  127. } elseif (imagetypes() & IMG_GIF) {
  128. $extension = 'gif';
  129. }
  130. } elseif (function_exists('imagepng')) {
  131. $extension = 'png';
  132. } elseif (function_exists('imagejpeg')) {
  133. $extension = 'jpeg';
  134. } elseif (function_exists('imagegif')) {
  135. $extension = 'gif';
  136. }
  137. }
  138. return $extension;
  139. }
  140. /**
  141. * Parse a category path to avoid loops with duplicate values
  142. *
  143. * @param string $cPath The category path to parse
  144. * @access public
  145. */
  146. function osc_parse_category_path($cPath) {
  147. // make sure the category IDs are integers
  148. $cPath_array = array_map('intval', explode('_', $cPath));
  149. // make sure no duplicate category IDs exist which could lock the server in a loop
  150. $tmp_array = array();
  151. $n = sizeof($cPath_array);
  152. for ($i=0; $i<$n; $i++) {
  153. if (!in_array($cPath_array[$i], $tmp_array)) {
  154. $tmp_array[] = $cPath_array[$i];
  155. }
  156. }
  157. return $tmp_array;
  158. }
  159. /**
  160. * Return an array as a string value
  161. *
  162. * @param array $array The array to return as a string value
  163. * @param array $exclude An array of parameters to exclude from the string
  164. * @param string $equals The equals character to symbolize what value a parameter is defined to
  165. * @param string $separator The separate to use between parameters
  166. */
  167. function osc_array_to_string($array, $exclude = '', $equals = '=', $separator = '&') {
  168. if (!is_array($exclude)) $exclude = array();
  169. $get_string = '';
  170. if (sizeof($array) > 0) {
  171. while (list($key, $value) = each($array)) {
  172. if ( (!in_array($key, $exclude)) && ($key != 'x') && ($key != 'y') ) {
  173. $get_string .= $key . $equals . $value . $separator;
  174. }
  175. }
  176. $remove_chars = strlen($separator);
  177. $get_string = substr($get_string, 0, -$remove_chars);
  178. }
  179. return $get_string;
  180. }
  181. /**
  182. * Return a variable value from a serialized string
  183. *
  184. * @param string $serialization_data The serialized string to return values from
  185. * @param string $variable_name The variable to return
  186. * @param string $variable_type The variable type
  187. */
  188. function osc_get_serialized_variable(&$serialization_data, $variable_name, $variable_type = 'string') {
  189. $serialized_variable = '';
  190. switch ($variable_type) {
  191. case 'string':
  192. $start_position = strpos($serialization_data, $variable_name . '|s');
  193. $serialized_variable = substr($serialization_data, strpos($serialization_data, '|', $start_position) + 1, strpos($serialization_data, '|', $start_position) - 1);
  194. break;
  195. case 'array':
  196. case 'object':
  197. if ($variable_type == 'array') {
  198. $start_position = strpos($serialization_data, $variable_name . '|a');
  199. } else {
  200. $start_position = strpos($serialization_data, $variable_name . '|O');
  201. }
  202. $tag = 0;
  203. for ($i=$start_position, $n=sizeof($serialization_data); $i<$n; $i++) {
  204. if ($serialization_data[$i] == '{') {
  205. $tag++;
  206. } elseif ($serialization_data[$i] == '}') {
  207. $tag--;
  208. } elseif ($tag < 1) {
  209. break;
  210. }
  211. }
  212. $serialized_variable = substr($serialization_data, strpos($serialization_data, '|', $start_position) + 1, $i - strpos($serialization_data, '|', $start_position) - 1);
  213. break;
  214. }
  215. return $serialized_variable;
  216. }
  217. /**
  218. * Call a function given in string format used by configuration set and use functions
  219. *
  220. * @param string $function The complete function to call
  221. * @param string $default The default value to pass to the function
  222. * @param string $key The key value to use for the input field
  223. */
  224. function osc_call_user_func($function, $default = null, $key = null) {
  225. if (strpos($function, '::') !== false) {
  226. $class_method = explode('::', $function);
  227. return call_user_func(array($class_method[0], $class_method[1]), $default, $key);
  228. } else {
  229. $function_name = $function;
  230. $function_parameter = '';
  231. if (strpos($function, '(') !== false) {
  232. $function_array = explode('(', $function, 2);
  233. $function_name = $function_array[0];
  234. $function_parameter = substr($function_array[1], 0, -1);
  235. }
  236. if (!function_exists($function_name)) {
  237. include(OSCOM::BASE_DIRECTORY . 'Core/Site/Admin/includes/functions/cfg_parameters/' . $function_name . '.php');
  238. }
  239. if (!empty($function_parameter)) {
  240. return call_user_func($function_name, $function_parameter, $default, $key);
  241. } else {
  242. return call_user_func($function_name, $default, $key);
  243. }
  244. }
  245. }
  246. /**
  247. * Validate a plain text password against an encrypted value
  248. *
  249. * @param string $plain The plain text password
  250. * @param string $encrypted The encrypted password to validate against
  251. */
  252. function osc_validate_password($plain, $encrypted) {
  253. if (!empty($plain) && !empty($encrypted)) {
  254. // split apart the hash / salt
  255. $stack = explode(':', $encrypted);
  256. if (sizeof($stack) != 2) {
  257. return false;
  258. }
  259. if (md5($stack[1] . $plain) == $stack[0]) {
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. function osc_toObjectInfo($array) {
  266. return new osCommerce\OM\Core\ObjectInfo($array);
  267. }
  268. ?>