list.php 6.1 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2
<?php

3 4 5 6 7
if (isset($_GET['basepath'])){
  $base = $_GET['basepath'];
}else{
  $base = "models/all";
}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
8

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
9
$THUMBNAME = "thumb.jpeg";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
10
$RATINGFILE = "rating.txt";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
11
$READMENAME = "README.txt";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
12

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
13 14 15
// for htaccess
$SECRET_PATTERN = "/# public access/";

16
$showall = false;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
17 18 19 20 21 22 23
$rating = false;

if (isset($_GET['rating'])){
  $rating = intval($_GET['rating']);
}

$rating = get_allowed_rating($rating);
24 25 26 27 28

if (isset($_GET['showall'])){
  $showall = true;
}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
29
$models = selective_scandir($base,false,$rating);
30

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
31 32 33 34 35
$res = "";

foreach($models as $model){

    $model_path = "$base/$model";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
36
    $thumb = "$model_path/$THUMBNAME";
37

38 39 40
    $f1 = is_file("$model_path/$THUMBNAME");
    $f2 = is_file("$model_path/$RATINGFILE");
    $f3 = is_file("$model_path/$model.kml");
41

42
    if (!($f1||$f2||$f3)){
43

44 45 46 47
      $group_path = selective_scandir($model_path,false,$rating);
      foreach($group_path as $group_item){
        $model_path = "$base/$model/$group_item";
        $thumb = "$model_path/$THUMBNAME";
48

49 50
        $model_rating = get_model_rating("$model_path/$RATINGFILE");
        if ($model_rating>=$rating){
51

52 53 54 55 56
          $versions = selective_scandir($model_path,$showall,0);
          // create thumb
          create_thumbnail($model_path,$versions,$thumb);

          if (!is_file($thumb)){$thumb="";}
57

58
          $res .= "<model name='$group_item' group='$model' thumb='$thumb'>\n";
59

60
          // read kml
61 62
          $res .= "\t<map>\n".parse_kml($model_path,$group_item)."\t</map>\n";

63 64 65 66 67 68 69 70 71 72 73 74 75
          foreach($versions as $version){

              $res .= "\t<version name='$version'>\n";
              $comments = "-";
              $readme = "$model_path/$version/$READMENAME";
              if (is_file($readme)){
                  $comments = trim(file_get_contents($readme),"\t\n\r");
              }
              $res .= "\t\t<comments>$comments</comments>\n";
              $res .= "\t</version>\n";
          }
          $res .= "</model>\n";
        }
76

77
      }
78

79
    }else{
80

81 82
      $model_rating = get_model_rating("$model_path/$RATINGFILE");
      if ($model_rating>=$rating){
83

84 85 86
        $versions = selective_scandir($model_path,$showall,0);
        // create thumb
        create_thumbnail($model_path,$versions,$thumb);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
87

88
        if (!is_file($thumb)){$thumb="";}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
89

90 91
        $res .= "<model name='$model' group='' thumb='$thumb'>\n";
        // read kml
92
        $res .= "\t<map>\n".parse_kml($model_path,$model)."\t</map>\n";
93
        foreach($versions as $version){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
94

95 96 97 98 99 100 101 102 103 104
            $res .= "\t<version name='$version'>\n";
            $comments = "-";
            $readme = "$model_path/$version/$READMENAME";
            if (is_file($readme)){
                $comments = trim(file_get_contents($readme),"\t\n\r");
            }
            $res .= "\t\t<comments>$comments</comments>\n";
            $res .= "\t</version>\n";
        }
        $res .= "</model>\n";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
105 106 107
      }

    }
108

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
109 110
}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
111 112
return_xml($res);

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
113 114
//functions

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115
function selective_scandir($path,$showall,$rating=5){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
116 117 118 119

    $results = Array();

    $contents = scandir($path);
120
    $contents = array_diff($contents, [".", ".."]);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
121 122

    foreach($contents as $item){
123
        if ($item!="jp4"&&is_dir("$path/$item")){
124 125 126
            if ($showall){
              array_push($results,$item);
            }else{
127
              // hidden directories
128 129 130 131 132
              if (($item[0]!=".")&&($item[0]!="_")){
                array_push($results,$item);
              }
            }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
133 134
        }
    }
135

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    return $results;

}

