bbPress

bbPress is a WordPress plugin that enable you as an WordPress administrator to add a full Forum server to your site.

I am testing it out now, and will install it soon on our Multisite WordPress solution.

bbPress is promising, and because it can be used in a WordPress context, you can benefit regarding spam issues if you have Akismet spam control enabled.
Also, if you already are an WordPress administrator, creating Forums and Topics is like making Pages and Posts.

I had a problem to get the bbPress Forum to work. I only got: “Can’t find the page 404” and “Sorry, this is embarrising”.
The solution was to go to:
Setting – Permalinks

That was it 🙂 Nothing more to it. Just the visit fixed the problem.

You can check the usage Forum here:

http://helge.w.uib.no/forums/
Or click on one of the Forums on the right sidebar

Alert Blog Admin php script

I had to send emails to all the Blog admins in our WordPress Multisite installation. This is how I did it:

[code lang=”php”]
<?php
# Alert Blog Admin php script
# connect to blog db, select from, find all blog admin email adresses
# get subject, body from pre-created local files, send the email

# how to run the script
# php alertblogadmin.php -s subjecttext.txt -b bodytext.txt

$mysqlserver="localhost";
$mysqluser="dbuser";
$mysqlpassword="XXXXXXX";
$database="blog";

$ERROR=0;

# This script require arguments
$arguments = getopt("s:b:f:dr");

# Test if array $arguments as at least s,b and r
# maybe to make sure that $arguments array contains at least 3 items?
if ( count($arguments) < 3 ){
echo "Missing arguments…\n";
echo "Usage: php alertblogadmin.php -s subjecttext.txt -b bodytext.txt\n";
die;
}

# 2 text files need to exist
if ( !file_exists($arguments["s"])) {
echo "File $arguments[s] does not exist?\n";
$ERROR=1;
}
if ( !file_exists($arguments["b"])) {
echo "File $arguments[b] does not exist?\n";
$ERROR=1;
}

if ($ERROR == "1"){
die;
}

$link = mysql_connect($mysqlserver, $mysqluser, $mysqlpassword);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db ($database,$link);

# collect blogs
$sql="SELECT blog_id from wp_blogs where deleted not like ‘1’";
$result=mysql_query($sql,$link);
if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}

$i = "0";
$emailarray = array();

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$blog_id = $row[0];
$table = "wp_" . $blog_id . "_options";

# Find email adresses to admins in each blog in the system
$sql2="SELECT option_value FROM $table WHERE option_name = ‘admin_email’";
$result2=mysql_query($sql2,$link);
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
$emailarray[$i] = $row2[0];
}
$i++;
}

# sort the email array and find unique emails
asort($emailarray);
$emailarray = array_unique($emailarray);

$emails = "";
foreach ($emailarray as $email) {
$emails .= $email.",";
}

$emails = substr($emails,0,-1);

# Could be smart to see who you will be sending your email to
echo $emails;

# open subject and body text files
$subject = file_get_contents($arguments["s"]);
$body = file_get_contents($arguments["b"]);
$from = $arguments["r"];

# Use this one to test before you actuelly send to $emails, which is a variabel that might contain hundreds of emails.
# You really would like to test first
$bcc = "user1@something.com,user2@something.net";
//$bcc = $emails; # uncomment this one, when you are ready to go

$headers = "From: noreply@something.com" . "\r\n" .
"Reply-To: noreply@something.com" . "\r\n" .
"Bcc: $bcc" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$header_ = ‘MIME-Version: 1.0’ . "\r\n" . ‘Content-type: text/plain; charset=UTF-8’ . "\r\n";

mail(”, ‘=?UTF-8?B?’.base64_encode($subject).’?=’, $body, $header_ . $headers);
mysql_close($link);
?>

[/code]

Find your files

In Linux you can use the command ‘find’ to find files and folders and then also do something with them on your system.

Find all files that ends with the .php extension in the folder /var/www/:
find /var/www/ -type f -name '*.php'

This will give you a list with a lot of files.
So, what? What is next?

Well, you can search for words in those files:
find /var/www/ -type f -name '*.php' | xargs grep 'foobar'

that will give files with file extension .php that has the word ‘foobar’ in it.

 

find /var/www/ -type f -iname '*.php' | xargs grep 'foobar'
Does the same right? Almost, only that it also search for also files with .PHP (iname means none-case-sensitive)

 

