Запись [c:_dev/main/record.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 | <?php defined('START') or die; class Record { public $id = 0; public $size = 0; public $name = ''; public $code = []; public $parents = 'update _dev_codebase set parents'; public $skm = false; public $mess = []; public $ary = []; public $cp = []; static $err = ''; const file = 'return preg_match("|^[\.\w/]*\w$|", $par) ? 0 : a("filename", "?help=draft&en", "class=\\"nd\\"") . " `$par` is wrong";'; const pname = '|^[a-z0-9][a-z0-9_\.]{1,30}[a-z]\.?$|'; function err($err, $is_error = false) { self::$err or $is_error and self::$err = $err; $this->skm or $this->mess[] = $err; } function mess($by = "\n ") { return implode($by, $this->mess); } function __toString() { return $this->mess(); } function syntax($name, $n, $row) { list($com, $par, $ary, $ln) = $row; $pre = "Error at N$n, command `$com`:"; $_com = 'd_' . str_replace(' ', '_', $com); global $$_com; if (!isset($$_com)) return $this->err("$pre unknown command!", true); list ($p, $a) = explode(' ', $$_com); # 2do: more exact syntax check for CBR. mysql, css syntax check ? $syn = [ 'any' => 'return 0;', 'onf' => 'return "on" === $par || "off" === $par ? 0 : "must be `on` or `off`";', 'name' => 'return preg_match("/^\w+(\+\w+)*$/", $par) ? 0 : "`name` definition `$par` is wrong";', '@' => 'return in_array($par, ["", "@", []], true) ? 0 : "`PAR` must be empty or @";', '0' => 'return "" === $par || [] === $par ? 0 : "`PAR` must be empty";', '1' => 'return $par ? 0 : "must have at least one empty string";', 'ids' => 'return preg_match("/^\d[\d,\-]*$/", $par) ? 0 : "`ids` definition `$par` is wrong";', 'file' => self::file, 'cell' => 'return preg_match("/^\w+\.\d+\.\w+$/", $par) ? 0 : "`cell` definition `$par` is wrong";', 'direct' => 'return in_array($par, ["forw", "back", "both"]) ? 0 : "`par` must be one of [forw|back|both]";', 'lang' => 'eval(cell(250)); return isset($all_lang_list[$par]) ? 0 : "language code `$par` is unknown";', ]; $ok = isset($syn[$p]) or $this->err("$pre `$p` - wrong " . a('definition', "?help=commands&en#$_com")); $ok &= isset($syn[$a]) or $this->err("$pre `$a` - wrong " . a('definition', "?help=commands&en#$_com")); if (!$ok) return self::$err = "$pre Internal error"; if ($err = eval($syn[$p])) $this->err("$pre $err", true); $par = $ary; if ($err = eval($syn[$a])) $this->err("$pre $err", true); } static function ary($ary, $me = false) { $me or $me = new Record and $me->skm = true; is_array($ary) or $ary = [$ary]; list($code, $name) = isset($ary[1]) ? $ary : sql("-select code, name from _dev_codebase where id=$ary[0]"); isset($ary[1]) or $me->name = $name; is_array($code) or $code = explode("\n", $code); # must used `explode`! if (@$name[1] != ':') { $me->err("Colon `:` expected in Record name", true); if (strlen($name) < 3) $name = 'f:_dev/temp/assigned_name.txt'; } global $cbr_typ0, $cbr_typ1; if (!$typ0 = in_array($name[0], $cbr_typ0)) { if (!in_array($name[0], $cbr_typ1)) { $me->err($name[0] . ": - unknown record type", true); $typ0 = true; } } $ary = []; if ($typ0) { $par = substr($name, 2); if ($err = eval(self::file)) $me->err("Error at N0 (CBR name): $err", true); $ary[0] = ['WRITE FILE', $par, [], -1]; } for ($emp = $i = $last = $n0 = $n1 = 0, $c = count($code); $i < $c; $i++) { if (($emp || !$i) && preg_match(MATCH_COM, $code[$i], $m)) { if ($ary) $me->skm or $me->syntax($name, $last, $ary[$last]); if ('DESC' === $m[1]) $last = --$n0; else $last = ++$n1; $ary[$last] = [$m[1], $m[2], [], $i]; $emp = 0; } elseif (!$i && !$ary) { $me->err("Error: command expected at first line", true); } elseif ($code[$i] !== '') { if ($emp) $emp = $ary[$last][2][] = ''; $ary[$last][2][] = cbunesc($code[$i]); } elseif ($emp) { $ary[$last][2][] = ''; } else $emp = 1; } if ($emp) $ary[$last][2][] = ''; if (!$me->skm && $ary) $me->syntax($name, $last, $ary[$last]); return $ary; } static function is_php($name) { global $u_php_files; $pos = strrpos($name = basename($name), '.'); $ext = $pos === false ? '.' : substr($name, $pos + 1); return in_array($ext, str2ary($u_php_files)); } function lint_php($n, $fn, $txt) { if (!self::is_php($fn)) return; file_put_contents('_dev/temp/_sp', $txt); chdir('_dev/temp'); #*** exec('php -l _sp', $out); chdir('../../'); if ('No syntax errors detected in _sp' === $out[0]) return; $ln = '?'; if (preg_match("/^Parse error: (.*?) in _sp on line (\d+)$/", $out[1], $m)) { $out[1] = $m[1]; $ln = $m[2]; } $this->err("Error at N$n: File `$fn`, line $ln: " . sprintf(span_b, "PHP $out[1]"), true); } function add_cp($new_id, $is_mess = false) { for ($ids = []; list($id, $parents) = each($this->cp); ) if (false === strpos($parents, ",$new_id,")) $ids[] = $id; if ($ids) { $ids = implode(',', $ids); sql("$this->parents = if (parents is null,',$new_id,',concat(parents,'$new_id,')) where id in ($ids)"); if ($is_mess) $this->err("Child<->parent added to ID=($ids)"); } } static function tune($src, $renew, $lint_php, $family) { $me = new Record; if ($save = is_array($src)) { extract($_POST, EXTR_PREFIX_ALL | EXTR_REFS, 'r'); list($r_id, $r_status) = $src; } else { if (!eval(sql("^select * from _dev_codebase where $src"))) return false; global $dev_rubrics; $me->id = $r_id; $me->name = $r_rubric_id ? "$r_name - $dev_rubrics[$r_rubric_id]" : $r_name; $r_status = explode(' ', $r_status, 7); if (isset($r_status[6])) array_pop($r_status); $r_status = implode(' ', $r_status); } $r_childs = trim($r_childs, ','); $tabs = $r_status[0]; $childs = $out = []; $ns = self::ary([$tabs ? sp2tabs($r_code) : unl($r_code), $r_name], $me); $nl = ''; foreach ($ns as $i => $one) { if ('WRITE FILE' == $one[0]) { $one[2] = implode("\n", $one[2]); if (2 == $renew && preg_match("/\{\\\$a_\w+\}/s", $one[2])) $me->err("Warning at N$i: updating not required due to {\$a_..} signature"); elseif ($renew) { if (is_array($s = getfile($one[1], $ftype = rectype($one[1])))) { $me->err("Error at N$i: $s[0]", $s[1]); # . sprintf(span_b, ) } else { $old = $one[2]; $one[2] = 1 == $ftype ? ($tabs ? sp2tabs($s) : unl($s)) : $s; if ($old !== $one[2]) $me->err("+File `$one[1]` updated in Codebase!"); } } if ($lint_php) $me->lint_php($i, $one[1], $one[2]); $one[2] = [$one[2]]; } elseif ('RUN RECORDS' == $one[0]) { $childs = array_merge($childs, $list = list_exp($one[1], ',')); if ($r_id && in_array($r_id, $list)) { $me->err("Warning at N$i: linked to self record", true); } if ($r_id && $r_id < 50000) foreach ($list as $id) if ($id > 50000) { $me->err("Warning at N$i: fixed to relative link", true); break; } } if ($i) $out[] = "$nl# $one[0]: $one[1] #"; if ($one[2]) $out[] = cbesc(implode("\n", $one[2])); $nl = "\n"; } $me->size = strlen($me->code = implode("\n", $out)); $me->code === $r_code and !$save or $me->ary += ['code' => escape($me->code)]; if ($childs) { sort($childs, SORT_NUMERIC); reset($childs); $childs = array_unique($childs); if ($family) { $me->cp = sqlf("@select id, parents from _dev_codebase where id in (%s) order by id", $childs); if ($ids = array_diff($childs, array_keys($me->cp))) { $me->err(sprintf("Reference to non existing (%s) record" . (@$ids[1] ? 's' : ''), implode(',', $ids)), true); } if ($r_id) $me->add_cp($r_id, !$save); } } if ($r_childs !== ($str = implode(',', $childs))) { $me->ary += ['childs' => $childs ? "',$str,'" : 'null']; $save or $me->err("Childs links fixed"); } if ($family && $r_id) { if ($ids = array_diff(str2ary($r_childs, ','), $childs)) { $ids = implode(',', $ids); sql("$me->parents = if (parents=',$r_id,',null,replace(parents,',$r_id,',',')) where id in ($ids)"); $save or $me->err("Child<->parent deleted from ID=($ids)"); } if ($r_parents) { $list = sqlf("@select id, childs from _dev_codebase where id in (%s) order by id", trim($parents = $r_parents, ',')); for ($ids = []; list($id, $childs) = each($list); ) if (false === strpos($childs, ",$r_id,")) { $parents = str_replace(",$id,", ',', $parents); $ids[] = $id; } if ($parents != $r_parents) { $me->ary += ['parents' => ',' == $parents ? 'null' : "'$parents'"]; $me->err('Parent<->child deleted: ' . list_imp($ids, 3)); } } } $fix = 1 != @$r_package_id && $r_id > 0 && $r_id < 10001; if (isset($r_package_id) && $save || $fix) { if ($fix) { $r_package_id = 1; $me->err("package_id tuned to 1"); } $me->ary += ['package_id' => $r_package_id]; } if (self::$err || $save) { if (self::$err) $r_status .= ' ' . self::$err; $me->ary += ['status' => escape($r_status)]; } return $me; } private $lnum = ''; private $back = ''; private $disp = 0; private $lenb = 0; private $is_svg = 0; private $desc; private $img; static $me; const lay_l = '<table cellpadding="0" cellspacing="0"><tr><td class="tdlnum">'; const lay_m = '</td><td style="padding-left:1px;vertical-align:top">'; const lay_r = '</td></tr></table>'; static function view($cbid, &$ftype, $lines) { self::$me = $me = new Record; $emp = $txt = $out = ''; $n = $pos = $first = 0; $me->img = "$cbid.0"; $com = 'WRITE FILE'; global $u_lang_priority; $me->desc = array_flip($u_lang_priority); foreach ($lines as $i => $line) { if (substr($line, 0, 2) == '# ' && ($emp || !$i) && preg_match(MATCH_COM, $line, $m)) { if ($emp) $txt = substr($txt, 0, -1); if ($txt) $out .= $me->show_thing($txt, 'DESC' == $com ? $pl : false, $m[1], $n, $ftype); $first or $first = $txt ? 1 : 2; $me->img = "$cbid." . ($i + 1); $txt = $emp = ''; $com = $m[1]; $ftype = $com == 'WRITE FILE' ? rectype($m[2]) : 0; if ($com == 'DESC') $pl = $m[2]; else { $n++; $c = substr($line, 2, -2); if ('SKY' == substr($c, 0, 3)) $c = '<a href="?main=sky&id=' . "$cbid.$n" . '" class="csky">' . "$c</a>"; $out .= '<div class="com"><span id="c' . $n . '" class="id">•</span><span style="color:red">N' . $n . ':</span><b>' . "$c</b>"; $out .= 'HEAD' == substr($c, 0, 4) ? ' <span class="pop" id="pop_commands">?</span></div>' : '</div>'; } } elseif ($line === '') { $emp = true; $txt .= "\n"; } else { $txt .= ($txt === '' ? '' : "\n") . cbunesc($line); $emp = false; } } if ($m[1] = '' or $txt) $out .= $me->show_thing($txt, 'DESC' == $com ? $pl : false, $m[1], $n, $ftype); $ftype = $first ? $first - 1 : 0; return $out; } # 2do: use this function depend of $u_php_files static function show_php($str, $bc = '') { $me = empty(self::$me) ? new Record : self::$me; if ($str === -1) return ''; $me->lnum = ''; $me->lenb = strlen($me->back = $bc); $me->disp = 0; if ($tag = preg_match("/^\s*[\$]\w+/sm", $str)) $str = "<?php $str"; $str = str_replace(["\r", "\n", '<code>','</code>'], '', highlight_string($str, true)); if ($tag) $str = preg_replace("|^(<span [^>]+>)<span [^>]+><\?php |", "$1", $str); $lines = explode('<br />', preg_replace("|^(<span [^>]+>)<br />|", "$1", $str)); array_walk($lines, [$me, 'add_line_no']); return self::lay_l . $me->lnum . self::lay_m . '<pre style="margin:0">' . implode('', $lines) . '</pre>' . self::lay_r; } private function show_thing($txt, $pl, $new, $n, $ftype) { if ($pl) { if (isset($this->desc[$pl])) $this->desc[$pl] = $txt; return 'DESC' == $new ? '' : $this->show_desc($n); } else switch($ftype) { case 2: return $this->show_img($txt); case 3: return $this->show_bin($txt); default: return self::show_php($txt); } } private function show_desc($n) { $out = $llt = ''; foreach ($this->desc as $lng => $txt) if (strlen($txt) > 2) { $dis = $out ? 'none' : 'block'; $llt .= sprintf("<br><a href=\"javascript:;\" onclick=\"lng('$lng',this)\" class=\"lng %s\">$lng</a>", $out ? '' : 'green'); if ($pos = strpos($txt, "\n")) { $head = '<span style="font-size:16px;border-bottom:1px solid red">'.substr($txt, 0, $pos).'</span>'; $txt = '<p>'.substr($txt, $pos + 1); } else $head = ''; $out .= "<div style=\"display:$dis\" id=\"$lng$n\" class=\"desc\">$head$txt</div>"; $this->desc[$lng] = ''; } return self::lay_l . " $llt" . self::lay_m . ($n ? "<hr>$out" : $out) . self::lay_r; } private function show_bin($str) { $length = strlen($str); $str = substr(base64_decode(str_replace("\n", '', $str)), 0, 384); $len = strlen($str); for ($out = $lines = '', $i = 0; $i < $len; $i++) { # 2do: use bin2hex() $mod = $i % 16; $hex = strtoupper(dechex($dec = ord($str{$i}))); $chr = $dec < 32 || $dec > 127 ? '.' : strtr($str{$i}, ['<' => '<', '&' => '&']); if ($mod == 0) { $line = $chr; $lines .= pad00(strtoupper(dechex(floor($i / 16))))."0<br>"; } else $line .= $chr; $out .= ' ' . ($dec < 16 ? "0$hex" : $hex) . ($mod == 7 ? ' |' : '') . ($mod == 15 ? " $line\n" : ''); } if ($length > $len) { $out .= ' ... ' . sprintf(span_r, '--file cutted--'); $lines .= 180; } return self::lay_l . $lines . self::lay_m . '<pre style="margin:0">' . "$out</pre>" . self::lay_r; } private function show_img($txt) { return self::lay_l . " " . self::lay_m . ($this->is_svg ? $txt : "<img src=\"?img=$this->img\">") . self::lay_r; } private function add_line_no(&$val, $key) { $val = strtr($val, ['=TOP-TITLE=' => '=TOP-TITLE=']); $colors = ['' => '', '=' => '', '*' => 'ff7', '+' => 'dfd', '-' => 'fdd', '.' => 'eee']; $pad = $c = ''; if (!$key) for(; $this->lenb > $key + $this->disp && $this->back{$key + $this->disp} == '.'; $this->disp++) $this->lnum .= "<br>\n" and $pad .= '<div class="code" style="background:#eee"> </div>'; if ($this->lenb > $key + $this->disp) $c = $colors[$this->back[$key + $this->disp]]; $key++; $this->lnum .= pad00($key, 3)."<br>\n"; if ($val === '') $val = ' '; elseif ($val == '</span>') $val = ' </span>'; elseif ($val == '</span></span>') $val = ' </span></span>'; $val = $pad.($c ? '<div class="code" style="background:'."#$c\">$val</div>" : "$val\n"); for (; $this->lenb > $key + $this->disp && $this->back{$key + $this->disp} == '.'; $this->disp++) # = - + * . $this->lnum .= "<br>\n" and $val .= '<div class="code" style="background:#eee"> </div>'; } } |