Example #1
0
 public function handleResultEnded(&$result)
 {
     if (!$this->fixed) {
         return;
     }
     $f = new TempFile();
     if ($this->args['add_field_names']) {
         $a = array();
         foreach ($this->fieldNames as $i => $t) {
             if (isset($this->widths[$i])) {
                 $a[] = str_pad($t, $this->widths[$i] + $this->spacing);
             } else {
                 $a[] = $t;
             }
         }
         $f->write(implode($this->separator, $a) . $this->term);
     }
     fseek($this->file->file, 0);
     while (($a = fgets($this->file->file)) !== FALSE) {
         $a = explode("\t", $a);
         foreach ($a as $i => &$c) {
             if (isset($this->widths[$i])) {
                 $c = str_pad($c, $this->widths[$i] + $this->spacing);
             }
         }
         $f->write(implode($this->separator, $a));
     }
     $this->file->close();
     unlink($this->file->name);
     $this->file = $f;
 }
 /**
  * Produce diff between the contents
  *
  * @param   string left
  * @param   string right
  */
 protected function diff($left, $right)
 {
     with($templ = new TempFile(), $tempr = new TempFile(), $templ->open(FILE_MODE_WRITE), $tempr->open(FILE_MODE_WRITE));
     $templ->write($left);
     $tempr->write($right);
     $templ->close();
     $tempr->close();
     // TODO: Implement "diff" in userland
     try {
         $p = new Process(sprintf('diff -u %s %s', $templ->getURI(), $tempr->getURI()));
         $p->in->close();
         while (!$p->out->eof()) {
             $this->out->writeLine($p->out->readLine());
         }
         $p->close();
     } catch (IOException $e) {
         $this->err->writeLine('!=> Invocation of `diff` program failed.');
         $templ->unlink();
         $tempr->unlink();
         return;
     }
     $templ->unlink();
     $tempr->unlink();
 }
Example #3
0
<?php

/*--------------------------------------------------------------------*
 | Copyright (c) 2010-2013 Vayer Software Ltd. - All Rights Reserved. |
 *--------------------------------------------------------------------*/
require_once __INC__ . '/mysql.php';
require_once __INC__ . '/export_stuff.php';
date_default_timezone_set("UTC");
$q = MySQLQuery::SimpleQuery($LINK, 'SELECT VERSION()', TRUE);
if ($q === FALSE) {
    return $LINK->error;
}
$file = new TempFile(NULL, 'UTF-8', $ARGS['file_encoding']);
$db = $ARGS['db'];
$disFKeys = $ARGS['tables_data'] && $ARGS['no_foreign_keys'];
$file->write(getExportHeader($disFKeys, $q[0][0], $db));
function delimit(&$s)
{
    if (strrchr($s, ";")) {
        $delim = "\$\$";
        while (strstr($s, $delim)) {
            $delim .= $delim;
        }
        return "DELIMITER {$delim}\n\n{$s}{$delim}\n\nDELIMITER ;\n\n\n";
    }
    return $s . ";\n\n\n";
}
if ($ARGS['with_create_db']) {
    if ($ARGS['with_drop_db']) {
        $file->write("DROP DATABASE IF EXISTS `{$db}`;\n");
    }
Example #4
0
File: data.php Project: milkae/Php
         $pageSize = $cellSize = $dataSize = $rowsAmnt = 0;
     }
     $a = new MySQLQuery($c, $req['limited'] ? new QueryResultJSONWriter($pageSize) : NULL, $cellSize, $dataSize, $rowsAmnt);
     if (is_array($q)) {
         $aff = 0;
         for ($i = 0; $i < count($q); $i++) {
             $r = $a->query($q[$i], $p[$i], $req['flags']);
             $aff += $r['affectedRows'];
         }
         $a = array('affectedRows' => $aff);
     } else {
         $a = $a->query($q, $p, $req['flags']);
     }
     if ($req['toFile'] && $a['results']) {
         $f = new TempFile();
         $f->write($a['results'][0]['data'][0][0]);
         $a = array('file' => $f->name, 'alias' => 'cell-' . date('Ymd-His') . '.data');
     }
     respond($a);
     break;
 case 'get_query_result':
     $f = $req['file'] . '-' . $req['result'] . '-' . $req['page'];
     if (($s = file_get_contents($f)) === FALSE) {
         respondError("Can't read file: {$f}");
     }
     respond($s);
     break;
 case 'kill_query_result':
     array_walk(glob($req['file'] . '-*'), create_function('&$v, $k', 'unlink($v);'));
     break;
 case 'ping':
