PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/scalr_newui/app/src/LibWebta/library/NET/DNS/class.AbstractDNSZone.php

https://github.com/kennethjiang/Wolke
PHP | 241 lines | 114 code | 31 blank | 96 comment | 14 complexity | 123de5edd90ba903287607e7427b19ad MD5 | raw file
  1. <?
  2. /**
  3. * This file is a part of LibWebta, PHP class library.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 2 of the GPL license,
  8. * that is bundled with this package in the file license.txt and is
  9. * available through the world-wide-web at the following url:
  10. * http://www.gnu.org/copyleft/gpl.html
  11. *
  12. * @category LibWebta
  13. * @package NET
  14. * @subpackage DNS
  15. * @copyright Copyright (c) 2003-2007 Webta Inc, http://www.gnu.org/licenses/gpl.html
  16. * @license http://www.gnu.org/licenses/gpl.html
  17. */
  18. /**
  19. * @name AbstractDNSZone
  20. * @abstract
  21. * @category LibWebta
  22. * @package NET
  23. * @subpackage DNS
  24. * @version 1.0
  25. * @author Igor Savchenko <http://webta.net/company.html>
  26. * @author Alex Kovalyov <http://webta.net/company.html>
  27. */
  28. abstract class AbstractDNSZone extends Core
  29. {
  30. /**
  31. * Generate a new serial based on given one.
  32. *
  33. * This generates a new serial, based on the often used format
  34. * YYYYMMDDXX where XX is an ascending serial,
  35. * allowing up to 100 edits per day. After that the serial wraps
  36. * into the next day and it still works.
  37. *
  38. * @param int $serial Current serial
  39. * @return int New serial
  40. */
  41. static public function RaiseSerial($serial = 0)
  42. {
  43. $serial = (int)$serial;
  44. if (substr($serial, 0, 8) == date('Ymd'))
  45. {
  46. //Serial's today. Simply raise it.
  47. $serial = (int)$serial + 1;
  48. }
  49. elseif ($serial > date('Ymd00'))
  50. {
  51. //Serial's after today.
  52. $serial = (int)$serial + 1;
  53. }
  54. else
  55. {
  56. //Older serial. Generate new one.
  57. $serial = date('YmdH');
  58. }
  59. return intval($serial);
  60. }
  61. /**
  62. * Checks if a value is an IP address or not.
  63. *
  64. * @param string Value to check.
  65. * @return bool true or false.
  66. * @access public
  67. */
  68. function IsIP($ip)
  69. {
  70. return (bool) preg_match('/^([0-9]{1,3}\.){4}$/', $ip . '.');
  71. }
  72. function IsDomain($domain)
  73. {
  74. if ($domain == "*")
  75. return true;
  76. return (bool) preg_match('/^[^\.]([a-zA-Z0-9\-]{0,}\.)+$/', $domain . ".");
  77. }
  78. /**
  79. * Checks if a value is a local network IP address or not.
  80. *
  81. * @param string Value to check.
  82. * @return bool true or false.
  83. * @access public
  84. */
  85. function IsValidIP($ip)
  86. {
  87. $internal = (bool) preg_match('/^192|10(\.[0-9]{1,3}){3}$/', $ip);
  88. $bcast = (bool) preg_match('/^([0-9]{1,3}\.){3}255|0$/', $ip);
  89. return !($internal || $bcast);
  90. }
  91. /**
  92. * Reverses IP address string for PTR needs
  93. *
  94. * @param string $ip Ip address string
  95. * @return string Reversed IP
  96. * @access public
  97. */
  98. public function ReverseIP($ip)
  99. {
  100. $chunks = explode(".", $ip);
  101. $chunksr = array_reverse($chunks);
  102. $retval = implode(".", $chunksr);
  103. return ($retval);
  104. }
  105. /**
  106. * Converts a BIND-style timeout(1D, 2H, 15M) to seconds.
  107. *
  108. * @param string $time Time to convert.
  109. * @return int time in seconds on success, PEAR error on failure.
  110. */
  111. function ParseTimeToSeconds($time)
  112. {
  113. if (is_numeric($time))
  114. {
  115. //Already a number. Return.
  116. return $time;
  117. }
  118. else
  119. {
  120. $pattern = '/([0-9]+)([a-zA-Z]+)/';
  121. $split = preg_split($pattern, $time, -1,
  122. PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  123. if (count($split) != 2) {
  124. Core::RaiseWarning(sprintf(_("Unable to parse time. %d"), $time));
  125. }
  126. list($num, $what) = $split;
  127. switch (strtoupper($what))
  128. {
  129. case 'S':
  130. $times = 1; //Seconds
  131. break;
  132. case 'M':
  133. $times = 1 * 60; //Minute
  134. break;
  135. case 'H':
  136. $times = 1 * 60 * 60; //Hour
  137. break;
  138. case 'D':
  139. $times = 1 * 60 * 60 * 24; //Day
  140. break;
  141. case 'W':
  142. $times = 1 * 60 * 60 * 24 * 7; //Week
  143. break;
  144. default:
  145. Core::RaiseWarning(sprintf(_("Unable to parse time. %d"), $time));
  146. break;
  147. }
  148. $time = $num * $times;
  149. return $time;
  150. }
  151. }
  152. /**
  153. * Append dot to the end of FQDN
  154. * @access public
  155. * @param string $domain Domain name
  156. * @return void
  157. */
  158. public function Dottify($value)
  159. {
  160. $retval = $this->UnDottify($value);
  161. $retval .= ".";
  162. return $retval;
  163. }
  164. /**
  165. * Remove leading dot
  166. * @access public
  167. * @param string $domain Domain name
  168. * @return void
  169. */
  170. public function UnDottify($domain)
  171. {
  172. $retval = rtrim($domain, ".");
  173. return $retval;
  174. }
  175. /**
  176. * Set current template
  177. * @access public
  178. * @param string $template DNS Zone template
  179. * @return void
  180. */
  181. public function SetTemplate($template)
  182. {
  183. $this->Template = $template;
  184. }
  185. /**
  186. * Raise MX pref on 10
  187. *
  188. * @param string $pref Preferences
  189. * @return string Reversed IP
  190. * @access protected
  191. */
  192. protected function RaiseMXPref($pref)
  193. {
  194. // Increase forcefully in case if this pref already assigned
  195. // to another MX record or pref is not set (default)
  196. if (count($this->MXPrefs))
  197. {
  198. if (in_array($pref, $this->MXPrefs) || !$pref)
  199. $retval = max($this->MXPrefs) + 10;
  200. else
  201. $retval = $pref;
  202. }
  203. else
  204. $retval = $pref;
  205. // Add this new pref to stack
  206. $this->MXPrefs[] = $retval;
  207. return($retval);
  208. }
  209. }
  210. ?>