array('self' => true), 'OnClosePopup' => array('self' => true), 'OnSaveSetting' => array('self' => true), // export/import permissions is checked within events 'OnExportCSV' => Array('self' => true), 'OnGetCSV' => Array('self' => true), 'OnCSVImportBegin' => Array('self' => true), 'OnCSVImportStep' => Array('self' => true), 'OnDropTempTablesByWID' => array('self' => true), ); $this->permMapping = array_merge($this->permMapping, $permissions); } /** * Checks permissions of user * * @param kEvent $event */ function CheckPermission(&$event) { $system_events = Array ( 'OnResetModRwCache', 'OnResetCMSMenuCache', 'OnResetSections', 'OnResetConfigsCache', 'OnCompileTemplates', 'OnGenerateTableStructure', 'OnRebuildThemes', 'OnCheckPrefixConfig', ); if ($this->Application->isDebugMode(false) && in_array($event->Name, $system_events)) { return true; } return parent::CheckPermission($event); } /** * Enter description here... * * @param kEvent $event */ function OnResetModRwCache(&$event) { if ($this->Application->GetVar('ajax') == 'yes') { $event->status = erSTOP; } $this->Conn->Query('DELETE FROM '.TABLE_PREFIX.'Cache WHERE VarName LIKE "mod_rw%"'); } function OnResetCMSMenuCache(&$event) { if ($this->Application->GetVar('ajax') == 'yes') { $event->status = erSTOP; } $this->Conn->Query('DELETE FROM '.TABLE_PREFIX.'Cache WHERE VarName = "cms_menu"'); } function OnResetSections(&$event) { if ($this->Application->GetVar('ajax') == 'yes') { $event->status = erSTOP; } $this->Conn->Query('DELETE FROM '.TABLE_PREFIX.'Cache WHERE VarName = "sections_parsed"'); if (isset($this->Application->Memcached)) { $this->Application->Memcached->delete('master:sections_parsed'); } } function OnResetConfigsCache(&$event) { if ($this->Application->GetVar('ajax') == 'yes') { $event->status = erSTOP; } $this->Conn->Query('DELETE FROM '.TABLE_PREFIX.'Cache WHERE VarName = "config_files" OR VarName = "configs_parsed" OR VarName = "sections_parsed"'); if (isset($this->Application->Memcached)) { $this->Application->Memcached->delete('master:config_files'); $this->Application->Memcached->delete('master:configs_parsed'); $this->Application->Memcached->delete('master:sections_parsed'); } } function OnCompileTemplates(&$event) { $compiler =& $this->Application->recallObject('NParserCompiler'); /* @var $compiler NParserCompiler */ $compiler->CompileTemplatesStep(); $event->status = erSTOP; } /** * Generates sturcture for specified table * * @param kEvent $event * @author Alex */ function OnGenerateTableStructure(&$event) { $types_hash = Array( 'string' => 'varchar|text|mediumtext|longtext|date|datetime|time|timestamp|char|year|enum|set', 'int' => 'smallint|mediumint|int|bigint|tinyint', 'float' => 'float|double|decimal', ); $table_name = $this->Application->GetVar('table_name'); if (!$table_name) { echo 'error: no table name specified'; return ; } if (TABLE_PREFIX && !preg_match('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', $table_name)) { // table name without prefix, then add it $table_name = TABLE_PREFIX.$table_name; } if (!$this->Conn->TableFound($table_name)) { // table with prefix doesn't exist, assume that just config prefix passed -> resolve table name from it $table_name = $this->Application->getUnitOption(substr($table_name, strlen(TABLE_PREFIX)), 'TableName'); } $table_info = $this->Conn->Query('DESCRIBE '.$table_name); // 1. prepare config keys $id_field = ''; $fields = Array(); $float_types = Array ('float', 'double', 'numeric'); foreach ($table_info as $field_info) { if (preg_match('/l[\d]+_.*/', $field_info['Field'])) { // don't put multilingual fields in config continue; } $field_options = Array (); // 1. get php field type by mysql field type foreach ($types_hash as $php_type => $db_types) { if (preg_match('/'.$db_types.'/', $field_info['Type'])) { $field_options['type'] = $php_type; break; } } $default_value = $field_info['Default']; if (in_array($php_type, $float_types)) { // this is float number if (preg_match('/'.$db_types.'\([\d]+,([\d]+)\)/i', $field_info['Type'], $regs)) { // size is described in structure -> add formatter $field_options['formatter'] = 'kFormatter'; $field_options['format'] = '%01.'.$regs[1].'f'; if ($field_info['Null'] != 'YES') { // null fields, will most likely have NULL as default value $default_value = 0; } } else { // no size information, just convert to float if ($field_info['Null'] != 'YES') { // null fields, will most likely have NULL as default value $default_value = (float)$default_value; } } } if (preg_match('/varchar\(([\d]+)\)/i', $field_info['Type'], $regs)) { $field_options['max_len'] = (int)$regs[1]; } if ($field_info['Null'] != 'YES') { $field_options['not_null'] = 1; } if ($field_info['Key'] == 'PRI') { $default_value = 0; $id_field = $field_info['Field']; } if ($php_type == 'int' && ($field_info['Null'] != 'YES' || is_numeric($default_value))) { // is integer field AND not null $field_options['default'] = (int)$default_value; } else { $field_options['default'] = $default_value; } $fields[ $field_info['Field'] ] = $this->transformDump($field_options); } $ret = stripslashes(var_export($fields, true)); $ret = preg_replace("/'(.*?)' => 'Array \((.*?), \)',/", "'\\1' => Array (\\2),", $ret); $ret = preg_replace("/\n '/", "\n\t'", $ret); $ret = "'IDField' => '".$id_field."',\n'Fields' => A".substr($ret, 1).','; ob_start(); ?> Table "<?php echo $table_name; ?>" Structure Close Window
highlightString($ret); ?>
Close Window
status = erSTOP; } function transformDump($dump) { if (is_array($dump)) { $dump = var_export($dump, true); } $dump = preg_replace("/,\n[ ]*/", ', ', $dump); $dump = preg_replace("/array \(\n[ ]*/", 'Array (', $dump); // replace array start $dump = preg_replace("/,\n[ ]*\),/", "),", $dump); // replace array end return $dump; } /** * Refreshes ThemeFiles & Theme tables by actual content on HDD * * @param kEvent $event */ function OnRebuildThemes(&$event) { if ($this->Application->GetVar('ajax') == 'yes') { $event->status = erSTOP; } $themes_helper =& $this->Application->recallObject('ThemesHelper'); /* @var $themes_helper kThemesHelper */ $themes_helper->refreshThemes(); } function OnSaveColumns(&$event) { $picker_helper =& $this->Application->recallObject('ColumnPickerHelper'); /* @var $picker_helper kColumnPickerHelper */ $picker_helper->SetGridName($this->Application->GetLinkedVar('grid_name')); $picked = trim($this->Application->GetVar('picked_str'), '|'); $hidden = trim($this->Application->GetVar('hidden_str'), '|'); $main_prefix = $this->Application->GetVar('main_prefix'); $picker_helper->SaveColumns($main_prefix, $picked, $hidden); $this->finalizePopup($event); } /** * Saves various admin settings via ajax * * @param kEvent $event */ function OnSaveSetting(&$event) { if ($this->Application->GetVar('ajax') != 'yes') { return ; } $var_name = $this->Application->GetVar('var_name'); $var_value = $this->Application->GetVar('var_value'); $this->Application->StorePersistentVar($var_name, $var_value); $event->status = erSTOP; } /** * Just closes popup & deletes last_template & opener_stack if popup, that is closing * * @param kEvent $event */ function OnClosePopup(&$event) { $event->SetRedirectParam('opener', 'u'); } /** * Occurs right after initialization of the kernel, used mainly as hook-to event * * @param kEvent $event */ function OnStartup(&$event) { } /** * Occurs right before echoing the output, in Done method of application, used mainly as hook-to event * * @param kEvent $event */ function OnBeforeShutdown(&$event) { } /** * Is called after tree was build (when not from cache) * * @param kEvent $event */ function OnAfterBuildTree(&$event) { } /** * Called by AJAX to perform CSV export * * @param kEvent $event */ function OnExportCSV(&$event) { $export_helper =& $this->Application->recallObject('CSVHelper'); /* @var $export_helper kCSVHelper */ $prefix_special = $this->Application->GetVar('PrefixSpecial'); if(!$prefix_special) { $prefix_special = $export_helper->ExportData('prefix'); } $prefix_elems = split('\.|_', $prefix_special, 2); $perm_sections = $this->Application->getUnitOption($prefix_elems[0], 'PermSection'); if(!$this->Application->CheckPermission($perm_sections['main'].'.view')) { $this->Application->Redirect('no_permission'); } $export_helper->PrefixSpecial = $prefix_special; $export_helper->grid = $this->Application->GetVar('grid'); $export_helper->ExportStep(); $event->status = erSTOP; } /** * Returning created by AJAX CSV file * * @param kEvent $event */ function OnGetCSV(&$event) { $export_helper =& $this->Application->recallObject('CSVHelper'); /* @var $export_helper kCSVHelper */ $prefix_special = $export_helper->ExportData('prefix'); $prefix_elems = split('\.|_', $prefix_special, 2); $perm_sections = $this->Application->getUnitOption($prefix_elems[0], 'PermSection'); if(!$this->Application->CheckPermission($perm_sections['main'].'.view')) { $this->Application->Redirect('no_permission'); } $export_helper->GetCSV(); } /** * Enter description here... * * @param kEvent $event */ function OnCSVImportBegin(&$event) { $prefix_special = $this->Application->GetVar('PrefixSpecial'); $prefix_elems = split('\.|_', $prefix_special, 2); $perm_sections = $this->Application->getUnitOption($prefix_elems[0], 'PermSection'); if(!$this->Application->CheckPermission($perm_sections['main'].'.add') && !$this->Application->CheckPermission($perm_sections['main'].'.edit')) { $this->Application->Redirect('no_permission'); } $object =& $event->getObject( Array('skip_autoload' => true) ); /* @var $object kDBItem */ $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); $field_values = array_shift($items_info); $object->SetFieldsFromHash($field_values); $event->redirect = false; $result = 'required'; if($object->GetDBField('ImportFile')) { $import_helper =& $this->Application->recallObject('CSVHelper'); /* @var $import_helper kCSVHelper */ $import_helper->PrefixSpecial = $this->Application->GetVar('PrefixSpecial'); $import_helper->grid = $this->Application->GetVar('grid'); $result = $import_helper->ImportStart( $object->GetField('ImportFile', 'file_paths') ); if($result === true) { $event->redirect = $this->Application->GetVar('next_template'); $event->SetRedirectParam('PrefixSpecial', $this->Application->GetVar('PrefixSpecial')); $event->SetRedirectParam('grid', $this->Application->GetVar('grid')); } } if($event->redirect === false) { $object->SetError('ImportFile', $result); $event->status = erFAIL; } } /** * Enter description here... * * @param kEvent $event */ function OnCSVImportStep(&$event) { $import_helper =& $this->Application->recallObject('CSVHelper'); /* @var $export_helper kCSVHelper */ $prefix_special = $import_helper->ImportData('prefix'); $prefix_elems = split('\.|_', $prefix_special, 2); $perm_sections = $this->Application->getUnitOption($prefix_elems[0], 'PermSection'); if(!$this->Application->CheckPermission($perm_sections['main'].'.add') && !$this->Application->CheckPermission($perm_sections['main'].'.edit')) { $this->Application->Redirect('no_permission'); } $import_helper->ImportStep(); $event->status = erSTOP; } /** * Shows unit config filename, where requested prefix is defined * * @param kEvent $event */ function OnCheckPrefixConfig(&$event) { $prefix = $this->Application->GetVar('config_prefix'); $config_file = $this->Application->UnitConfigReader->prefixFiles[$prefix]; ob_start(); ?> Unit Config of "<?php echo $prefix; ?>" prefix Close Window

Prefix:
Unit Config: highlightString($config_file); ?>

Close Window
status = erSTOP; } function OnUploadFile(&$event) { // Flash uploader does NOT send correct cookies, so we need to make our own check $cookie_name = 'adm_'.$this->Application->ConfigValue('SessionCookieName'); $this->Application->HttpQuery->Cookie['cookies_on'] = 1; $this->Application->HttpQuery->Cookie[$cookie_name] = $this->Application->GetVar('flashsid'); $admin_ses =& $this->Application->recallObject('Session.admin'); /* @var $admin_ses Session */ $user = $admin_ses->RecallVar('user_id'); $perm_helper =& $this->Application->recallObject('PermissionsHelper'); /* @var $perm_helper kPermissionsHelper */ /*if() { $prefix_special = $this->Application->GetVar('PrefixSpecial'); $prefix_elems = split('\.|_', $prefix_special, 2); $perm_sections = $this->Application->getUnitOption($prefix_elems[0], 'PermSection'); $section = $perm_sections['main']; } else {*/ $section = $event->getSection(); /*}*/ if ($this->Application->GetVar('t') != 'import/import_start' && !$perm_helper->CheckUserPermission($user, $section.'.add') && !$perm_helper->CheckUserPermission($user, $section.'.edit')) { $event->status = erPERM_FAIL; header('HTTP/1.0 403 You don\'t have permissions to upload'); exit; return; } if (!$cookie_name) $cookie_name = 'sid'; $value = $this->Application->GetVar('Filedata'); if (!$value) return ; $tmp_path = WRITEABLE.'/tmp/'; $fname = $value['name']; $id = $this->Application->GetVar('id'); if ($id) $fname = $id.'_'.$fname; if (!is_writable($tmp_path)) { header('HTTP/1.0 500 Write permissions not set on the server'); exit; } move_uploaded_file($value['tmp_name'], $tmp_path.$fname); exit; } function OnDropTempTablesByWID(&$event) { $sid = $this->Application->GetSID(); $wid = $this->Application->GetVar('m_wid'); $tables = $this->Conn->GetCol('SHOW TABLES'); $mask_edit_table = '/'.TABLE_PREFIX.'ses_'.$sid.'_'.$wid.'_edit_(.*)$/'; foreach($tables as $table) { if( preg_match($mask_edit_table,$table,$rets) ) { $this->Conn->Query('DROP TABLE IF EXISTS '.$table); } } echo 'OK'; $event->status = erSTOP; return ; } }