Blog |

drapeko

@author drapeko

Archive for May, 2009

PHP, Create filesystem hierarchical array

with 2 comments

You have a straightforward list of directories. Your task is to create an hierarchical array of these directories. How can you do it? The simplest way is presented below.

You have an array of directories (straightforward list of directories):


	 $array = array(
		 '/home/drapeko/var',
		 '/home/drapeko/var/y',
		 '/home/drapeko',
		 '/home',
		 '/var/libexec'
	 );

And you would like to transform this array to hierarchy of directories:


 $array = array (
     'home' => array (
         'drapeko' => array (
             'var' => array (
                 'y' => array()
             )
         )
     ),
     'var' => array(
         'libexec' => array()
     )
 );

How can you do it?

First of all the below function will help us.


/**
 * This function converts real filesystem path to the string array representation.
 *
 * for example,
 * '/home/drapeko/var/y   will be converted to  ['home']['drapeko']['var']['y']
 *
 *
 * @param $path 		realpath of the directory
 * @return string		string array representation of the path
 */
function pathToArrayStr($path) {
     $res_path = str_replace(
          array(':/', ':\\', '/', '\\', DIRECTORY_SEPARATOR), '/', $path
     );
     // if the first or last symbol is '/' delete it (e.g. for linux)
     $res_path = preg_replace(array("/^\//", "/\/$/"), '', $res_path);
     // create string
     $res_path = '[\''.str_replace('/', '\'][\'', $res_path).'\']';

     return $res_path;
}

It simply converts the real path of the file to array string representation.

How can you use this function? I know it looks a little bit confusing. But it’s quite simple. Consider the example below:


 $result = array();
 $check = array();
 foreach($array as $val) {
 	$str = pathToArrayStr($val);
 	foreach($check as $ck) {
 		if (strpos($ck, $str) !== false) {
 			continue 2;
 		}
 	}
 	$check[] = $str;
 	eval('$result'.$str.' = array();');
 }
print_r($result);

Heh, how do you find it? This approach has helped me very much. I hope you will find it useful. :)

  • Share/Save/Bookmark

Written by rdrapeko

May 24th, 2009 at 12:53 am

Posted in Posts

Tagged with , , , ,

PHP Intelligencer, tiny autoload framework

with one comment

Several months ago I wrote an article that included several interesting examples of __autoload function, some autoloading approaches and a tiny script that is able find interfaces/classes and generate arrays of associations among these classes/interfaces, their locations and extended/included classes/interfaces.

http://wp.drapeko.com/2009/03/28/autoloading-in-php/

I widely use this script and I’ve found it really convenient.  I don’t have to think anymore of how to store my classes, what is the structure of the project etc. All this work is done by this script.

But… I have to launch it manually.

I’ve decided to go further and began to create a tiny framework called Intelligencer. This framework will extend functionality of the autoload generator script.

Some Intelligencers features:

  1. It is very tiny and does not depend on other frameworks and external libraries. It’s very easy to integrate it.
  2. It has an ability to store and control inheritance associations and relations between classes/interfaces and their locations.
  3. If something has changed Intelligencer will automatically regenerate (if necessary) lists of associations. It’s really very useful in the development stage.
  4. Intelligencer has a huge number of config settings. It’s flexible.
  5. Intelligencer supports environments. You can easily create your custom environments and switch between them.
  6. You can create several Intelligencers that will be responsible for different parts of your application.
  7. You can work with Intelligencer both on config level and API level.

If you use it, it will do all work for you. It’s an open source. Location -  sourceforge.

The first release will come very soon.

  • Share/Save/Bookmark

Written by rdrapeko

May 20th, 2009 at 10:37 pm

Google Adsense Appeal letter

with 7 comments

I began to use Google Adsense two months ago. One month ago my account was banned. Unfortunately, the first appeal was unsuccessful and I did not get any explanations.  I don’t know the exact reason why I was banned. It makes me laugh but the number of ‘earned’ money was a little bit more than 10 dollars. But I’m really very interested in good relations with Google, that’s why several days ago I appealed for the second time.

I sent a letter below.

Dear Sir/Madam Appeal

I am writing for the second time regarding these misunderstanding. Your
previous letter made me very unhappy and I didn’t expect such a decision to
be made. I am indeed very interested in cooperation with Google and I am
convinced that there was a misunderstanding.

I claim that I have followed all the terms and conditions and I have not
broken any rules/ In particular, I have not place any forbidden content, I
didn’t click on the banners and I have not incited anybody to click on
them. What if somebody did it on purpose so as to deprive me? In that case
I have absolutely no protection against somebody’s bad intentions. My
account has been active only for a week. Is the statistics gathered during
such a short period of time sufficient to conclude this is indeed me who
tried to cheat Google?

I’d hate to lose the opportunity to work with Google and with AdSense in
particular. I have no intention to create other accounts under a different
name. I am not interested in illegal actions or unfair profit.  I am ready
to uphold my rights and prove that. I would really want to have my account
annulled so as to start building the long-term and trustworthy relations
with Google.

I appeal to Google to reconsider the matter.

I look forward to hearing from you,

Yours faithfully,
Roman

Have you been in a similar situations? What is the result?

P.S. I decided to be independent from one particular adv company. I will try to implement one interesting idea very soon in terms of advertisement. Stay in touch. :)

  • Share/Save/Bookmark

Written by rdrapeko

May 16th, 2009 at 5:46 pm

Posted in Posts

Tagged with , , , ,

Connect remotely to Unix/Linux using SSH with X-Windows support

without comments

Situation: you would like to connect remotely using SSH protocol to Linux/Unix OS from Windows environment and to run remotely a program that uses X-Windows (e.g. Eclipse) .

How can you do it?

  1. Download SSH client. The most popular is free PuTTY client (http://www.chiark.greenend.org.uk/~sgtatham/putty/)
  2. Secondly download and install X-Windows server for your Windows environment. I suggest to use xming (http://sourceforge.net/projects/xming). xming installation tips are available here: http://gears.aset.psu.edu/hpc/guides/xming/
  3. Open Putty, go to Connection->SSH->X11 and enable X11 forwarding
  4. Connect to the remote host and launch the program
  • Share/Save/Bookmark

Written by rdrapeko

May 16th, 2009 at 2:31 pm

Promotions (coming soon)