Exemplo n.º 1
0
function register($login, $email, $password)
{
    global $db;
    if ($db['save_dest'] == 'database') {
        return register_db($login, $email, $password);
    } else {
        return register_file($login, $email, $password);
    }
}
/**
 *
 * @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_processed_dir = 0;
    $rep_ignored = 0;
    $f_items = null;
    //reference to items element
    $progress_divid++;
    print "<div><b>{$dir}</b><span id='progress{$progress_divid}'></span></div>";
    ob_flush();
    flush();
    if (!is_writable($dir)) {
        //$rep_issues = "Folder ".$dir." is not writable. Check permissions";
        print "<div style=\"color:red\">Folder is not writeable. Check permissions</div>";
        return 0;
    }
    $manifest_file = $dir . "fieldhelper.xml";
    $all_files = scandir($dir);
    /*****DEBUG****/
    //
    if (file_exists($manifest_file)) {
        //read fieldhelpe.xml
        if (is_readable($manifest_file)) {
            //check write permission
            if (!is_writable($manifest_file)) {
                print "<div style=\"color:red\">Manifest is not writable. Check permissions</div>";
                //$rep_issues = $rep_issues."<br/> Manifest is not writable in ".$dir;
                return 0;
            }
        } else {
            print "<div style=\"color:red\">Manifest is not readable. Check permissions</div>";
            //$rep_issues = $rep_issues."<br> manifest is not readable in ".$dir;
            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>";
            //$rep_issues = "Manifest file is corrupted";
            return 0;
        }
        //MAIN 	LOOP in manifest
        $not_found = true;
        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;
                    $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;
                        /*foreach ($el as $key=>$value2){
                        		$value = $value2;
                        		break;
                        		}*/
                        if ($key == "md5") {
                            $old_md5 = $value;
                        } else {
                            if (array_key_exists($key, $fieldhelper_to_heurist_map) && $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) {
                                        $details["t:" . $key3] = array("1" => $dir);
                                    }
                                } 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) {
                        //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);
                                } else {
                                    print "<div style=\"color:#ff8844\">warning {$filename_base} failed to register, no record created</div>";
                                    //$rep_issues = $rep_issues."<br/>Can't register file:".$filename.". ".$file_id;
                                    $file_id = null;
                                }
                            } else {
                                print "<div style=\"color:#ff8844\">warning {$filename_base} file not found, no record created</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 style=\"color:#ff8844\">warning {$filename_base} no title</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);
                        }
                        //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 style='color:red'>{$filename_base} Error: " . implode("; ", $out["error"]) . "</div>";
                        } else {
                            if ($new_md5 == null) {
                                $new_md5 = md5_file($filename);
                            }
                            //update xml
                            if ($recordId == null) {
                                if ($old_md5 != $new_md5) {
                                    print "<div style=\"color:#ff8844\">warning {$filename_base} checksum differs from value in manifest</div>";
                                }
                                $f_item->addChild("heurist_id", $out["bibID"]);
                                $f_item->addChild("md5", $new_md5);
                                $f_item->addChild("filesize", filesize($filename));
                            } else {
                                $el_heuristid["heurist_id"] = $out["bibID"];
                            }
                            if (@$out['warning']) {
                                print "<div style=\"color:#ff8844\">{$filename_base} Warning: " . implode("; ", $out["warning"]) . "</div>";
                            }
                            $rep_processed++;
                        }
                    } else {
                        $rep_ignored++;
                    }
                    $cnt_files++;
                    if ($cnt_files % 5 == 0) {
                        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>";
            //$rep_issues=$rep_issues."<br>folder $dir cotains corrupted or empty manifest file";
        } else {
            if ($rep_processed > 0) {
                print "<div>{$rep_processed} records created</div>";
            }
            if ($rep_ignored > 0) {
                print "<div>{$rep_ignored} files already indexed</div>";
                //$rep_issues=$rep_issues."<br> $rep_ignored entries in manifest are ignored for ".$dir;
            }
        }
    } 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 directory
    foreach ($all_files as $filename) {
        if (!($filename == "." || $filename == ".." || is_dir($dir . $filename) || $filename == "fieldhelper.xml")) {
            /*****DEBUG****/
            //error_log("2>>>>".is_dir($dir.$filename)."  ".$filename);
            $filename_base = $filename;
            $filename = $dir . $filename;
            $currfile = $filename;
            $flleinfo = pathinfo($filename);
            //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);
                } else {
                    print "<div style=\"color:#ff8844\">warning {$filename_base} failed to register, no record created:  .{$file_id}</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) {
                    $details["t:" . $key] = array("1" => $flleinfo['dirname']);
                }
                $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 = saveRecord(null, RT_MEDIA_RECORD, null, null, null, null, null, null, null, null, null, $details, null, null, null, null, null);
                /*****DEBUG****/
                //error_log(">>>>>".filemtime($filename)."  ".date("Y/m/d H:i:s.", filemtime($filename)));
                $f_item = $f_items->addChild("item");
                $f_item->addChild("filename", $flleinfo['basename']);
                $f_item->addChild("nativePath", $filename);
                $f_item->addChild("folder", $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", $flleinfo['basename']);
                $f_item->addChild("heurist_id", $out["bibID"]);
                $f_item->addChild("md5", $new_md5);
                $f_item->addChild("filesize", filesize($filename));
                $rep_processed_dir++;
                $cnt_added++;
            }
            //check ext
            $cnt_files++;
            if ($cnt_files % 5 == 0) {
                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
    if ($rep_processed_dir > 0) {
        print "<div style=\"color:green\">{$rep_processed_dir} records created (new entries added to manifest)</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;
}
Exemplo n.º 3
0
     }
 }
 //attr id, freetext, measure, certainity, vocabid
 echo "<div style='padding-left:30px'>" . $row[6] . "  " . $row[4] . "  " . $row[7] . "  " . $row[8] . "  " . $row[5] . "  " . "</div>";
 //get detailtype id in Heurist by attrib id
 $key = -1;
 if (@$detailMap[$row[6]]) {
     $key = intval($detailMap[$row[6]][0]);
     $detType = $detailMap[$row[6]][1];
 }
 if ($key > 0) {
     if ($detType == "file") {
         $filename = dirname($dbname_faims) . DIRECTORY_SEPARATOR . $row[4];
         if (file_exists($filename)) {
             //add-update the uploaded file
             $value = register_file($filename, null, false);
             if (!is_numeric($value)) {
                 print "<div style=\"color:red\">warning {$filename} failed to register, detail type ignored. {$value}</div>";
                 $value = null;
             }
         } else {
             print "<div style=\"color:red\">warning {$filename} file not found, detail type ignored</div>";
             $value = null;
         }
     } else {
         $vocabID = $row[5];
         if ($vocabID) {
             //vocabID
             if (@$termsMap[$vocabID]) {
                 $value = $termsMap[$vocabID];
             } else {
Exemplo n.º 4
0
/**
 * copy file from another H2 instance and register it
 *
 * @param mixed $src_fileid - file id in source db
 * @return int - file id in destionation db, null - if copy and registration are failed
 */
function copyRemoteFileH2($src_fileid)
{
    global $sourcedbname, $dbPrefix, $db_prefix;
    $sourcedb = $db_prefix . $sourcedbname;
    $HEURIST_UPLOAD_ROOT_OLD = HEURIST_DOCUMENT_ROOT . "/uploaded-heurist-files/";
    $_src_HEURIST_UPLOAD_DIR = $HEURIST_UPLOAD_ROOT_OLD . $sourcedbname . '/';
    $res = mysql_query("select * from `{$sourcedb}`.`files` where `file_id`=" . $src_fileid);
    if (mysql_num_rows($res) != 1) {
        print "<div  style='color:red;'>no entry for file id#" . $src_fileid . "</div>";
        return null;
        // nothing returned if parameter does not match one and only one row
    }
    $file = mysql_fetch_assoc($res);
    $filename = $_src_HEURIST_UPLOAD_DIR . "/" . $file['file_id'];
    if (!file_exists($filename)) {
        //check if this file is remote
        print "<div  style='color:red;'>File {$fielname} not found. Can't register it</div>";
        ob_flush();
        flush();
        // flush to screen
        return null;
    }
    $newfilename = HEURIST_UPLOAD_DIR . $file['file_orig_name'];
    //if file in source upload dirtectiry copy it to destionation upload directory
    if (!copy($filename, $newfilename)) {
        print "<div  style='color:red;'>Can't copy file {$fielname} to " . HEURIST_UPLOAD_DIR . "</div>";
        ob_flush();
        flush();
        // flush to screen
        return null;
    }
    $filename = $newfilename;
    //returns new file id in dest database
    $ret = register_file($filename, null, false);
    if (intval($ret) > 0) {
        return $ret;
    } else {
        print "<div  style='color:red;'>Can't register file " . $filename . "</div>";
        ob_flush();
        flush();
        // flush to screen
        return null;
    }
}
Exemplo n.º 5
0
    # Remove repeated files
    #
    $files = array_unique($files, SORT_STRING);
    # Reset file registry
    #
    reset_registry();
    # Iterate through each extension
    #
    foreach ($config['extensions'] as $ext) {
        $arrayIterator = new ArrayIterator($files);
        $regexIterator = new RegexIterator($arrayIterator, sprintf('/\\.%s$/i', $ext));
        foreach ($regexIterator as $file) {
            if (!file_exists($file)) {
                continue;
            }
            switch (register_file($file)) {
                case REGISTRY_NEW_FILE:
                    echo sprintf("[New] %s\n-- %s\n\n", date('H:i:s'), realpath($file));
                case REGISTRY_MODIFIED_FILE:
                    echo sprintf("[Modified] %s\n-- %s\n\n", date('H:i:s'), realpath($file));
            }
        }
    }
    $outputs = summarize_registry();
    # Output these files respectively
    #
    foreach (array_keys($outputs) as $ext) {
        file_put_contents($config['destination'] . '/all.' . $ext, $outputs[$ext]);
    }
    sleep(1);
}
/**
 *
 * @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;
}