forked from steven-copley/nubuilder-forte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnubuilder-forte.php
256 lines (199 loc) · 10.6 KB
/
nubuilder-forte.php
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
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* @package nubuilder-forte
* @version 1.5
*/
/*
Plugin Name: nuBuilder Forte
Plugin URI: https://www.nubuilder.com/
Description: nuBuilder is a low-code, open-source application builder. Build reports and forms using Wordpress tables and any other table created in your Wordpress database.
Author: nuSoftware
Version: 1.5
Text Domain: nubuilder-forte
*/
if ( ! defined( 'ABSPATH' ) ) exit;
require_once('nubuilder-include.php');
class nuBuilderForte {
function __construct() {
nubuilderInitDB();
add_action('auth_redirect', 'nubuilderSetMenu');
add_action('init', 'nubuilderStartSession', 1);
add_shortcode( 'nubuilder', 'nubuilderForteShortcode' );
}
function activate() {
nubuilderWordpressImportNewDB();
nuCreateViews(true);
nubuilderConstructAccessLevels();
flush_rewrite_rules();
wp_register_script('nubuilder-forte', plugins_url('nubuilder-forte.php', __FILE__));
wp_enqueue_script('nubuilder-forte');
}
function deactivate() {
flush_rewrite_rules();
}
}
if ( class_exists( 'nuBuilderForte' ) ) {
$nuBuilderForte = new nuBuilderForte();
}
register_activation_hook( __FILE__, array( $nuBuilderForte, 'activate' ) );
register_deactivation_hook( __FILE__, array( $nuBuilderForte, 'deactivate' ) );
function nubuilderSetMenu() {
add_menu_page('nuBuilder Forte', 'nuBuilder', 'read', 'nubuilder-forte-slug', 'nubuilderMenuFunction', 'dashicons-chart-bar');
}
function nubuilderStartSession() {
if(!session_id()) {
session_start();
} else {
$_SESSION['nubuilder_wordpress_session_data'] = array();
$_SESSION['nubuilder_session_data'] = array();
}
}
function nubuilderMenuFunction() {
nubuilderStartSession();
nubuilderConstructSessionData();
nubuilderConstructAccessLevels();
$url = plugin_dir_url( __FILE__ );
$user = wp_get_current_user();
$roles = $user->roles;
if ( in_array('administrator', $roles) ) {
echo "<div class='welcome-panel'>";
echo "<p class='breadcrumbs'><span class='prefix'>You are here: <strong>nuBuilder for Wordpress</strong></span></p>";
echo "<h1 class='page-title'>nuBuilder Forte</h1>";
echo "<div class='welcome-panel'>";
echo "Using nuBuilder Forte with Wordpress will enable you to build your own custom reports and forms to manage your data.<br>";
echo "<a class='button button-primary button-hero' href='$url' target='_BLANK'>Start nuBuilder</a>";
echo '
<p>You can find help with nuBuilder Forte from the following sites:</p>
<ul>
<li><a href="https://www.nubuilder.com/wiki" target="_blank">Wiki Documentation</a></li>
<li><a href="https://www.nubuilder.com/videos" target="_blank">Video Tutorials</a></li>
<li><a href="https://www.nubuilder.com/forums" target="_blank">Forums</a></li>
<li><a href="https://www.nubuilder.com/nusoftware" target="_blank">nuSoftware Professional Services</a></li>
</ul>';
echo "When you use nuBuilder Forte inside of Wordpress, all Roles built into your Wordpress setup will be automatically inserted into your nuBuilder Access Levels.<br>";
echo "Users in Wordpress with the 'Administrator' role will be automatically granted <em>globeadmin</em> access inside nuBuilder.";
echo "</div>";
echo "</div>";
} else {
echo "<script type=\"text/javascript\">window.open('$url');</script>";
}
}
function nubuilderForteShortcode($short_code_attributes){
nubuilderStartSession();
$validated_short_code_attributes = nuValidateShortCode($short_code_attributes);
$is_user_logged_in_status = is_user_logged_in();
if ( empty($validated_short_code_attributes[NU_SHORT_CODE_AL]) && false === $is_user_logged_in_status ) {
// blank access level and no user signed in :: Scenario #3
return '';
} elseif ( empty($validated_short_code_attributes[NU_SHORT_CODE_AL]) && true === $is_user_logged_in_status ) {
// blank access level and a user is signed in :: Scenario #2
$nubuilder_session_data = nubuilderConstructSessionDataHelper();
$nubuilder_session_data->SHORT_CODE_REQUEST = 'N';
$nubuilder_session_data->SHORT_CODE_SESSION_NAME = nuID();
$nubuilder_session_data->SHORT_CODE_ACCESS_LEVEL = ''; // will get when inside nubuilder
$auth_info = wp_get_current_user();
$nubuilder_session_data->USER_LOGIN = $auth_info->user_login;
$nubuilder_session_data->USER_PASS = $auth_info->user_pass;
$nubuilder_session_data->USER_EMAIL = $auth_info->user_email;
$nubuilder_session_data->USER_DISPLAY_NAME = $auth_info->display_name;
$nubuilder_session_data->USER_ROLES = $auth_info->roles;
if ( in_array('administrator',$auth_info->roles) ) {
$nubuilder_session_data->GLOBEADMIN = true;
}
} elseif ( !empty($validated_short_code_attributes[NU_SHORT_CODE_AL]) ) {
// non-blank access level :: Scenario #1
$nubuilder_session_data = nubuilderConstructSessionDataHelper();
$nubuilder_session_data->SHORT_CODE_REQUEST = 'Y';
$nubuilder_session_data->SHORT_CODE_SESSION_NAME = nuID();
$nubuilder_session_data->SHORT_CODE_ACCESS_LEVEL = $validated_short_code_attributes[NU_SHORT_CODE_AL];
$nubuilder_session_data->USER_LOGIN = '';
$nubuilder_session_data->USER_PASS = '';
$nubuilder_session_data->USER_EMAIL = '';
$nubuilder_session_data->USER_DISPLAY_NAME = '';
$nubuilder_session_data->USER_ROLES = '';
} else {
return '';
}
$json = json_encode($nubuilder_session_data);
$encode = base64_encode($json);
$_SESSION['nubuilder_short_code_data'] = $encode;
$iframe_url = plugin_dir_url( __FILE__ ).'shortcode.php';
$width = $validated_short_code_attributes[NU_SHORT_CODE_WIDTH] . 'px';
$height = $validated_short_code_attributes[NU_SHORT_CODE_HEIGHT] . 'px';
$top = $validated_short_code_attributes[NU_SHORT_CODE_TOP] . 'px';
$left = $validated_short_code_attributes[NU_SHORT_CODE_LEFT] . 'px';
if ( $validated_short_code_attributes[NU_SHORT_CODE_STYLE] == '' ) {
$style = "style='position:absolute;overflow:hidden;top:$top;left:$left;border-style:hidden;border-width:0px;border-color:lightgrey;width:$width;height:$height'";
} else {
$style = "style='" . $validated_short_code_attributes[NU_SHORT_CODE_STYLE] ."'";
}
$result = "<iframe id='nubuilder4_iframe' $style scrolling='no' src='$iframe_url'></iframe>";
return $result;
}
function nubuilderConstructSessionDataHelper() {
$nubuilder_session_data = new stdClass;
$nubuilder_session_data->PLUGIN = true;
$nubuilder_session_data->GLOBEADMIN = false;
$nubuilder_session_data->DB_NAME = DB_NAME;
$nubuilder_session_data->DB_USER = DB_USER;
$nubuilder_session_data->DB_PASSWORD = DB_PASSWORD;
$nubuilder_session_data->DB_HOST = DB_HOST;
$nubuilder_session_data->DB_CHARSET = DB_CHARSET;
$nubuilder_session_data->NU_SITE_URL = function_exists('plugin_dir_url') ? plugin_dir_url( __FILE__ ) : '';
$nubuilder_session_data->WP_ADMIN_URL = function_exists('admin_url') ? admin_url() : '';
$nubuilder_session_data->WP_ROLES = nubuilderConstructRoles();
$nubuilder_session_data->WP_SITE_URL = function_exists('site_url') ? site_url() : '';
$nubuilder_session_data->WP_HOME_PATH = function_exists('get_home_path') ? get_home_path() : '';
$nubuilder_session_data->WP_ABSPATH = ABSPATH;
$nubuilder_session_data->WP_WPINC = WPINC;
$nubuilder_session_data->WP_BLOG_CHARSET = function_exists('get_option') ? get_option('blog_charset') : '';
return $nubuilder_session_data;
}
function nubuilderConstructSessionData() {
$auth_info = wp_get_current_user();
$nubuilder_session_data = nubuilderConstructSessionDataHelper();
if ( in_array('administrator',$auth_info->roles) ) {
$nubuilder_session_data->GLOBEADMIN = true;
}
$nubuilder_session_data->USER_LOGIN = $auth_info->user_login;
$nubuilder_session_data->USER_PASS = $auth_info->user_pass;
$nubuilder_session_data->USER_EMAIL = $auth_info->user_email;
$nubuilder_session_data->USER_DISPLAY_NAME = $auth_info->display_name;
$nubuilder_session_data->USER_ROLES = $auth_info->roles;
$json = json_encode($nubuilder_session_data);
$encode = base64_encode($json);
$_SESSION['nubuilder_wordpress_session_data'] = $encode;
}
// used during activation
function nubuilderConstructAccessLevels() {
$select = "SELECT * FROM zzzzsys_access WHERE zzzzsys_access_id = ? ";
$insert = "INSERT INTO zzzzsys_access (zzzzsys_access_id, sal_code, sal_description, sal_zzzzsys_form_id) VALUES (?, ?, ?, 'nuuserhome')";
$roles = nubuilderConstructRoles();
for( $x=0; $x < count($roles); $x++ ) {
$zzzzsys_access_id = $roles[$x][0];
$sal_code = $roles[$x][1];
$sal_description = $roles[$x][2];
$select_values = array($zzzzsys_access_id);
$insert_values = array($zzzzsys_access_id, $sal_code, $sal_description);
$rs = nuRunQuery($select, $select_values);
if ( db_num_rows($rs) == 0 ) {
// this will insert access level only if they don't already exits
nuRunQuery($insert, $insert_values);
}
}
}
// used during activation
function nubuilderConstructRoles() {
global $wp_roles;
$roles = $wp_roles->get_names();
$nu_roles = array();
foreach ($roles as $key => $value) {
$nu_wp_role_id = nubuilderDerriveId($key);
$nu_wp_role_code = nubuilderDerriveCode($key);
$nu_wp_role_desc = trim($value);
$this_array = array($nu_wp_role_id,$nu_wp_role_code,$nu_wp_role_desc);
$nu_roles[] = $this_array;
}
return $nu_roles;
}
?>