PHP Settings / Config File For Variables? - php

I'm currently working on a script for our linux hosting company. We need to store variables such as versions and last update checks in a config.ini file which needs to be accessed and written to according to any updates made by the updater script.
I really need help writing the variables to a config.ini file (and later overwriting or changing the variables when they are updated), creating the config.ini file and reading the variables from the config.ini file.
The variables get stored at different times and not all at once, so one array won't work. I also have if statements, so some variables will be put in the file in certain situations and in other situations, there will be different variables in the file.
Example:
Server1 has game1 installed.
Server2 has game2 installed.
Script checks for game1, then checks database for latest version, compares with the config file. If config file is blank or old, updates and then changes config file version to database version. Repeats again for each game. If the game is not there, no variables stored and skip to the next step.
I'm battling a lot with trying to read and write to the file at separate intervals without overwriting the previous lines.
Would be awesome to get some support.
Regards,
Skowt

I guess you want ideas. Try using yaml, xml, sqlitedb to store your config settings. XML might be bit hard since you have to read and write. To me yaml files seems like a good options. I haven't tried PHP yaml function yet, symfony has a good yaml component.
Another options as I mentioned is to use a sqlitedb might come in handy if you have a lot of complicated configs.

Related

PhpStorm update version text in .env file whenever a file is saved

I'm using PhpStorm to manage many of my client Laravel PHP projects, however as these projects have become larger it's become important to maintain and also show version information inside the system I'm developing.
To do this I'm using the .env file to hold a config called APP_VERSION.
What I would like to do is whenever a file is saved inside PhpStorm - any file - I want to automatically update the value (by incrementing) for this configuration variable.
However I can't seem to find a way to do this, or if it's even possible.
I have looked at File Watchers but this doesn't seem to solve the problem I'm having in that it only allows certain file types and even then it wants to run a compiler (such as a Sass or Less compiler, etc.) which is certainly not what I'm after.
Is there any way to do this, without me having to manually increment the minor version number with each file I update?
Update
So by using an adaptation of the answer by #LazyOne I created a small script which executes the necessary text update to the file in question.
I used a custom File Watcher which was setup against changes to any file type with a custom scope created to exclude certain files and folders that I didn't want watched.
End result, when I save a file that is within a valid scope, the script I wrote executes to update my file with the necessary change.
PhpStorm does not have such functionality (including other IDEA-based IDEs). There are some tickets asking for such functionality .. but it will definitely not work for your scenario (the use case there is different).
You will have to do it "manually". By this I mean:
either actual manually editing the file (as in your question)...
or automate it by writing some script (e.g. in PHP) that would do it for you and then just execute it either on demand (e.g. External Tools or manually) or via File Watcher (File Watcher is basically (simply speaking) External Tools that is run on file modification -- you can run any program there, even your own shell/batch script).
Such script will open your .env file, find the right line and edit the value there -- little bit of file parsing job -- nothing super heavy.
Hint: Laravel has artisan .. and one of the commands is key:generate (which alters .env file -- at very least it is run at very least once when you create new Laravel app). You may do it in similar fashion -- do your script as artisan command (so that you have full Laravel power behind it) and just call it when needed (File Watcher or whatever).
P.S. Instead of editing .env file (which is more environment specific rather than global setting, which version info is) .. why not store this in custom config file (e.g. config/version.php) which will contain that info only and therefore will be much easier to alter or even generate from scratch (template) as it has much simple structure and therefore no need to maintain other existing info there (which you have to do with .env files)?
At the end of the day -- there is no big difference for you/programmers between calling env('APP_VERSION') and config('version.app_version') to get that info in your app.
I'm not sure if that's possible directly within PHPStorm. You could, however, write yourself a small PHP script which monitors the specified directory for file changes and when one happens your script can make the necessary changes in the file you want.
Try looking at inotify for more info.

Read user's environment variable in php script

I'm trying to query environment variables that i have set my .bashrc file (i'm running Ubuntu 14.04) in a PHP scripts that run's under Apache server.
When i query the getenv('MY_VAR_NAME') or $_ENV['MY_VAR_NAME'] while accessing the page, it seems that those variables are missing..
My guess is: when the script is been executed the user is www-data, so 'MY_VAR_NAME' is not accessible.
Is there any procedures / best practices to this kind of problems ?
Thanks
My guess is: when the script is been executed the user is www-data, so 'MY_VAR_NAME' is not accessible.
Your guess is correct. :)
Is there any procedures / best practices to this kind of problems ?
What most programmers would do is to have a configuration file containing these variables. You would store the configuration file in some area where the PHP scripts could get to it -- whether that's in a home directory somewhere, in /etc. or in some other place such as in the web root or a directory near it.
Different frameworks take different approaches to the format of the configuration files -- some are PHP scripts, some are YAML files, some are windows/DOS format INI files, some are XML, some are JSON, etc.
Personally I like the idea of doing this:
Store in a simple INI file only the configuration required to access the database.
Store all of the remaining configuration in a database table, and build an editor for the parts that you need to be able to edit.
Cache the contents of the configuration database table in memcached.
Whichever method you use is up to you, however, and will vary depending on the needs of your application.

