Creating a selection list for 1C configurations published as web applications

New task related to 1C. Given: 1C server, it has several 1C configurations published as web applications. To enter the configuration you need to follow the links like:

http://192.168.1.2/config

where config is the name of the configuration under which it is published.

As I mentioned above, there are several configurations, and some users use two or even three at once, and choosing configurations by poking around in browser bookmarks is not always convenient.

Having examined the contents of the web server configuration files (in my case, this is the apache2 server) and the data directory (again, in my case, /var/www). The settings for each 1C configuration were located in their own directories and the names of the directories coincided with the path specified in the url. The address http://192.168.1.2/config corresponded to the path /var/www/config. I came to the conclusion that I could easily organize the configuration selection by writing one small script, which should be placed in the default data directory configured by the creators of my Linux distribution (/var/www/html). The script, according to my idea, should sort through the directories and look in them for the settings file of the published web application (default.vrd). When it detects settings, it will add a link to the configuration to the list that will be shown to the user. I decided to write in PHP, since everything for this was pre-installed on the server.

Next, having opened the settings file (default.vrd) from one of the configurations, which turned out to be in xml format, I came to the conclusion that I could also add a small description of each configuration for the convenience of users. Since you can use a comment in the xml format, I decided to add it to the file as the last line () and enter the name of the configuration in it. If the settings file does not contain a comment as the last line, the user will be shown the name of the directory with the configuration.

So, the task is clear, the solutions have been determined. You can start writing code. After some time, the following script was ready:

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1С конфигурации на сервере</title>
    <link rel="shortcut icon" href="https://habr.com/favicon.ico">
    <style>
<!-- поле для творчества -->
    <style>
</head>
<body>
<div class="container">
<h1>1С:Предприятие 8.3</h1>
<ul class="list">
<?php

function getTitle($file)
{
    $fileContent=file($file);
    $lastLine = end($fileContent);
    preg_match('/<!--(.*)-->/', $lastLine, $matches);
    if (isset($matches[1])){
	return trim($matches[1]);
    }
    return false;
}

The getTitle($file) function is responsible for obtaining a configuration description from the settings file that will be shown to the user. If the last line of the file does not contain a comment, then the function will return a logical false; if a comment is present, then a description substring will be extracted from it and returned as a result.

    $baseDir="/var/www";
    $dirList = scandir($baseDir);

We define the $baseDir variable – this is the root directory of the web server. We also immediately scan the root directory, thereby receiving a $dirList array with subdirectories in which we will look for files containing settings for published configurations (default.vrd files).

foreach ($dirList as $value) {
    if ($value === '.' || $value === '..') {
        continue;
    }
    $vrdFile = $baseDir.DIRECTORY_SEPARATOR.$value.DIRECTORY_SEPARATOR.'default.vrd';
    if (file_exists($vrdFile)) {
        $title = getTitle($vrdFile);
        if (!$title) {
            $title = $value;
        }
        echo '<li><a href="https://habr.com/".$value.'" target="_blank">База:'.$title.'</a></li>'.PHP_EOL;
    }
}
?>
</ul>
</div>
</body>
</html>

We go through the list of subdirectories from the $dirList array. If we encounter directories “.” or “..”, then we skip them, and in the rest we look for the publication settings file. When we find the default.vrd file, we look for a comment in it using the getTitle function defined above, create a link (url) to enter the 1C configuration and add an html list element.

In this simple way, users received a convenient choice, and I reduced my headache, since remembering one IP address is easier than remembering the entire link, and at any time there may be more configurations.

I won’t show the styling of the html elements, I’ll leave you room for creativity, and what I came up with will be in the title of this article.

I hope that my experience will be useful to someone. Thank you for reading to the end.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *