<?php
/**
 * @version $Id: MyuploaderInstallUtils.class.php 679 2012-08-20 12:41:35Z hodaka $
 * @package myuploader
 */

if(!defined('XOOPS_ROOT_PATH'))
{
    exit;
}

if(class_exists('Myuploader_InstallUtils'))
{
    return;
}

/**
 * Myuploader_InstallUtils
**/
class Myuploader_InstallUtils
{
    /**
     * installSQLAutomatically
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installSQLAutomatically(&$module, &$log)
    {
        $sqlFileInfo =& $module->getInfo('sqlfile');
        if(!isset($sqlFileInfo[XOOPS_DB_TYPE]))
        {
            return true;
        }
        $sqlFile = $sqlFileInfo[XOOPS_DB_TYPE];

        $dirname = $module->getVar('dirname');
        $sqlFilePath = sprintf('%s/%s/%s',XOOPS_MODULE_PATH, $dirname, $sqlFile);
        if(!file_exists($sqlFilePath))
        {
            $trustdirname = Myuploader_InstallUtils::_getTrustDirname($module, $log);
            if(!$trustdirname) {
                $log->addError(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_SQL_FILE_NOT_FOUND,
                        $sqlFile
                    )
                );
                return false;
            } else {
                $sqlFilePath = sprintf(
                    '%s/modules/%s/%s',
                    XOOPS_TRUST_PATH,
                    $trustdirname,
                    $sqlFile
                );
            }
        }

        require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';    // TODO will be use other class?
        $scanner = new Legacy_SQLScanner();
        $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
        $scanner->setDirname($dirname);
        if(!$scanner->loadFile($sqlFilePath))
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_SQL_FILE_NOT_FOUND,
                    $sqlFile
                )
            );
            return false;
        }

        $scanner->parse();
        $root = XCube_Root::getSingleton();
        $db =& $root->mController->getDB();

        foreach($scanner->getSQL() as $sql)
        {
            if(!$db->query($sql))
            {
                $log->addError($db->error());
                return false;
            }
        }
        $log->addReport(_MI_MYUPLOADER_INSTALL_MSG_DB_SETUP_FINISHED);
        return true;
    }

    /**
     * DBquery
     *
     * @param   string  $query
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function DBquery($query, &$module, &$log)
    {
        require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';    // TODO will be use other class?
        $scanner = new Legacy_SQLScanner();
        $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
        $scanner->setDirname($module->get('dirname'));
        $scanner->setBuffer($query);
        $scanner->parse();
        $sqls = $scanner->getSQL();

        $root = XCube_Root::getSingleton();

        $successFlag = true;
        foreach($sqls as $sql)
        {
            if($root->mController->mDB->query($sql))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_SQL_SUCCESS,
                        $sql
                    )
                );
            }
            else
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_SQL_ERROR,
                        $sql
                    )
                );
                $successFlag = false;
            }
        }
        return $successFlag;
    }

    /**
     * replaceDirname
     *
     * @param   string  $from
     * @param   string  $dirname
     * @param   string  $trustDirname
     *
     * @return  {string 'public',string 'trust'}
     *
     * @static
    **/
    function replaceDirname($from, $dirname, $trustDirname = null)
    {
        return array(
            'public' => str_replace('{dirname}', $dirname, $from),
            'trust' => ($trustDirname != null) ? str_replace('{dirname}', $trustDirname, $from) : null
        );
    }

    /**
     * readTemplateFile
     *
     * @param   string  $dirname
     * @param   string  $trustDirname
     * @param   string  $filename
     * @param   bool  $isBlock
     *
     * @return  string
     *
     * @static
    **/
    function readTemplateFile($dirname, $trustDirname, $filename, $isBlock=false)
    {
        $filePath = sprintf(
            '%s/%s/templates/%s%s',
            XOOPS_MODULE_PATH,
            $dirname,
            ($isBlock ? 'blocks/' : ''),
            $filename
        );

        if(!file_exists($filePath))
        {
            $filePath = sprintf(
                '%s/modules/%s/templates/%s%s',
                XOOPS_TRUST_PATH,
                $trustDirname,
                ($isBlock ? 'blocks/' : ''),
                $filename
            );
            if(!file_exists($filePath))
            {
                return false;
            }
        }

        if(!($lines = file($filePath)))
        {
            return false;
        }

        $tplData = '';
        foreach($lines as $line)
        {
            $tplData .= str_replace("\n", "\r\n", str_replace("\r\n","\n",$line));
        }

        return $tplData;
    }

