Запись [c:_dev/main/external.php] < dev.sky.1.001
Версия 1.000
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 | <?php defined('START') and AUTH_OK or die; class External { private function _dsp($m) { if (is_array($m)) $m = $m[1]; return eval($this->dsp_rule) ? $m + $this->dsp : $m; } function fix_family($list) { return preg_replace_callback('|(\d+)|', [$this, '_dsp'], $list); } function fix_code($ary) { $out = []; $flag = ''; foreach ($ary as $i => $rec) { if ('RUN RECORDS' == $rec[0]) { $rec[1] = list_imp($this->fix_family(list_exp($rec[1], ','))); } elseif ('OPEN CELL' == $rec[0]) { # 2do } if ($i) $out[] = "$flag# $rec[0]: $rec[1] #"; if ($rec[2]) $out[] = cbesc(implode("\n", $rec[2])); $flag = "\n"; } return implode("\n", $out); } function fix_record(&$ary) { $ary['id'] = $this->_dsp($ary['id']); $ary['parents'] = $this->fix_family($ary['parents']); $ary['childs'] = $this->fix_family($ary['childs']); $cbr = Record::ary([$ary['code'], $ary['name']], $rec = new Record); if ($rec->mess) return $rec->mess(); # if an error, 2check ! $ary['code'] = $this->fix_code($cbr); return 0; # no errors } private $format_rec = 'id name version rubric_id used/author/status/dt/dt_upd/comment/parents/childs/code'; private $format_pack = 'name version lang rubric_id start_id/status/dt/tmemo'; private $dsp; private $dsp_rule = 'return $m > 50000;'; function __construct($dsp, $dsp_rule = false) { $this->dsp = $dsp; if ($dsp_rule) $this->dsp_rule = $dsp_rule; } function export_rec($ary, &$name, $pack = '_') { $ary['author'] or $ary['author'] = 'null'; if ($this->fix_record($ary)) return 'Error in record'; $body = "pack $this->format_rec\n$pack"; $rec = explode(' ', $this->format_rec); $end = array_pop($rec); foreach ($rec as $one) $body .= ' ' . $ary[$one]; $rec = explode('/', $end); $body .= ' ' . $ary[array_shift($rec)]; foreach ($rec as $one) $body .= "\n" . ('author' == $one && 'null' == $ary[$one] ? 'energy' : $ary[$one]); $fn = strtr($ary['name'], ['/' => '_', ':' => '_']); for ($i = 0; !$i || file_exists($name); $i++) $name = preg_replace("/([\._])+/", "$1", sprintf("_dev/temp/$fn%s.dev", $i ? "_$i" : '')); return file_put_contents($name, $body) ? 0 : 'Error writing record file'; } function import_rec($txt, $pack_id = 0, $p3 = 0) { # $name $lines = str2ary(unl($txt), "\n"); if (!$lines || "pack $this->format_rec" != $lines[0]) return 'format error'; $ary = array_combine(str2ary(array_shift($lines)), str2ary(array_shift($lines))); $pack = array_shift($ary); end($ary); $key = str2ary(key($ary), '/'); if ('code' != array_pop($key)) return 'syntax error'; $second = [array_shift($key) => array_pop($ary)]; $third = array_combine($key, array_splice($lines, 0, count($key))); $ary += $second + $third + ['code' => $lines, 'package_id' => $pack_id]; if ($this->fix_record($ary)) return 'Error in record'; $ary['status'][2] = 0; # my vote $ary['used'] = 0; # my vote foreach ($ary as &$v) is_numeric($v) or $v = $v ? escape($v) : 'null'; if (sql("+select 0 from _dev_codebase where id=$ary[id] union select 1")) { return (int)sqlf("insert into _dev_codebase %s", $ary); # no errors, inserted } if (!$p3 || $pack_id) return "record $ary[id] already exists, update not allowed"; sqlf("update _dev_codebase set %s where id=$ary[id]", $ary); return 0; # no errors, updated } static function export_tmemo($str, $cnt) { $lines = explode("\n", trim(preg_replace("/\n+/s", "\n", unl($str)), "\n")); foreach ($lines as &$v) { if (!preg_match("/^\\$([a-z_][a-z_\d]*) = (-?(\d+\.)?\d+|'.*');$/i", $v, $m)) return ''; $v = "'" == $m[2][0] ? strtr(substr($m[2], 1, -1), ["\\\\" => "\\", "\\'" => "'"]) : $m[2]; $v = "$m[1] " . ('cnt' == $m[1] ? $cnt : $v); } return implode("\n", $lines); } static function import_tmemo($lines, &$sky_id, $cnt) { is_array($lines) or $lines = explode("\n", $lines); $ts = time(); foreach ($lines as &$v) { if (!preg_match("/^([a-z_][a-z_\d]*) (.*)$/i", $v, $m)) return ''; if ('sky_id' == $m[1] && is_numeric($m[2])) { if (-1 == $sky_id) $sky_id = (int)$m[2]; else $m[2] = $sky_id; } 'ts' == $m[1] and $m[2] = $ts and $ts = 0; $v = "\$$m[1] = " . (is_numeric($m[2]) ? $m[2] : var_export($m[2], true)) . ';'; if ('cnt' == $m[1] && $cnt != $m[2]) return ''; } if ($ts) $lines[] = "\$ts = $ts;"; return -1 == $sky_id ? '' : implode("\n", $lines); } static function pack_name($str, $path = true) { $str = preg_replace("/([\._])+/", "$1", "_$str.pack"); return $path ? "_dev/temp/$str" : $str; } static function pack_arch($str, $from_table = true) { if ($from_table) $str = self::pack_name($str); return substr($str, 0, -4) . 'sky'; } function export_pack($r, $name = 0, $cnt = 0) { if ($cnt && file_exists($name)) return "Package file `$name` already exists"; if ($r['start_id'] > 50000 && $this->dsp < 0) $r['start_id'] += $this->dsp; $body = "cnt $this->format_pack\n$cnt"; $pack = explode(' ', $this->format_pack); $end = array_pop($pack); foreach ($pack as $one) $body .= ' ' . $r[$one]; $pack = explode('/', $end); $body .= ' ' . $r[array_shift($pack)]; foreach ($pack as $one) $body .= "\n" . ('tmemo' == $one ? self::export_tmemo($r[$one], $cnt) : $r[$one]); if (!$cnt) return $body; return file_put_contents($name, $body) ? 0 : 'Error writing package file'; } function import_pack($p0, &$cnt, $sky_ids = 0, $pack_id = 0) { # first_user_name@pack_name:version ? if ($is_list = is_array($p0)) { $type = '01'; # found packages } else { $cnt = 0; chdir('_dev/temp'); if ('.sky' == substr($p0, -4)) { if (is_string($cnt = $this->x_explode($p0))) return $cnt; $p0 = substr($p0, 0, -3) . 'pack'; $cnt--; # ? } $txt = @file_get_contents($p0); chdir('../..'); if (false === $txt) return "Cannot read file `_dev/temp/$p0`"; if ($pack_id) { # downloaded package sqlf("update _dev_packages set %s where id=$pack_id", [ 'status' => "concat('11',substr(status,3))", 'start_id' => "if (start_id < 50001, start_id, start_id+$this->dsp)", ]); return (int)$pack_id; } if (!$sky_ids) $sky_ids = self::packages(); $p0 = [$txt]; $type = '11'; } foreach ($p0 as $sky_id => $one) { $lines = str2ary(unl($one), "\n"); if ($lines && $is_list) $lines = $this->fix_psr($lines); if (!$lines || "cnt $this->format_pack" != $lines[0]) return 'format error'; $ary = array_combine(str2ary(array_shift($lines)), str2ary(array_shift($lines))); $cnt = array_shift($ary); end($ary); $key = str2ary(key($ary), '/'); if ('tmemo' != array_pop($key)) return 'syntax error'; $is_list or $sky_id = -1; $ary += [array_shift($key) => array_pop($ary)] + array_combine($key, array_splice($lines, 0, count($key))) + ['tmemo' => $tmemo = self::import_tmemo($lines, $sky_id, $cnt)]; if (!$tmemo) return 'cnt not equal or tmemo format not good'; $sid = $ary['start_id']; $name = $ary['name']; if (!is_numeric($sid) || $sid <= 0 || $sid > 50001 || $sid < 50001 && (cell($sid, 'c', 'id') || sql("+select 1 from _dev_packages where start_id=$sid union select 0")) ) return 'error in start_id'; if ($sky_id < 0) return 'syntax error'; if ($sky_id && in_array($sky_id, $sky_ids)) return "Package with `SKY_ID=$sky_id` already loaded"; $ary['start_id'] = $this->_dsp($sid); $ary['status'] = $type . substr($ary['status'], 2); foreach ($ary as &$v) is_numeric($v) or $v = escape($v); $name_ver = sql("+select count(id) from _dev_packages where name=$ary[name] and version=$ary[version]"); if ($name_ver) return sprintf("Package `%s` already exists", ver($ary['version'], $name)); # if ($h[6] > 50000 && $this->dsp) $h[6] += $this->dsp; $pack_id = sqlf("insert into _dev_packages %s", $ary); } return (int)$pack_id; } function fix_psr($lines) { $y = explode('/', $this->format_pack); $x = explode(' ', array_shift($y)); $tmemo = array_splice($lines, count($x) + count($y)); $cnt = explode(' ', array_pop($lines)); if (2 != count($cnt) || 'cnt' != $cnt[0]) return false; $out = ["cnt $this->format_pack"]; $row = $cnt[1]; foreach ($x as $one) { if (!$lines) return false; list ($name, $val) = explode(' ', array_shift($lines), 2); if ($name != $one) return false; $row .= " $val"; } $out[] = $row; array_pop($y); foreach ($y as $one) { if (!$lines) return false; list ($name, $val) = explode(' ', array_shift($lines), 2); if ($name != $one) return false; $out[] = $val; } return array_merge($out, ["cnt $cnt[1]"], $tmemo); } static function packages($found = false, $list = []) { # search for existing sky_id and save new packages headers if (!$list) { $tmems = sql("@select tmemo from _dev_packages"); # where substr(status,2,1)='1' foreach ($tmems as $tmemo) { eval($tmemo); if ($sky_id > 0) $list[] = $sky_id; } $list = array_unique($list); } if (false === $found) return $list; # list for search request $ext = new External(0); return is_string($id = $ext->import_pack($found, $cnt, $list)) ? $id : 0; } function x_explode($fn) { $chunk = 8192; if (false === ($bz = @bzopen($fn, 'r'))) return "Cannot open file `$fn`"; if (substr($str = bzread($bz, $chunk), 0, 17) == "### folders: ###\n") $str = substr($str, 17); $end = 'end package:'; $cnt = 0; while (preg_match("@^### (file: ([^:/\s]+): (\d+)|$end) ###@", $str, $m)) { if ($m[1] == $end) { if (feof($bz) && $m[0] == $str) $end = 0; break; } $str = substr($str, 1 + strlen($m[0])); if (false === ($fw = fopen($m[2], 'wb'))) return "Cannot open file `$m[2]` for writing"; $done = $size = fwrite($fw, $str, $m[3]); while ($done < $m[3]) { if (feof($bz)) return 'End of file reached'; $done += $size = fwrite($fw, $str = bzread($bz, $chunk), $m[3] - $done); # FILE WRITE } fclose($fw); $cnt++; if (!feof($bz) && strlen($str) - $size - 1 < $chunk) $str .= bzread($bz, $chunk); # READ if ($size) $str = substr($str, 1 + $size); } bzclose($bz); if ($end) return 'Error match signature'; return $cnt; } static function get_votes(&$collected) { for ($ext = $collected = [], $q = sql("select * from _dev_packages order by id"); eval(fetch); ) { eval($r_tmemo); # 0 1 2 3 4 5 if (!$d_voted || !$sky_id) continue; # ===== $recs = sql("@select 1+id-$r_start_id, substr(status,3,1) from _dev_codebase where package_id=$r_id and substr(status,3,1) in (2,4,3)"); $ary = []; foreach ([3, 4, 2] as $val) $ary[] = implode(',', array_keys(array_filter($recs, create_function('$v', "return $val == \$v;")))); $ext[] = "$sky_id:$r_status[4]~" . implode('~', $ary); $collected[$r_id] = $ary + [3 => $r_start_id]; } return $ext ? ['votes' => implode(' ', $ext)] : $ext; } static function unmark_votes($collected) { foreach ($collected as $id => $ary) { $start_id = array_pop($ary) - 1; foreach ([0, 5, 1] as $i => $val) if ($ary[$i]) sql("update _dev_codebase set status=concat(substr(status,1,2),'$val',substr(status,4)) where (id-$start_id) in ($ary[$i])"); set('d_voted', 0, -$id); } set('d_voted', 0, 2); } static function curl_header($curl, $row) { if (preg_match("/^Set-Cookie: sky=([^;]+); expires=([^;]+);/", $row, $m)) set('d_sky', strtotime($m[2]) . ":$m[1]"); return strlen($row); } static function curl_progress() { $ary = func_get_args(); is_int($ary[0]) or array_shift($ary); list($all_in, $in, $all_out, $out) = $ary; if (!$all_out) $n = 0; elseif ($all_out == $out) $n = 100; else 100 != ($n = round(100 * $out / $all_out)) or $n = 99; set('percent', "$n%"); return 0; } static function gate($in, $is_multipart = false) { global $u_reg_access_code, $u_dev_lang, $d_sky; trace($in += ['lg' => $u_dev_lang], 'GATE REQUEST'); $file = $size = $gate_err = $response = 0; $cookie = "key=$u_reg_access_code"; if (@$d_sky) { list($ts, $d_sky_v) = explode(':', $d_sky); if (time() < $ts) $cookie .= "; sky=$d_sky_v"; } if ($is_multipart) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, SKY . 'gate'); curl_setopt($curl, CURLOPT_COOKIE, $cookie); curl_setopt($curl, CURLOPT_USERAGENT, 'DEV.SKY.'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_NOPROGRESS, false); curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, ['External', 'curl_progress']); curl_setopt($curl, CURLOPT_HEADERFUNCTION, ['External', 'curl_header']); curl_setopt($curl, CURLOPT_POSTFIELDS, $in); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); if ($no = curl_errno($curl)) $gate_err = "CURL error: " . curl_strerror($no); else trace(curl_getinfo($curl)); curl_close($curl); } else { ini_set('user_agent', 'DEV.SKY.'); $opts = ['http' => [ 'method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\nCookie: key=$u_reg_access_code", # $cookie 'content' => http_build_query($in), ]]; if ($fr = fopen(SKY . 'gate', 'rb', false, stream_context_create($opts))) { $md = stream_get_meta_data($fr); foreach ($md['wrapper_data'] as $row) if (preg_match("/^Content\-Length: (\d+)$/i", $row, $m)) $size = $m[1]; elseif (preg_match("/^.*?filename=\"([^\"]+)\"$/i", $row, $m)) $file = "_dev/temp/$m[1]"; else self::curl_header(null, $row); if ($file) { if (file_exists($file)) for ($i = 0; !is_writable($file); $i++ < 5) sleep(1); @unlink($file); if (file_exists($file)) $gate_err = "File `$file` already exists and not deleted"; elseif ($fw = fopen($file, 'wb')) { sqlf("update _dev_memory set imemo=$size, cmemo=%s, dt=now() where id=7", escape(basename($file))); for (; !feof($fr); fwrite($fw, fread($fr, 8192))); fclose($fw); } else $gate_err = "Cannot write file `$file`"; } else { $response = fread($fr, 102400); } fclose($fr); } else $gate_err = 'Cannot open gate'; } $out = ['file' => $file, 'size' => $size, 'gate_err' => $gate_err]; if (!$gate_err && !$file) { if ('BZ' == substr($response, 0, 2)) $out += unserialize(bzdecompress($response)); else { $err = '' == $response ? 'empty response' : 'BZ'; $out['gate_err'] = "Gate ($err) error"; if ('read' == $in['request']) sql("update _dev_memory set imemo=-1, cmemo=null, dt=now() where id=7"); } } trace($out, 'GATE RESPONSE'); return (object)$out; } function mail($to, $attachment) { # 2DO, use in CopyTo $title = 'DEV.SKY. export'; } function ssh($host, $is_exec) { } function ftp($host) { } } |