functions.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. function getSetup($key = null, $default_value = null) {
  3. $arr = parse_ini_file('setup.ini');
  4. return isset($key) ? (isset($arr[$key])? trim($arr[$key]) : $default_value) : $arr;
  5. }
  6. function get_msg_file_path($saloon)
  7. {
  8. return dirname( __FILE__ ) .'/history/' . sanitize_file_name('msg_' . $saloon . '.txt');
  9. }
  10. function get_session_file_path($saloon)
  11. {
  12. //return dirname( __FILE__ ) .'/history/' . sanitize_file_name('sessions_' . $saloon . '.txt');
  13. // all saloons
  14. return dirname( __FILE__ ) .'/history/' . sanitize_file_name('sessions.txt');
  15. }
  16. function get_file_to_lock($file_path)
  17. {
  18. $hFile = @fopen($file_path . '.lock', 'rb');
  19. if(!$hFile)
  20. {
  21. file_put_contents($file_path . '.lock', '');
  22. $hFile = @fopen($file_path . '.lock', 'rb');
  23. }
  24. return $hFile;
  25. }
  26. // read file with share lock
  27. function file_lock_and_get_json($file_path)
  28. {
  29. $hFile = get_file_to_lock($file_path);
  30. @flock($hFile, LOCK_SH);
  31. $contents = @file_get_contents($file_path);
  32. @flock($hFile, LOCK_UN);
  33. fclose($hFile);
  34. $json = json_decode($contents, true);
  35. return $json;
  36. }
  37. // destroy file and lock
  38. function file_lock_and_destroy($file_path)
  39. {
  40. $hFile = get_file_to_lock($file_path);
  41. @flock($hFile, LOCK_SH);
  42. @unlink($file_path);
  43. @flock($hFile, LOCK_UN);
  44. fclose($hFile);
  45. @unlink(get_file_to_lock($file_path));
  46. return true;
  47. }
  48. function json_encode_for_save($data)
  49. {
  50. $json_pretty_option = getSetup('pretty_json', false);
  51. $json_options = $json_pretty_option ? JSON_PRETTY_PRINT : 0;
  52. return json_encode($data, $json_options);
  53. }
  54. // append message with exclusive lock
  55. function file_lock_and_append_msg($file_path, $msg)
  56. {
  57. $hFile = get_file_to_lock($file_path);
  58. @flock($hFile, LOCK_EX);
  59. // read entire file
  60. $string = @file_get_contents($file_path);
  61. $json_msgs = json_decode($string, true);
  62. // append message
  63. $json_msgs['msgs'][] = $msg;
  64. file_put_contents($file_path, json_encode_for_save($json_msgs));
  65. @flock($hFile, LOCK_UN);
  66. fclose($hFile);
  67. return true;
  68. }
  69. function file_lock_and_update_session($file_path, $session)
  70. {
  71. $hFile = get_file_to_lock($file_path);
  72. @flock($hFile, LOCK_EX);
  73. // read entire file
  74. $string = @file_get_contents($file_path);
  75. $json_sessions = json_decode($string, true);
  76. // update our session
  77. $json_sessions[$session['session_id']] = $session;
  78. $json_sessions = update_sessions_status($json_sessions);
  79. file_put_contents($file_path, json_encode_for_save($json_sessions));
  80. @flock($hFile, LOCK_UN);
  81. fclose($hFile);
  82. return true;
  83. }
  84. // mark idle sessions and delete old ones
  85. function update_sessions_status($json_sessions)
  86. {
  87. if(!$json_sessions) return array();
  88. $delete_ids = array();
  89. foreach ($json_sessions as $session_id => $session)
  90. {
  91. $delta_second = time() - $session['last_update'];
  92. $session['idle'] = ($delta_second > getSetup('warning_idle_session_seconds', 20))?'true':'false';
  93. if($delta_second > getSetup('max_idle_session_seconds', 5*60))
  94. {
  95. $delete_ids [] = $session_id;
  96. }
  97. }
  98. foreach($delete_ids as $delete_id)
  99. {
  100. unset($json_sessions[$delete_id]);
  101. }
  102. return $json_sessions;
  103. }
  104. function stripslashes_deep($value)
  105. {
  106. $value = is_array($value) ?
  107. array_map('stripslashes_deep', $value) :
  108. stripslashes($value);
  109. return $value;
  110. }
  111. // $_POST value, dealing with magic quotes
  112. function get_post_var($name)
  113. {
  114. $value = $_POST[$name];
  115. if (get_magic_quotes_gpc())
  116. {
  117. $value = stripslashes_deep($value);
  118. }
  119. return $value;
  120. }
  121. /**
  122. * Sanitizes a filename, replacing whitespace with dashes.
  123. *
  124. * Removes special characters that are illegal in filenames on certain
  125. * operating systems and special characters requiring special escaping
  126. * to manipulate at the command line. Replaces spaces and consecutive
  127. * dashes with a single dash. Trims period, dash and underscore from beginning
  128. * and end of filename. It is not guaranteed that this function will return a
  129. * filename that is allowed to be uploaded.
  130. *
  131. * @since 2.1.0
  132. *
  133. * @param string $filename The filename to be sanitized
  134. * @return string The sanitized filename
  135. */
  136. function sanitize_file_name( $filename )
  137. {
  138. $filename_raw = $filename;
  139. $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
  140. /**
  141. * Filters the list of characters to remove from a filename.
  142. *
  143. * @since 2.8.0
  144. *
  145. * @param array $special_chars Characters to remove.
  146. * @param string $filename_raw Filename as it was passed into sanitize_file_name().
  147. */
  148. //$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
  149. $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
  150. $filename = str_replace( $special_chars, '', $filename );
  151. $filename = str_replace( array( '%20', '+' ), '-', $filename );
  152. $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
  153. $filename = trim( $filename, '.-_' );
  154. // if ( false === strpos( $filename, '.' ) ) {
  155. // $mime_types = wp_get_mime_types();
  156. // $filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
  157. // if ( $filetype['ext'] === $filename ) {
  158. // $filename = 'unnamed-file.' . $filetype['ext'];
  159. // }
  160. // }
  161. return $filename;
  162. // Split the filename into a base and extension[s]
  163. $parts = explode('.', $filename);
  164. // Return if only one extension
  165. if ( count( $parts ) <= 2 ) {
  166. /**
  167. * Filters a sanitized filename string.
  168. *
  169. * @since 2.8.0
  170. *
  171. * @param string $filename Sanitized filename.
  172. * @param string $filename_raw The filename prior to sanitization.
  173. */
  174. // return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
  175. }
  176. // Process multiple extensions
  177. $filename = array_shift($parts);
  178. $extension = array_pop($parts);
  179. $mimes = get_allowed_mime_types();
  180. /*
  181. * Loop over any intermediate extensions. Postfix them with a trailing underscore
  182. * if they are a 2 - 5 character long alpha string not in the extension whitelist.
  183. */
  184. foreach ( (array) $parts as $part) {
  185. $filename .= '.' . $part;
  186. if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
  187. $allowed = false;
  188. foreach ( $mimes as $ext_preg => $mime_match ) {
  189. $ext_preg = '!^(' . $ext_preg . ')$!i';
  190. if ( preg_match( $ext_preg, $part ) ) {
  191. $allowed = true;
  192. break;
  193. }
  194. }
  195. if ( !$allowed )
  196. $filename .= '_';
  197. }
  198. }
  199. $filename .= '.' . $extension;
  200. /** This filter is documented in wp-includes/formatting.php */
  201. return apply_filters('sanitize_file_name', $filename, $filename_raw);
  202. }
  203. ?>