I am looking for some type of framework or package or library for PHP that would manage my PHP web apps configuration parameters. Is there such a thing? Rather than repeatedly opening a file and reading and parsing the contents, is there something already existing I can use?
Take a look at Zend_Config, you have there an xml version, but also ini, json or yaml.
update
I don't understand exactly your usage scenario and the main idea of Zend_Config is to remove the configuration data from various parts of the application and to put it in only one place. But you can adapt it to your use. If you have a config file with your users to be parsed, you can update that config independently and you application does not have to know how many users are in the file.
I would have liked to have used Zend_Config but just couldn't get it to work. I settled for the less programatically sexy alternative : SimpleXMLElement. Sometimes, in programming as in life, we choose to settle for the practical rather than the sexy. A Volkswagen instead of a Ferrari. Sarah Rodriguez instead of Pamela Anderson. Now I need to find something for Javascript. Any suggestions?
Related
I am a new learner to node. I am getting some issues with node. Like if it is really required to use any framework/module like 'Express' etc with node?
Cant we just simply create a web application similar to PHP or asp etc. In PHP we can simply create files & we know where to put our files & rest of the things handled by server itself.
PHP Frameworks : CodeIgniter, CakePHP (Tell us where to put code, means MVC etc)
Node Frameworks : Express , Fab.js (Tell us without using these we cant proceed easily)
For example i want to create a simple Profile edit form for a student. In php i can simply create a form in HTML, display already filled values by user in fields & can add some validations on it. But with node,how we can do it?
Where actually should i place my server side files?
Do i really require any framework(express) to do that in node?
Can i write a mixture of html & Node.js together as we usually do in PHP using < ?php ?>
Like if it is really required to use any framework/module like 'Express' etc with node?
No. As with any framework, it just makes life easier for you by doing some common things for you.
In PHP we can simply create files & we know where to put our files & rest of the things handled by server itself.
Typical Node apps have the server built in and have to do URL routing themselves (this is what Express does).
Typical (small) PHP apps use a separate server and let it handle their routing for them.
You can do that with Node, but you need to have some way to link the server to the specific JavaScript program you want to run. You could write all your JavaScript programs to conform with the CGI specification, but CGI isn't very efficient and (my impression is that) most people choosing to use Node do so for performance reasons.
Where actually should i place my server side files?
This is up to you. There are lots of approaches you can take to organise your code.
Can i write a mixture of html & Node.js together as we usually do in PHP using < ?php ?>
PHP makes it easy to mix your business logic with your display logic. This makes it very easy to knock out trivial programs. It also makes it very easy to make non-trivial programs a maintenance nightmare.
As far as I know, Node doesn't come with a templating language but plenty are available through NPM. I'm not aware of any that let you splurge raw JavaScript into the middle of templates, but that isn't a feature I'd consider desirable.
you can put your node files anywhere, it's common to have the main entry point to your app at root level of your project. Then libraries generally are out in a subdirectory, 3rd party modules are managed through npm and install to node_modules by default. node_modules is on node path by default so they can be referenced without paths ie require('async'), assuming you have installed npm async
you don't need any framework, node provides all the tools necessary to create a server in its standard library
node has a couple of templating libraries, I'm pretty sure most of them allow you to mix html and a couple of expressions that evaluate to js, like loops and conditionals. Some might allow arbitrary js, but I don't have any experience with them
I've recently inherited a large PHP application with NO objects/modules/namespaces...only a lot of files containing functions.
Of course, there is a LOT of dependencies (and all files and almost always included).
I'm looking for a tool that could analyse the files and generate a dependencies graph. It would then be easier to detect independent files/set of files and re-factor the whole thing.
So far the best solution I've found would be to write a CodeSniffer sniff to detect all functions calls and then use that to generate the graph.
It seems something useful for other, so I'm sure tools already exists for it.
What would you recommend ?
I think that the best solution is use a doc generat + grapviz, PHPDocumentor looks to have a Grapviz extension at https://github.com/phpDocumentor/GraphViz
This is a example made with PHPDocumentor:
http://demo.phpdoc.org/Clean/graphs/classes.svg
Too you can use a hierarchical profiler like xhprof (https://github.com/facebook/xhprof), this can draw a tree of all call to functions from a execution.
A example form xhprof draw done by Graphviz
I could recommend a lightweight project I wrote few days ago. Basically I had a 300+ files PHP project and I wanted to detect what files do these files require/include and vice-versa. Moreover, I wanted to check for each individual file what files does this file requires/includes (directly or indirectly, ie. via file inheritance) and vice-versa: what are the files that include this particular file. For any combination of these I wanted an interactive dependency graph (base on file inclusion and not on class/function calls/usage).
Check out the project's sandbox and its source code.
Note that the whole thing was written in only 2 days so don't judge it
too harsh. What's important is that it's doing its job!
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
We run multiple Windows/IIS/.Net sites (up to 30+ sites per server). Each site is customized for the individual customer via a configuration file that contains the settings.
I am tasked with writing a small tool that will 'grep' all of the config files on a certain server for a particular config setting (or settings) and return the values for a nice tabled web page display. It will save many groups lots of time, especially since most groups don't have access to production servers, but they need to know how a customer is currently configured.
I have working code that finds all .config files from a starting path, I can easily extend this to do my grep'ing. Here are the challenges:
I want to aggregate this data from MULTIPLE servers. That means, the tool will be hosted on its own server -- and will make calls to a list of servers.
I'm limited to using .NET/ASP on the actual servers (they won't install PHP on IIS), but I'm writing the tool in PHP.
PROPOSED DESIGN: From my vantage point, I'm thinking the best way to accomplish this is to write my PHP tool and have it make AJAX or CURL requests to ASP scripts that live on each server in the list. Each ASP script could do the recursive directory parsing to find the config files and individually grep the files for the data, and return it in the RESPONSE.
Is that the best way to accomplish this? Should the ASP or PHP side do the 'heavy lifting'? Is their a recommended data format I should be using to pass the data.
Any ideas or samples would be great. If you need more info, I can provide!
Thanks!
Update: Here's an example of a config. Its a basic ASP file that gets included in other scripts.
custConfig1 = " 8,9,6:5:5 "
custConfig2 = " On "
I think you're bang on using PHP for the "receiving" script, and pretty sure you have that in hand.
Based on the format of your example config file, you could use ExecuteGlobal in classic ASP to load each file as you loop through them in your recursive directory lookup. Then you can use the custConfig1 et al. names in your script. e.g. (pseudo)
for each file
output("custConfig1") = custConfig1
next
Return what you need as JSON using a handy library and then do all the "hard" work of collating it and outputting it in PHP.
Yes, "grep" (if by that you mean importing a text file and using reg expressions to navigate it) isn't the best solution, in my humble opinion, use either JSON or XML as the format, and use PHP's built in XML or JSON tools.
JSON: http://php.net/manual/en/book.json.php
XML: http://php.net/manual/en/book.simplexml.php
You could use the DOM to navigate XML alternatively to SimpleXML, but SimpleXML is easier to learn (again, in my opinion) and will work for your needs.
Im making a small experiment. In the old days, most people would save configuration files in a php file as a bunch of constants or a class with a loooot of attributes. Now, we see projects like symfony using yaml or something else. My question is:
If you could can pick from three different ways to store your config files: xml, yaml or php files, which one would you pick? Most important: Why?
Thanks for your help
For me it depends on who is going to touch that configuration.
If it is developers, then PHP files are the best, as they do not require any additional parsing.
If it is technical users (for example, other developers, or sysadmins) then there is choice: complicated config file would better go with a structured file, like XML or YAML, as there is less chance to break the PHP code if something goes wrong (and you can report a specific parsing error with suggestions how to fix). Simple choices can be written with PHP (but here if someone forgets a quote character the program will fail with strange errors, or with no errors at all if errors go to the log only!).
If it is final users... then no configuration files should be exposed at all, in my opinion. You need to provide an installer which will handle everything (and generate the machine-readable configuration files or write things to the db).