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

/trunk/flatpress/fp-includes/core/core.system.php

https://bitbucket.org/alexandrul/flatpress
PHP | 234 lines | 125 code | 59 blank | 50 comment | 17 complexity | 914afffd3379ffb3b74f2257d4a46d06 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', '1.0');
  61. function system_ver() {
  62. return 'fp-' . SYSTEM_VER;
  63. }
  64. function system_ver_compare($newver, $oldver) {
  65. $nv_arr = explode('.', $newver);
  66. $ov_arr = explode('.', $oldver);
  67. $cn = count($nv_arr);
  68. $co = count($ov_arr);
  69. $max = min($cn, $co);
  70. // let's compare if one of the first version numbers differs
  71. // from new version, being greater
  72. for ($i=0; $i<$max; $i++) {
  73. if ( $nv_arr[ $i ] > $ov_arr[ $i ] ) { return 1; }
  74. if ( $nv_arr[ $i ] < $ov_arr[ $i ] ) { return 0; }
  75. }
  76. // if they equals, but still new version has more digits
  77. // then old-version is still outdated
  78. if ($cn > $co) return 1;
  79. }
  80. function system_generate_id($string) {
  81. return 'fp-'.dechex(crc32($string) ^ mt_rand());
  82. }
  83. function system_guessblogroot() {
  84. return substr($_SERVER['REQUEST_URI'], 0,strrpos($_SERVER['REQUEST_URI'],'/')+1);
  85. }
  86. function system_guessbaseurl() {
  87. return 'http://'.$_SERVER['HTTP_HOST']. BLOG_ROOT;
  88. }
  89. function system_getindex() {
  90. if (MOD_BLOG != INDEX)
  91. return MOD_BLOG;
  92. else
  93. return 'index.php';
  94. }
  95. function system_unregister_globals() {
  96. $v = @ini_get('register_globals');
  97. // on error we unregister anyway
  98. if ($v || is_null($v)) {
  99. foreach ($_REQUEST as $var => $val) {
  100. unset($GLOBALS[$var]);
  101. }
  102. }
  103. }
  104. function system_sanitizequery() {
  105. $err = false;
  106. foreach ($_GET as $k => $v) {
  107. if (preg_match('![<>]|://!', $v)) {
  108. $err = true;
  109. break;
  110. }
  111. }
  112. if ($err) {
  113. // @todo add log handler
  114. utils_redirect();
  115. }
  116. }
  117. function system_prepare_iis() {
  118. if (!@$_SERVER['REQUEST_URI']) {
  119. $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
  120. if (isset($_SERVER['QUERY_STRING'])) {
  121. $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
  122. }
  123. }
  124. }
  125. function system_init_action_params() {
  126. global $fp_params;
  127. $fp_params = array();
  128. if ($x = @$_GET['x'])
  129. $fp_params = utils_kexplode($x, ':;', false);
  130. $fp_params = array_merge($_GET, $fp_params);
  131. }
  132. function system_init() {
  133. system_sanitizequery();
  134. system_unregister_globals();
  135. system_prepare_iis();
  136. $GLOBALS['fpdb'] = new FPDB;
  137. $GLOBALS['fp_widgets'] = new widget_indexer;
  138. $GLOBALS['smarty'] =& $GLOBALS['_FP_SMARTY'];
  139. $smarty =& $GLOBALS['smarty'];
  140. $GLOBALS['fp_config'] = config_load();
  141. cookie_setup();
  142. sess_setup();
  143. user_loggedin();
  144. ob_start();
  145. $GLOBALS['theme'] = theme_loadsettings();
  146. $GLOBALS['lang'] = lang_load();
  147. plugin_loadall();
  148. // init smarty
  149. $smarty->compile_dir = CACHE_DIR;
  150. $smarty->cache_dir = SMARTY_DIR . 'cache/';
  151. $smarty->caching = 0;
  152. do_action('init');
  153. ob_end_clean();
  154. }
  155. function system_seterr($module, $val) {
  156. if ($module)
  157. $elem = 'success_'.$module;
  158. else
  159. $elem = 'success';
  160. sess_add($elem, $val);
  161. }
  162. function system_geterr($module='') {
  163. if ($module)
  164. $elem = 'success_'.$module;
  165. else
  166. $elem = 'success';
  167. return sess_remove($elem);
  168. }
  169. /* delayed print */
  170. function system_dpr($action, $content) {
  171. $p = print_r($content,1);
  172. $f = create_function('', "echo '<pre style=\'position:absolute\'>$p</pre>';");
  173. add_action($action, $f);
  174. }
  175. ?>