root/trunk/scripts/JlogUpdater.php

Revision 1768, 4.7 KB (checked in by driehle, 3 months ago)

converted tabs to 4 spaces

Line 
1<?php
2
3
4class JlogUpdater 
5{
6    /**
7     * Existing versions of Jlog as array
8     * version -> next version in history
9     *
10     * @var array
11     */
12    var $versions = array(
13        '1.0.2' => '1.1.0',
14        '1.1.0' => '1.1.1',
15        '1.1.1' => '1.1.2'
16    );
17   
18    function JlogUpdater() 
19    {
20        require_once(JLOG_BASEPATH."scripts".DIRECTORY_SEPARATOR."settings.class.php");
21    }
22   
23    function getOldVersion() 
24    {
25        return JLOG_INSTALLED_VERSION;
26    }
27   
28    function getNewVersion() 
29    {
30        return JLOG_SOFTWARE_VERSION;
31    }
32   
33    function isUp2Date() 
34    {
35        if (version_compare($this->getOldVersion(), $this->getNewVersion(), '<')) {
36            return false;
37        }
38        return true;
39    }
40   
41    function prepareForm($l) 
42    {
43        $html = '<form action="' . $_SERVER['SCRIPT_NAME'] . '" method="post">'
44              . '<p>' . $l['admin']['e_admin_password'] . ': '
45              . '<input type="password" name="jlog_password" value="" />'
46              . '</p>';
47        $version = $this->getOldVersion();
48        while (isset($this->versions[$version])) {
49            $class = $this->_loadUpdateClass($version, $this->versions[$version]);
50            $html .= sprintf("<h2>Update <var>%s</var> &#x2192; <var>%s</var></h2>\n", $version, $this->versions[$version]);
51            $html .= $class->getForm($l);
52            $version = $this->versions[$version];
53        }
54        $html .= '<p><input type="submit" name="update" value="' . $l['admin']['update_start'] . '" /></p>';
55        $html .= '</form>';
56        return $html;
57    }
58   
59    function performUpdate($l) 
60    {
61        if (JLOG_AMDIN_PASSWORD !== md5($_POST['jlog_password']) and JLOG_ADMIN_PASSWORD !== md5(utf8_decode($_POST['jlog_password']))) {
62            return '<p>' . $l['admin']['login_false_pw'] . '</p>';
63        }
64       
65        require_once(JLOG_BASEPATH."scripts".DIRECTORY_SEPARATOR."settings.class.php");
66        // read current settings from environment
67        $settings = new Settings($l);
68        $settings->importDataByConstants();
69       
70        $error = false;
71        $html = '';
72        $version = $this->getOldVersion();
73        while (isset($this->versions[$version])) {
74            $class = $this->_loadUpdateClass($version, $this->versions[$version]);
75            $html .= sprintf("<h2>Update <var>%s</var> &#x2192; <var>%s</var></h2>\n", $version, $this->versions[$version]);
76            $result = $class->performUpdate($l, $settings);
77            if ($result === true) {
78                // we know that update class ran successfully
79                $result = $this->_updateVersionNumber($settings, $this->versions[$version]);
80                // check if errors occured
81                if (!empty($result)) {
82                    $this->_renderErrors($result);
83                    break;
84                }
85                else {
86                    $html .= '<p>' . $l['admin']['update_successfull_part'] . '</p>';
87                }
88            }
89            else {
90                $html .= $this->_renderErrors($result);
91                break;
92            }
93            $version = $this->versions[$version];
94        }
95        if ($error) {
96            $html .= '<p>' . $l['admin']['update_failure'] . '</p>';
97        }
98        else {
99            $html .= '<p>' . $l['admin']['update_successfull'] . '</p>';
100        }
101        return $html;
102    }
103   
104    function _getUpdateFile($oldver, $newver) 
105    {
106        $oldver = str_replace('.', '', $oldver);
107        $newver = str_replace('.', '', $newver);
108        return "{$oldver}To{$newver}.php";
109    }
110   
111    function _getUpdateClass($oldver, $newver) 
112    {
113        $oldver = str_replace('.', '', $oldver);
114        $newver = str_replace('.', '', $newver);
115        return "JlogUpdate_{$oldver}To{$newver}";
116    }
117   
118    function _loadUpdateClass($oldver, $newver) 
119    {
120        $file = $this->_getUpdateFile($oldver, $newver);
121        $class = $this->_getUpdateClass($oldver, $newver);
122        require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . $file);
123        return new $class(); 
124    }
125   
126    function _renderErrors($errors)
127    {
128        $html = '<ul class="error">';
129        foreach ($errors as $error) {
130            $html .= '<li>' . $error . '</li>';
131        }
132        $html .= '</ul>';
133        return $html;
134    }
135   
136    function _updateVersionNumber($settings, $newver) 
137    {   
138        $settings->setValue('jlog_installed_version', $newver);
139        $settings->setValue('jlog_installed_url', JLOG_SOFTWARE_URL);
140        $settings->setValue('jlog_installed_phpv', JLOG_SOFTWARE_PHPV);
141        $settings->setValue('jlog_installed_mysqlv', JLOG_SOFTWARE_MYSQLV);
142   
143        // rewrite settings.inc.php
144        return $settings->do_settings();
145    }
146}
Note: See TracBrowser for help on using the browser.