Completed
Push — demo-2-part-migration ( a53207 )
by Ilia
10:35
created

start.php ➔ pleio_migration_page_handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__) . "/../../vendor/autoload.php");
3
spl_autoload_register("pleio_autoloader");
4
function pleio_autoloader($class) {
5
    $filename = "classes/" . str_replace("\\", "/", $class) . ".php";
6
    if (file_exists(dirname(__FILE__) . "/" . $filename)) {
7
        include($filename);
8
    }
9
}
10
11
elgg_register_event_handler("init", "system", "pleio_init");
12
13
function pleio_init() {
14
    elgg_unregister_page_handler("login");
15
    elgg_register_page_handler("login", "pleio_page_handler");
16
    elgg_register_page_handler("migrate_openid", "pleio_migration_page_handler");
17
18
    elgg_unregister_action("register");
19
    elgg_unregister_page_handler("register");
20
21
    elgg_unregister_action("logout");
22
    elgg_register_action("logout", dirname(__FILE__) . "/actions/logout.php", "public");
23
24
    elgg_unregister_action("user/passwordreset");
25
    elgg_unregister_action("user/requestnewpassword");
26
27
    elgg_unregister_action("admin/user/resetpassword");
28
29
    elgg_unregister_menu_item("page", "users:add");
30
    elgg_unregister_action("useradd");
31
32
    elgg_register_plugin_hook_handler("register", "menu:user_hover", "pleio_user_hover_menu");
33
34
    elgg_unregister_plugin_hook_handler("usersettings:save", "user", "users_settings_save");
35
36
    elgg_unregister_action("admin/site/update_advanced");
37
    elgg_register_action("admin/site/update_advanced", dirname(__FILE__) . "/actions/admin/site/update_advanced.php", "admin");
38
39
    elgg_register_page_handler("register", "pleio_register_page_handler");
40
    elgg_register_page_handler("access_requested", "pleio_access_requested_page_handler");
41
42
    elgg_register_action("pleio/request_access", dirname(__FILE__) . "/actions/request_access.php", "public");
43
    elgg_register_action("admin/pleio/process_access", dirname(__FILE__) . "/actions/admin/process_access.php", "admin");
44
45
    elgg_register_plugin_hook_handler("public_pages", "walled_garden", "pleio_public_pages_handler");
46
    elgg_register_plugin_hook_handler("action", "admin/site/update_basic", "pleio_admin_update_basic_handler");
47
48
    // elgg_register_plugin_hook_handler("entity:icon:url", "user", "pleio_user_icon_url_handler");
49
    // elgg_register_admin_menu_item("administer", "access_requests", "users");
50
    // elgg_register_admin_menu_item("administer", "import", "users");
51
    
52
    elgg_register_action("admin/user/import_step1", dirname(__FILE__) . "/actions/admin/user/import_step1.php", "admin");
53
    elgg_register_action("admin/user/import_step2", dirname(__FILE__) . "/actions/admin/user/import_step2.php", "admin");
54
55
    elgg_extend_view("css/elgg", "pleio/css/site");
56
    elgg_extend_view("page/elements/head", "page/elements/topbar/fix");
57
    elgg_extend_view("page/elements/foot", "page/elements/stats");
58
59
    if ( elgg_is_active_plugin('web_services') ) {
60
        elgg_ws_expose_function(
61
            "pleio.verifyuser",
62
            "pleio_verify_user_creds",
63
            array(
64
                "user" => array('type' => 'string', 'required' => true),
65
                "password" => array('type' => 'string', 'required' => true)
66
            ),
67
            'Verifies user credentials based on email and password.',
68
            'POST',
69
            false,
70
            false
71
        );
72
73
        function pleio_verify_user_creds($user, $password) {
74
            $user_entity = get_user_by_email($user)[0];
75
76
            if (!$user_entity) {
77
                return json_encode(false);
78
            }
79
80
            $username = $user_entity->username;
81
            $name = $user_entity->name;
82
            $admin = elgg_is_admin_user($user_entity->guid);
83
            $valid = elgg_authenticate($username, $password);
84
85
            $return = array("name" => $name, "valid" => $valid, "admin" => $admin);
86
87
            return $return;
88
        }
89
90
        elgg_ws_expose_function(
91
            "pleio.userexists",
92
            "pleio_verify_user_exists",
93
            array(
94
                "user" => array('type' => 'string', 'required' => true)
95
            ),
96
            'Verifies user exists based on email.',
97
            'POST',
98
            false,
99
            false
100
        );
101
102
        function pleio_verify_user_exists($user) {
103
            $user_entity = get_user_by_email($user)[0];
104
105
            if (!$user_entity) {
106
                return json_encode(false);
107
            }
108
109
            $return = array("name" => $user_entity->name, "valid" => true);
110
111
            return $return;
112
        }
113
114
        if( elgg_is_active_plugin('gcRegistration_invitation') ){
115
            elgg_ws_expose_function(
116
                "pleio.invited",
117
                "pleio_invited",
118
                array(
119
                    "email" => array('type' => 'string', 'required' => true)
120
                ),
121
                'Verifies email address is in invitation list.',
122
                'POST',
123
                false,
124
                false
125
            );
126
127
            function pleio_invited($email) {
128
                $valid = json_encode(false);
129
130
                // Checks against the email invitation list...
131
                $invitation_query = "SELECT email FROM email_invitations WHERE email = '{$email}'";
132
                $result = get_data($invitation_query);
133
134
                if( count($result) > 0 ) 
135
                    $valid = true;
136
137
                return $valid;
138
            }
139
        }
140
    }
141
}
142
143
function pleio_page_handler($page) {
144
    include(dirname(__FILE__) . "/pages/login.php");
145
}
146
function pleio_migration_page_handler($page) {
147
    include(dirname(__FILE__) . "/pages/migrate_openid.php");
148
}
149
150
function pleio_access_requested_page_handler($page) {
151
    $body = elgg_view_layout("walled_garden", [
152
        "content" => elgg_view("pleio/access_requested"),
153
        "class" => "elgg-walledgarden-double",
154
        "id" => "elgg-walledgarden-login"
155
    ]);
156
157
    echo elgg_view_page(elgg_echo("pleio:access_requested"), $body, "walled_garden");
158
    return true;
159
}
160
161
function pleio_register_page_handler($page) {
162
    forward("/login");
163
    return true;
164
}
165
166
function pleio_admin_update_basic_handler($hook, $type, $value, $params) {
167
    $site = elgg_get_site_entity();
168
169
    $site_permission = get_input("site_permission");
170
    if ($site_permission) {
171
        set_config("site_permission", $site_permission, $site->guid);
172
    }
173
}
174
175
function pleio_public_pages_handler($hook, $type, $value, $params) {
176
    $value[] = "action/pleio/request_access";
177
    $value[] = "access_requested";
178
    return $value;
179
}
180
181
function pleio_user_icon_url_handler($hook, $type, $value, $params) {
182
    $entity = $params["entity"];
183
    $size = $params["size"];
184
185
    if (!$entity) {
186
        return $value;
187
    }
188
189 View Code Duplication
    if (!in_array($size, ["large", "medium", "small", "tiny", "master", "topbar"])) {
190
        $size = "medium";
191
    }
192
193
    $dbprefix = elgg_get_config("dbprefix");
194
    $guid = (int) $entity->guid;
195
196
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
197
    if ($result->pleio_guid) {
198
        $pleio_guid = $result->pleio_guid;
199
    } else {
200
        return $value;
201
    }
202
203
    $auth_url = elgg_get_plugin_setting('auth_url', 'pleio');
204
205
    $url = $auth_url . "mod/profile/icondirect.php?guid={$pleio_guid}&size={$size}";
206
207
    if ($entity->last_login) {
208
        $url .= "&lastcache={$entity->last_login}";
209
    }
210
211
    return $url;
212
}
213
214
function pleio_user_hover_menu($hook, $type, $items, $params) {
215
    foreach ($items as $key => $item) {
216
        if (in_array($item->getName(), ["resetpassword"])) {
217
            unset($items[$key]);
218
        }
219
    }
220
221
    return $items;
222
}
223
224
function pleio_users_settings_save() {
225
    elgg_set_user_default_access();
226
}
227
228
function pleio_is_valid_returnto($url) {
229
    $site_url = parse_url(elgg_get_site_url());
230
    $returnto_url = parse_url($url);
231
232
    if (!$site_url || !$returnto_url) {
233
        return false;
234
    }
235
236
    // check returnto is relative or absolute
237
    if (!$returnto_url["host"] && $returnto_url["path"]) {
238
        return true;
239
    } else {
240
        if ($site_url["scheme"] !== $returnto_url["scheme"]) {
241
            return false;
242
        }
243
244
        if ($site_url["host"] !== $returnto_url["host"]) {
245
            return false;
246
        }
247
    }
248
249
    return true;
250
}
251
252
function get_user_by_pleio_guid_or_email($guid, $email) {
253
    $guid = (int) $guid;
254
    if (!$guid) {
255
        return false;
256
    }
257
258
    $email = sanitize_string($email);
259
    if (!$email) {
260
        return false;
261
    }
262
263
    $dbprefix = elgg_get_config("dbprefix");
264
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE pleio_guid = {$guid}");
265
    if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
266
        return get_entity($result->guid);
267
    }
268
269
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE email = '{$email}'");
270
    if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
271
        update_data("UPDATE {$dbprefix}users_entity SET pleio_guid = {$guid} WHERE guid={$result->guid}");
272
        return get_entity($result->guid);
273
    }
274
275
    return false;
276
}
277
278
function pleio_get_required_profile_fields() {
279
    if (!elgg_is_active_plugin("profile_manager")) {
280
        return [];
281
    }
282
283
    $result = profile_manager_get_categorized_fields(null, true, true, true, $profile_type_guid);
0 ignored issues
show
Bug introduced by
The variable $profile_type_guid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
284
285
    if (empty($result["categories"])) {
286
        return [];
287
    }
288
289
    $return = [];
290
    foreach ($result["categories"] as $category_guid => $category) {
291
        foreach ($result["fields"][$category_guid] as $field) {
292
            if ($field->show_on_register == "yes" && $field->mandatory == "yes") {
293
                $return[] = $field;
294
            }
295
        }
296
    }
297
298
    return $return;
299
}