XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
ModuleInstallUtils.class.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
17require_once XOOPS_LEGACY_PATH . '/admin/class/ModuleInstallInformation.class.php';
18require_once XOOPS_LEGACY_PATH . '/admin/class/ModuleInstaller.class.php';
19require_once XOOPS_LEGACY_PATH . '/admin/class/ModuleUpdater.class.php';
20require_once XOOPS_LEGACY_PATH . '/admin/class/ModuleUninstaller.class.php';
21
22require_once XOOPS_ROOT_PATH . '/class/template.php';
23
24define('MODINSTALL_LOGTYPE_REPORT', 'report');
25define('MODINSTALL_LOGTYPE_WARNING', 'warning');
26define('MODINSTALL_LOGTYPE_ERROR', 'error');
27
32{
33 public $mFetalErrorFlag = false;
34 public $mMessages = [];
35
36 public function add($msg)
37 {
38 $this->mMessages[] = ['type' => MODINSTALL_LOGTYPE_REPORT, 'message' => $msg];
39 }
40
41 public function addReport($msg)
42 {
43 $this->add($msg);
44 }
45
46 public function addWarning($msg)
47 {
48 $this->mMessages[] = ['type' => MODINSTALL_LOGTYPE_WARNING, 'message' => $msg];
49 }
50
51 public function addError($msg)
52 {
53 $this->mMessages[] = ['type' => MODINSTALL_LOGTYPE_ERROR, 'message' => $msg];
54 $this->mFetalErrorFlag = true;
55 }
56
57 public function hasError()
58 {
59 return $this->mFetalErrorFlag;
60 }
61}
62
74{
81 public static function &createInstaller($dirname)
82 {
83 $installer =& Legacy_ModuleInstallUtils::_createInstaller($dirname, 'installer', 'Legacy_ModuleInstaller');
84 return $installer;
85 }
86
93 public static function &createUpdater($dirname)
94 {
95 $updater =& Legacy_ModuleInstallUtils::_createInstaller($dirname, 'updater', 'Legacy_ModulePhasedUpgrader');
96 return $updater;
97 }
98
105 public static function &createUninstaller($dirname)
106 {
107 $uninstaller =& Legacy_ModuleInstallUtils::_createInstaller($dirname, 'uninstaller', 'Legacy_ModuleUninstaller');
108 return $uninstaller;
109 }
110
119 public static function &_createInstaller($dirname, $mode, $defaultClassName)
120 {
121 $info = [];
122
123 $root =& XCube_Root::getSingleton();
124 $root->mLanguageManager->loadModinfoMessageCatalog($dirname);
125 $filepath = XOOPS_MODULE_PATH . "/{$dirname}/xoops_version.php";
126
127 if (file_exists($filepath)) {
128 @include $filepath;
129 $info = $modversion;
130 }
131
132 if (isset($info['legacy_installer']) && is_array($info['legacy_installer']) && isset($info['legacy_installer'][$mode])) {
133 $updateInfo = $info['legacy_installer'][$mode];
134
135 $className = $updateInfo['class'];
136 // @gimaster
137 $filePath = $updateInfo['filepath'] ?? XOOPS_MODULE_PATH . "/{$dirname}/admin/class/{$className}.class.php";
138 $namespace = $updateInfo['namespace'] ?? ucfirst($dirname);
139
140 if (null != $namespace) {
141 $className = "{$namespace}_{$className}";
142 }
143
144 if (!XC_CLASS_EXISTS($className) && file_exists($filePath)) {
145 require_once $filePath;
146 }
147
148 if (XC_CLASS_EXISTS($className)) {
149 $installer =new $className();
150 return $installer;
151 }
152 }
153
154 $installer =new $defaultClassName();
155 return $installer;
156 }
157
169 public static function installSQLAutomatically(&$module, &$log)
170 {
171 $pieces = null;
172 $dbTypeAliases = [
173 'mysqli' => 'mysql'
174 ];
175 $sqlfileInfo =& $module->getInfo('sqlfile');
176 $dirname = $module->getVar('dirname');
177 $dbType = (isset($sqlfileInfo[XOOPS_DB_TYPE]) || !isset($dbTypeAliases[XOOPS_DB_TYPE]))? XOOPS_DB_TYPE : $dbTypeAliases[XOOPS_DB_TYPE];
178
179 if (!isset($sqlfileInfo[$dbType])) {
180 return;
181 }
182
183 $sqlfile = $sqlfileInfo[$dbType];
184 $sqlfilepath = XOOPS_MODULE_PATH . "/{$dirname}/{$sqlfile}";
185
186 if (isset($module->modinfo['cube_style']) && true == $module->modinfo['cube_style']) {
187 require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';
188 $scanner =new Legacy_SQLScanner();
189 $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
190 $scanner->setDirname($module->get('dirname'));
191
192 if (!$scanner->loadFile($sqlfilepath)) {
193 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_SQL_FILE_NOT_FOUND, $sqlfile));
194 return false;
195 }
196
197 $scanner->parse();
198 $sqls = $scanner->getSQL();
199
200 $root =& XCube_Root::getSingleton();
201 $db =& $root->mController->getDB();
202
203 //
204 // TODO The following variable exists for rollback, but it is not implemented.
205 //
206 foreach ($sqls as $sql) {
207 if (!$db->query($sql)) {
208 $log->addError($db->error());
209 return;
210 }
211 }
212
213 $log->addReport(_AD_LEGACY_MESSAGE_DATABASE_SETUP_FINISHED);
214 } else {
215 require_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php';
216
217 $reservedTables = ['avatar', 'avatar_users_link', 'block_module_link', 'xoopscomments', 'config', 'configcategory', 'configoption', 'image', 'imagebody', 'imagecategory', 'imgset', 'imgset_tplset_link', 'imgsetimg', 'groups', 'groups_users_link', 'group_permission', 'online', 'bannerclient', 'banner', 'bannerfinish', 'priv_msgs', 'ranks', 'session', 'smiles', 'users', 'newblocks', 'modules', 'tplfile', 'tplset', 'tplsource', 'xoopsnotifications'];
218
219 $root =& XCube_Root::getSingleton();
220 $db =& $root->mController->mDB;
221
222 $sql_query = fread(fopen($sqlfilepath, 'r'), filesize($sqlfilepath));
223 $sql_query = trim($sql_query);
224 SqlUtility::splitMySqlFile($pieces, $sql_query);
225 $created_tables = [];
226 foreach ($pieces as $piece) {
227 // [0] contains the prefixed query
228 // [4] contains unprefixed table name
229 $prefixed_query = SqlUtility::prefixQuery($piece, $db->prefix());
230 if (!$prefixed_query) {
231 $log->addError("{$piece} is not a valid SQL!");
232 return;
233 }
234
235 // check if the table name is reserved
236 if (!in_array($prefixed_query[4], $reservedTables)) {
237 // not reserved, so try to create one
238 if (!$db->query($prefixed_query[0])) {
239 $log->addError($db->error());
240 return;
241 } else {
242 if (!in_array($prefixed_query[4], $created_tables)) {
243 $log->addReport(' Table ' . $db->prefix($prefixed_query[4]) . ' created.');
244 $created_tables[] = $prefixed_query[4];
245 } else {
246 $log->addReport(' Data inserted to table ' . $db->prefix($prefixed_query[4]));
247 }
248 }
249 } else {
250 // the table name is reserved, so halt the installation
251 $log->addError($prefixed_query[4] . ' is a reserved table!');
252 return;
253 }
254 }
255 }
256 }
257
276 public static function installAllOfModuleTemplates(&$module, &$log)
277 {
278 $templates = $module->getInfo('templates');
279 if (false != $templates) {
280 foreach ($templates as $template) {
281 Legacy_ModuleInstallUtils::installModuleTemplate($module, $template, $log);
282 }
283 }
284 }
285
303 public static function installModuleTemplate($module, $template, &$log)
304 {
305 $tplHandler =& xoops_gethandler('tplfile');
306
307 $fileName = trim($template['file']);
308
309 $tpldata = Legacy_ModuleInstallUtils::readTemplateFile($module->get('dirname'), $fileName);
310 if (false == $tpldata) {
311 return false;
312 }
313
314 //
315 // Create template file object, then store it.
316 //
317 $tplfile =& $tplHandler->create();
318 $tplfile->setVar('tpl_refid', $module->getVar('mid'));
319 $tplfile->setVar('tpl_lastimported', 0);
320 $tplfile->setVar('tpl_lastmodified', time());
321
322 if (preg_match("/\.css$/i", $fileName)) {
323 $tplfile->setVar('tpl_type', 'css');
324 } else {
325 $tplfile->setVar('tpl_type', 'module');
326 }
327
328 $tplfile->setVar('tpl_source', $tpldata, true);
329 $tplfile->setVar('tpl_module', $module->getVar('dirname'));
330 $tplfile->setVar('tpl_tplset', 'default');
331 $tplfile->setVar('tpl_file', $fileName, true);
332
333 $description = $template['description'] ?? '';
334 $tplfile->setVar('tpl_desc', $description, true);
335
336 if ($tplHandler->insert($tplfile)) {
337 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_TEMPLATE_INSTALLED, $fileName));
338 } else {
339 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_INSTALL_TEMPLATE, $fileName));
340 return false;
341 }
342 }
343
364 public static function _uninstallAllOfModuleTemplates(&$module, $tplset, &$log)
365 {
366 //
367 // The following processing depends on the structure of Legacy_RenderSystem.
368 //
369 $tplHandler =& xoops_gethandler('tplfile');
370 $delTemplates = null;
371
372 $delTemplates =& $tplHandler->find($tplset, 'module', $module->get('mid'));
373
374 if (is_array($delTemplates) && count($delTemplates) > 0) {
375 //
376 // clear cache
377 //
378 $xoopsTpl = new XoopsTpl();
379 $xoopsTpl->clear_cache(null, 'mod_' . $module->get('dirname'));
380
381 foreach ($delTemplates as $tpl) {
382 if (!$tplHandler->delete($tpl)) {
383 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_TEMPLATE_UNINSTALLED, $tpl->get('tpl_file')));
384 }
385 }
386 }
387 }
388
389 public static function uninstallAllOfModuleTemplates(&$module, &$log)
390 {
392 }
393
394 public static function clearAllOfModuleTemplatesForUpdate(&$module, &$log)
395 {
397 }
398
411 public static function installAllOfBlocks(&$module, &$log)
412 {
413 $definedBlocks = $module->getInfo('blocks');
414 if (false == $definedBlocks) {
415 return true;
416 }
417
418 $func_num = 0;
419 foreach ($definedBlocks as $block) {
420 $successFlag = true;
421 $updateblocks = [];
422
423 // Try (1) --- func_num
424 foreach ($definedBlocks as $idx => $block) {
425 if (isset($block['func_num'])) {
426 $updateblocks[$idx] = $block;
427 } else {
428 $successFlag = false;
429 break;
430 }
431 }
432
433 // Try (2) --- index pattern
434 if (false == $successFlag) {
435 $successFlag = true;
436 $updateblocks = [];
437 foreach ($definedBlocks as $idx => $block) {
438 if (is_int($idx)) {
439 $block['func_num'] = $idx;
440 $updateblocks[$idx] = $block;
441 } else {
442 $successFlag = false;
443 break;
444 }
445 }
446 }
447
448 // Try (3) --- automatic
449 if (false == $successFlag) {
450 $successFlag = true;
451 $updateblocks = [];
452
453 $func_num = 0;
454 foreach ($definedBlocks as $block) {
455 $block['func_num'] = $func_num;
456 $updateblocks[] = $block;
457 }
458 }
459 }
460
461 foreach ($updateblocks as $block) {
462 $newBlock =& Legacy_ModuleInstallUtils::createBlockByInfo($module, $block, $block['func_num']);
463 Legacy_ModuleInstallUtils::installBlock($module, $newBlock, $block, $log);
464 }
465 }
466
481 public static function uninstallAllOfBlocks(&$module, &$log)
482 {
483 $handler =& xoops_gethandler('block');
484 $criteria = new Criteria('mid', $module->get('mid'));
485
486 $blockArr =& $handler->getObjectsDirectly($criteria);
487
488 $successFlag = true;
489
490 foreach (array_keys($blockArr) as $idx) {
491 $successFlag &= Legacy_ModuleInstallUtils::uninstallBlock($blockArr[$idx], $log);
492 }
493
494 return $successFlag;
495 }
496
504 public static function &createBlockByInfo(&$module, $block, $func_num)
505 {
506 $options = $block['options'] ?? null;
507 $edit_func = $block['edit_func'] ?? null;
508 $template = $block['template'] ?? null;
509 $visible = $block['visible'] ?? $block['visible_any'] ?? 0;
510 $blockHandler =& xoops_gethandler('block');
511 $blockObj =& $blockHandler->create();
512
513 $blockObj->set('mid', $module->getVar('mid'));
514 $blockObj->set('options', $options);
515 $blockObj->set('name', $block['name']);
516 $blockObj->set('title', $block['name']);
517 $blockObj->set('block_type', 'M');
518 $blockObj->set('c_type', 1);
519 $blockObj->set('isactive', 1);
520 $blockObj->set('dirname', $module->getVar('dirname'));
521 $blockObj->set('func_file', $block['file']);
522
523 //
524 // IMPORTANT CONVENTION
525 //
526 $show_func = '';
527 if (isset($block['class'])) {
528 $show_func = 'cl::' . $block['class'];
529 } else {
530 $show_func = $block['show_func'];
531 }
532
533 $blockObj->set('show_func', $show_func);
534 $blockObj->set('edit_func', $edit_func);
535 $blockObj->set('template', $template);
536 $blockObj->set('last_modified', time());
537 $blockObj->set('visible', $visible);
538
539 $func_num = isset($block['func_num']) ? (int)$block['func_num'] : $func_num;
540 $blockObj->set('func_num', $func_num);
541
542 return $blockObj;
543 }
544
553 public static function installBlock(&$module, &$blockObj, &$block, &$log)
554 {
555 $isNew = $blockObj->isNew();
556 $blockHandler =& xoops_gethandler('block');
557
558 if (!empty($block['show_all_module'])) {
559 $autolink = false;
560 } else {
561 $autolink = true;
562 }
563 if (!$blockHandler->insert($blockObj, $autolink)) {
564 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_INSTALL_BLOCK, $blockObj->getVar('name')));
565
566 return false;
567 } else {
568 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_BLOCK_INSTALLED, $blockObj->getVar('name')));
569
570 $tplHandler =& xoops_gethandler('tplfile');
571
572 Legacy_ModuleInstallUtils::installBlockTemplate($blockObj, $module, $log);
573
574 //
575 // Process of a permission.
576 //
577 if ($isNew) {
578 if (!empty($block['show_all_module'])) {
579 $link_sql = 'INSERT INTO ' . $blockHandler->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $blockObj->getVar('bid') . ', 0)';
580 if (!$blockHandler->db->query($link_sql)) {
581 $log->addWarning(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SET_LINK, $blockObj->getVar('name')));
582 }
583 }
584 $gpermHandler =& xoops_gethandler('groupperm');
585 $bperm =& $gpermHandler->create();
586 $bperm->setVar('gperm_itemid', $blockObj->getVar('bid'));
587 $bperm->setVar('gperm_name', 'block_read');
588 $bperm->setVar('gperm_modid', 1);
589
590 if (!empty($block['visible_any'])) {
591 $memberHandler =& xoops_gethandler('member');
592 $groupObjects =& $memberHandler->getGroups();
593 foreach ($groupObjects as $group) {
594 $bperm->setVar('gperm_groupid', $group->getVar('groupid'));
595 $bperm->setNew();
596 if (!$gpermHandler->insert($bperm)) {
597 $log->addWarning(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SET_BLOCK_PERMISSION, $blockObj->getVar('name')));
598 }
599 }
600 } else {
601 $root =& XCube_Root::getSingleton();
602 $groups = $root->mContext->mXoopsUser->getGroups(true);
603 foreach ($groups as $mygroup) {
604 $bperm->setVar('gperm_groupid', $mygroup);
605 $bperm->setNew();
606 if (!$gpermHandler->insert($bperm)) {
607 $log->addWarning(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SET_BLOCK_PERMISSION, $blockObj->getVar('name')));
608 }
609 }
610 }
611 }
612
613 return true;
614 }
615 }
616
627 public static function uninstallBlock(&$block, &$log)
628 {
629 $blockHandler =& xoops_gethandler('block');
630 $blockHandler->delete($block);
631 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_UNINSTALLATION_BLOCK_SUCCESSFUL, $block->get('name')));
632
633 //
634 // Deletes permissions
635 //
636 $gpermHandler =& xoops_gethandler('groupperm');
637 $criteria =new CriteriaCompo();
638 $criteria->add(new Criteria('gperm_name', 'block_read'));
639 $criteria->add(new Criteria('gperm_itemid', $block->get('bid')));
640 $criteria->add(new Criteria('gperm_modid', 1));
641 $gpermHandler->deleteAll($criteria);
642 }
643
652 public static function installBlockTemplate(&$block, &$module, &$log)
653 {
654 if (null == $block->get('template')) {
655 return true;
656 }
657
658 $tplHandler =& xoops_gethandler('tplfile');
659
660 $criteria =new CriteriaCompo();
661 $criteria->add(new Criteria('tpl_type', 'block'));
662 $criteria->add(new Criteria('tpl_tplset', 'default'));
663 $criteria->add(new Criteria('tpl_module', $module->get('dirname')));
664 $criteria->add(new Criteria('tpl_file', $block->get('template')));
665 $tplfiles =& $tplHandler->getObjects($criteria);
666
667 if ((is_countable($tplfiles) ? count($tplfiles) : 0) > 0) {
668 $tplfile =& $tplfiles[0];
669 } else {
670 $tplfile =& $tplHandler->create();
671 $tplfile->set('tpl_refid', $block->get('bid'));
672 $tplfile->set('tpl_tplset', 'default');
673 $tplfile->set('tpl_file', $block->get('template'));
674 $tplfile->set('tpl_module', $module->get('dirname'));
675 $tplfile->set('tpl_type', 'block');
676 // $tplfile->setVar('tpl_desc', $tpl_desc);
677 $tplfile->set('tpl_lastimported', 0);
678 }
679
680 $tplSource = Legacy_ModuleInstallUtils::readTemplateFile($module->get('dirname'), $block->get('template'), true);
681 $tplfile->set('tpl_source', $tplSource);
682 $tplfile->set('tpl_lastmodified', time());
683
684 if ($tplHandler->insert($tplfile)) {
685 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_BLOCK_TEMPLATE_INSTALLED, $block->get('template')));
686 return true;
687 } else {
688 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_BLOCK_TEMPLATE_INSTALL, $block->get('name')));
689 return false;
690 }
691 }
692
702 public static function readTemplateFile($dirname, $fileName, $isblock = false)
703 {
704 //
705 // Load template data
706 //
707 if ($isblock) {
708 $filePath = XOOPS_MODULE_PATH . '/' . $dirname . '/templates/blocks/' . $fileName;
709 } else {
710 $filePath = XOOPS_MODULE_PATH . '/' . $dirname . '/templates/' . $fileName;
711 }
712
713 if (!file_exists($filePath)) {
714 return false;
715 }
716
717 $lines = file($filePath);
718 if (false == $lines) {
719 return false;
720 }
721
722 $tpldata = '';
723 foreach ($lines as $line) {
724 //
725 // Unify linefeed to "\r\n"
726 //
727 $tpldata .= str_replace("\n", "\r\n", str_replace("\r\n", "\n", $line));
728 }
729
730 return $tpldata;
731 }
732
733 public static function installAllOfConfigs(&$module, &$log)
734 {
735 $dirname = $module->get('dirname');
736
737 $fileReader =new Legacy_ModinfoX2FileReader($dirname);
738 $preferences =& $fileReader->loadPreferenceInformations();
739
740 //
741 // Preferences
742 //
743 foreach (array_keys($preferences->mPreferences) as $idx) {
744 Legacy_ModuleInstallUtils::installPreferenceByInfo($preferences->mPreferences[$idx], $module, $log);
745 }
746
747 //
748 // Comments
749 //
750 foreach (array_keys($preferences->mComments) as $idx) {
751 Legacy_ModuleInstallUtils::installPreferenceByInfo($preferences->mComments[$idx], $module, $log);
752 }
753
754 //
755 // Notifications
756 //
757 foreach (array_keys($preferences->mNotifications) as $idx) {
758 Legacy_ModuleInstallUtils::installPreferenceByInfo($preferences->mNotifications[$idx], $module, $log);
759 }
760 }
761
762 public static function installPreferenceByInfo(&$info, &$module, &$log)
763 {
764 $handler =& xoops_gethandler('config');
765 $config =& $handler->createConfig();
766 $config->set('conf_modid', $module->get('mid'));
767 $config->set('conf_catid', 0);
768 $config->set('conf_name', $info->mName);
769 $config->set('conf_title', $info->mTitle);
770 $config->set('conf_desc', $info->mDescription);
771 $config->set('conf_formtype', $info->mFormType);
772 $config->set('conf_valuetype', $info->mValueType);
773 $config->setConfValueForInput($info->mDefault);
774 $config->set('conf_order', $info->mOrder);
775
776 if ((is_countable($info->mOption->mOptions) ? count($info->mOption->mOptions) : 0) > 0) {
777 foreach (array_keys($info->mOption->mOptions) as $idx) {
778 $option =& $handler->createConfigOption();
779 $option->set('confop_name', $info->mOption->mOptions[$idx]->mName);
780 $option->set('confop_value', $info->mOption->mOptions[$idx]->mValue);
781 $config->setConfOptions($option);
782 unset($option);
783 }
784 }
785
786 if ($handler->insertConfig($config)) {
787 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_INSERT_CONFIG, $config->get('conf_name')));
788 } else {
789 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_INSERT_CONFIG, $config->get('conf_name')));
790 }
791 }
792
798 public static function &getConfigInfosFromManifesto(&$module)
799 {
800 $configInfos = $module->getInfo('config');
801
802 //
803 // Insert comment config by old style.
804 //
805 if (0 != $module->getVar('hascomments')) {
806 require_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
807
808 $configInfos[] = [
809 'name' => 'com_rule',
810 'title' => '_CM_COMRULES',
811 'description' => '',
812 'formtype' => 'select',
813 'valuetype' => 'int',
814 'default' => 1,
815 'options' => ['_CM_COMNOCOM' => XOOPS_COMMENT_APPROVENONE, '_CM_COMAPPROVEALL' => XOOPS_COMMENT_APPROVEALL, '_CM_COMAPPROVEUSER' => XOOPS_COMMENT_APPROVEUSER, '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN]
816 ];
817
818 $configInfos[] = [
819 'name' => 'com_anonpost',
820 'title' => '_CM_COMANONPOST',
821 'description' => '',
822 'formtype' => 'yesno',
823 'valuetype' => 'int',
824 'default' => 0
825 ];
826 }
827
828 //
829 // Insert comment config by old style.
830 //
831 if (0 != $module->get('hasnotification')) {
832 require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
833 require_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
834
835 $t_options = [];
836 $t_options['_NOT_CONFIG_DISABLE'] = XOOPS_NOTIFICATION_DISABLE;
837 $t_options['_NOT_CONFIG_ENABLEBLOCK'] = XOOPS_NOTIFICATION_ENABLEBLOCK;
838 $t_options['_NOT_CONFIG_ENABLEINLINE'] = XOOPS_NOTIFICATION_ENABLEINLINE;
839 $t_options['_NOT_CONFIG_ENABLEBOTH'] = XOOPS_NOTIFICATION_ENABLEBOTH;
840
841 $configInfos[] = [
842 'name' => 'notification_enabled',
843 'title' => '_NOT_CONFIG_ENABLE',
844 'description' => '_NOT_CONFIG_ENABLEDSC',
845 'formtype' => 'select',
846 'valuetype' => 'int',
847 'default' => XOOPS_NOTIFICATION_ENABLEBOTH,
848 'options' => $t_options
849 ];
850
851 //
852 // FIXME: doesn't work when update module... can't read back the
853 // array of options properly... " changing to &quot;
854 //
855
856 unset($t_options);
857
858 $t_options = [];
859 $t_categoryArr =& notificationCategoryInfo('', $module->get('mid'));
860 foreach ($t_categoryArr as $t_category) {
861 $t_eventArr =& notificationEvents($t_category['name'], false, $module->get('mid'));
862 foreach ($t_eventArr as $t_event) {
863 if (!empty($t_event['invisible'])) {
864 continue;
865 }
866 $t_optionName = $t_category['title'] . ' : ' . $t_event['title'];
867 $t_options[$t_optionName] = $t_category['name'] . '-' . $t_event['name'];
868 }
869 }
870
871 $configInfos[] = [
872 'name' => 'notification_events',
873 'title' => '_NOT_CONFIG_EVENTS',
874 'description' => '_NOT_CONFIG_EVENTSDSC',
875 'formtype' => 'select_multi',
876 'valuetype' => 'array',
877 'default' => array_values($t_options),
878 'options' => $t_options
879 ];
880 }
881
882 return $configInfos;
883 }
884
891 public static function uninstallAllOfConfigs(&$module, &$log)
892 {
893 if (0 == $module->get('hasconfig')) {
894 return;
895 }
896
897 $configHandler =& xoops_gethandler('config');
898 $configs =& $configHandler->getConfigs(new Criteria('conf_modid', $module->get('mid')));
899
900 if (0 == (is_countable($configs) ? count($configs) : 0)) {
901 return;
902 }
903
904 foreach ($configs as $config) {
905 $configHandler->deleteConfig($config);
906 }
907 }
908
909 public static function smartUpdateAllOfBlocks(&$module, &$log)
910 {
911 $dirname = $module->get('dirname');
912
913 $fileReader =new Legacy_ModinfoX2FileReader($dirname);
914 $latestBlocks =& $fileReader->loadBlockInformations();
915
916 $dbReader =new Legacy_ModinfoX2DBReader($dirname);
917 $currentBlocks =& $dbReader->loadBlockInformations();
918
919 $currentBlocks->update($latestBlocks);
920
921 foreach (array_keys($currentBlocks->mBlocks) as $idx) {
922 switch ($currentBlocks->mBlocks[$idx]->mStatus) {
923 case LEGACY_INSTALLINFO_STATUS_LOADED:
924 Legacy_ModuleInstallUtils::updateBlockTemplateByInfo($currentBlocks->mBlocks[$idx], $module, $log);
925 break;
926
927 case LEGACY_INSTALLINFO_STATUS_UPDATED:
928 Legacy_ModuleInstallUtils::updateBlockByInfo($currentBlocks->mBlocks[$idx], $module, $log);
929 break;
930
931 case LEGACY_INSTALLINFO_STATUS_NEW:
932 Legacy_ModuleInstallUtils::installBlockByInfo($currentBlocks->mBlocks[$idx], $module, $log);
933 break;
934
935 case LEGACY_INSTALLINFO_STATUS_DELETED:
936 Legacy_ModuleInstallUtils::uninstallBlockByFuncNum($currentBlocks->mBlocks[$idx]->mFuncNum, $module, $log);
937 break;
938 }
939 }
940 }
941
942 public static function smartUpdateAllOfPreferences(&$module, &$log)
943 {
944 $dirname = $module->get('dirname');
945
946 $fileReader =new Legacy_ModinfoX2FileReader($dirname);
947 $latestPreferences =& $fileReader->loadPreferenceInformations();
948
949 $dbReader =new Legacy_ModinfoX2DBReader($dirname);
950 $currentPreferences =& $dbReader->loadPreferenceInformations();
951
952 $currentPreferences->update($latestPreferences);
953
954 //
955 // Preferences
956 //
957 foreach (array_keys($currentPreferences->mPreferences) as $idx) {
958 switch ($currentPreferences->mPreferences[$idx]->mStatus) {
959 case LEGACY_INSTALLINFO_STATUS_UPDATED:
960 Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mPreferences[$idx], $module, $log);
961 break;
962
963 case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
964 Legacy_ModuleInstallUtils::updatePreferenceOrderByInfo($currentPreferences->mPreferences[$idx], $module, $log);
965 break;
966
967 case LEGACY_INSTALLINFO_STATUS_NEW:
968 Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mPreferences[$idx], $module, $log);
969 break;
970
971 case LEGACY_INSTALLINFO_STATUS_DELETED:
972 Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mPreferences[$idx]->mOrder, $module, $log);
973 break;
974 }
975 }
976
977 //
978 // Comments
979 //
980 foreach (array_keys($currentPreferences->mComments) as $idx) {
981 switch ($currentPreferences->mComments[$idx]->mStatus) {
982 case LEGACY_INSTALLINFO_STATUS_UPDATED:
983 Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mComments[$idx], $module, $log);
984 break;
985
986 case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
987 Legacy_ModuleInstallUtils::updatePreferenceOrderByInfo($currentPreferences->mComments[$idx], $module, $log);
988 break;
989
990 case LEGACY_INSTALLINFO_STATUS_NEW:
991 Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mComments[$idx], $module, $log);
992 break;
993
994 case LEGACY_INSTALLINFO_STATUS_DELETED:
995 Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mComments[$idx]->mOrder, $module, $log);
996 break;
997 }
998 }
999
1000 //
1001 // Notifications
1002 //
1003 foreach (array_keys($currentPreferences->mNotifications) as $idx) {
1004 switch ($currentPreferences->mNotifications[$idx]->mStatus) {
1005 case LEGACY_INSTALLINFO_STATUS_UPDATED:
1006 Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mNotifications[$idx], $module, $log);
1007 break;
1008
1009 case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
1010 Legacy_ModuleInstallUtils::updatePreferenceOrderByInfo($currentPreferences->mNotifications[$idx], $module, $log);
1011 break;
1012
1013 case LEGACY_INSTALLINFO_STATUS_NEW:
1014 Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mNotifications[$idx], $module, $log);
1015 break;
1016
1017 case LEGACY_INSTALLINFO_STATUS_DELETED:
1018 Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mNotifications[$idx]->mOrder, $module, $log);
1019 break;
1020 }
1021 }
1022 }
1023
1024 public static function updateBlockTemplateByInfo(&$info, &$module, &$log)
1025 {
1026 $handler =& xoops_getmodulehandler('newblocks', 'legacy');
1027
1028 $criteria =new CriteriaCompo();
1029 $criteria->add(new Criteria('dirname', $module->get('dirname')));
1030 $criteria->add(new Criteria('func_num', $info->mFuncNum));
1031
1032 $blockArr =& $handler->getObjects($criteria);
1033 foreach (array_keys($blockArr) as $idx) {
1034 Legacy_ModuleInstallUtils::clearBlockTemplateForUpdate($blockArr[$idx], $module, $log);
1035 Legacy_ModuleInstallUtils::installBlockTemplate($blockArr[$idx], $module, $log);
1036 }
1037 }
1038
1039 public static function updateBlockByInfo(&$info, &$module, &$log)
1040 {
1041 $handler =& xoops_getmodulehandler('newblocks', 'legacy');
1042
1043 $criteria =new CriteriaCompo();
1044 $criteria->add(new Criteria('dirname', $module->get('dirname')));
1045 $criteria->add(new Criteria('func_num', $info->mFuncNum));
1046
1047 $blockArr =& $handler->getObjects($criteria);
1048 foreach (array_keys($blockArr) as $idx) {
1049 $blockArr[$idx]->set('options', $info->mOptions);
1050 $blockArr[$idx]->set('name', $info->mName);
1051 $blockArr[$idx]->set('func_file', $info->mFuncFile);
1052 $blockArr[$idx]->set('show_func', $info->mShowFunc);
1053 $blockArr[$idx]->set('edit_func', $info->mEditFunc);
1054 $blockArr[$idx]->set('template', $info->mTemplate);
1055
1056 if ($handler->insert($blockArr[$idx])) {
1057 $log->addReport(XCube_Utils::formatString('Update {0} block successfully.', $blockArr[$idx]->get('name')));
1058 } else {
1059 $log->addError(XCube_Utils::formatString('Could not update {0} block.', $blockArr[$idx]->get('name')));
1060 }
1061
1062 Legacy_ModuleInstallUtils::clearBlockTemplateForUpdate($blockArr[$idx], $module, $log);
1063 Legacy_ModuleInstallUtils::installBlockTemplate($blockArr[$idx], $module, $log);
1064 }
1065 }
1066
1067 public static function updatePreferenceByInfo(&$info, &$module, &$log)
1068 {
1069 $handler =& xoops_gethandler('config');
1070
1071 $criteria =new CriteriaCompo();
1072 $criteria->add(new Criteria('conf_modid', $module->get('mid')));
1073 $criteria->add(new Criteria('conf_catid', 0));
1074 $criteria->add(new Criteria('conf_name', $info->mName));
1075
1076 $configArr =& $handler->getConfigs($criteria);
1077
1078 if (!((is_countable($configArr) ? count($configArr) : 0) > 0 && is_object($configArr[0]))) {
1079 $log->addError('Execption Error: Could not find config.');
1080 return;
1081 }
1082
1083 $config =& $configArr[0];
1084
1085 $config->set('conf_title', $info->mTitle);
1086 $config->set('conf_desc', $info->mDescription);
1087
1088 //
1089 // Decide whether it changes values.
1090 //
1091 $oldValueType = $config->get('conf_valuetype');
1092 if ($config->get('conf_formtype') != $info->mFormType && $oldValueType != $info->mValueType) {
1093 $config->set('conf_formtype', $info->mFormType);
1094 $config->set('conf_valuetype', $info->mValueType);
1095 $config->setConfValueForInput($info->mDefault);
1096 } else {
1097 $updateValue = null;
1098 if ($oldValueType != $info->mValueType) {
1099 if ('array' === $oldValueType || 'array' === $info->mValueType) {
1100 $updateValue = $info->mDefault;
1101 } else {
1102 $updateValue = $config->getConfValueForOutput();
1103 }
1104 }
1105 $config->set('conf_formtype', $info->mFormType);
1106 $config->set('conf_valuetype', $info->mValueType);
1107 if (null !== $updateValue) {
1108 $config->setConfValueForInput($updateValue);
1109 }
1110 }
1111
1112 $config->set('conf_order', $info->mOrder);
1113
1114 $optionArr =& $handler->getConfigOptions(new Criteria('conf_id', $config->get('conf_id')));
1115 if (is_array($optionArr)) {
1116 foreach (array_keys($optionArr) as $idx) {
1117 $handler->_oHandler->delete($optionArr[$idx]);
1118 }
1119 }
1120
1121 if ((is_countable($info->mOption->mOptions) ? count($info->mOption->mOptions) : 0) > 0) {
1122 foreach (array_keys($info->mOption->mOptions) as $idx) {
1123 $option =& $handler->createConfigOption();
1124 $option->set('confop_name', $info->mOption->mOptions[$idx]->mName);
1125 $option->set('confop_value', $info->mOption->mOptions[$idx]->mValue);
1126 $option->set('conf_id', $option->get('conf_id'));
1127 $config->setConfOptions($option);
1128 unset($option);
1129 }
1130 }
1131
1132 if ($handler->insertConfig($config)) {
1133 $log->addReport(XCube_Utils::formatString("Preference '{0}' is updateded.", $config->get('conf_name')));
1134 } else {
1135 $log->addError(XCube_Utils::formatString("Could not update preference '{0}'.", $config->get('conf_name')));
1136 }
1137 }
1138
1139 public static function updatePreferenceOrderByInfo(&$info, &$module, &$log)
1140 {
1141 $handler =& xoops_gethandler('config');
1142
1143 $criteria =new CriteriaCompo();
1144 $criteria->add(new Criteria('conf_modid', $module->get('mid')));
1145 $criteria->add(new Criteria('conf_catid', 0));
1146 $criteria->add(new Criteria('conf_name', $info->mName));
1147
1148 $configArr =& $handler->getConfigs($criteria);
1149
1150 if (!((is_countable($configArr) ? count($configArr) : 0) > 0 && is_object($configArr[0]))) {
1151 $log->addError('Execption Error: Could not find config.');
1152 return;
1153 }
1154
1155 $config =& $configArr[0];
1156
1157 $config->set('conf_order', $info->mOrder);
1158
1159 if (!$handler->insertConfig($config)) {
1160 $log->addError(XCube_Utils::formatString("Could not update the order of preference '{0}'.", $config->get('conf_name')));
1161 }
1162 }
1163
1164 public static function installBlockByInfo(&$info, &$module, &$log)
1165 {
1166 $handler =& xoops_gethandler('block');
1167 $block =& $handler->create();
1168
1169 $block->set('mid', $module->get('mid'));
1170 $block->set('func_num', $info->mFuncNum);
1171 $block->set('options', $info->mOptions);
1172 $block->set('name', $info->mName);
1173 $block->set('title', $info->mName);
1174 $block->set('dirname', $module->get('dirname'));
1175 $block->set('func_file', $info->mFuncFile);
1176 $block->set('show_func', $info->mShowFunc);
1177 $block->set('edit_func', $info->mEditFunc);
1178 $block->set('template', $info->mTemplate);
1179 $block->set('block_type', 'M');
1180 $block->set('c_type', 1);
1181
1182 if (!$handler->insert($block)) {
1183 $log->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_INSTALL_BLOCK, $block->get('name')));
1184 return false;
1185 } else {
1186 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_BLOCK_INSTALLED, $block->get('name')));
1187
1189
1190 return true;
1191 }
1192 }
1193
1200 public static function uninstallBlockByFuncNum($func_num, &$module, &$log)
1201 {
1202 $handler =& xoops_getmodulehandler('newblocks', 'legacy');
1203
1204 $criteria =new CriteriaCompo();
1205 $criteria->add(new Criteria('dirname', $module->get('dirname')));
1206 $criteria->add(new Criteria('func_num', $func_num));
1207
1208 $blockArr =& $handler->getObjects($criteria);
1209 foreach (array_keys($blockArr) as $idx) {
1210 if ($handler->delete($blockArr[$idx])) {
1211 $log->addReport(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_UNINSTALLATION_BLOCK_SUCCESSFUL, $blockArr[$idx]->get('name')));
1212 } else {
1213 // Uninstall fail
1214 }
1215
1216 Legacy_ModuleInstallUtils::uninstallBlockTemplate($blockArr[$idx], $module, $log);
1217 }
1218 }
1219
1232 public static function _uninstallBlockTemplate(&$block, &$module, $tplset, &$log)
1233 {
1234 $handler =& xoops_gethandler('tplfile');
1235 $criteria =new CriteriaCompo();
1236 $criteria->add(new Criteria('tpl_refid', $block->get('bid')));
1237 $criteria->add(new Criteria('tpl_file', $block->get('template')));
1238 $criteria->add(new Criteria('tpl_module', $module->get('dirname')));
1239 $criteria->add(new Criteria('tpl_type', 'block'));
1240
1241 if (null != $tplset) {
1242 // See 'FIXME'
1243 $criteria->add(new Criteria('tpl_tplset', $tplset));
1244 }
1245
1246 $handler->deleteAll($criteria);
1247 }
1248
1249 public static function uninstallBlockTemplate(&$block, &$module, &$log)
1250 {
1251 Legacy_ModuleInstallUtils::_uninstallBlockTemplate($block, $module, null, $log);
1252 }
1253
1261 public static function clearBlockTemplateForUpdate(&$block, &$module, &$log)
1262 {
1263 Legacy_ModuleInstallUtils::_uninstallBlockTemplate($block, $module, 'default', $log);
1264 }
1265
1266 public static function uninstallPreferenceByOrder($order, &$module, &$log)
1267 {
1268 $handler =& xoops_gethandler('config');
1269
1270 $criteria =new CriteriaCompo();
1271 $criteria->add(new Criteria('conf_modid', $module->get('mid')));
1272 $criteria->add(new Criteria('conf_catid', 0));
1273 $criteria->add(new Criteria('conf_order', $order));
1274
1275 $configArr =& $handler->getConfigs($criteria);
1276
1277 foreach (array_keys($configArr) as $idx) {
1278 if ($handler->deleteConfig($configArr[$idx])) {
1279 $log->addReport(XCube_Utils::formatString("Delete preference '{0}'.", $configArr[$idx]->get('conf_name')));
1280 } else {
1281 $log->addError(XCube_Utils::formatString("Could not delete preference '{0}'.", $configArr[$idx]->get('conf_name')));
1282 }
1283 }
1284 }
1285
1293 public static function DBquery($query, &$module, $log)
1294 {
1295 require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';
1296
1297 $successFlag = true;
1298
1299 $scanner =new Legacy_SQLScanner();
1300 $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
1301 $scanner->setDirname($module->get('dirname'));
1302 $scanner->setBuffer($query);
1303 $scanner->parse();
1304 $sqlArr = $scanner->getSQL();
1305
1306 $root =& XCube_Root::getSingleton();
1307
1308 foreach ($sqlArr as $sql) {
1309 if ($root->mController->mDB->query($sql)) {
1310 $log->addReport("Success: {$sql}");
1311 $successFlag &= true;
1312 } else {
1313 $log->addError("Failure: {$sql}");
1314 $successFlag = false;
1315 }
1316 }
1317
1318 return $successFlag;
1319 }
1320
1321 public static function deleteAllOfNotifications(&$module, &$log)
1322 {
1323 $handler =& xoops_gethandler('notification');
1324 $criteria =new Criteria('not_modid', $module->get('mid'));
1325 $handler->deleteAll($criteria);
1326 }
1327
1328 public static function deleteAllOfComments(&$module, &$log)
1329 {
1330 $handler =& xoops_gethandler('comment');
1331 $criteria =new Criteria('com_modid', $module->get('mid'));
1332 $handler->deleteAll($criteria);
1333 }
1334}
static uninstallAllOfConfigs(&$module, &$log)
static readTemplateFile($dirname, $fileName, $isblock=false)
static installAllOfModuleTemplates(&$module, &$log)
static uninstallBlock(&$block, &$log)
static _uninstallBlockTemplate(&$block, &$module, $tplset, &$log)
static installModuleTemplate($module, $template, &$log)
static installBlockTemplate(&$block, &$module, &$log)
static uninstallAllOfBlocks(&$module, &$log)
static installBlock(&$module, &$blockObj, &$block, &$log)
static installSQLAutomatically(&$module, &$log)
static & _createInstaller($dirname, $mode, $defaultClassName)
static DBquery($query, &$module, $log)
static _uninstallAllOfModuleTemplates(&$module, $tplset, &$log)
static & getConfigInfosFromManifesto(&$module)
static uninstallBlockByFuncNum($func_num, &$module, &$log)
static installAllOfBlocks(&$module, &$log)
static & createBlockByInfo(&$module, $block, $func_num)
static clearBlockTemplateForUpdate(&$block, &$module, &$log)
static formatString()
[Static] Formats string with special care for international.