1: <?php
2: /**
3: * ManiaLive - TrackMania dedicated server manager in PHP
4: *
5: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
6: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
7: * @version $Revision: 328 $:
8: * @author $Author: melot.philippe $:
9: * @date $Date: 2012-01-16 10:54:45 +0100 (lun., 16 janv. 2012) $:
10: */
11:
12: namespace ManiaLive\Features;
13:
14: use ManiaLive\Event\Dispatcher;
15: use ManiaLive\Features\Tick\Listener as TickListener;
16: use ManiaLive\Features\Tick\Event as TickEvent;
17: use ManiaLive\Utilities\Console;
18:
19: /**
20: * Checks for updates periodically.
21: *
22: * @author Florian Schnell
23: */
24: class Updater extends \ManiaLib\Utils\Singleton implements TickListener
25: {
26: protected $lastDisplayed;
27:
28: protected function __construct()
29: {
30: $this->lastDisplayed = 0;
31: Dispatcher::register(TickEvent::getClass(), $this);
32: }
33:
34: /**
35: * (non-PHPdoc)
36: * @see libraries/ManiaLive/Features/Tick/ManiaLive\Features\Tick.Listener::onTick()
37: */
38: function onTick()
39: {
40: if ($this->lastDisplayed < time())
41: {
42: $this->checkUpdate();
43: $this->lastDisplayed = time() + 3600;
44: }
45: }
46:
47: /**
48: * Routine that compares version number to
49: * the latest version online.
50: */
51: function checkUpdate()
52: {
53: try
54: {
55: $client = new UpdaterClient();
56: // $response = $client->checkVersion(\ManiaLiveApplication\Version);
57: }
58: catch (\Exception $e)
59: {
60: Console::println('ERROR: Update service was unable to contact server ...');
61: return;
62: }
63:
64: // display message in console
65: // if ($response->update)
66: // {
67: // $days = ceil((time() - strtotime($response->version->date)) / 86400);
68: // Console::println(str_repeat('#', 79));
69: // Console::println('#' . str_repeat(' ', 77) . '#');
70: // Console::println(str_pad("# A new version of ManiaLive is available since $days day(s)!", 78) . "#");
71: // Console::println(str_pad("# An update is strongly recommended!", 78) . "#");
72: // Console::println('#' . str_repeat(' ', 77) . '#');
73: // Console::println(str_repeat('#', 79));
74: // }
75: }
76: }
77:
78: class UpdaterClient extends \Maniaplanet\WebServices\HTTPClient
79: {
80: function checkVersion($version)
81: {
82: return $this->execute('GET', '/manialive/version/check/'.$version.'/index.json');
83: }
84: }
85:
86: ?>