<?php
/*
Copyright (C) 2008 by oscim   Project lossim  [ 15.09.2009, 09:17:59] @oscim
// kate: replace-trailing-space-save true; space-indent on; indent-width 2; mixedindent off; indent-mode cstyle; word-wrap-marker-color #41C2A4
ENCODE UTF-8
*/
function op_files ($fichier,$chemin,$action,$contenu='',$new_name='',$title='',$description='' ){
  global $cstr,$var_module_wysing_add;

  error_reporting(E_ALL);
  if (empty($fichier)) return false;

  $t=NewFct('extract_nom_dater_evoluer',$fichier,"");
  if (empty($chemin) && preg_match( "/^.*\/$/", $chemin ) ) $chemin .= '/';
  else $chemin=$t['chemin'];

  $name=$t['nom_complet'];
  $lang=$t['lang'];
  $rub=$t['rub'];
  $name_ss_ext=$t['nom_ss_ext'];
  $ext='.'.$t['ext'];
  $bak_path_file=$rub.$name;

  /**
    Traitement des copy
  */

  $t=NewFct('extract_nom_dater_evoluer',$new_name,"");
  $bchemin=$t['chemin'];
  $bname=$t['nom_complet'];
  $blang=$t['lang'];
  $brub=$t['rub'];
  $bname_ss_ext=$t['nom_ss_ext'];
  $bext='.'.$t['ext'];
  $n_path_file=$bchemin.$bname;


  if ( (!empty($contenu))AND($ext =='.xml') )  {
//     $content = stripslashes($contenu);
//     $content = str_replace( "\n\r", "", $content);
//     $content = str_replace( "& ", "&amp; ", $content);
//     $content = str_replace("&nbsp;","", $content);
//     $content = str_replace("<br>","<sot></sot>\n", $content);
    ## Element lier module
//     if (is_array($var_module_wysing_add))
//     while ($r=each($var_module_wysing_add))
//       if (function_exists('module_'.$r['value'].'_reg')) eval("\$content=module_".$r['value']."_reg(\$content);");

//       $content=substr($contenu,strpos('<!-- DEBUT REEL PAGE -->'),(strlen($contenu)-7) );
//       $contenu ='<content><![CDATA['. $content.']]><content>';

        $match = '/(.+)(\$'.preg_quote('<.*[^>]/>','/').')[\s](.+);#/mU';
        if (preg_match($match,$contenu)){
           preg_replace($match,'<content><![CDATA[$2]]><content>',$contenu);
        }
//         foreach(preg_match($match,$contenu) as )


//     $content=eregi_replace ( "<br[a-z0-9_\.= \":,;/\)\(-]*>","<sot></sot>\n", $content);
//     /* Special golive */
//     $content=eregi_replace ( "<spacer[a-z0-9_\.= \":,;/\)\(-]*>","<sot></sot>\n", $content);
//     ## trait image, balise fermante
//       if ( (eregi ( "<img(.*)(><[a-z0-9_]*>|>[a-z0-9_]*)",  $content))AND (!eregi("</img>", $content)) ) {
//         $content= str_replace('/>', '>',$content);
//         $r=explode ("<img", $content);
//          count ($r);
//         $new='';
//         while ( $row=each($r)){
//           if (!eregi("^(.?src)",$row['value'])) $new .=$row['value'];
//           else {
//             $l=strpos($row['value'],">");
//             $img=substr($row['value'],0,$l);
//             $new .="<img ".addslashes($img)."></img>".substr($row['value'],$l+1);
//           }
//         }
//       $contenu =$new;
//       } else $contenu=$content;
    }
    else $contenu = stripslashes($contenu);


  if (!empty($contenu))$contenu=stripslashes($contenu);
//     if(tep_file($chemin.$name)){
      require_once("include/class/class.files.php");
      $le_fichier = new files($bak_path_file, $lang);
//       echo $action;
      switch ($action){
        case 'ADD':
          if ($ext =='.xml' && !$le_fichier->BackupFile()) $cstr->_call('message_stack','add_session',ERROR_CREATION_REVISION,'erreur');
          $le_fichier->AddContent($contenu);
          $savefile=true;
        break;
        case 'COPY':
          if ($ext =='.xml' && !$le_fichier->BackupFile())  $cstr->_call('message_stack','add_session',ERROR_CREATION_REVISION,'erreur');
          $savefile=false;
          if(!$le_fichier-> CopyFile($lang.'/'.$rub.$name,$blang.'/'.$brub.$bname)) $cstr->_call('message_stack','add_session',COPY_FILE_ERREUR,'erreur');
          else $cstr->_call('message_stack','add_session',COPY_FILES_OK,'ok');
        break;
        case 'DEL':
          if ($ext =='.xml' && !$le_fichier->BackupFile())  echo nett_acen("<p class='erreur'>".ERROR_CREATION_REVISION."</p>");
          $le_fichier-> SuppFile();
          $savefile=false;
        break;
        case 'BAK':
          if (eregi("bak_",$fichier)) $le_fichier-> RennFile($chemin.eregi_replace("bak_","",$fichier).$ext);
          else $le_fichier-> RennFile($chemin."bak_".$fichier.$ext);
          $savefile=true;
        break;
        case 'RENN':
          if ($ext =='.xml' && !$le_fichier->BackupFile())  $cstr->_call('message_stack','add_session',ERROR_CREATION_REVISION,'erreur');
          if(!$le_fichier-> RennFile($new_name)) $cstr->_call('message_stack','add_session',WRITE_NOT_POSSIBLE,'erreur');
          $savefile=false;
        break;
        default:
        // NEW
          $le_fichier->AddContent($contenu);
          $savefile=true;
      }
      ## prepa feed
      $descri=nett_acen($description);
      if ((!empty($chemin))AND ($chemin<>ADD_PATH_ADMIN.REP_DATA)) $link= URL_DU_SITE.'?'.$chemin."/".$name_ss_ext;
      else $link= URL_DU_SITE."?".$name_ss_ext;
      if (!$savefile) {
        if (isset($data) && !empty($data) )$cstr->_call('message_stack','add_session',$data,'erreur'); //echo nett_acen($data);
//           if (test_const('CACHE_PUBLIC')) {
//             @require_once("advRmDir.inc");
//             print "<p>".advRmDir(ADD_PATH_ADMIN."cache/")."</p>\n"; // purge cache
//           }
//         if ( (defined('BO_MOD_GENERE_RSS')) && (test_const('RSS')) ) {
//           @require_once(UPLOAD_RSS_INC);
//           if ($ext=='.xml') upload_rss(ADD_PATH_ADMIN.'rss/'.FILE_RSS.".xml",$title,$descri,$link); // feed
//         }
      } else {
        if ($le_fichier->saveFile()) {
          $cstr->_call('message_stack','add_session',UPLOAD_OK,'ok');

          if (isset($data) && !empty($data))  $cstr->_call('message_stack','add_session',$data,'erreur'); //echo nett_acen($data); //echo nett_acen($data);
//             if (test_const('CACHE_PUBLIC')) {
//               @require_once("advRmDir.inc");
//               print "<p>".advRmDir(ADD_PATH_ADMIN."cache/")."</p>\n"; // purge cache
//             }
//         if ( (defined('BO_MOD_GENERE_RSS')) && test_const('RSS') ) {
//           @require_once(UPLOAD_RSS_INC);
//           if ($ext=='.xml') upload_rss(ADD_PATH_ADMIN.'rss/'.FILE_RSS.".xml",$title,$descri,$link); // feed
//         }
        } else {

          $cstr->_call('message_stack','add_session',UPLOAD_NOT_OK.WRITE_NOT_POSSIBLE,'erreur');
          $cstr->_call('message_stack','add_session',sprintf(WRITE_MODIF_NOT_OK, $_GET['file']),'info');
        }
      }
//     }else {
//       $cstr->_call('message_stack','add',ERREUR_FILE_NOT_EXIST.WRITE_NOT_POSSIBLE.$chemin.$name,'erreur');
//       $cstr->_call('message_stack','add',sprintf(WRITE_NOT_POSSIBLE_DETAIL, NOM_DU_PROJET ),'info');
//     }
}
?>