Запись [n:_dev/main/parser_cbr.php] < dev.sky.1.001
Версия 1.001
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | <?php defined('START') and AUTH_OK or die; # w- define('WIND', PHP_SAPI == 'cli'); # w+ // $wind_eol - parametr in command line ? define('SYS_EOL', WIND ? (PHP_OS == 'WINNT' ? "\r\n" : "\n") : $u_system_eol); //? mac $detect_eol = SYS_EOL; /* w- $id0 - if $id0 > 0 then it is initial (0-step), else `continue` step modes: 1. AJAX - forward - imitate 2. AJAX - forward - run 3. AJAX - rollback - imitate 4. AJAX - rollback - run 5. cli_WIND - forward - imitate 6. cli_WIND - forward - run 7. cli_WIND - rollback - imitate 8. cli_WIND - rollback - run 9. MWS - make Wind script */ if (defined('REAL_RUN')): defined('MWS') or define('MWS', false); isset($rollback) or $rollback = 0; # w+ isset($imitate_only) or $imitate_only = 1; isset($force) or $force = 0; //? $err = $cbr = $color = $opened_txt = $com = $close_handler = $search = $end = $the_end = ''; $ftrd = 'A^0/0/0/0 D^0/0/0/0 E^0/0/0/_ R^0/0/_/0'; $strict = 1; if (WIND) { # w- $n = 1; # w+ define('ROLLBACK', $rollback); $imitate = 1; $err = w_run_record($record); if (false === $err && $imitate && !$imitate_only) { $imitate = 0; $err = w_run_record($record); } false === $err and $err = 1; echo "\n" . err_mode($err, $n, $color, $imitate) . "\n"; } # w- $mem = []; define('ID', 87); isset($imitate) or $imitate = 1; if ($id0) { // init step 0 $rr = sql(">select c.name, c.version, c.changed, c.package_id, p.name as pn, p.version as pv, p.tmemo from _dev_codebase c left join _dev_packages p on (p.id = c.package_id) where c.id=$id0 "); set([ 'start' => microtime(true), 'rollback' => (int)$rollback, 'imitate' => (int)$imitate, 'imitate_only' => (int)$imitate_only, 'ftrd' => $ftrd, 'n=head_done' => $n = 0, 'start_id=cur_id' => $cur_id = $id0, 'tree' => serialize($tree = [$id0 => $id0]), 'path' => "[$id0]", 'rname' => $rr->name, 'vname' => $id0 > 10000 && $rr->pn ? ver($rr->pv, $rr->pn) : ver($rr->version, $rr->name), 'changed' => (int)$rr->changed, 'sky_id' => get_var($rr->tmemo, 'sky_id'), 'priorun' => 1 == $rr->package_id && 1 != $id0 ? '' : get_var($rr->tmemo, 'priorun'), 'package_id' => $rr->package_id, ]); if ('a' == $rr->name[0]) set('a_live_public', substr($rr->name, 2), 10); } else { // continue $vars = ('install' == START ? '' : cell(10, 'm') . "\n") . cell(ID, 'm'); #if (app && cell exist) $vars .= "\n" . cell(app_cell, 'm'); -- 2do eval($vars); define('ROLLBACK', $rollback); is_array($u_text_files) or $u_text_files = str2ary($u_text_files); $err = c_run_records($cur_id); if (false === $err) { // end of script if ($imitate && !$imitate_only) { $mem += ['imitate' => $imitate = 0, 'tree' => serialize([$cur_id => $cur_id])]; $n = $err = $end = 0; } else { // very end of script in OK status $err = 1; if ('a' == $rname[0]) set('u_my_app', substr($rname, 2), 3); $id = sql("+select ifnull(max(id)+1,500) from _dev_memory where id>499 and id<1000"); if ($id == 1000) return 'Internal error: maximum Builds reached!'; sqlf("insert into _dev_memory %s", [ 'id' => $id, 'dt' => 'now()', 'code' => $start_id, 'imemo' => $package_id, 'cmemo' => "'$vname'", 'tmemo' => escape(set([ 'ftrd' => $ftrd, 'can_rollback' => $is_rb, 'sky_id' => $sky_id, 'run_changed' => $changed, 'priorun' => $priorun, ], 0, 0))]); } } set(['n' => $n, 'cur_id' => $cur_id] + $mem); } MWS or echo_json2($color, $n); if ('install' == START) return $err; endif; // REAL_RUN ########### COMMAND FUNCTIONS ######################################## function c_run_records($par, $txt = 0) { if (ROLLBACK) return r_oll_back($par, $txt); global $tree, $path, $cbr, $n, $cur_id, $mem, $close_handler, $imitate, $end, $the_end, $head_done, $com; $com = ''; $cbr = Record::ary($par, $rec = new Record); if ($rec->mess) return $rec->mess('<br>'); for ($end = 0, $cnt = count($cbr); true; $n++) { if (0 === $n && !isset($cbr[0])) continue; if (!isset($cbr[$n]) || $the_end) { // end of cbr $end = 1; if ($close_handler) { if ($err = eval($close_handler)) return $err; } $imitate or sql("update _dev_codebase set used=1 where id=$par"); if ($path === "[$par]") return false; // end of script else { // end of cbr is_array($tree) or $tree = unserialize($tree); $path = substr($path, 0, strrpos($path, '[')); eval("\$t = \$tree$path;"); $next = false; foreach($t as $id => $v) if (true === $next) $next = $id; elseif ($id == $par) $next = true; $cur_id = $next > 0 ? $next : -$next; $n = $next > 0 ? 0 : $t[$next]; $mem += ['path' => $next > 0 ? "{$path}[$next]" : $path]; return $end = 0; // end of step } } else $r = $cbr[$n]; $com = $r[0]; trace("RUN $par.N$n: $com: $r[1]"); if (!$head_done && !in_array($com, ['HEAD', 'SKIP IMITATE'])) return c_head(); if ($close_handler && $com != 'SEARCH' && $com != 'REPLACE') return eval($close_handler); if ('RUN RECORDS' == $com) { $par = array_flip(list_exp($r[1], ',')) + [-$par => 1 + $n]; is_array($tree) or $tree = unserialize($tree); eval("\$tree$path = \$par;"); if ($err = test_infinity($tree)) return $err; $cur_id = key($par); $mem += ['tree' => serialize($tree), 'path' => "{$path}[$cur_id]"]; return $n = 0; } elseif (MWS && 'HEAD' != $com) { if ('SKY ' == substr($com, 0, 4)) { if ($err = sky_object($r[2], $cbr, $par)) return $err; $r[2] = [$r[2]]; $com = 'SKY FILE' == $com ? 'WRITE FILE' : 'UPDATE CELL'; } if ($err = create_wind($com, $r[1], $r[2], $par)) return 0; } elseif ($err = call_user_func(pfunc($com), $r[1], $r[2])) return $err; if (0 === $err) { $end = isset($cbr[++$n]) ? 0 : 1; return 0; } } } function c_sky_file($par, $txt) { if (ROLLBACK) return c_delete_files('', [$par], false); global $cbr, $cur_id; if ($err = sky_object($txt, $cbr, $cur_id)) return $err; return c_write_file($par, [$txt]); } function c_write_file($par, $txt) { if (ROLLBACK) return c_delete_files('', [$par], false); if ($err = check_dir($par)) return $err; if (file_exists($par)) return "File `$par` already exists!"; $txt = implode(SYS_EOL, $txt); if (1 != rectype($par)) $txt = base64_decode($txt); // decode if not a text else $txt = preg_replace_callback('|(\{\$a_\w+\})|sm', 'replace_a_vars', $txt); if (false === $txt) return "Error converting file `$par`"; global $imitate; if (!$imitate) { $n = @file_put_contents($par, $txt); if ($n !== strlen($txt)) return "Error writing file `$par`"; ftrd('fA'); } return 0; // OK } function c_append_file($par, $txt) { if ($err = open_file($par)) return $err; global $opened_txt; $txt = implode("\n", $txt); if (ROLLBACK) { $len = strlen($txt); if ($txt != substr($opened_txt, -$len)) return 'File\'s tail not found'; $opened_txt = substr($opened_txt, 0, -$len); } else $opened_txt .= "\n$txt"; return close_file($par); } function c_translate_file($par, $txt) { global $strict, $opened_txt; if ($err = open_file($par)) return $strict ? $err : 0; if (is_string($err = parse_trans($txt))) return $strict ? $err : 0; $opened_txt = str_replace($err[0], $err[1], $opened_txt); return close_file($par); } function open_file($fn) { if (!is_file($fn)) return "File `$fn` not exists"; if (!is_readable($fn)) return "File `$fn` is not readable"; if (!is_writeable($fn)) return "File `$fn` is not writeable"; $fh = @fopen($fn, 'r'); if ($fh === false) return "File `$fn` probably locked"; @fclose($fh); # 2do if (is_array($str = getfile($file, $ftype = rectype($file)))) return $str[0]; $txt = @file_get_contents($fn); if (false === $txt) return "Error reading file `$fn`"; if ('' === $txt) return "File `$fn` is empty"; global $opened_txt; $opened_txt = str_replace(["\r\n", "\r"], "\n", $txt); return 0; } function c_open_file($par, $txt) { if (ROLLBACK) return r_open_file($par, $txt); if ($err = open_file($par)) return $err; global $close_handler; $close_handler = "return close_file('$par');"; return ''; // continue } function close_file($fn) { global $imitate, $opened_txt, $close_handler; if (!$imitate) { $c = @file_put_contents($fn, puteol($opened_txt)); if (!$c) return "Error writing file `$fn` (empty) when close_file()"; ftrd('fE'); } $opened_txt = $close_handler = ''; return 0; } function c_delete_files($par, $txt, $trash = true) { if (ROLLBACK && $trash) return r_delete_files($par, $txt); global $imitate; foreach(list_exp($txt) as $file) { if (trim($file) === '') continue; if (!is_file($file)) return "File `$file` not exists, cannot delete"; if (!is_writable($file)) return "File `$file` is not writeable"; if ($trash && !WIND && !is_readable($file)) return "File `$file` is not readable"; if (!$imitate) { if ($trash && !WIND) { if (is_array($str = getfile($file, $ftype = rectype($file)))) return $str[0]; sqlf("insert into _dev_recycled values(null, '$file', $ftype, %s, now())", escape($str)); } if (!@unlink($file)) return "Cannot delete file `$file`"; ftrd('fD'); } } return 0; } function c_move_files($par, $txt) { if (ROLLBACK) $txt = array_reverse($txt); global $imitate; foreach ($txt as $v) { if (!preg_match("|^\S+ ~ \S+$|", $v)) return "Syntax error in `MOVE FILES`"; if (ROLLBACK) list($to, $from) = explode(' ~ ', $v); else list($from, $to) = explode(' ~ ', $v); if (!is_file($from)) return "File `$from` not exists"; if (!is_writeable($from)) return "File `$from` is not writeable"; if (file_exists($to)) return "File `$to` already exists!"; if ($err = check_dir($to)) return $err; if (!$imitate) { // ? if (!@rename($from, $to)) return "Cannot move file `$from` to `$to`"; ftrd('fR'); } } return 0; } function c_create_dirs($par, $txt, $flag = true) { if (ROLLBACK && $flag) return c_delete_dirs($par, $txt, false); $dirs = list_exp($txt); $cache = []; if (ROLLBACK) $dirs = array_reverse($dirs); foreach($dirs as $dir) { $cache[] = $dir; if ('@' == $par) { if (is_file($dir)) return "File `$dir` already exists"; } elseif (file_exists($dir)) return "Dir or file `$dir` already exists"; if ($err = check_dir($dir, $cache)) return $err; if ($dir == '.') return "Cannot delete root dir `.`"; } global $imitate; if (!$imitate) foreach($dirs as $dir) { if (('@' != $par || !is_dir($dir)) && !@mkdir($dir)) return "Cannot create dir `$dir`"; ftrd('dA'); } return 0; } function c_delete_dirs($par, $txt, $flag = true) { if (ROLLBACK && $flag) return c_create_dirs($par, $txt, false); $dirs = list_exp($txt); if (ROLLBACK) $dirs = array_reverse($dirs); foreach($dirs as $dir) { if (!is_dir($dir)) return "Dir `$dir` not exists"; if (!is_writeable($dir)) return "Dir `$dir` is not writeable, cannot delete"; if ($dir == '.') return "Cannot delete root dir `.`"; } global $imitate; if (!$imitate) foreach($dirs as $dir) { if (count(scandir($dir)) != 2) return "Dir `$dir` is not empty, cannot delete"; if (!@rmdir($dir)) return "Cannot delete dir `$dir`"; ftrd('dD'); } return 0; } function c_move_dirs($par, $txt) { if (ROLLBACK) $txt = array_reverse($txt); $cache = []; foreach($txt as $v) { if (!preg_match("|^\S+ ~ \S+$|", $v)) return "Syntax error in `MOVE DIRS`"; if (ROLLBACK) list($to, $from) = explode(' ~ ', $v); else list($from, $to) = explode(' ~ ', $v); if ($from == '.' || $to == '.') return "Cannot change root dir `.`"; if (!is_dir($from)) return "Dir `$from` not exists"; if (!is_writeable($from)) return "Dir `$from` is not writeable, cannot move"; if (!in_array($to, $cache) && file_exists($to)) return "Dir `$to` already exists"; if ($err = check_dir($to, $cache)) return $err; // check parent dir for destination if (!$imitate) { if (!@rename($from, $to)) return "Cannot move dir `$from` to `$to`"; ftrd('dR'); } else array_push($cache, $from, $to); //? } return 0; } function c_sky_cell($par, $txt) { if (ROLLBACK) return r_update_cell($par); global $cbr, $cur_id; if ($err = sky_object($txt, $cbr, $cur_id)) return $err; return c_update_cell($par, [$txt]); } function c_update_cell($par, $txt) { if (ROLLBACK) return r_update_cell($par); if ($err = open_cell($par)) return $err; global $opened_txt; $opened_txt = implode("\n", $txt); return eval($par); } function c_append_cell($par, $txt) { if ($err = open_cell($par)) return $err; global $opened_txt, $detect_eol; $txt = implode($detect_eol, $txt); if (ROLLBACK) { $len = strlen($txt); if ($txt != substr($opened_txt, -$len)) return 'Cell\'s tail not found'; $opened_txt = substr($opened_txt, 0, -$len); } else $opened_txt .= $txt; return eval($par); } function c_translate_cell($par, $txt) { global $strict, $opened_txt; if ($err = open_cell($par)) return $strict ? $err : 0; if (is_string($err = parse_trans($txt))) return $strict ? $err : 0; $opened_txt = str_replace($err[0], $err[1], $opened_txt); return eval($par); } function open_cell(&$par, $save = false) { if (is_numeric($par)) $cn = 'id' and $par = "_dev_codebase.$par.code"; if (2 == substr_count($par, '.')) { list($table, $id, $column) = explode('.', $par); if (!isset($cn)) list(,$cn,,) = table_id_test("$table."); if (!$cn) return 'Autoincrement column not found'; } else return 'Error in cell syntax'; global $idc; $q = mysqli_query($idc, "select $column from $table where $cn=$id"); if ($r = mysqli_fetch_row($q)) { global $opened_txt, $imitate; if ($save && !$imitate && !WIND) sqlf("insert into _dev_recycled values(null, '$table.$id.$column', -1, %s, now())", escape($r[0])); $opened_txt = preg_replace_callback('|(\{\$a_\w+\})|sm', 'replace_a_vars', geteol($r[0])); if (false === $opened_txt) return "Error converting cell `$table.$id.$column`"; $par = "return close_cell('$table', '$column', '$cn=$id');"; return 0; } else return 'Cell not exists'; } function c_open_cell($par, $txt) { if (ROLLBACK) return r_open_cell($par, $txt); if ($err = open_cell($par)) return $err; global $close_handler; $close_handler = $par; return ''; } function close_cell($table, $column, $row) { global $imitate, $opened_txt, $close_handler, $detect_eol; if (!$imitate) { $c = sqlf("update $table set $column=%s where $row", escape(puteol($opened_txt, $detect_eol))); if (!$c) return '-Data not modified when close_cell()'; ftrd('rE'); } $opened_txt = $close_handler = ''; return 0; } function c_search($par, $txt) { if (ROLLBACK) return r_search($par, $txt); global $opened_txt, $search; if ('' === $opened_txt) return 'Trying `SEARCH` in empty...'; $txt = implode("\n", $txt); if ('' === $txt) return '`SEARCH` signature is empty'; if (1 !== substr_count($opened_txt, $txt)) return 'Signature count for `SEARCH` is not equal to one'; $search = $txt; return ''; // continue } function c_replace($par, $txt) { if (ROLLBACK) return r_replace($par, $txt); global $opened_txt, $search; $txt = implode("\n", $txt); if ('' === $txt) return '`REPLACE` signature is empty'; $opened_txt = str_replace($search, $txt, $opened_txt); return $search = ''; // continue } function c_sql($par, $txt) { $list = sql_split(trim(implode("\n", $txt))); if (ROLLBACK) return r_sql(array_reverse($list)); global $imitate, $idc; if (!$imitate) foreach ($list as $sql) { mysqli_query($idc, trim($sql)); if (mysqli_errno($idc)) return mysqli_error($idc); // ftrd('dA'); ? } // 2do: check in imitate mode! return 0; } function c_head($par = 0, $txt = []) { $vars = []; $defaults = [ 'author' => ['author', ''], 'script tested' => [0, 'no'], 'edited manualy' => [0, 'no'], 'ftrd' => ['ftrd_in', 'A^0/0/0/0 D^0/0/0/0 E^0/0/0/_ R^0/0/_/0'], 'this script' => [0, 'auto, 0 records, 0 objects, 0 bytes'], 'reused code' => [0, 'coresky'], 'rollback' => ['is_rb', 'no'], 'rollback wind' => ['is_rbw', 'no'], 'text files' => ['u_text_files', 'svg php inc html xhtml htm tpl js css txt htaccess cfg sql c pl'], 'svn skips' => ['u_svn_skips', '.svn Thumbs.db'], 'include paths' => ['u_inc_p', ''], 'exclude paths' => ['u_exc_p', ''], 'include tables' => ['u_inc_t', ''], 'exclude tables' => ['u_exc_t', ''], 'include rows' => ['u_inc_r', ''], 'exclude rows' => ['u_exc_r', ''], ]; foreach ($txt as $line) if (preg_match("/^([^:]+):\s+(.*)/", $line, $m) && isset($defaults[$m[1]])) { $_ = $defaults[$m[1]]; if (!$_[0]) continue; $vars[$_[0]] = in_array($m[2], ['yes', 'no']) ? intval('yes' == $m[2]) : $m[2]; } foreach ($defaults as $row) if ($row[0] && !isset($vars[$row[0]])) $vars[$row[0]] = $row[1]; global $mem; $mem += $vars + ['head_done' => 1]; return 0; } function c_end($par, $txt) { global $the_end, $imitate; if ($imitate && !MWS) return 0; $the_end = 1; return ''; } function c_skip_imitate($par, $txt) { global $mem, $imitate, $imitate_only, $start_id, $cur_id, $n; if (!$imitate) return 0; if (1 != $n || !WIND && $start_id != $cur_id) return 'Command `SKIP IMITATE` must be first at enter point'; if ($imitate_only) return 0; if (WIND) return $imitate = 0; $mem = ['imitate' => $imitate = 0] + $mem; return 0; } function c_strict($par, $txt) { global $strict; $strict = 'off' == $par ? 0 : 1; return 0; } function c_message($par, $txt) { return '+' . implode(WIND ? SYS_EOL : '<br>', $txt); } function c_stop($par, $txt) { return $par == 'both' || ROLLBACK && $par == 'back' || !ROLLBACK && $par == 'forw' ? "Command `STOP` executed, cannot continue." : 0; } function c_data($par, $txt) { return 0; } function c_run_util($par, $txt) { return 0; # 2do - UTIL: visual # execute `visual` macro and more... } function c_sky_package($par, $txt) { return 0; # 2do - future... } function c_sky_query($par, $txt) { return 0; # 2do - future... future.. } function c_desc($par, $txt) { /* never called */ } ########### ROLLBACK COMMANDS ######################################## function list_com($v) { return preg_match(MATCH_COM, $v); //*** } function r_oll_back($par, $txt = 0) { return 0; if (is_numeric($PVAL = $par)) ;#eval(_open_cdebase); else foreach (array_reverse(list_exp($par, ',')) as $id) if ($err = r_oll_back($id, '')) return $err; static $deep = 0, $stack = []; $deep++; $list_com = array_filter($lines, 'list_com'); $com = end($list_com); $p = count($lines); while ($p > 0) { if ($com) $i = key($list_com); else $i = -1 and $com = "#<- WRITE FILE: $name ->#"; //*** preg_match(MATCH_COM, $com, $m); //*** $txt = []; if ($com = isset($rollback_rules[$m[1]]) ? $rollback_rules[$m[1]] : '') { $par = isset($m[2]) ? $m[2] : ''; if ($com != 'r_oll_back' && $com != 'c_delete_files') for ($j = $i + 1; $j < $p; $j++) $txt[] = $lines[$j]; trace("COM: $com $par"); if ($err = call_user_func($com, $par, $txt, false)) return $err; } $com = prev($list_com); $p = $i - 1; } sql("update _dev_codebase set used=0 where id=$r[0]"); if ($deep == 1) { sql("delete from _dev_memory where id=".sql("+select max(id) from _dev_memory where id>500 and id<1000")); } $deep--; return 0; } function r_delete_files($par, $txt) { return 0; $err = 0; if ($par) array_unshift($txt, $par); foreach(list_exp($txt) as $file) { if (!$file) continue; $file = $file; if ($r = sql("-select txt from _dev_recycled where obj_id=0 and name='$file'")) { file_put_contents($file, $r[0]); sql("delete from _dev_recycled where obj_id=0 and name='$file'"); } else return "File `$file` absent in trash. Can't restore"; } return $err; } function r_sql($par, $txt) { return 0; $rollsql_rules = [ '^create table (\w+).*' => 'drop table $1', '^insert into (\w+) values\s?\((\d+),.*' => 'delete from $1 where id=$2', '^insert into (\w+) \(id[^\)]*\) values\s?\((\d+).*' => 'delete from $1 where id=$2', '^alter table (\w+) add (\w+)' => 'alter table $1 drop $2', ]; global $idc; foreach (array_reverse(explode(";\n\n", implode("\n", $txt))) as $sql) { $sql = preg_replace("/\s+/sm", ' ', trim($sql)); foreach ($rollsql_rules as $k => $v) { $new = preg_replace("/$k/i", $v, $sql); if ($new != $sql) { mysqli_query($idc, trim($new)); if (mysqli_errno($idc)) return mysqli_error($idc); break; } } if ($new == $sql) return "Can't rollback SQL: $sql"; } return 0; } function r_search($par, $txt) { return 0; } function r_replace($par, $txt) { return 0; } function r_open_file($par, $txt) { //close_file return 0; } function r_open_cell($par, $txt) { return 0; } function r_update_cell($par, $txt) { return 0; } ########### TO DELETE ################################################ function __c_delete_rows($par, $txt, $trash = true) { if (ROLLBACK) return r_delete_rows($par, $txt); return 0; if (!preg_match("|^(\w+)\.([,\-\d]+)$|", $par, $m)) return 'Syntax error in `DELETE ROWS`'; $par = "from $m[1] where id in (".implode(',', list_exp($m[2], ',')).")"; if ($trash && !WIND && !$imitate) { $q = sql("select * $par"); while ($r = fetch($q, true)) { foreach ($r as $i => $col) $r[$i] = isset($r[$i]) ? (is_numeric($r[$i]) ? $col : escape($col)) : 'NULL'; $rec = "$m[1] values(".implode(',', $r).")"; sqlf("insert into _dev_recycled values(null, '$m[1]', $r[0], %s, now())", escape($rec)); # %s ?? } } if ($imitate) { } else $rs_dl += sql("delete $par"); return 0; } function __r_delete_rows($par, $txt) { return 0; if (!preg_match("|^(\w+)\.([,\-\d]+)$|", $par, $m)) return 'syntax error in R DELETED ROWS'; $par = "id in (".implode(',', list_exp($m[2], ',')).")"; sql("delete from $m[1] where $par"); $q = sql("select txt from _dev_recycled where name='$m[1]' and obj_$par"); while (eval(fetch)) sql("insert into $r_txt"); sql("delete from _dev_recycled where name='$m[1]' and obj_$par"); return 0; } ########### OTHER FUNCTIONS ########################################## function w_run_record($txt) { $cbr = []; $emp = 1; global $n, $close_handler; $n = 0; foreach (explode("\n", $txt) as $line) { if ($emp && preg_match(MATCH_COM, $line, $m)) $cbr[++$n] = [$m[1], $m[2], []] and $emp = 0; else { if ($emp) $cbr[$n][2][] = $emp = ''; if ('' === $line) $emp = 1; else $cbr[$n][2][] = cbunesc($line); } } if ($cbr && $emp) $cbr[$n][2][] = ''; if (ROLLBACK) return w_rollback($cbr); foreach ($cbr as $n => $r) { if ($close_handler && $r[0] != 'SEARCH' && $r[0] != 'REPLACE') { if ($err = eval($close_handler)) return $err; } $func = 'c_' . str_replace(' ', '_', strtolower($r[0])); if ($err = call_user_func($func, $r[1], $r[2])) return $err; } if ($close_handler and $err = eval($close_handler)) return $err; return false; } function w_rollback($cbr) { global $n, $close_handler; return false; } function geteol($str) { global $detect_eol; $rn = substr_count($str, "\r\n"); $n = substr_count($str, "\n") - $rn; $r = substr_count($str, "\r") - $rn; if ($r > $n && $r > $rn) $detect_eol = "\r"; else $detect_eol = $n > $rn ? "\n" : SYS_EOL; return str_replace(["\r\n", "\r"], "\n", $str); } function puteol($str, $eol = 0) { $eol or $eol = SYS_EOL; return str_replace("\n", $eol, $str); } function check_dir($file, $cache = []) { $dir = substr($file, 0, -1 - strlen(basename($file))); if ($dir === false) $dir = '.'; if (in_array($dir, $cache)) return 0; if (!is_dir($dir)) return "Dir `$dir` not exists!"; return is_writable($dir) ? 0 : "Dir `$dir` is not writeable"; } function test_infinity($tree, $key = false) { static $list = []; if (false === $key) { $list = []; array_walk_recursive($tree, 'test_infinity'); return $list === array_unique($list) ? 0 : 'Infinite cycle found. Cannot continue.'; } else array_push($list, $key > 0 ? $key : -$key); } function err_mode($err, &$n, &$color, $imitate, $end = 0) { if ($end) $n--; if (!$err) return ''; $mode = $imitate ? 'IMITATE mode (no actions has been made)' : 'REAL RUN ! mode'; if (1 === $err) $color = 'green'; elseif ('-' == $err[0]) $color = 'yellow' and $err = substr($err, 1) and $what = 'Warning'; elseif ('+' == $err[0]) $color = 'skyblue' and $err = substr($err, 1) and $what = 'Message'; else $color = 'red' and $what = 'Error'; $nl = WIND ? "\n" : '<br>'; global $ftrd; $pre = 1 === $err ? "Script FINISHED in $mode{$nl}FTRD: $ftrd" : "$what in $mode at " . ($end ? "the END of Record" : "N$n"); return $pre . $nl . (1 === $err ? 'All done OK.' : $err); } function ftrd($mode, $val = 1) { global $mem, $ftrd; // 'A^0/0/0/0 D^0/0/0/0 E^0/0/0/_ R^0/0/_/0' sample: fA static $aa = ['A' => 0, 'D' => 1, 'E' => 2, 'R' => 3]; static $oa = ['f' => 0, 't' => 1, 'r' => 2, 'd' => 3]; $ftrd = explode(' ', $ftrd); $vs = $ftrd[$i = $aa[$mode[1]]]; $vv = explode('/', substr($vs, 2)); $vs = substr($vs, 0, 2); $vv[$oa[$mode[0]]] += $val; $ftrd[$i] = $vs . implode('/', $vv); $ftrd = implode(' ', $ftrd); if (!WIND) $mem = ['ftrd' => $ftrd] + $mem; return 0; } function parse_trans($lines) { $s = $r = $s0 = $r0 = []; $ml = 0; $err = 'Error in TRANSLATE .. syntax'; array_push($lines, ''); foreach ($lines as $v) if ('' === $v) { if ($ml == 2 && $r0) { $s[] = trim(implode("\n", $s0)); $r[] = trim(implode("\n", $r0)); $s0 = $r0 = []; } elseif ($ml != 3) return $err; $ml = 0; } elseif ('~' === $v) { if ($ml != 1) return $err; $ml = 2; } elseif (2 == count($q = explode(' ~ ', $v, 2))) { if ($ml && $ml != 3) return $err; $s[] = trim($q[0]); $r[] = trim($q[1]); $ml = 3; } elseif ($ml < 2) { $s0[] = $v; $ml = 1; } else { if ($ml != 2) return $err; $r0[] = $v; } return $s ? (ROLLBACK ? [array_reverse($r), array_reverse($s)] : [$s, $r]) : $err; } ########### NOT FOR WIND ############################################# # functions below will never packed in WIND function echo_json2(&$color, &$n) { global $d_step_timeout, $err, $cur_id, $imitate, $end; #usleep($d_step_timeout * 1000); # milli to micro seconds print json_encode([ 'err' => err_mode($err, $n, $color, $imitate, $end), 'color' => $color, 'id' => $cur_id, 'n' => $n, ]); } function pfunc($com) { return 'c_' . str_replace(' ', '_', strtolower($com)); } function sky_object(&$defs, $ary, $id) { $out = $cache = []; $pcs = ['DATA', 'WRITE FILE', 'UPDATE CELL']; foreach(list_exp($defs) as $def) { list($cid, $name) = explode('.', $def, 2); if ('this' == $cid) $cid = $id; if (!isset($cache[$cid])) { if ($cid == $id) $cbr = $ary; else { $cbr = Record::ary($cid, $rec = new Record); if ($rec->mess) return $rec->mess(); } foreach ($cbr as $r) if (in_array($r[0], $pcs)) { $q = explode('+', $r[1]); $dn = array_shift($q); $cache[$cid][$dn] = [$r[2], array_reverse($q)]; } } if (!isset($out["$cid.$name"])) { $_ = $ary = []; if ('*' == $name) $ary = array_keys($cache[$cid]); else do { if (!isset($cache[$cid][$name])) return "Item `$name` not found at ID=$cid"; array_unshift($ary, $name); $_ = array_merge($cache[$cid][$name][1], $_); } while ($name = array_shift($_)); foreach ($ary as $dn) if (!isset($cache[$cid][$dn])) return "Item `$dn` not found at ID=$cid"; elseif (!isset($out["$cid.$dn"])) $out["$cid.$dn"] = implode("\n", $cache[$cid][$dn][0]); } } $defs = implode("\n\n", $out); return 0; } |
Записи Codebase (89)
Статус SKY.
Current version: 1.001
Coresky records: 22
Local (DEV) records: 89
Web (all) records: 105
Download: dev.php
Coresky records: 22
Local (DEV) records: 89
Web (all) records: 105
Download: dev.php