1<?php
2namespace ZM;
3require_once('database.php');
4require_once('Object.php');
5
6
7class Server extends ZM_Object {
8  protected static $table = 'Servers';
9
10  protected $defaults = array(
11    'Id'                   => null,
12    'Name'                 => '',
13    'Protocol'             => '',
14    'Hostname'             => '',
15    'Port'                 => null,
16    'PathToIndex'          => null,
17    'PathToZMS'            => ZM_PATH_ZMS,
18    'PathToApi'            => '/zm/api',
19    'zmaudit'              => 1,
20    'zmstats'              => 1,
21    'zmtrigger'            => 0,
22    'zmeventnotification'  => 0,
23  );
24
25  public static function find( $parameters = array(), $options = array() ) {
26    return ZM_Object::_find(get_class(), $parameters, $options);
27  }
28
29  public static function find_one( $parameters = array(), $options = array() ) {
30    return ZM_Object::_find_one(get_class(), $parameters, $options);
31  }
32
33  public function Hostname( $new = null ) {
34    if ( $new != null )
35      $this->{'Hostname'} = $new;
36
37    if ( isset( $this->{'Hostname'}) and ( $this->{'Hostname'} != '' ) ) {
38      return $this->{'Hostname'};
39    } else if ( $this->Id() ) {
40      return $this->{'Name'};
41    }
42    # This theoretically will match ipv6 addresses as well
43    if ( preg_match( '/^(\[[[:xdigit:]:]+\]|[^:]+)(:[[:digit:]]+)?$/', $_SERVER['HTTP_HOST'], $matches ) ) {
44      return $matches[1];
45    }
46
47    $result = explode(':', $_SERVER['HTTP_HOST']);
48    return $result[0];
49  }
50
51  public function Protocol( $new = null ) {
52    if ( $new != null )
53      $this->{'Protocol'} = $new;
54
55    if ( isset($this->{'Protocol'}) and ( $this->{'Protocol'} != '' ) ) {
56      return $this->{'Protocol'};
57    }
58
59    return  (
60              ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' )
61              or
62              ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and ( $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) )
63            ) ? 'https' : 'http';
64  }
65
66  public function Port( $new = '' ) {
67    if ( $new != '' )
68      $this->{'Port'} = $new;
69
70    if ( isset($this->{'Port'}) and $this->{'Port'} ) {
71      return $this->{'Port'};
72    }
73
74    if ( isset($_SERVER['HTTP_X_FORWARDED_PORT']) ) {
75      return $_SERVER['HTTP_X_FORWARDED_PORT'];
76    }
77
78    return $_SERVER['SERVER_PORT'];
79  }
80
81  public function PathToZMS( $new = null ) {
82    if ( $new != null )
83      $this->{'PathToZMS'} = $new;
84    if ( $this->Id() and $this->{'PathToZMS'} ) {
85      return $this->{'PathToZMS'};
86    } else {
87      return ZM_PATH_ZMS;
88    }
89  }
90
91  public function UrlToZMS( $port = null ) {
92    return $this->Url($port).$this->PathToZMS();
93  }
94
95	public function Url( $port = null ) {
96    if ( ! ( $this->Id() or $port ) ) {
97      # Don't specify a hostname or port, the browser will figure it out
98      return '';
99    }
100
101    $url = $this->Protocol().'://';
102		$url .= $this->Hostname();
103    if ( !$port ) {
104      $port = $this->Port();
105    }
106    if ( $this->Protocol() == 'https' and $port == 443 ) {
107    } else if ( $this->Protocol() == 'http' and $port == 80 ) {
108    } else {
109      $url .= ':'.$port;
110    }
111    return $url;
112	}
113
114  public function PathToIndex( $new = null ) {
115    if ( $new != null )
116      $this->{'PathToIndex'} = $new;
117
118    if ( isset($this->{'PathToIndex'}) and $this->{'PathToIndex'} ) {
119      return $this->{'PathToIndex'};
120    }
121    // We can't trust PHP_SELF to not include an XSS vector. See note in skin.js.php.
122    return preg_replace('/\.php.*$/i', '.php', $_SERVER['PHP_SELF']);
123  }
124
125  public function UrlToIndex( $port=null ) {
126    return $this->Url($port).$this->PathToIndex();
127  }
128  public function UrlToApi( $port=null ) {
129    return $this->Url($port).$this->PathToApi();
130  }
131  public function PathToApi( $new = null ) {
132    if ( $new != null )
133      $this->{'PathToApi'} = $new;
134
135    if ( isset($this->{'PathToApi'}) and $this->{'PathToApi'} ) {
136      return $this->{'PathToApi'};
137    }
138    return '/zm/api';
139  }
140} # end class Server
141?>
142