Posts filed under 'Coding'

Two minor PHP bugs filed today

First, set_error_handler() doesn’t return false if no previous handler was set but returns NULL.

Second, it’s not possible to restore the PHP original error_handler in PHP with set_error_handler().

I hope I did my research well, but as far as I could see, those two are correct according the current CVS code.

Add comment November 4th, 2005

Extending SimpleXML

Christian Stocker quoted Rob Richards from the PHP internals mailing list on how to extend SimpleXML. Someone needed more information of the structure of the XML document, for example the name of the node. On the list it was suggested to use the DOM extension which can, without too much overhead, import SimpleXML document (which can be re-imported to SimpleXML again).

The solution adviced on the list was to derive a class from SimpleXMLElement, pass this class to the simplexml_load_* function as second parameter.

Add comment November 1st, 2005

getParent patch for tidy

This is a small patch for current CVS HEAD in PHP which enables the tidy getParent() function which seemed to be forgotten.

Update on 24th of March, 2006: seems it’s necessary to ask the PHP developer with whom to sleep to get a patch finally commited.

Index: php_tidy.h
===================================================================
RCS file: /repository/php-src/ext/tidy/php_tidy.h,v
retrieving revision 1.25
diff -u -r1.25 php_tidy.h
--- php_tidy.h  25 Apr 2005 20:46:16 -0000      1.25
+++ php_tidy.h  15 Jul 2005 09:59:44 -0000
@@ -89,6 +89,7 @@
TIDY_NODE_METHOD(isJste);
TIDY_NODE_METHOD(isAsp);
TIDY_NODE_METHOD(isPhp);
+TIDY_NODE_METHOD(getParent);

ZEND_BEGIN_MODULE_GLOBALS(tidy)
char *default_config;
Index: tidy.c
===================================================================
RCS file: /repository/php-src/ext/tidy/tidy.c,v
retrieving revision 1.65
diff -u -r1.65 tidy.c
--- tidy.c      25 Apr 2005 20:46:16 -0000      1.65
+++ tidy.c      15 Jul 2005 09:59:45 -0000
@@ -291,6 +291,7 @@
TIDY_NODE_ME(isJste, NULL)
TIDY_NODE_ME(isAsp, NULL)
TIDY_NODE_ME(isPhp, NULL)
+       TIDY_NODE_ME(getParent, NULL)
{NULL, NULL, NULL}
};

@@ -1668,6 +1669,30 @@
}
/* }}} */

