Skip to content

My PHP , Wordpress and Linux Lab

Archive

Category: PHP

A couple of users thankfully pointed out the issues with the paths, as i am stuck with windows as a development platform,  case sensitive directory names mess things up on Linux. So very minor updates to a few files.

Also PLEASE read the README file provided in the zip for the instructions on how to set up the application.  PHP Viz needs a database for storing the configurations and settings, so that must be created by using the SCHEMA.sql provided.

Download PHP Viz 0.2.1

Full text searches have been the one part which would always worry me in any project which will hold a reasonable amount of data with a decent traffic.  Not anymore!

I have been using Sphinx for a while and i have been really impressed. Easy deployment, easy integration and its pretty flexible as well. I cant say that i have explored everything it has to offer but whatever I needed it to do, it surely did for me.

Among other features you can use it to rank , filter ,  group , sort and query to targeted columns. It even supports “Sounds like” searches ….. wonderful! .  With several different matching and ranking modes , ones got to be right for you.

The PHP Client is a pure PHP implementation, so no deployment headaches. The PHP client API is included in the download package, hook it up with your DB wrapper and you are ready to battle with those Full Text Searches. What it would do is .. it would return to you the ranked record IDs, along with weights and then you can bring the full record of that ID directly from MySQL. Response times from Sphinx are amazing, So far I haven’t seen it take more than 0.7 seconds to return the resultset. Best of all, you can scale as much as you need, without any problems.

Here are the key features taken from their site directly:

  • high indexing speed (upto 10 MB/sec on modern CPUs)
  • high search speed (avg query is under 0.1 sec on 2-4 GB text collections)
  • high scalability (upto 100 GB of text, upto 100 M documents on a single CPU)
  • supports distributed searching (since v.0.9.6)
  • supports MySQL natively (MyISAM and InnoDB tables are both supported)
  • supports phrase searching
  • supports phrase proximity ranking, providing good relevance
  • supports English and Russian stemming
  • supports any number of document fields (weights can be changed on the fly)
  • supports document groups
  • supports stopwords
  • supports different search modes (”match all”, “match phrase” and “match any” as of v.0.9.5)
  • generic XML interface which greatly simplifies custom integration
  • pure-PHP (ie. NO module compiling etc) search client API

Amazing stuff! So if you are in the business of developing high volume traffic and/or data sites … you have to check it out for sure! Its available for both Linux and Windows platforms!

2 Thumbs Up for these guys!

It brings me tremendous pleasure to announce the first public release of PHP Viz.

PHP Viz is an open source PHP code generator , which is also written in PHP 5 , released under the GPL license. It allows you to create MySQL based sites rather than just pages. The resulting sites follow a very simplified MVC pattern, which allows re-usability and partitioning of code.

The best feature PHP Viz provides is the linking of Primary keys to Foreign keys and allows you to display records from linked tables. You can create modules for adding , editing , deleting and viewing data. Once you have created your modules, you can create pages which hold one or more of your modules. Reusing these modules in custom code is pretty easy as well and allows you create independant entities which can be plugged into any page.

The application is in a very early stage of development and I am hoping to get alot of feedback, based on which I will prioritize the features that need to go in there.  Soon I will make available an online demo version as well for quick viewing of all the features.

Please read the README file found in the zip to find the install instructions.

Please feel free to put in any comments and feedback in the comments area.

Download PHP Viz 0.2


Update : Sorry guys the link was broken earlier , thanks to some nifty logic in Wordpress , should be working now!

Some screenshots below:

Main Screen

Site Modules And Pages

Module Settings

Generated Site Login Screen

Generated Site View Data Screen


UPDATE: Please turn on “short open tags” in php.ini .. i will try to remove all the short tags in the next release.  My apologies for this oversight.

I have been so busy over the past 2 years that I almost forgot about this code generator that I started to write. But recently i resumed work on this and created a new version from scratch.  and I am very happy with the results so far :)

PHP Viz is written in PHP and lets you create MySQL driven PHP sites. A prime feature it provides is the support to link up multiple tables (based on foreign and primary keys) and create views from them.

