summaryrefslogtreecommitdiffstats
path: root/mirrorstatus.php
blob: 12f20306df61fc96e08d229cd5d9102b2ec07ed1 (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
<?php
 if(!isset($_GET['mr']))
  {
    include("tde-head-and-foot.php");
    doHeader("Active TDE Mirrors", "Developlment", "TDE Team");
?>

<p>Please note that this list may change from time to time as mirrors are added and removed.</p>

<table cellpadding="4">
<?php
  }
 else
  {
    header('Content-Type: text/plain');
  }

 $mirrorsJson = file_get_contents("mirrors.json");
 $mirrors = json_decode($mirrorsJson, true);

 $statusType = array(
    "active" => "green",
    "inaccessible" => "red",
    "outdated" => "orange",
    "unknown" => "gray",
 );

 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379, 2);
 $ch = array();
 $cmh = curl_multi_init();
 foreach( $mirrors as $mirrorName => $mirrorInfo )
  {
    $savedResult = $redis->isConnected() ? $redis->get('mirrorStatus-'.$mirrorName) : false;
    if($savedResult)
     {
       $mirrors[$mirrorName]['synctime'] = $savedResult;
     }
    else
     {
       $ch[$mirrorName] = curl_init();
       curl_setopt( $ch[$mirrorName], CURLOPT_URL, $mirrorInfo['url'].'-synctime' );
       curl_setopt( $ch[$mirrorName], CURLOPT_TIMEOUT, 10 );
       curl_setopt( $ch[$mirrorName], CURLOPT_HEADER, true );
       curl_setopt( $ch[$mirrorName], CURLOPT_FOLLOWLOCATION, true );
       curl_setopt( $ch[$mirrorName], CURLOPT_RETURNTRANSFER, true );
       curl_multi_add_handle( $cmh, $ch[$mirrorName] );
     }
  }

 if(!empty($ch))
  {
    $running = null;
    do
     {
       curl_multi_exec($cmh, $running);
     }
    while($running);

    foreach( $ch as $mirrorName => $cs )
     {
       $ret = curl_multi_getcontent( $cs );
       curl_multi_remove_handle( $cmh, $cs );
       if(!empty( $ret ))
        {
          if(!preg_match('#(^|\n)HTTP/1.1 200 OK#', $ret))
           {
             $mirrors[$mirrorName]['synctime'] = '0';
           }
          else
           {
             $mirrors[$mirrorName]['synctime'] = strtotime(preg_replace( "#.*\r\n#", '', $ret));
           }
        }
       else
        {
          $mirrors[$mirrorName]['synctime'] = '-1';
        }
       if($redis->isConnected())
        {
          $redis->setEx( 'mirrorStatus-'.$mirrorName,
                         $mirrors[$mirrorName]['synctime'] > 0 ? 1800 : 300,
                         $mirrors[$mirrorName]['synctime'] );
        }
     }
  }
 curl_multi_close($cmh);
 $redis->close();

 foreach( $mirrors as $mirrorName => $mirrorInfo )
  {
    if( $mirrorInfo['synctime'] < 0 )
     {
       $mirrorStatus = 'inaccessible';
     }
    if( $mirrorInfo['synctime'] == 0 ||
        $mirrors['master']['synctime'] == 0 )
     {
       $mirrorStatus = 'unknown';
     }
    if( $mirrorInfo['synctime'] > 0 )
     {
       $mirrorStatus = ( abs($mirrorInfo['synctime'] -
                             $mirrors['master']['synctime'])
                         > (60*60*30) /* 30 hours */
                         ? 'outdated' : 'active' );
     }
    if(!isset($_GET['mr']))
     {
       if(isset($statusType[$mirrorStatus]))
        {
          $mirrorStatus = ("<font color=\"".$statusType[$mirrorStatus]."\">".
                           $mirrorStatus."</font>");
        }
       echo( "<tr valign=\"top\">\n".
             "<td><b>".$mirrorInfo['descr'].": </b>\n".
             "<br/><a href=\"".$mirrorInfo['url']."\">".$mirrorInfo['url']."</a></td>\n".
             "<td><b>".$mirrorStatus."</b></td>\n".
             "</tr>\n");
      }
     else
      {
        if($mirrorStatus == 'active')
         {
           echo( $mirrorInfo['url']."\n" );
         }
      }
  }

 if(!isset($_GET['mr']))
  {
?>
</table>

<?php
    doFooter();
  }
?>