PluginProbe
Moloni / trunk
Moloni vtrunk
5.0.10 5.0.09 5.0.08 5.0.07 5.0.06 trunk 0.4.0.00 0.4.8.1 3.0.10 3.0.11 3.0.35 3.0.36 3.0.37 3.0.38 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.0.46 3.0.47 3.0.48 3.0.49 3.0.50 All 98 releases
moloni / src / Start.php

Start.php in Moloni trunk, at src/Start.php

175 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Moloni;
4
5 use Moloni\Enums\Boolean;
6 use Moloni\Helpers\Debug;
7 use Moloni\Exceptions\APIException;
8
9 /**
10 * Class Start
11 * This is one of the main classes of the module
12 * Every call should pass here before
13 * This will render the login form or the company form, or it will return a bool
14 * This will also handle the tokens
15 * @package Moloni
16 */
17 class Start
18 {
19 /** @var bool */
20 private static $ajax = false;
21
22 /**
23 * Handles session, login and settings
24 *
25 * @param bool $ajax
26 *
27 * @return bool
28 */
29 public static function login($ajax = false)
30 {
31 global $wpdb;
32
33 $action = isset($_REQUEST['action']) ? sanitize_text_field(trim($_REQUEST['action'])) : '';
34 $username = isset($_POST['user']) ? sanitize_email(trim($_POST['user'])) : '';
35 $password = isset($_POST['pass']) ? stripslashes(sanitize_text_field(trim($_POST['pass']))) : '';
36
37 if ($ajax) {
38 self::$ajax = true;
39 }
40
41 if (!empty($username) && !empty($password)) {
42 $loginValid = false;
43 $errorMessage = '';
44 $errorBag = [];
45
46 try {
47 $login = Curl::login($username, $password);
48
49 if ($login && isset($login['access_token'])) {
50 $loginValid = true;
51
52 Model::setTokens($login['access_token'], $login['refresh_token']);
53 }
54 } catch (APIException $e) {
55 $errorMessage = $e->getMessage();
56 $errorBag = $e->getData();
57 }
58
59 if (!$loginValid) {
60 self::loginForm($errorMessage, $errorBag);
61 return false;
62 }
63 }
64
65 if ($action === 'save') {
66 self::saveSettings();
67 }
68
69 if ($action === 'logout') {
70 Model::resetTokens();
71 }
72
73 $tokensRow = Model::getTokensRow();
74 if (!empty($tokensRow['main_token']) && !empty($tokensRow['refresh_token'])) {
75 Model::defineConfigs();
76 Model::refreshTokens();
77 Model::defineValues();
78
79 if (empty(Storage::$MOLONI_ACCESS_TOKEN)) {
80 self::loginForm();
81 return false;
82 }
83
84 if (Storage::$MOLONI_COMPANY_ID) {
85 return true;
86 }
87
88 if (isset($_GET['company_id'])) {
89 $wpdb->update($wpdb->get_blog_prefix() . 'moloni_api', [
90 'company_id' => (int)$_GET['company_id']
91 ],
92 [
93 'id' => Storage::$MOLONI_SESSION_ID
94 ]
95 );
96
97 Model::defineValues();
98 return true;
99 }
100
101 self::companiesForm();
102 return false;
103 }
104
105 self::loginForm();
106 return false;
107 }
108
109 /**
110 * Shows a login form
111 *
112 * @param bool|string $error Is used in include
113 * @param bool|array $errorData Is used in include
114 */
115 public static function loginForm($error = false, $errorData = false)
116 {
117 if (self::$ajax) {
118 return;
119 }
120
121 include(MOLONI_TEMPLATE_DIR . 'LoginForm.php');
122 }
123
124 /**
125 * Draw all companies available to the user
126 * Except the demo one
127 */
128 public static function companiesForm()
129 {
130 if (self::$ajax) {
131 return;
132 }
133
134 try {
135 $companies = Curl::simple('companies/getAll', []);
136 } catch (APIException $e) {
137 $companies = [];
138 }
139
140 foreach ($companies as $key => $company) {
141 if ((int)$company['company_id'] !== 5) {
142 continue;
143 }
144
145 unset($companies[$key]);
146
147 break;
148 }
149
150 include(MOLONI_TEMPLATE_DIR . 'CompanySelect.php');
151 }
152
153 /**
154 * Save plugin settings
155 *
156 * @return void
157 */
158 private static function saveSettings(): void
159 {
160 add_settings_error('general', 'settings_updated', __('Alterações guardadas.'), 'updated');
161 $options = $_POST['opt'];
162
163 foreach ($options as $option => $value) {
164 $option = sanitize_text_field($option);
165 $value = sanitize_text_field($value);
166
167 Model::setOption($option, $value);
168 }
169
170 if (isset($options['moloni_debug_mode']) && (int)$options['moloni_debug_mode'] === Boolean::NO) {
171 Debug::deleteAllLogs();
172 }
173 }
174 }
175