The application lets you create “sites” rather than simple pages. You can even have Login protection on your site if you have a users table in there somewhere in your database. All the basic functions are there , you can create tabular views, single record view , add and edit view. The generated code is very clean , simple and based on a very very simplified MVC architecture. This allows for expansion and code re-usability with ease.

The application code itself  is very rough at this point and i just want to see some feedback from the community to make sure that i am heading in the right direction. I am hoping to add some more advanced level features down the line and hoping to make this an every day tool for the PHP developer.

PHP Viz 0.2 should be publicly available within the next week. I am hoping that the next Site Admin you make is built by PHP Viz :)

SVN and MySQL Schema Synchronization

Sep 29
Posted by : Sabeen Malik in MySQL, PHP, SVN, Web Development

When I started to work in a virtual team comprising of 6 developers , I felt that the most time consuming task was syncing the database changes amongst team members. As we were using SVN , we didn’t have to worry about the files but the DB schema was always an issue. So I started to look into something solid which we could couple of with SVN client in order to make this process as smooth as possible. Blame it on my search capabilities or eye sight but honestly i didn’t come across something I liked. So i devised a mechanism which might be far from ideal but which is working well for us. Keep in mind that we all are working in Windows environment (as the client is using Windows server) , so some steps might be slightly different for people in the Linux camp.

Here is what I did :

Step 1:
I located a ready made script which would let me sync 2 databases. I found a well written class by “Diogo Resende” here

Step 2:
I modified the class , so that instead of actually syncing the databases , it would just return the queries which it is going to run in order to sync the databases.

Step 3:
Now I created an exact copy of my current database , lets call this database “test”. The called the copy i created “svn_test”.  Keep in mind that my working database is still “test”. After this point I never touched “svn_test”.

Step 4:
I created a small database on my online server with a table called “changes” with  change_id , change_set , submit_time fields. “change_id” was set to be primary and auto increment. I created a one page management script for this and placed the script on the remote server to be easily accessible by all. Lets say the location of this script is http://something.com/changes_list.php. This script lists all the records in descending order. I can edit , delete and add records from this script as well.

Step 5:
I created a PHP script called “pre_hook.php”, which would compare “test” and “svn_test” and return to me all the queries. These queries were stored in the remote database table called “changes” , it would store the last returned “change_id” in a file called “change-id.txt”. It would also generate a file called “changed.txt”, if there was no changes ,  nothing would be submited to the remote DB  and  the  file “changed.txt” will be deleted if it were present. This script also created a file called “run.bat” , if there were any new queries , the script would insert explorer “http://something.com/changes_list.php in the “run.bat” file , otherwise it would just insert “exit” in it.

Step 6 :
I created another script called “post_hook.php” , it would check if “changed.txt” was present , if so it would replace the database “svn_test” with “test”.

Step 7:
I created yet another script called “post_update.php” , which would connect to the remote database and tried to find query the records which were entered after its last run. The script stores the highest “change_id” it got in a text file. So next time it would only try to query for records which are higher than the stored “change_id”. If the script got any new records from the remote database. It would parse the queries in each record and run them on database “test”.

Step 8:
As we all use Tortoise SVN , I created 4 hooks. A hook on “start commit” , this hook would run the “pre_hook.php” script. Hook 2 was created on “pre commit” which would run the “run.bat” file created by pre_hook.php. Hook 3 was created on “post commit”, this would run the “post_hook.php” script. Hook 4 was created to on “post update” , this would run the “post_update.php” script.

How it works (or is supposed to work):
After all the above steps. I goto the database “test” and make a change to xyz table. Now remember that “svn_test” DOESNT have this change. So now i continue to work on php scripts and I decide to commit to SVN. So what will happen is when the commit starts the “pre_hook.php” will gather all the schema changes (if any) , send them to the remote MySQL database , place the link to the remote script I deployed at http://something.com/changes_list.php inside the “run.bat” file. BEFORE actually sending the files to the SVN , the” run.bat” would be executed and it would show me the list of changes that were just submitted , so that i can view them manually as well and adjust if needed , which is rare . After the commit is complete , the “test” database is copied over “svn_test” by “post_hook.php”. Next time someone updates the repository , the “post_update.php” script will kick in after the update is complete and return all the new changes submitted to the remote database. Now that persons database has the changes I made to mine as well. The remote DB management script also lets us input other queries as we may please.  So that on next update everyone can get the changes those queries reflect.

