Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 121
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InstallController
0.00% covered (danger)
0.00%
0 / 121
0.00% covered (danger)
0.00%
0 / 3
342
0.00% covered (danger)
0.00%
0 / 1
 pageSetupDB
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 1
90
 pageSetupMasterAccount
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 setupMasterAccount
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace BuyerKiosk\Core\Controllers;
4
5// Handles installation-related activities
6class InstallController extends BaseController {
7
8    public function pageSetupDB(){
9        $messages = [];
10        // 1. Check PHP version
11        
12        // PHP_VERSION_ID is available as of PHP 5.2.7, if our version is lower than that, then emulate it
13        if (!defined('PHP_VERSION_ID')) {
14            $version = explode('.', PHP_VERSION);
15            define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
16        }
17        if (PHP_VERSION_ID < 50400){
18            $messages[] = [
19                "title" => "You need to upgrade your PHP installation.",
20                "message" => "I'm sorry, UserFrosting relies on numerous features of PHP that are only available in PHP 5.4 or later.  Please upgrade your version of PHP, or contact your web hosting service and ask them to upgrade it for you."
21            ];
22        }
23        
24        // 2. Check that PDO is installed and enabled
25        if (!class_exists('PDO')){
26            $messages[] = [
27                "title" => "PDO is not installed.",
28                "message" => "I'm sorry, you must have PDO installed and enabled in order for UserFrosting to access the database.  If you don't know what PDO is, please see <a href='http://php.net/manual/en/book.pdo.php'>http://php.net/manual/en/book.pdo.php</a>.  You must also have MySQL version 4.1 or higher installed, since UserFrosting relies on native prepared statements."
29            ];    
30        }
31        
32        // 3. Check database connection
33        
34        if (!Database::testConnection()){
35            $messages[] = [
36                "title" => "We couldn't connect to your database.",
37                "message" => "Make sure that your database is properly configured in <code>config-userfrosting.php</code>, and that you have selected the correct configuration mode ('dev' or 'production').  Also, make sure that your database user has the proper privileges to connect to the database."
38            ]; 
39        } 
40        
41        $tables = Database::getCreatedTables();
42        if (count($tables) > 0){
43            $messages[] = [
44                "title" => "One or more tables already exist.",
45                "message" => "The following tables already exist in the database: <strong>" . implode(", ", $tables) . "</strong>.  Do you already have another installation of UserFrosting in this database?  Please either create a new database (recommended), or change the table prefix in <code>config-userfrosting.php</code> if you cannot create a new database."
46            ]; 
47        }
48        
49        if (count($messages) > 0){
50            $this->_app->render('common/install/install-errors.html', [
51                'page' => [
52                    'author' =>         $this->_app->site->author,
53                    'title' =>          "Installation Error",
54                    'description' =>    "Installation page for UserFrosting",
55                    'alerts' =>         $this->_app->alerts->getAndClearMessages()
56                ],
57                "messages" => $messages
58            ]);
59        } else {
60            // Create tables
61            Database::install();
62            
63            $messages[] = [
64                "title" => "<i class='fa fa-lock'></i> PDO is installed.",
65                "message" => "No need to worry about any pesky SQL injection attacks!",
66                "class" => "success"
67            ];
68            
69            $messages[] = [
70                "title" => "<i class='fa fa-database'></i> Database connection",
71                "message" => "Hooray!  We were able to connect to your database and create the core tables for UserFrosting.",
72                "class" => "success"
73            ];
74            if (PHP_VERSION_ID >= 50400 && PHP_VERSION_ID < 50500){
75               $messages[] = [
76                    "title" => "<i class='fa fa-warning'></i> PHP version",
77                    "message" => "You currently have version " . PHP_VERSION . " of PHP installed.  We recommend version 5.5 or later.  UserFrosting can still be installed, but we highly recommend you upgrade soon.", 
78                    "class" => "warning"
79                ];
80            } else {
81                $messages[] = [
82                    "title" => "<i class='fa fa-check'></i> PHP version",
83                    "message" => "You currently have version " . PHP_VERSION . " of PHP installed.  Good job!", 
84                    "class" => "success"
85                ]; 
86            }            
87            
88            $this->_app->render('common/install/install-ready.html', [
89                'page' => [
90                    'author' =>         $this->_app->site->author,
91                    'title' =>          "Installation",
92                    'description' =>    "Installation page for UserFrosting",
93                    'alerts' =>         $this->_app->alerts->getAndClearMessages()
94                ],
95                "messages" => $messages
96            ]);        
97        }
98    }
99    
100    public function pageSetupMasterAccount(){
101        
102        // Get the alert message stream
103        $ms = $this->_app->alerts;
104        
105        // Do not allow registering a master account if one has already been created     
106        if (UserLoader::exists($this->_app->config('user_id_master'))){
107            $ms->addMessageTranslated("danger", "MASTER_ACCOUNT_EXISTS");
108            $this->_app->redirect($this->_app->urlFor('uri_home'));
109        }
110        
111        // Load validator rules
112        $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/install.json");
113        $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema);
114
115        $this->_app->render('common/install/install-master.html', [
116            'page' => [
117                'author' =>         $this->_app->site->author,
118                'title' =>          "Installation | Register Master Account",
119                'description' =>    "Set up the master account for your installation of UserFrosting",
120                'alerts' =>         $this->_app->alerts->getAndClearMessages()
121            ],
122            'validators' => $validators->formValidationRulesJson(),
123            'table_config' => Database::getTable('configuration')->name
124        ]);    
125    }
126
127    public function setupMasterAccount(){
128        $post = $this->_app->request->post();
129        
130        // Get the alert message stream
131        $ms = $this->_app->alerts;
132        
133        // Check the honeypot. 'spiderbro' is not a real field, it is hidden on the main page and must be submitted with its default value for this to be processed.
134        if (!$post['spiderbro'] || $post['spiderbro'] != "http://"){
135            error_log("Possible spam received:" . print_r($this->_app->request->post(), true));
136            $ms->addMessage("danger", "Aww hellllls no!");
137            $this->_app->halt(500);     // Don't let on about why the request failed ;-)
138        }  
139        
140        // Do not allow registering a master account if one has already been created     
141        if (UserLoader::exists($this->_app->config('user_id_master'))){
142            $ms->addMessageTranslated("danger", "MASTER_ACCOUNT_EXISTS");
143            $this->_app->halt(403); 
144        }        
145        
146        // Check the configuration token
147        if ($post['root_account_config_token'] != $this->_app->site->root_account_config_token) {
148            $ms->addMessageTranslated("danger", "CONFIG_TOKEN_MISMATCH");
149            $this->_app->halt(403);
150        }
151          
152        // Load the request schema
153        $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/register.json");
154                   
155        // Set up Fortress to process the request
156        $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post);        
157        
158        // Sanitize data
159        $rf->sanitize();
160                
161        // Validate, and halt on validation errors.
162        $error = !$rf->validate(true);
163        
164        // Get the filtered data
165        $data = $rf->data();        
166        
167        // Remove configuration token, password confirmation from object data
168        $rf->removeFields(['root_account_config_token', 'passwordc']);
169        
170        // Perform desired data transformations.  Is this a feature we could add to Fortress?
171        $data['user_name'] = strtolower(trim($data['user_name']));
172        $data['display_name'] = trim($data['display_name']);
173        $data['email'] = strtolower(trim($data['email']));
174        $data['active'] = 1;
175        $data['locale'] = $this->_app->site->default_locale;
176                
177        // Halt on any validation errors
178        if ($error) {
179            $this->_app->halt(400);
180        }
181    
182        // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY)
183        $primaryGroup = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default");
184        $data['primary_group_id'] = $primaryGroup->id;
185        // Set default title for new users
186        $data['title'] = $primaryGroup->new_user_title;
187        // Hash password
188        $data['password'] = Authentication::hashPassword($data['password']);
189            
190        // Create the user
191        $user = new User($data, $this->_app->config('user_id_master'));
192
193        // Add user to default groups, including default primary group
194        $defaultGroups = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default");
195        $user->addGroup($primaryGroup->id);
196        foreach ($defaultGroups as $group_id => $group)
197            $user->addGroup($group_id);    
198        
199        // Store new user to database, forcing it to insert the new user
200        $user->store(true);
201        // No activation required
202        $ms->addMessageTranslated("success", "ACCOUNT_REGISTRATION_COMPLETE_TYPE1");
203        
204        // Update install status
205        $this->_app->site->install_status = "new";
206        $this->_app->site->root_account_config_token = "";
207        $this->_app->site->store();
208    }
209    
210}
211
212?>