Application =& kApplication::Instance(); $this->Conn =& $this->Application->GetADODBConnection(); } $this->INIFile = FULL_PATH . '/config.php'; $this->systemConfig = $this->ParseConfig(true); } /** * Sets installator * * @param kInstallator $instance */ function setInstallator(&$instance) { $this->_installator =& $instance; } /** * Checks prerequisities before module install or upgrade * * @param string $module_path * @param string $versions * @param string $mode upgrade mode = {install, standalone, upgrade} */ function CheckPrerequisites($module_path, $versions, $mode) { static $prerequisit_classes = Array (); $prerequisites_file = sprintf(PREREQUISITE_FILE, $module_path); if (!file_exists($prerequisites_file) || !$versions) { return Array (); } if (!isset($prerequisit_classes[$module_path])) { // save class name, because 2nd time // (in after call $prerequisite_class variable will not be present) include_once $prerequisites_file; $prerequisit_classes[$module_path] = $prerequisite_class; } $prerequisite_object = new $prerequisit_classes[$module_path](); if (method_exists($prerequisite_object, 'setToolkit')) { $prerequisite_object->setToolkit($this); } // some errors possible return $prerequisite_object->CheckPrerequisites($versions, $mode); } /** * Processes one license, received from server * * @param string $file_data */ function processLicense($file_data) { $modules_helper =& $this->Application->recallObject('ModulesHelper'); /* @var $modules_helper kModulesHelper */ $file_data = explode('Code==:', $file_data); $file_data[0] = str_replace('In-Portal License File - do not edit!' . "\n", '', $file_data[0]); $file_data = array_map('trim', $file_data); if ($modules_helper->verifyLicense($file_data[0])) { $this->setSystemConfig('Intechnic', 'License', $file_data[0]); if (array_key_exists(1, $file_data)) { $this->setSystemConfig('Intechnic', 'LicenseCode', $file_data[1]); } else { $this->setSystemConfig('Intechnic', 'LicenseCode'); } $this->SaveConfig(); } else { // invalid license received from licensing server $this->_installator->errorMessage = 'Invalid License File'; } } /** * Saves given configuration values to database * * @param Array $config */ function saveConfigValues($config) { foreach ($config as $config_var => $value) { $sql = 'UPDATE ' . TABLE_PREFIX . 'ConfigurationValues SET VariableValue = ' . $this->Conn->qstr($value) . ' WHERE VariableName = ' . $this->Conn->qstr($config_var); $this->Conn->Query($sql); } } /** * Sets module version to passed * * @param string $module_name * @param string $version */ function SetModuleVersion($module_name, $version = false) { if ($version === false) { $version = $this->GetMaxModuleVersion($module_name); } // get table prefix from config, because application may not be available here $table_prefix = $this->getSystemConfig('Database', 'TablePrefix'); if ($module_name == 'kernel') { $module_name = 'in-portal'; } $sql = 'UPDATE ' . $table_prefix . 'Modules SET Version = "' . $version . '" WHERE LOWER(Name) = "' . strtolower($module_name) . '"'; $this->Conn->Query($sql); } /** * Sets module root category to passed * * @param string $module_name * @param string $category_id */ function SetModuleRootCategory($module_name, $category_id = 0) { // get table prefix from config, because application may not be available here $table_prefix = $this->getSystemConfig('Database', 'TablePrefix'); if ($module_name == 'kernel') { $module_name = 'in-portal'; } $sql = 'UPDATE ' . $table_prefix . 'Modules SET RootCat = ' . $category_id . ' WHERE LOWER(Name) = "' . strtolower($module_name) . '"'; $this->Conn->Query($sql); } /** * Returns maximal version of given module by scanning it's upgrade scripts * * @param string $module_name * @return string */ function GetMaxModuleVersion($module_name) { $upgrades_file = sprintf(UPGRADES_FILE, mb_strtolower($module_name).'/', 'sql'); if (!file_exists($upgrades_file)) { // no upgrade file return '4.0.1'; } $sqls = file_get_contents($upgrades_file); $versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs); if (!$versions_found) { // upgrades file doesn't contain version definitions return '4.0.1'; } return end($regs[1]); } /** * Runs SQLs from file * * @param string $filename * @param mixed $replace_from * @param mixed $replace_to */ function RunSQL($filename, $replace_from = null, $replace_to = null) { if (!file_exists(FULL_PATH.$filename)) { return ; } $sqls = file_get_contents(FULL_PATH.$filename); if (!$this->RunSQLText($sqls, $replace_from, $replace_to)) { if (is_object($this->_installator)) { $this->_installator->Done(); } else { if (isset($this->Application)) { $this->Application->Done(); } exit; } } } /** * Runs SQLs from string * * @param string $sqls * @param mixed $replace_from * @param mixed $replace_to */ function RunSQLText(&$sqls, $replace_from = null, $replace_to = null, $start_from=0) { $table_prefix = $this->getSystemConfig('Database', 'TablePrefix'); // add prefix to all tables if (strlen($table_prefix) > 0) { $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); foreach ($replacements as $replacement) { $sqls = str_replace($replacement, $replacement . $table_prefix, $sqls); } } $sqls = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sqls); $sqls = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sqls); if (isset($replace_from) && isset($replace_to)) { // replace something additionally, e.g. module root category $sqls = str_replace($replace_from, $replace_to, $sqls); } $sqls = str_replace("\r\n", "\n", $sqls); // convert to linux line endings $sqls = preg_replace("/#([^;]*?)\n/", '', $sqls); // remove all comments $sqls = explode(";\n", $sqls . "\n"); // ensures that last sql won't have ";" in it $db_collation = $this->getSystemConfig('Database', 'DBCollation'); for ($i=$start_from; $i add collation $sql .= ' COLLATE \'' . $db_collation . '\''; } $this->Conn->Query($sql); if ($this->Conn->getErrorCode() != 0) { if (is_object($this->_installator)) { $this->_installator->errorMessage = 'Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg().'

Last Database Query:
'; $this->_installator->LastQueryNum = $i + 1; } return false; } } return true; } /** * Performs clean language import from given xml file * * @param string $lang_file * @param bool $upgrade */ function ImportLanguage($lang_file, $upgrade = false) { $lang_file = FULL_PATH.$lang_file.'.lang'; if (!file_exists($lang_file)) { return ; } $lang_xml =& $this->Application->recallObject('LangXML'); /* @var $lang_xml LangXML_Parser */ $lang_xml->Parse($lang_file, '|0|1|2|', '', $upgrade ? LANG_SKIP_EXISTING : LANG_OVERWRITE_EXISTING); } /** * Converts module version in format X.Y.Z to signle integer * * @param string $version * @return int */ function ConvertModuleVersion($version) { $parts = explode('.', $version); $bin = ''; foreach ($parts as $part) { $bin .= str_pad(decbin($part), 8, '0', STR_PAD_LEFT); } return bindec($bin); } /** * Returns themes, found in system * * @param bool $rebuild * @return int */ function getThemes($rebuild = false) { if ($rebuild) { $this->rebuildThemes(); } $id_field = $this->Application->getUnitOption('theme', 'IDField'); $table_name = $this->Application->getUnitOption('theme', 'TableName'); $sql = 'SELECT Name, ' . $id_field . ' FROM ' . $table_name; return $this->Conn->GetCol($sql, $id_field); } function ParseConfig($parse_section = false) { if (!file_exists($this->INIFile)) { return Array(); } if( file_exists($this->INIFile) && !is_readable($this->INIFile) ) { die('Could Not Open Ini File'); } $contents = file($this->INIFile); $retval = Array(); $section = ''; $ln = 1; $resave = false; foreach ($contents as $line) { if ($ln == 1 && $line != '<'.'?'.'php die() ?'.">\n") { $resave = true; } $ln++; $line = trim($line); $line = eregi_replace(';[.]*','',$line); if (strlen($line) > 0) { //echo $line . " - "; if(eregi('^[[a-z]+]$',str_replace(' ', '', $line))) { //echo 'section'; $section = mb_substr($line, 1, (mb_strlen($line) - 2)); if ($parse_section) { $retval[$section] = array(); } continue; } elseif (eregi('=',$line)) { //echo 'main element'; list ($key, $val) = explode(' = ', $line); if (!$parse_section) { $retval[trim($key)] = str_replace('"', '', $val); } else { $retval[$section][trim($key)] = str_replace('"', '', $val); } } } } if ($resave) { $fp = fopen($this->INIFile, 'w'); reset($contents); fwrite($fp,'<'.'?'.'php die() ?'.">\n\n"); foreach ($contents as $line) { fwrite($fp,"$line"); } fclose($fp); } return $retval; } function SaveConfig($silent = false) { if (!is_writeable($this->INIFile)) { trigger_error('Cannot write to "' . $this->INIFile . '" file.', $silent ? E_USER_NOTICE : E_USER_ERROR); return ; } $fp = fopen($this->INIFile, 'w'); fwrite($fp,'<'.'?'.'php die() ?'.">\n\n"); foreach ($this->systemConfig as $section_name => $section_data) { fwrite($fp, '['.$section_name."]\n"); foreach ($section_data as $key => $value) { fwrite($fp, $key.' = "'.$value.'"'."\n"); } fwrite($fp, "\n"); } fclose($fp); } /** * Sets value to system config (yet SaveConfig must be called to write it to file) * * @param string $section * @param string $key * @param string $value */ function setSystemConfig($section, $key, $value = null) { if (isset($value)) { if (!array_key_exists($section, $this->systemConfig)) { // create section, when missing $this->systemConfig[$section] = Array (); } // create key in section $this->systemConfig[$section][$key] = $value; return ; } unset($this->systemConfig[$section][$key]); } /** * Returns information from system config * * @return string */ function getSystemConfig($section, $key) { if (!array_key_exists($section, $this->systemConfig)) { return false; } if (!array_key_exists($key, $this->systemConfig[$section])) { return false; } return $this->systemConfig[$section][$key] ? $this->systemConfig[$section][$key] : false; } /** * Checks if system config is present and is not empty * * @return bool */ function systemConfigFound() { return file_exists($this->INIFile) && $this->systemConfig; } /** * Checks if given section is present in config * * @param string $section * @return bool */ function sectionFound($section) { return array_key_exists($section, $this->systemConfig); } /** * Returns formatted module name based on it's root folder * * @param string $module_folder * @return string */ function getModuleName($module_folder) { if ($module_folder == 'kernel') { $module_folder = 'in-portal'; } return implode('-', array_map('ucfirst', explode('-', $module_folder))); } /** * Creates module root category in "Home" category using given data and returns it * * @param string $name * @param string $description * @param string $category_template * @param string $status * @return kDBItem */ function &createModuleCategory($name, $description, $category_template = null, $status = 1) { static $fields = null; if (!isset($fields)) { $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); $fields['name'] = $ml_formatter->LangFieldName('Name'); $fields['description'] = $ml_formatter->LangFieldName('Description'); } $category =& $this->Application->recallObject('c', null, Array ('skip_autoload' => true)); /* @var $category kDBItem */ $category_fields = Array ( $fields['name'] => $name, 'Filename' => $name, 'AutomaticFilename' => 1, $fields['description'] => $description, 'Status' => $status, ); $category_fields['ParentId'] = $this->Application->findModule('Name', 'Core', 'RootCat'); if (isset($category_template)) { $category_fields['Template'] = $category_template; $category_fields['CachedTemplate'] = $category_template; } $category->Clear(); $category->SetDBFieldsFromHash($category_fields); $category->Create(); $priority_helper =& $this->Application->recallObject('PriorityHelper'); /* @var $priority_helper kPriorityHelper */ $event = new kEvent('c:OnListBuild'); // ensure, that newly created category has proper value in Priority field $priority_helper->recalculatePriorities($event, 'ParentId = ' . $category_fields['ParentId']); return $category; } /** * Sets category item template into custom field for given prefix * * @param kDBItem $category * @param string $prefix * @param string $item_template */ function setModuleItemTemplate(&$category, $prefix, $item_template) { $this->Application->removeObject('c-cdata'); // recreate all fields, because custom fields are added during install script $category->defineFields(); $category->prepareConfigOptions(); // creates ml fields $category->SetDBField('cust_' . $prefix .'_ItemTemplate', $item_template); $category->Update(); } /** * Link custom field records with search config records + create custom field columns * * @param string $module_folder * @param int $item_type */ function linkCustomFields($module_folder, $prefix, $item_type) { $module_folder = strtolower($module_folder); $module_name = ($module_folder == 'kernel') ? 'in-portal' : $module_folder; $db =& $this->Application->GetADODBConnection(); $sql = 'SELECT FieldName, CustomFieldId FROM ' . TABLE_PREFIX . 'CustomField WHERE Type = ' . $item_type . ' AND IsSystem = 0'; // config is not read here yet :( $this->Application->getUnitOption('p', 'ItemType'); $custom_fields = $db->GetCol($sql, 'CustomFieldId'); foreach ($custom_fields as $cf_id => $cf_name) { $sql = 'UPDATE ' . TABLE_PREFIX . 'SearchConfig SET CustomFieldId = ' . $cf_id . ' WHERE (TableName = "CustomField") AND (LOWER(ModuleName) = "' . $module_name . '") AND (FieldName = ' . $db->qstr($cf_name) . ')'; $db->Query($sql); } $this->Application->refreshModuleInfo(); // this module configs are now processed // because of configs was read only from installed before modules (in-portal), then reread configs $unit_config_reader =& $this->Application->recallObject('kUnitConfigReader'); $unit_config_reader->scanModules(MODULES_PATH . '/' . $module_folder); // create correct columns in CustomData table $ml_helper =& $this->Application->recallObject('kMultiLanguageHelper'); $ml_helper->createFields($prefix . '-cdata', true); } /** * Deletes cache, useful after separate module install and installator last step * */ function deleteCache($refresh_permissions = false) { $sql = 'DELETE FROM ' . TABLE_PREFIX . 'Cache WHERE VarName IN ("config_files", "configs_parsed", "sections_parsed", "cms_menu", "StructureTree")'; $this->Conn->Query($sql); if ($refresh_permissions) { if ($this->Application->ConfigValue('QuickCategoryPermissionRebuild')) { // refresh permission without progress bar $updater =& $this->Application->recallObject('kPermCacheUpdater'); /* @var $updater kPermCacheUpdater */ $updater->OneStepRun(); } else { // refresh permissions with ajax progress bar (when available) $fields_hash = Array ( 'VarName' => 'ForcePermCacheUpdate', 'Data' => 1, ); $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache'); } } } /** * Perform redirect after separate module install * * @param string $module_folder * @param bool $refresh_permissions */ function finalizeModuleInstall($module_folder, $refresh_permissions = false) { if (!$this->Application->GetVar('redirect')) { return ; } $this->SetModuleVersion($module_folder); $this->deleteCache($refresh_permissions); $url_params = Array ( 'pass' => 'm', 'admin' => 1, 'RefreshTree' => 1, 'index_file' => 'index.php', ); $this->Application->Redirect('modules/modules_list', $url_params); } /** * Performs rebuild of themes * */ function rebuildThemes() { $this->Application->HandleEvent($themes_event, 'adm:OnRebuildThemes'); } }