PureEdit (rocking ultra light CMS system) and utf8 / unicode support. Pagination.

I've found PureEdit today and decided to use it as my CMS backend for some of my simpler projects!
This is a GREAT and FAST way to setup a backend content editing.
Take a look at the videos on their site! I was fascinated!

I've had old database filled with correct Unicode (UTF-8) data (in Cyrillic).
I've installed and loaded pe-admin, updated my db accortind to PureEdit specs, added some fields type like (status 1||0).
When opened pe-admin I've come up with few Unicode problems.
(as described here: http://www.pureedit.com/community/comments.php?DiscussionID=149&page=1 )
I've solved my problem exactly by putting: mysql_query('SET NAMES UTF8');

I've had to modify the connect function in pe-admin/databases/mysql.db.php like this:
function connect($host, $username, $password, $database)
{
$dbh = mysql_connect($host, $username, $password);
mysql_query('SET NAMES UTF8');
mysql_select_db($database, $dbh);
}
(it used to connect/select db in one line, but you MUST make SET NAMES BEFORE selecting db!)

The second problem that I've had as with utf8 again.
Utils class is not UTF-8 ready.
I've had to add to set utf8 encoding
public function __construct() {
mb_internal_encoding('UTF-8');
}
and replace all str functions with mb_str.

After that I wanted to have pagination.
Here it's described how to set it up.

http://www.pureedit.com/community/comments.php?DiscussionID=221&page=1#Item_0

Now I have to setup host type filed (that will be automatically filled with $_SERVER['REMOTE_ADDR']) and some othe fancy stuff, but in about 40 minutes I've setup a GREAT and easy to use backend posting solution for my website!

Thanks PureEdit :-)

The PureEdit code is not from the greatest ones but when it works and if .htaccess protected - it simply works! :-)

php mail function overloading or why we shouldn't use mb_send_mail as sending mail function.

As a programmer I have a nice cool helpers - classes I've written to help me do my daily work.
Such class is my Mailer class which I use almost daily.
It has all kind of fancy options and can do all kind of stuff - from sending simple mail to just setting an array with files which should be attached in the mail.
It used to work very good until today.
I was very surprised when I saw a broken email coming from it.
I've investigated a few minutes what has changed and I've found that the problem
I've set
mbstring.func_overload = 7
according to the description in file:
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
it turned out that when set to 7 instead of using mail() function, php starts using mb_send_mail.
mb_send_mail works only with base64 emails.
It automatically encodes the body and subject (but not the sender wtf?) and it breaks mail ugly!
If you manually add/set headers do not put \r\n at the very end of the string, mb_send_mail adds two headers after yours:
Mime-Version: 1.0
Content-Transfer-Encoding: BASE64
and if you put one new line(\r\n) to separate the headers from body then you get the email totally messed up, because the base64 transfer won't apply and you'll get encoded string as plain text or html or whatever content-type you set previously.
After I've lost another 20 minutes figuring out this, I've finally achieved receiving my email as it should be... well... almost.
When the mail contained only ASCII symbols everything was ok, but when I tried to send email in utf8 Bulgarian... omfg?!
The encoding was broke and for some reason the UTF8 symbols looked like you have page in UTF8 but your browser is set to WINDOWS-1251....
WTF?!!! I've read few posts that said they never got it successfully working fine with mb_send_mail.
It took me some time to find something very useful - there is an undocumented php function: mb_orig_mail (http://osdir.com/ml/php.internationalization/2003-01/msg00054.html).
When you use overloaded function you can fall back to the standart mail function by calling this one.
So here is what I did to solve my problem - I use exactly the same class that used to work until the overload get active.
But when it comes to sending I simply check if the mail function is overloaded and if yes, then call the mb_orig_mail.
Here is the actual code:

$mailFunct = 'mail';
if (ini_get('mbstring.func_overload') == 1 || ini_get('mbstring.func_overload') > 4) {
$mailFunct = 'mb_orig_mail';
}
if(! $mailFunct($tostr, $subject, $tmpBody, $tmpHeaders) ) {
echo 'sent';
} else {
echo 'error sending';
}

It's as simple as that.
Hope that helps to anyone.
Comments are welcome.