<?php
error_reporting(0);
	include_once 'fotoweb57.php';
        include_once '../marker/Toolkit_Version.php';          

        // Check for operation modes 2 or 3
        // i.e. $filename is defined, and $new_ps_file_info_array is not
        if ( ( ! isset( $new_ps_file_info_array ) ) &&
             ( isset( $filename ) ) &&
             ( is_string( $filename ) ) )
        {
                // Hide any unknown EXIF tags
                $GLOBALS['HIDE_UNKNOWN_TAGS'] = TRUE;

                // Accessing the existing file info for the specified file requires these includes
                include_once '../marker/JPEG.php';
                include_once '../marker/XMP.php';
                include_once '../marker/Photoshop_IRB.php';
                include_once '../marker/EXIF.php';
                include_once '../marker/Photoshop_File_Info.php';

                // Retrieve the header information from the JPEG file
                $jpeg_header_data = get_jpeg_header_data( $filename );

                // Retrieve EXIF information from the JPEG file
                $Exif_array = get_EXIF_JPEG( $filename );

                // Retrieve XMP information from the JPEG file
                $XMP_array = read_XMP_array_from_text( get_XMP_text( $jpeg_header_data ) );

                // Retrieve Photoshop IRB information from the JPEG file
                $IRB_array = get_Photoshop_IRB( $jpeg_header_data );

                // Retrieve Photoshop File Info from the three previous arrays
                $new_ps_file_info_array = get_photoshop_file_info( $Exif_array, $XMP_array, $IRB_array );


                // Check if there is an array of defaults available
                if ( ( isset( $default_ps_file_info_array) ) &&
                     ( is_array( $default_ps_file_info_array) ) )
                {
                        // There are defaults defined

                        // Check if there is a default for the date defined
                        if ( ( ! array_key_exists( 'date', $default_ps_file_info_array ) ) ||
                             ( ( array_key_exists( 'date', $default_ps_file_info_array ) ) &&
                               ( $default_ps_file_info_array['date'] == '' ) ) )
                        {
                                // No default for the date defined
                                // figure out a default from the file

                                // Check if there is a EXIF Tag 36867 "Date and Time of Original"
                                if ( ( $Exif_array != FALSE ) &&
                                     ( array_key_exists( 0, $Exif_array ) ) &&
                                     ( array_key_exists( 34665, $Exif_array[0] ) ) &&
                                     ( array_key_exists( 0, $Exif_array[0][34665] ) ) &&
                                     ( array_key_exists( 36867, $Exif_array[0][34665][0] ) ) )
                                {
                                        // Tag "Date and Time of Original" found - use it for the default date
                                        $default_ps_file_info_array['date'] = $Exif_array[0][34665][0][36867]['Data'][0];
                                        $default_ps_file_info_array['date'] = preg_replace( "/(\d\d\d\d):(\d\d):(\d\d)( \d\d:\d\d:\d\d)/", "$1-$2-$3", $default_ps_file_info_array['date'] );
                                }
                               // Check if there is a EXIF Tag 36868 "Date and Time when Digitized"
                                else if ( ( $Exif_array != FALSE ) &&
                                     ( array_key_exists( 0, $Exif_array ) ) &&
                                     ( array_key_exists( 34665, $Exif_array[0] ) ) &&
                                     ( array_key_exists( 0, $Exif_array[0][34665] ) ) &&
                                     ( array_key_exists( 36868, $Exif_array[0][34665][0] ) ) )
                                {
                                        // Tag "Date and Time when Digitized" found - use it for the default date
                                        $default_ps_file_info_array['date'] = $Exif_array[0][34665][0][36868]['Data'][0];
                                        $default_ps_file_info_array['date'] = preg_replace( "/(\d\d\d\d):(\d\d):(\d\d)( \d\d:\d\d:\d\d)/", "$1-$2-$3", $default_ps_file_info_array['date'] );
                                }
                                // Check if there is a EXIF Tag 306 "Date and Time"
                                else if ( ( $Exif_array != FALSE ) &&
                                     ( array_key_exists( 0, $Exif_array ) ) &&
                                     ( array_key_exists( 306, $Exif_array[0] ) ) )
                                {
                                        // Tag "Date and Time" found - use it for the default date
                                        $default_ps_file_info_array['date'] = $Exif_array[0][306]['Data'][0];
                                        $default_ps_file_info_array['date'] = preg_replace( "/(\d\d\d\d):(\d\d):(\d\d)( \d\d:\d\d:\d\d)/", "$1-$2-$3", $default_ps_file_info_array['date'] );
                                }
                                else
                                {
                                        // Couldn't find an EXIF date in the image
                                        // Set default date as creation date of file
                                       

                                        $default_ps_file_info_array['date'] = date ("Y-m-d");
                                }
                        }

                        // Cycle through all the elements of the default values array
                        foreach( $default_ps_file_info_array as $def_key =>$default_item )
                        {
                                // Check if the current element is Keywords or
                                // Supplemental Categories as these are arrays
                                // and need to be treated differently
                                if ( ( strcasecmp( $def_key, "keywords" ) == 0 ) ||
                                     ( strcasecmp( $def_key, "supplementalcategories" ) == 0 ) )
                                {
                                        // Keywords or Supplemental Categories found
                                        // Check if the File Info from the file is empty for this element
                                        // and if there are default values in this array element
                                        if ( ( count( $new_ps_file_info_array[ $def_key ] ) == 0 ) &&
                                             ( is_array( $default_item ) ) &&
                                             ( count( $default_item ) >= 0 ) )
                                        {
                                                // The existing file info is empty, and there are
                                                // defaults - add them
                                                $new_ps_file_info_array[ $def_key ] = $default_item;
                                        }
                                }
                                // Otherwise, this is not an array element, just check if it is blank in the existing file info
                                else if ( trim( $new_ps_file_info_array[ $def_key ] ) == "" )
                                {
                                        // The existing file info is blank, add the default value
                                        $new_ps_file_info_array[ $def_key ] = $default_item;
                                }

                        }
                }
        }
        // Check for operation mode 4 - $new_ps_file_info_array and $filename are not defined,
        else if ( ( ( !isset($new_ps_file_info_array) ) || ( ! is_array($new_ps_file_info_array) ) ) &&
                  ( ( !isset($filename) ) || ( ! is_string( $filename ) ) ) )
        {
                // No filename or new_ps_file_info_array defined, create a blank file info array to display
                $new_ps_file_info_array = array(
                      "title" => "",
                      "author" => "",
                      "authorsposition" => "",
                      "caption" => "",
                      "captionwriter" => "",
                      "jobname" => "",
                      "copyrightstatus" => "",
                      "copyrightnotice" => "http://". $_SERVER['REMOTE_ADDR']."\"",
                      "ownerurl" => "",
                      "keywords" => array(),
                      "category" => "",
                      "supplementalcategories" => array(),
                      "date" => "",
                      "city" => "",
                      "state" => "",
                      "country" => "",
                      "credit" => "",
                      "source" => "",
                      "headline" => "",
                      "instructions" => "",
                      "transmissionreference" => "",
                      "urgency" => "" );
        }


