PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/kloxo/httpdocs/htmllib/lib/pserver/ipaddresslib.php

https://bitbucket.org/Nemcio/kloxo-mr
PHP | 752 lines | 518 code | 186 blank | 48 comment | 106 complexity | f5d88505392097541726c3ae37466a10 MD5 | raw file
  1. <?php
  2. class Ipaddress extends Lxdb
  3. {
  4. //Core
  5. static $__desc = array("", "", "ipaddress");
  6. //Data
  7. static $__desc_nname = Array("", "", "device_name");
  8. static $__desc_devname = Array("s", "", "device_name", URL_SHOW);
  9. static $__desc_ipaddr = Array("n", "", "ipaddress", URL_SHOW);
  10. static $__desc_server_name = Array("", "", "server_name");
  11. static $__desc_clientslist = Array("", "", "list_of_clients");
  12. static $__desc_clients_no = Array("", "", "no_of_clients");
  13. static $__desc_shared = Array("", "", "shared_ip_address");
  14. static $__desc_used_f = Array("e", "", "Used");
  15. static $__desc_used_f_v_on = Array("", "", "Used");
  16. static $__desc_used_f_v_dull = Array("", "", "not_used");
  17. static $__desc_netmask = Array("n", "", "netmask");
  18. static $__desc_status = Array("e", "", "s", "a=update&sa=toggle_status");
  19. static $__desc_status_v_on = array("", "", "enabled");
  20. static $__desc_status_v_off = array("", "", "disabled");
  21. static $__desc_usectl = Array("", "", "root_user");
  22. static $__desc_bproto = Array("", "", "root_user");
  23. static $__desc_peerdns = Array("", "", "dns_record_modify");
  24. static $__desc_clientname = Array("", "", "exclusive_client");
  25. static $__desc_gateway = Array("", "", "gateway");
  26. static $__desc_itype = Array("", "", "internet_type");
  27. static $__desc_ipv6init = Array("", "", "ipv6");
  28. static $__desc_syncserver = Array("", "", "syncserver");
  29. static $__desc_sslipaddress_o = Array("d", "", "syncserver");
  30. static $__desc_domainipaddress_o = Array("d", "", "syncserver");
  31. static $__desc_anonftpipaddress_o = Array("d", "", "syncserver");
  32. static $__rewrite_nname_const = Array("devname", "syncserver");
  33. static $__acdesc_update_update = array("", "", "edit");
  34. static $__acdesc_update_exclusive = array("", "", "exclusive_client");
  35. static $__desc_ipaddr_begin = Array("n", "", "ipaddress_begin", URL_SHOW);
  36. static $__desc_ipaddr_end = Array("n", "", "ipaddress_end", URL_SHOW);
  37. //Objects
  38. function display($var)
  39. {
  40. global $gbl, $sgbl, $login, $ghtml;
  41. if ($var === "devname") {
  42. if (csa($this->$var, "-")) {
  43. list($name, $num) = explode("-", $this->$var);
  44. return "$name:$num";
  45. }
  46. return $this->$var;
  47. }
  48. if ($var === 'clientname') {
  49. if (!$this->$var) {
  50. return 'Unassigned';
  51. }
  52. }
  53. if ($var === 'used_f') {
  54. $this->createGblIfNotExist();
  55. if (array_search_bool($this->ipaddr, $gbl->__var_ip_domainlist)) {
  56. return 'on';
  57. } else {
  58. return 'dull';
  59. }
  60. }
  61. return parent::display($var);
  62. }
  63. function createExtraVariables()
  64. {
  65. global $gbl, $sgbl, $login, $ghtml;
  66. $driverapp = $gbl->getSyncClass(null, $this->syncserver, 'dns');
  67. $this->__var_dnsdriver = $driverapp;
  68. }
  69. static function searchVar()
  70. {
  71. return "ipaddr";
  72. }
  73. function createGblIfNotExist()
  74. {
  75. global $gbl, $sgbl, $login, $ghtml;
  76. if (!isset($gbl->__var_ip_domainlist)) {
  77. $sq = new Sqlite($this->__masterserver, 'web');
  78. $res = $sq->getTable(array('ipaddress'));
  79. $list = get_namelist_from_arraylist($res, "ipaddress");
  80. $gbl->__var_ip_domainlist = $list;
  81. }
  82. }
  83. function isSelect()
  84. {
  85. global $gbl, $sgbl, $login, $ghtml;
  86. if (self::checkIfBaseAddress($this->devname)) {
  87. return false;
  88. }
  89. return true;
  90. if (!$sgbl->isKloxo()) {
  91. return true;
  92. }
  93. $this->createGblIfNotExist();
  94. if (array_search_bool($this->ipaddr, $gbl->__var_ip_domainlist)) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. function getOne()
  100. {
  101. $temp['devname'] = $this->devname;
  102. $temp['id'] = $this->id;
  103. $this->devname = implode('-', $temp);
  104. $temp = explode("-", $this->nname);
  105. $this->nname = $this->devname . "-" . $temp[1];
  106. }
  107. function getId()
  108. {
  109. return $this->ipaddr;
  110. }
  111. static function getIpList($parent)
  112. {
  113. global $gbl, $sgbl, $login, $ghtml;
  114. error_reporting(0);
  115. $ipl = $parent->getList("ipaddress");
  116. $i = 0;
  117. foreach ($ipl as $ip) {
  118. $list[$i] = $ip->devname;
  119. $i++;
  120. }
  121. $result = "";
  122. foreach ($list as $row) {
  123. list($devname, $id) = explode("-", $row);
  124. if (!isset($id) || $id === null || $id = "") {
  125. $result[] = $devname;
  126. }
  127. }
  128. return $result;
  129. }
  130. static function getLeastId($parent, $devname)
  131. {
  132. global $gbl, $sgbl, $login, $ghtml;
  133. print(" this is the devname u passed -$devname ");
  134. // I have removed the sorting from getlist, and here earlier, the result was sorted according to 'nname'.
  135. // this is needed only when you add a new device, so ignoring now.
  136. $list1 = $parent->getList("ipaddress");
  137. $llist = get_namelist_from_objectlist($list1, "devname");
  138. // dprintr($llist);
  139. for ($i = 0; $i < 1000000; $i++) {
  140. $name = "$devname:$i";
  141. if (!array_search_bool($name, $llist)) {
  142. return "$devname-$i";
  143. }
  144. }
  145. }
  146. function updateRecord($result)
  147. {
  148. $this->devname = $result['devname'];
  149. $this->netmask = $result['netmask'];
  150. $this->status = $result['status'];
  151. $this->ipaddr = $result['ipaddr'];
  152. $this->gateway = $result['gateway'];
  153. if (!isset($result['client_num'])) {
  154. $this->client_num = null;
  155. } else {
  156. $this->client_num = $result['client_num'];
  157. }
  158. if (!isset($result['shared'])) {
  159. $this->shared = "yes";
  160. } else {
  161. $this->shared = $result['shared'];
  162. }
  163. if (!isset($result['userctl'])) {
  164. $this->userctl = null;
  165. } else {
  166. $this->userctl = $result['userctl'];
  167. }
  168. if (!isset($result['itype'])) {
  169. $this->itype = null;
  170. } else {
  171. $this->itype = $result['itype'];
  172. }
  173. if (!isset($resutl['ipv6init'])) {
  174. $this->ipv6init = null;
  175. } else {
  176. $this->ipv6init = $result['ipv6init'];
  177. }
  178. if (!isset($resutl['peerdns'])) {
  179. $this->peerdns = null;
  180. } else {
  181. $this->peerdns = $result['peerdns'];
  182. }
  183. $this->dbaction = "update";
  184. }
  185. static function isValidIpaddress($ip)
  186. {
  187. return validate_ipaddress($ip);
  188. }
  189. // Temporary hack... Ipaddress doesn't contain a show at all. So just printing....
  190. // (Later...) this is not the actual hack. This is the normal way. If there is no other stuff in 'show',
  191. // we compeltely avoid the 'edit' link and directly do the editing in teh show page itself...
  192. function createShowUpdateform()
  193. {
  194. $uflist['update'] = null;
  195. return $uflist;
  196. }
  197. function createShowPropertyList(&$alist)
  198. {
  199. global $gbl, $sgbl, $login, $ghtml;
  200. $alist['property'][] = 'a=show';
  201. if ($sgbl->isKloxo() && !$this->getParentO()->isClass('pserver')) {
  202. $alist['property'][] = 'a=show&o=sslipaddress';
  203. $alist['property'][] = 'a=show&o=domainipaddress';
  204. if ($this->getParentO()->isAdmin()) {
  205. $alist['property'][] = "a=updateform&sa=exclusive";
  206. }
  207. //$alist = null;
  208. }
  209. }
  210. function createShowAlist(&$alist, $subaction = null)
  211. {
  212. global $gbl, $sgbl, $login, $ghtml;
  213. return $alist;
  214. }
  215. static function createListAlist($parent, $class)
  216. {
  217. global $gbl, $sgbl, $login, $ghtml;
  218. $alist[] = "a=list&c=$class";
  219. if ($parent->isClass('pserver')) {
  220. $alist[] = "a=addform&c=$class";
  221. $alist[] = "a=update&sa=readipaddress";
  222. }
  223. return $alist;
  224. }
  225. static function createListNlist($parent, $view)
  226. {
  227. //$nlist["nname"] = "3%";
  228. global $gbl, $sgbl, $login, $ghtml;
  229. if ($sgbl->isKloxo()) {
  230. //$nlist["used_f"] = "5%";
  231. }
  232. $nlist["ipaddr"] = "100%";
  233. if (!$parent->isClass('pserver')) {
  234. $nlist['syncserver'] = '10%';
  235. }
  236. $nlist["devname"] = "30%";
  237. if ($sgbl->isKloxo() && $parent->isAdmin()) {
  238. $nlist["clientname"] = "30%";
  239. }
  240. return $nlist;
  241. }
  242. function update($subaction, $param)
  243. {
  244. if ($subaction === 'toggle_status') {
  245. return $param;
  246. }
  247. if ($subaction === 'delete') {
  248. return $param;
  249. }
  250. if ($subaction === 'exclusive') {
  251. return $param;
  252. }
  253. self::VerifyString($this->getParentO(), $param);
  254. return $param;
  255. }
  256. function isAction($var)
  257. {
  258. if ($var === 'status') {
  259. if ($this->devname === 'eth0') {
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. static function chekWhetherToBlock($ip)
  266. {
  267. global $gbl, $sgbl, $login, $ghtml;
  268. $blockipl = $gbl->getList("blockidip");
  269. $i = 0;
  270. foreach ($blockipl as $block) {
  271. $blockip = explode("/", $block);
  272. if (isset($blockip[1])) {
  273. $result[$i] = self::chekIsExists($block, $ip);
  274. } else {
  275. $result[$i] = self:: compareIp($blockip[0], $ip);
  276. }
  277. $i++;
  278. }
  279. foreach ($result as $res) {
  280. if ($res === 1) {
  281. return 1;
  282. } else {
  283. return 0;
  284. }
  285. }
  286. }
  287. static function copyCertificate($devname, $machinename)
  288. {
  289. global $sgbl;
  290. $ppath = $sgbl->__path_program_root;
  291. $spath = $sgbl->__path_ssl_root;
  292. $name = $devname . "___" . $machinename;
  293. $name = sslcert::getSslCertnameFromIP($name);
  294. if (!lxfile_exists("__path_ssl_root")) {
  295. lxfile_mkdir("__path_ssl_root");
  296. }
  297. if (!lxfile_exists("{$spath}/$name.crt")) {
  298. lxfile_cp("{$ppath}/file/default.crt", "{$spath}/$name.crt");
  299. }
  300. if (!lxfile_exists("{$spath}/$name.key")) {
  301. lxfile_cp("{$ppath}/file/default.key", "{$spath}/$name.key");
  302. }
  303. if (!lxfile_exists("{$spath}/$name.ca")) {
  304. lxfile_cp("{$ppath}/file/default.ca", "{$spath}/$name.ca");
  305. }
  306. // MR -- add for missing (lighttpd error when select because need .pem file
  307. if (!lxfile_exists("{$spath}/$name.pem")) {
  308. // if (!lxfile_exists("{$ppath}/file/default.pem")) {
  309. /*
  310. // MR -- look like not work!
  311. $contentscer = lfile_get_contents("__path_program_root/file/default.crt");
  312. $contentskeyl = file_get_contents("__path_program_root/file/default.key");
  313. $contentpem = "$contentscer\n$contentskey";
  314. lfile_put_contents("__path_program_root/file/default.pem");
  315. */
  316. // }
  317. exec("cat {$ppath}/file/default.crt {$ppath}/file/default.key > {$ppath}/file/default.pem");
  318. lxfile_cp("{$ppath}/file/default.pem", "{$spath}/$name.pem");
  319. }
  320. }
  321. static function chekIsExists($blockip, $ip)
  322. {
  323. $string = self::checkvalidity($blockip);
  324. list($v, $ipaddr, $num) = explode("-", $string);
  325. if ($v != 1) {
  326. return 0;
  327. }
  328. $netmaskl = self::findbits($num);
  329. $netmask = implode(".", $netmaskl);
  330. $localNetwork = self::doAndOperation($ipaddr, $netmask);
  331. $remoteNetwork = self::doAndOperation($ip, $netmask);
  332. if (($localNetwork === $ipaddr) && ($remoteNetwork === $localNetwork)) {
  333. return 1;
  334. } else {
  335. if ($localNetwork === $remoteNetwork) {
  336. $res = self::compareIp($ip, $ipaddr);
  337. return $res;
  338. } else {
  339. return 0;
  340. }
  341. }
  342. }
  343. static function compareIp($ip, $ipaddr)
  344. {
  345. if ($ip === $ipaddr) {
  346. return 1;
  347. } else {
  348. return 0;
  349. }
  350. }
  351. static function doAndOperation($ipaddr, $netmask)
  352. {
  353. $ipaddrl = explode(".", $ipaddr);
  354. $netmaskl = explode(".", $netmask);
  355. $i = 0;
  356. foreach ($ipaddrl as $row) {
  357. $ipaddr_binary[$i] = str_pad(base_convert($row, 10, 2), 8, '0', STR_PAD_LEFT);
  358. $i++;
  359. }
  360. $i = 0;
  361. foreach ($netmaskl as $row) {
  362. $netmask_binary[$i] = str_pad(base_convert($row, 10, 2), 8, '0', STR_PAD_LEFT);
  363. $i++;
  364. }
  365. for ($i = 0; $i < 4; $i++) {
  366. $converted[$i] = ($ipaddr_binary[$i] & $netmask_binary[$i]);
  367. $converted1[$i] = base_convert($converted[$i], 2, 10);
  368. }
  369. $networkaddress = implode(".", $converted1);
  370. return $networkaddress;
  371. }
  372. static function findbits($mask)
  373. {
  374. for ($i = 0; $i < 32; $i++) {
  375. if ($i < $mask) {
  376. $tbits[$i] = 1;
  377. } else {
  378. $tbits[$i] = 0;
  379. }
  380. }
  381. $bytes = array_chunk($tbits, 8, true);
  382. foreach ($bytes as $b) {
  383. $list[] = base_convert(implode("", $b), 2, 10);
  384. }
  385. return $list;
  386. }
  387. static function checkvalidity($blockip)
  388. {
  389. $blist = explode("/", $blockip);
  390. $ipaddr = $blist[0];
  391. $num = $blist[1];
  392. $iplist = explode(".", $ipaddr);
  393. $c = 0;
  394. if (($iplist[0] >= 1) && ($iplist[0] <= 126) && ($iplist[0] != 127) && ($num >= 8) && ($num != 9) && ($num <= 15)) {
  395. $v = 1;
  396. } else {
  397. if (($iplist[0] >= 128) && ($iplist[0] <= 191) && ($num >= 16) && ($num != 17) && ($num <= 23)) {
  398. $v = 1;
  399. } else {
  400. if (($iplist[0] >= 192) && ($iplist[0] <= 223) && ($num >= 24) && ($num != 25) && ($num <= 32)) {
  401. $v = 1;
  402. } else {
  403. $v = 0;
  404. }
  405. }
  406. }
  407. return $v . "-" . $ipaddr . "-" . $num;
  408. }
  409. function updateform($subaction, $param)
  410. {
  411. if ($subaction === 'update') {
  412. $vlist['devname'] = array("M", $this->devname);
  413. $vlist['ipaddr'] = array('M', $this->ipaddr);
  414. // MR -- range ip still not work
  415. // $vlist['ipaddr_begin'] = array('M', $this->ipaddr_begin);
  416. // $vlist['ipaddr_end'] = array('M', $this->ipaddr_end);
  417. $vlist['netmask'] = array('M', $this->netmask);
  418. $vlist['gateway'] = array('M', $this->gateway);
  419. $vlist['__v_button'] = "";
  420. } else {
  421. if ($subaction === "exclusive") {
  422. $db = new Sqlite($this->__masterserver, "client");
  423. $list = $db->getTable(array("nname"));
  424. $list = get_namelist_from_arraylist($list);
  425. $list = lx_merge_good('--unassigned--', $list);
  426. $vlist['clientname'] = array('s', $list);
  427. }
  428. }
  429. return $vlist;
  430. }
  431. function isSync()
  432. {
  433. global $gbl, $sgbl, $login, $ghtml;
  434. if (!$login->isAdmin()) {
  435. $this->subaction = 'clean';
  436. return false;
  437. }
  438. if ($this->subaction === 'exclusive') {
  439. return false;
  440. }
  441. return true;
  442. }
  443. static function VerifyString($parent, $param)
  444. {
  445. if (!self::isValidIpaddress($param['ipaddr'])) {
  446. throw new lxexception("ipaddress_invalid", 'ipaddr');
  447. }
  448. /*
  449. // MR -- range ip still not work
  450. if (!self::isValidIpaddress($param['ipaddr_begin'])) {
  451. throw new lxexception("ipaddress_invalid", 'ipaddr_begin');
  452. }
  453. if (!self::isValidIpaddress($param['ipaddr_end'])) {
  454. throw new lxexception("ipaddress_invalid", 'ipaddr_end');
  455. }
  456. */
  457. if ($param['gateway']) {
  458. if (!self::isValidIpaddress($param['gateway'])) {
  459. throw new lxexception("gateway_invalid", 'gateway');
  460. }
  461. }
  462. if (!self::isValidIpaddress($param['netmask'])) {
  463. throw new lxexception("netmask_invalid", 'netmask');
  464. }
  465. $sq = new Sqlite($parent->__masterserver, "ipaddress");
  466. $res = $sq->getRowsWhere("syncserver = '$parent->nname'");
  467. $list = get_namelist_from_arraylist($res, "ipaddr");
  468. if (array_search_bool($param['ipaddr'], $list)) {
  469. throw new lxexception("ipaddress_already_configured", 'ipaddr');
  470. }
  471. $ret = lxshell_return("ping", "-n", "-c", "1", "-w", "5", $param['ipaddr']);
  472. if (!$ret) {
  473. throw new lxexception("some_other_host_uses_this_ip", 'ipaddr');
  474. }
  475. /*
  476. // MR -- range ip still not work
  477. for ($a = $param['ipaddr_begin']; $a <= $param['ipaddr_end']; &a++) {
  478. $ret = lxshell_return("ping", "-n", "-c", "1", "-w", "5", $a);
  479. if (!$ret) {
  480. throw new lxexception("some_other_host_uses_this_ip", $a);
  481. }
  482. }
  483. */
  484. }
  485. function postAdd()
  486. {
  487. $domainip = new DomainIpaddress(null, $this->syncserver, $this->nname);
  488. $domainip->get();
  489. $this->addObject('domainipaddress', $domainip);
  490. }
  491. static function add($parent, $class, $param)
  492. {
  493. $dev = $param['devname'];
  494. if (!isset($param['netmask'])) {
  495. $param['netmask'] = "255.255.255.0";
  496. }
  497. self::VerifyString($parent, $param);
  498. $param['devname'] = self::getLeastId($parent, $param['devname']);
  499. $param['gateway'] = "";
  500. $param['syncserver'] = $parent->nname;
  501. $param['status'] = 'on';
  502. return $param;
  503. }
  504. static function addform($parent, $class, $typetd = null)
  505. {
  506. $result = self::getIpList($parent);
  507. $vlist['devname'] = array('s', $result);
  508. $vlist['ipaddr'] = "";
  509. // MR -- range ip still not work
  510. // $vlist['ipaddr_begin'] = "";
  511. // $vlist['ipaddr_end'] = "";
  512. $vlist['netmask'] = array('m', '255.255.255.0');
  513. $ret['variable'] = $vlist;
  514. $ret['action'] = "add";
  515. return $ret;
  516. }
  517. static function fixstatus($result)
  518. {
  519. $i = 0;
  520. $result2 = null;
  521. foreach ($result as $row) {
  522. if ($row['ipaddr'] === '127.0.0.1') {
  523. continue;
  524. }
  525. if ($row['devname'] === "lo") {
  526. continue;
  527. }
  528. if ($row['status'] === "yes") {
  529. $row['status'] = "on";
  530. } else {
  531. $row['status'] = "off";
  532. }
  533. $result2[] = $row;
  534. }
  535. return $result2;
  536. }
  537. static function checkIfBaseAddress($name)
  538. {
  539. return !csa($name, "-");
  540. }
  541. function setNoClients($no)
  542. {
  543. $this->clients_no = $no;
  544. $this->dbaction = "update";
  545. }
  546. function setClients($string)
  547. {
  548. $this->ser_clientslist = $string;
  549. $this->dbaction = "update";
  550. }
  551. static function initThisListRule($parent, $class)
  552. {
  553. if ($parent->isAdmin()) {
  554. $res = '__v_table';
  555. } else {
  556. if ($parent->isClass('pserver')) {
  557. $res[] = array('parent_clname', '=', "'{$parent->getClName()}'");
  558. } else {
  559. $res[] = array('clientname', '=', "'{$parent->nname}'");
  560. }
  561. }
  562. return $res;
  563. }
  564. }