function return_xml($str){

    $str = "<?xml version='1.0'  standalone='yes'?>\n<Document>\n$str</Document>";

    header("Content-Type: text/xml");
    header("Content-Length: ".strlen($str)."\n");
    header("Pragma: no-cache\n");
    printf($str);

}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
function get_model_rating($file){

  if (is_file($file)){
    $r = intval(trim(file_get_contents($file)));
  }else{
    $r = 0;
  }
  return $r;

}

function get_allowed_rating($r){

  global $SECRET_PATTERN;

166
  if (is_file(".htaccess")) {
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
167 168 169 170 171 172 173

    $htaccess = file_get_contents(".htaccess");

    $m = preg_match($SECRET_PATTERN,$htaccess);

    // restrict to 1
    if ($m) {
174
      $r = max(1,$r);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
175 176 177 178 179 180 181 182
    }

  }

  return $r;

}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
183 184 185
function create_thumbnail($path,$vpaths,$thumbname){

    if (!is_file($thumbname)){
186

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
187
        if (count($vpaths)>=1){
188

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
189
            $srcpath = "$path/{$vpaths[0]}";
190

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
191 192
            $files = scandir($srcpath);
            foreach($files as $file){
193

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
194
                $test = preg_match('/(texture-bgnd-ext)/',$file);
195

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
196 197
                if ($test){
                    $file = "$srcpath/$file";
198

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
199
                    if (extension_loaded('imagick')){
200

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
201
                        $imagick = new Imagick($file);
202

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
203
                        $imagick->trimImage(0);
204

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
205 206
                        $w = $imagick->getImageWidth();
                        $h = $imagick->getImageHeight();
207

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
208
                        $imagick->borderImage('black', 100, 100);
209

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
210
                        //$imagick->cropImage($w/2, $h/4, $w/4, $h/4);
211

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
212
                        $imagick->thumbnailImage(200, 100, true, true);
213

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
214
                        $imagick->writeImage($thumbname);
215

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
216 217 218
                    }
                    break;
                }
219

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
220 221 222 223 224 225 226 227 228
                /*
                $pinfo = pathinfo("$srcpath/$file");
                if ($pinfo['extension']=="jpeg"){
                    $file = "$srcpath/$file";
                    echo "go-go-go with $file";
                    break;
                }
                */
            }
229

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
230
        }
231

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
232
    }
233

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
234 235 236 237
    return 0;

}

238 239 240 241 242 243 244 245 246 247 248
// there is master_kml.xml
function parse_kml($path,$file){

    $master_kml = "$path/master_kml.xml";

    if (is_file($master_kml)){
      $xml = simplexml_load_file($master_kml);
      $file = "../".($xml->name)."/".($xml->name);
    }

    $pf = "$path/$file.kml";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
249 250 251

    $res = "";

252
    if (is_file($pf)){
253

254
        $xml = simplexml_load_file($pf);
255

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
256
        $recs = $xml->Document->children();
257

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
258 259 260
        foreach($recs as $rec){
            $res .= "\t".$rec->Camera->asXML()."\n";
        }
261

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
262
    }else{
263
        echo "File not found, you suck!\n";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
264 265 266 267 268 269 270 271 272 273 274 275 276
        $res = <<<TEXT
<Camera>
    <longitude>-111.9328843</longitude>
    <latitude>40.7233861</latitude>
    <altitude>1305.1</altitude>
    <heading>0</heading>
    <tilt>90</tilt>
    <roll>00</roll>
</Camera>
TEXT;
    }

    return $res;
277

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
278 279
}

280
?>