This system is not perfect, but it saves us plenty of time.  I am still thinking/searching for a better solution , which takes care of the changes in the data as well but for now this is what I have.

Hoping that this would help someone else as well.

PHP Code Cleaner : Download

Jul 25
Posted by : Sabeen Malik in PHP

Alot of people have been asking me regarding the installable version so I just zipped up the files I had on my hard drive for PHP Code Cleaner. The files include a script which helps you clean up multiple files at the same time. Just go through the “readme.txt”.

Please understand that the code is a very raw attempt on solving the problem of cleaning and indentation , so it does have its problems. Please report the bugs you can find and i will try to fix them as soon as i find some time.

Download

PHP Code Cleaner and Indenter

Dec 19
Posted by : Sabeen Malik in PHP

Online Code Cleaner Demo

With six years of experience with PHP , I have been ‘blessed’ with working with code written by really lazy people , who just didn’t think it was important at all to indent their code or comment it or make the opening and ending curly braces align.

And sometimes our favorite FTP clients would add God knows why those silly annoying empty lines and the code would keep getting bigger and bigger.

And I am sure that I am not alone in my frustrations.

That is why is am writing this script which will try to take care of all that.

Basically it will try to achieve the following goals:

  • Indent your code with tabs.
  • Put a comment block at the start of class definitions or functions.
  • Provide you with the facility to align curly brackets.
  • Remove unwanted lines from the code.
  • Put some spacing in blocks for improved readability.
  • Replaces short open tags with proper <?php tags
  • If a PHP block contains only one line , it will put the whole block in one line to improve readability.

Please provide your valuable feedback to improve this script. I will be soon releasing a full script which will clean the code in all PHP files in a directory. Stay tuned!

Online Code Cleaner Demo

Web based FTP Sync Tool written in PHP

Dec 4
Posted by : Sabeen Malik in Linux, Misc, MySQL, PHP

FTP Sync Jobs ScreenFTP Sync 0.9

PURPOSE:
To upload files which have changed to your production server , automating the task of comparing which files have changed. You simply create a FTP job
in the script , run the job and it will upload the changed files for you.

REQUIREMENTS:
FTP functions are required for this script to work.
PHP 4 Or 5
MySQL 4+

INSTALL:
Open dbinc.php and put in the database connection variables in there. Once done run installer.php from your browser and you are all set to go.
Its advised to remove installer.php once you have finished installation.

SECURITY:
It if advised that the admin creates users and allocates jobs to them. Also the admin should block access to the database and script files
from general staff.

INFO:
We made this script for our internal use , thought it might be useful for some of you out there. It was a tiring job to find which files needed to be uploaded
to the main server after development on local server. Thus i created this script , where you can create jobs and upload folders and files which were changed.

The decision to upload happens on 3 rules:

1 : you can provide time and date in the job page , files which were created after that time will be uploaded.

2 : you can ask the script to match the time against the server file , the script would calculate the time offset and calculate if the file has
changed since last upload.

3 : The script also keeps a log , if the file changed after last upload , it will upload the file.

You can exclude the files you dont want to upload, for instance you wont want to upload and overwrite config.php or something like that. So you
can create the skip list. The skip list is the path of the file from the source directory you have provided.

For instance if you have a source directory called “/www/something/upload/” , and you dont want to upload the file “/www/something/upload/config.php”
or the folder “/www/something/upload/users/images” then you would put in something like this:

config.php
users/images

Remember no trailing slash for folders.

You need to have writable permissions for the logs folder , also if you want to utlize the copy permissions feature you will need the BcMath library
compiled with PHP.

The admin can run any job , but if you want to have users which can run specific jobs only please utilize the users section , in which a user can
run all jobs or only specific jobs.

Same goes for the jobs , you can set an attribute to allow all users to run the job.

