I somehow thought that it would be nice to make a module for managing configs, so that you can download the daddy via ftp, set passwords and then use it in any CMS / scripts / applications. What I would like from him: universality (storage of any variables), speed, ease, protected "admin" editor, methods for changing-saving. Because the search was not pleased, but the work of craftsmen leaves much to be desired, I decided to do it myself.

Immediate questions:

  1. in what format to store configs? (we need speed and versatility, hand editing is absolutely not important, because it is clearly not ini and obviously not xml, I look towards serialize, waiting for criticism or approval)
  2. Does anyone have any ideas for the "value of function" type? For now, I’m thinking of making a static array, so that at each access it will not produce functions, and set variables with a special type (see the appendix). What for? For example, to auto-install paths. As an option - stupidly eval'it code (interesting arguments for / against).
  3. Does anyone know a thing like winbinder , but more pleasant to handle? I would like to do an independent editor for this case, and I don’t really want to write a parser-serializer from scratch = /
  4. and the question is clearly experienced hackers (in the original sense) - how to stop parsing and interpreting the script in a certain place, or simply insert a non-executable area into the file? (see attachment 2)

Application:

if (!in_array($function_id, $static_array)) $static_array[$function_id] = create_function('', $function_code); return $static_array[$function_id](); 

Appendix 2:

 <? // исполняемая область exit; ?> неисполняемая область, набор случайных символов, в которой может встретиться "<?" или "<?php", а также непечатаемые символы (%00-%09 например) есть ли способ где-то в районе "exit;" остановить парсинг, чтобы этот файл отработал без ошибок? Без обработки неисполняемой области. Может быть что-то связанное с HEREDOC, мне чутье подсказывает. <? // исполняемая область, ее можно исключить ?> 

PS: if it goes, of course, I will share the result)

UPDATE: joyful update!) 4 question solved by myself. If anyone is interested, this is an idea for php-sfx archives. If you create a heredoc tag from sha1 (mt_rand (0, 100000). $ _ SERVER ['SERVER_NAME']. 'Still a bunch of variables'), you can even shove archives into each other without steaming. Those who like the idea - use)

 <? if (!$f = fopen(__FILE__, 'rb')) die('Pls open read rights'); $CONTENT = ''; while (!feof($f) && (trim($s = fgets($f)) != '<<<PHPSFX8BAE970D3871669D96F9DF91C41735A14F8873A6')) {}; while (!feof($f) && (trim($s = fgets($f)) != 'PHPSFX8BAE970D3871669D96F9DF91C41735A14F8873A6;')) $CONTENT .= $s; fclose($f); echo '<pre>'.htmlspecialchars($CONTENT).'</pre>'; echo '<pre>'.htmlspecialchars(file_get_contents(__FILE__)).'</pre>'; exit; <<<PHPSFX8BAE970D3871669D96F9DF91C41735A14F8873A6 ?>aas das d<?asd $@#^ @W SC GDG SDG Q@#% ! %@ @#% @# @#@@@@@@@@@@@@@@@@@<?ASDasdasd PHPSFX8BAE970D3871669D96F9DF91C41735A14F8873A6; ?> 

This is a test script, demo here .

    3 answers 3

    For more data that will be podgrudzhatsya all the time I used and use JSON. This approach has its advantages and disadvantages, but I like the simplest processing of data most of all, of course, there are no big problems with simpleXML, but still everything is not so simple:

      $configData = json_decode(file_get_contents('filename.json')); 

    Serialize - I personally do not approve, justified by the fact that until now, serialization and deserialization in php are not well implemented (who did not write over 2k lines of code on this topic - better not argue =))

    With json, no such problems were found, and, if necessary, it would be possible to load the JSON config in the admin panel for editing (it would not be difficult to format the formatting here, and even an editor, which by the way, takes you very much there is not enough code both on the server and on the client)

    As for exit, is it so crucial to use exit instead of exit() ? =)

    PS: I apologize right away if I wrote something out of the topic somewhere, I simply, frankly, did not understand the point at all =) But, nevertheless, I advised something


    upd

    As before, I don’t understand the essence, but if the amount of data is really large and really fast access is needed, you can use the database and Engine Memory.

    Here, of course, the code is not so trivial (it’s necessary to somehow check if the table is not empty and if empty, create it with a new one)


    The speed of the database with a table with engine memory (the table is stored directly in the RAM) does not shine? O_o

    @ Sh4dow - still shine like it =)

    • and the array will return not the array, but the stdClass object , while unserialize (file_get_contents ($ file)); immediately returns the array of data. And about the serialized data, I wrote what exactly will depend on the number of parameters. - Artem
    • @Shrek with objects, normal people can work the same way as with arrays; D Moreover (this is just for labor) the "chosen ones" are able to convert a stdClass object into an array in the first line: DDD - Zowie
    • Yes, it's clear, I’m just talking about the number of actions, but I don’t write that JSON isn’t working at all :) - Artem
    • Do you think that casting is a heavy action and hash is fundamentally different from a stdClass object? Teach materiel =) - Zowie
    • : P: P: P: P - Artem
    1. Flat array generated by var_export() , or formed by hands. In the presence of any opkecher this will be the fastest and most versatile way.

       <?php return array( 'key1' => 'val1', 'keyN' => 'valN', ); 
    2. Taking into account claim 1, you can simply store the circuit. Although the idea of ​​storing functions in the config file seems to me at least strange.

    3. -
    4. For this there is a special construction __halt_compiler()

       <?php $fp = fopen(__FILE__, 'rb'); fseek($fp, __COMPILER_HALT_OFFSET__); $content = stream_get_contents($fp); fclose($fp); // string(158) "Эти данные не будут парсится PHP, // в т.ч. теги <?php, переменные $someVar // или любые другие данные." var_dump($content); __halt_compiler();Эти данные не будут парсится PHP, в т.ч. теги <?php, переменные $someVar или любые другие данные. 

    I also advise you to read about Phar

    PS

    <<<PHPSFX8BAE970D3871669D96F9DF91C41735A14F8873A6

    It will not work, because variables are substituted into heredoc. However, nowdoc can be used instead.

    • According to claim 1, the piece is good, but unfortunately it lost in speed, json:serialize:var_export on an array of 50k values ​​and 10k iterations (assemble-disassemble) it turned out to be a time ratio of 1:1.2:3.4 . Yes, I was also surprised) p.2 - I still have a lot of sites on php 5.0+, so it's dangerous to watch 5.3. Well, taking into account claim 1, it will not work out well. p.4 - that's a big thank you for it) If it's not a secret, where does such knowledge come from? Do you read the documentation like a bedtime story or are there still certificate courses? And about the PS I will object - it will work, because the site is after the exit and will never be executed, checked already) - Sh4dow
    • > the piece is good, but unfortunately in speed it lost Export variables and should not work quickly. And why do you need export speed? You will not generate it 100 times per second. And here reading in the presence of the opkecher will be as fast as possible. Actually, it will not even read the file itself, the array will be in the memory of the opkescher as bytecode. > If not a secret, from where such knowledge? From there, from where everyone has it :) Experience, php.net, google> because the site is after the exit and never executed I agree, exit did not take into account. - Ilya Pirogov
    • But such things with Halt (p.4) cannot be thrust into each other without treatment = / sad. - Sh4dow

    Well, I would look in the direction of serialized arrays + just arrays. More specifically, it will depend on the number of values ​​in the config.

    XML is clearly an inconvenient format for the config. Ini files still did not go.

    For quick access, you can make an ini file for markup, and inside each section, draw serialize arrays.