[1] | 1 | #!/usr/local/bin/php -q |
---|
| 2 | <?php |
---|
| 3 | /** |
---|
| 4 | * file_importer.cli.php |
---|
| 5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | require realpath(dirname(__FILE__) . '/..') . '/config/cli_config.inc.php'; |
---|
| 9 | |
---|
[127] | 10 | $app =& App::getInstance(); |
---|
[130] | 11 | $db =& DB::getInstance(); |
---|
| 12 | |
---|
[127] | 13 | |
---|
[1] | 14 | // Test arguments. |
---|
| 15 | if ($_SERVER['argc'] == 2 && is_dir($_SERVER['argv'][1])) { |
---|
| 16 | $file_dir = preg_replace('/\/$/', '', $_SERVER['argv'][1]); |
---|
| 17 | } else { |
---|
| 18 | die("You must specify the directory of files to import as the only argument.\n"); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | // Get array of all files. |
---|
| 22 | $files = getFilesRecursive($file_dir, 1); |
---|
| 23 | |
---|
| 24 | // Loop through files. |
---|
| 25 | // dump($files); |
---|
| 26 | $file_count = 0; |
---|
| 27 | if (is_array($files) && !empty($files)) { |
---|
| 28 | foreach ($files as $file) { |
---|
| 29 | if ($fp = fopen($file, 'r')) { |
---|
| 30 | echo ++$file_count . "\n"; |
---|
| 31 | $file_text = join('', file($file)); |
---|
| 32 | fclose($fp); |
---|
[42] | 33 | |
---|
[1] | 34 | // Do something with file contents. |
---|
| 35 | preg_match('/BALANCE:\s*\$([\.\d]+)/', $file_text, $amt); |
---|
| 36 | $file_date = date('Y-m-d', strtotime(preg_replace('|[^_]*_|', '', basename($file)))); |
---|
[130] | 37 | // $db->query(" |
---|
[1] | 38 | // INSERT INTO invoice_tbl ( |
---|
| 39 | // client_id, |
---|
| 40 | // invoice_type, |
---|
| 41 | // invoice_date, |
---|
| 42 | // invoice_amount, |
---|
| 43 | // invoice_status, |
---|
| 44 | // payment_type, |
---|
| 45 | // invoice_text, |
---|
| 46 | // email_sent_datetime, |
---|
| 47 | // added_datetime |
---|
| 48 | // ) VALUES ( |
---|
[130] | 49 | // '" . $db->escapeString(0) . "', |
---|
| 50 | // '" . $db->escapeString('hosting') . "', |
---|
| 51 | // '" . $db->escapeString($file_date) . "', |
---|
| 52 | // '" . $db->escapeString($amt[1]) . "', |
---|
| 53 | // '" . $db->escapeString('Paid') . "', |
---|
| 54 | // '" . $db->escapeString('') . "', |
---|
| 55 | // '" . $db->escapeString($file_text) . "', |
---|
| 56 | // '" . $db->escapeString($file_date) . "', |
---|
[1] | 57 | // NOW() |
---|
| 58 | // ) |
---|
| 59 | // "); |
---|
| 60 | |
---|
| 61 | } else { |
---|
[127] | 62 | $app->logMsg('Could not open file: ' . $file, LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 63 | } |
---|
| 64 | } |
---|
[127] | 65 | $app->logMsg('Proccessing complete: ' . $file_count . ' files total.', LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 66 | } else { |
---|
[127] | 67 | $app->logMsg('No files available in that directory.', LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
[42] | 74 | * Find all files in directories recursivly with a specified file extension. |
---|
[1] | 75 | * |
---|
| 76 | * @param string $dir the full path to the directory to scan |
---|
| 77 | * @param array $valid_extensions valid extensions |
---|
| 78 | * @param int $recurs_lev how many levels deep to scan. Set this |
---|
| 79 | * to a low number to prevent infinite loops |
---|
| 80 | * |
---|
| 81 | * @return array multi-dimentional array of found files on success |
---|
| 82 | */ |
---|
| 83 | function getFilesRecursive($dir, $recurs_lev=0, $return=true) |
---|
| 84 | { |
---|
| 85 | static $output; |
---|
[42] | 86 | |
---|
[1] | 87 | $dir_handle = opendir($dir); |
---|
| 88 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
| 89 | if (!preg_match('/^\./', $file)) { |
---|
| 90 | if (is_dir($dir . '/' . $file) && $recurs_lev > 0) { |
---|
| 91 | getFilesRecursive($dir . '/' . $file, $valid_extensions, $recurs_lev - 1); |
---|
| 92 | } else if (!is_dir($dir . '/' . $file)) { |
---|
| 93 | $output[] = $dir . '/' . $file; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | if ($return) { |
---|
| 98 | return $output; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | ?> |
---|