/** * Save the configuration to the config snippet */ private function load_snippet() { midcom::get('auth')->request_sudo('org.openpsa.core'); $lib_snippetdir = new midcom_db_snippetdir(); $lib_snippetdir->get_by_path("/org.openpsa.cache"); if (!$lib_snippetdir->guid) { $sd = new midcom_db_snippetdir(); $sd->name = 'org.openpsa.cache'; if (!$sd->create()) { throw new midcom_error("Failed to create snippetdir /org.openpsa.cache: " . midcom_connection::get_error_string()); } $lib_snippetdir = new midcom_db_snippetdir($sd->guid); } $this->snippet = new midcom_db_snippet(); $this->snippet->get_by_path("/org.openpsa.cache/siteconfig"); if ($this->snippet->id == false) { $this->snippet = new midcom_db_snippet(); $this->snippet->up = $lib_snippetdir->id; $this->snippet->name = 'siteconfig'; $this->snippet->code = "//AUTO-GENERATED BY org_openpsa_core_siteconfig\n"; $this->snippet->create(); $this->initialize_site_structure(); } midcom::get('auth')->drop_sudo(); eval("\$array = array ( {$this->snippet->code}\n );"); $this->data = $array; }
private function _save_configuration() { $sg_snippetdir = new midcom_db_snippetdir(); $sg_snippetdir->get_by_path($GLOBALS['midcom_config']['midcom_sgconfig_basedir']); if ($sg_snippetdir->id == false) { $sd = new midcom_db_snippetdir(); $sd->up = 0; $sd->name = $GLOBALS['midcom_config']['midcom_sgconfig_basedir']; if (!$sd->create()) { throw new midcom_error("Failed to create {$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}" . midcom_connection::get_error_string()); } $sg_snippetdir = new midcom_db_snippetdir($sd->guid); unset($sd); } $lib_snippetdir = new midcom_db_snippetdir(); $lib_snippetdir->get_by_path($GLOBALS['midcom_config']['midcom_sgconfig_basedir'] . "/" . $this->_component_name); if ($lib_snippetdir->id == false) { $sd = new midcom_db_snippetdir(); $sd->up = $sg_snippetdir->id; $sd->name = $this->_component_name; if (!$sd->create()) { throw new midcom_error("Failed to create {$this->_component_name}" . midcom_connection::get_error_string()); } $lib_snippetdir = new midcom_db_snippetdir($sd->guid); unset($sd); } $snippet = new midcom_db_snippet(); $snippet->get_by_path($GLOBALS['midcom_config']['midcom_sgconfig_basedir'] . "/" . $this->_component_name . "/config"); if ($snippet->id == false) { $sn = new midcom_db_snippet(); $sn->up = $lib_snippetdir->id; $sn->name = "config"; if (!$sn->create()) { throw new midcom_error("Failed to create config snippet" . midcom_connection::get_error_string()); } $snippet = new midcom_db_snippet($sn->id); } $snippet->code = $this->_get_config($this->_controller); if ($snippet->code == '' || !$snippet->code) { throw new midcom_error("code-init content generation failed."); } return $snippet->update(); }
private function read_snippetdir($path, $parent_id) { $snippetdir_name = basename($path); $object_qb = midcom_db_snippetdir::new_query_builder(); $object_qb->add_constraint('up', '=', $parent_id); $object_qb->add_constraint('name', '=', $snippetdir_name); if ($object_qb->count() == 0) { // New snippetdir $snippetdir = new midcom_db_snippetdir(); $snippetdir->up = $parent_id; $snippetdir->name = $snippetdir_name; if (!$snippetdir->create()) { return false; } } else { $snippetdirs = $object_qb->execute(); $snippetdir = $snippetdirs[0]; } $directory = dir($path); $foldernames = array(); $filenames = array(); while (false !== ($entry = $directory->read())) { if (substr($entry, 0, 1) == '.') { // Ignore dotfiles continue; } if (is_dir("{$path}/{$entry}")) { // Recurse deeper $this->read_snippetdir("{$path}/{$entry}", $snippetdir->id); $foldernames[] = $entry; continue; } // Check file type $filename_parts = explode('.', $entry); if (count($filename_parts) < 2) { continue; } $snippet_name = $filename_parts[0]; $field = false; switch ($filename_parts[count($filename_parts) - 1]) { case 'php': $field = 'code'; break; case 'txt': $field = 'doc'; break; } if (!$field) { continue; } $filenames[] = $snippet_name; // Deal with element $file_contents = file_get_contents("{$path}/{$entry}"); $encoding = mb_detect_encoding($file_contents); if ($encoding != 'UTF-8') { $file_contents = @iconv($encoding, 'UTF-8', $file_contents); } $qb = midcom_db_snippet::new_query_builder(); $qb->add_constraint('up', '=', $snippetdir->id); $qb->add_constraint('name', '=', $snippet_name); if ($qb->count() == 0) { // New element $snippet = new midcom_db_snippet(); $snippet->up = $snippetdir->id; $snippet->name = $snippet_name; $snippet->{$field} = $file_contents; $snippet->create(); continue; } $snippets = $qb->execute(); $snippet = $snippets[0]; // Update existing elements only if they have actually changed if ($snippet->{$field} != $file_contents) { $snippet->{$field} = $file_contents; $snippet->update(); } } $directory->close(); if ($this->delete_missing) { // Then delete files and folders that are in DB but not in the importing folder $this->delete_missing_folders($foldernames, $snippetdir->id); $this->delete_missing_files($filenames, $snippetdir->id); } }
/** * Save configuration values to a topic as "serialized" array * * @return boolean */ private function _save_snippet($config) { $sg_snippetdir = new midcom_db_snippetdir(); $sg_snippetdir->get_by_path($GLOBALS['midcom_config']['midcom_sgconfig_basedir']); if (!$sg_snippetdir->guid) { // Create SG config snippetdir $sd = new midcom_db_snippetdir(); $sd->up = 0; $sd->name = $GLOBALS['midcom_config']['midcom_sgconfig_basedir']; // remove leading slash from name $sd->name = preg_replace("/^\\//", "", $sd->name); if (!$sd->create()) { throw new midcom_error("Failed to create snippetdir {$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}: " . midcom_connection::get_error_string()); } $sg_snippetdir = new midcom_db_snippetdir($sd->guid); } $lib_snippetdir = new midcom_db_snippetdir(); $lib_snippetdir->get_by_path("{$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}/{$this->_request_data['name']}"); if (!$lib_snippetdir->guid) { $sd = new midcom_db_snippetdir(); $sd->up = $sg_snippetdir->id; $sd->name = $this->_request_data['name']; if (!$sd->create()) { throw new midcom_error("Failed to create snippetdir {$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}/{$data['name']}: " . midcom_connection::get_error_string()); } $lib_snippetdir = new midcom_db_snippetdir($sd->guid); } $snippet = new midcom_db_snippet(); $snippet->get_by_path("{$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}/{$this->_request_data['name']}/config"); if ($snippet->id == false) { $sn = new midcom_db_snippet(); $sn->up = $lib_snippetdir->id; $sn->name = 'config'; $sn->code = $config; return $sn->create(); } $snippet->code = $config; return $snippet->update(); }