/
oninstall.php
177 lines (158 loc) · 6.23 KB
/
oninstall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
eval('function xoops_module_install_'.$mydirname.'($module){return xsns_oninstall($module, "'.$mydirname.'");}');
if( !function_exists('xsns_oninstall') ) {
function xsns_oninstall($module, $mydirname)
{
global $ret;
if( defined( 'XOOPS_CUBE_LEGACY' ) ) {
$root =& XCube_Root::getSingleton();
$root->mDelegateManager->add( 'Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Success' , 'xsns_message_append_oninstall' ) ;
$root->mDelegateManager->add( 'Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Fail' , 'xsns_message_append_oninstall' ) ;
$ret = array() ;
}
else{
if( !is_array($ret) ){
$ret = array() ;
}
}
$constpref = '_MI_'.strtoupper($mydirname);
if(strlen($mydirname) > 15){
$ret[] = constant($constpref.'_INSTERR').'<br />';
}
$db = XoopsDatabaseFactory::getDatabaseConnection() ;
$mid = $module->getVar('mid') ;
// Tables
$get_server_info = (is_object($db->conn) && get_class($db->conn) === 'mysqli')? 'mysqli_get_server_info' : 'mysql_get_server_info';
$sql_ver = floatval(substr($get_server_info($db->conn), 0, 3));
if($sql_ver < 4){
$sql_file = 'mysql3.sql';
}
elseif($sql_ver == 4.0){
$sql_file = 'mysql40.sql';
}
else{
$sql_file = 'mysql.sql';
}
$sql_file_path = realpath(dirname(__FILE__).'/sql/'.$sql_file);
$prefix_mod = $db->prefix() . '_' . $mydirname ;
if( file_exists( $sql_file_path ) ) {
$ret[] = "SQL file found at <b>".htmlspecialchars($sql_file_path)."</b>.<br /> Creating tables...";
if( file_exists( XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ) ) {
include_once XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ;
$sqlutil = new OldSqlUtility ;
} else {
include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php' ;
$sqlutil = new SqlUtility ;
}
$sql_query = trim( file_get_contents( $sql_file_path ) ) ;
$sqlutil->splitMySqlFile( $pieces , $sql_query ) ;
$created_tables = array() ;
if( is_array( $pieces ) ) {
foreach( $pieces as $piece ) {
$prefixed_query = $sqlutil->prefixQuery( $piece , $prefix_mod ) ;
if( ! $prefixed_query ) {
$ret[] = "Invalid SQL <b>".htmlspecialchars($piece)."</b><br />";
return false ;
}
if( ! $db->query( $prefixed_query[0] ) ) {
$ret[] = '<b>'.htmlspecialchars( $db->error() ).'</b><br />' ;
return false ;
}
else {
if( ! in_array( $prefixed_query[4] , $created_tables ) ) {
$ret[] = 'Table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b> created.<br />'; $created_tables[] = $prefixed_query[4];
}
else {
$ret[] = 'Data inserted to table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b>.</br />';
}
}
}
}
}
// Templates
$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
$tpl_path = dirname(__FILE__).'/templates' ;
if( $handler = @opendir( $tpl_path . '/' ) ) {
while( ( $file = readdir( $handler ) ) !== false ) {
if( substr( $file , 0 , 1 ) == '.' || !preg_match('/(\.html$)|(\.css$)/i', $file)){
continue ;
}
$file_path = $tpl_path . '/' . $file ;
if( is_file( $file_path ) ) {
$mtime = intval( @filemtime( $file_path ) ) ;
$tplfile =& $tplfile_handler->create() ;
$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
$tplfile->setVar( 'tpl_refid' , $mid ) ;
$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
$tplfile->setVar( 'tpl_file' , $mydirname . '_' . $file ) ;
$tplfile->setVar( 'tpl_desc' , '' , true ) ;
$tplfile->setVar( 'tpl_module' , $mydirname ) ;
$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
$tplfile->setVar( 'tpl_type' , 'module' ) ;
if( ! $tplfile_handler->insert( $tplfile ) ) {
$ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> to the database.</span><br />';
} else {
$tplid = $tplfile->getVar( 'tpl_id' ) ;
$ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
// generate compiled file
include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
include_once XOOPS_ROOT_PATH.'/class/template.php' ;
if( ! xoops_template_touch( $tplid ) ) {
$ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b>.</span><br />';
} else {
$ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> compiled.</span><br />';
}
}
}
}
closedir( $handler ) ;
}
include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
include_once XOOPS_ROOT_PATH.'/class/template.php' ;
xoops_template_clear_module_cache( $mid ) ;
// Set default categories
$ini_category_list = array(
// 小カテゴリ名, 表示順, 中カテゴリID
array(constant($constpref.'_CATEGORY_1'), 1, 1),
array(constant($constpref.'_CATEGORY_2'), 2, 1),
array(constant($constpref.'_CATEGORY_3'), 3, 1),
array(constant($constpref.'_CATEGORY_4'), 50, 1),
);
$sql_values = array();
$selector_arr = array();
$id = 1;
foreach($ini_category_list as $category){
$values = array();
foreach($category as $v){
$values[] = "'".$v."'";
}
$sql_values[] = "(".implode(',', $values).")";
$selector_arr[] = "<a href=\"".XOOPS_URL."/modules/".$mydirname."/?cat_id=".($id++)."\">".$category[0]."<nobr><small>(0)</small></nobr></a>";
}
if(count($sql_values) > 0 || count($selector_arr) > 0){
$sql = "INSERT INTO ".$db->prefix($mydirname.'_c_commu_category').
" (name,sort_order,c_commu_category_parent_id) VALUES ".
implode(",", $sql_values);
if($db->query($sql)){
$sql = "INSERT INTO ".$db->prefix($mydirname.'_c_commu_category_parent').
" (name,sort_order,selector) VALUES".
" ('".constant($constpref.'_CATEGORY')."', '1', '".implode(" - ", $selector_arr)."')";
return $db->query($sql);
}
else{
return false;
}
}
return true;
}
function xsns_message_append_oninstall( &$module_obj , &$log )
{
if( is_array( @$GLOBALS['ret'] ) ) {
foreach( $GLOBALS['ret'] as $message ) {
$log->add( strip_tags( $message ) ) ;
}
}
}
}
?>