PHP, Drupal, Diem, FreeBSD, shell, Mercurial, Netbeans, Perl, MySQL, JavaScript tips, snippets, one-liners, questions and answers

unable to load dynamic library: php_mbstring.dll
Edit your php.ini so that php_exif.dll is loaded after php_mbstring.dll
utf-8 encoded e-mails appear garbled in MS Outlook
Set the charset to 'utf-8', not 'utf8' or any other variation.
unable to open a COM port using fopen() on Windows
PHP can't open a serial port if it's greater than COM9 (COM10 and more). If you can, change the port number for your device. (ref)
i'd like to bill my client easily. i'm already entering my hours into google calendar
Check out GCal Billing and its demo
how do i recursively chmod only directories or files?
dirs:
$ find . -type d -exec chmod 755 {} \;
or files:
$ find . -type f -exec chmod 644 {} \;
thanks.
how do i specify default permissions for new files?
Use umask:
$ umask 002
(for default permissions of 664). For permanent effect, enter this line into ~/.profile
For Apache (under FreeBSD) add this line to /usr/local/etc/apache22/envvars.d/umask.env:
umask 2
why is my drupal installation constantly running out of max execution time limit during clearing cache?
On Windows several antiviruses can treat this as a suspicious activity. Disable realtime scanning of php.exe or php-cgi.exe, depending on your setup.
how to remove the "add new comment" link from drupal node talk tab/page?
Use a theme function.

<?php
function THEMENAME_links__node__comment($variables) {
    return; 
// no "Add new comment" link
}
?>

when running a program on freebsd i get an error:
ELF interpreter /libexec/ld-elf.so.1 not found.
Abort trap: 6
Most probably you are running a 32-bit program on a 64-bit OS. Check this with the command:
uname -a
If you are on a GENERIC build (look for that word in the output), run this:
make build32
make install32
On other builds you may have to do some preparation work, look that up on the web.
how to add a footer area with sitemap to a diem cms driven site?
One possible way to achieve this:
  1. Check config/dm/modules.yml and make sure it contains the following:
    Content:
    
      Global:
    
    	main:
          components:
            header:
            footer:
            sitemap:
  2. Add to apps/front/modules/main/actions/components.class.php:

    <?php
      
    public function executeFooter()
      {
        
    $this->menu $this->getService('sitemap_menu')->build();
      }
    ?>

  3. Add to apps/front/modules/dmFront/templates/*Success.php:

    <?php echo $helper->renderArea('layout.bottom'?>

  4. Visit pages with different layouts and verify that there is a Zone widget where you want to put the footer widget. Press the green «+» and drag «Footer» from «MAIN» section into the zone. Click Save.
how to quickly check for a string in files?
find . -name "*.php" -print0 | xargs -0 grep -i --color=auto "Unknown error"
when trying to do anything in my hg repo, i get a stack trace and "ValueError: overflow in dirstate"
Probably there was some messy file copying happening. Run
hg debugrebuildstate
can't get netbeans on windows vista / 7 and java jdk 7 to work with remote projects over ftp
Launch Command Prompt with administrative rights and run this:
netsh advfirewall set global StatefulFTP disable
when using mod_vhost_alias on apache together with mod_rewrite, i stumble upon an error message «Request exceeded the limit of 10 internal redirects due to probable configuration error.»
Add
RewriteBase /
to your project's .htaccess file. (ref)
when installing a random port i get an error «pkgconf conflicts with installed package(s) pkg-config»
sudo pkg_delete -f pkg-config-0.25_1
(ref)
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] are empty when trying to use http basic authentication
Probably PHP is running in CGI or FastCGI mode.
  1. Enable mod_rewrite
  2. Create a .htaccess file if needed and add these lines:
    RewriteEngine on
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
  3. Add these lines before using the variables:

    <?php
    if(preg_match('/Basic+(.*)$/i',
            
    $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches))
        list(
    $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
            
    explode(':' base64_decode(substr($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 6)));
    ?>

(ref)
when converting a pdf to jpg via imagick i get an error like "Postscript delegate failed `/tmp/magick-bfxIrUJ5': No such file or directory @ error/pdf.c/ReadPDFImage/669"
  1. Ghostscript should be visible from different paths. Ensure that you have it both in /usr/bin and /usr/local/bin:
    # ln -s /usr/local/bin/gs /usr/bin
    (ref)
  2. Test it by running /usr/bin/gs. If it gives an error like ""/libexec/ld-elf.so.1" Shared object libpng.so.6 not found, required by libgs.so.8", it means that some libraries were not propery updated. In this case you may try to upgrade libpng and the dependent packages. In case of FreeBSD:
    portupgrade -fr graphics/png
how do i delete all the files which match to a certain pattern? how to i pipe the "find" command result to "rm"?
find <pattern and other parameters> -print0 | xargs -0 rm
(ref)
how do i get a first-level file and directory list sorted by size?
perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -hs *`;die@h{sort%h}'
(ref)
how do i drop all the tables in a mysql database if i don't have the rights to drop/create db?
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
	FROM information_schema.tables 
    WHERE table_schema = 'DB_NAME'; -- specify DB name here.

SET @tables = CONCAT('DROP TABLE ', @tables);
SET FOREIGN_KEY_CHECKS = 0;
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
(ref)
how do i delete a million files without running into an «argument list too long» error?
echo 'find . -maxdepth 1 -type f -print0 | xargs -r0 rm -f' | sudo -s
upon launching phpmyadmin i get a mysql socket error #2002 - No such file or directory
Probably you're on Mac OS X. Set the socket path and the type of connection to "http":
$cfg['Servers'][$i]['connect_type'] = 'http';
$cfg['Servers'][$i]['socket'] = '/tmp/mysql.sock';
(ref)
how do i copy some file from the skel directory contents to all existing users?
for i in `cat /etc/trueuserowners | cut -d':' -f1`
;do \cp -f cpanel3-skel/.ssh/authorized_keys /home/$i/.ssh/authorized_keys ;done
how do i remove a line from all users' ssh authorized keys file?
for i in `cat /etc/trueuserowners | cut -d':' -f1`
;do sed -i '/olduseruniquestring/d' /home/$i/.ssh/authorized_keys ;done
how do i get the size of all files older than some certain date?
 find . -not -newermt 2014-01-01 -exec ls -l {} \; | awk '{s+=$5} END {print s/(1024*1024)}'
(ref 1, ref 2)
how do i add environment variables to apache on mac os x?
Edit the plist file /System/Library/LaunchDaemons/org.apache.httpd.plist, adding:
<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin</string>
</dict>
If there already is such a key, add the entry to its dict. (ref)
how do i find events bound on an element?
In jQuery:
$._data($("#someElement")[0], "events");
(ref)