    /**
     * installAllOfModuleTemplates
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function installAllOfModuleTemplates(&$module, &$log)
    {
        $templates =& $module->getInfo('templates');
        if(is_array($templates) && count($templates) > 0)
        {
            foreach($templates as $template)
            {
                Myuploader_InstallUtils::installModuleTemplate($module, $template, $log);
            }
        } else {
            Myuploader_InstallUtils::installFlatModuleTemplates($module, $log);
        }
    }

    function installFlatModuleTemplates(&$module, &$log) {
        $tplfile_handler =& Myuploader_Utils::getXoopsHandler('tplfile');
        $tpl_path = dirname(dirname(dirname(dirname(__FILE__)))).'/templates' ;
        if($handler = @opendir($tpl_path.'/')) {
            while(($file = readdir($handler)) !== false ) {
                if(substr($file, 0, 1) == '.') continue ;
                $file_path = $tpl_path.'/'.$file;
                if(is_file($file_path) && in_array(strrchr($file, '.'), array('.xml', '.html', '.css', '.js'))) {
                    $mtime = intval(@filemtime($file_path));
                    $tplfile =& $tplfile_handler->create() ;
                    $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
                    $tplfile->setVar('tpl_refid', $module->getVar('mid'));
                    $tplfile->setVar('tpl_tplset', 'default');
                    $tplfile->setVar('tpl_file', $module->getVar('dirname').'_'.$file);
                    $tplfile->setVar('tpl_desc', '', true);
                    $tplfile->setVar('tpl_module', $module->getVar('dirname'));
                    $tplfile->setVar('tpl_lastmodified', $mtime);
                    $tplfile->setVar('tpl_lastimported', 0);
                    $tplfile->setVar('tpl_type', 'module');
                    if(!$tplfile_handler->insert($tplfile)) {
                        $log->addError(
                            XCube_Utils::formatString(_MI_MYUPLOADER_INSTALL_ERROR_TPL_INSTALLED, $module->get('dirname').'_'.$file)
                        );
                        return false;

                    } else {
                        $log->addReport(
                            XCube_Utils::formatString(_MI_MYUPLOADER_INSTALL_MSG_TPL_INSTALLED, $module->get('dirname').'_'.$file)
                        );

                        // generate compiled file
                        require_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
                        require_once XOOPS_ROOT_PATH.'/class/template.php' ;
                        if(!xoops_template_touch($tplfile->getVar('tpl_id'))) {
                            $log->addError(
                                XCube_Utils::formatString(_MI_MYUPLOADER_INSTALL_ERROR_TPL_COMPILED, $module->getr('dirname').'_'.$file)
                            );
                            return false;
//                   $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b>.</span><br />';
                        } else {
                            $log->addReport(
                                XCube_Utils::formatString(_MI_MYUPLOADER_INSTALL_MSG_TPL_COMPILED, $module->get('dirname').'_'.$file)
                            );
//                        $ret[] = 'Template <strong>'.htmlspecialchars($mydirname.'_'.$file).'</strong> compiled.</span><br />';
                        }
                    }
                }
            }
            closedir($handler);
        }

        require_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
        require_once XOOPS_ROOT_PATH.'/class/template.php' ;
        xoops_template_clear_module_cache($module->get('mid'));
    }

    /**
     * installModuleTemplate
     *
     * @param   XoopsModule  &$module
     * @param   string[]  $template
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installModuleTemplate(&$module, $template, &$log)
    {
        $dirname = $module->getVar('dirname');

        $trustdirname = Myuploader_InstallUtils::_getTrustDirname($module, $log);
        if(!$trustdirname) {
            return false;
        }

//        $trustDirname =& $module->getInfo('trust_dirname');
        $tplHandler =& Myuploader_Utils::getXoopsHandler('tplfile');
        $filename   =  Myuploader_InstallUtils::replaceDirname(trim($template['file']), $dirname, $trustdirname);
        $tplData    =  Myuploader_InstallUtils::readTemplateFile($dirname, $trustdirname, $filename['trust']);

        if($tplData == false)
        {
            return false;
        }

        $tplFile =& $tplHandler->create();
        $tplFile->setVar('tpl_refid'       ,$module->getVar('mid'));
        $tplFile->setVar('tpl_lastimported',0);
        $tplFile->setVar('tpl_lastmodified',time());
        $tplFile->setVar('tpl_type'        ,(substr($filename['trust'],-4) == '.css') ? 'css' : 'module');
        $tplFile->setVar('tpl_source'      ,$tplData,true);
        $tplFile->setVar('tpl_module'      ,$module->getVar('dirname'));
        $tplFile->setVar('tpl_tplset'      ,'default');
        $tplFile->setVar('tpl_file'        ,$filename['public'],true);
        $tplFile->setVar('tpl_desc'        ,isset($template['desctiption']) ? $template['description'] : '',true);

        if($tplHandler->insert($tplFile))
        {
            $log->addReport(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_TPL_INSTALLED,
                    $filename['public']
                )
            );
        }
        else
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_TPL_INSTALLED,
                    $filename['public']
                )
            );
            return false;
        }

        return true;
    }

    /**
     * uninstallAllOfModuleTemplates
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     * @param   bool  $defaultOnly
     *
     * @return  void
     *
     * @static
    **/
    function uninstallAllOfModuleTemplates(&$module, &$log, $defaultOnly = true)
    {
        $tplHandler   =& Myuploader_Utils::getXoopsHandler('tplfile');

        $delTemplates =& $tplHandler->find($defaultOnly ? 'default' : null, 'module', $module->get('mid'));

        if(is_array($delTemplates) && count($delTemplates) > 0)
        {
            $xoopsTpl = new XoopsTpl();
            $xoopsTpl->clear_cache(null,'mod_' . $module->get('dirname'));
            foreach($delTemplates as $tpl)
            {
                if(!$tplHandler->delete($tpl))
                {
                    $log->addError(
                        XCube_Utils::formatString(
                            _MI_MYUPLOADER_INSTALL_ERROR_TPL_UNINSTALLED,
                            $tpl->get('tpl_file')
                        )
                    );
                }
                else    // hodaka added
                {
                    $log->addReport(
                        XCube_Utils::formatString(
                            _MI_MYUPLOADER_INSTALL_MSG_TPL_UNINSTALLED,
                            $tpl->get('tpl_file')
                        )
                    );
                }
            }
        }
    }

