Completed
Push — add-pleio-mod ( 6d68a5 )
by
unknown
28:07
created

start.php ➔ pleio_autoloader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__) . "/lib/background_tasks.php");
3
require_once(dirname(__FILE__) . "/../../vendor/autoload.php");
4
spl_autoload_register("pleio_autoloader");
5
function pleio_autoloader($class) {
6
    $filename = "classes/" . str_replace("\\", "/", $class) . ".php";
7
    if (file_exists(dirname(__FILE__) . "/" . $filename)) {
8
        include($filename);
9
    }
10
}
11
12
elgg_register_event_handler("init", "system", "pleio_init");
13
14
function pleio_init() {
15
    elgg_unregister_page_handler("login");
16
    elgg_register_page_handler("login", "pleio_page_handler");
17
18
    elgg_unregister_action("register");
19
    elgg_unregister_page_handler("register");
20
    elgg_register_page_handler("register", "pleio_register_page_handler");
21
22
    elgg_unregister_action("logout");
23
    elgg_register_action("logout", dirname(__FILE__) . "/actions/logout.php", "public");
24
25
    elgg_unregister_action("avatar/crop");
26
    elgg_unregister_action("avatar/remove");
27
    elgg_register_action("avatar/remove", dirname(__FILE__) . "/actions/avatar/remove.php");
28
29
    elgg_unregister_action("avatar/upload");
30
    elgg_register_action("avatar/upload", dirname(__FILE__) . "/actions/avatar/upload.php");
31
32
    elgg_unregister_action("user/passwordreset");
33
    elgg_unregister_action("user/requestnewpassword");
34
35
    elgg_unregister_action("admin/user/resetpassword");
36
    elgg_unregister_action("admin/user/delete");
37
    elgg_register_action("admin/user/delete", dirname(__FILE__) . "/actions/admin/user/delete.php", "admin");
38
39
    elgg_unregister_action("admin/user/unban");
40
    elgg_register_action("admin/user/unban", dirname(__FILE__) . "/actions/admin/user/unban.php", "admin");
41
42
    elgg_unregister_menu_item("page", "users:unvalidated");
43
    elgg_unregister_menu_item("page", "users:add");
44
    elgg_unregister_action("useradd");
45
46
    elgg_register_plugin_hook_handler("register", "menu:user_hover", "pleio_user_hover_menu");
47
48
    elgg_unregister_plugin_hook_handler("usersettings:save", "user", "users_settings_save");
49
50
    elgg_unregister_action("admin/site/update_advanced");
51
    elgg_register_action("admin/site/update_advanced", dirname(__FILE__) . "/actions/admin/site/update_advanced.php", "admin");
52
53
    elgg_register_page_handler("access_requested", "pleio_access_requested_page_handler");
54
    elgg_register_page_handler("validate_access", "pleio_access_validate_access_page_handler");
55
56
    elgg_register_action("pleio/request_access", dirname(__FILE__) . "/actions/request_access.php", "public");
57
    elgg_register_action("admin/pleio/process_access", dirname(__FILE__) . "/actions/admin/process_access.php", "admin");
58
59
    elgg_register_plugin_hook_handler("public_pages", "walled_garden", "pleio_public_pages_handler");
60
    elgg_register_plugin_hook_handler("action", "admin/site/update_basic", "pleio_admin_update_basic_handler");
61
    elgg_register_plugin_hook_handler("entity:icon:url", "user", "pleio_user_icon_url_handler");
62
63
    elgg_register_admin_menu_item("administer", "access_requests", "users");
64
65
    elgg_register_admin_menu_item("administer", "import", "users");
66
    elgg_register_action("admin/user/import_step1", dirname(__FILE__) . "/actions/admin/user/import_step1.php", "admin");
67
    elgg_register_action("admin/user/import_step2", dirname(__FILE__) . "/actions/admin/user/import_step2.php", "admin");
68
69
    elgg_extend_view("css/elgg", "pleio/css/site");
70
    elgg_extend_view("page/elements/head", "page/elements/topbar/fix");
71
    elgg_extend_view("page/elements/foot", "page/elements/stats");
72
73
    if (elgg_is_active_plugin("web_services")) {
74
        elgg_ws_expose_function(
75
            "pleio.verifyuser",
76
            "pleio_verify_user_creds",
77
            array(
78
                "user" => array('type' => 'string', 'required' => true),
79
                "password" => array('type' => 'string', 'required' => true)
80
            ),
81
            'Verifies user credentials based on email and password (for use with Pleio_account).',
82
            'POST',
83
            false,
84
            false
85
        );
86
87
        function pleio_verify_user_creds($user, $password) {
88
            $user_entity = get_user_by_email($user)[0];
89
90
            if (!$user_entity) {
91
                return json_encode(false);
92
            }
93
94
            $username = $user_entity->username;
95
            $name = $user_entity->name;
96
            $admin = elgg_is_admin_user($user_entity->guid);
97
            $valid = elgg_authenticate($username, $password);
98
99
            $return = array("name" => $name, "valid" => $valid, "admin" => $admin);
100
101
            return $return;
102
        }
103
    }
104
}
105
106
function pleio_page_handler($page) {
107
    include(dirname(__FILE__) . "/pages/login.php");
108
    return true;
109
}
110
111
function pleio_access_requested_page_handler($page) {
0 ignored issues
show
Coding Style introduced by
pleio_access_requested_page_handler uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
112
    $body = elgg_view_layout("walled_garden", [
113
        "content" => elgg_view("pleio/access_requested", [
114
            "resourceOwner" => $_SESSION["pleio_resource_owner"]
115
        ]),
116
        "class" => "elgg-walledgarden-double",
117
        "id" => "elgg-walledgarden-login"
118
    ]);
119
120
    echo elgg_view_page(elgg_echo("pleio:access_requested"), $body, "walled_garden");
121
    return true;
122
}
123
124
function pleio_access_validate_access_page_handler($page) {
125
    include(dirname(__FILE__) . "/pages/validate_access.php");
126
    return true;
127
}
128
129
function pleio_register_page_handler($page) {
130
    forward("/login?method=register");
131
132
    return true;
133
}
134
135
function pleio_admin_update_basic_handler($hook, $type, $value, $params) {
136
    $site = elgg_get_site_entity();
137
138
    $site_permission = get_input("site_permission");
139
    if ($site_permission) {
140
        set_config("site_permission", $site_permission, $site->guid);
141
    }
142
}
143
144
function pleio_public_pages_handler($hook, $type, $value, $params) {
145
    $value[] = "action/pleio/request_access";
146
    $value[] = "validate_access";
147
    $value[] = "access_requested";
148
    return $value;
149
}
150
151
function pleio_user_icon_url_handler($hook, $type, $value, $params) {
152
    global $CONFIG;
153
    
154
    $auth = elgg_get_plugin_setting('auth', 'pleio');
155
    $auth_url = elgg_get_plugin_setting('auth_url', 'pleio', $CONFIG->pleio->url);
156
157
    if ($auth == 'oidc') {
158
        $auth_url = str_replace("openid", "", $auth_url);
159
    }
160
161
    $entity = $params["entity"];
162
    $size = $params["size"];
163
164
    if (!$entity) {
165
        return $value;
166
    }
167
168 View Code Duplication
    if (!in_array($size, ["large", "medium", "small", "tiny", "master", "topbar"])) {
169
        $size = "medium";
170
    }
171
172
    $dbprefix = elgg_get_config("dbprefix");
173
    $guid = (int) $entity->guid;
174
175
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
176
    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...
177
        $pleio_guid = $result->pleio_guid;
178
    } else {
179
        $pleio_guid = 0;
180
    }
