Posts

YUI javascript compressor and PHP tags

YUI compressor is very useful and powerful tool for javascript files compression. Many developers compress their static javascript files but what about files with PHP tags? With something like this: var username = "<?php echo $username; ?>"; or like this function() { var some_code_before; <?php if (Acl::check("finance")) { ?> Ext.addShortcut('finance'); <?php } ?> var some_code_after; } First example will work, but YUI will throw an exception on second. And also this piece of javascript code does not pass validation. Lets wrap PHP tags into comments: function() { var some_code_before; /*<?php if (Acl::check("finance")) { ?>*/ Ext.addShortcut('finance'); /*<?php } ?>*/ var some_code_after; } Now this code is valid. But lets try to feed it to YUI: # yui test.js function(){var b;Ext.addShortcut("finance");var a}; PHP tags are stripped away by YUI and ...

How to dump http request headers with PHP under CGI/FastCGI SAPI

Good old days of PHP installation exclusively as an Apache module are gone. Most of the modern PHP installations interact with a web server via FastCGI protocol. For instance nginx webserver and php-fpm daemon. This means that certain apache SAPI functions are not available anymore. One of this functions is apache_request_headers() to dump http headers sent by client. From CGI specification rfc3875, section 4.1.18: Meta-variables with names beginning with "HTTP_" contain values read from the client request header fields, if the protocol used is HTTP. The HTTP header field name is converted to upper case, has all occurrences of "-" replaced with "_" and has "HTTP_" prepended to give the meta-variable name. According to the specification above i wrote a piece of the PHP code that dumps all headers sent by client under any PHP SAPI: foreach ($_SERVER as $key => $value) { if (strpos($key, 'HTTP_') === 0) { $chunks = e...

Using konqueror to navigate through man pages

Image
If you are a KDE user you can use Konqueror for MAN pages navigation, just add "man:" prefix before interested command. For instance enter  man:ssh-copy-id to Konqueror's address bar and enjoy nicely rendered MAN page entry and all browser's features like search results highlighting, hyperlinks to the related MAN entries (yay!). I wrote simple bash script that utilizes man binary functionality but displays MAN page in Konqueror. Put this code to the /usr/local/bin/kman file: #!/bin/sh #Su'ed root console support if [ $UID = 0 ]; then     su - $LOGNAME -c "env DISPLAY=:0 konqueror man:$@" 2>/dev/null & else      konqueror man:$@ 2>/dev/null & fi For autocompletion support add this to the /etc/bash.bashrc.local file: #konqueror man complete ${_def} -F _man_ ${_file} kman Run  kman   ssh-copy-id and have a fun. Tested only in Opensuse.

PHP associative arrays or hashes: in-depth look

Image
Before you start reading i warn you: this article won't help you in every day PHP developing in any way. But if you are naturally interested, like me, in how things work you're welcome to spend your precious time on reading and understanding this article. Still here? Great. Lets try to stay sane after all this low level mumbo-jumbo and start. PHP array or in other terms hash is a combination of the data storing methods called "vector" and "linked list". Vector is a nice aligned structure in the memory, simplest and fastest way to store and access linked pieces of the data because accessing vector offsets functionality is baked right into the modern CPU's. I'm referring to the assembler's offset calculating functionality like MOV [BASE+OFFSET], DATA. Think of the vectors as a numerically indexed PHP array. If you need to access array member you just pass offset to it like this: $array = array("a", "b", "c"); ...

PHP extension development with Linux and Eclipse CDT. Building your environment

Image
It is hard to start with PHP extensions development in Unix environment. It is not enough to be familiar with C programming language also you need at least basic knowledge in packaging, GNU build tools and of course PHP internals. If your desktop is beautified by Linux - there is a good news for you: you don't need to setup and build everything by yourself! You just need to know how to use already configured package for your distribution. Don't even ask what to do if your desktop is crippled by Windows - play minesweeper. This post is intended to give you a good place to start. Everything is shown on example of the Opensuse 11.4, but other distributions are suitable as well. First of all we need to obtain PHP source code. Raw PHP source from php.net can be used but you will need to take care of PHP configuring and all external symbols. Delete stripped php binaries first: # zypper rm php5 next # zypper si php5 this command will download source R...

Curl multi hangs, connection timeout. Asynchronous DNS support

The problem. Curl is a nice library that takes advantage of asynchronous loading. Many developers use CURL in high level languages to load multiple web pages simultaneously. I use curl_multi functions very heavily in my PHP scripts too. But recently i discovered very awkward problem: CURL loads web pages asynchronously but DNS resolution is single threaded! This means that if one of the CURL handles hangs on DNS resolving other handles will hang too and wait for their turn in resolving queue! And if you set curl option "connection timeout" and DNS resolving takes more than timeout you set - all requests will fail! The solution. Starting from version 7.20.0 CURL can handle async DNS resolution using libcares. But cares support isn't enabled by default (at least in Opensuse packages and FreeBSD ports). To take advantage of async DNS resolution rebuild libcurl with --enable-ares option. If your copy of the libcurl is already compiled with async DNS support PHP will repor...

Opensuse linux and D-link DWA-525 (RaLink 3060 based)

Recently i've upgraded wifi network card to D-link DWA-525 based on RaLink 3060 chipset which is wifi-N capable. But my Opensuse 11.4 wasn't able to detect it. There is two ways to solve this problem - first is to download chipset driver from ralink's site ( http://www.ralinktech.com/en/04_support/support.php?sn=501 ), setup, compile it and use. But it seems to be in a beta state because after installation it spams your logs with debug messages (can be disabled by removing -DDBG from config.mk but anyway i don't trust drivers with debug mode turned on by default). Second and proper way in my opinion is to install ralink drivers from repositories. Search for the "ralink-firmware" on  http://software.opensuse.org/search?q=ralink-firmware  and "one click install" will do all the job for you.