/***************************************************************************
*
* Now output the actual HTML form
*
***************************************************************************/
$outputfilename=$filename;

//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array['title'])==0 )
   $new_ps_file_info_array['title']=getTitreIptc($filename);

//initialisation IPTC au cas ou
if ( $new_ps_file_info_array[ 'caption' ]==0 )
   $new_ps_file_info_array[ 'caption' ]=getLegendeIptc($filename);
   
//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array[ 'keywords' ])==0 )
   $new_ps_file_info_array['keywords']=getKeyWordIptc($filename);
   
//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array[ 'city' ])==0 )
   $new_ps_file_info_array[ 'city' ]=getVilleIptc($filename); 
   
//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array[ 'country' ])==0 )
   $new_ps_file_info_array[ 'country' ]=getPaysIptc($filename);         

//initialisation IPTC au cas ou
//if ( strlen($new_ps_file_info_array[ 'state' ])==0 )
//   $new_ps_file_info_array[ 'state' ]=getEtatIptc($filename);         

//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array[ 'copyrightnotice' ])==0 )
   $new_ps_file_info_array[ 'copyrightnotice' ]=getTitreIptc($filename);         

//initialisation IPTC au cas ou
if ( strlen($new_ps_file_info_array[ 'author' ])==0 )
   $new_ps_file_info_array[ 'author' ]=getPhotogIptc($filename);         