Example #5
0
 private function handleRequest($offset, $time)
 {
     Log::SetPrefix(sprintf("Thumbnail(%d): ", getmypid()));
     $hash = $this->getHash();
     if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $hash) {
         header(getenv("SERVER_PROTOCOL") . " 304 Not Modified");
         header("Content-Length: 0");
         exit;
     }
     $cachedir = CACHE_DIR;
     umask(077);
     if (!is_dir($cachedir)) {
         Log::Info("Creating cache directory %s", $cachedir);
         @mkdir($cachedir);
     }
     $file = $cachedir . '/' . $this->getHash() . '-' . $offset;
     if (is_file($file)) {
         $lm = filemtime($file);
         header("Content-Type: image/jpeg");
         header("Etag: \"" . $this->getHash() . "-" . $offset . "\"");
         header("Last-Modified: " . gmdate('D, d M Y H:i:s T', $lm));
         header("Expires: " . gmdate('D, d M Y H:i:s T', $lm + 86400));
         if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) && $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
             header(getenv("SERVER_PROTOCOL") . " 304 Not Modified");
             header("Content-Length: 0");
             Log::Debug("request already satisfied by HTTP_IF_MODIFIED_SINCE header, file=%s", $file);
             exit;
         }
         echo file_get_contents($file);
         exit;
     }
     $outdir = $cachedir . '/' . getmypid();
     if (!is_dir($outdir)) {
         Log::Debug("Creating work directory %s", $outdir);
         mkdir($outdir, 0700);
     }
     $tempfile = new TempFile(".ts");
     $infile = new File($this->stream, false);
     $infile->seek($offset);
     $data = $infile->read(1024 * 1024);
     $infile->close();
     $tempfile->write($data);
     $cmd = sprintf("%s -nolirc -really-quiet -zoom -quiet -xy %d -vo jpeg:outdir=%s:maxfiles=2 -ao null %s -frames 2 &> /dev/null", MPLAYER, $this->width(), $outdir, escapeshellarg($tempfile->Filename()));
     $ts = microtime(true);
     Log::Debug("command=%s", $cmd);
     exec($cmd);
     $elapsed = microtime(true) - $ts;
     Log::Debug("command executed, duration=%.6f sec", $elapsed);
     $tempfile = $this->chooseBestImage($outdir);
     Log::Debug("using image %s", $tempfile);
     if (!is_file($tempfile)) {
         Log::Error("command failed, command was %s", $cmd);
         header("HTTP/1.0 404 not found");
         exit;
     }
     $im = @imagecreatefromjpeg($tempfile);
     $timestring = sprintf("%02d:%02d:%02d", floor($time / 3600), floor($time % 3600 / 60), $time % 60);
     $this->writeTextAligned($im, self::ALIGN_LEFT, self::ALIGN_TOP, $timestring);
     ob_start();
     imagejpeg($im, '', 60);
     $data = ob_get_contents();
     ob_end_clean();
     $this->cleanDirectory($outdir);
     if ($data != '') {
         Log::Debug("finished generation, Size=%d", strlen($data));
         header("Content-Type: image/jpeg");
         file_put_contents($file, $data);
         $lm = filemtime($file);
         header("Last-Modified: " . gmdate('D, d M Y H:i:s T', $lm));
         header("Etag: \"" . $this->getHash() . "-" . $offset . "\"");
         header("Expires: " . gmdate('D, d M Y H:i:s T', $lm + 86400));
     } else {
         Log::Error("oops, data is empty, should not happen");
     }
     print $data;
     exit;
 }
Example #6
0
<?php

/*--------------------------------------------------------------------*
 | Copyright (c) 2010-2013 Vayer Software Ltd. - All Rights Reserved. |
 *--------------------------------------------------------------------*/
$res = "--\n-- DbNinja v" . VERSION . " for MySQL\n-- Date: " . date("Y-m-d H:i:s") . " (UTC)\n--\n\n";
foreach ($ARGS['users'] as $i) {
    $u = "'" . addslashes($i['user']) . "'@'" . addslashes($i['host']) . "'";
    if ($i['withDrop']) {
        $res .= "DROP USER {$u};\n";
    }
    if ($i['withCreate']) {
        $res .= "CREATE USER {$u};\n";
    }
    if ($i['withGrants'] && (!$i['withDrop'] || $i['withCreate'])) {
        $q = MySQLQuery::SimpleQuery($LINK, "SHOW GRANTS FOR {$u}", TRUE);
        if ($q === FALSE) {
            return $LINK->error;
        }
        $res .= $q[0][0] . ";\n";
    }
    $res .= "\n";
}
if ($ARGS['to_file']) {
    $f = new TempFile();
    $f->write($res);
}
return array('result' => $ARGS['to_file'] ? $f->name : $res);
Example #7
0
            }
            if ($trgType[$i] == 's' || $trgType[$i] == 'b' && !$hex) {
                $v = '"' . addcslashes($v, '"') . '"';
            } else {
                if ($trgType[$i] == 'n' && $v == '') {
                    $v = '""';
                }
            }
        }
        $b[] = $v;
    }
    $dataLineCnt++;
    $sql .= ($sql ? ",\n" : '') . '(' . implode(', ', $b) . ')';
    if (strlen($sql) >= 0x80000) {
        if ($toFile) {
            $file->write($sqlPrefix . $sql . ";\n");
        } else {
            if (!MySQLQuery::NoResultQuery($LINK, $sqlPrefix . $sql)) {
                return $LINK->error;
            }
        }
        $sql = '';
    }
    if ($lim > 0 && $dataLineCnt == $lim) {
        break;
    }
}
fclose($f);
if ($sql) {
    if ($toFile) {
        $file->write($sqlPrefix . $sql . ";\n");