File and Folder Attributes - Programming API

I knew that PHP is able to read file content by different ways, for example: fread, file_get_contents, file, readfile, etc.
Currently, I am looking for an API that can read real index of files and folders in specific partition or folder, for example:
drive d:\ in windows contains three folders (folder1, folder2, folder3), and each folder contains some files, we can get these directory structure using PHP (opendir, scandir, readdir, etc) and list them as I want, however, windows saved file and folder names inside hard-disk with their attributes (size, last modified, created on, etc).
How I can read hard-disc using PHP and retrieving all file and folder attributes for a specific path?
for instance, if we consider last modified time we can use (filemtime()) function, but this attribute not saved inside the file, its saved some where else inside hard-drive, other attributes also saved in other location not inside the file.
When windows user copying file from flash-drive to local hard, windows will copy all file and folder attributes and saves them inside local hard drive. When using PHP for copying file, it depends on OS to handle this job, its not native support (as I think) for file and folder operations.
Do you have any idea?
There are many recovery program that uses this technology for reading hard-drive indexes, however, for PHP: I cant find any source for this problem.
Applications if I get correct answer:
I can check if such file securely deleted from my hard-drive? I can create secure delete application using PHP, or clearing hard-drive indexes for a given file.
Your help appreciated.
Problems with the proposition
The attributes of files, such as timestamps, permission flags etc, are stored in the file system (FAT, NTFS, Ext3 etc). As you say some of them can be read using PHPs different file and directory methods, but they all act through the OS file system abstraction and cant have access to block level information on the disk, such as what precise byte on disk stores the archive flag for file X. The whole point of the OS and FS is to abstract away this information from the user/client programs.
As suggested there are external tools, written in c or similar, that does have this access and that you can call from inside PHP. If you want a 'native' PHP way of doing this you'll have to compile a c extension for PHP that exposes these low level functions to you.
I'd say external tools is the way to go if you want to stick with PHP but for the task at hand, as far as we can see from your description, I'd go with another language that has more low level access. Like C or C++. PHP is a high level language for HTML pre processing and as such is a poor choice for low level system programming.
Practical advice
After looking through the PHP documentation and assorted third party libraries:
An of the shelf solution for reading file system information on a file allocation table level doesn't exist for PHP. The lowest level you get is the fstat() function, and that is not very far for what you want.
External tools
No mater exactly what you want to do there is probably a small binary that does it. PHP can be integrated with these programs, as suggested elsewhere, via the exec() function. This is probably the easiest approach for you unless you have serious amounts of time and/or development resources to devote to this problem.
Wrapping a library
There are libraries that solves this problem for you, written in low level languages. An open source library can be wrapped with SWIG to expose it to PHP. This will give you access to the low level methods you need, but it's a non trivial task. These kind of libraries also often require sole access to the device while they work on it, something that is difficult to achieve in most normal operating environments.
Note also that you will probably need a library per file system. Microsofts VFAT extension to FAT12/16/32 requiers a licens to use. So if you want to work with FAT and have files with long names (not 8.3 format) you'll have to fork up some dough to be legit.
Low level implementation
A last middle ground would be to write your own CLI tool that uses an external library to access the low level FS functions. You can then use exec() from inside PHP to interact with your own implementation.
This might be a reasonable path if you cant find an existing tool that solves your problem and you are not willing to spend the time to wrap a library.
In closing
You give a very narrow problem description with little to go on as for what the application is about. A broader discussion (in another forum) might yield better results since the problem might be better solved in another way entirely.
I found something on PHP.net which appears to do what you want:
http://php.net/manual/en/function.readdir.php#103418
Edit: I mis-understood the question. Attributes such as the last modified time, last accessed date and the like are stored in the file systems master file table. As far as I can tell, this isn't accessible with PHP, and if you were to write your own method to do this then you'd also have to account for different file systems as they all handle the storage of these attributes in their own unique way.
It could be that to get all of the information you're looking for is not possible with PHP without writing some form of extension to PHP itself.
Edit 2: Upon researching a little more...
http://php.net/manual/en/function.fileinode.php
This function could be an interesting one to look at.
Well if I understand correctly you just want to securely delete a file. You can just call [shred][1]
[1]: http://linux.die.net/man/1/shred via system or exec if you are on linux and you are good to go