This code has been tested on PHP 5 and PHP 4.4.

You are free to modify the code as you please. The code is released in the public domain under the GPL license.

Enjoy!

PHP :: Serialize and Unserialize Alternative

Nov 17
Posted by : Sabeen Malik in PHP

For quite sometime now I have been really annoyed by the unreliability of serialize and unserialize , when you would have a large string or some some special characters in there that wouldnt turn back into variables. So finally after not finding a decent alternative by someone else ( maybe I am only one suffering from the problem????? ) , I decided to write my own two functions.

These two are working great for me , hope they serve some purpose to someone else. I am sure there is room for improvement here, please send in any suggestions or ideas to incorporate.

  1.  
  2.         /// Usage
  3.         ////– first parameter is the var name , second is the var itself
  4.         $string = makeXML(’somevarname’ , $somevarname);
  5.  
  6.         ////— extractVars takes the above string as parameter ,
  7.         ////— and returns an arrray with variable name as key and variable itself
  8.         ///—  as value , i have used extract() to take all vars into code.
  9.         extract(extractVars($string));
  10.  
  11.  
  12.         function extractVars($xml , $name= , $type=){
  13.                 $reg="!<xmlvar name=\’(.*?)\’ type=’(.*?)’>(.*?)</xmlvar name=\’\\1\’>!s";
  14.                 preg_match_all($reg , $xml , $matches);
  15.  
  16.                 foreach($matches[1] as $index=>$key){
  17.                         $optname = $key;
  18.                         $opttype = $matches[2][$index];
  19.                         $optval = $matches[3][$index];
  20.  
  21.                         $optxpl = explode("::" , $optname);
  22.                         $optname = $optxpl[count($optxpl)-1];
  23.  
  24.                         if($opttype == ‘object’){
  25.                                 $bigarr[$optname] = (object)extractVars($optval , $optname , $opttype);
  26.                         }elseif($opttype==‘array’){
  27.                                 $bigarr[$optname] = extractVars($optval , $optname , $opttype);
  28.                         }else{
  29.                                 $bigarr[$optname] = $optval;
  30.                         }
  31.                 }
  32.  
  33.                 return $bigarr;
  34.         }
  35.  
  36.  
  37.  
  38.         function makeXML($name , $var="" , $parent = NULL ){
  39.                 if(!is_null($parent)) $parent = $parent."::";
  40.  
  41.                 if(is_object($var)){
  42.                         $xml .= "<xmlvar name=’$parent$name’ type=’object’>";
  43.                         foreach($var as $key=>$val){
  44.                                 $xml .= makeXML($key , $val , $parent.$name);
  45.                         }
  46.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
  47.                 }elseif(is_array($var)){
  48.                         $xml .= "<xmlvar name=’$parent$name’ type=’array’>";
  49.                         foreach($var as $key=>$val){
  50.                                 $xml .= $tab.makeXML($key , $val , $parent.$name);
  51.                         }
  52.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
  53.                 }elseif(is_bool($var)){
  54.                         $xml .= "<xmlvar name=’$parent$name’ type=’bool’>";
  55.                         $xml .= "$var";
  56.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
  57.                 }else{
  58.                         $xml .= "<xmlvar name=’$parent$name’ type=’string’>";
  59.                         $xml .= "$var";
  60.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
  61.                 }
  62.                 return $xml;
  63.         }
  64.  

Affordable PHP 5 and MySQL 5 Hosting Available

Aug 25
Posted by : Sabeen Malik in MySQL, PHP, Web World

Decode IT has announced PHP 5 and MySQL 5 hosting.  After signup customers will be given a choice between version 4 and 5. The servers are dual core servers with high uptime and reliability. Small packages are ideal for Wordpress installations which can be done with clicks via the Fantastico control panel. All accounts contain the all time great CPanel account manager.

Here is the overview….
50 MB Hosting – $2.00 pm
100 MB Hosting – $4.00 pm
200 MB Hosting – $8.00 pm
500 MB Hosting – $20.00 pm
1000 MB Hosting – $35.00 pm

You can find the details of the packages here.

Enjoy!!