File: controller.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (244 lines) | stat: -rw-r--r-- 7,281 bytes parent folder | download
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
 * Controller for installation
 *
 * This file is part of Zoph.
 *
 * Zoph 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 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph 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.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */

namespace install;

use db\db;
use db\install;
use db\select;
use tables\collection as allTables;
use conf\conf;
use generic\controller as genericController;
use requirements;
use settings;
use template\block;
use user;
use auth\validator;
use web\request;
use zoph\app;

use configurationException;
use db\exception as dbException;
use securityException;
use PDOException;

/**
 * Controller for migrations
 */
class controller extends genericController {
    protected static $viewAdminuser     = view\adminuser::class;
    protected static $viewConfig        = view\config::class;
    protected static $viewDatabase      = view\database::class;
    protected static $viewDbcheck       = view\dbcheck::class;
    protected static $viewDbpass        = view\dbpass::class;
    protected static $viewDisplay       = view\display::class;
    protected static $viewFinish        = view\finish::class;
    protected static $viewInifile       = view\inifile::class;
    protected static $viewIniform       = view\iniform::class;
    protected static $viewRequirements  = view\requirements::class;
    protected static $viewTables        = view\tables::class;

    /** @var array Actions that can be used in this controller
     *
     * The installation goes through the following stages:
     * display -> requirements -> iniform -> inifile -> dbcheck -> dbpass -> database -> tables -> adminuser -> config -> finish
     */
    protected   $actions    = array(
        "display",
        "requirements",
        "iniform",
        "inifile",
        "dbcheck",
        "dbpass",
        "database",
        "tables",
        "adminuser",
        "config",
        "finish"
    );

   /**
     * Do action
     * @param string action
     */
    public function doAction(string $action) : void {
        if ($action !== "display" && $action !== "" && !app::checkToken($this->request["token"] ?? "")) {
            throw new securityException("Configuration not possible");
        }
        try {
            if (!in_array($action, array("adminuser", "config", "finish"))) {
                db::query(new select("photos"));
                if (app::getMode(app::TEST)) {
                    throw new configurationException("Testing...");
                }
                die("no configuration necessary");
            } else {
                parent::doAction($action);
            }
        } catch (dbException | configurationException | PDOException $e) {
            parent::doAction($action);
        }

    }


    /**
     * Do action 'display'
     */
    public function actionDisplay() {
        app::setToken();
        $this->view = new static::$viewDisplay($this->request);
    }

    /**
     * Do action 'requirements'
     */
    public function actionRequirements() {
        $requirements = new requirements\check();
        $requirements->doChecks();

        $this->view = new static::$viewRequirements($this->request);
        $this->view->addRequirements($requirements);
    }

    /**
     * Do action 'iniform'
     */
    public function actionIniform() {
        $this->view = new static::$viewIniform($this->request);
    }

    /**
     * Do action 'inifile'
     */
    public function actionInifile() {
        $inifile =
            "[" . $this->request->name . "]\n" .
            "    db_host = \"" . $this->request->db_host . "\"\n" .
            "    db_name = \"" . $this->request->db_name . "\"\n" .
            "    db_user = \"" . $this->request->db_user . "\"\n" .
            "    db_pass = \"" . $this->request->db_pass . "\"\n" .
            "    db_prefix = \"" . $this->request->db_prefix . "\"\n" .
            "    php_location = \"" . settings::$phpLocation . "\"\n\n";

        $this->view = new static::$viewInifile($this->request);
        $this->view->setInifile($inifile);
    }

    /**
     * Do action 'dbcheck'
     * This checks if the INI file is correct
     * and whether or not the database already exists
     */
    public function actionDbcheck() {
        $this->view = new static::$viewDbcheck($this->request);

        try {
            settings::loadINI();
        } catch (configurationException $e) {
            $this->view->setError("INI file incorrect. " . $e->getMessage());
        }

        try {
            foreach (array("photos", "albums", "categories", "places", "people") as $table) {
                $dbinfo[$table] = (new \db\select($table))->addFunction(array("count" => "COUNT(*)"))->getCount();
            }
            $this->view->setDatabaseInfo($dbinfo);
        } catch (dbException $e) {
            // Exception because database does not exist
            // in this case, this is expected.
        }

    }

    /**
     * Do action 'dbpass'
     */
    public function actionDbPass() {
        $this->view = new static::$viewDbpass($this->request);

    }

    /**
     * Do action 'database'
     */
    public function actionDatabase() {
        $this->view = new static::$viewDatabase($this->request);

        $dbLogin = db::getLoginDetails();

        $dbuser = $this->request["user"] ?? $dbLogin["user"];
        $dbpass = $this->request["admin_pass"] ?? $dbLogin["pass"];

        $install = new install($dbuser, $dbpass);
        $install->user();
        $install->create();
        $install->grant();
        $this->view->setResults($install->getResults());
    }

    /**
     * Do action 'tables'
     */
    public function actionTables() {
        $this->view = new static::$viewTables($this->request);

        $results = array();

        foreach (new allTables() as $table) {
            $table->create();
            $table->addData();
            $results[]=$table->getResult();
        }

        $this->view->setResults($results);
    }

    /**
     * Do action 'adminuser'
     */
    public function actionAdminuser() {
        $this->view = new static::$viewAdminuser($this->request);
    }

    /**
     * Do action 'config'
     */
    public function actionConfig() {
        $this->view = new static::$viewConfig($this->request);

        $password = $this->request["admin_pass"];

        $user = user::getByName("admin");
        $user->set("password", validator::hashPassword($password));
        $user->update();
    }

    /**
     * Do action 'finish'
     */
    public function actionFinish() {
        $this->view = new static::$viewFinish($this->request);
        conf::loadFromRequestVars($this->request->getRequestVars());
    }
}