Newer
Older
DirtyScripts / RS / ExcelCal / xls2ical.php
root on 21 Apr 2022 3 KB xls2ical
  1. <?php
  2.  
  3. /***
  4. * CONFIGURATION - SET THESE
  5. */
  6.  
  7. $folder = "/mnt/hgfs/PentestOS/RootShell/cal_xls/"; // the path to the folder containing the XLS files dump
  8. $user = "Ross Marks"; // name in column A to search for
  9.  
  10.  
  11. /***
  12. * MAIN PROGRAM DO NOT EDIT BELOW
  13. */
  14. use Shuchkin\SimpleXLSX;
  15. $usermap = ""; // to store which sheet contains user details
  16. $concatMap = array(); // to store job lengths
  17.  
  18. require_once __DIR__.'/SimpleXLSX.php';
  19.  
  20. // find latest file
  21. $files = glob($folder."*.xlsx");
  22. $files = array_combine($files, array_map("filemtime", $files));
  23. arsort($files);
  24. $latest_file = key($files);
  25.  
  26. if ( $xlsx = SimpleXLSX::parse($latest_file) ) {
  27. // first find the users sheet
  28. for($i = 0; $i <= sizeof($xlsx->sheetNames()); $i++){
  29. //echo $i." : ".$xlsx->sheetName($i)."\n";
  30. if($xlsx->sheetName($i) == "User Map"){
  31. $usermap = $i;
  32. //echo "Users sheet: $usermap\n";
  33. }
  34. }
  35. //echo "User: ".$user."\n";
  36. $ical = "BEGIN:VCALENDAR
  37. VERSION:2.0
  38. PRODID:-//$user, Inc.//EN";
  39.  
  40.  
  41. // all other sheets != usermap
  42. for($i = 0; $i <= sizeof($xlsx->sheetNames())-1; $i++){
  43. if($xlsx->sheetName($i) != "User Map"){
  44.  
  45. // populate map of jobs > 1 day long
  46. foreach( $xlsx->worksheet($i)->mergeCells->mergeCell as $c ) {
  47. $vals = explode(":", $c['ref']);
  48. $concatMap[$i.'-'.$vals[0]] = $vals[1];
  49. }
  50.  
  51. //echo "### ".$xlsx->sheetName($i)." ###\n";
  52.  
  53. foreach( $xlsx->rowsEx($i) as $rowsEx){
  54. for($j=1; $j<= sizeof($rowsEx)-2; $j++){
  55. if(trim($rowsEx[0]['value']) == $user){ // found user
  56. if(trim($rowsEx[$j]['value']) <> "" && trim($rowsEx[$j]['value']) <> "."){ // if contains data
  57. //echo $j." ".$xlsx->sheetName($i)." : " .$rowsEx[$j]['value']."\n";
  58. //print_r($rowsEx[$j]);
  59. $diff = 1;
  60. if(isset($concatMap[$i."-" .$rowsEx[$j]['name']])){
  61. //echo "more than 1 day! \n";
  62. $diff += getDiff($i."-" .$rowsEx[$j]['name'], $concatMap[$i."-" .$rowsEx[$j]['name']]);
  63. //echo $diff."\n";
  64. }
  65.  
  66. $ical .= "
  67. BEGIN:VEVENT
  68. UID:" .md5(uniqid(mt_rand(), true))."
  69. DTSTAMP:".gmdate("Ymd")."T".gmdate("His")."Z
  70. DTSTART:".date("Ymd", strtotime($j." ".$xlsx->sheetName($i)));
  71.  
  72. if($diff >= 1)
  73. $ical .= "\nDTEND:".date("Ymd", strtotime($j." ".$xlsx->sheetName($i) ." +$diff day"));
  74.  
  75. $ical .= "
  76. SUMMARY:Booking for ".$rowsEx[$j]['value']."
  77. END:VEVENT";
  78.  
  79.  
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86.  
  87.  
  88. $ical .= "
  89. END:VCALENDAR";
  90.  
  91. header("Content-type: text/calendar; charset=utf-8");
  92. header("Content-Disposition: inline; filename=calendar.ics");
  93. echo $ical;
  94.  
  95. } else {
  96. echo SimpleXLSX::parseError();
  97. }
  98.  
  99. function getDiff($start, $end){
  100. $start = preg_replace("/[^A-Z]+/", "", $start);
  101. $end = preg_replace("/[^A-Z]+/", "", $end);
  102. $s1 = alphabet_to_number($start);
  103. $e1 = alphabet_to_number($end);
  104. return $e1 - $s1;
  105. //return 2;
  106. }
  107. function alphabet_to_number($string) {
  108. $string = strtoupper($string);
  109. $length = strlen($string);
  110. $number = 0;
  111. $level = 1;
  112. while ($length >= $level ) {
  113. $char = $string[$length - $level];
  114. $c = ord($char) - 64;
  115. $number += $c * (26 ** ($level-1));
  116. $level++;
  117. }
  118. return $number;
  119. }
  120. ?>
Buy Me A Coffee