?>

    <form id="formAdmin" action="../marker/Write_File_Info.php" method="post">


        <?php echo "<input name=\"filename\" type=\"hidden\" value=\"$outputfilename\">"; ?>

                <table>

                        <tr>
                                <td>&nbsp;&nbsp;</td>
                                
                                <td class='libellepetit' alt='url à copier coller pour accéder directement à la photo' title='url à copier coller pour accéder directement à la photo'>
                                        <?php
                                        echo "http://". $_SERVER['HTTP_HOST']."/application/visuPhoto_NoFrame.php?ntof=".$id;
                                        ?>
                                </td>
                        </tr>
                        <tr>
                                <td class='titrepage'>
                                        Titre : 
                                </td>
                                <td >
                                        <?php
                                        echo "<input size=49 name=\"title\" type=\"text\" value=\"". $new_ps_file_info_array[ 'title' ] ."\">";
                                        ?>
                                </td>
                        </tr>
                       
                        <tr>
                                <td class='titrepage'>
                                        Description : 
                                </td>
                                <td>
                                        <textarea name="caption" rows=7 cols=37 wrap="off"><?php echo $new_ps_file_info_array[ 'caption' ]; ?></textarea>
                                </td>
                        </tr>

                        

                        <tr>
                                <td class='titrepage'>
                                        Mots clés :
                                </td>
                                <td>
                                        <textarea name="keywords" rows=3 cols=37 wrap="off"><?php
                                                                                                foreach( $new_ps_file_info_array[ 'keywords' ] as $keyword )
                                                                                                {
                                                                                                        echo "$keyword&#xA;";
                                                                                                }
                                                                                            ?></textarea>
                                </td>
                        </tr>

                        

                        <tr>
                                <td class='titrepage'>
                                        Ville :
                                </td>
                                <td>
                                        <?php
                                                echo "<input size=49 name=\"city\" type=\"text\" value=\"". $new_ps_file_info_array[ 'city' ] ."\">";
                                        ?>
                                </td>
                        </tr>


                        <tr>
                                <td class='titrepage'>
                                        Etat :
                                </td>
                                <td>
                                        <?php
                                                echo "<input size=49 name=\"state\" type=\"text\" value=\"". $new_ps_file_info_array[ 'state' ] ."\">";
                                        ?>
                                </td>
                        </tr>


                        <tr>
                                <td class='titrepage'>
                                        Pays :
                                </td>
                                <td>
                                        <?php
                                                echo "<input size=49 name=\"country\" type=\"text\" value=\"". $new_ps_file_info_array[ 'country' ] ."\">";
                                        ?>
                                </td>
                     
			<tr>
			
                        <tr>
                                <td class='titrepage'>
                                        Photographe :
                                </td>
                                <td>
                                        <?php
                                        echo "<input size=49 name=\"author\" type=\"text\" value=\"". $new_ps_file_info_array[ 'author' ] ."\">";
                                        ?>
                                </td>
                        </tr>
                        <tr>
                                <td class='titrepage'>
                                        Copyright :
                                </td>
                                <td>
                                        <?php
                                                echo "<input size=49 name=\"copyrightnotice\" type=\"text\" value=\"". $new_ps_file_info_array[ 'copyrightnotice' ] ."\">";
                                        ?>                                
                                </td>
                        </tr>
			
			<td >&nbsp;&nbsp;</td>
			<td><?php
      echo "<p><a href=\"../marker/Example.php?jpeg_fname=".$filename."\" >Voir toutes les métadonnées Exif</a></p>";
 				?>
 			 <td></tr>
 			<tr><td >&nbsp;&nbsp;</td></tr> 
			<tr>
 				<td align='center' valign='bottom' width='100%' class='libelle' colspan='2'>
 			  <INPUT TYPE=SUBMIT VALUE="Enregistrer IPTC" align='center'>
      </td> 
			</tr>
      </table>
              
              
        </form>
      
       