    /**
     * installAllOfBlocks
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installAllOfBlocks(&$module, &$log)
    {
        $blocks =& $module->getInfo('blocks');
        if(is_array($blocks) && count($blocks) > 0)
        {
            foreach($blocks as $block)
            {
                $newBlock =& Myuploader_InstallUtils::createBlockByInfo($module, $block);
                Myuploader_InstallUtils::installBlock($module, $newBlock, $block, $log);
            }
        }
        return true;
    }

    /**
     * &createBlockByInfo
     *
     * @param   XoopsModule  &$module
     * @param   string[]  $block
     *
     * @return  XoopsBlock
     *
     * @static
    **/
    function &createBlockByInfo(&$module, $block)
    {
        $visible = isset($block['visible']) ?
            $block['visible'] :
            (isset($block['visible_any']) ? $block['visible_any'] : 0);
        $filename = isset($block['template']) ?
            Myuploader_InstallUtils::replaceDirname($block['template'], $module->get('dirname')) :
            null;

        $blockHandler =& Myuploader_Utils::getXoopsHandler('block');
        $blockObj =& $blockHandler->create();

        $blockObj->set('mid', $module->getVar('mid'));
        $blockObj->set('options', isset($block['options']) ? $block['options'] : null);
        $blockObj->set('name', $block['name']);
        $blockObj->set('title', $block['name']);
        $blockObj->set('block_type', 'M');
        $blockObj->set('c_type', '1');
        $blockObj->set('isactive', 1);
        $blockObj->set('dirname', $module->getVar('dirname'));
        $blockObj->set('func_file', $block['file']);
        $blockObj->set('show_func', 'cl::'.$block['class']);            // hodaka added for altsys-compatible
        $blockObj->set('edit_func', ucfirst($block['class']).'_edit');  // hodaka added for altsys-compatible
        $blockObj->set('template', $filename['public']);

        $blockObj->set('last_modified', time());
        $blockObj->set('visible', $visible);
        $blockObj->set('func_num', intval($block['func_num']));
        return $blockObj;
    }

