DOMNodeList as an Array
By admin | August 31, 2008
When using getElementsByTagName, it returns a DOMNodeList which you can use with foreach, but you can’t use array indexes to access elements.
For example, you can use the following code to the contents of the first “test” tag:
<?php
$file = 'z.xml';
$xmlfile = new DOMDocument();
@$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
$i = 0;
foreach($iplist as $v) {
if($i == 0) {
var_dump($v->nodeValue);
}
$i++;
}
?>
Use this as z.xml:
<?xml version="1.0" ?><test>Testing: hello, world</test>
So as you can see, the $iplist variable is acting like an array (via foreach behaviour). But try this code:
<?php
$file = 'z.xml';
$xmlfile = new DOMDocument();
@$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
var_dump($iplist[0]->nodeValue);
?>
The error “Fatal error: Cannot use object of type DOMNodeList as array in /usr/dev/php5/error.php on line 6” should occur. To fix this, use $iplist->item(0) instead of $iplist[0], though you can still use DOMNodeList with foreach. The following code should work:
<?php
$file = 'z.xml';
$xmlfile = new DOMDocument();
@$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
var_dump($iplist->item(0)->nodeValue);
?>
If you found this article helpful or useful, please help Compdigitec by spreading the word or by leaving a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: PHP | 12 Comments »
The “Gtk-CRITICAL gtk_text_buffer_emit_insert assertion” warning in PHP-GTK2
By admin | August 30, 2008
If your internationalized PHP-GTK2 apps have ever failed to work, you probably received the following message in the terminal before:
Gtk-CRITICAL **: gtk_text_buffer_emit_insert: assertion `g_utf8_validate (text, len, NULL)' failed
This means that you have been using or passing non-UTF8 (good chance that it is CP1252 on Windows) strings to PHP-GTK2 classes and methods, such as GtkWindow, GtkLabel or GtkTextBuffer. To fix this, you should use iconv to convert those non-UTF8 strings. Below is a list of commonly used encodings:
- English – UTF-8, windows-1252, ISO-8859-1
- Chinese, Simplified – GB2312
- Chinese, Traditional – Big5
- Spanish – ISO-8859-1, windows-1252
- French – windows-1252, ISO-8859-1
- German – windows-1252, ISO-8859-1
- Greek – windows-1253, ISO-8859-7
- Arabic – ISO-8859-6
- Hebrew – windows-1255
- Romanian – ISO-8856-16
To use this list, take the encoding and put it in parameter 1. See below for examples:
<?php
// Convert from Chinese, Simplified
$result = iconv(“GB2312”,“UTF-8”,$source);
// Convert from Greek
$result = iconv(“ISO-8859-7”,“UTF-8”,$source);
// Convert from Arabic
$result = iconv(“ISO-8859-6”,“UTF-8”,$source);
// Convert from French, German, and other Western languages
$result = iconv(“windows-1252”,“UTF-8”,$source);
?>
If, as an exception, that you don’t have the iconv extension (included on most PHP-GTK2 setups), you can change the php-gtk.codepage directive. However, this is not flexible and can only be used for one language.
If you found this article helpful or useful, please help Compdigitec by spreading the word or by leaving a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: PHP | 5 Comments »
WordPress author email blacklist patch
By admin | August 29, 2008
If you’ve ever needed to block a single comment spammer using multiple IPs, referrers and urls, but the same email, here’s a patch for WordPress 2.5. This patch is released under the GNU General Public License.
Add the following code to wp-comments-post.php after the line “$comment_content = trim($_POST[‘comment’]);” (line 30 on v2.5):
// ****** Start of comment email patch by Compdigitec ******
$name = 'comment_author_email_blacklist.txt';
$blacklist_email = ''; // ** You can change the above blacklist file
$flag = true;
if(file_exists($name) && is_readable($name)) {
$ble = trim(file_get_contents($name));
$blacklist_email = explode("\n",$ble);
foreach($blacklist_email as $value) {
if($comment_author_email == $value) {
$flag = false;
}
}
if($flag === false) {
wp_die(__('Sorry, you are on our spam blacklist and therefore can not post comments.'));
}
} else {
// Not readable.
echo __("<!-- Warning: Comment Email Blacklist could not be read. -->");
}
// ****** End of comment email patch by Compdigitec ******
Next, create a file called comment_author_email_blacklist.txt (or your custom filename if you changed it above). In the file, add your email addresses to blacklist, one per line. Don’t forget to add testblacklist@example.com to test it.
If the patch has been installed, log out if you are logged in as the site admin. Then go to any post and try to post a comment (make sure there is some content, even if it is nonsense) with the email as “testblacklist@example.com” (without the quotes). You should be confronted with the following dialog:
If you do, congratulations! You have successfully installed the WordPress author email patch by Compdigitec. You can now proceed to adding more blacklist emails to your blacklist.
Troubleshooting
- “<!– Warning: Comment Email Blacklist could not be read. –>” appears when you view the source every time you post a comment. Self-explaining – you need to create your blacklist file or make your blacklist file readable.
If you found the patch for WordPress helpful or useful, please help Compdigitec by spreading the word. Also, please also take a look at our main site, our wiki and our forums. Don’t forget to subscribe to Compdigitec Labs for more intersting articles!
Topics: Internet | 7 Comments »
Geany – the ultimate IDE for programming
By admin | August 28, 2008
Geany (Wikipedia article) is one of the best open source, multi-platform (supports Windows, Linux, Mac and BSD) and multi-language IDE/text editors. It also supports a selection of development commands and supports lots of languages/formats (see note 1). It also supports some unusual file formats such as Makefiles, ini files and SQL files.
Installing/Upgrading Geany (Ubuntu+Debian)
sudo apt-get install geany
(See package geany in Ubuntu, package geany in Debian)
Removing Geany (Ubuntu+Debian)
sudo apt-get remove geany
Known Issues
- Known to randomly crash after saving file (Ubuntu 8.04)
Notes:
Topics: Linux, Windows | 1 Comment »
Binaries for rarcrack 0.2
By admin | August 27, 2008
Update (2009-01-24): You may have been looking for the Windows binaries.
Rarcrack is a lightweight and powerful tool for cracking lost or forgotten RAR passwords. However, (again), there is no already made package, so here is one premade for you.
- Download: .deb for Debian and Ubuntu (version 0.2, 33KB)
- Download: .rpm for Red Hat or Mandriva (version 0.2, 35KB)
If you wish to compile and install it yourself, follow these steps:
- Download http://internap.dl.sourceforge.net/sourceforge/rarcrack/rarcrack-0.2.tar.bz2
- Untarbz2 the tarball – “tar xjvf rarcrack-0.2.tar.bz2“. If you are using Windows, see the article on creating and extracting tarballs on Windows.
- Change into the new directory (rarcrack-0.2).
- make #(Build it)
- make install #(Now install it)
- You’re done! Launch rarcrack with “rarcrack“.
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Linux | 13 Comments »
Latest comment spammers on Compdigitec Labs
By admin | August 25, 2008
A list of recent comment spammers for you to check IPs against. Note that the actual comment or the link spammed is not provided because it either may be copyright violation or the sites may be harmful or is an bad exploit website.
- IP 142.32.208.234 using email d62s_test469@hotmail.com. Spammed on 2008-08-17 21:16:21 GMT. Spam consisted of link flooding about insurance.
- (repeat offense) IP 142.32.208.234 using email d25s_test268@hotmail.com. Spammed on 2008-08-11 18:40:52 GMT. Spam consisted of general link spam.
- IP 194.120.231.244 using email d68s_test333@hotmail.com. Spammed on 2008-08-17 21:16:34 GMT. Spam consisted of link flooding about auto insurance.
- IP 142.32.208.231 using email d30s_test338@hotmail.com. Spammed on 2008-08-17 21:16:45 GMT. Spam consisted of link flooding about various (illegal?) links to websites.
- (repeat offense) IP 142.32.208.231 using email d10s_test573@hotmail.com. Spammed on 2008-08-17 08:47:03 GMT. Spam consisted of link spam.
- IP 75.128.124.37 using email d95s_test431@hotmail.com. Spammed on 2008-08-17 21:17:00 GMT. Spam consisted of massive amounts of links.
- IP 92.113.23.162 using email kalugin-vilorii@mail.ru. Spammed on 2008-08-15 13:29:52 GMT. Spam consisted of nonsense with a link at the end.
- IP 93.174.93.224 using email ulyquw@kbkvbg.com. Spammed on 2008-08-22 22:54:23 GMT. Spam consisted of absolute linked nonsense.
- IP 200.208.169.133 using email d21s_test626@hotmail.com. Spammed on 2008-08-03 12:08:42 GMT. Spam consisted of massive amounts of links.
- IP 142.32.208.233 using email d12s_test253@hotmail.com. Spammed on 2008-08-03 12:09:04 GMT. Spam consisted of massive amounts of links.
- (repeat offense) IP 142.32.208.233 using email d49s_test696@hotmail.com. Spammed on 2008-08-03 12:09:49 GMT. Spam consisted of massive amounts of links.
- IP 125.64.4.168 using email d30s_test739@hotmail.com. Spammed on 2008-08-03 12:09:21 GMT. Spam consisted of massive amounts of links.
- IP 219.165.100.106 using email d74s_test923@hotmail.com. Spammed on 2008-08-11 18:40:31 GMT. Spam consisted of massive amounts of links.
- IP 92.42.224.251 using email d34s_test452@hotmail.com. Spammed on 2008-08-11 18:40:44 GMT. Spam consisted of massive amounts of links.
- (repeat offense) IP 92.42.224.251 using email d92s_test984@hotmail.com. Spammed on 2008-08-11 18:41:08 GMT. Spam consisted of general link spam.
- IP 190.144.41.196 using email d15s_test913@hotmail.com. Spammed on 2008-08-11 18:41:01 GMT. Spam consisted of insurance link spam.
- IP 96.3.138.136 using email d78s_test147@hotmail.com. Spammed on 2008-08-17 08:46:54 GMT. Spam consisted of link spam.
- IP 91.142.110.25 using email d43s_test403@hotmail.com. Spammed on 2008-08-17 08:47:13 GMT. Spam consisted of semi-nonsense with add link spam.
- IP 62.150.26.54 using email d87s_test602@hotmail.com. Spammed on 2008-08-17 08:47:29 GMT. Spam consisted of nonsense or gibberish with embedded links.
If you found this spam list useful or helpful, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Internet | 7 Comments »
Aliases for common command line typos
By admin | August 24, 2008
Update (2008-08-25): Lists complete and ready for download.
Update (2008-08-24): The list may take some time to compile, so downloads may not be available until August 25.
Are you tired from “bash: somethinghere: command not found” errors that happen when you type something like vf instead of cd, or cla instead of cls (alias for clear)? Well, now you can prevent it, because we have composed a list of most common command line typos. You won’t be able to use these aliases on Microsoft Windows due to cmd.exe not supporting aliases (unlike bash). You can, however, use these with Cygwin bash.
To use it, download the first download for each command and add to your ~/.bashrc. If you’re feeling a bit adventurous, you can download the CSV version (line 1 = command name, followed by one word for each typo) and parse it with Compdigitec FlatfileTools. If you don’t know how, read the article on it. Note: both the alias list and the CSV version use UNIX/Linux encoding (\n).
Typographical errors of the ls command. You can download the list of aliases or the CSV version.
- la
- lx
- llx
- lz
- ks
- kx
- kz
Typographical errors of the cd command. You can download the list of aliases or the CSV version.
- vf
- cf
- vd
- ce
- xd
- xs
Typographical errors of the php command. Download alias list or CSV version.
- hph
- hhp
- pph
- pphpp
- phph
Typographical errors of the make command. Download alias list or CSV format.
- amke
- kame
- maek
- emak
- mesk
- amsk
- smak
- maek
Typographical errors of the wget command. Download alias list or CSV format.
- wegt
- whet
- wfget
- fget
- wger
- weget
A part 2 article is under way for more commands.
If you found this article interesting or useful, please help Compdigitec spread the word or please leave a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Linux, Windows | 9 Comments »
Manipulating symbolic and hard links on Linux and Windows
By admin | August 23, 2008
Symbolic and hard links are a very convenient feature of the Linux and UNIX operating systems – they enable you to create a short alias, use it to avoid duplicate data and much, much more. They are also availible on Windows to a limited extent – NTFS supports only hard links while FAT/FAT32/VFAT supports neither. However, using Cygwin, you can use Windows shortcuts as symbolic links, but that is as far as it gets.
To create symbolic and hard links in Linux, make sure you have the GNU Coreutils installed, which provides the ln utility.
To create hard links and shortcuts in Microsoft Windows, download ln for Windows and place in a directory inside your %PATH%.
ln -s TARGET LINKNAME
– Creates a symbolic link (shortcut on Windows) called LINKNAME that points to TARGET. This works with both folders and files.ln TARGETFILE LINKNAME
– Creates a hard link called LINKNAME that points to the file TARGETFILE. Only works for files; if you try to do it with a folder, the error message “ln: `/folder/name/here‘: hard link not allowed for directory“.ln -i COMMAND
– Runs ln interactive mode, where it will ask you before running the command COMMAND, where the COMMAND is one of the commands above.ln -f COMMAND
– Forces ln to override any existing files while running the command COMMAND. For example, if you do ln -s /bin file and currently file exists, you will get “ln: creating (symbolic/hard) link `linkname‘: File exists“. Use ln -f /bin file to make it work.- (Windows only) You remove fake symbolic links (shortcuts) with
del linkname.lnk
to accommodate the fact that it is a Windows shortcut. You can remove hard links the normal way you delete a file –del hardlinkname
. - (Linux only) You remove symbolic and hard links the regular way you would remove files –
rm yourlinkname
.
If you found this article useful or helpful, please help Compdigitec spread the word or please leave a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Linux, Windows | 8 Comments »
Changing the user agent of PHP
By admin | August 20, 2008
If you ever need to adjust or change the default user agent that PHP accesses pages with (http and ftp wrappers) because either the default PHP user agent was blocked (“PHP”) or you wrote a bot with PHP and wish to customize it, you can do so easily with a php directive called “user_agent”. Note that 3rd party ways (such as cURL) are not affected by this setting.
To set it, you can use the following function:
<?php
function set_php_user_agent($useragent = ‘PHP’) {
$u = strval($useragent);
if(ini_set(‘user_agent’,$u) === false) {
$r = false;
} else {
$r = true;
}
return $r;
}
// Example:
set_php_user_agent(‘TestAgent/1.0 (http://www.example.com/testagent/)’);
?>
To get you started, here are some user agents for you to use or test:
- Mozilla/2.01 (Win16; I) – Netscape 2 on Windows 3.1
- Mozilla/2.0 (compatible; MSIE 3.0; I; Win95) – IE 3 on Windows 95
- Mozilla/3.0 (WinNT4; I) – Netspace 3 on Windows NT4
- Mozilla/4.0 (compatible; MSIE 4.01; Win98) – MSIE 4.01 on Windows 98
- Opera/5.11 (compatible; MSIE 5.0; Windows ME) [en] – Opera 5.11 on Windows ME
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) – IE6 on WinXP
- Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0 (ubuntu-hardy) – Firefox 3.0 on Ubuntu Hardy
- Lynx/2.8.6rel.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.0.4 (Linux i686) – Lynx 2.8.6 on Linux
- Mozilla/5.0 Galeon/1.0.2 (X11; Linux i686; U;) Gecko/20011224 – Galeon 1.0.2 on Linux
- Lynx/2.8.6rel.4 (Windows NT 5.1; I) – Lynx 2.8.6 on Windows XP
- Wget/1.10.2 – GNU Wget 1.10.2
If you found this article interesting or helpful, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Internet, PHP | 11 Comments »
Compiling re2c 0.13.5 on Ubuntu 8.04+ (binaries available)
By admin | August 19, 2008
If you need the latest features of the re2c compiler, you would normally sudo apt-get install re2c to install the re2c compiler. However, the Ubuntu packaging team (again) has not updated their re2c package to the latest re2c v0.13.5, so here are some pre-compiled re2c binaries. Be sure to remove the official version in the repos before installing.
Download pre-compiled packages of re2c 0.13.5
If you still want to compile it yourself, make sure you install GNU Bison, GNU C Compiler and the build essentials – you can do all of this with sudo apt-get install bison gcc build-essential
.
- Download re2c 0.13.5 and extract it to a temporary folder (/usr/src/re2c in the logs).
- Configure re2c with
./configure --prefix=/re2c
. If you require it, here are logs of an successful configure. - If successful, build re2c with
make
. Again, here are the make logs of a successful make if you require it. - If successful, copy re2c to /re2c with
make install
. If you require it, here are make install logs. (Installation comes later). - Test it by running
/re2c/bin/re2c --version
. It should return “re2c 0.13.5”. If it does not, repeat all of the steps above and try again. - Now build a Debian/Ubuntu package: (stripped down version of this article – read it for more details)
- Make a new folder called package
- Change into the package folder
- Make a new folder called DEBIAN
- Inside the DEBIAN folder, create a file called control. See http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Package for more details. (Note: do not name the package re2c to avoid conflict with the existing one)
- Copy /re2c to here and rename it usr
- (Important) Create a dummy link with
ln -s /re2c /usr
- Change to the parent directory
- Build package with
dpkg-deb --build package
. - If everything was successful, your new package is in package.deb!
- Remove /re2c and all sub-folders.
- You’re done! Install your package with dpkg -i package.deb.
If this article was useful or helpful, please help Compdigitec spread the word or please leave a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
Topics: Linux | 14 Comments »