/web/helper/misc.php

https://github.com/hoveychen/Sicily · PHP · 83 lines · 53 code · 15 blank · 15 comment · 5 complexity · a5f9b85bef250315c6cd623efe875156 MD5 · raw file

  1. <?php
  2. /**
  3. * Get the global app config
  4. */
  5. function get_app_config() {
  6. global $app_config;
  7. return $app_config;
  8. }
  9. function cleanCookieHash() {
  10. setcookie("uid", "", time() - 3600, "/");
  11. setcookie("hash", "", time() - 3600, "/");
  12. setcookie(session_name(), "", time() - 3600, "/");
  13. }
  14. function error($message, $url="javascript:history.go(-1);") {
  15. ?>
  16. <div id="error_msg">
  17. <? echo $message; ?>
  18. [<a href="<? echo $url; ?>" >Back</a>]
  19. </div>
  20. <?
  21. die();
  22. }
  23. /**
  24. * Set locale
  25. * @param string $lang Optional for 'cn', 'en'
  26. * @param type $domain Default 'Sicily'
  27. */
  28. function set_language($lang = 'en', $domain = 'Sicily') {
  29. $availLang = array('cn' => 'zh_CN.utf8', 'en' => 'en_US.utf8');
  30. if (!array_key_exists($lang, $availLang))
  31. $lang = "en";
  32. $locale = $availLang[$lang];
  33. putenv('LANG=' . $locale);
  34. setlocale(LC_ALL, $locale);
  35. bindtextdomain($domain, dirname(__FILE__) . '/../locale/');
  36. textdomain($domain);
  37. bind_textdomain_codeset($domain, 'UTF-8');
  38. }
  39. /**
  40. * Output a binary file to client
  41. * No content should be output before this function
  42. * @param string $filename path of file to output
  43. * @param string $localname file name sent to client
  44. * $param bool $del_flag whether delete the file after output(Default TRUE)
  45. */
  46. function output_file($filename, $localname, $del_flag = TRUE) {
  47. header("Content-type: application/octet-stream");
  48. header("Content-Length: " . filesize($filename));
  49. header("Content-Disposition: attachment; filename=\"$localname\"");
  50. $fp = fopen("php://output", "w");
  51. $outfile = fopen($filename, "r");
  52. while (!feof($outfile)) {
  53. fwrite($fp, fread($outfile, 8 * 1024 * 1024));
  54. }
  55. fclose($fp);
  56. if ($del_flag) {
  57. unlink($filename);
  58. }
  59. die();
  60. }
  61. function array_select($needle, $haystackarray, $default) {
  62. if (in_array($needle, $haystackarray)) {
  63. return $needle;
  64. } else {
  65. return $default;
  66. }
  67. }
  68. ?>