Completed
Push — notification_liked_post ( 79541a )
by
unknown
21:19
created

start.php ➔ pleio_invited()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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