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

$base = "models";

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
5
$THUMBNAME = "thumb.jpeg";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
6
$RATINGFILE = "rating.txt";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
7
$READMENAME = "README.txt";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
8

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
9 10 11
// for htaccess
$SECRET_PATTERN = "/# public access/";

12
$showall = false;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
13 14 15 16 17 18 19
$rating = false;

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

$rating = get_allowed_rating($rating);
20 21 22 23 24

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
25
$models = selective_scandir($base,false,$rating);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
26 27 28 29 30
$res = "";

foreach($models as $model){

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
33
    $model_rating = get_model_rating("$model_path/$RATINGFILE");
34

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
35
    if ($model_rating>=$rating){
36

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
37
      $versions = selective_scandir($model_path,$showall,0);
38

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
39 40
      // create thumb
      create_thumbnail($model_path,$versions,$thumb);
41

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
42 43 44
      if (!is_file($thumb)){
          $thumb="";
      }
45

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
46
      $res .= "<model name='$model' thumb='$thumb'>\n";
47

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
48 49
      // read kml
      $res .= "\t<map>\n".parse_kml("$base/$model/$model.kml")."\t</map>\n";
50

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
51
      foreach($versions as $version){
52

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
53
          $res .= "\t<version name='$version'>\n";
54

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
55 56 57 58 59
          $comments = "-";
          $readme = "$model_path/$version/$READMENAME";
          if (is_file($readme)){
              $comments = trim(file_get_contents($readme),"\t\n\r");
          }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
60

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
61
          $res .= "\t\t<comments>$comments</comments>\n";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
62

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
63 64 65 66 67 68 69
          $res .= "\t</version>\n";

      }

      $res .= "</model>\n";

    }
70

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
71 72
}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
73 74
return_xml($res);

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
75 76
//functions

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
77
function selective_scandir($path,$showall,$rating=5){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
78 79 80 81 82 83

    $results = Array();

    $contents = scandir($path);

    foreach($contents as $item){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
84
        if ($item!='.'&&$item!='..'&&$item!="jp4"&&is_dir("$path/$item")){
85 86 87 88 89 90 91 92
            if ($showall){
              array_push($results,$item);
            }else{
              if (($item[0]!=".")&&($item[0]!="_")){
                array_push($results,$item);
              }
            }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
93 94
        }
    }
95

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
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;

126
  if (is_file(".htaccess")) {
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
127 128 129 130 131 132 133

    $htaccess = file_get_contents(".htaccess");

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

    // restrict to 1
    if ($m) {
134
      $r = max(1,$r);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
135 136 137 138 139 140 141 142
    }

  }

  return $r;

}

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
143 144 145
function create_thumbnail($path,$vpaths,$thumbname){

    if (!is_file($thumbname)){
146

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

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
151 152
            $files = scandir($srcpath);
            foreach($files as $file){
153

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
156 157
                if ($test){
                    $file = "$srcpath/$file";
158

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
159
                    if (extension_loaded('imagick')){
160

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
161
                        $imagick = new Imagick($file);
162

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
163
                        $imagick->trimImage(0);
164

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
165 166
                        $w = $imagick->getImageWidth();
                        $h = $imagick->getImageHeight();
167

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

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

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
174
                        $imagick->writeImage($thumbname);
175

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
176 177 178
                    }
                    break;
                }
179

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
180 181 182 183 184 185 186 187 188
                /*
                $pinfo = pathinfo("$srcpath/$file");
                if ($pinfo['extension']=="jpeg"){
                    $file = "$srcpath/$file";
                    echo "go-go-go with $file";
                    break;
                }
                */
            }
189

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
190
        }
191

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
192
    }
193

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
194 195 196 197 198 199 200 201 202
    return 0;

}

function parse_kml($file){

    $res = "";

    if (is_file($file)){
203

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
204
        $xml = simplexml_load_file($file);
205

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
208 209 210
        foreach($recs as $rec){
            $res .= "\t".$rec->Camera->asXML()."\n";
        }
211

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }else{
        $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;
226

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
227 228
}

229
?>