PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/page_tools/misc_tools.php

https://github.com/floodyberry/carmack
PHP | 169 lines | 134 code | 32 blank | 3 comment | 3 complexity | a27d6bf286f222f8f914f793d8e02ddd MD5 | raw file
  1. <?
  2. function utime( ) {
  3. return (float )( vsprintf( '%d.%06d', gettimeofday( ) ) );
  4. }
  5. function date_to_string( $date ) {
  6. global $mon;
  7. $yr = substr( $date, 0, 4 );
  8. $mn = substr( $date, 4, 2 );
  9. $dy = substr( $date, 6, 2 );
  10. return ( $mon[ $mn ]." $dy, $yr" );
  11. }
  12. function datetime_to_string( $date ) {
  13. global $mon;
  14. // 0123456789012
  15. // YYYYMMDD HHMM
  16. $yr = substr( $date, 0, 4 );
  17. $mn = substr( $date, 4, 2 );
  18. $dy = substr( $date, 6, 2 );
  19. $hr = substr( $date, 9, 2 );
  20. $min = substr( $date, 11, 2 );
  21. return ( $mon[ $mn ]." $dy, $yr - $hr:$min" );
  22. }
  23. function datetime_to_timestring( $date ) {
  24. $hr = substr( $date, 9, 2 );
  25. $min = substr( $date, 11, 2 );
  26. return ( "$hr:$min" );
  27. }
  28. function mon_to_fullstring( $m ) {
  29. global $mon_full;
  30. if ( strlen( $m ) == 1 )
  31. $m = "0$m";
  32. return ( $mon_full[ $m ] );
  33. }
  34. function reset_timeline( ) {
  35. global $conf, $timeline;
  36. $timeline = Array( );
  37. unlink( "../timeline/".$conf->user."_timeline.txt" );
  38. }
  39. function add_to_timeline( $timestamp, $type, $title, $url ) {
  40. global $conf, $timeline;
  41. $utime = utime( );
  42. $time_since = ( $utime - $conf->last_utime );
  43. $conf->last_utime = $utime;
  44. echo "$time_since [$type]: $timestamp - $title<br />\n";
  45. $timeline[ $timestamp ][] = Array( "type"=>$type, "url"=>$url, "title"=>$title );
  46. }
  47. function write_timeline( ) {
  48. global $conf, $timeline;
  49. $timeline_file = "../timeline/".$conf->user."_timeline.txt";
  50. $fp = fopen( $timeline_file, "w" );
  51. ksort( $timeline );
  52. foreach( $timeline as $timestamp => $link_array ) {
  53. // $date_string = date_to_string( $timestamp );
  54. foreach( $link_array as $idx => $link_info ) {
  55. fputs( $fp, "$timestamp, $link_info[title] ($link_info[type]), $link_info[url]\n" );
  56. }
  57. }
  58. fclose( $fp );
  59. chmod( $timeline_file, 0777 );
  60. }
  61. function read_file( $file ) {
  62. $fp = fopen( $file, "r" );
  63. $text = fread( $fp, filesize( $file ) );
  64. fclose( $fp );
  65. return ( $text );
  66. }
  67. function write_file( $file, $text ) {
  68. $fp = fopen( $file, "w+" );
  69. $text = fwrite( $fp, $text );
  70. fclose( $fp );
  71. chmod( $file, 0777 );
  72. }
  73. function write_template( $template, $keywords="", $title="", $extra=array() ) {
  74. global $conf;
  75. $in = array();
  76. $out = array();
  77. foreach( $extra as $find=>$replace ) {
  78. $in[] = $find;
  79. $out[] = $replace;
  80. }
  81. print_r($extra);
  82. $page = str_replace(
  83. array( "@keywords", "@title" ),
  84. array( $conf->keywords.$keywords, $conf->title.$title ),
  85. $conf->header );
  86. $page .= str_replace(
  87. $in,
  88. $out,
  89. read_file( "template/".$template ) );
  90. $page .= $conf->footer;
  91. write_file( "../".$template, $page );
  92. }
  93. function human_readable_size($size) {
  94. $mod = 1024;
  95. $units = explode(' ','b kb mb gb tb pb');
  96. for ($i = 0; $size > $mod; $i++)
  97. $size /= $mod;
  98. return round($size, 2).$units[$i];
  99. }
  100. $mon[ "Jan" ] = "01";
  101. $mon[ "Feb" ] = "02";
  102. $mon[ "Mar" ] = "03";
  103. $mon[ "Apr" ] = "04";
  104. $mon[ "May" ] = "05";
  105. $mon[ "Jun" ] = "06";
  106. $mon[ "Jul" ] = "07";
  107. $mon[ "Aug" ] = "08";
  108. $mon[ "Sep" ] = "09";
  109. $mon[ "Oct" ] = "10";
  110. $mon[ "Nov" ] = "11";
  111. $mon[ "Dec" ] = "12";
  112. $mon[ "01" ] = "Jan";
  113. $mon[ "02" ] = "Feb";
  114. $mon[ "03" ] = "Mar";
  115. $mon[ "04" ] = "Apr";
  116. $mon[ "05" ] = "May";
  117. $mon[ "06" ] = "Jun";
  118. $mon[ "07" ] = "Jul";
  119. $mon[ "08" ] = "Aug";
  120. $mon[ "09" ] = "Sep";
  121. $mon[ "10" ] = "Oct";
  122. $mon[ "11" ] = "Nov";
  123. $mon[ "12" ] = "Dec";
  124. $mon_full[ "01" ] = "January";
  125. $mon_full[ "02" ] = "February";
  126. $mon_full[ "03" ] = "March";
  127. $mon_full[ "04" ] = "April";
  128. $mon_full[ "05" ] = "May";
  129. $mon_full[ "06" ] = "June";
  130. $mon_full[ "07" ] = "July";
  131. $mon_full[ "08" ] = "August";
  132. $mon_full[ "09" ] = "September";
  133. $mon_full[ "10" ] = "October";
  134. $mon_full[ "11" ] = "November";
  135. $mon_full[ "12" ] = "December";
  136. require_once( "conf.php" );
  137. ?>