rawbk

Untitled

Oct 27th, 2021
831
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // this can either be run via a web server OR via CLI:
  4. // php index.php --file='employees.csv'
  5.  
  6. $is_cli = (php_sapi_name() == "cli"); // is this being run via CLI ?
  7. $csv_file = false; // instantiate a boolean to determine if a CSV is already provided
  8.  
  9. if (!$is_cli) { // if not, display html
  10.     echo <<<___HRD___
  11. <!DOCTYPE html>
  12. <html>
  13. <body>
  14. ___HRD___;
  15.  
  16.     // CSV file is provided via web server
  17.     $csv_file = isset($_FILES['upload']) ? $_FILES['upload'] : $csv_file;
  18.  
  19. } else {
  20.  
  21.     // CSV is provided via CLI
  22.     $opts = getopt('', ['file:']);
  23.     $csv_file = [
  24.         'tmp_name' => $opts['file'],
  25.         'name' => $opts['file']
  26.     ];
  27. }
  28.  
  29. if ($csv_file) { // if a file has been submitted, process it
  30.  
  31.     $file = $csv_file['tmp_name']; // uploaded file location
  32.     $file_name = $csv_file['name']; // uploaded file name
  33.  
  34.     // Ensure that it's a .csv .  This literally only checks the extension and isn't a perfect solution nor is it really
  35.     // secure, but I guess it's at least something (Apache won't serve it as an executable unless we have some weird settings)
  36.     $file_type = strtolower(pathinfo($file_name,PATHINFO_EXTENSION)); // lowercase file extension
  37.  
  38.     if ($file_type === 'csv') { // it's a .csv extension, we can continue
  39.  
  40.         // the time to which we are making a comparison. Anything after the current time and within this limit will be displayed.
  41.         $time_limit = strtotime('+7 days');
  42.  
  43.         $unix_timestamp = time(); // the current unix timestamp
  44.         $current_year = date('Y'); // the current year (to add to incomplete information below)
  45.         $return = $matches = []; // initialize an array to store the output
  46.         $i = 0; // a counter that increments with each row in the CSV
  47.  
  48.         if (($handle = fopen($file, 'r')) !== FALSE) { // nice, we're able to read the upload...
  49.  
  50.             // for each row... I'd be surprised if any rows are greater than 4K characters
  51.             while (($data = fgetcsv($handle, 4000, ',')) !== FALSE) {
  52.  
  53.                 // store any pertinent information
  54.                 $return[$i] = [
  55.                     'name' => $data[0],
  56.                     'title' => $data[1],
  57.                     'birthday' => $data[2],
  58.                     'hire_date' => $data[3],
  59.                     'birthday_match' => false,
  60.                     'hire_match' => false,
  61.                     'matches' => false
  62.                 ];
  63.  
  64.                 // Thomas!  If you're not familiar with regex, this tutorial will seriously make a lot of things easier in the long run:
  65.                 // https://www.regular-expressions.info/
  66.  
  67.                 // For the birthday, it looks like we might not have a year.
  68.                 // This regular expression searches for either one or two digits separated by a '/' .
  69.                 // If found, we'll add the current year to the date.
  70.                 if (preg_match('/^\\d{1,2}\/\\d{1,2}$/', $return[$i]['birthday']) !== false) { // no year supplied
  71.                     $return[$i]['birthday_modified'] = "{$return[$i]['birthday']}/{$current_year}";
  72.                 } else { // if this isn't in a format without the year, attempt to save it as-is
  73.                     $return[$i]['birthday_modified'] = $return[$i]['birthday'];
  74.                 }
  75.  
  76.                 $m = false; // instantiate a variable to determine if the anniversary matches
  77.  
  78.                 // For the hire date, record which anniversary it is and capture the backreference in $1 .
  79.                 if (preg_match('/^\\d{1,2}\/\\d{1,2}\/(\\d{2,4})$/', $return[$i]['hire_date'], $m) !== false) {
  80.                     $return[$i]['hire_date_years'] = $current_year - $m[1];
  81.                 }
  82.  
  83.                 // Grab UNIX timestamps for the birthday and date hired (this is the number of seconds since 1970-01-01).
  84.                 $birthday_timestamp = strtotime($return[$i]['birthday_modified']); // timestamp for birthdays
  85.  
  86.                 // Since we want the anniversary of hire dates, we need to remove the year ("/{FOUR_DIGITS}" at the end of the string).
  87.                 $replace = preg_replace('/\/\\d{4}$/', '', $return[$i]['hire_date']); // remove the year
  88.                 $return[$i]['hire_date_modified'] = $replace . "/{$current_year}"; // add this year
  89.                 $hire_timestamp = strtotime($return[$i]['hire_date_modified']); // finalized timestamp for hire dates
  90.  
  91.                 // Figure out if the birthdate or hire date for a row matches time constraints
  92.                 // (after the current time, within the next $time_limit).
  93.  
  94.                 // Is the birthday timestamp is greater than now, but less than the timestamp for $time_limit ?
  95.                 $birthday_match = (($birthday_timestamp > $unix_timestamp) && ($birthday_timestamp <= $time_limit));
  96.  
  97.                 // Is the hire date timestamp is greater than now, but less than the timestamp for $time_limit ?
  98.                 $hire_match = (($hire_timestamp > $unix_timestamp) && ($hire_timestamp <= $time_limit));
  99.  
  100.                 // If birthdate is greater than the current time and within the upper time constraint, store it.
  101.                 if ($birthday_match) {
  102.                     $return[$i]['birthday_match'] = true;
  103.                     $matches[] = $return[$i];
  104.                 }
  105.  
  106.                 // If hire date is greater than the current time and within the upper time constraint, store it.
  107.                 if ($hire_match) {
  108.                     $return[$i]['hire_match'] = true;
  109.                     $matches[] = $return[$i];
  110.                 }
  111.  
  112.                 $i++; // increment the counter
  113.  
  114.             }
  115.  
  116.             fclose($handle); // we don't need anything else from this file
  117.  
  118.         }
  119.  
  120.         foreach ($matches as $match) {
  121.             if ($match['birthday_match']) {
  122.                 $match['matches'] = true; // For debugging.
  123.                 echo "{$match['name']} ({$match['title']}) BIRTHDAY: {$match['birthday']}";
  124.                 echo $is_cli ? '' : '<br />'; // if CLI, don't print out html
  125.                 echo "\r\n";
  126.             }
  127.  
  128.             if ($match['hire_match']) {
  129.                 $match['matches'] = true; // For debugging.
  130.                 echo "{$match['name']} ({$match['title']}) HIRE ANNIVERSARY: {$match['hire_date']} ({$match['hire_date_years']} years)";
  131.                 echo $is_cli ? '' : '<br />'; // if CLI, don't print out html
  132.                 echo "\r\n";
  133.             }
  134.  
  135.         }
  136.  
  137.         // echo '<pre>'; print_r($return); echo '</pre>'; // for debugging
  138.  
  139.     } else { // only allow files with a .csv extension (thanks Bill Gates)
  140.         echo 'File type must be csv.';
  141.     }
  142.  
  143.  
  144. } else { // no CSV submitted, just display a form for upload
  145.  
  146.     echo <<<___HRD___
  147. <form action="index.php" method="post" enctype="multipart/form-data">
  148.     Select CSV to upload:
  149.     <input type="file" name="upload" id="upload">
  150.     <input type="submit" value="Upload" name="submit">
  151. </form>
  152. ___HRD___;
  153.  
  154. }
  155.  
  156. if (!$is_cli) {
  157.     echo <<<___HRD___
  158. </body>
  159. </html>
  160. ___HRD___;
  161. }
  162.  
  163. // EOF
  164.  
RAW Paste Data