PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/session-fix/flatpress/fp-includes/core/core.system.php

https://bitbucket.org/alexandrul/flatpress
PHP | 186 lines | 95 code | 45 blank | 46 comment | 10 complexity | eac36abb0abb26ff02f724ffd9eb9a6b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MIT
  1. <?php
  2. /**
  3. * system.php
  4. * string-to-php and general system functions
  5. */
  6. /**
  7. * function system_save
  8. *
  9. * This function saves a list of variables provided after $file
  10. * encapsulated in an array where KEY is the var name
  11. * in a php file.
  12. *
  13. * Example usage:
  14. * <code>
  15. * <?php
  16. * // Let's suppose you want to save an array called $my_arr
  17. * // in file $my_file
  18. * $my_file = 'path/to/file'
  19. * $my_arr = array ('val1', 'val2', 'val3');
  20. * $save_arr = array('$my_arr' => $my_arr); //same as: $save_arr['$my_arr'] = $my_arr);
  21. * system_save($my_file, $my_arr);
  22. * // now the file $my_file will contain the following lines:
  23. * // global $my_arr;
  24. * // $my_arr = array (
  25. * // '$my_arr' => val1',
  26. * // '$my_arr' => 'val2',
  27. * // '$my_arr' => 'val3'
  28. * // );
  29. * ?>
  30. * </code>
  31. *
  32. * @param string $file file path where $array contents will be saved
  33. * @array $var_list list of vars to be saved
  34. * @return bool
  35. *
  36. * @see config_save, config_load
  37. *
  38. */
  39. function system_save($file, $array ) {
  40. //if ( ( $numargs = func_num_args() ) > 1) {
  41. $string = "<?php\n\n";
  42. //$arg_list = func_get_args();
  43. foreach ($array as $key => $arg) {
  44. //$vname = utils_vname ($arg);
  45. //var_export($arg);
  46. $s = /*" global {$key};\n*/ "\${$key} = " .
  47. var_export($arg, true) . ";\n";
  48. $string .= $s;
  49. }
  50. $string .= "\n?>";
  51. return io_write_file($file, $string);
  52. //} else die('Wrong number of parameters!');
  53. }
  54. function system_hashsalt_save($force=false) {
  55. global $fp_config;
  56. if ($force || !file_exists(HASHSALT_FILE))
  57. return system_save(HASHSALT_FILE, array('fp_hashsalt'=>$fp_config['general']['blogid'] . ABS_PATH . BLOG_BASEURL .mt_rand()));
  58. return true;
  59. }
  60. define('SYSTEM_VER', '0.803');
  61. function system_ver() {
  62. return 'fp-' . SYSTEM_VER;
  63. }
  64. function system_generate_id($string) {
  65. return 'fp-'.dechex(crc32($string) ^ mt_rand());
  66. }
  67. function system_guessblogroot() {
  68. return substr($_SERVER['REQUEST_URI'], 0,strrpos($_SERVER['REQUEST_URI'],'/')+1);
  69. }
  70. function system_guessbaseurl() {
  71. return 'http://'.$_SERVER['HTTP_HOST']. BLOG_ROOT;
  72. }
  73. function system_getindex() {
  74. if (MOD_BLOG != INDEX)
  75. return MOD_BLOG;
  76. else
  77. return 'index.php';
  78. }
  79. function system_unregister_globals() {
  80. $v = @ini_get('register_globals');
  81. // on error we unregister anyway
  82. if ($v || is_null($v)) {
  83. foreach ($_REQUEST as $var => $val) {
  84. unset($GLOBALS[$var]);
  85. }
  86. }
  87. }
  88. function system_sanitizequery() {
  89. $err = false;
  90. foreach ($_GET as $k => $v) {
  91. if (preg_match('![<>]|://!', $v)) {
  92. $err = true;
  93. break;
  94. }
  95. }
  96. if ($err) {
  97. // @todo add log handler
  98. utils_redirect();
  99. }
  100. }
  101. function system_init() {
  102. system_sanitizequery();
  103. system_unregister_globals();
  104. $GLOBALS['fpdb'] =& new FPDB;
  105. $GLOBALS['fp_widgets'] =& new widget_indexer;
  106. $GLOBALS['smarty'] =& $GLOBALS['_FP_SMARTY'];
  107. $smarty =& $GLOBALS['smarty'];
  108. $GLOBALS['fp_config'] =& config_load();
  109. $GLOBALS['theme'] =& theme_loadsettings();
  110. $GLOBALS['lang'] =& lang_load();
  111. cookie_setup();
  112. sess_setup();
  113. user_loggedin();
  114. plugin_loadall();
  115. // init smarty
  116. $smarty->compile_dir = CACHE_DIR;
  117. $smarty->cache_dir = SMARTY_DIR . 'cache/';
  118. $smarty->caching = 0;
  119. do_action('init');
  120. }
  121. function system_seterr($module, $val) {
  122. if ($module)
  123. $elem = 'success_'.$module;
  124. else
  125. $elem = 'success';
  126. sess_add($elem, $val);
  127. }
  128. function system_geterr($module='') {
  129. if ($module)
  130. $elem = 'success_'.$module;
  131. else
  132. $elem = 'success';
  133. return sess_remove($elem);
  134. }
  135. /* delayed print */
  136. function system_dpr($action, $content) {
  137. $p = print_r($content,1);
  138. $f = create_function('', "echo '<pre style=\'position:absolute\'>$p</pre>';");
  139. add_action($action, $f);
  140. }
  141. ?>