Example #1
0
 /**
  * Runs the test.
  */
 public function test()
 {
     $path = DIR_FILES . '/mail/logo.gif';
     $name = 'logo.gif';
     $mimeType = 'image/gif';
     $attachment = new File($path, $name, $mimeType);
     $this->assertEquals(file_get_contents($path), $attachment->getContent());
     $this->assertEquals($name, $attachment->getName());
     $this->assertEquals($mimeType, $attachment->getMimeType());
     $this->assertEquals(\Jyxo\Mail\Email\Attachment::DISPOSITION_ATTACHMENT, $attachment->getDisposition());
     $this->assertFalse($attachment->isInline());
     $this->assertEquals('', $attachment->getCid());
     $this->assertEquals('', $attachment->getEncoding());
 }
 /**
  * Save a file. The new file have an extension like ".new", and is saved in the same folder as the original.
  * 
  * @HERE : The new file no more have an extension '.new'. Now, it's saved into 'module-name-'new folder, with the same folder's hierarchie
  *
  *
  */
 public function saveFile()
 {
     $am = AccountManager::getInstance();
     $tx = new ToolsXmllint();
     if (!$am->isLogged()) {
         return JsonResponseBuilder::failure();
     }
     $filePath = $this->getRequestVariable('filePath');
     $fileName = $this->getRequestVariable('fileName');
     $fileLang = $this->getRequestVariable('fileLang');
     $type = $this->hasRequestVariable('type') ? $this->getRequestVariable('type') : 'file';
     $emailAlert = $this->hasRequestVariable('emailAlert') ? $this->getRequestVariable('emailAlert') : '';
     // Clean up path
     $filePath = str_replace('//', '/', $filePath);
     // Extract lang from path
     if ($fileLang == 'all') {
         $t = explode('/', $filePath);
         $fileLang = $t[0];
         array_shift($t);
         $filePath = '/' . implode('/', $t);
     }
     // Remove \
     $fileContent = $this->getRequestVariable('fileContent');
     // Replace   by space
     $fileContent = str_replace(" ", "", $fileContent);
     // We check the Xml consistence only for .xml file
     if (substr($fileName, -3) == 'xml') {
         $xmlError = $tx->checkForError($fileContent);
         if ($xmlError != 'no_error') {
             return JsonResponseBuilder::failure(array('XmlError' => $xmlError));
         }
     }
     // Get file object
     $file = new File($fileLang, $filePath . $fileName);
     // Rules to allow this file to be saved or not.
     if ($infoModified = $file->isModified()) {
         $infoModified = json_decode($infoModified);
         // If the user who have modified this file isn't the current one
         if ($am->userID == $infoModified->userID) {
             // We can modify it, it's mine ;)
         } else {
             // If the current user have karma, he can modify it.
             if ($am->haveKarma) {
                 // The current user can modify it
             } else {
                 // We must trow an error. We can't modify it.
                 return JsonResponseBuilder::failure(array('type' => 'save_you_cant_modify_it'));
             }
         }
     }
     // 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') {
         $fileContent = iconv("UTF-8", $charset, $fileContent);
     }
     // We detect tab caracters and trow an error if we find one.
     if (strstr("\t", $fileContent)) {
         return JsonResponseBuilder::failure(array('type' => 'tabs_found'));
     }
     // Get revision
     $info = $file->getInfo($fileContent);
     if ($type == 'file') {
         $er = $file->save($fileContent);
         $isError = is_array($er) && empty($er['state']);
         if (!$isError) {
             $r = RepositoryManager::getInstance()->addProgressWork($file, $info['rev'], $info['en-rev'], $info['reviewed'], $info['reviewed_maintainer'], $info['maintainer']);
             return JsonResponseBuilder::success(array('id' => $r, 'lang' => $fileLang, 'revision' => $info['rev'], 'en_revision' => $info['en-rev'], 'maintainer' => $info['maintainer'], 'reviewed' => $info['reviewed'], 'reviewed_maintainer' => $info['reviewed_maintainer']));
         } else {
             return JsonResponseBuilder::failure(array('type' => 'fs_error'));
         }
     } else {
         if ($type == 'trans') {
             // We must ensure that this folder exist localy
             if ($file->folderExist()) {
                 $er = $file->save($fileContent);
                 if ($er['state']) {
                     $r = RepositoryManager::getInstance()->addProgressWork($file, $info['rev'], $info['en-rev'], $info['reviewed'], $info['reviewed_maintainer'], $info['maintainer'], 'new');
                     return JsonResponseBuilder::success(array('id' => $r, 'lang' => $fileLang, 'revision' => $info['rev'], 'en_revision' => $info['en-rev'], 'maintainer' => $info['maintainer'], 'reviewed' => $info['reviewed'], 'reviewed_maintainer' => $info['reviewed_maintainer']));
                 } else {
                     return JsonResponseBuilder::failure(array('type' => 'fs_error'));
                 }
             } else {
                 return JsonResponseBuilder::failure();
             }
         }
     }
 }
 /**
  * Read the translation's file which hold informations about all translators
  * and put it into database.
  */
 public function updateTranslatorInfo()
 {
     $ExistingLanguage = $this->getExistingLanguage();
     $am = AccountManager::getInstance();
     foreach ($ExistingLanguage as $lang) {
         $lang = $lang["code"];
         $txml = false;
         // Path to find translation.xml file, set default values,
         // in case we can't find the translation file
         $translation_xml = new File($lang, '/translation.xml');
         if (file_exists($translation_xml->full_path)) {
             // Else go on, and load in the file, replacing all
             // space type chars with one space
             $txml = preg_replace('/\\s+/', ' ', $translation_xml->read(true));
         }
         if ($txml) {
             // Find all persons matching the pattern
             $matches = array();
             if (preg_match_all('!<person (.+)/\\s?>!U', $txml, $matches)) {
                 $default = array('vcs' => 'n/a', 'nick' => 'n/a', 'editor' => 'n/a', 'email' => 'n/a', 'name' => 'n/a');
                 $persons = $translation_xml->parseAttribute($matches[1]);
                 $charset = $translation_xml->getEncoding($txml);
                 foreach ($persons as $person) {
                     if ($charset == 'utf-8') {
                         $name = utf8_decode($person['name']);
                     } else {
                         $name = $person['name'];
                     }
                     $person = array_merge($default, $person);
                     // We try to remove this record if it exist
                     $query = 'DELETE FROM `translators` WHERE `project`="%s" AND `lang`="%s" AND `nick`="%s"';
                     $params = array($am->project, $lang, $person['nick']);
                     $this->conn->query($query, $params);
                     $query = 'INSERT INTO `translators` (`project`, `lang`, `nick`, `name`, `mail`, `vcs`, `editor`)
                          VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s")';
                     $params = array($am->project, $lang, $person['nick'], $name, $person['email'], $person['vcs'], $person['editor']);
                     $this->conn->query($query, $params);
                 }
             }
         }
     }
 }
Example #4
0
 /**
  * __construct() should return chunker if $encoding exists
  */
 public function testConstruct_returnsObject_ifFileAndEncodingDoesExist()
 {
     $chunker = new File($this->fileIsEmpty, 'UTF-8');
     $this->assertEquals($this->fileIsEmpty, $chunker->getName());
     $this->assertEquals('UTF-8', $chunker->getEncoding());
     return;
 }