PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/functions.php

http://snaptest.googlecode.com/
PHP | 113 lines | 88 code | 18 blank | 7 comment | 21 complexity | 2847c35aa39719cf323dc261fb136964 MD5 | raw file
  1. <?php
  2. function SNAP_get_long_options() {
  3. $sequentials = array();
  4. $arguments = array();
  5. // get our arguments off of $_REQUEST in CGI mode
  6. if (SNAP_CGI_MODE) {
  7. foreach ($_REQUEST as $key => $value) {
  8. if ($value === "") {
  9. // a value of "" means it had no =, sequential
  10. $sequentials[] = SNAP_unmangle_request_var($key);
  11. }
  12. else {
  13. // standard key/value pair
  14. $key = SNAP_unmangle_request_var($key);
  15. $key = preg_replace('/^--(.*)/', '$1', $key);
  16. $arguments[$key] = SNAP_unmangle_request_var($value);
  17. }
  18. }
  19. }
  20. else {
  21. // CLI mode
  22. global $argv;
  23. if (!is_array($argv)) {
  24. break;
  25. }
  26. foreach ($argv as $idx => $arg) {
  27. if ($idx == 0) {
  28. continue;
  29. }
  30. if (strpos($arg, '--') === FALSE) {
  31. $sequentials[] = $arg;
  32. continue;
  33. }
  34. list($opt_name, $opt_value) = explode('=', substr($arg, 2), 2);
  35. $arguments[$opt_name] = trim($opt_value, '"\'');
  36. }
  37. }
  38. foreach ($sequentials as $idx => $arg) {
  39. $arguments[$idx] = $arg;
  40. }
  41. return $arguments;
  42. }
  43. function SNAP_make_long_options($opts) {
  44. $opt_string = '';
  45. foreach ($opts as $key => $value) {
  46. if (is_numeric($key)) {
  47. $opt_string .= ' ' . SNAP_mangle_request_var($value);
  48. }
  49. else {
  50. $opt_string .= ' --' . SNAP_mangle_request_var($key) . '=' . SNAP_mangle_request_var($value);
  51. }
  52. }
  53. return $opt_string;
  54. }
  55. function SNAP_unmangle_request_var($var) {
  56. return str_replace('__D_O_T__', '.', $var);
  57. }
  58. function SNAP_mangle_request_var($var) {
  59. if (SNAP_CGI_MODE) {
  60. return str_replace('.', '__D_O_T__', $var);
  61. }
  62. else {
  63. return $var;
  64. }
  65. }
  66. function SNAP_recurse_directory($path, $xtn) {
  67. if (!is_dir($path)) {
  68. return array($path);
  69. }
  70. $file_list = array();
  71. $handle = opendir($path);
  72. while (false !== ($file = readdir($handle))) {
  73. if (substr($file, 0, 1) == '.') {
  74. continue;
  75. }
  76. if (substr($path, -1) == DIRECTORY_SEPARATOR) {
  77. $file = $path . $file;
  78. }
  79. else {
  80. $file = $path.DIRECTORY_SEPARATOR.$file;
  81. }
  82. // recursion on directory
  83. if (is_dir($file)) {
  84. $file_list = array_merge($file_list, SNAP_recurse_directory($file, $xtn));
  85. continue;
  86. }
  87. // is a file, check xtn
  88. if (!preg_match('#'.$xtn.'#', $file)) {
  89. continue;
  90. }
  91. // valid, add
  92. $file_list[] = $file;
  93. }
  94. return $file_list;
  95. }