Completed
Push — master ( 3a6ebc...3a5a05 )
by Michael
04:13
created

tpls_functions.php ➔ tplsadmin_get_basefilepath()   D

Complexity

Conditions 13
Paths 138

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 13
eloc 17
c 5
b 1
f 0
nc 138
nop 3
dl 0
loc 32
rs 4.8178

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
include_once XOOPS_ROOT_PATH.'/class/template.php';
4
include_once __DIR__.'/altsys_functions.php' ;
5
6
function tplsadmin_import_data($tplset, $tpl_file, $tpl_source, $lastmodified = 0)
7
{
8
    $db = XoopsDatabaseFactory::getDatabaseConnection() ;
9
10
    // check the file is valid template
11
    list($count) = $db->fetchRow($db->query('SELECT COUNT(*) FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='default' AND tpl_file='" . addslashes($tpl_file) . "'")) ;
12
    if (! $count) {
13
        return false ;
14
    }
15
16
    // check the template exists in the tplset
17
    if ($tplset != 'default') {
18
        list($count) = $db->fetchRow($db->query('SELECT COUNT(*) FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='" . addslashes($tplset) . "' AND tpl_file='" . addslashes($tpl_file) . "'")) ;
19
        if ($count <= 0) {
20
            // copy from 'default' to the tplset
21
            $result = $db->query('SELECT * FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='default' AND tpl_file='" . addslashes($tpl_file) . "'") ;
22 View Code Duplication
            while ($row = $db->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
                $db->queryF('INSERT INTO ' . $db->prefix('tplfile') . " SET tpl_refid='" . addslashes($row['tpl_refid']) . "',tpl_module='" . addslashes($row['tpl_module']) . "',tpl_tplset='" . addslashes($tplset) . "',tpl_file='" . addslashes($tpl_file) . "',tpl_desc='" . addslashes($row['tpl_desc']) . "',tpl_type='" . addslashes($row['tpl_type']) . "'") ;
24
                $tpl_id = $db->getInsertId() ;
25
                $db->queryF('INSERT INTO ' . $db->prefix('tplsource') . " SET tpl_id='$tpl_id', tpl_source=''") ;
26
            }
27
        }
28
    }
29
30
    // UPDATE just tpl_lastmodified and tpl_source
31
    $drs = $db->query('SELECT tpl_id FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='" . addslashes($tplset) . "' AND tpl_file='" . addslashes($tpl_file) . "'") ;
32 View Code Duplication
    while (list($tpl_id) = $db->fetchRow($drs)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
        $db->queryF('UPDATE ' . $db->prefix('tplfile') . " SET tpl_lastmodified='" . addslashes($lastmodified) . "',tpl_lastimported=UNIX_TIMESTAMP() WHERE tpl_id='$tpl_id'") ;
34
        $db->queryF('UPDATE ' . $db->prefix('tplsource') . " SET tpl_source='" . addslashes($tpl_source) . "' WHERE tpl_id='$tpl_id'") ;
35
        altsys_template_touch($tpl_id) ;
36
    }
37
38
    return true ;
39
}
40
41
42
43
44
function tplsadmin_get_fingerprint($lines)
45
{
46
    $str = '' ;
47
    foreach ($lines as $line) {
48
        if (trim($line)) {
49
            $str .= md5(trim($line)) ;
50
        }
51
    }
52
    return md5($str) ;
53
}
54
55
56
57
function tplsadmin_copy_templates_db2db($tplset_from, $tplset_to, $whr_append = '1')
58
{
59
    global $db ;
60
61
    // get tplfile and tplsource
62
    $result = $db->query("SELECT tpl_refid,tpl_module,'".addslashes($tplset_to)."',tpl_file,tpl_desc,tpl_lastmodified,tpl_lastimported,tpl_type,tpl_source FROM ".$db->prefix('tplfile') . ' NATURAL LEFT JOIN '
63
                         . $db->prefix('tplsource') . " WHERE tpl_tplset='" . addslashes($tplset_from) . "' AND ($whr_append)") ;
64
65
    while ($row = $db->fetchArray($result)) {
66
        $tpl_source = array_pop($row) ;
67
68
        $drs = $db->query('SELECT tpl_id FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='" . addslashes($tplset_to) . "' AND ($whr_append) AND tpl_file='" . addslashes($row['tpl_file']) . "' AND tpl_refid='" . addslashes($row['tpl_refid']) . "'") ;
69
70
        if (! $db->getRowsNum($drs)) {
71
            // INSERT mode
72
            $sql = 'INSERT INTO ' . $db->prefix('tplfile') . ' (tpl_refid,tpl_module,tpl_tplset,tpl_file,tpl_desc,tpl_lastmodified,tpl_lastimported,tpl_type) VALUES (';
73
            foreach ($row as $colval) {
74
                $sql .= "'".addslashes($colval)."'," ;
75
            }
76
            $db->query(substr($sql, 0, -1) . ')') ;
77
            $tpl_id = $db->getInsertId() ;
78
            $db->query('INSERT INTO ' . $db->prefix('tplsource') . " SET tpl_id='$tpl_id', tpl_source='" . addslashes($tpl_source) . "'") ;
79
            altsys_template_touch($tpl_id) ;
80
        } else {
81 View Code Duplication
            while (list($tpl_id) = $db->fetchRow($drs)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                // UPDATE mode
83
                $db->query('UPDATE ' . $db->prefix('tplfile') . " SET tpl_refid='" . addslashes($row['tpl_refid']) . "',tpl_desc='" . addslashes($row['tpl_desc']) . "',tpl_lastmodified='" . addslashes($row['tpl_lastmodified']) . "',tpl_lastimported='" . addslashes($row['tpl_lastimported']) . "',tpl_type='" . addslashes($row['tpl_type']) . "' WHERE tpl_id='$tpl_id'") ;
84
                $db->query('UPDATE ' . $db->prefix('tplsource') . " SET tpl_source='" . addslashes($tpl_source) . "' WHERE tpl_id='$tpl_id'") ;
85
                altsys_template_touch($tpl_id) ;
86
            }
87
        }
88
    }
89
}
90
91
92
93
function tplsadmin_copy_templates_f2db($tplset_to, $whr_append = '1')
94
{
95
    global $db ;
96
97
    // get tplsource
98
    $result = $db->query('SELECT * FROM ' . $db->prefix('tplfile') . "  WHERE tpl_tplset='default' AND ($whr_append)") ;
99
100
    while ($row = $db->fetchArray($result)) {
101
        $basefilepath = tplsadmin_get_basefilepath($row['tpl_module'], $row['tpl_type'], $row['tpl_file']) ;
102
        $tpl_source = rtrim(implode('', file($basefilepath))) ;
103
        $lastmodified = filemtime($basefilepath) ;
104
105
        $drs = $db->query('SELECT tpl_id FROM ' . $db->prefix('tplfile') . " WHERE tpl_tplset='" . addslashes($tplset_to) . "' AND ($whr_append) AND tpl_file='" . addslashes($row['tpl_file']) . "' AND tpl_refid='" . addslashes($row['tpl_refid']) . "'") ;
106
107
        if (! $db->getRowsNum($drs)) {
108
            // INSERT mode
109
            $sql = 'INSERT INTO ' . $db->prefix('tplfile') . " SET tpl_refid='" . addslashes($row['tpl_refid']) . "',tpl_desc='" . addslashes($row['tpl_desc']) . "',tpl_lastmodified='" . addslashes($lastmodified) . "',tpl_type='" . addslashes($row['tpl_type']) . "',tpl_tplset='" . addslashes($tplset_to) . "',tpl_file='" . addslashes($row['tpl_file']) . "',tpl_module='" . addslashes($row['tpl_module']) . "'" ;
110
            $db->query($sql) ;
111
            $tpl_id = $db->getInsertId() ;
112
            $db->query('INSERT INTO ' . $db->prefix('tplsource') . " SET tpl_id='$tpl_id', tpl_source='" . addslashes($tpl_source) . "'") ;
113
            altsys_template_touch($tpl_id) ;
114
        } else {
115 View Code Duplication
            while (list($tpl_id) = $db->fetchRow($drs)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                // UPDATE mode
117
                $db->query('UPDATE ' . $db->prefix('tplfile') . " SET tpl_lastmodified='" . addslashes($lastmodified) . "' WHERE tpl_id='$tpl_id'") ;
118
                $db->query('UPDATE ' . $db->prefix('tplsource') . " SET tpl_source='" . addslashes($tpl_source) . "' WHERE tpl_id='$tpl_id'") ;
119
                altsys_template_touch($tpl_id) ;
120
            }
121
        }
122
    }
123
}
124
125
126
127
function tplsadmin_get_basefilepath($dirname, $type, $tpl_file)
128
{
129
    // module instance
130
    $path = $basefilepath = XOOPS_ROOT_PATH.'/modules/'.$dirname.'/templates/'.($type=='block'?'blocks/':'').$tpl_file ;
131
132
    if (is_callable('Legacy_Utils::getTrustDirnameByDirname')) {
133
        $mytrustdirname = Legacy_Utils::getTrustDirnameByDirname($dirname);
134
    } elseif (!defined('XOOPS_CUBE_LEGACY')) {
135
        $mytrustdirname = XOOPS_PATH;
136
    }
137
138
    if (defined('ALTSYS_TPLSADMIN_BASEPATH')) {
139
        // Special hook
140
        $path = ALTSYS_TPLSADMIN_BASEPATH.'/'.substr($tpl_file, strlen($dirname) + 1) ;
141
    } elseif ($mytrustdirname || @include(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/mytrustdirname.php')) {
0 ignored issues
show
Bug introduced by
The variable $mytrustdirname does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
142
        // D3 module base
143
        if (! empty($mytrustdirname)) {
144
            $mid_path = $mytrustdirname == 'altsys' ? '/libs/' : '/modules/' ;
145
146
            $path = XOOPS_TRUST_PATH.$mid_path.$mytrustdirname.'/templates/'.($type=='block'?'blocks/':'').substr($tpl_file, strlen($dirname) + 1) ;
147
            //new for xcck etc.other trust_module
148
            if (! file_exists($path)) {
149
                $path = XOOPS_TRUST_PATH.$mid_path.$mytrustdirname.'/templates/'.($type=='block'?'blocks/':'').$tpl_file ;
150
                if (! file_exists($path)) {
151
                    $path = $basefilepath;
152
                }
153
            }
154
        }
155
    }
156
157
    return $path ;
158
}
159
160
161
function tplsadmin_die($msg, $target_dirname = '', $wait = 2)
162
{
163
    if (strtolower($_SERVER['REQUEST_METHOD']) === 'post') {
164
        redirect_header('?mode=admin&lib=altsys&page=mytplsadmin&dirname='.$target_dirname, $wait, $msg) ;
165
        exit ;
166
    } else {
167
        die($msg) ;
168
    }
169
}
170