aboutsummaryrefslogtreecommitdiffstats
path: root/admin/create_listing_file.php
blob: 507a4862f0cbc0226a0ac92b3e1189209023a623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/***************************************************************************
 *                          create_listing_file.php                        *
 *                            -------------------                          *
 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
 *                                                                         *
 *   $Id$
 *                                                                         *
 ***************************************************************************/

$prefix_thumbnail = 'TN-';
	
$conf['picture_ext'] = array ( 'jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG' );

$listing = '';

$end = strrpos( $_SERVER['PHP_SELF'], '/' ) + 1;
$local_folder = substr( $_SERVER['PHP_SELF'], 0, $end );
$url = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;

$listing.= '<url>'.$url.'</url>';
	
// get_dirs retourne un tableau contenant tous les sous-r�pertoires d'un
// r�pertoire
function get_dirs( $rep, $indent, $level )
{
  $sub_rep = array();
  $i = 0;
  $dirs = "";
  if ( $opendir = opendir ( $rep ) )
  {
    while ( $file = readdir ( $opendir ) )
    {
      if ( $file != "."
           and $file != ".."
           and is_dir ( $rep."/".$file )
           and $file != "thumbnail" )
      {
        $sub_rep[$i++] = $file;
        if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
        {
          echo '<span style="color:red;">"'.$file.'" : ';
          echo 'The name of the directory should be composed of ';
          echo 'letters, figures, "-", "_" or "." ONLY';
          echo '</span><br />';
        }
      }
    }
  }
  // write of the dirs
  for ( $i = 0; $i < sizeof( $sub_rep ); $i++ )
  {
    $dirs.= "\n".$indent.'<dir'.$level.' name="'.$sub_rep[$i].'">';
    $dirs.= get_pictures( $rep.'/'.$sub_rep[$i], $indent.'  ' );
    $dirs.= get_dirs( $rep.'/'.$sub_rep[$i], $indent.'  ', $level + 1 );
    $dirs.= "\n".$indent.'</dir'.$level.'>';
  }
  return $dirs;		
}

// get_extension returns the part of the string after the last "."
function get_extension( $filename )
{
  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
}

// get_filename_wo_extension returns the part of the string before the last
// ".".
// get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar'
function get_filename_wo_extension( $filename )
{
  return substr( $filename, 0, strrpos( $filename, '.' ) );
}

function is_image( $filename )
{
  global $conf;

  if ( !is_dir( $filename )
       and in_array( get_extension( $filename ), $conf['picture_ext'] ) )
  {
    return true;
  }
  return false;
}

function TN_exists( $dir, $file )
{
  global $conf, $prefix_thumbnail;

  $titre = get_filename_wo_extension( $file );

  for ( $i = 0; $i < sizeof ( $conf['picture_ext'] ); $i++ )
  {
    $base_tn_name = $dir.'/thumbnail/'.$prefix_thumbnail.$titre.'.';
    $ext = $conf['picture_ext'][$i];
    if ( is_file( $base_tn_name.$ext ) )
    {
      return $ext;
    }
  }
  echo 'The thumbnail is missing for '.$dir.'/'.$file;
  echo '-> '.$dir.'/thumbnail/'.$prefix_thumbnail.$titre.'.xxx';
  echo ' ("xxx" can be : ';
  for ( $i = 0; $i < sizeof ( $conf['picture_ext'] ); $i++ )
  {
    if ( $i > 0 )
    {
      echo ', ';
    }
    echo '"'.$conf['picture_ext'][$i].'"';
  }
  echo ')<br />';
  return false;
}

function get_pictures( $rep, $indent )
{
  $pictures = array();		

  $tn_ext = '';
  $root = '';
  if ( $opendir = opendir ( $rep ) )
  {
    while ( $file = readdir ( $opendir ) )
    {
      if ( is_image( $file ) and $tn_ext = TN_exists( $rep, $file ) )
      {
        $picture = array();

        $picture['file']     = $file;
        $picture['tn_ext']   = $tn_ext;
        $picture['date']     = date('Y-m-d',filemtime( $rep.'/'.$file ) );
        $picture['filesize'] = floor( filesize( $rep."/".$file ) / 1024 );
        $image_size = @getimagesize( $rep."/".$file );
        $picture['width']    = $image_size[0];
        $picture['height']   = $image_size[1];

        array_push( $pictures, $picture );

        if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
        {
          echo '<span style="color:red;">"'.$file.'" : ';
          echo 'The name of the picture should be composed of ';
          echo 'letters, figures, "-", "_" or "." ONLY';
          echo '</span><br />';
        }
      }
    }
  }
  // write of the node <root> with all the pictures at the root of the
  // directory
  $root.= "\n".$indent."<root>";
  if ( sizeof( $pictures ) > 0 )
  {
    for( $i = 0; $i < sizeof( $pictures ); $i++ )
    {
      $root.= "\n".$indent.'  ';
      $root.= '<picture';
      $root.= ' file="'.     $pictures[$i]['file'].     '"';
      $root.= ' tn_ext="'.   $pictures[$i]['tn_ext'].   '"';
      $root.= ' date="'.     $pictures[$i]['date'].     '"';
      $root.= ' filesize="'. $pictures[$i]['filesize']. '"';
      $root.= ' width="'.    $pictures[$i]['width'].    '"';
      $root.= ' height="'.   $pictures[$i]['height'].   '"';
      $root.= ' />';
    }
  }
  $root.= "\n".$indent.'</root>';
  return $root;
}

$listing.= get_dirs( '.', '', 0 );

if ( $fp = @fopen("./listing.xml","w") )
{
  fwrite( $fp, $listing );
  fclose( $fp );
}
else
{
  echo "I can't write the file listing.xml";
}

echo "listing.xml created";
?>