find /var/www/ -type d -name 'tmp' -exec ls -ald {} \;
will find all the folders under /var/www/ with the name “tmp” and list the information out about these folder.
Here is my output from my server:
find / -type d -name 'tmp' -exec ls -ald {} \;

drwxrwxrwt. 11 root root 4096 Oct 22 21:54 /tmp
drwxrwxrwt. 2 root root 4096 Oct 22 16:33 /var/tmp
drwxrwx–T. 2 root lp 4096 Sep 5 10:28 /var/spool/cups/tmp
drwxr-xr-x 2 root root 4096 May 9 10:30 /backup/db/tmp

Let us get to something more interesting:

Find all files that has the string rocket or ROCKET in their filename, and move them to the folder /tmp:
find / -type f -iname '*rocket*' -exec mv {} /tmp/ \;

Git

Git is a tool that can help you track changes in your files, specially when you share your files with your friends on a Linux server.

Here is a very simple description:

log into your server
ssh foobarserver

go to your folder where you want to track files
cd to/your/folder/for-git-tracking/test/

create a local git repository
git init

Add your files to the local repo
git add myfile.sh

or add more files at once
git add .

Commit your changes to the repo, give a message -m, so that your friends can understand what you have done
git commit -m "myfile.sh: first commit" myfile.sh

or commit all files in current directory (the character “.” is the directory where your files are.)
git commit -m "My files, snapshot" .

Now, start editing your file:

vim myfile.sh

or

nano -w myfile.sh

After saving your changes, do:

git commit -m "myfile.sh: I have changed an important part in this file" myfile.sh

or
git commit -m "My files, here is my explanation of the changes" .

Later on, you can do:

to see the log and the changes done
git log

did you forget to commit some files, or did someone change something?
git status

Using unix command ‘du’, disk usage

Sometimes you like to know which folders are using up all your space on your Linux server.

This command will sum up the disk usage per folder, and print the result:

du -m --max-depth=10 / | sort -r -n | head -10

– m (display in megabyte)
– max-depth=10 (folder depth? Should be plenty, depends on your system)
– sort -n -r (sort it by numbers, and reverse it)
– head -10 (only show the 10 top folder with most data.)

result on “myserver”:

6268    usr
5330    var
3414    var/www
2715    backup
2714    backup/postgresql_dumps
2399    usr/share
2192    var/www/applications
2126    var/www/applications/logonlogoff
2034    var/www/applications/logonlogoff/cache
1691    usr/local

Emails from Linux with æ, ø and å

Sometimes one need to send an email from Linux (automated messages, warnings and so on).
The problem is that the content looks strange when received by the email clients. The issue could be related to character encoding and UTF8.

Here is a php script I used to send an email with the correct character encoding.

[php]
<?php

# If you are sending the email to several people, sometimes it is good to use BCC (blind carbon copy)

$bcc = "person1@something,person2@something,person3@something";
$subject = "Here is some text with special characters ø æ å";
$body = "Hello, this is the norwegian characters ø, æ and å";
$headers = "From: apache" . "\r\n" .
"Reply-To: me@something" . "\r\n" .
"Bcc: $bcc" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$header_ = ‘MIME-Version: 1.0’ . "\r\n" . ‘Content-type: text/plain; charset=UTF-8’ . "\r\n";

# Send the email with headers!
mail(”, ‘=?UTF-8?B?’.base64_encode($subject).’?=’, $body, $header_ . $headers);
?>
[/php]

Use ‘mutt’ to send email with attachment

The unix ‘mutt’ command can be used to send an email with attachment:

mutt -s "Test message" name@something -a files.tar < message.txt

where files.tar is the attached file, and message.txt is the file containing the message.

Subject to the email is added with the -s option. In this case:
-s “Test message”

name@something is the receiver email address

You can also do this:

echo "Message text" | mutt -s "Subject text" name@something -a files.tar

Reference: http://www.cyberciti.biz/tips/sending-mail-with-attachment.html

Why would you like to use a unix command to send an email with an attachment?
The answer is that in the case you want to automate some process, let us say, you would like to pack together some important files, and send them every Sunday to a certain person. It could be statistical data, for instance, and the person would like to have a report each Sunday so to read them on Monday before lunch.
You could then make a simple bash script that uses ‘tar’ to collect the files into a single tar-file. Then call on the unix ‘mutt’ command, and send the message together with the important tar-file. All can be automated by calling this bash script from cron (unix job scheduler)