Open source software - How to setup files

I'm in the middle of making my own custom forum system software. Much like phpbb, mybb, vbulletin, etc. except it's obviously quite less advanced. It's just a personal project for myself and I've run into some problems since I've never had to develop something that can be repackaged for others.
The file structure is as follows:
So, config.php is the end all be all of including files. It has the database connection information, it instantiates my database class as well, and none of the function files require/include any files since they'll always be accessed where config.php is required.
HERE'S THe QUESTION!
However I'm running into simple but very annoying problems, for example I call a function in config.php towards the top that checks the users cookies values and makes sure they all belong to the same user, and if not it deletes the cookies. However, it has to be after the database files require. And things like, a variable declared in config.php isn't always accessible, so sometimes I have to declare it in the header files.
Seems like it's not much of a question, but I guess it's just asking for how I can include/require in general without running into issues.
As a general note, most people don't mix config variables and code in one file. If you look at popular open source packages like Wordpress, Config.php just has config variables set. No code.
If you're using certain functions in anything more than a "one off" situation, you may want to consider putting them into your main class - that way they're available as needed.
#James is right, separate your config file. You can include it inside an "application.php" required file (so it's available globally).
I have run into a situation where I absolutely needed HTTP Header information prior to page build. Though it seemed a little backward, the solution was to call that file first, then include the application.php file. Checking for a cookie should be fine.
In another situation, #include('myStubbonPricing.php') was the answer. I'm not an advocate of error suppression, but in my case it only outputted a shipping rate (if the zip code was entered). To my defense !isset and the like would not fix the problem due to an XML request/response scenario.

using includes cross domain that contain php code - failing

I have a series of web sites all hosted on the same server with different domains. I want to host some common PHP scrips and then be able to call these from the other domains.
Im am a bit fresh with my php so pls excuse code attempts - I have tried iterations of the following which may try and help you understand what I am aiming for!
from within php tags ...
include('http://www.mydomain/common_include.php?show_section=$section');
$show_section = $_GET['show_section'];
include('http://www.mydomain/common_include.php');//Then $show_section would be available to the included file/cod
Finally I have tried pulling in the include which contains a function then trying to run that include from the parent script.
I would much prefer to keep this PHP
orientated rather than getting
involved with the server (file
systems etc (but I can change
permissions etc)
I can but would prefer not to just upload the same library to each of the domains separately
I understand PHP is run on the server hence maybe problematic to include scripts across onto another server.
Thanks in advance.
#
EDIT
OK OK - I get that its bad practice so will not do it....THANKS VERY MUCH FOR THE QUICK ANSWERS.
However is there any other recommendations of how to esentially show this basic php app on all of the sites with out haveing to add the files to the root of each site? Just to prevent massive script duplication...(thinking out loud call the scripts in from a db or anyother soloutions)
Again thanks for your assistance
That would be a huge security risk if you could just include remote PHP files to your own projects. The PHP gets parsed before the server sends it to you so cross-domain includes would only contain the output the script generates. The only way to include PHP files so that they can be executed is via local filesystem.
If you look at PHP.net's documentation about include, you can find this:
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Which pretty much explains the whole thing.
The root of the original question seemed to be the poster's concern about using a PHP script or plugin on multiple sites and then having an onerous task each time it needs to be updated. While trying to include PHP files across sites is a bad idea, it is a better plan to structure your script to be as self contained as possible. Keep the entire plugin contained in one directory.... and ensure your function calls to utilize it are as well formed as possible - clean, well named functions, uniform naming conventions and a well thought out plan for what parameters each function needs. Avoid using global variables.
Ideally you should then have quite an easy time each time you need to update the plugin/script in all locations. You can even set up an automated process that will upload the new directory containing the plugin to each site replacing the old one. And the function calls within your code should rarely if ever change.
If your script is big enough you might implement an automatic update process like the more recent versions of Wordpress use. Click a button and it updates itself. In the past, updating a dozen sites running Wordpress (as an example) was a massive pain.
That is very bad practice.
Actually you're including not PHP but just HTML code.
Include files, not urls. It is possible for the same server.
Just use absolute path to these files.
Apart from the fact that it's a bad practice you should first check if include allows URLs if you really want to do that.
If however all the sites that need to use the script, you could put the script somewhere in a directory accessible by the user that executes php and add that dir to the php.ini include_path property (can also be done at runtime)
(Or you could create a php extension and load it as extension)
If you have root rights on that server, you could just use absolute path from filesystem root, but most hostings won't let you do this.

Categories