PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/plugins/cloud/web/class/cloudnat.class.php

https://github.com/qyjohn/openqrm
PHP | 243 lines | 143 code | 49 blank | 51 comment | 18 complexity | 5c3268c56483fa5bb395f6c5e22a9795 MD5 | raw file
  1. <?php
  2. /*
  3. This file is part of openQRM.
  4. openQRM is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2
  6. as published by the Free Software Foundation.
  7. openQRM is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with openQRM. If not, see <http://www.gnu.org/licenses/>.
  13. Copyright 2009, Matthias Rechenburg <matt@openqrm.com>
  14. */
  15. // This class represents a cloudnat translation in openQRM
  16. $RootDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/base/';
  17. require_once "$RootDir/include/openqrm-database-functions.php";
  18. require_once "$RootDir/class/resource.class.php";
  19. require_once "$RootDir/class/virtualization.class.php";
  20. require_once "$RootDir/class/image.class.php";
  21. require_once "$RootDir/class/kernel.class.php";
  22. require_once "$RootDir/class/plugin.class.php";
  23. require_once "$RootDir/class/event.class.php";
  24. $CLOUD_NAT_TABLE="cloud_nat";
  25. global $CLOUD_NAT_TABLE;
  26. $event = new event();
  27. global $event;
  28. class cloudnat {
  29. var $id = '';
  30. var $internal_network = '';
  31. var $external_network = '';
  32. // ---------------------------------------------------------------------------------
  33. // methods to create an instance of a cloudnat object filled from the db
  34. // ---------------------------------------------------------------------------------
  35. // returns an appliance from the db selected by id or name
  36. function get_instance($id) {
  37. global $CLOUD_NAT_TABLE;
  38. global $event;
  39. $db=openqrm_get_db_connection();
  40. if ("$id" != "") {
  41. $cloudnat_array = &$db->Execute("select * from $CLOUD_NAT_TABLE where cn_id=$id");
  42. } else {
  43. $event->log("get_instance", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", "Could not create instance of event without data", "", "", 0, 0, 0);
  44. return;
  45. }
  46. foreach ($cloudnat_array as $index => $cloudnat) {
  47. $this->id = $cloudnat["cn_id"];
  48. $this->internal_network = $cloudnat["cn_internal_net"];
  49. $this->external_network = $cloudnat["cn_external_net"];
  50. }
  51. return $this;
  52. }
  53. // returns an appliance from the db selected by id
  54. function get_instance_by_id($id) {
  55. $this->get_instance($id);
  56. return $this;
  57. }
  58. // ---------------------------------------------------------------------------------
  59. // general cloudnat methods
  60. // ---------------------------------------------------------------------------------
  61. // checks if given cloudnat id is free in the db
  62. function is_id_free($cloudnat_id) {
  63. global $CLOUD_NAT_TABLE;
  64. global $event;
  65. $db=openqrm_get_db_connection();
  66. $rs = &$db->Execute("select cn_id from $CLOUD_NAT_TABLE where cn_id=$cloudnat_id");
  67. if (!$rs)
  68. $event->log("is_id_free", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", $db->ErrorMsg(), "", "", 0, 0, 0);
  69. else
  70. if ($rs->EOF) {
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. // adds cloudnat to the database
  77. function add($cloudnat_fields) {
  78. global $CLOUD_NAT_TABLE;
  79. global $event;
  80. if (!is_array($cloudnat_fields)) {
  81. $event->log("add", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", "cloudnat_field not well defined", "", "", 0, 0, 0);
  82. return 1;
  83. }
  84. // set stop time and status to now
  85. $now=$_SERVER['REQUEST_TIME'];
  86. $db=openqrm_get_db_connection();
  87. $result = $db->AutoExecute($CLOUD_NAT_TABLE, $cloudnat_fields, 'INSERT');
  88. if (! $result) {
  89. $event->log("add", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", "Failed adding new cloudnat to database", "", "", 0, 0, 0);
  90. }
  91. }
  92. //--------------------------------------------------
  93. /**
  94. * update an cloudnat
  95. * <code>
  96. * $fields = array();
  97. * $fields['cn_internal'] = 'ip';
  98. * $fields['cn_external'] = 'ip';
  99. * $image = new cloudnat();
  100. * $image->update(1, $fields);
  101. * </code>
  102. * @access public
  103. * @param int $cn_id
  104. * @param array $cn_fields
  105. * @return bool
  106. */
  107. //--------------------------------------------------
  108. function update($cn_id, $cn_fields) {
  109. global $CLOUD_NAT_TABLE;
  110. global $event;
  111. if ($cn_id < 0 || ! is_array($cn_fields)) {
  112. $event->log("update", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", "Unable to update cn $cn_id", "", "", 0, 0, 0);
  113. return 1;
  114. }
  115. $db=openqrm_get_db_connection();
  116. unset($cn_fields["cn_id"]);
  117. $result = $db->AutoExecute($CLOUD_NAT_TABLE, $cn_fields, 'UPDATE', "cn_id = $cn_id");
  118. if (! $result) {
  119. $event->log("update", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", "Failed updating cn $cn_id", "", "", 0, 0, 0);
  120. }
  121. }
  122. // removes cloudnat from the database
  123. function remove($cloudnat_id) {
  124. global $CLOUD_NAT_TABLE;
  125. $db=openqrm_get_db_connection();
  126. $rs = $db->Execute("delete from $CLOUD_NAT_TABLE where cn_id=$cloudnat_id");
  127. }
  128. // returns the number of cloudnat
  129. function get_count() {
  130. global $CLOUD_NAT_TABLE;
  131. $count=0;
  132. $db=openqrm_get_db_connection();
  133. $rs = $db->Execute("select count(cn_id) as num from $CLOUD_NAT_TABLE");
  134. if (!$rs) {
  135. print $db->ErrorMsg();
  136. } else {
  137. $count = $rs->fields["num"];
  138. }
  139. return $count;
  140. }
  141. // returns a list of all cloudnat ids
  142. function get_all_ids() {
  143. global $CLOUD_NAT_TABLE;
  144. global $event;
  145. $cloudnat_list = array();
  146. $query = "select cn_id from $CLOUD_NAT_TABLE";
  147. $db=openqrm_get_db_connection();
  148. $rs = $db->Execute($query);
  149. if (!$rs)
  150. $event->log("get_list", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", $db->ErrorMsg(), "", "", 0, 0, 0);
  151. else
  152. while (!$rs->EOF) {
  153. $cloudnat_list[] = $rs->fields;
  154. $rs->MoveNext();
  155. }
  156. return $cloudnat_list;
  157. }
  158. // displays the cloudnat-overview
  159. function display_overview($offset, $limit, $sort, $order) {
  160. global $CLOUD_NAT_TABLE;
  161. global $event;
  162. $db=openqrm_get_db_connection();
  163. $recordSet = &$db->SelectLimit("select * from $CLOUD_NAT_TABLE order by $sort $order", $limit, $offset);
  164. $cloudnat_array = array();
  165. if (!$recordSet) {
  166. $event->log("display_overview", $_SERVER['REQUEST_TIME'], 2, "cloudnat.class.php", $db->ErrorMsg(), "", "", 0, 0, 0);
  167. } else {
  168. while (!$recordSet->EOF) {
  169. array_push($cloudnat_array, $recordSet->fields);
  170. $recordSet->MoveNext();
  171. }
  172. $recordSet->Close();
  173. }
  174. return $cloudnat_array;
  175. }
  176. // translates an internal cloud ip to an external one
  177. function translate($internal_ip) {
  178. global $CLOUD_NAT_TABLE;
  179. global $event;
  180. $this->get_instance_by_id(1);
  181. $external_net = $this->external_network;
  182. $lastbyte_internal = strrchr($internal_ip, ".");
  183. $external_net_last_dot = strrpos($external_net, ".");
  184. $external_without_last_byte = substr($external_net, 0, $external_net_last_dot);
  185. $translated_internal_ip = $external_without_last_byte.$lastbyte_internal;
  186. return $translated_internal_ip;
  187. }
  188. // ---------------------------------------------------------------------------------
  189. }
  190. ?>