Exemplo n.º 1
0
         $line_of_text = fgets($file_handle);
         $parts = explode('/n', $line_of_text);
         foreach ($parts as $img_capts) {
             list($img_filename, $img_caption) = explode('|', $img_capts);
             $img_captions[$img_filename] = $img_caption;
         }
     }
     fclose($file_handle);
 }
 // 3. LOAD FILES
 if ($file != "." && $file != ".." && $file != "folder.jpg") {
     // JPG, GIF and PNG
     if (preg_match("/.jpg\$|.gif\$|.png\$/i", $file)) {
         //Read EXIF
         if ($display_exif == 1) {
             $img_captions[$file] .= readEXIF($currentdir . "/" . $file);
         }
         checkpermissions($currentdir . "/" . $file);
         list($img_width, $img_height, $img_type, $img_attr) = getimagesize($currentdir . "/" . $file);
         $files[] = array("name" => $file, "date" => filemtime($currentdir . "/" . $file), "size" => filesize($currentdir . "/" . $file), "html" => "<li  class='thumbnail'>" . "<a href='" . $currentdir . "/" . $file . "' rel='gallery' title='{$img_captions[$file]}'>" . "<img class='detalle' data-original-title='" . $file . "' " . "data-content='Dimensiones: {$img_width} por {$img_height} pixels' src='" . GALLERY_ROOT . "phpThumb.php?src=" . $thumbdir . "/" . $file . "&amp;w={$thumb_size}&amp;h={$thumb_size}&amp;zc=1' alt='{$label_loading}' />" . "</a>" . "</li>");
     }
     // Other filetypes
     $extension = "";
     if (preg_match("/.pdf\$/i", $file)) {
         $extension = "PDF";
     }
     // PDF
     if (preg_match("/.zip\$/i", $file)) {
         $extension = "ZIP";
     }
     // ZIP archive
Exemplo n.º 2
0
         }
     }
 }
 // 3. LOAD FILES
 if ($file != "." && $file != ".." && $file != "folder.jpg") {
     if ($display_filename) {
         $filename_caption = "<em>" . padstring($file, $label_max_length) . "</em>";
     } else {
         $filename_caption = "";
     }
     // JPG, GIF and PNG
     if (preg_match("/.jpg\$|.gif\$|.png\$/i", $file)) {
         //Read EXIF
         if (!array_key_exists($file, $img_captions)) {
             if ($display_exif == 1) {
                 $exifReaden = readEXIF($currentdir . "/" . $file);
                 //Add to the caption all the EXIF information
                 $img_captions[$file] = $file . $exifReaden;
             } else {
                 //If no EXIF, just use the filename as caption
                 $img_captions[$file] = $file;
             }
         }
         // Read the optionnal image title and caption in html file (image.jpg --> image.jpg.html)
         // Format: title::caption
         // Example: My cat::My cat like to <i>roll</i> on the floor.
         // If file is not provided, image filename will be used instead.
         checkpermissions($currentdir . "/" . $file);
         if (is_file($currentdir . '/' . $file . '.html')) {
             $img_captions[$file] = $file . '::' . htmlspecialchars(file_get_contents($currentdir . '/' . $file . '.html'), ENT_QUOTES);
         }
/**
 *
 * @global type $rep_counter
 * @global string $rep_issues
 * @global array $fieldhelper_to_heurist_map
 * @param type $dir
 */
function doHarvestInDir($dir)
{
    global $rep_issues, $fieldhelper_to_heurist_map, $mediaExts, $progress_divid, $geoDT, $fileDT, $titleDT, $startdateDT, $enddateDT, $descriptionDT;
    $rep_processed = 0;
    $rep_added = 0;
    $rep_updated = 0;
    $rep_processed_dir = 0;
    $rep_ignored = 0;
    $f_items = null;
    //reference to items element
    $progress_divid++;
    ob_start();
    print "<div><b>{$dir}</b><span id='progress{$progress_divid}'></span></div>";
    ob_flush();
    flush();
    if (!is_writable($dir)) {
        print "<div style=\"color:red\">Folder is not writeable. Check permissions</div>";
        return 0;
    }
    $manifest_file = $dir . "fieldhelper.xml";
    //list of all files in given folder - need to treat new files that are not mentioned in manifest file
    $all_files = scandir($dir);
    if (file_exists($manifest_file)) {
        //read fieldhelper.xml
        if (is_readable($manifest_file)) {
            //check write permission
            if (!is_writable($manifest_file)) {
                print "<div style=\"color:red\">Manifest is not writeable. Check permissions.</div>";
                return 0;
            }
        } else {
            print "<div style=\"color:red\">Manifest is not readable. Check permissions.</div>";
            return 0;
        }
        $fh_data = simplexml_load_file($manifest_file);
        if ($fh_data == null || is_string($fh_data)) {
            print "<div style=\"color:red\">Manifest is corrupted</div>";
            return 0;
        }
        //MAIN 	LOOP in manifest
        $not_found = true;
        //true if manifest is empty
        foreach ($fh_data->children() as $f_gen) {
            if ($f_gen->getName() == "items") {
                $f_items = $f_gen;
                $not_found = false;
                $tot_files = count($f_gen->children());
                $cnt_files = 0;
                foreach ($f_gen->children() as $f_item) {
                    $recordId = null;
                    $recordType = RT_MEDIA_RECORD;
                    //media by default
                    $recordURL = null;
                    $recordNotes = null;
                    $el_heuristid = null;
                    $lat = null;
                    $lon = null;
                    $filename = null;
                    $filename_base = null;
                    //filename only
                    $details = array();
                    $file_id = null;
                    $old_md5 = null;
                    foreach ($f_item->children() as $el) {
                        //$key=>$value
                        $content = strval($el);
                        // (string)$el;
                        $key = $el->getName();
                        $value = $content;
                        if ($key == "md5") {
                            $old_md5 = $value;
                        } else {
                            if (@$fieldhelper_to_heurist_map[$key]) {
                                $key2 = $fieldhelper_to_heurist_map[$key];
                                if ($key2 == "file") {
                                    $filename = $dir . $value;
                                    $filename_base = $value;
                                    $key3 = $fieldhelper_to_heurist_map['file_name'];
                                    if ($key3 > 0) {
                                        $details["t:" . $key3] = array("1" => $value);
                                    }
                                    $key3 = $fieldhelper_to_heurist_map['file_path'];
                                    if ($key3 > 0) {
                                        $relative_path = getRelativePath(HEURIST_FILESTORE_DIR, $dir);
                                        $details["t:" . $key3] = array("1" => $relative_path);
                                        //change to relative path
                                    }
                                } else {
                                    if ($key2 == "lat") {
                                        $lat = floatval($value);
                                    } else {
                                        if ($key2 == "lon") {
                                            $lon = floatval($value);
                                        } else {
                                            if ($key2 == "recordId") {
                                                $recordId = $value;
                                                $el_heuristid = $el;
                                            } else {
                                                if (intval($key2) > 0) {
                                                    //add to details
                                                    $details["t:" . $key2] = array("1" => $value);
                                                }
                                            }
                                        }
                                    }
                                }
                                // else field type not defined in this instance
                            }
                        }
                    }
                    //for item keys
                    if ($filename) {
                        //exclude from the list of all files in this folder
                        if (in_array($filename_base, $all_files)) {
                            $ind = array_search($filename_base, $all_files, true);
                            unset($all_files[$ind]);
                        }
                    }
                    if ($recordId != null) {
                        //veify that this record exists
                        $res = mysql__select_array("Records", "rec_ID", "rec_ID=" . $recordId);
                        if (!(is_array($res) && count($res) > 0)) {
                            print "<div>File: <i>{$filename_base}</i> was indexed as rec# {$recordId}. " . "This record was not found. File will be reindexed</div>";
                            $recordId = null;
                        }
                    }
                    if ($recordId == null) {
                        //import only new
                        if ($filename) {
                            if (file_exists($filename)) {
                                $currfile = $filename;
                                //assign to global
                                //add-update the uploaded file
                                $file_id = register_file($filename, null, false);
                                if (is_numeric($file_id)) {
                                    $details["t:" . $fileDT] = array("1" => $file_id);
                                    //read EXIF data for JPEG images
                                    $recordNotes = readEXIF($filename);
                                } else {
                                    print "<div>File: <i>{$filename_base}</i> <span  style=\"color:red\">" . "Error: Failed to register. No record created</span></div>";
                                    $file_id = null;
                                }
                            } else {
                                print "<div>File: <i>{$filename_base}</i> <span  style=\"color:red\">" . "File is referenced in fieldhelper.xml but was not found on disk." . "No record was created.</span></div>";
                            }
                        }
                        if (!$file_id) {
                            continue;
                            //add with valid file only
                        }
                        if (is_numeric($lat) && is_numeric($lon) && ($lat != 0 || $lon != 0)) {
                            $details["t:" . $geoDT] = array("1" => "p POINT ({$lon} {$lat})");
                        }
                        //set title by default
                        if (!array_key_exists("t:" . $titleDT, $details)) {
                            $details["t:" . $titleDT] = array("1" => $filename);
                            print "<div>File: <i>{$filename_base}</i> <span  style=\"color:#ff8844\">" . "Warning: there was no title recorded in the XML manifest for this file." . "Using file path + file name as title.</span></div>";
                        }
                        $new_md5 = null;
                        $key = $fieldhelper_to_heurist_map['md5'];
                        if ($key > 0) {
                            $new_md5 = md5_file($filename);
                            $details["t:" . $key] = array("1" => $new_md5);
                        }
                        $out['error'] = 'test';
                        //add-update Heurist record
                        $out = saveRecord($recordId, $recordType, $recordURL, $recordNotes, null, null, null, null, null, null, null, $details, null, null, null, null, null);
                        if (@$out['error']) {
                            print "<div>File: <i>{$filename_base}</i> <span  style='color:red'>Error: " . implode("; ", $out["error"]) . "</span></div>";
                        } else {
                            if ($new_md5 == null) {
                                $new_md5 = md5_file($filename);
                            }
                            //update xml
                            if ($recordId == null) {
                                if ($old_md5 != $new_md5) {
                                    print "<div>File: <i>{$filename_base}</i> <span  style=\"color:#ff8844\">" . "Warning: Checksum differs from value in manifest; " . "data file may have been changed</span></div>";
                                }
                                $f_item->addChild("heurist_id", $out["bibID"]);
                                $f_item->addChild("md5", $new_md5);
                                $f_item->addChild("filesize", filesize($filename));
                                $rep_added++;
                            } else {
                                $el_heuristid["heurist_id"] = $out["bibID"];
                                $rep_updated++;
                            }
                            if (@$out['warning']) {
                                print "<div>File: <i>{$filename_base}</i> <span  style=\"color:#ff8844\">Warning: " . implode("; ", $out["warning"]) . "</span></div>";
                            }
                        }
                        $rep_processed++;
                    } else {
                        $rep_ignored++;
                    }
                    $cnt_files++;
                    if ($cnt_files % 5 == 0) {
                        ob_start();
                        print '<script type="text/javascript">update_counts(' . $progress_divid . ',' . $cnt_files . ',' . $rep_processed . ',' . $tot_files . ')</script>' . "\n";
                        ob_flush();
                        flush();
                    }
                }
                //for items
            }
            //if has items
        }
        //for all children in manifest
        if ($not_found) {
            print "<div style=\"color:red\">Manifest is either corrupted or empty</div>";
        } else {
            if ($rep_processed > 0) {
                print "<div>{$rep_processed} files processed. {$rep_added} added. {$rep_updated} updated.</div>";
            }
            if ($rep_ignored > 0) {
                print "<div>{$rep_ignored} files already indexed.</div>";
            }
        }
    } else {
        //create empty manifest XML  - TODO!!!!
        $s_manifest = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<fieldhelper version="1">
  <info>
    <AppName>Heurist</AppName>
    <AppVersion>v 3.0.0 2012-01-01</AppVersion>
    <AppCopyright>© ArtEresearch, University of Sydney</AppCopyright>
    <date></date>
  </info>
<formatOutput>1</formatOutput>
</fieldhelper>
XML;
        $fh_data = simplexml_load_string($s_manifest);
    }
    // add new empty items element
    if ($f_items == null) {
        $f_items = $fh_data->addChild("items");
    }
    $tot_files = count($all_files);
    $cnt_files = 0;
    $cnt_added = 0;
    //for files in folder that are not specified in the manifest file
    foreach ($all_files as $filename) {
        if (!($filename == "." || $filename == ".." || is_dir($dir . $filename) || $filename == "fieldhelper.xml")) {
            $filename_base = $filename;
            $filename = $dir . $filename;
            $currfile = $filename;
            $flleinfo = pathinfo($filename);
            $recordNotes = null;
            //checks for allowed extensions
            if (in_array(strtolower(@$flleinfo['extension']), $mediaExts)) {
                $details = array();
                $file_id = register_file($filename, null, false);
                if (is_numeric($file_id)) {
                    $details["t:" . $fileDT] = array("1" => $file_id);
                    //read EXIF data for JPEG images
                    $recordNotes = readEXIF($filename);
                } else {
                    print "<div>File: <i>{$filename_base}</i> <span  style=\"color:#ff8844\">" . "Warning: failed to register. No record created for:  .{$file_id}</span></div>";
                    //$rep_issues = $rep_issues."<br/>Can't register file:".$filename.". ".$file_id;
                    $file_id = null;
                    continue;
                }
                $details["t:" . $titleDT] = array("1" => $flleinfo['basename']);
                /* TODO - extract these data from exif
                   $details["t:".$descriptionDT] = array("1"=>$file_id);
                   $details["t:".$startdateDT] = array("1"=>$file_id);
                   $details["t:".$enddateDT] = array("1"=>$file_id);
                   $details["t:".$geoDT] = array("1"=>$file_id);
                   */
                $new_md5 = md5_file($filename);
                $key = $fieldhelper_to_heurist_map['md5'];
                if ($key > 0) {
                    $details["t:" . $key] = array("1" => $new_md5);
                }
                $key = $fieldhelper_to_heurist_map['file_name'];
                if ($key > 0) {
                    $details["t:" . $key] = array("1" => $flleinfo['basename']);
                }
                $key = $fieldhelper_to_heurist_map['file_path'];
                if ($key > 0) {
                    $targetPath = $flleinfo['dirname'];
                    $rel_path = getRelativePath(HEURIST_FILESTORE_DIR, $targetPath);
                    //getRelativePath2($targetPath);
                    $details["t:" . $key] = array("1" => $rel_path);
                    /*print "<div>".HEURIST_FILESTORE_DIR."</div>";
                      print "<div>file path :".$targetPath."</div>";
                      print "<div>relative path :".strpos($targetPath, HEURIST_FILESTORE_DIR)."--".$rel_path."</div>";
                      print "<div>relative path old :".getRelativePath(HEURIST_FILESTORE_DIR, $targetPath)."<br><br></div>";*/
                }
                $key = $fieldhelper_to_heurist_map['extension'];
                if ($key > 0) {
                    $details["t:" . $key] = array("1" => $flleinfo['extension']);
                }
                $key = $fieldhelper_to_heurist_map['filesize'];
                if ($key > 0) {
                    $details["t:" . $key] = array("1" => filesize($filename));
                }
                //add-update Heurist record
                $out['error'] = 'test2';
                $out = saveRecord(null, RT_MEDIA_RECORD, null, $recordNotes, null, null, null, null, null, null, null, $details, null, null, null, null, null);
                $f_item = $f_items->addChild("item");
                $f_item->addChild("filename", htmlspecialchars($flleinfo['basename']));
                $f_item->addChild("nativePath", htmlspecialchars($filename));
                $f_item->addChild("folder", htmlspecialchars($flleinfo['dirname']));
                $f_item->addChild("extension", $flleinfo['extension']);
                //$f_item->addChild("DateTime", );
                //$f_item->addChild("DateTimeOriginal", );
                $f_item->addChild("filedate", date("Y/m/d H:i:s.", filemtime($filename)));
                $f_item->addChild("typeContent", "image");
                $f_item->addChild("device", "image");
                $f_item->addChild("duration", "2000");
                $f_item->addChild("original_metadata", "chk");
                $f_item->addChild("Name0", htmlspecialchars($flleinfo['basename']));
                $f_item->addChild("md5", $new_md5);
                $f_item->addChild("filesize", filesize($filename));
                if (@$out['error']) {
                    print "<div>Fle: <i>{$filename_base}</i> <span style='color:red'>Error: " . implode("; ", $out["error"]) . "</span></div>";
                } else {
                    $f_item->addChild("heurist_id", $out["bibID"]);
                    $cnt_added++;
                }
                $rep_processed_dir++;
            }
            //check ext
            $cnt_files++;
            if ($cnt_files % 5 == 0) {
                ob_start();
                print '<script type="text/javascript">update_counts(' . $progress_divid . ',' . $cnt_files . ',' . $cnt_added . ',' . $tot_files . ')</script>' . "\n";
                ob_flush();
                flush();
            }
        }
    }
    //for files in folder that are not specified in the directory
    ob_start();
    if ($rep_processed_dir > 0) {
        print "<div style=\"color:green\">{$rep_processed_dir} processed. {$cnt_added} records created (new entries added to manifests)</div>";
    }
    print '<script type="text/javascript">update_counts(' . $progress_divid . ',' . $cnt_files . ',' . $cnt_added . ',0)</script>' . "\n";
    ob_flush();
    flush();
    if ($rep_processed + $rep_processed_dir > 0) {
        //save modified xml (with updated heurist_id tags)
        $fh_data->formatOutput = true;
        $fh_data->saveXML($manifest_file);
    }
    return $rep_processed + $rep_processed_dir;
}