Esempio n. 1
0
 function store_database($dbinfo, $doc)
 {
     global $Conf, $OK;
     $N = 400000;
     $idcol = $dbinfo->id_column;
     $while = "while storing document in database";
     $a = $ks = $vs = array();
     foreach ($dbinfo->columns as $k => $v) {
         if ($k !== $idcol) {
             $ks[] = "`{$k}`=?";
             $vs[] = substr($v, 0, $N);
         }
     }
     if (isset($dbinfo->columns[$idcol])) {
         $q = "update {$dbinfo->table} set " . join(",", $ks) . " where {$idcol}=?";
         $vs[] = $dbinfo->columns[$idcol];
     } else {
         $q = "insert into {$dbinfo->table} set " . join(",", $ks);
     }
     if (!($result = Dbl::query_apply($q, $vs))) {
         set_error_html($doc, $Conf->db_error_html(true, $while));
         return;
     }
     if (isset($dbinfo->columns[$idcol])) {
         $doc->{$idcol} = $dbinfo->columns[$idcol];
     } else {
         $doc->{$idcol} = $result->insert_id;
         if (!$doc->{$idcol}) {
             set_error_html($doc, $Conf->db_error_html(true, $while));
             $OK = false;
             return;
         }
     }
     for ($pos = $N; true; $pos += $N) {
         $a = array();
         foreach ($dbinfo->columns as $k => $v) {
             if (strlen($v) > $pos) {
                 $a[] = "`" . $k . "`=concat(`" . $k . "`,'" . sqlq(substr($v, $pos, $N)) . "')";
             }
         }
         if (!count($a)) {
             break;
         }
         if (!$Conf->q("update {$dbinfo->table} set " . join(",", $a) . " where {$idcol}=" . $doc->{$idcol})) {
             set_error_html($doc, $Conf->db_error_html(true, $while));
             return;
         }
     }
     // check that paper storage succeeded
     if ($dbinfo->check_contents && (!($result = $Conf->qe("select length({$dbinfo->check_contents}) from {$dbinfo->table} where {$idcol}=" . $doc->{$idcol})) || !($row = edb_row($result)) || $row[0] != strlen(self::content($doc)))) {
         set_error_html($doc, "Failed to store your document. Usually this is because the file you tried to upload was too big for our system. Please try again.");
         return;
     }
 }