Completed
Push — Submit-Comment-Mobile-API ( dc14cd )
by
unknown
74:57 queued 54:25
created

start.php ➔ pleio_user_icon_url_handler()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 37
Code Lines 22

Duplication

Lines 3
Ratio 8.11 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 18
nop 4
dl 3
loc 37
rs 8.439
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
17
    elgg_unregister_action("register");
18
    elgg_unregister_page_handler("register");
19
20
    elgg_unregister_action("logout");
21
    elgg_register_action("logout", dirname(__FILE__) . "/actions/logout.php", "public");
22
23
    elgg_unregister_action("avatar/crop");
24
    elgg_unregister_action("avatar/remove");
25
    elgg_unregister_action("avatar/upload");
26
    elgg_unregister_action("user/passwordreset");
27
    elgg_unregister_action("user/requestnewpassword");
28
29
    elgg_unregister_action("admin/user/resetpassword");
30
    elgg_unregister_action("admin/user/delete");
31
    elgg_register_action("admin/user/delete", dirname(__FILE__) . "/actions/admin/user/delete.php", "admin");
32
33
    elgg_unregister_menu_item("page", "users:unvalidated");
34
    elgg_unregister_menu_item("page", "users:add");
35
    elgg_unregister_action("useradd");
36
37
    elgg_register_plugin_hook_handler("register", "menu:user_hover", "pleio_user_hover_menu");
38
39
    elgg_unregister_plugin_hook_handler("usersettings:save", "user", "users_settings_save");
40
41
    elgg_unregister_action("admin/site/update_advanced");
42
    elgg_register_action("admin/site/update_advanced", dirname(__FILE__) . "/actions/admin/site/update_advanced.php", "admin");
43
44
    elgg_register_page_handler("register", "pleio_register_page_handler");
45
    elgg_register_page_handler("access_requested", "pleio_access_requested_page_handler");
46
47
    elgg_register_action("pleio/request_access", dirname(__FILE__) . "/actions/request_access.php", "public");
48
    elgg_register_action("admin/pleio/process_access", dirname(__FILE__) . "/actions/admin/process_access.php", "admin");
49
50
    elgg_register_plugin_hook_handler("public_pages", "walled_garden", "pleio_public_pages_handler");
51
    elgg_register_plugin_hook_handler("action", "admin/site/update_basic", "pleio_admin_update_basic_handler");
52
    elgg_register_plugin_hook_handler("entity:icon:url", "user", "pleio_user_icon_url_handler");
53
54
    elgg_register_admin_menu_item("administer", "access_requests", "users");
55
56
    elgg_register_admin_menu_item("administer", "import", "users");
57
    elgg_register_action("admin/user/import_step1", dirname(__FILE__) . "/actions/admin/user/import_step1.php", "admin");
58
    elgg_register_action("admin/user/import_step2", dirname(__FILE__) . "/actions/admin/user/import_step2.php", "admin");
59
60
    elgg_extend_view("css/elgg", "pleio/css/site");
61
    elgg_extend_view("page/elements/head", "page/elements/topbar/fix");
62
    elgg_extend_view("page/elements/foot", "page/elements/stats");
63
}
64
65
function pleio_page_handler($page) {
66
    include(dirname(__FILE__) . "/pages/login.php");
67
}
68
69
function pleio_access_requested_page_handler($page) {
70
    $body = elgg_view_layout("walled_garden", [
71
        "content" => elgg_view("pleio/access_requested"),
72
        "class" => "elgg-walledgarden-double",
73
        "id" => "elgg-walledgarden-login"
74
    ]);
75
76
    echo elgg_view_page(elgg_echo("pleio:access_requested"), $body, "walled_garden");
77
    return true;
78
}
79
80
function pleio_register_page_handler($page) {
81
    forward("/login");
82
    return true;
83
}
84
85
function pleio_admin_update_basic_handler($hook, $type, $value, $params) {
86
    $site = elgg_get_site_entity();
87
88
    $site_permission = get_input("site_permission");
89
    if ($site_permission) {
90
        set_config("site_permission", $site_permission, $site->guid);
91
    }
92
}
93
94
function pleio_public_pages_handler($hook, $type, $value, $params) {
95
    $value[] = "action/pleio/request_access";
96
    $value[] = "access_requested";
97
    return $value;
98
}
99
100
function pleio_user_icon_url_handler($hook, $type, $value, $params) {
101
    $auth = elgg_get_plugin_setting('auth', 'pleio');
102
    $auth_url = elgg_get_plugin_setting('auth_url', 'pleio');
103
104
    if ($auth == 'oidc') {
105
        $auth_url = str_replace("openid", "", $auth_url);
106
    }
107
108
    $entity = $params["entity"];
109
    $size = $params["size"];
110
111
    if (!$entity) {
112
        return $value;
113
    }
114
115 View Code Duplication
    if (!in_array($size, ["large", "medium", "small", "tiny", "master", "topbar"])) {
116
        $size = "medium";
117
    }
118
119
    $dbprefix = elgg_get_config("dbprefix");
120
    $guid = (int) $entity->guid;
121
122
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
123
    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...
124
        $pleio_guid = $result->pleio_guid;
125
    } else {
126
        $pleio_guid = 0;
127
    }
