1
0

sql.php 29 KB

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