181
182
    $url = $auth_url . "mod/profile/icondirect.php?guid={$pleio_guid}&size={$size}";
183
184
    if ($entity->icontime) {
185
        $url .= "&lastcache={$entity->icontime}";
186
    } elseif ($entity->last_login) {
187
        $url .= "&lastcache={$entity->last_login}";
188
    }
189
190
    return $url;
191
}
192
193
function pleio_user_hover_menu($hook, $type, $items, $params) {
194
    foreach ($items as $key => $item) {
195
        if (in_array($item->getName(), ["resetpassword"])) {
196
            unset($items[$key]);
197
        }
198
    }
199
200
    return $items;
201
}
202
203
function pleio_users_settings_save() {
204
    elgg_set_user_default_access();
205
}
206
207
function pleio_is_valid_returnto($url) {
208
    $site_url = parse_url(elgg_get_site_url());
209
    $returnto_url = parse_url($url);
210
211
    if (!$site_url || !$returnto_url) {
212
        return false;
213
    }
214
215
    // check returnto is relative or absolute
216
    if (!$returnto_url["host"] && $returnto_url["path"]) {
217
        return true;
218
    } else {
219
        if ($site_url["scheme"] !== $returnto_url["scheme"]) {
220
            return false;
221
        }
222
223
        if ($site_url["host"] !== $returnto_url["host"]) {
224
            return false;
225
        }
226
    }
227
228
    return true;
229
}
230
231
function get_user_by_pleio_guid_or_email($guid, $email) {
232
    $guid = (int) $guid;
233
    if (!$guid) {
234
        return false;
235
    }
236
237
    $email = sanitize_string($email);
238
    if (!$email) {
239
        return false;
240
    }
241
242
    $dbprefix = elgg_get_config("dbprefix");
243
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE pleio_guid = {$guid}");
244
    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...
245
        return get_entity($result->guid);
246
    }
