sql.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. if (! defined("CONFIG")) die("Not defined");
  3. if (! defined("SQL")) { die("Not defined"); }
  4. function new_connection ($db_host, $db_user, $db_password, $db_name)
  5. {
  6. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  7. $result = mysqli_connect($db_host,$db_user,$db_password,$db_name);
  8. if (! $result) {
  9. echo "Error connect to MYSQL " . PHP_EOL;
  10. echo "Errno: " . mysqli_connect_errno() . PHP_EOL;
  11. echo "Error message: " . mysqli_connect_error() . PHP_EOL;
  12. exit();
  13. }
  14. /* enable utf8 */
  15. if (!mysqli_set_charset($result,'utf8mb4')) {
  16. printf("Error loading utf8: %s\n", mysqli_error($result));
  17. exit();
  18. }
  19. //mysqli_options($result, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
  20. return $result;
  21. }
  22. function run_sql($db, $query)
  23. {
  24. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  25. unset($matches);
  26. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  27. if (!empty($matches[1])) {
  28. if (!allow_update($matches[1], 'del')) {
  29. LOG_DEBUG($db, "Access denied: $query ");
  30. return;
  31. }
  32. }
  33. unset($matches);
  34. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  35. if (!empty($matches[1])) {
  36. if (!allow_update($matches[1], 'add')) {
  37. LOG_DEBUG($db, "Access denied: $query ");
  38. return;
  39. }
  40. }
  41. unset($matches);
  42. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  43. if (!empty($matches[1])) {
  44. if (!allow_update($matches[1], 'update')) {
  45. LOG_DEBUG($db, "Access denied: $query ");
  46. return;
  47. }
  48. }
  49. unset($matches);
  50. }
  51. $sql_result = mysqli_query($db, $query);
  52. if (!$sql_result) {
  53. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  54. return;
  55. }
  56. return $sql_result;
  57. }
  58. function get_count_records($db, $table, $filter)
  59. {
  60. if (!empty($filter)) {
  61. $filter = 'where ' . $filter;
  62. }
  63. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  64. list($count) = mysqli_fetch_array($t_count);
  65. if (!isset($count)) {
  66. $count = 0;
  67. }
  68. return $count;
  69. }
  70. function get_id_record($db, $table, $filter)
  71. {
  72. if (isset($filter)) {
  73. $filter = 'WHERE ' . $filter;
  74. }
  75. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  76. list($id) = mysqli_fetch_array($t_record);
  77. return $id;
  78. }
  79. function set_changed($db, $id)
  80. {
  81. $auth['changed'] = 1;
  82. update_record($db, "User_auth", "id=" . $id, $auth);
  83. }
  84. //action: add,update,del
  85. function allow_update($table, $action = 'update', $field = '')
  86. {
  87. //always allow modification for tables
  88. if (preg_match('/(variables|dns_cache|worklog|sessions)/i', $table)) {
  89. return 1;
  90. }
  91. if (isset($_SESSION['login'])) {
  92. $work_user = $_SESSION['login'];
  93. }
  94. if (isset($_SESSION['user_id'])) {
  95. $work_id = $_SESSION['user_id'];
  96. }
  97. if (isset($_SESSION['acl'])) {
  98. $user_level = $_SESSION['acl'];
  99. }
  100. if (!isset($work_user) or !isset($work_id) or empty($user_level)) {
  101. return 0;
  102. }
  103. //always allow Administrator
  104. if ($user_level == 1) {
  105. return 1;
  106. }
  107. //always forbid ViewOnly
  108. if ($user_level == 3) {
  109. return 0;
  110. }
  111. //allow tables for Operator
  112. if (preg_match('/(dns_queue|User_auth_alias)/i', $table)) {
  113. return 1;
  114. }
  115. if ($action == 'update') {
  116. $operator_acl = [
  117. 'User_auth' => [
  118. 'comments' => '1',
  119. 'dns_name' => '1',
  120. 'firmware' => '1',
  121. 'link_check' => '1',
  122. 'nagios' => '1',
  123. 'nagios_handler' => '1',
  124. 'Wikiname' => '1'
  125. ],
  126. 'User_list' => [
  127. 'fio' => '1',
  128. 'login' => '1',
  129. ],
  130. ];
  131. if (!isset($operator_acl[$table])) {
  132. return 0;
  133. }
  134. if (isset($operator_acl[$table]) and empty($field)) {
  135. return 1;
  136. }
  137. if (!isset($operator_acl[$table][$field])) {
  138. return 0;
  139. }
  140. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field] == '0') {
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. function get_record_field($db, $table, $field, $filter)
  148. {
  149. if (!isset($table)) {
  150. LOG_ERROR($db, "Search in unknown table! Skip command.");
  151. return;
  152. }
  153. if (!isset($filter)) {
  154. LOG_ERROR($db, "Search filter is empty! Skip command.");
  155. return;
  156. }
  157. if (!isset($field)) {
  158. LOG_ERROR($db, "Search field is empty! Skip command.");
  159. return;
  160. }
  161. if (preg_match('/=$/', $filter)) {
  162. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  163. return;
  164. }
  165. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  166. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  167. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  168. foreach ($old as $key => $value) {
  169. if (!isset($value) or $value === 'NULL') {
  170. $value = '';
  171. }
  172. $result[$key] = $value;
  173. }
  174. return $result[$field];
  175. }
  176. function get_record($db, $table, $filter)
  177. {
  178. if (!isset($table)) {
  179. LOG_ERROR($db, "Search in unknown table! Skip command.");
  180. return;
  181. }
  182. if (!isset($filter)) {
  183. LOG_ERROR($db, "Search filter is empty! Skip command.");
  184. return;
  185. }
  186. if (preg_match('/=$/', $filter)) {
  187. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  188. return;
  189. }
  190. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  191. $get_record = mysqli_query($db, $get_sql);
  192. if (!$get_record) {
  193. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  194. return;
  195. }
  196. $fields = [];
  197. while ($field = mysqli_fetch_field($get_record)) {
  198. $f_table = $field->table;
  199. $f_name = $field->name;
  200. $fields[$f_table][$f_name] = $field;
  201. }
  202. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  203. $result = NULL;
  204. if (!empty($record)) {
  205. foreach ($record as $key => $value) {
  206. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  207. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  208. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  209. $value = 0;
  210. }
  211. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  212. $value = '';
  213. }
  214. }
  215. }
  216. if (!empty($key)) {
  217. $result[$key] = $value;
  218. }
  219. }
  220. }
  221. return $result;
  222. }
  223. function get_records($db, $table, $filter)
  224. {
  225. if (!isset($table)) {
  226. LOG_ERROR($db, "Search in unknown table! Skip command.");
  227. return;
  228. }
  229. if (isset($filter) and preg_match('/=$/', $filter)) {
  230. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  231. return;
  232. }
  233. $s_filter = '';
  234. if (isset($filter)) {
  235. $s_filter = 'WHERE ' . $filter;
  236. }
  237. $get_sql = "SELECT * FROM $table $s_filter";
  238. $get_record = mysqli_query($db, $get_sql);
  239. if (!$get_record) {
  240. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  241. return;
  242. }
  243. $fields = [];
  244. while ($field = mysqli_fetch_field($get_record)) {
  245. $f_table = $field->table;
  246. $f_name = $field->name;
  247. $fields[$f_table][$f_name] = $field;
  248. }
  249. $result = NULL;
  250. $index = 0;
  251. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  252. foreach ($rec as $key => $value) {
  253. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  254. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  255. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  256. $value = 0;
  257. }
  258. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  259. $value = '';
  260. }
  261. }
  262. }
  263. $result[$index][$key] = $value;
  264. }
  265. $index++;
  266. }
  267. return $result;
  268. }
  269. function get_records_sql($db, $sql)
  270. {
  271. $result = NULL;
  272. if (empty($sql)) {
  273. LOG_ERROR($db, "Empty query! Skip command.");
  274. return $result;
  275. }
  276. $records = mysqli_query($db, $sql);
  277. if (!$records) {
  278. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  279. return $result;
  280. }
  281. $fields = [];
  282. //we assume that fields with the same name have the same type
  283. while ($field = mysqli_fetch_field($records)) {
  284. $f_name = $field->name;
  285. $fields[$f_name] = $field;
  286. }
  287. $index = 0;
  288. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  289. foreach ($rec as $key => $value) {
  290. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  291. if (!empty($key) and !empty($fields[$key])) {
  292. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  293. $value = 0;
  294. }
  295. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  296. $value = '';
  297. }
  298. }
  299. }
  300. if (!empty($key)) {
  301. $result[$index][$key] = $value;
  302. }
  303. }
  304. $index++;
  305. }
  306. return $result;
  307. }
  308. function get_record_sql($db, $sql)
  309. {
  310. $result = NULL;
  311. if (!isset($sql)) {
  312. LOG_ERROR($db, "Empty query! Skip command.");
  313. return $result;
  314. }
  315. $record = mysqli_query($db, $sql . " LIMIT 1");
  316. if (!isset($record)) {
  317. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  318. return $result;
  319. }
  320. $fields = [];
  321. //we assume that fields with the same name have the same type
  322. while ($field = mysqli_fetch_field($record)) {
  323. $f_name = $field->name;
  324. $fields[$f_name] = $field;
  325. }
  326. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  327. if (!empty($rec)) {
  328. foreach ($rec as $key => $value) {
  329. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  330. if (!empty($key) and !empty($fields[$key])) {
  331. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  332. $value = 0;
  333. }
  334. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  335. $value = '';
  336. }
  337. }
  338. }
  339. if (!empty($key)) {
  340. $result[$key] = $value;
  341. }
  342. }
  343. }
  344. return $result;
  345. }
  346. function update_record($db, $table, $filter, $newvalue)
  347. {
  348. if (!isset($table)) {
  349. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  350. return;
  351. }
  352. if (!isset($filter)) {
  353. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  354. return;
  355. }
  356. if (preg_match('/=$/', $filter)) {
  357. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  358. return;
  359. }
  360. if (!isset($newvalue)) {
  361. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  362. return;
  363. }
  364. if (!allow_update($table, 'update')) {
  365. LOG_INFO($db, "Access denied: $table [ $filter ]");
  366. return 1;
  367. }
  368. $old_sql = "SELECT * FROM $table WHERE $filter";
  369. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  370. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  371. $rec_id = NULL;
  372. if (!empty($old['id'])) {
  373. $rec_id = $old['id'];
  374. }
  375. $changed_log = '';
  376. $run_sql = '';
  377. $network_changed = 0;
  378. $dhcp_changed = 0;
  379. $dns_changed = 0;
  380. $acl_fields = [
  381. 'ip' => '1',
  382. 'ip_int' => '1',
  383. 'enabled' => '1',
  384. 'dhcp' => '1',
  385. 'filter_group_id' => '1',
  386. 'deleted' => '1',
  387. 'dhcp_acl' => '1',
  388. 'queue_id' => '1',
  389. 'mac' => '1',
  390. 'blocked' => '1',
  391. ];
  392. $dhcp_fields = [
  393. 'ip' => '1',
  394. 'dhcp' => '1',
  395. 'deleted' => '1',
  396. 'dhcp_option_set' =>'1',
  397. 'dhcp_acl' => '1',
  398. 'mac' => '1',
  399. ];
  400. $dns_fields = [
  401. 'ip' => '1',
  402. 'dns_name' => '1',
  403. 'alias' => '1',
  404. ];
  405. foreach ($newvalue as $key => $value) {
  406. if (!allow_update($table, 'update', $key)) {
  407. continue;
  408. }
  409. if (!isset($value)) {
  410. $value = '';
  411. }
  412. $value = trim($value);
  413. if (strcmp($old[$key], $value) == 0) {
  414. continue;
  415. }
  416. if ($table === "User_auth") {
  417. if (!empty($acl_fields["$key"])) {
  418. $network_changed = 1;
  419. }
  420. if (!empty($dhcp_fields["$key"])) {
  421. $dhcp_changed = 1;
  422. }
  423. if (!empty($dns_fields["$key"])) {
  424. $dns_changed = 1;
  425. }
  426. }
  427. if ($table === "User_auth_alias") {
  428. if (!empty($dns_fields["$key"])) {
  429. $dns_changed = 1;
  430. }
  431. }
  432. if (!preg_match('/password/i', $key)) {
  433. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  434. }
  435. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  436. }
  437. if ($table === "User_auth" and $dns_changed) {
  438. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  439. $del_dns['name_type'] = 'A';
  440. $del_dns['name'] = $old['dns_name'];
  441. $del_dns['value'] = $old['ip'];
  442. $del_dns['type'] = 'del';
  443. if (!empty($rec_id)) {
  444. $del_dns['auth_id'] = $rec_id;
  445. }
  446. insert_record($db, 'dns_queue', $del_dns);
  447. }
  448. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  449. $new_dns['name_type'] = 'A';
  450. $new_dns['name'] = $newvalue['dns_name'];
  451. $new_dns['value'] = $newvalue['ip'];
  452. $new_dns['type'] = 'add';
  453. if (!empty($rec_id)) {
  454. $new_dns['auth_id'] = $rec_id;
  455. }
  456. insert_record($db, 'dns_queue', $new_dns);
  457. }
  458. }
  459. if ($table === "User_auth_alias" and $dns_changed) {
  460. $auth_id = NULL;
  461. if ($old['auth_id']) {
  462. $auth_id = $old['auth_id'];
  463. }
  464. if (!empty($old['alias'])) {
  465. $del_dns['name_type'] = 'CNAME';
  466. $del_dns['name'] = $old['alias'];
  467. $del_dns['type'] = 'del';
  468. if (!empty($auth_id)) {
  469. $del_dns['auth_id'] = $auth_id;
  470. $del_dns['value'] = get_dns_name($db, $auth_id);
  471. }
  472. insert_record($db, 'dns_queue', $del_dns);
  473. }
  474. if (!empty($newvalue['alias'])) {
  475. $new_dns['name_type'] = 'CNAME';
  476. $new_dns['name'] = $newvalue['alias'];
  477. $new_dns['type'] = 'add';
  478. if (!empty($auth_id)) {
  479. $new_dns['auth_id'] = $auth_id;
  480. $new_dns['value'] = get_dns_name($db, $auth_id);
  481. }
  482. insert_record($db, 'dns_queue', $new_dns);
  483. }
  484. }
  485. if (empty($run_sql)) {
  486. return 1;
  487. }
  488. if ($network_changed) {
  489. $run_sql = $run_sql . " `changed`='1',";
  490. }
  491. if ($dhcp_changed) {
  492. $run_sql = $run_sql . " `dhcp_changed`='1',";
  493. }
  494. $changed_log = substr_replace($changed_log, "", -1);
  495. $run_sql = substr_replace($run_sql, "", -1);
  496. if ($table === 'User_auth') {
  497. $changed_time = GetNowTimeString();
  498. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  499. }
  500. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  501. LOG_DEBUG($db, "Run sql: $new_sql");
  502. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  503. if (!$sql_result) {
  504. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  505. return;
  506. }
  507. if ($table !== "sessions") {
  508. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  509. }
  510. return $sql_result;
  511. }
  512. function delete_record($db, $table, $filter)
  513. {
  514. if (!allow_update($table, 'del')) {
  515. LOG_WARNING($db, "User does not have write permission");
  516. return;
  517. }
  518. if (!isset($table)) {
  519. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  520. return;
  521. }
  522. if (!isset($filter)) {
  523. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  524. return;
  525. }
  526. if (preg_match('/=$/', $filter)) {
  527. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  528. return;
  529. }
  530. $old_sql = "SELECT * FROM $table WHERE $filter";
  531. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  532. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  533. $rec_id = NULL;
  534. if (!empty($old['id'])) {
  535. $rec_id = $old['id'];
  536. }
  537. $changed_log = 'record: ';
  538. if (!empty($old)) {
  539. asort($old, SORT_STRING);
  540. $old = array_reverse($old, 1);
  541. foreach ($old as $key => $value) {
  542. if (empty($value)) {
  543. continue;
  544. }
  545. if (preg_match('/action/', $key)) {
  546. continue;
  547. }
  548. if (preg_match('/status/', $key)) {
  549. continue;
  550. }
  551. if (preg_match('/time/', $key)) {
  552. continue;
  553. }
  554. if (preg_match('/found/', $key)) {
  555. continue;
  556. }
  557. $changed_log = $changed_log . " $key => $value,";
  558. }
  559. }
  560. $delete_it = 1;
  561. //never delete user ip record
  562. if ($table === 'User_auth') {
  563. $delete_it = 0;
  564. $changed_time = GetNowTimeString();
  565. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  566. LOG_DEBUG($db, "Run sql: $new_sql");
  567. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  568. if (!$sql_result) {
  569. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  570. return;
  571. }
  572. //dns
  573. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  574. $del_dns['name_type'] = 'A';
  575. $del_dns['name'] = $old['dns_name'];
  576. $del_dns['value'] = $old['ip'];
  577. $del_dns['type'] = 'del';
  578. if (!empty($rec_id)) {
  579. $del_dns['auth_id'] = $rec_id;
  580. }
  581. insert_record($db, 'dns_queue', $del_dns);
  582. }
  583. LOG_VERBOSE($db, "Deleted FROM table $table WHERE $filter $changed_log");
  584. return $changed_log;
  585. }
  586. //never delete permanent user
  587. if ($table === 'User_list' and $old['permanent']) { return; }
  588. //remove aliases
  589. if ($table === 'User_auth_alias') {
  590. //dns
  591. if (!empty($old['alias'])) {
  592. $del_dns['name_type'] = 'CNAME';
  593. $del_dns['name'] = $old['alias'];
  594. $del_dns['value'] = '';
  595. $del_dns['type'] = 'del';
  596. if (!empty($old['auth_id'])) {
  597. $del_dns['auth_id'] = $old['auth_id'];
  598. $del_dns['value'] = get_dns_name($db, $old['auth_id']);
  599. }
  600. insert_record($db, 'dns_queue', $del_dns);
  601. }
  602. }
  603. if ($delete_it) {
  604. $new_sql = "DELETE FROM $table WHERE $filter";
  605. LOG_DEBUG($db, "Run sql: $new_sql");
  606. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  607. if (!$sql_result) {
  608. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  609. return;
  610. }
  611. } else { return; }
  612. if ($table !== "sessions") {
  613. LOG_VERBOSE($db, "Deleted FROM table $table WHERE $filter $changed_log");
  614. }
  615. return $changed_log;
  616. }
  617. function insert_record($db, $table, $newvalue)
  618. {
  619. if (!allow_update($table, 'add')) {
  620. LOG_WARNING($db, "User does not have write permission");
  621. return;
  622. }
  623. if (!isset($table)) {
  624. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  625. return;
  626. }
  627. if (empty($newvalue)) {
  628. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  629. return;
  630. }
  631. $changed_log = '';
  632. $field_list = '';
  633. $value_list = '';
  634. foreach ($newvalue as $key => $value) {
  635. if (empty($value) and $value != '0') {
  636. $value = '';
  637. }
  638. if (!preg_match('/password/i', $key)) {
  639. $changed_log = $changed_log . " $key => $value,";
  640. }
  641. $field_list = $field_list . "`" . $key . "`,";
  642. $value = trim($value);
  643. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  644. }
  645. if (empty($value_list)) {
  646. return;
  647. }
  648. $changed_log = substr_replace($changed_log, "", -1);
  649. $field_list = substr_replace($field_list, "", -1);
  650. $value_list = substr_replace($value_list, "", -1);
  651. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  652. LOG_DEBUG($db, "Run sql: $new_sql");
  653. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  654. if (!$sql_result) {
  655. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  656. return;
  657. }
  658. $last_id = mysqli_insert_id($db);
  659. if ($table !== "sessions") {
  660. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  661. }
  662. if ($table === 'User_auth') {
  663. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  664. }
  665. if ($table === 'User_auth_alias') {
  666. //dns
  667. if (!empty($newvalue['alias'])) {
  668. $add_dns['name_type'] = 'CNAME';
  669. $add_dns['name'] = $newvalue['alias'];
  670. $add_dns['value'] = get_dns_name($db, $newvalue['auth_id']);
  671. $add_dns['type'] = 'add';
  672. $add_dns['auth_id'] = $newvalue['auth_id'];
  673. insert_record($db, 'dns_queue', $add_dns);
  674. }
  675. }
  676. if ($table === 'User_auth') {
  677. //dns
  678. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  679. $add_dns['name_type'] = 'A';
  680. $add_dns['name'] = $newvalue['dns_name'];
  681. $add_dns['value'] = $newvalue['ip'];
  682. $add_dns['type'] = 'add';
  683. $add_dns['auth_id'] = $last_id;
  684. insert_record($db, 'dns_queue', $add_dns);
  685. }
  686. }
  687. return $last_id;
  688. }
  689. function dump_record($db, $table, $filter)
  690. {
  691. $result = '';
  692. $old = get_record($db, $table, $filter);
  693. if (empty($old)) {
  694. return $result;
  695. }
  696. $result = 'record: ' . get_rec_str($old);
  697. return $result;
  698. }
  699. function get_rec_str($array)
  700. {
  701. $result = '';
  702. foreach ($array as $key => $value) {
  703. $result .= "[" . $key . "]=" . $value . ", ";
  704. }
  705. $result = preg_replace('/,\s+$/', '', $result);
  706. return $result;
  707. }
  708. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  709. {
  710. if (!isset($table)) {
  711. return;
  712. }
  713. if (!isset($filter)) {
  714. return;
  715. }
  716. if (!isset($newvalue)) {
  717. return;
  718. }
  719. if (!isset($only_changed)) {
  720. $only_changed = 0;
  721. }
  722. $old_sql = "SELECT * FROM $table WHERE $filter";
  723. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  724. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  725. $changed_log = "\r\n";
  726. foreach ($newvalue as $key => $value) {
  727. if (strcmp($old[$key], $value) !== 0) {
  728. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  729. }
  730. }
  731. $old_record = '';
  732. if (!$only_changed) {
  733. $old_record = "\r\n Has not changed:\r\n";
  734. foreach ($old as $key => $value) {
  735. if (!empty($newvalue[$key])) {
  736. $old_record = $old_record . " $key = $value,\r\n";
  737. }
  738. }
  739. $old_record = substr_replace($old_record, "", -3);
  740. }
  741. // print $changed_log;
  742. return $changed_log . $old_record;
  743. }
  744. function delete_user_auth($db, $id)
  745. {
  746. //remove aliases
  747. $t_User_auth_alias = get_records($db,'User_auth_alias',"auth_id=$id ORDER BY alias");
  748. if (!empty($t_User_auth_alias)) {
  749. foreach ( $t_User_auth_alias as $row ) {
  750. LOG_INFO($db, "Remove alias id: ".$row['id']." for auth_id: $id :: ".dump_record($db,'User_auth_alias','id='.$row['id']));
  751. delete_record($db,'User_auth_alias','id='.$row['id']);
  752. }
  753. }
  754. //remove connections
  755. run_sql($db, 'DELETE FROM connections WHERE auth_id=' . $id);
  756. //remove user auth record
  757. LOG_INFO($db, "Removed user auth_id: $id :: ".dump_record($db,'User_auth','id='.$id));
  758. $changes = delete_record($db, "User_auth", "id=" . $id);
  759. return $changes;
  760. }
  761. function delete_user($db,$id)
  762. {
  763. //remove user record
  764. $changes = delete_record($db, "User_list", "id=" . $id);
  765. //if fail - exit
  766. if (!isset($changes) or empty($changes)) { return; }
  767. //remove auth records
  768. $t_User_auth = get_records($db,'User_auth',"user_id=$id");
  769. if (!empty($t_User_auth)) {
  770. foreach ( $t_User_auth as $row ) { delete_user_auth($db,$row['id']); }
  771. }
  772. //remove device
  773. $device = get_record($db, "devices", "user_id='$id'");
  774. if (!empty($device)) {
  775. LOG_INFO($db, "Delete device for user id: $id ".dump_record($db,'devices','user_id='.$id));
  776. unbind_ports($db, $device['id']);
  777. run_sql($db, "DELETE FROM connections WHERE device_id=" . $device['id']);
  778. run_sql($db, "DELETE FROM device_l3_interfaces WHERE device_id=" . $device['id']);
  779. run_sql($db, "DELETE FROM device_ports WHERE device_id=" . $device['id']);
  780. run_sql($db, "DELETE FROM device_filter_instances WHERE device_id=" . $device['id']);
  781. run_sql($db, "DELETE FROM gateway_subnets WHERE device_id=".$device['id']);
  782. delete_record($db, "devices", "id=" . $device['id']);
  783. }
  784. //remove auth assign rules
  785. run_sql($db, "DELETE FROM auth_rules WHERE user_id=$id");
  786. return $changes;
  787. }
  788. function delete_device($db,$id)
  789. {
  790. LOG_INFO($db, "Try delete device id: $id ".dump_record($db,'devices','id='.$id));
  791. //remove user record
  792. $changes = delete_record($db, "devices", "id=" . $id);
  793. //if fail - exit
  794. if (!isset($changes) or empty($changes)) {
  795. LOG_INFO($db,"Device id: $id has not been deleted");
  796. return;
  797. }
  798. unbind_ports($db, $id);
  799. run_sql($db, "DELETE FROM connections WHERE device_id=" . $id);
  800. run_sql($db, "DELETE FROM device_l3_interfaces WHERE device_id=" . $id);
  801. run_sql($db, "DELETE FROM device_ports WHERE device_id=" . $id);
  802. run_sql($db, "DELETE FROM device_filter_instances WHERE device_id=" . $id);
  803. run_sql($db, "DELETE FROM gateway_subnets WHERE device_id=".$id);
  804. return $changes;
  805. }
  806. $db_link = new_connection(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  807. ?>