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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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')) { |
|
|
|
|
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
|
|
|
|
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.