/**
  * Write a file to the disk.
  * This function is heavily based on the way smarty process its own files.
  * Is using a temporary file and then rename the file. We guess the file system will be smarter than us, avoiding a writing / reading
  *  while renaming the file.
  */
 function write($file, $data)
 {
     $_dirname = dirname($file);
     //asking to create the directory structure if needed.
     $this->_createDir($_dirname);
     if (!@is_writable($_dirname)) {
         // cache_dir not writable, see if it exists
         if (!@is_dir($_dirname)) {
             trigger_error(CopixI18N::get('copix:copix.error.cache.directoryNotExists', array($_dirname)));
             return false;
         }
         trigger_error(CopixI18N::get('copix:copix.error.cache.notWritable', array($file, $_dirname)));
         return false;
     }
     // write to tmp file, then rename it to avoid
     // file locking race condition
     $_tmp_file = tempnam($_dirname, 'wrt');
     if (!($fd = @fopen($_tmp_file, 'wb'))) {
         $_tmp_file = $_dirname . '/' . uniqid('wrt');
         if (!($fd = @fopen($_tmp_file, 'wb'))) {
             trigger_error(CopixI18N::get('copix:copix.error.cache.errorWhileWritingFile', array($file, $_tmp_file)));
             return false;
         }
     }
     fwrite($fd, $data);
     fclose($fd);
     // Delete the file if it allready exists (this is needed on Win,
     // because it cannot overwrite files with rename())
     if (CopixConfig::osIsWindows() && file_exists($file)) {
         @unlink($file);
     }
     @rename($_tmp_file, $file);
     @chmod($file, 0644);
     return true;
 }
 /**
  * Ecriture d'un fichier sur le disque dur
  *
  * Cette fonction est basée sur le code trouvé dans Smarty (http://smarty.php.net)
  *
  * @param	string	$file le nom du fichier (le fichier sera crée ou remplacé)
  * @param	mixed	$data les données à écrire dans le fichier
  * @return	bool 	si la fonction a correctement écrit les données dans le fichier
  */
 public static function write($file, $data)
 {
     $_dirname = dirname($file);
     //If the $file finish with / just createDir
     if (($lastChar = substr($file, -1)) == '/' || $lastChar == '\\') {
         self::_createDir($file);
         return true;
     } else {
         //asking to create the directory structure if needed.
         self::_createDir($_dirname);
     }
     if (!@is_writable($_dirname)) {
         // cache_dir not writable, see if it exists
         if (!@is_dir($_dirname)) {
             throw new Exception(_i18n('copix:copix.error.cache.directoryNotExists', array($_dirname)));
         }
         throw new Exception(_i18n('copix:copix.error.cache.notWritable', array($file, $_dirname)));
     }
     // write to tmp file, then rename it to avoid
     // file locking race condition
     $_tmp_file = tempnam($_dirname, 'wrt');
     if (!($fd = @fopen($_tmp_file, 'wb'))) {
         $_tmp_file = $_dirname . '/' . uniqid('wrt');
         if (!($fd = @fopen($_tmp_file, 'wb'))) {
             throw new Exception(_i18n('copix:copix.error.cache.errorWhileWritingFile', array($file, $_tmp_file)));
         }
     }
     fwrite($fd, $data);
     fclose($fd);
     // Delete the file if it allready exists (this is needed on Win,
     // because it cannot overwrite files with rename())
     if (CopixConfig::osIsWindows()) {
         // DDT : ajout du test pour la vérification de l'existence du fichier
         if (file_exists($file)) {
             @unlink($file);
         }
         @copy($_tmp_file, $file);
         //Sur certaines configuration bien particulières, il arrive que
         //windows echoue sur le rename... ?
         @unlink($_tmp_file);
     } else {
         @rename($_tmp_file, $file);
     }
     @chmod($file, self::FILEMOD);
     return true;
 }
 /**
  * Renvoi le répertoire réel (utilise realpath si activé sur le serveur, sinon, renvoie un équivalent)
  *
  * @param string $pPath Répertoire dont on veut le realpath
  * @return string
  */
 public static function getRealPath($pPath)
 {
     $config = CopixConfig::instance();
     if ($config->realPathDisabled === false) {
         $realPath = realpath($pPath);
         $last = substr($pPath, strlen($pPath) - 1);
         // si on a mi un caractère de fin de répertoire
         if ($last == '\\' || $last == '/') {
             $realPath .= CopixConfig::osIsWindows() ? '\\' : '/';
         }
         return $realPath;
     } else {
         $result = array();
         $pPathA = preg_split('/[\\/\\\\]/', $pPath);
         if (!$pPathA[0]) {
             $result[] = '';
         }
         foreach ($pPathA as $key => $dir) {
             if ($dir == '..') {
                 if (end($result) == '..') {
                     $result[] = '..';
                 } elseif (!array_pop($result)) {
                     $result[] = '..';
                 }
             } elseif ($dir && $dir != '.') {
                 $result[] = $dir;
             }
         }
         if (!end($pPathA)) {
             $result[] = '';
         }
         return implode(CopixConfig::osIsWindows() ? '\\' : '/', $result);
     }
 }