openSRS_loader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace DNSManager\opensrs;
  3. use \stdClass;
  4. require_once ("openSRS_config.php");
  5. // Load / Include all files from specified folders
  6. loadOpenSRS (OPENSRSURI);
  7. loadOpenSRS (OPENSRSURI . OPENSRSDOMAINS, "php", true);
  8. //loadOpenSRS (OPENSRSURI . OPENSRSMAIL);
  9. //loadOpenSRS (OPENSRSURI . OPENSRSFASTLOOKUP);
  10. function loadOpenSRS ($local = "", $extension="php", $subfolders=false){
  11. if (!is_dir($local)){
  12. trigger_error("OSRS Error - Unable to find library directory $local, please check the OPENSRSURI path in openSRS_config.php");
  13. } else {
  14. $dir = opendir($local);
  15. while(false != ($file = readdir($dir))) {
  16. // Files in root directory
  17. if($file != "." && $file != ".." && !is_dir($local . $file ."/")) {
  18. $fileArr = explode(".", $file);
  19. $lastExt = count ($fileArr) - 1;
  20. if( substr($fileArr[0], 0, 1) != "_" && $fileArr[$lastExt] == $extension){
  21. if (!is_file($local . $file))
  22. trigger_error("OSRS Error - Unable to find library file $file, please check the paths in openSRS_config.php");
  23. else
  24. require_once ($local . $file);
  25. }
  26. }
  27. // Files in subfolders
  28. if($file != "." && $file != ".." && is_dir($local . $file ."/") && $subfolders) {
  29. $subPath = $local . $file ."/";
  30. if (!is_dir($subPath)){
  31. trigger_error("OSRS Error - Unable to find library directory $subPath, please check path constants in openSRS_config.php");
  32. } else {
  33. $subDir = opendir($subPath);
  34. while(false != ($subFile = readdir($subDir))) {
  35. // Files in subdirectory
  36. if($subFile != "." && $subFile != ".." && !is_dir($subPath . $subFile ."/")) {
  37. $subfileArr = explode(".", $subFile);
  38. $sublastExt = count ($subfileArr) - 1;
  39. if( substr($subfileArr[0], 0, 1) != "_" && $subfileArr[$sublastExt] == $extension) {
  40. if (!is_file($subPath . $subFile))
  41. trigger_error("OSRS Error - Unable to find library file $file, please check the paths in openSRS_config.php");
  42. else
  43. require_once ($subPath . $subFile);
  44. }
  45. }
  46. }
  47. closedir($subDir);
  48. }
  49. }
  50. }
  51. closedir($dir);
  52. }
  53. }
  54. // Array -> Object -> Array conversion
  55. function array2object($data) {
  56. if(!is_array($data)) return $data;
  57. $object = new stdClass();
  58. if (is_array($data) && count($data) > 0) {
  59. foreach ($data as $name=>$value) {
  60. $name = strtolower(trim($name));
  61. if (!empty($name)) {
  62. $object->$name = array2object($value);
  63. }
  64. }
  65. }
  66. return $object;
  67. }
  68. function object2array($data){
  69. if(!is_object($data) && !is_array($data)) return $data;
  70. if(is_object($data)) $data = get_object_vars($data);
  71. return array_map('object2array', $data);
  72. }
  73. // Call parsers and functions of openSRS
  74. function processOpenSRS ($type="", $data="", $config = '', $rData = array()) {
  75. if ($type != "" && $data != ""){
  76. if ($type == "array") $dataArray = $data; // ARRAY
  77. if ($type == "json"){ // JSON
  78. $json = str_replace("\\\"", "\"", $data); // Replace \" with " for JSON that comes from Javascript
  79. $dataArray = json_decode($json, true);
  80. }
  81. if ($type == "yaml") $dataArray = Spyc::YAMLLoad($data); // YAML
  82. // Convert associative array to object
  83. $dataObject = array2object($dataArray);
  84. }
  85. $class = '\DNSManager\opensrs\\' . $dataObject->func;
  86. // Call appropriate class
  87. if (class_exists($class)){
  88. if(!empty($rData))
  89. $classCall = new $class($type, $dataObject, $config, $rData);
  90. else
  91. $classCall = new $class($type, $dataObject, $config);
  92. } else {
  93. $classCall = null;
  94. trigger_error("OSRS Error - Unable to find the function passed. Either the function is misspelled or there are incorrect file paths set in openSRS_config.php.");
  95. }
  96. return $classCall;
  97. }
  98. function convertArray2Formated ($type="", $data="") {
  99. $resultString = "";
  100. if ($type == "json") $resultString = json_encode($data);
  101. if ($type == "yaml") $resultString = Spyc::YAMLDump($data);
  102. return $resultString;
  103. }
  104. function convertFormated2array ($type="", $data="") {
  105. $resultArray = "";
  106. if ($type == "json") $resultArray = json_decode($data, true);
  107. if ($type == "yaml") $resultArray = Spyc::YAMLLoad($data);;
  108. return $resultArray;
  109. }