/gui/include/ispCP/NetworkCard.php

https://github.com/BenBE/ispCP · PHP · 260 lines · 101 code · 46 blank · 113 comment · 12 complexity · afb00007a049910418861941915a3d6c MD5 · raw file

  1. <?php
  2. /**
  3. * ispCP ω (OMEGA) a Virtual Hosting Control System
  4. *
  5. * The contents of this file are subject to the Mozilla Public License
  6. * Version 1.1 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS"
  11. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  12. * License for the specific language governing rights and limitations
  13. * under the License.
  14. *
  15. * The Original Code is "ispCP - ISP Control Panel".
  16. *
  17. * The Initial Developer of the Original Code is ispCP Team.
  18. * Portions created by Initial Developer are Copyright (C) 2006-2011 by
  19. * isp Control Panel. All Rights Reserved.
  20. *
  21. * @category ispCP
  22. * @package ispCP_NetworkCard
  23. * @copyright 2006-2011 by ispCP | http://isp-control.net
  24. * @author ispCP Team
  25. * @version SVN: $Id$
  26. * @link http://isp-control.net ispCP Home Site
  27. * @license http://www.mozilla.org/MPL/ MPL 1.1
  28. */
  29. /**
  30. * Class Network Card
  31. *
  32. * @category ispCP
  33. * @package ispCP_NetworkCard
  34. * @author ispCP Team
  35. */
  36. class ispCP_NetworkCard {
  37. /**
  38. * Should be documented
  39. *
  40. * @var array
  41. */
  42. protected $_interfacesInfo = array();
  43. /**
  44. * Should be documented
  45. *
  46. * @var array
  47. */
  48. protected $_interfaces = array();
  49. /**
  50. * Should be documented
  51. *
  52. * array
  53. */
  54. protected $_offlineInterfaces = array();
  55. /**
  56. * Should be documented
  57. *
  58. * @var array
  59. */
  60. protected $_virtualInterfaces = array();
  61. /**
  62. * Should be documented
  63. *
  64. * @var array
  65. */
  66. protected $_availableInterfaces = array();
  67. /**
  68. * Should be documented
  69. *
  70. * @var array
  71. */
  72. protected $_errors = '';
  73. /**
  74. * Should be documented
  75. *
  76. * @return void
  77. */
  78. public function __construct() {
  79. $this->_getInterface();
  80. $this->_populateInterfaces();
  81. }
  82. /**
  83. * Should be documented
  84. *
  85. * @param $filename
  86. * @return string
  87. */
  88. public function read($filename) {
  89. if (($result = @file_get_contents($filename)) === false) {
  90. $this->_errors .= sprintf(tr('File %s does not exists or cannot be reached!'), $filename);
  91. return '';
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Should be documented
  97. *
  98. * @return array
  99. */
  100. public function network() {
  101. $file = $this->read('/proc/net/dev');
  102. preg_match_all('/(.+):.+/', $file, $dev_name);
  103. return $dev_name[1];
  104. }
  105. /**
  106. * Should be documented
  107. *
  108. * @return void
  109. */
  110. private function _getInterface() {
  111. foreach ($this->network() as $value) {
  112. $this->_interfaces[] = trim($value);
  113. }
  114. }
  115. /**
  116. * Should be documented
  117. *
  118. * @param string $strProgram
  119. * @param string &$strError
  120. * @return bool|string
  121. */
  122. protected function executeExternal($strProgram, &$strError) {
  123. $strBuffer = '';
  124. $descriptorspec = array(
  125. 0 => array('pipe', 'r'),
  126. 1 => array('pipe', 'w'),
  127. 2 => array('pipe', 'w')
  128. );
  129. $pipes = array();
  130. $process = proc_open($strProgram, $descriptorspec, $pipes);
  131. if (is_resource($process)) {
  132. while (!feof($pipes[1])) {
  133. $strBuffer .= fgets($pipes[1], 1024);
  134. }
  135. fclose($pipes[1]);
  136. while (!feof($pipes[2])) {
  137. $strError .= fgets($pipes[2], 1024);
  138. }
  139. fclose($pipes[2]);
  140. }
  141. $return_value = proc_close($process);
  142. $strError = trim($strError);
  143. $strBuffer = trim($strBuffer);
  144. if (!empty($strError) || $return_value != 0) {
  145. $strError .= "\nReturn value: " . $return_value;
  146. return false;
  147. }
  148. return $strBuffer;
  149. }
  150. /**
  151. * Should be documented
  152. *
  153. * @return bool
  154. */
  155. private function _populateInterfaces() {
  156. $cfg = ispCP_Registry::get('Config');
  157. $err = '';
  158. $message = $this->executeExternal($cfg->CMD_IFCONFIG, $err);
  159. if (!$message) {
  160. $this->_errors .= tr('Error while trying to obtain list of network cards!') . $err;
  161. return false;
  162. }
  163. preg_match_all("/(?isU)([^ ]{1,}) {1,}.+(?:(?:\n\n)|$)/", $message, $this->_interfacesInfo);
  164. foreach ($this->_interfacesInfo[0] as $a) {
  165. if (preg_match("/inet addr\:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/",$a,$b)) {
  166. $this->_interfacesInfo[2][] = trim($b[1]);
  167. } else {
  168. $this->_interfacesInfo[2][] = '';
  169. }
  170. }
  171. $this->_offlineInterfaces =
  172. array_diff($this->_interfaces, $this->_interfacesInfo[1]);
  173. $this->_virtualInterfaces =
  174. array_diff($this->_interfacesInfo[1], $this->_interfaces);
  175. $this->_availableInterfaces = array_diff(
  176. $this->_interfaces,
  177. $this->_offlineInterfaces,
  178. $this->_virtualInterfaces,
  179. array('lo')
  180. );
  181. }
  182. /**
  183. * Should be documented
  184. *
  185. * @return array
  186. */
  187. public function getAvailableInterface() {
  188. return $this->_availableInterfaces;
  189. }
  190. /**
  191. * Should be documented
  192. *
  193. * @return string
  194. */
  195. public function getErrors() {
  196. return nl2br($this->_errors);
  197. }
  198. /**
  199. * Should be documented
  200. *
  201. * @param string $ip
  202. * @return array
  203. */
  204. public function ip2NetworkCard($ip) {
  205. $key = array_search($ip,$this->_interfacesInfo[2]);
  206. if ($key === false) {
  207. $this->_errors .= sprintf(
  208. tr("This IP (%s) is not assigned to any network card!"), $ip
  209. );
  210. } else {
  211. return $this->_interfacesInfo[1][$key];
  212. }
  213. }
  214. }