+/* {{{ proto boolean tidyNode::getParent()
+   Returns the parent node if available or NULL */
+TIDY_NODE_METHOD(getParent)
+{
+       TidyNode        parent_node;
+       PHPTidyObj *newobj;
+       TIDY_FETCH_ONLY_OBJECT;
+
+       parent_node = tidyGetParent(obj->node);
+       if(parent_node) {
+               tidy_instanciate(tidy_ce_node, return_value TSRMLS_CC);
+               newobj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
+               newobj->node = parent_node;
+               newobj->type = is_node;
+               newobj->ptdoc = obj->ptdoc;
+               newobj->ptdoc->ref_count++;
+               tidy_add_default_properties(newobj, is_node TSRMLS_CC);
+       } else {
+               ZVAL_NULL(return_value);
+       }
+}
+/* }}} */
+
+
static void _php_tidy_register_nodetypes(INIT_FUNC_ARGS)
{
TIDY_NODE_CONST(ROOT, Root);

Add comment August 12th, 2005

Never trust user input, even not PHP_SELF

As Sean Coates points out, never trust user data any kind of. He gives a good example on what could slip the developers eye: the PHP_SELF variable. Read his blog for examples and how to cope with it.

Add comment May 24th, 2005

PHP upgrading fun with ob_gzhandler

For some people it’s fun upgrading php. For me it’s work. This time I had the pleasure of upgrading 4.2.2 to 4.3.9. Unfortunately it did break the behaviour of ob_gzhandler.

For some reason it was decided to modify the behaviour of ob_gzhandler when the client sends no Accept-Encoding header. Prior to this change, the function simple returned the data unmodified if it wasn’t able to detect any support encoding.

After this change, it simply returns false. So this is pretty bad when you use it this way

echo ob_gzhandler($data);

because now all you get back is a blank page.

What client does not send an accept encoding? As it seems some wierd Internet Explorer configration insisting to use the HTTP/1.0 protocol. Or wget. Now tell me this is something people verify nowadays when upgrading a server-side software?

Add comment April 6th, 2005

session_destroy and custom session save handler bug

When using a custom session save handler in PHP and you’re calling session_destroy() you’re effictively also killing your session save handlers. The only solution right now seems to call session_set_save_handler again after session_destroy, if you further want to use the session_functions.

See PHP bug http://bugs.php.net/32330 for details.

Add comment March 16th, 2005

MediaWiki: remember textarea scrollbar when previewing pages

I wrote a little patch for current MediaWiki CVS which remembers the position of the scrollbar of the textarea when previewing content.

Since I wrote quite a few pages in our local MediaWiki based Intranet, I stumbled over one thing which really annoyed me: when previewing pages, I had to search where I was last time in the textarea. Although you can break down things with editing only specific sections, I always dealt with many content inside the textarea. This patch aims to enhance the useability using javascript to at least remember the position of the scrollbar when the preview button is hit. Read the bug report for how it works, its limits and which browser do not support it.

Bug report with patch: http://bugzilla.wikimedia.org/show_bug.cgi?id=1499

Add comment February 23rd, 2005

401 Auth for MediaWiki

I wrote a small external authentication script for use with MediaWiki. It should be prepended in front of every PHP script ran with MediaWiki. This can easily done with auto_prepend_file.

New users cannot register themselves this way, however for my environment this was needed. I’m using WikiMedia for an intranet and it should provide external access for existing users, 401 authenticated against the MediaWiki user database and automatic login. In the session the state, whether the user has successfully authenticated, is recorded and kept. If he isn’t, he’s challenged with a 401. The nice thing is that, upon successful first time login, the script emulates the login process by carefully setting the environment variables which are usually set when a real login is done. I initially wanted to use the native MediaWiki methods, but the abstraction was not practically useable for me.

Source

Add comment February 16th, 2005

Updated ChelloStatus class

The updated ChelloStatus class encapsulates the actions better and this time I’ll only server a phps link, no crap code pasting here: ChelloStatus class.

Achja, requires PHP5.

Add comment February 14th, 2005

Chello Bandwith Status

With this PHP script you can easily check your bandwith status if you’re a customer of chello. This is most likely only useable from within austria/vienna.

Run it from the console and it returns ‘green’, ‘yellow’, ‘red’ or ‘unknown’. The first three map to the traffic light image. If the state can’t be detected, ‘unknown’ is returned.

Note:

  • Use at your own risk
  • Requires the PEAR HTTP_Client package (pear install -o HTTP_Client)
  • Has a weak detection mechansim (regular expression search for the traffic light image)
  • Will break as soon as they change their site in any area this script relies on
  • Usage: create a new ChelloStatus instance and call getStatus() with the proper authentication information
  • Uh .. WordPress doesn’t nicely support code fragments …

[php]
require_once 'HTTP/Client.php';

class ChelloStatus {
const FORM_USERNAME_NAME    = 'username';
const FORM_PASSWORD_NAME    = 'password';
const FORM_SUBMIT_NAME        = 'submit';
const FORM_SUBMIT_VALUE        = 'absenden';
const URL_LOGIN                = 'http://ecc.chello.at/cgi-bin/uwfe/casc/casc_start.cgi';
const URL_ACCOUNT            = 'http://ecc.chello.at/cgi-bin/uwfe/odv/xacct.cgi?no=0&new=1';
private $oHttpClient;
private $sUsername;
private $sPassword;
function __construct() {
$this->oHttpClient = new HTTP_Client;
}
public function getStatus($sUsername, $sPassword) {
$this->sUsername = $sUsername;
$this->sPassword = $sPassword;

return $this->getChelloStatus();
}
private function getChelloStatus() {
$this->oHttpClient->post(
ChelloStatus::URL_LOGIN,
array(
ChelloStatus::FORM_USERNAME_NAME => $this->sUsername,
ChelloStatus::FORM_PASSWORD_NAME => $this->sPassword,
ChelloStatus::FORM_SUBMIT_NAME => ChelloStatus::FORM_SUBMIT_VALUE
)
);
$this->oHttpClient->get(ChelloStatus::URL_ACCOUNT);
$aLastResponses = array_pop($this->oHttpClient->_responses);
return $this->filterStatus($aLastResponses['body']);
}
public function filterStatus($sHhtml) {
$aChelloStatus = array(
'images/xacct/green.gif'     => 'green',
'images/xacct/yellow.gif'     => 'yellow',
'images/xacct/red.gif'         => 'red',
);
$sStateFound = "unknown";
foreach ($aChelloStatus as $sPreg => $sState) {
if (preg_match(";$sPreg;", $sHhtml)) {
$sStateFound = $sState;
break;
}
}
return $sStateFound;
}
}
?>
[/php]

Add comment February 6th, 2005

Next Posts Previous Posts


Calendar

September 2010
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
27282930  

Posts by Month

Posts by Category