128
129
    $url = $auth_url . "mod/profile/icondirect.php?guid={$pleio_guid}&size={$size}";
130
131
    if ($entity->last_login) {
132
        $url .= "&lastcache={$entity->last_login}";
133
    }
134
135
    return $url;
136
}
137
138
function pleio_user_hover_menu($hook, $type, $items, $params) {
139
    foreach ($items as $key => $item) {
140
        if (in_array($item->getName(), ["resetpassword"])) {
141
            unset($items[$key]);
142
        }
143
    }
144
145
    return $items;
146
}
147
148
function pleio_users_settings_save() {
149
    elgg_set_user_default_access();
150
}
151
152
function pleio_is_valid_returnto($url) {
153
    $site_url = parse_url(elgg_get_site_url());
154
    $returnto_url = parse_url($url);
155
156
    if (!$site_url || !$returnto_url) {
157
        return false;
158
    }
159
160
    // check returnto is relative or absolute
161
    if (!$returnto_url["host"] && $returnto_url["path"]) {
162
        return true;
163
    } else {
164
        if ($site_url["scheme"] !== $returnto_url["scheme"]) {
165
            return false;
166
        }
167
168
        if ($site_url["host"] !== $returnto_url["host"]) {
169
            return false;
170
        }
171
    }
172
173
    return true;
174
}
175
176
function get_user_by_pleio_guid_or_email($guid, $email) {
177
    $guid = (int) $guid;
178
    if (!$guid) {
179
        return false;
180
    }
181
182
    $email = sanitize_string($email);
183
    if (!$email) {
184
        return false;
185
    }
186
187
    $dbprefix = elgg_get_config("dbprefix");
188
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE pleio_guid = {$guid}");
189
    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...
190
        return get_entity($result->guid);
191
    }
192
193
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE email = '{$email}'");
194
    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...
195
        update_data("UPDATE {$dbprefix}users_entity SET pleio_guid = {$guid} WHERE guid={$result->guid}");
196
        return get_entity($result->guid);
197
    }
198
199
    return false;
200
}
201
202
function pleio_get_required_profile_fields() {
203
    if (!elgg_is_active_plugin("profile_manager")) {
204
        return [];
205
    }
206
207
    $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...
208
209
    if (empty($result["categories"])) {
210
        return [];
211
    }
212
213
    $return = [];
214
    foreach ($result["categories"] as $category_guid => $category) {
215
        foreach ($result["fields"][$category_guid] as $field) {
216
            if ($field->show_on_register == "yes" && $field->mandatory == "yes") {
217
                $return[] = $field;
218
            }
219
        }
220
    }
221
222
    return $return;
223
}
224
225
elgg_ws_expose_function(
226
    "pleio.verifyuser",
227
    "pleio_verify_user_creds",
228
    array(
229
        "user" => array('type' => 'string', 'required' => true),
230
        "password" => array('type' => 'string', 'required' => true)
231
    ),
232
    'Verifies user credentials based on email and password.',
233
    'POST',
234
    false,
235
    false
236
);
237
238
function pleio_verify_user_creds($user, $password) {
239
    $user_entity = get_user_by_email($user)[0];
240
241
    if (!$user_entity) {
242
        return json_encode(false);
243
    }
244
245
    $username = $user_entity->username;
246
    $name = $user_entity->name;
247
    $admin = elgg_is_admin_user($user_entity->guid);
248
    $valid = elgg_authenticate($username, $password);
249
250
    $return = array("name" => $name, "valid" => $valid, "admin" => $admin);
251
252
    return $return;
253
}