" );
return $tmp;
}
return null;
}
###
# Lister les fichiers d'un répertoire
###
function liste_dir($dir, $ext="txt.gz") {
$data_dir_obj = dir ($dir);
while ($my_files[] = $data_dir_obj->read());
sort($my_files);
while ($file=next($my_files)) {
if (ereg("^.*\.".$ext."$", $file)) {
$ret []= $file;
}
}
$data_dir_obj->close();
foreach($ret as $r) {
$itm=basename($r, ".".$ext);
$new[$r]=$itm;
}
return $new;
}
### Main
function get_item($item, $decode=0)
{
global $codetype;
/*
Vérifie l'existance d'un fichier, selon la langue
détecte le cookie lang
ajoute à l'item _$lang s'il n'existe pas déjà
s'il n'y a pas de fichier, appelle la base de données
*/
$bar="";
$lang=$_COOKIE['lang'];
if ($lang=="fr" || empty($lang))$lang="";
else $bar="_";
if (file_exists("datas/".$item.$bar.$lang.".wiki.gz")) {//cas d'un wiki
$codetype = "wiki";
$wiki = new wiki2xhtml_new();
$wiki->registerFunction('macro:importpage',array($wiki,'importPage'));
if (!$decode) $res = $wiki->transform(@implode("",@gzfile("datas/".$item.$bar.$lang.".wiki.gz"))); //xhtml coded version
else $res = @implode("",@gzfile("datas/".$item.$bar.$lang.".wiki.gz")); //brute wiki coded version
if (!empty($res)) return $res;
}
else if (file_exists("datas/".$item.$bar.$lang.".txt.gz")) {//cas d'un texte brut
$codetype = "txt";
$res = @implode("",@gzfile("datas/".$item.$bar.$lang.".txt.gz"));
if (!empty($res)) return $res;
}
else if (file_exists("datas/".$item.$bar.$lang.".html.gz")) {//cas d'un html
$codetype = "html";
$res = utf8_ensure(html_entity_decode( @implode("",@gzfile("datas/".$item.$bar.$lang.".html.gz"))));
if (!empty($res)) return $res;
}
else
{
$codetype = "none";
return "\n";
}
}
function put_item($item,$string,$code="html")
{
/*
comme on est en mode admin, on enregistre tout dans la base de données, mais on crée un cache fichiers
détecte le cookie lang
ajoute à l'item _$lang s'il n'existe pas déjà
*/
$bar="";
$lang=$_COOKIE['lang'];
if ($lang=="fr" || empty($lang))$lang="";
else $bar="_";
$f="datas/".$item.$bar.$lang.".".$code.".gz";
$fp=gzopen($f, "w");
gzwrite($fp, stripslashes($string));
gzclose($fp);
@chmod($f,0666);
$datas=array();
if(!empty($string))
{
$datas['item']=$item;
$datas['text']=$string;
$datas['date']=date("Y/m/d H:i:s");
$datas['type']=1;
$datas['status']=1;
$datas['code']=3;
$datas['lang']=$_COOKIE['lang'];
}
//~ print "".$item."";
//~ print html_entity_decode($string);
}
function get_title($str, $motif)
{
preg_match('/<'.$motif.'[^>]*>(.*)<\/'.$motif.'>/i', $str, $matches);
$res=$matches[1];
$res=preg_replace("/(<.*>)/","",$res);
if (!empty($res))
return " - ".$res;
}
### CHECK SETUP
function CheckSetup(){
if (!is_dir('./datas/')){
if (!mkdir ("./datas", 0777)){
echo 'ERROR : datas/ folder doesn\'t exists ';
echo 'please create a folder "datas" at the root of the site and change its property to be writable ("chmod 777 datas/") ';
}
}
if (!is_dir('./images/')){
if (!mkdir ("./images", 0777)){
echo 'ERROR : images/ folder doesn\'t exists ';
echo 'please create a folder "images" at the root of the site and change its property to be writable ("chmod 777 images/") ';
}
}
if (!is_dir('./html/')){
if (!mkdir ("./html", 0766)){
echo 'ERROR : html/ folder doesn\'t exists ';
echo 'please create a folder "html" at the root of the site and change its property to be writable ("chmod 777 html/") ';
}
}
if (!is_dir('./style/')){
if (!mkdir ("./style", 0766)){
echo 'ERROR : style/ folder doesn\'t exists ';
echo 'please create a folder "style" at the root of the site and change its property to be writable ("chmod 766 style/") ';
}
}
if (!is_file('./style/style.css')){
if (!touch ("./style/style.css")){
echo 'ERROR : style/style.css file doesn\'t exists ';
echo 'please create a file "style/style.css" at the root of the site and change its property to be writable ("chmod 644 style/") ';
}
}
if (!is_dir('./datas/') || !is_dir('./images/')){die();}
/*
if (!is_writeabledir('./datas/')) {
echo 'ERROR : datas/ folder isn\'t writable ';
}
if (!is_writeabledir('./images/')) {
echo 'ERROR : images/ folder isn\'t writable ';
}
*/
}
function is_writeabledir($dir){
$folder = opendir($dir);
while($file = readdir( $folder ))
if($file != '.' && $file != '..' &&
( !is_writable( $dir."/".$file ) ||
( is_dir( $dir."/".$file ) && !is_writeabledir( $dir."/".$file ) ) ))
{
closedir($dir);
return false;
}
closedir($dir);
return true;
}
#######switch_lang
function switch_lang()
{
global $item;
if($item=="fr" ||$item=="en" ||$item=="de")
{
setcookie("lang", $item);
header("Location: ".$_SERVER['HTTP_REFERER']);
}
}
/***********fonctions diverses *****************/
if (!function_exists('utf8_ensure')) {
function seems_utf8($Str) {
for ($i=0; $i'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
);
function cleanForShortURL($toClean) {
$toClean = str_replace('&', '-and-', $toClean);
$toClean = trim(preg_replace('/[^\w\d_ -]/si', '', $toClean));//remove all illegal chars
$toClean = str_replace(' ', '-', $toClean);
$toClean = str_replace('--', '-', $toClean);
return strtr($toClean, $GLOBALS['normalizeChars']);
}
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
* Examples: $array = xml2array(file_get_contents('feed.xml'));
* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}
return($xml_array);
}
function is_mobile()
{
$mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|mobile|maemo)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {$mobile_browser++;}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {$mobile_browser++;}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-','mobile','droid');
if(in_array($mobile_ua,$mobile_agents)) {$mobile_browser++;}
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {$mobile_browser++;}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {$mobile_browser=0;}
if($mobile_browser>0) {return true;}
else {return false;}
}
/* Enregistrement des phrases dans les 3 langues */
function make_sentences()
{
global $sentences;
if (file_exists("datas/translations.php"))
{
$sentences=array();
$f=file("datas/translations.php");
foreach($f as $l)
{
if(substr($l,0,1)=="#" ||substr($l,0,1)=="<"
||substr($l,0,1)=="/"
||substr($l,0,1)=="*") continue;
if(empty($l)) continue;
$p=explode(";",$l);
$sentences[$p[0]]=array('fr'=>$p[1],'de'=>$p[2],'en'=>$p[3]);
}
}
}
/* Détection des langues acceptées */
function detect_language()
{
global $lang;
if(!empty($_GET['lang'])) {$lang=$_GET['lang'];setcookie('lang',$_GET['lang'],time()+356*24*3600);}
else if(!empty($_COOKIE['lang'])) {$lang=$_COOKIE['lang'];}
else
{
if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,5)=="fr-FR")$lang="fr";
else if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,5)=="de-DE")$lang="de";
else if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,5)=="en-US")$lang="en";
else if(stristr($_SERVER['HTTP_ACCEPT_LANGUAGE'],"fr-fr"))$lang="fr";
else if(stristr($_SERVER['HTTP_ACCEPT_LANGUAGE'],"de-de"))$lang="de";
else if(stristr($_SERVER['HTTP_ACCEPT_LANGUAGE'],"en-us"))$lang="en";
else $lang="en";
}
}
function t($s)
{
global $sentences, $lang;
$ret = $sentences[$s][$lang];
if(empty($ret))
{
//on ajoute la ligne manquante
$fp=fopen("datas/translations.php","a");
$str=$s.";".$s.";".gt($s,"de").";".gt($s,"en")."\n";
fwrite($fp,$str);
fclose($fp);
//on écrit quand même la bonne traduction !
$googletrans=gt($s,$lang);
if (!empty($googletrans))$ret=$googletrans;
else $ret=$s;
}
return $ret;
}
function tt($s)
{
echo t($s);
}
function gt($s,$t)
{
$f=implode('',file("http://translate.google.fr/translate_a/t?client=t&text=".urlencode(htmlentities(utf8_decode($s)))."&hl=fr&sl=fr&tl=".$t."&multires=1&pc=0&sc=1"));
preg_match("/\[\[\[\"([^\"]*)\",.*$/",$f,$m);
return $m[1];
}
detect_language();
make_sentences();
?>
|