1: <?php
2: // start hack by Trabis
3: if (!class_exists('ProtectorRegistry')) {
4: exit('Registry not found');
5: }
6:
7: $registry = ProtectorRegistry::getInstance();
8: $mydirname = $registry->getEntry('mydirname');
9: $mydirpath = $registry->getEntry('mydirpath');
10: $language = $registry->getEntry('language');
11: // end hack by Trabis
12:
13: eval(' function xoops_module_install_' . $mydirname . '( $module ) { return protector_oninstall_base( $module , "' . $mydirname . '" ) ; } ');
14:
15: if (!function_exists('protector_oninstall_base')) {
16:
17: /**
18: * @param $module
19: * @param $mydirname
20: *
21: * @return bool
22: */
23: function protector_oninstall_base($module, $mydirname)
24: {
25: /** @var XoopsModule $module */
26: // translations on module install
27:
28: global $ret; // TODO :-D
29:
30: if (!is_array($ret)) {
31: $ret = array();
32: }
33:
34: /** @var XoopsMySQLDatabase $db */
35: $db = XoopsDatabaseFactory::getDatabaseConnection();
36: $mid = $module->getVar('mid');
37:
38: // TABLES (loading mysql.sql)
39: $sql_file_path = __DIR__ . '/sql/mysql.sql';
40: $prefix_mod = $db->prefix() . '_' . $mydirname;
41: if (file_exists($sql_file_path)) {
42: $ret[] = 'SQL file found at <b>' . htmlspecialchars($sql_file_path, ENT_QUOTES) . '</b>.<br> Creating tables...<br>';
43:
44: include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
45: $sqlutil = new SqlUtility; //old code is -> $sqlutil =& new SqlUtility ; //hack by Trabis
46:
47: $sql_query = trim(file_get_contents($sql_file_path));
48: $sqlutil::splitMySqlFile($pieces, $sql_query);
49: $created_tables = [];
50: foreach ($pieces as $piece) {
51: $prefixed_query = $sqlutil::prefixQuery($piece, $prefix_mod);
52: if (!$prefixed_query) {
53: $ret[] = 'Invalid SQL <b>' . htmlspecialchars($piece, ENT_QUOTES) . '</b><br>';
54:
55: return false;
56: }
57: if (!$db->query($prefixed_query[0])) {
58: $ret[] = '<b>' . htmlspecialchars($db->error(), ENT_QUOTES) . '</b><br>';
59:
60: //var_dump( $db->error() ) ;
61: return false;
62: } else {
63: if (!in_array($prefixed_query[4], $created_tables)) {
64: $ret[] = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b> created.<br>';
65: $created_tables[] = $prefixed_query[4];
66: } else {
67: $ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b>.</br />';
68: }
69: }
70: }
71: }
72:
73: // TEMPLATES
74: /** @var XoopsTplfileHandler $tplfile_handler */
75: $tplfile_handler = xoops_getHandler('tplfile');
76: $tpl_path = __DIR__ . '/templates';
77: // Check if the directory exists
78: if (is_dir($tpl_path) && is_readable($tpl_path)) {
79: // Try to open the directory
80: if ($handler = opendir($tpl_path . '/')) {
81: while (($file = readdir($handler)) !== false) {
82: if (substr($file, 0, 1) === '.') {
83: continue;
84: }
85: $file_path = $tpl_path . '/' . $file;
86: if (is_file($file_path) && in_array(strrchr($file, '.'), array('.html', '.css', '.js'))) {
87: $mtime = (int)(@filemtime($file_path));
88: $tplfile = $tplfile_handler->create();
89: $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
90: $tplfile->setVar('tpl_refid', $mid);
91: $tplfile->setVar('tpl_tplset', 'default');
92: $tplfile->setVar('tpl_file', $mydirname . '_' . $file);
93: $tplfile->setVar('tpl_desc', '', true);
94: $tplfile->setVar('tpl_module', $mydirname);
95: $tplfile->setVar('tpl_lastmodified', $mtime);
96: $tplfile->setVar('tpl_lastimported', 0);
97: $tplfile->setVar('tpl_type', 'module');
98: if (!$tplfile_handler->insert($tplfile)) {
99: $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> to the database.</span><br>';
100: } else {
101: $tplid = $tplfile->getVar('tpl_id');
102: $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>';
103: // generate compiled file
104: include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
105: include_once XOOPS_ROOT_PATH . '/class/template.php';
106: if (!xoops_template_touch((string)$tplid)) {
107: $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b>.</span><br>';
108: } else {
109: $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> compiled.</span><br>';
110: }
111: }
112: }
113: }
114: closedir($handler);
115: } else {
116: // Handle the error condition when opendir fails
117: $ret[] = '<span style="color:#ff0000;">ERROR: Could not open the template directory: <b>' . htmlspecialchars($tpl_path, ENT_QUOTES) . '</b>.</span><br>';
118: }
119: } else {
120: // Directory does not exist; handle this condition
121: $ret[] = '<span style="color:#ff0000;">ERROR: The template directory does not exist or is not readable: <b>' . htmlspecialchars($tpl_path, ENT_QUOTES) . '</b>.</span><br>';
122: }
123: include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
124: include_once XOOPS_ROOT_PATH . '/class/template.php';
125: xoops_template_clear_module_cache($mid);
126:
127: return true;
128: }
129:
130: /**
131: * @param $module_obj
132: * @param $log
133: */
134: function protector_message_append_oninstall(&$module_obj, &$log)
135: {
136: if (isset($GLOBALS['ret']) && is_array($GLOBALS['ret'])) {
137: foreach ($GLOBALS['ret'] as $message) {
138: $log->add(strip_tags($message));
139: }
140: }
141:
142: // use mLog->addWarning() or mLog->addError() if necessary
143: }
144: }
145: