Esempio n. 1
0
 // Note: (at this point in execution)
 // If the entity contains binary fields, their values have been initialized from the $_POST array; and not the $_FILES array
 // Even if an existing entity was loaded with a binary value, that value could have been overriden with a value from $_POST
 // Validate
 $errs = EntityValidator::validate($entity);
 // Process file uploads for binary types
 foreach ($entityBP->fields() as $field) {
     if ($field->getDataType() == "binary") {
         $key = $field->getKey();
         Log::debug("Processing file upload for: {$key}");
         /*
         // Check for file upload
         */
         // construct file name
         $entityKey = substr($entitySignature, 0, strpos($entitySignature, "."));
         $fileExtension = Binary::extension($field->getMimeType());
         $filename = $entityKey . "." . $key . "." . $entityId . "." . $fileExtension;
         // determine destination for temporary file
         $path_upload_today = BPConfig::$path_upload . date("Y-m-d") . "/";
         $path_upload_user = $path_upload_today . $session_id . "/";
         $file_upload = $path_upload_user . $filename;
         Log::debug("Checking for file <{$file_upload}>");
         if (is_readable($file_upload)) {
             // NOTE: file was validated by /conduit/upload.php
             $size = filesize($file_upload);
             Log::debug("...Found file ({$size})");
             $entity->set($key, file_get_contents($file_upload));
             $entity->length($key, $size);
             if (count($errs) == 0) {
                 // clean up temporary files
                 @unlink($file_upload);
 public static function export(Blueprint $blueprint, $type = "tar", $where = NULL, array $filters = NULL)
 {
     // NOTE: type=(tar, csv)
     $tag = "EntityExporter::export()";
     Log::notice("{$tag}");
     // create export path
     $path_tmp = BPConfig::$path_tmp;
     $path_unique = $path_tmp . uniqid("bp") . "/";
     $path_export = $path_unique . $blueprint->signature() . "/";
     Log::debug("{$tag}: path_export = {$path_export}");
     $fields = $blueprint->fields();
     // get data to export
     try {
         $results = EntityExporter::prepareData($blueprint, $where, $filters);
     } catch (Exception $e) {
         throw $e;
     }
     // create tmp directory for tar files
     if ($type == "tar") {
         if (is_writable($path_tmp)) {
             if (!mkdir($path_export, 0777, true)) {
                 throw new Exception("{$tag}: Failed to mkdir: {$path_export}");
             }
         } else {
             throw new Exception("{$tag}: Cannot write to " . $path_tmp);
         }
     }
     // start output buffer
     $csv = "";
     $csv .= "id,";
     // add header row
     foreach ($fields as $f) {
         $displayAs = $f->getKey();
         $csv .= "{$displayAs},";
         if ($f->isForeignKey()) {
             // Add column header for foreign value
             $csv .= $f->getForeignValue() . ",";
         }
     }
     $csv = substr($csv, 0, strlen($csv) - 1);
     // remove trailing comma ","
     $csv .= "\n";
     // add result rows
     for ($i = 0; $i < $results->get_num_rows(); $i++) {
         $row = $results->get_next_row();
         $csv .= $row->id . ",";
         foreach ($fields as $f) {
             $key = $f->getKey();
             // retrieve value
             if ($f->isForeignKey()) {
                 // Insert local value before foreign value
                 $value = $row->{$key};
                 $csv .= "{$value},";
                 // Retrieve foreign value and continue with formatting below...
                 $foreignValue = str_replace(".", "_", $f->getForeignValue());
                 $value = $row->{$foreignValue};
             } else {
                 $value = $row->{$key};
             }
             // format values
             if (!empty($value) || $value == 0) {
                 // format values based on dataType
                 switch ($f->getDataType()) {
                     case "int":
                         if ($f->isForeignKey()) {
                             $value = "\"{$value}\"";
                         }
                         break;
                     case "string":
                         // encrypted values
                         if ($f->getEncType() == "md5" || $f->getEncType() == "sha1") {
                             $value = "******";
                         } else {
                             $value = addslashes($value);
                             $value = "\"{$value}\"";
                         }
                         break;
                     case "text":
                         $value = "\"{$value}\"";
                         break;
                     case "binary":
                         // generate filename
                         $filename_bin = $blueprint->getKey() . "." . $key . "." . $row->id . "." . Binary::extension($f->getMimeType());
                         // write binary data to file
                         if ($type == "tar") {
                             $path_bin = $path_export . $filename_bin;
                             Log::debug("{$tag}: Creating binary file: {$path_bin}");
                             if (!file_put_contents($path_bin, $value)) {
                                 Log::warning("{$tag}: Failed to create binary file: {$path_bin}");
                             }
                         }
                         // replace raw data with filename
                         $value = $filename_bin;
                         break;
                     default:
                         $value = addslashes($value);
                 }
                 // TODO: format values based on ListBlueprint formatter
             }
             // END: if( (!empty($value)) || ($value==0) )
             $csv .= "{$value},";
         }
         $csv = substr($csv, 0, strlen($csv) - 1);
         // remove trailing comma ","
         $csv .= "\n";
     }
     // create tar
     if ($type == "tar") {
         // create blueprint xml file in export path
         $path_blueprint = $path_export . $blueprint->signature();
         $xmlStr = $blueprint->asXml();
         file_put_contents($path_blueprint, $xmlStr);
         // create csv file in export path
         $path_csv = $path_export . $blueprint->getKey() . ".csv";
         file_put_contents($path_csv, $csv);
         $path_tar = $path_unique . $blueprint->getKey() . ".tar";
         $cmd = "tar --create --directory={$path_unique} --file={$path_tar} --gzip " . $blueprint->signature();
         Log::debug("{$tag}: Executing: {$cmd}");
         exec($cmd);
         // retrieve contents of tar file
         $tar = file_get_contents($path_tar);
         // cleanup
         $glob = glob($path_export . "*");
         foreach ($glob as $path_clean) {
             if (!unlink($path_clean)) {
                 Log::warning("{$tag}: Failed to unlink: {$path_clean}");
             }
         }
         if (!rmdir($path_export)) {
             Log::warning("{$tag}: Failed to rmdir: {$path_export}");
         }
         if (!unlink($path_tar)) {
             Log::warning("{$tag}: Failed to unlink: {$path_tar}");
         }
         if (!rmdir($path_unique)) {
             Log::warning("{$tag}: Failed to rmdir: {$path_unique}");
         }
         return $tar;
     } else {
         if ($type == "csv") {
             return $csv;
         }
     }
 }
        $bp = BlueprintReader::read($blueprint_file_name);
        // lookup the field
        $f = $bp->get($field);
        // retrieve meta data for file creation
        $mimeType = $f->getMimeType();
        // retrieve the binary as a string
        $binaryString = Binary::getString($blueprint, $field, $id);
    } catch (Exception $e) {
        Log::error($e->getMessage());
        echo "EXCEPTION: " . $e->getMessage();
        exit;
    }
    switch ($action) {
        case "file":
            // create filename
            $ext = Binary::extension($mimeType);
            $filename = $blueprint . "." . $field . "." . $id . "." . $ext;
            // set headers
            header("Content-Type: {$mimeType}");
            header("Content-Disposition: attachment; filename={$filename}");
            header("Content-Transfer-Encoding: binary");
            echo "{$binaryString}";
            break;
        case "stream":
            // set headers
            header("Content-Type: {$mimeType}");
            echo "{$binaryString}";
            break;
    }
}
Log::debug("END: /blueprints/binary.php" . "\n");