peer.php 9.92 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
 *
 * @package    mahara
20
21
 * @subpackage core
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
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
 *
 */


class Peer {

    const   UNINITIALIZED           = 0;
    const   INITIALIZED             = 1;
    const   PERSISTENT              = 2;

    private $initialized            = self::UNINITIALIZED;
    private $changed                = false;
    private $application;
    private $oldwwwroot;
    private $members = array(/* host table */
                             'wwwroot' => '',
                             'name' => '',
                             'institution' => '',
                             'ipaddress' => '',
                             'portno' => 80,
                             'publickey' => '',
                             'publickeyexpires' => 0,
                             'deleted' => 0,
                             'lastconnecttime' => 0,
                             'appname' => ''
                             );

    public function __construct($result = null) {

53
        if (null == $result) {
54
            require_once(get_config('libroot') . 'application.php');
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            $this->application = new Application();
            return;
        }
        $this->populate($result);
        $this->initialized = self::PERSISTENT;
    }

    protected function populate($result) {
        $values = get_object_vars($result);
        foreach ($values as $key => $value) {
            $this->__set($key, $value);
        }
        $this->oldwwwroot = $result->wwwroot;
    }

    public function __set($name, $value) {
        if (!array_key_exists($name, $this->members)) {
            throw new ParamOutOfRangeException(addslashes($name) .' is not a member of Peer.');
        }
Donal McMullan's avatar
WIP    
Donal McMullan committed
74

75
        if (is_scalar($value) != is_scalar($this->members[$name]) || $value != $this->members[$name]) {
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
            if ($name == 'appname') {
                $this->application = Application::findByName($value);
            } elseif ($name == 'wwwroot') {
                if (!empty($this->appname) && is_object($this->application) && !empty($this->application->xmlrpcserverurl)) {
                    
                }
            } elseif ($name == 'appname') {
                
            }
            $this->members[$name] = $value;
            $this->changed = true;
        }

        if (!empty($this->wwwroot) &&
            !empty($this->name) &&
            !empty($this->institution) &&
            !empty($this->ipaddress) &&
            !empty($this->portno) &&
            !empty($this->appname) &&
            !empty($this->publickey) &&
            !empty($this->publickeyexpires)) {

            $this->initialized = self::INITIALIZED;
        }
        return $this;
    }

    public function findByWwwroot($wwwroot) {
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
        $wwwroot = dropslash($wwwroot);
        $result = get_record('host', 'wwwroot', $wwwroot);

        if ($result != false) {
            $this->populate($result);
            $this->initialized = self::PERSISTENT;
            $this->members['publickey'] = new PublicKey($this->members['publickey'], $this->members['wwwroot']);
            return $this;
        }
        return false;
    }

    public function __get($name) {
        if ($name == 'certificate') {
            return $this->members['publickey']->certificate;
        } elseif ($name == 'application') {
            return $this->application;
        } elseif ($name == 'publickeyexpires') {
            return $this->publickey->expires;
        }
        return $this->members[$name];
    }

    public function delete() {
        $this->deleted = 1;
        $this->changed = true;
    }

