sql.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. 'mac' => '1',
  397. ];
  398. $dns_fields = [
  399. 'ip' => '1',
  400. 'dns_name' => '1',
  401. 'alias' => '1',
  402. ];
  403. foreach ($newvalue as $key => $value) {
  404. if (!allow_update($table, 'update', $key)) {
  405. continue;
  406. }
  407. if (!isset($value)) {
  408. $value = '';
  409. }
  410. $value = trim($value);
  411. if (strcmp($old[$key], $value) == 0) {
  412. continue;
  413. }
  414. if ($table === "User_auth") {
  415. if (!empty($acl_fields["$key"])) {
  416. $network_changed = 1;
  417. }
  418. if (!empty($dhcp_fields["$key"])) {
  419. $dhcp_changed = 1;
  420. }
  421. if (!empty($dns_fields["$key"])) {
  422. $dns_changed = 1;
  423. }
  424. }
  425. if ($table === "User_auth_alias") {
  426. if (!empty($dns_fields["$key"])) {
  427. $dns_changed = 1;
  428. }
  429. }
  430. if (!preg_match('/password/i', $key)) {
  431. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  432. }
  433. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  434. }
  435. if ($table === "User_auth" and $dns_changed) {
  436. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  437. $del_dns['name_type'] = 'A';
  438. $del_dns['name'] = $old['dns_name'];
  439. $del_dns['value'] = $old['ip'];
  440. $del_dns['type'] = 'del';
  441. if (!empty($rec_id)) {
  442. $del_dns['auth_id'] = $rec_id;
  443. }
  444. insert_record($db, 'dns_queue', $del_dns);
  445. }
  446. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  447. $new_dns['name_type'] = 'A';
  448. $new_dns['name'] = $newvalue['dns_name'];
  449. $new_dns['value'] = $newvalue['ip'];
  450. $new_dns['type'] = 'add';
  451. if (!empty($rec_id)) {
  452. $new_dns['auth_id'] = $rec_id;
  453. }
  454. insert_record($db, 'dns_queue', $new_dns);
  455. }
  456. }
  457. if ($table === "User_auth_alias" and $dns_changed) {
  458. $auth_id = NULL;
  459. if ($old['auth_id']) {
  460. $auth_id = $old['auth_id'];
  461. }
  462. if (!empty($old['alias'])) {
  463. $del_dns['name_type'] = 'CNAME';
  464. $del_dns['name'] = $old['alias'];
  465. $del_dns['type'] = 'del';
  466. if (!empty($auth_id)) {
  467. $del_dns['auth_id'] = $auth_id;
  468. $del_dns['value'] = get_dns_name($db, $auth_id);
  469. }
  470. insert_record($db, 'dns_queue', $del_dns);
  471. }
  472. if (!empty($newvalue['alias'])) {
  473. $new_dns['name_type'] = 'CNAME';
  474. $new_dns['name'] = $newvalue['alias'];
  475. $new_dns['type'] = 'add';
  476. if (!empty($auth_id)) {
  477. $new_dns['auth_id'] = $auth_id;
  478. $new_dns['value'] = get_dns_name($db, $auth_id);
  479. }
  480. insert_record($db, 'dns_queue', $new_dns);
  481. }
  482. }
  483. if (empty($run_sql)) {
  484. return 1;
  485. }
  486. if ($network_changed) {
  487. $run_sql = $run_sql . " `changed`='1',";
  488. }
  489. if ($dhcp_changed) {
  490. $run_sql = $run_sql . " `dhcp_changed`='1',";
  491. }
  492. $changed_log = substr_replace($changed_log, "", -1);
  493. $run_sql = substr_replace($run_sql, "", -1);
  494. if ($table === 'User_auth') {
  495. $changed_time = GetNowTimeString();
  496. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  497. }
  498. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  499. LOG_DEBUG($db, "Run sql: $new_sql");
  500. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  501. if (!$sql_result) {
  502. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  503. return;
  504. }
  505. if ($table !== "sessions") {
  506. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  507. }
  508. return $sql_result;
  509. }
  510. function delete_record($db, $table, $filter)
  511. {
  512. if (!allow_update($table, 'del')) {
  513. LOG_WARNING($db, "User does not have write permission");
  514. return;
  515. }
  516. if (!isset($table)) {
  517. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  518. return;
  519. }
  520. if (!isset($filter)) {
  521. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  522. return;
  523. }
  524. if (preg_match('/=$/', $filter)) {
  525. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  526. return;
  527. }
  528. $old_sql = "SELECT * FROM $table WHERE $filter";
  529. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  530. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  531. $rec_id = NULL;
  532. if (!empty($old['id'])) {
  533. $rec_id = $old['id'];
  534. }
  535. $changed_log = 'record: ';
  536. if (!empty($old)) {
  537. asort($old, SORT_STRING);
  538. $old = array_reverse($old, 1);
  539. foreach ($old as $key => $value) {
  540. if (empty($value)) {
  541. continue;
  542. }
  543. if (preg_match('/action/', $key)) {
  544. continue;
  545. }
  546. if (preg_match('/status/', $key)) {
  547. continue;
  548. }
  549. if (preg_match('/time/', $key)) {
  550. continue;
  551. }
  552. if (preg_match('/found/', $key)) {
  553. continue;
  554. }
  555. $changed_log = $changed_log . " $key => $value,";
  556. }
  557. }
  558. $delete_it = 1;
  559. //never delete user ip record
  560. if ($table === 'User_auth') {
  561. $delete_it = 0;
  562. $changed_time = GetNowTimeString();
  563. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  564. LOG_DEBUG($db, "Run sql: $new_sql");
  565. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  566. if (!$sql_result) {
  567. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  568. return;
  569. }
  570. //dns
  571. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  572. $del_dns['name_type'] = 'A';
  573. $del_dns['name'] = $old['dns_name'];
  574. $del_dns['value'] = $old['ip'];
  575. $del_dns['type'] = 'del';
  576. if (!empty($rec_id)) {
  577. $del_dns['auth_id'] = $rec_id;
  578. }
  579. insert_record($db, 'dns_queue', $del_dns);
  580. }
  581. LOG_VERBOSE($db, "Deleted FROM table $table WHERE $filter $changed_log");
  582. return $changed_log;
  583. }
  584. //never delete permanent user
  585. if ($table === 'User_list' and $old['permanent']) { return; }
  586. //remove aliases
  587. if ($table === 'User_auth_alias') {
  588. //dns
  589. if (!empty($old['alias'])) {
  590. $del_dns['name_type'] = 'CNAME';
  591. $del_dns['name'] = $old['alias'];
  592. $del_dns['value'] = '';
  593. $del_dns['type'] = 'del';
  594. if (!empty($old['auth_id'])) {
  595. $del_dns['auth_id'] = $old['auth_id'];
  596. $del_dns['value'] = get_dns_name($db, $old['auth_id']);
  597. }
  598. insert_record($db, 'dns_queue', $del_dns);
  599. }
  600. }
  601. if ($delete_it) {
  602. $new_sql = "DELETE FROM $table WHERE $filter";
  603. LOG_DEBUG($db, "Run sql: $new_sql");
  604. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  605. if (!$sql_result) {
  606. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  607. return;
  608. }
  609. } else { return; }
  610. if ($table !== "sessions") {
  611. LOG_VERBOSE($db, "Deleted FROM table $table WHERE $filter $changed_log");
  612. }
  613. return $changed_log;
  614. }
  615. function insert_record($db, $table, $newvalue)
  616. {
  617. if (!allow_update($table, 'add')) {
  618. LOG_WARNING($db, "User does not have write permission");
  619. return;
  620. }
  621. if (!isset($table)) {
  622. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  623. return;
  624. }
  625. if (empty($newvalue)) {
  626. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  627. return;
  628. }
  629. $changed_log = '';
  630. $field_list = '';
  631. $value_list = '';
  632. foreach ($newvalue as $key => $value) {
  633. if (empty($value) and $value != '0') {
  634. $value = '';
  635. }
  636. if (!preg_match('/password/i', $key)) {
  637. $changed_log = $changed_log . " $key => $value,";
  638. }
  639. $field_list = $field_list . "`" . $key . "`,";
  640. $value = trim($value);
  641. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  642. }
  643. if (empty($value_list)) {
  644. return;
  645. }
  646. $changed_log = substr_replace($changed_log, "", -1);
  647. $field_list = substr_replace($field_list, "", -1);
  648. $value_list = substr_replace($value_list, "", -1);
  649. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  650. LOG_DEBUG($db, "Run sql: $new_sql");
  651. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  652. if (!$sql_result) {
  653. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  654. return;
  655. }
  656. $last_id = mysqli_insert_id($db);
  657. if ($table !== "sessions") {
  658. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  659. }
  660. if ($table === 'User_auth') {
  661. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  662. }
  663. if ($table === 'User_auth_alias') {
  664. //dns
  665. if (!empty($newvalue['alias'])) {
  666. $add_dns['name_type'] = 'CNAME';
  667. $add_dns['name'] = $newvalue['alias'];
  668. $add_dns['value'] = get_dns_name($db, $newvalue['auth_id']);
  669. $add_dns['type'] = 'add';
  670. $add_dns['auth_id'] = $newvalue['auth_id'];
  671. insert_record($db, 'dns_queue', $add_dns);
  672. }
  673. }
  674. if ($table === 'User_auth') {
  675. //dns
  676. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  677. $add_dns['name_type'] = 'A';
  678. $add_dns['name'] = $newvalue['dns_name'];
  679. $add_dns['value'] = $newvalue['ip'];
  680. $add_dns['type'] = 'add';
  681. $add_dns['auth_id'] = $last_id;
  682. insert_record($db, 'dns_queue', $add_dns);
  683. }
  684. }
  685. return $last_id;
  686. }
  687. function dump_record($db, $table, $filter)
  688. {
  689. $result = '';
  690. $old = get_record($db, $table, $filter);
  691. if (empty($old)) {
  692. return $result;
  693. }
  694. $result = 'record: ' . get_rec_str($old);
  695. return $result;
  696. }
  697. function get_rec_str($array)
  698. {
  699. $result = '';
  700. foreach ($array as $key => $value) {
  701. $result .= "[" . $key . "]=" . $value . ", ";
  702. }
  703. $result = preg_replace('/,\s+$/', '', $result);
  704. return $result;
  705. }
  706. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  707. {
  708. if (!isset($table)) {
  709. return;
  710. }
  711. if (!isset($filter)) {
  712. return;
  713. }
  714. if (!isset($newvalue)) {
  715. return;
  716. }
  717. if (!isset($only_changed)) {
  718. $only_changed = 0;
  719. }
  720. $old_sql = "SELECT * FROM $table WHERE $filter";
  721. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  722. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  723. $changed_log = "\r\n";
  724. foreach ($newvalue as $key => $value) {
  725. if (strcmp($old[$key], $value) !== 0) {
  726. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  727. }
  728. }
  729. $old_record = '';
  730. if (!$only_changed) {
  731. $old_record = "\r\n Has not changed:\r\n";
  732. foreach ($old as $key => $value) {
  733. if (!empty($newvalue[$key])) {
  734. $old_record = $old_record . " $key = $value,\r\n";
  735. }
  736. }
  737. $old_record = substr_replace($old_record, "", -3);
  738. }
  739. // print $changed_log;
  740. return $changed_log . $old_record;
  741. }
  742. function delete_user_auth($db, $id)
  743. {
  744. //remove aliases
  745. $t_User_auth_alias = get_records($db,'User_auth_alias',"auth_id=$id ORDER BY alias");
  746. if (!empty($t_User_auth_alias)) {
  747. foreach ( $t_User_auth_alias as $row ) {
  748. LOG_INFO($db, "Remove alias id: ".$row['id']." for auth_id: $id :: ".dump_record($db,'User_auth_alias','id='.$row['id']));
  749. delete_record($db,'User_auth_alias','id='.$row['id']);
  750. }
  751. }
  752. //remove connections
  753. run_sql($db, 'DELETE FROM connections WHERE auth_id=' . $id);
  754. //remove user auth record
  755. LOG_INFO($db, "Removed user auth_id: $id :: ".dump_record($db,'User_auth','id='.$id));
  756. $changes = delete_record($db, "User_auth", "id=" . $id);
  757. return $changes;
  758. }
  759. function delete_user($db,$id)
  760. {
  761. //remove user record
  762. $changes = delete_record($db, "User_list", "id=" . $id);
  763. //if fail - exit
  764. if (!isset($changes) or empty($changes)) { return; }
  765. //remove auth records
  766. $t_User_auth = get_records($db,'User_auth',"user_id=$id");
  767. if (!empty($t_User_auth)) {
  768. foreach ( $t_User_auth as $row ) { delete_user_auth($db,$row['id']); }
  769. }
  770. //remove device
  771. $device = get_record($db, "devices", "user_id='$id'");
  772. if (!empty($device)) {
  773. LOG_INFO($db, "Delete device for user id: $id ".dump_record($db,'devices','user_id='.$id));
  774. unbind_ports($db, $device['id']);
  775. run_sql($db, "DELETE FROM connections WHERE device_id=" . $device['id']);
  776. run_sql($db, "DELETE FROM device_l3_interfaces WHERE device_id=" . $device['id']);
  777. run_sql($db, "DELETE FROM device_ports WHERE device_id=" . $device['id']);
  778. run_sql($db, "DELETE FROM device_filter_instances WHERE device_id=" . $device['id']);
  779. run_sql($db, "DELETE FROM gateway_subnets WHERE device_id=".$device['id']);
  780. delete_record($db, "devices", "id=" . $device['id']);
  781. }
  782. //remove auth assign rules
  783. run_sql($db, "DELETE FROM auth_rules WHERE user_id=$id");
  784. return $changes;
  785. }
  786. function delete_device($db,$id)
  787. {
  788. LOG_INFO($db, "Try delete device id: $id ".dump_record($db,'devices','id='.$id));
  789. //remove user record
  790. $changes = delete_record($db, "devices", "id=" . $id);
  791. //if fail - exit
  792. if (!isset($changes) or empty($changes)) {
  793. LOG_INFO($db,"Device id: $id has not been deleted");
  794. return;
  795. }
  796. unbind_ports($db, $id);
  797. run_sql($db, "DELETE FROM connections WHERE device_id=" . $id);
  798. run_sql($db, "DELETE FROM device_l3_interfaces WHERE device_id=" . $id);
  799. run_sql($db, "DELETE FROM device_ports WHERE device_id=" . $id);
  800. run_sql($db, "DELETE FROM device_filter_instances WHERE device_id=" . $id);
  801. run_sql($db, "DELETE FROM gateway_subnets WHERE device_id=".$id);
  802. return $changes;
  803. }
  804. $db_link = new_connection(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  805. ?>