247
248
    $result = get_data_row("SELECT guid FROM {$dbprefix}users_entity WHERE email = '{$email}'");
249
    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...
250
        update_data("UPDATE {$dbprefix}users_entity SET pleio_guid = {$guid} WHERE guid={$result->guid}");
251
        return get_entity($result->guid);
252
    }
253
254
    return false;
255
}
256
257
function pleio_get_required_profile_fields() {
258
    if (!elgg_is_active_plugin("profile_manager")) {
259
        return [];
260
    }
261
262
    $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...
263
264
    if (empty($result["categories"])) {
265
        return [];
266
    }
267
268
    $return = [];
269
    foreach ($result["categories"] as $category_guid => $category) {
270
        foreach ($result["fields"][$category_guid] as $field) {
271
            if ($field->show_on_register == "yes" && $field->mandatory == "yes") {
272
                $return[] = $field;
273
            }
274
        }
275
    }
276
277
    return $return;
278
}
279
280
function pleio_get_domain_from_email($email) {
281
    return substr(strrchr($email, "@"), 1);
282
}
283
284
function pleio_domain_in_whitelist($domain) {
285
    $plugin_setting = elgg_get_plugin_setting("domain_whitelist", "pleio");
286
    $domains = $plugin_setting ? explode(",", $plugin_setting) : [];
287
288
    $domains = array_map(function($domain) { return trim($domain); }, $domains);
289
290
    if (in_array($domain, $domains)) {
291
        return true;
292
    }
293
294
    return false;
295
}
296
297
function pleio_schedule_in_background($function, $param) {
0 ignored issues
show
Coding Style introduced by
pleio_schedule_in_background uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
298
    $input = base64_encode(json_encode([
299
        "http_host" => $_SERVER["HTTP_HOST"],
300
        "https" => $_SERVER["HTTPS"],
301
        "env" => [
302
            "DB_USER" => getenv("DB_USER"),
303
            "DB_PASS" => getenv("DB_PASS"),
304
            "DB_NAME" => getenv("DB_NAME"),
305
            "DB_HOST" => getenv("DB_HOST"),
306
            "DB_PREFIX" => getenv("DB_PREFIX"),
307
            "DATAROOT" => getenv("DATAROOT"),
308
            "PLEIO_ENV" => getenv("PLEIO_ENV"),
309
            "SMTP_DOMAIN" => getenv("SMTP_DOMAIN"),
310
            "BLOCK_EMAIL" => getenv("BLOCK_EMAIL"),
311
            "MEMCACHE_ENABLED" => getenv("MEMCACHE_ENABLED"),
312
            "MEMCACHE_PREFIX" => getenv("MEMCACHE_PREFIX"),
313
            "MEMCACHE_SERVER_1" => getenv("MEMCACHE_SERVER_1"),
314
            "ELASTIC_INDEX" => getenv("ELASTIC_INDEX"),
315
            "ELASTIC_SERVER_1" => getenv("ELASTIC_SERVER_1")
316
        ],
317
        "function" => $function,
318
        "param" => $param
319
    ]));
320
321
    $script_location = dirname(__FILE__) . "/procedures/run_function.php";
322
323
    if (file_exists("/usr/local/bin/php")) {
324
        $binary = "/usr/local/bin/php";
325
    } else {
326
        $binary = "php";
327
    }
328
329
    if (PHP_OS === "WINNT") {
330
        pclose(popen("start /B {$binary} {$script_location} {$input}", "r"));
331
    } else {
332
        exec("{$binary} {$script_location} {$input} > /tmp/pleio-background.log &");
333
    }
334
}
335