    public function commit() {
        if ($this->initialized == self::UNINITIALIZED) return false;
        if (false == $this->changed) return true;
        $host = new stdClass();
        $host->wwwroot          = $this->wwwroot;
        $host->deleted          = $this->deleted;
        $host->ipaddress        = $this->ipaddress;
        $host->name             = $this->name;
        $host->publickey        = $this->certificate;
        $host->publickeyexpires = $this->publickeyexpires;
        $host->portno           = $this->portno;
        $host->lastconnecttime  = $this->lastconnecttime;
        $host->appname          = $this->appname;
        $host->institution      = $this->institution;

        if ($this->initialized == self::INITIALIZED) {
            $this->initialized = self::PERSISTENT;
Donal McMullan's avatar
WIP    
Donal McMullan committed
150
151
152
            $exists = get_record('host', 'wwwroot', $host->wwwroot);
            if (false == $exists) {
                return insert_record('host',$host);
153
            }
Donal McMullan's avatar
WIP    
Donal McMullan committed
154
155
156
            return true;
        }

157
        return update_record('host',$host,array('wwwroot' => $host->wwwroot));
158
159
    }

Donal McMullan's avatar
WIP    
Donal McMullan committed
160
    public function bootstrap($wwwroot, $pubkey, $appname = 'moodle', $institution = null) {
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
188
189
190
191
192
193
194
195
196
197

        $wwwroot = dropslash($wwwroot);

        if ( ! $this->findByWwwroot($wwwroot) ) {

            $hostname = get_hostname_from_uri($wwwroot);

            // Get the IP address for that host - if this fails, it will
            // return the hostname string
            $ipaddress = gethostbyname($hostname);

            // Couldn't find the IP address?
            if ($ipaddress === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
                throw new ParamOutOfRangeException('Could not find IP address for host: '.addslashes($hostname));
                return false;
            }

            // Default the name to the wwwroot
            $this->name = $wwwroot;

            // Get a page from the remote host, and check its title.
            $homepage = file_get_contents($wwwroot);
            if (!empty($homepage) && $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches)) {
                $this->name = $matches[1];
            }

            $exists = get_record('application', 'name', $appname);

            if (empty($exists)) {
                throw new ParamOutOfRangeException('Application '.addslashes($appname) .' does not exist.');
            }

            $this->appname             = $appname;
            $this->application         = Application::findByName($this->appname);
            $this->wwwroot             = $wwwroot;
            $this->ipaddress           = $ipaddress;

198
            require_once(get_config('libroot') .'institution.php');
Donal McMullan's avatar
WIP    
Donal McMullan committed
199
200
201

            if (null == $institution) {
                $institution = new Institution;
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                $institution->name = preg_replace('/[^a-zA-Z]/', '', $this->name);

                // Check that the institution name has not already been taken. 
                // If it has, we change it until we find a name that works
                $existinginstitutionnames = get_column('institution', 'name');
                if (in_array($institution->name, $existinginstitutionnames)) {
                    $success = false;
                    foreach (range('a', 'z') as $character) {
                        $testname = $institution->name . $character;
                        if (!in_array($testname, $existinginstitutionnames)) {
                            $success = true;
                            $institution->name = $testname;
                            break;
                        }
                    }

                    if (!$success) {
                        // We couldn't find a unique name. Noes!
                        throw new RemoteServerException('Could not create a unique institution name');
                    }
                }

224
225
                $institution->displayname = $this->name;
                $institution->commit();
Donal McMullan's avatar
WIP    
Donal McMullan committed
226
227
228
                $this->institution = $institution->name;
            } else {
                $this->institution = $institution;
229
230
            }

231
            if (empty($pubkey)) {
232
233
234
235
236
237
238
                try {
                    $somekey = get_public_key($this->wwwroot, $this->appname);
                    $this->publickey       = new PublicKey($somekey, $this->wwwroot);
                } catch (XmlrpcClientException $e) {
                    $errcode = $e->getCode();
                    if ($errcode == 404) {
                        throw new RemoteServerException('404: Incorrect WWWRoot or Application: file not found.');
239
240
                    } elseif($errcode == 704) {
                        throw new RemoteServerException('Networking is disabled on the host at '.$this->wwwroot.'.');
241
                    }
242
                    else {
243
                        throw new RemoteServerException('Error retrieving public key, failed with error code ' . $errcode . ': ' . $e->getMessage());
244
                    }
245
                } catch (Exception $e) {
246
                    throw new RemoteServerException('Error retrieving public key: ' . $e->getMessage());
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
                }
            } else {
                $this->publickey       = new PublicKey($pubkey, $this->wwwroot);
            }

            $this->lastconnecttime     = 0;
            $this->initialized         = self::INITIALIZED;
            $this->changed             = true;
            if (false == $this->publickey->expires) {
                $this->publickey == null;
                return false;
            }

        }

        return true;
        
    }
}
?>