Passed
Push — master ( 6ebac0...a0be99 )
by Michael
19:32
created

get_drupal_path()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 18
c 1
b 1
f 0
nc 9
nop 0
dl 0
loc 25
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/** This file is part of KCFinder project
4
 *
5
 * @desc      CMS integration code: Drupal
6
 * @package   KCFinder
7
 * @version   3.12
8
 * @author    Dany Alejandro Cabrera <[email protected]>
9
 * @copyright 2010-2014 KCFinder Project
10
 * @license   http://opensource.org/licenses/GPL-3.0 GPLv3
11
 * @license   http://opensource.org/licenses/LGPL-3.0 LGPLv3
12
 * @link      http://kcfinder.sunhater.com
13
 */
14
15
// gets a valid drupal_path
16
function get_drupal_path()
17
{
18
    if (!empty($_SERVER['SCRIPT_FILENAME'])) {
19
        $drupal_path = dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))));
20
        if (!file_exists($drupal_path . '/includes/bootstrap.inc')) {
21
            $drupal_path = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
22
            $depth       = 2;
23
            do {
24
                $drupal_path = dirname($drupal_path);
25
                $depth++;
26
            } while (!($bootstrap_file_found = file_exists($drupal_path . '/includes/bootstrap.inc')) && $depth < 10);
27
        }
28
    }
29
30
    if (!isset($bootstrap_file_found) || !$bootstrap_file_found) {
31
        $drupal_path = '../../../../..';
32
        if (!file_exists($drupal_path . '/includes/bootstrap.inc')) {
33
            $drupal_path = '../..';
34
            do {
35
                $drupal_path .= '/..';
36
                $depth       = substr_count($drupal_path, '..');
37
            } while (!($bootstrap_file_found = file_exists($drupal_path . '/includes/bootstrap.inc')) && $depth < 10);
0 ignored issues
show
Unused Code introduced by
The assignment to $bootstrap_file_found is dead and can be removed.
Loading history...
38
        }
39
    }
40
    return $drupal_path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $drupal_path does not seem to be defined for all execution paths leading up to this point.
Loading history...
41
}
42
43
function CheckAuthentication($drupal_path)
44
{
45
    static $authenticated;
46
47
    if (!isset($authenticated)) {
48
        if (!isset($bootstrap_file_found) || $bootstrap_file_found) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bootstrap_file_found seems to never exist and therefore isset should always be false.
Loading history...
49
            $current_cwd = getcwd();
50
            if (!defined('DRUPAL_ROOT')) {
51
                define('DRUPAL_ROOT', $drupal_path);
52
            }
53
54
            // Simulate being in the drupal root folder so we can share the session
55
            chdir(DRUPAL_ROOT);
56
57
            global $base_url;
58
            $base_root = (isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS']) ? 'https' : 'http';
59
            $base_url  = $base_root .= '://' . preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);
60
61
            if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
62
                $base_path = "/$dir";
63
                $base_url  .= $base_path;
64
            }
65
66
            // correct base_url so it points to Drupal root
67
            $pos      = strpos($base_url, '/sites/');
68
            $base_url = substr($base_url, 0, $pos); // drupal root absolute url
69
70
            // If we aren't in a Drupal installation, or if Drupal path hasn't been properly found, die
71
            if (!file_exists(DRUPAL_ROOT . '/includes/bootstrap.inc')) {
72
                die('The CMS integration service for -drupal- requires KCFinder to be properly placed inside your Drupal installation.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
73
            }
74
75
            // bootstrap
76
            require_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
77
            drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
0 ignored issues
show
Bug introduced by
The constant DRUPAL_BOOTSTRAP_FULL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function drupal_bootstrap was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            /** @scrutinizer ignore-call */ 
78
            drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Loading history...
78
79
            // if user has access permission...
80
            if (user_access('access kcfinder')) {
0 ignored issues
show
Bug introduced by
The function user_access was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            if (/** @scrutinizer ignore-call */ user_access('access kcfinder')) {
Loading history...
81
                if (!isset($_SESSION['KCFINDER'])) {
82
                    $_SESSION['KCFINDER']             = [];
83
                    $_SESSION['KCFINDER']['disabled'] = false;
84
                }
85
86
                // User has permission, so make sure KCFinder is not disabled!
87
                if (!isset($_SESSION['KCFINDER']['disabled'])) {
88
                    $_SESSION['KCFINDER']['disabled'] = false;
89
                }
90
91
                global $user;
92
                $_SESSION['KCFINDER']['uploadURL'] = strtr(variable_get('kcfinder_upload_url', 'sites/default/files/kcfinder'), ['%u' => $user->uid, '%n' => $user->name]);
0 ignored issues
show
Bug introduced by
The function variable_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
                $_SESSION['KCFINDER']['uploadURL'] = strtr(/** @scrutinizer ignore-call */ variable_get('kcfinder_upload_url', 'sites/default/files/kcfinder'), ['%u' => $user->uid, '%n' => $user->name]);
Loading history...
93
                $_SESSION['KCFINDER']['uploadDir'] = strtr(variable_get('kcfinder_upload_dir', ''), ['%u' => $user->uid, '%n' => $user->name]);
94
                $_SESSION['KCFINDER']['theme']     = variable_get('kcfinder_theme', 'default');
95
96
                //echo '<br />uploadURL: ' . $_SESSION['KCFINDER']['uploadURL']<br />;
97
                //echo '<br />uploadDir: ' . $_SESSION['KCFINDER']['uploadDir']<br />;
98
99
                chdir($current_cwd);
100
101
                return true;
102
            }
103
104
            chdir($current_cwd);
105
            return false;
106
        }
107
    }
108
}
109
110
CheckAuthentication(get_drupal_path());
111