/**
  * Update information about a file after commit (update informations added with revcheck tools).
  *
  * @param $files An array of File instances for info update.
  */
 public function updateFileInfo($files)
 {
     $am = AccountManager::getInstance();
     foreach ($files as $file) {
         $info = $file->getInfo();
         $size = intval(filesize($file->full_path) / 1024);
         $date = filemtime($file->full_path);
         if ($file->lang == 'en') {
             // en file
             // update EN file info
             $s = 'UPDATE `files`
                     SET
                         `xmlid`    = "%s",
                         `revision` = "%s",
                         `size`     = "%s",
                         `mdate`    = "%s"
                     WHERE
                         `project` = "%s" AND
                         `lang` = "%s" AND
                         `path` = "%s" AND
                         `name` = "%s"';
             $params = array($info['xmlid'], $info['rev'], $size, $date, $am->project, $file->lang, $file->path, $file->name);
             $this->conn->query($s, $params);
             // update LANG file info
             $s = 'UPDATE `files`
                     SET
                         `en_revision` = "%s"
                     WHERE
                         `project` = "%s" AND
                         `lang` != "%s" AND
                         `path`  = "%s" AND
                         `name`  = "%s"';
             $params = array($info['rev'], $am->project, $file->lang, $file->path, $file->name);
             $this->conn->query($s, $params);
         } else {
             // lang file
             // If this file don't exist in EN, we should skip all this proces
             $en = new File('en', $file->path . $file->name);
             if ($en->exist()) {
                 $enInfo = $en->getInfo();
                 $sizeEN = intval(filesize($en->full_path) / 1024);
                 $dateEN = filemtime($en->full_path);
                 $size_diff = $sizeEN - $size;
                 $date_diff = intval((time() - $dateEN) / 86400) - intval((time() - $date) / 86400);
                 // update LANG file info
                 $s = 'UPDATE `files`
                         SET
                             `xmlid`      = "%s",
                             `revision`   = "%s",
                             `en_revision`= "%s",
                             `reviewed`   = "%s",
                             `reviewed_maintainer` = "%s",
                             `size`       = "%s",
                             `mdate`      = "%s",
                             `maintainer` = "%s",
                             `status`     = "%s",
                             `size_diff`  = "%s",
                             `mdate_diff` = "%s"
                         WHERE
                             `project` = "%s" AND
                             `lang` = "%s" AND
                             `path` = "%s" AND
                             `name` = "%s"';
                 $params = array($info['xmlid'], $info['en-rev'], $enInfo['rev'], trim($info['reviewed']), trim($info['reviewed_maintainer']), $size, $date, trim($info['maintainer']), trim($info['status']), $size_diff, $date_diff, $am->project, $file->lang, $file->path, $file->name);
                 $this->conn->query($s, $params);
                 // Run the errorTools under this file
                 $tmpFile[0]['en_content'] = $en->read(true);
                 $tmpFile[0]['lang_content'] = $file->read(true);
                 $tmpFile[0]['lang'] = $file->lang;
                 $tmpFile[0]['path'] = $file->path;
                 $tmpFile[0]['name'] = $file->name;
                 $tmpFile[0]['maintainer'] = $info['maintainer'];
                 $errorTools = new ToolsError();
                 $errorTools->updateFilesError($tmpFile);
             } else {
                 // update LANG file info
                 $s = 'UPDATE `files`
                         SET
                             `xmlid`      = "%s",
                             `revision`   = "%s",
                             `en_revision`= "%s",
                             `reviewed`   = "%s",
                             `reviewed_maintainer` = "%s",
                             `size`       = "%s",
                             `mdate`      = "%s",
                             `maintainer` = "%s",
                             `status`     = "%s",
                             `size_diff`  = "%s",
                             `mdate_diff` = "%s"
                         WHERE
                             `project` = "%s" AND
                             `lang` = "%s" AND
                             `path` = "%s" AND
                             `name` = "%s"';
                 $params = array($info['xmlid'], $info['en-rev'], 0, trim($info['reviewed']), trim($info['reviewed_maintainer']), $size, $date, trim($info['maintainer']), trim($info['status']), 0, 0, $am->project, $file->lang, $file->path, $file->name);
                 $this->conn->query($s, $params);
                 // Run the errorTools under this file
                 // If the EN file don't exist, it's because we have a file witch only exist into LANG, for example, translator.xml
                 // We fake the EN with the LANG content to fake the errorTools ;)
                 $tmpFile[0]['en_content'] = $file->read(true);
                 $tmpFile[0]['lang_content'] = $file->read(true);
                 $tmpFile[0]['lang'] = $file->lang;
                 $tmpFile[0]['path'] = $file->path;
                 $tmpFile[0]['name'] = $file->name;
                 $tmpFile[0]['maintainer'] = $info['maintainer'];
                 $errorTools = new ToolsError();
                 $errorTools->updateFilesError($tmpFile);
             }
         }
     }
 }
 /**
  * Check if a file have an error according to ToolsError's class.
  */
 public function checkFileError()
 {
     if (!AccountManager::getInstance()->isLogged()) {
         return JsonResponseBuilder::failure();
     }
     $FilePath = $this->getRequestVariable('FilePath');
     $FileName = $this->getRequestVariable('FileName');
     $FileLang = $this->getRequestVariable('FileLang');
     // Remove \
     $FileContent = stripslashes($this->getRequestVariable('FileContent'));
     // Replace   by space
     $FileContent = str_replace(" ", "", $FileContent);
     $file = new File($FileLang, $FilePath . $FileName);
     // Detect encoding
     $charset = $file->getEncoding($FileContent);
     // If the new charset is set to utf-8, we don't need to decode it
     if ($charset != 'utf-8') {
         // Utf8_decode
         $FileContent = utf8_decode($FileContent);
     }
     // Get EN content to check error with
     $en_file = new File('en', $FilePath . $FileName);
     $readOriginal = true;
     $en_content = $en_file->read($readOriginal);
     // Update DB with this new Error (if any)
     $info = $file->getInfo($FileContent);
     $anode[0] = array('lang' => $FileLang, 'path' => $FilePath, 'name' => $FileName, 'en_content' => $en_content, 'lang_content' => $FileContent, 'maintainer' => $info['maintainer']);
     $errorTools = new ToolsError();
     $r = $errorTools->updateFilesError($anode, 'nocommit');
     return JsonResponseBuilder::success(array('error' => $r['state'], 'error_first' => $r['first']));
 }