    /**
     * installBlock
     *
     * @param   XoopsModule  &$module
     * @param   XoopsBlock  &$blockObj
     * @param   string[]  &$block
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installBlock(&$module, &$blockObj, &$block, &$log)
    {
        $isNew = $blockObj->isNew();
        $blockHandler =& Myuploader_Utils::getXoopsHandler('block');
        $autoLink = isset($block['show_all_module']) ? $block['show_all_module'] : false;

        if(!$blockHandler->insert($blockObj, $autoLink))
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_INSTALLED,
                    $blockObj->getVar('name')
                )
            );
            return false;
        }

        $log->addReport(
            XCube_Utils::formatString(
                _MI_MYUPLOADER_INSTALL_MSG_BLOCK_INSTALLED,
                $blockObj->getVar('name')
            )
        );

        Myuploader_InstallUtils::installBlockTemplate($blockObj, $module, $log);

        if(!$isNew)
        {
            return true;
        }

        if($autoLink)
        {
            $sql = sprintf(
                'insert into `%s` set (`block_id`,`module_id`) values (%d,0);',
                $blockHandler->db->prefix('block_module_link'),
                $blockObj->getVar('bid')
            );
            if(!$blockHandler->db->query($sql))
            {
                $log->addWarning(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_COULD_NOT_LINK,
                        $blockObj->getVar('name')
                    )
                );
            }
        }

        $gpermHandler =& Myuploader_Utils::getXoopsHandler('groupperm');
        $perm =& $gpermHandler->create();
        $perm->setVar('gperm_itemid', $blockObj->getVar('bid'));
        $perm->setVar('gperm_name', 'block_read');
        $perm->setVar('gperm_modid', 1);
        if(isset($block['visible_any']) && $block['visible_any'])
        {
            $memberHandler =& Myuploader_Utils::getXoopsHandler('member');
            $groups =& $memberHandler->getGroups();
            foreach($groups as $group)
            {
                $perm->setVar('gperm_groupid', $group->getVar('groupid'));
                $perm->setNew();
                if(!$gpermHandler->insert($perm))
                {
                    $log->addWarning(
                        XCube_Utils::formatString(
                            _MI_MYUPLOADER_INSTALL_ERROR_PERM_COULD_NOT_SET,
                            $blockObj->getVar('name')
                        )
                    );
                }
            }
        }
        else
        {
            $root = XCube_Root::getSingleton();
            $groups = $root->mContext->mXoopsUser->getGroups();
            foreach($groups as $group)
            {
                $perm->setVar('gperm_groupid',$group);
                $perm->setNew();
                if(!$gpermHandler->insert($perm))
                {
                    $log->addWarning(
                        XCube_Utils::formatString(
                            _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_PERM_SET,
                            $blockObj->getVar('name')
                        )
                    );
                }
            }
        }

        return true;
    }

    /**
     * installBlockTemplate
     *
     * @param   XoopsBlock  &$block
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installBlockTemplate(&$block, &$module, &$log)
    {
        if($block->get('template') == null)
        {
            return true;
        }

        $trustdirname = Myuploader_InstallUtils::_getTrustDirname($module, $log);
        if(!$trustdirname) {
            return false;
        }

        $info =& $module->getInfo('blocks');
        $filename = Myuploader_InstallUtils::replaceDirname(
            $info[$block->get('func_num')]['template'],
            $module->get('dirname'),
//            $module->getInfo('trust_dirname')
            $trustdirname
        );
        $tplHandler =& Myuploader_Utils::getXoopsHandler('tplfile');

        $cri = new CriteriaCompo();
        $cri->add(new Criteria('tpl_type','block'));
        $cri->add(new Criteria('tpl_tplset','default'));
        $cri->add(new Criteria('tpl_module',$module->get('dirname')));
        $cri->add(new Criteria('tpl_file',$filename['public']));

        $tpls =& $tplHandler->getObjects($cri);

        if(count($tpls) > 0)
        {
            $tplFile =& $tpls[0];
        }
        else
        {
            $tplFile =& $tplHandler->create();
            $tplFile->set('tpl_refid',$block->get('bid'));
            $tplFile->set('tpl_tplset','default');
            $tplFile->set('tpl_file',$filename['public']);
            $tplFile->set('tpl_module',$module->get('dirname'));
            $tplFile->set('tpl_type','block');
            //$tplFile->set('tpl_desc',$block->get('description'));
            $tplFile->set('tpl_lastimported',0);
        }

        $tplSource = Myuploader_InstallUtils::readTemplateFile(
            $module->get('dirname'),
            $module->getInfo('trust_dirname'),
            $filename['trust'],
            true
        );

        $tplFile->set('tpl_source',$tplSource);
        $tplFile->set('tpl_lastmodified',time());
        if($tplHandler->insert($tplFile))
        {
            $log->addReport(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_BLOCK_TPL_INSTALLED,
                    $filename['public']
                )
            );
            return true;
        }

        $log->addError(
            XCube_Utils::formatString(
                _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_TPL_INSTALLED,
                $filename['public']
            )
        );
        return false;
    }

    /**
     * uninstallAllOfBlocks
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function uninstallAllOfBlocks(&$module,&$log)
    {
        $successFlag = true;

        $blockHandler =& Myuploader_Utils::getXoopsHandler('block');
        $gpermHandler =& Myuploader_Utils::getXoopsHandler('groupperm');
        $cri = new Criteria('mid',$module->get('mid'));
        $blocks =& $blockHandler->getObjectsDirectly($cri);

        foreach($blocks as $block)
        {
            if($blockHandler->delete($block))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_BLOCK_UNINSTALLED,
                        $block->get('name')
                    )
                );
            }
            else
            {
                $log->addWarning(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_UNINSTALLED,
                        $block->get('name')
                    )
                );
                $successFlag = false;
            }

            $cri = new CriteriaCompo();
            $cri->add(new Criteria('gperm_name', 'block_read'));
            $cri->add(new Criteria('gperm_itemid', $block->get('bid')));
            $cri->add(new Criteria('gperm_modid', 1));
            if(!$gpermHandler->deleteAll($cri))
            {
                $log->addWarning(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_PERM_DELETE,
                        $block->get('name')
                    )
                );
                $successFlag = false;
            }
        }

        return $successFlag;
    }

    /**
     * smartUpdateAllOfBlocks
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function smartUpdateAllOfBlocks(&$module, &$log)
    {
        $dirname = $module->get('dirname');

        $fileReader = new Legacy_ModinfoX2FileReader($dirname);
        $dbReader = new Legacy_ModinfoX2DBReader($dirname);

        $blocks =& $dbReader->loadBlockInformations();
        $blocks->update($fileReader->loadBlockInformations());

        foreach($blocks->mBlocks as $block)
        {
            switch($block->mStatus)
            {
                case LEGACY_INSTALLINFO_STATUS_LOADED:
                    Myuploader_InstallUtils::updateBlockTemplateByInfo($block, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_UPDATED:
                    Myuploader_InstallUtils::updateBlockByInfo($block, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_NEW:
                    Myuploader_InstallUtils::installBlockByInfo($block, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_DELETED:
                    Myuploader_InstallUtils::uninstallBlockByFuncNum($block->mFuncNum, $module, $log);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * updateBlockTemplateByInfo
     *
     * @param   Legacy_BlockInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function updateBlockTemplateByInfo(&$info, &$module, &$log)
    {
        $blockHandler =& Myuploader_Utils::getModuleHandler('newblocks','legacy');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('dirname', $module->get('dirname')));
        $cri->add(new Criteria('func_num',$info->mFuncNum));
        $blocks =& $blockHandler->getObjects($cri);

        foreach($blocks as $block)
        {
            Myuploader_InstallUtils::uninstallBlockTemplate($block, $module, $log);
            Myuploader_InstallUtils::installBlockTemplate($block, $module, $log);
        }
    }

    /**
     * updateBlockByInfo
     *
     * @param   Legacy_BlockInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function updateBlockByInfo(&$info, &$module, &$log)
    {
        $blockHandler =& Myuploader_Utils::getModuleHandler('newblocks','legacy');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('dirname', $module->get('dirname')));
        $cri->add(new Criteria('func_num', $info->mFuncNum));
        $blocks =& $blockHandler->getObjects($cri);

        $trustdirname = Myuploader_InstallUtils::_getTrustDirname($module, $log);
        if(!$trustdirname) {
            return false;
        }

        foreach($blocks as $block)
        {
            $filename = Myuploader_InstallUtils::replaceDirname(
                $info->mTemplate,
                $module->get('dirname'),
//                $module->getInfo('trust_dirname')
                $trustdirname
            );
            $block->set('options',$info->mOptions);
            $block->set('name',$info->mName);
            $block->set('func_file',$info->mFuncFile);
            $block->set('show_func',$info->mShowFunc);
            $block->set('edit_func',$info->mEditFunc);
            $block->set('template',$filename['public']);
            if($blockHandler->insert($block))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_BLOCK_UPDATED,
                        $block->get('name')
                    )
                );
            }
            else
            {
                $log->addError(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_UPDATED,
                        $block->get('name')
                    )
                );
            }
            Myuploader_InstallUtils::uninstallBlockTemplate($block,$module,$log);
            Myuploader_InstallUtils::installBlockTemplate($block,$module,$log);
        }
    }

    /**
     * installBlockByInfo
     *
     * @param   Legacy_BlockInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installBlockByInfo(&$info, &$module, &$log)
    {
        $trustdirname = Myuploader_InstallUtils::_getTrustDirname($module, $log);
        if(!$trustdirname) {
            return false;
        }

        $filename = Myuploader_InstallUtils::replaceDirname(
            $modInfo->mTemplate,
            $module->get('dirname'),
//            $module->getInfo('trust_dirname')
            $trustdirname
        );

        $blockHandler =& Myuploader_Utils::getXoopsHandler('block');

        $block =& $blockHandler->create();
        $block->set('mid', $module->get('mid'));
        $block->set('func_num', $info->mFuncNum);
        $block->set('options', $info->mOptions);
        $block->set('name', $info->mName);
        $block->set('title', $info->mName);
        $block->set('dirname', $module->get('dirname'));
        $block->set('func_file', $info->mFuncFile);
        $block->set('show_func', $info->mShowFunc);
        $block->set('edit_func', $info->mEditFunc);
        $block->set('template', $filename['public']);
        $block->set('block_type', 'M');
        $block->set('c_type', 1);

        if(!$blockHandler->insert($block))
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_INSTALLED,
                    $block->get('name')
                )
            );
            return false;
        }

        $log->addReport(
            XCube_Utils::formatString(
                _MI_MYUPLOADER_INSTALL_MSG_BLOCK_INSTALLED,
                $block->get('name')
            )
        );

        Myuploader_InstallUtils::installBlockTemplate($block,$module,$log);
        return true;
    }

    /**
     * uninstallBlockByFuncNum
     *
     * @param   int  $func_num
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function uninstallBlockByFuncNum($func_num, &$module, &$log)
    {
        $blockHandler =& Myuploader_Utils::getModuleHandler('newblocks', 'legacy');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('dirname', $module->get('dirname')));
        $cri->add(new Criteria('func_num', $func_num));
        $blocks =& $blockHandler->getObjects($cri);

        $successFlag = true;
        foreach($blocks as $block)
        {
            if($blockHandler->delete($block))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_BLOCK_UNINSTALLED,
                        $block->get('name')
                    )
                );
            }
            else
            {
                $log->addError(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_BLOCK_UNINSTALLED,
                        $block->get('name')
                    )
                );
                $successFlag = false;
            }
        }
        return $successFlag;
    }

    /**
     * uninstallBlockTemplate
     *
     * @param   XoopsBlock  &$block
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function uninstallBlockTemplate(&$block, &$module, &$log)
    {
        $tplHandler =& Myuploader_Utils::getXoopsHandler('tplfile');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('tpl_refid', $block->get('bid')));
        $cri->add(new Criteria('tpl_file', $block->get('template')));
        $cri->add(new Criteria('tpl_module', $module->get('dirname')));
        $cri->add(new Criteria('tpl_type', 'block'));

        if(!$tplHandler->deleteAll($cri))
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_TPL_UNINSTALLED,
                    $block->get('template')
                )
            );
            return false;
        }

        $log->addReport(
            XCube_Utils::formatString(
                _MI_MYUPLOADER_INSTALL_MSG_BLOCK_TPL_UNINSTALLED,
                $block->get('template')
            )
        );
        return true;
    }

    /**
     * installAllOfConfigs
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function installAllOfConfigs(&$module, &$log)
    {
        $successFlag = true;
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');
        $fileReader = new Legacy_ModinfoX2FileReader($module->get('dirname'));    // TODO will be use other class?
        $preferences =& $fileReader->loadPreferenceInformations();

        foreach($preferences->mPreferences as $info)
        {
            $config =& $configHandler->createConfig();
            $config->set('conf_modid', $module->get('mid'));
            $config->set('conf_catid', 0);
            $config->set('conf_name', $info->mName);
            $config->set('conf_title', $info->mTitle);
            $config->set('conf_desc', $info->mDescription);
            $config->set('conf_formtype', $info->mFormType);
            $config->set('conf_valuetype', $info->mValueType);
            $config->setConfValueForInput($info->mDefault);
            $config->set('conf_order', $info->mOrder);

            if(count($info->mOption->mOptions) > 0)
            {
                foreach($info->mOption->mOptions as $opt)
                {
                    $option = $configHandler->createConfigOption();
                    $option->set('confop_name', $opt->mName);
                    $option->set('confop_value', $opt->mValue);
                    $config->setConfOptions($option);
                    unset($option);
                }
            }

            if($configHandler->insertConfig($config))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_CONFIG_ADDED,
                        $config->get('conf_name')
                    )
                );
            }
            else
            {
                $log->addError(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_ADDED,
                        $config->get('conf_name')
                    )
                );
                $successFlag = false;
            }
        }

        return $successFlag;
    }

    /**
     * installConfigByInfo
     *
     * @param   Legacy_PreferenceInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function installConfigByInfo(&$info, &$module, &$log)
    {
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');
        $config =& $configHandler->createConfig();
        $config->set('conf_modid', $module->get('mid'));
        $config->set('conf_catid', 0);
        $config->set('conf_name', $info->mName);
        $config->set('conf_title', $info->mTitle);
        $config->set('conf_desc', $info->mDescription);
        $config->set('conf_formtype', $info->mFormType);
        $config->set('conf_valuetype', $info->mValueType);
        $config->setConfValueForInput($info->mDefault);
        $config->set('conf_order', $info->mOrder);

        if(count($info->mOption->mOptions) > 0)
        {
            foreach($info->mOption->mOptions as $opt)
            {
                $option = $configHandler->createConfigOption();
                $option->set('confop_name', $opt->mName);
                $option->set('confop_value', $opt->mValue);
                $config->setConfOptions($option);
                unset($option);
            }
        }

        if($configHandler->insertConfig($config))
        {
            $log->addReport(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_CONFIG_ADDED,
                    $config->get('conf_name')
                )
            );
        }
        else
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_ADDED,
                    $config->get('conf_name')
                )
            );
        }

    }

    /**
     * uninstallAllOfConfigs
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function uninstallAllOfConfigs(&$module, &$log)
    {
//error_log('path1'."\n", 3, 'd:/tmp/debug.log');
        if($module->get('hasconfig') == 0)
        {
            return true;
        }
//error_log('path2'."\n", 3, 'd:/tmp/debug.log');
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');
        $configs =& $configHandler->getConfigs(new Criteria('conf_modid', $module->get('mid'))); // hodka fixed a typo
//error_log('count of configs='.count($configs)."\n", 3, 'd:/tmp/debug.log');
        if(count($configs) == 0)
        {
            return true;
        }

        $sucessFlag = true;
        foreach($configs as $config)
        {
            if($configHandler->deleteConfig($config))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_CONFIG_DELETED,
                        $config->getVar('conf_name')
                    )
                );
            }
            else
            {
                $log->addWarning(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_DELETED,
                        $config->getVar('conf_name')
                    )
                );
                $sucessFlag = false;
            }
        }
//error_log('success flag='.$sucessFlag."\n", 3, 'd:/tmp/debug.log');
        return $sucessFlag;
    }

    /**
     * uninstallConfigByOrder
     *
     * @param   int  $order
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function uninstallConfigByInfo($info, &$module, &$log)
    {
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');

        $cri = new CriteriaCompo();
        $cri->add(new Criteria('conf_modid', $module->get('mid')));
        $cri->add(new Criteria('conf_catid', 0));
        $cri->add(new Criteria('conf_name', $info->mName));
        $configs = $configHandler->getConfigs($cri);

        foreach($configs as $config)
        {
            if($handler->deleteConfig($config))
            {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_CONFIG_DELETED,
                        $config->get('conf_name')
                    )
                );
            }
            else
            {
                $log->addError(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_DELETED,
                        $config->get('conf_name')
                    )
                );
            }
        }
    }

    /**
     * smartUpdateAllOfConfigs
     *
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  void
     *
     * @static
    **/
    function smartUpdateAllOfConfigs(&$module, &$log)
    {
        $dirname = $module->get('dirname');

        $fileReader = new Legacy_ModinfoX2FileReader($dirname);
        $dbReader = new Legacy_ModinfoX2DBReader($dirname);

        $configs  =& $dbReader->loadPreferenceInformations();
        $configs->update($fileReader->loadPreferenceInformations());

        foreach($configs->mPreferences as $config)
        {
            switch($config->mStatus)
            {
                case LEGACY_INSTALLINFO_STATUS_UPDATED:
                    Myuploader_InstallUtils::updateConfigByInfo($config, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
                    Myuploader_InstallUtils::updateConfigOrderByInfo($config, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_NEW:
                    Myuploader_InstallUtils::installConfigByInfo($config, $module, $log);
                    break;
                case LEGACY_INSTALLINFO_STATUS_DELETED:
                    Myuploader_InstallUtils::uninstallConfigByInfo($config, $module, $log);
                    break;
                default:
                    break;
            }
        }

        // TODO keynext
        // Comments
        //
        foreach ($configs->mComments as $config) {
            switch ($config->mStatus) {
                case LEGACY_INSTALLINFO_STATUS_UPDATED:
                    Myuploader_InstallUtils::updateConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mComments[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
                    Myuploader_InstallUtils::updateConfigOrderByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::updatePreferenceOrderByInfo($currentPreferences->mComments[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_NEW:
                    Myuploader_InstallUtils::installConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mComments[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_DELETED:
                    Myuploader_InstallUtils::uninstallConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mComments[$idx]->mOrder, $module, $log);
                    break;
            }
        }

        //
        // Notifications
        //
        foreach ($configs->mNotifications as $config) {
            switch ($config->mStatus) {
                case LEGACY_INSTALLINFO_STATUS_UPDATED:
                    Myuploader_InstallUtils::updateConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mNotifications[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
                    Myuploader_InstallUtils::updateConfigOrderByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::updatePreferenceOrderByInfo($currentPreferences->mNotifications[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_NEW:
                    Myuploader_InstallUtils::installConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mNotifications[$idx], $module, $log);
                    break;

                case LEGACY_INSTALLINFO_STATUS_DELETED:
                    Myuploader_InstallUtils::uninstallConfigByInfo($config, $module, $log);
                    //Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mNotifications[$idx]->mOrder, $module, $log);
                    break;
            }
        }
    }

    /**
     * updateConfigByInfo
     *
     * @param   Legacy_PreferenceInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function updateConfigByInfo(&$info, &$module, &$log)
    {
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('conf_modid', $module->get('mid')));
        $cri->add(new Criteria('conf_catid', 0));
        $cri->add(new Criteria('conf_name', $info->mName));
        $configs =& $configHandler->getConfigs($cri);

        if(!(count($configs) > 0 && is_object($configs[0])))
        {
            $log->addError(_MI_MYUPLOADER_INSTALL_ERROR_CONFIG_NOT_FOUND);
            return false;
        }

        $config =& $configs[0];
        $config->set('conf_title',$info->mTitle);
        $config->set('conf_desc',$info->mDescription);
        if($config->get('conf_formtype') != $info->mFormType && $config->get('conf_valuetype') != $info->mValueType)
        {
            $config->set('conf_formtype', $info->mFormType);
            $config->set('conf_valuetype', $info->mValueType);
            $config->setConfValueForInput($info->mDefault);
        }
        else
        {
            $config->set('conf_formtype', $info->mFormType);
            $config->set('conf_valuetype', $info->mValueType);
        }
        $config->set('conf_order',$info->mOrder);

        $options =& $configHandler->getConfigOptions(new Criteria('conf_id', $config->get('conf_id')));
        if(is_array($options))
        {
            foreach($options as $opt)
            {
                $configHandler->_oHandler->delete($opt);  // TODO will be use other method
            }
        }

        if(count($info->mOption->mOptions) > 0)
        {
            foreach($info->mOption->mOptions as $opt)
            {
                $option =& $configHandler->createConfigOption();
                $option->set('confop_name', $opt->mName);
                $option->set('confop_value', $opt->mValue);
                $option->set('conf_id', $option->get('conf_id'));    // TODO check conf_id is right
                $config->setConfOptions($option);
                unset($option);
            }
        }

        if($configHandler->insertConfig($config))
        {
            $log->addReport(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_CONFIG_UPDATED,
                    $config->get('conf_name')
                )
            );
            return true;
        }

        $log->addError(
            XCube_Utils::formatString(
                _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_UPDATED,
                $config->get('conf_name')
            )
        );
        return false;
    }

    /**
     * updateConfigOrderByInfo
     *
     * @param   Legacy_PreferenceInformation  &$info
     * @param   XoopsModule  &$module
     * @param   Legacy_ModuleInstallLog  &$log
     *
     * @return  bool
     *
     * @static
    **/
    function updateConfigOrderByInfo(&$info, &$module, &$log)
    {
        $configHandler =& Myuploader_Utils::getXoopsHandler('config');
        $cri = new CriteriaCompo();
        $cri->add(new Criteria('conf_modid', $module->get('mid')));
        $cri->add(new Criteria('conf_catid', 0));
        $cri->add(new Criteria('conf_name', $info->mName));
        $configs =& $configHandler->getConfigs($cri);

        if(!(count($configs) > 0 && is_object($configs[0])))
        {
            $log->addError(_MI_MYUPLOADER_INSTALL_ERROR_CONFIG_NOT_FOUND);
            return false;
        }

        $config =& $configs[0];
        $config->set('conf_order',$info->mOrder);
        if(!$configHandler->insertConfig($config))
        {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_CONFIG_UPDATED,
                    $config->get('conf_name')
                )
            );
            return false;
        }
        return true;
    }

    function unlinkAllOfImages(&$module, &$log) {
        $handler =& xoops_gethandler('config');
        $moduleConfig = $handler->getConfigsByCat(0, $module->get('mid'));
        $path = !empty($moduleConfig['upload_dir'])? preg_replace("|^(.+)/$|", '$1', XOOPS_ROOT_PATH.$moduleConfig['upload_dir']) : XOOPS_UPLOAD_PATH.'/myuploader';

        if(!file_exists($path) && !ini_get('safe_mode')) {
            $log->addWarning(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_SKIPPED_DELETING_IMAGES,
                    $path
                )
            );
            return true;
        }

        if($handler = @opendir($path)) {
            while(($file = readdir($handler)) !== false ) {
                if(substr($file, 0, 1) == '.') continue ;
                $file_path = $path.'/'.$file;
                if(is_file($file_path)) {
                    @unlink($file_path);
                }
            }
            closedir($handler);

            $log->addReport(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_MSG_IMAGES_DELETED,
                    $path
                )
            );
        }

        // remove dir
        if(ini_get('safe_mode')) {
            $log->addWarning(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_UPLOADDIR_SAFEMODE,
                    $path
                )
            );
        } else {
            if(rmdir($path)) {
                $log->addReport(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_MSG_UPLOADDIR_DELETED,
                        $path
                    )
                );
            } else {
                $log->addWarning(
                    XCube_Utils::formatString(
                        _MI_MYUPLOADER_INSTALL_ERROR_UPLOADDIR_DELETE,
                        $path
                    )
                );
            }
        }
        return true;
    }

    function _getTrustDirname(&$module, &$log) {
        $dirname = $module->getVar('dirname');
        $file = sprintf('%s/modules/%s/mytrustdirname.php', XOOPS_ROOT_PATH, $dirname);
        if(!file_exists($file)) {
            $log->addError(
                XCube_Utils::formatString(
                    _MI_MYUPLOADER_INSTALL_ERROR_TRUST_DIRNAME_UNDEFINED,
                    'Trust Dirname'
                )
            );
            return false;
        }

        require $file;
        return $mytrustdirname;
    }
}

?>