PDA

View Full Version : Strtange issue with cron und charset


Pessimist
01-23-2009, 08:05 PM
There are:
domen 1 - forum, vB 3.7.0, cp1251
domen 2 - site, Wordpress, utf-8

Task:
5 last new threads from forum (domen 1) at site (domen 2).

This file is in a folder http://www._forumname_.ru/include/cron/ and it should generate once at hour file cron_newthreads.html which included in WordPress:

<?php
// ######################## SET PHP ENVIRONMENT ###########################

error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db)) {
exit;
}

// ################################################## ######################
// ######################### START MAIN SCRIPT ############################
// ################################################## ######################

// ###### Start of settings ######

// directory of forum (without "/" at and)
// full domenname, if forum and site in others domens
$forumdir = "http://www._forumname_.ru";

// private forums (IDs)
$privateforumid = "70,95,99,108,109,124,129,142,153,154,155";

// limit to show
$showlimit = "5";

// only this forum to show (ID)
$and_fid = "";

// ###### End of settings ######

if(empty($and_fid) OR $and_fid == '0') {
$and_forumid = '';
}
else {
$and_forumid = " AND forumid = $and_fid ";
}

if(empty($privateforumid) OR $privateforumid == '0') {
$priv_forumid = '';
}
else {
$priv_forumid = " AND forumid NOT IN ($privateforumid) ";
}

if(empty($showlimit) OR $showlimit == '0') {
$showlimit = '5';
}

$result = $vbulletin->db->query_read("SELECT threadid,
lastposter,
dateline,
title,
postusername,
T.lastpost AS lastpostdate,
views,
postuserid,
replycount,
U.userid as userid
FROM ".TABLE_PREFIX."thread T
LEFT JOIN ".TABLE_PREFIX."user U ON (T.lastposter = U.username)
WHERE 1=1 $priv_forumid $and_forumid
ORDER BY dateline DESC
LIMIT $showlimit");

ob_start();

while ($lastx_thread = $vbulletin->db->fetch_array($result)) {
if($stylevar['charset'] != 'UTF-8') {
if(@function_exists('mb_convert_encoding')) {
$lastx_thread['title'] = mb_convert_encoding($lastx_thread['title'], 'UTF-8', $stylevar['charset']);
$lastx_thread['lastposter'] = mb_convert_encoding($lastx_thread['lastposter'], 'UTF-8', $stylevar['charset']);
}
else {
$lastx_thread['title'] = iconv($stylevar['charset'], 'UTF-8', $lastx_thread['title']);
$lastx_thread['lastposter'] = iconv($stylevar['charset'], 'UTF-8', $lastx_thread['lastposter']);
}
}

echo "<li>".date("d.m.y, H:i",$lastx_thread['dateline'])."<br /><a href='$forumdir/showthread.php?t=".$lastx_thread['threadid']."' target='_blank'>".$lastx_thread['title']."</a></li>
";
}

$html = ob_get_clean();

file_put_contents(DIR.'/cron_newthreads.html', $html);
log_cron_action('', $nextitem, 1);

?>



And all works perfectly if I press the button Run now in admincp (Scheduled Task Manager). But if the scheduled task starts itself automatically, the generated file is created with the incorrect coding (see attach).
Why??? What is wrong?

Dismounted
01-24-2009, 02:44 AM
Did you check the values in $stylevar? My guess is that $stylevar is not available when running the task normally.

Pessimist
01-24-2009, 12:28 PM
Dismounted
Sorry, I'm not programmer.

How I can check up it, what it is necessary to make?

Lynne
01-24-2009, 04:47 PM
I think what he's saying is you may need to make $stylevar a global so it is available for you to use:

global $stylevar;

(Your eye avatar freaks me out! :D )

Pessimist
01-25-2009, 08:23 AM
I have replaced
if(@function_exists('mb_convert_encoding')) {
$lastx_thread['title'] = mb_convert_encoding($lastx_thread['title'], 'UTF-8', $stylevar['charset']);
$lastx_thread['lastposter'] = mb_convert_encoding($lastx_thread['lastposter'], 'UTF-8', $stylevar['charset']);
}
else {
$lastx_thread['title'] = iconv($stylevar['charset'], 'UTF-8', $lastx_thread['title']);
$lastx_thread['lastposter'] = iconv($stylevar['charset'], 'UTF-8', $lastx_thread['lastposter']);
}with
if(@function_exists('mb_convert_encoding')) {
$lastx_thread['title'] = mb_convert_encoding($lastx_thread['title'], 'UTF-8', 'cp1251');
$lastx_thread['lastposter'] = mb_convert_encoding($lastx_thread['lastposter'], 'UTF-8', 'cp1251');
}
else {
$lastx_thread['title'] = iconv('cp1251', 'UTF-8', $lastx_thread['title']);
$lastx_thread['lastposter'] = iconv('cp1251', 'UTF-8', $lastx_thread['lastposter']);
}And now works for me :)

(Your eye avatar freaks me out! :D )It is really my eye, little bit edited (color) in the Photoshop :)