getAvailableTranslations() static public method

Get available translations for a language
static public getAvailableTranslations ( $language ) : array
$language language
return array of table / field translated item
 /**
  * @covers NotificationTargetTicket::getTags
  * @covers NotificationTargetCommonITILObject::getTags
  * @covers NotificationTargetTicket::getDatasForObject
  * @covers NotificationTargetCommonITILObject::getDatasForObject
  */
 public function testgetDatasForObject()
 {
     global $CFG_GLPI;
     $tkt = getItemByTypeName('Ticket', '_ticket01');
     $notiftargetticket = new NotificationTargetTicket(getItemByTypeName('Entity', '_test_root_entity', true), 'new', $tkt);
     $notiftargetticket->getTags();
     // basic test for ##task.categorycomment## tag
     $expected = ['tag' => 'task.categorycomment', 'value' => true, 'label' => 'Category comment', 'events' => 0, 'foreach' => false, 'lang' => true, 'allowed_values' => []];
     $this->assertEquals($expected, $notiftargetticket->tag_descriptions['lang']['##lang.task.categorycomment##']);
     $this->assertEquals($expected, $notiftargetticket->tag_descriptions['tag']['##task.categorycomment##']);
     // basic test for ##task.categorid## tag
     $expected = ['tag' => 'task.categoryid', 'value' => true, 'label' => 'Category id', 'events' => 0, 'foreach' => false, 'lang' => true, 'allowed_values' => []];
     $this->assertEquals($expected, $notiftargetticket->tag_descriptions['lang']['##lang.task.categoryid##']);
     $this->assertEquals($expected, $notiftargetticket->tag_descriptions['tag']['##task.categoryid##']);
     // advanced test for ##task.categorycomment## and ##task.categoryid## tags
     // test of the getDatasForObject for default language en_US
     $taskcat = getItemByTypeName('TaskCategory', '_subcat_1');
     $expected = [['##task.id##' => 1, '##task.isprivate##' => 'No', '##task.author##' => '_test_user', '##task.categoryid##' => $taskcat->getID(), '##task.category##' => '_cat_1 > _subcat_1', '##task.categorycomment##' => 'Comment for sub-category _subcat_1', '##task.date##' => '2016-10-19 11:50', '##task.description##' => 'Task to be done', '##task.time##' => '0 seconds', '##task.status##' => 'To do', '##task.user##' => '_test_user', '##task.group##' => '', '##task.begin##' => '', '##task.end##' => '']];
     $ret = $notiftargetticket->getDatasForObject($tkt, array());
     $this->assertEquals($expected, $ret['tasks']);
     // test of the getDatasForObject for default language fr_FR
     $CFG_GLPI['translate_dropdowns'] = 1;
     $_SESSION["glpilanguage"] = Session::loadLanguage('fr_FR');
     $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);
     $ret = $notiftargetticket->getDatasForObject($tkt, array());
     $expected = [['##task.id##' => 1, '##task.isprivate##' => 'Non', '##task.author##' => '_test_user', '##task.categoryid##' => $taskcat->getID(), '##task.category##' => 'FR - _cat_1 > FR - _subcat_1', '##task.categorycomment##' => 'FR - Commentaire pour sous-catégorie _subcat_1', '##task.date##' => '2016-10-19 11:50', '##task.description##' => 'Task to be done', '##task.time##' => '0 seconde', '##task.status##' => 'A faire', '##task.user##' => '_test_user', '##task.group##' => '', '##task.begin##' => '', '##task.end##' => '']];
     $this->assertEquals($expected, $ret['tasks']);
     // switch back to default language
     $_SESSION["glpilanguage"] = Session::loadLanguage('en_US');
 }
示例#2
0
 function prepareInputForUpdate($input)
 {
     global $CFG_GLPI;
     //picture manually uploaded by user
     if (isset($input["_blank_picture"]) && $input["_blank_picture"]) {
         self::dropPictureFiles($this->fields['picture']);
         $input['picture'] = 'NULL';
     } else {
         if (isset($_FILES['picture'])) {
             if (!count($_FILES['picture']) || empty($_FILES['picture']['name']) || !is_file($_FILES['picture']['tmp_name'])) {
                 switch ($_FILES['picture']['error']) {
                     case UPLOAD_ERR_INI_SIZE:
                     case UPLOAD_ERR_FORM_SIZE:
                         Session::addMessageAfterRedirect(__('File too large to be added.'), false, ERROR);
                         break;
                     case UPLOAD_ERR_NO_FILE:
                         // Session::addMessageAfterRedirect(__('No file specified.'),false,ERROR);
                         break;
                 }
             } else {
                 if (toolbox::getMime($_FILES['picture']['tmp_name'], 'image')) {
                     // Unlink old picture (clean on changing format)
                     self::dropPictureFiles($this->fields['picture']);
                     // Move uploaded file
                     $filename = uniqid($this->fields['id'] . '_');
                     $sub = substr($filename, -2);
                     /* 2 hex digit */
                     $tmp = explode(".", $_FILES['picture']['name']);
                     $extension = array_pop($tmp);
                     @mkdir(GLPI_PICTURE_DIR . "/{$sub}");
                     $picture_path = GLPI_PICTURE_DIR . "/{$sub}/{$filename}.{$extension}";
                     self::dropPictureFiles($filename . "." . $extension);
                     if (in_array($extension, array('jpg', 'jpeg', 'png', 'bmp', 'gif')) && Document::renameForce($_FILES['picture']['tmp_name'], $picture_path)) {
                         Session::addMessageAfterRedirect(__('The file is valid. Upload is successful.'));
                         // For display
                         $input['picture'] = "{$sub}/{$filename}.{$extension}";
                         //prepare a thumbnail
                         $thumb_path = GLPI_PICTURE_DIR . "/{$sub}/{$filename}_min.{$extension}";
                         Toolbox::resizePicture($picture_path, $thumb_path);
                     } else {
                         Session::addMessageAfterRedirect(__('Potential upload attack or file too large. Moving temporary file failed.'), false, ERROR);
                     }
                 } else {
                     Session::addMessageAfterRedirect(__('The file is not an image file.'), false, ERROR);
                 }
             }
         } else {
             //ldap jpegphoto synchronisation.
             if (isset($this->fields["authtype"]) && ($this->fields["authtype"] == Auth::LDAP || Auth::isAlternateAuth($this->fields['authtype'])) && ($picture = $this->syncLdapPhoto())) {
                 if (!empty($picture)) {
                     $input['picture'] = $picture;
                 }
             }
         }
     }
     if (isset($input["password2"])) {
         // Empty : do not update
         if (empty($input["password"])) {
             unset($input["password"]);
         } else {
             if ($input["password"] == $input["password2"]) {
                 // Check right : my password of user with lesser rights
                 if (isset($input['id']) && Config::validatePassword($input["password"]) && ($input['id'] == Session::getLoginUserID() || $this->currentUserHaveMoreRightThan($input['id']) || $input['password_forget_token'] == $this->fields['password_forget_token'] && abs(strtotime($_SESSION["glpi_currenttime"]) - strtotime($this->fields['password_forget_token_date'])) < DAY_TIMESTAMP && $this->isEmail($input['email']))) {
                     $input["password"] = Auth::getPasswordHash(Toolbox::unclean_cross_side_scripting_deep(stripslashes($input["password"])));
                 } else {
                     unset($input["password"]);
                 }
                 unset($input["password2"]);
             } else {
                 Session::addMessageAfterRedirect(__('Error: the two passwords do not match'), false, ERROR);
                 return false;
             }
         }
     } else {
         if (isset($input["password"])) {
             // From login
             unset($input["password"]);
         }
     }
     // Update User in the database
     if (!isset($input["id"]) && isset($input["name"])) {
         if ($this->getFromDBbyName($input["name"])) {
             $input["id"] = $this->fields["id"];
         }
     }
     if (isset($input["entities_id"]) && Session::getLoginUserID() === $input['id']) {
         $_SESSION["glpidefault_entity"] = $input["entities_id"];
     }
     // Security on default profile update
     if (isset($input['profiles_id'])) {
         if (!in_array($input['profiles_id'], Profile_User::getUserProfiles($input['id']))) {
             unset($input['profiles_id']);
         }
     }
     // Security on default entity  update
     if (isset($input['entities_id'])) {
         if (!in_array($input['entities_id'], Profile_User::getUserEntities($input['id']))) {
             unset($input['entities_id']);
         }
     }
     if (isset($input['_reset_personal_token'])) {
         $input['personal_token'] = self::getUniquePersonalToken();
         $input['personal_token_date'] = $_SESSION['glpi_currenttime'];
     }
     // Manage preferences fields
     if (Session::getLoginUserID() === $input['id']) {
         if (isset($input['use_mode']) && $_SESSION['glpi_use_mode'] != $input['use_mode']) {
             $_SESSION['glpi_use_mode'] = $input['use_mode'];
             //Session::loadLanguage();
         }
     }
     foreach ($CFG_GLPI['user_pref_field'] as $f) {
         if (isset($input[$f])) {
             if (Session::getLoginUserID() === $input['id']) {
                 if ($_SESSION["glpi{$f}"] != $input[$f]) {
                     $_SESSION["glpi{$f}"] = $input[$f];
                     // reinit translations
                     if ($f == 'language') {
                         $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);
                         unset($_SESSION['glpimenu']);
                     }
                 }
             }
             if ($input[$f] == $CFG_GLPI[$f]) {
                 $input[$f] = "NULL";
             }
         }
     }
     return $input;
 }
示例#3
0
 /**
  * @param $target             NotificationTarget object
  * @param $user_infos   array
  * @param $event
  * @param $options      array
  *
  * @return id of the template in templates_by_languages / false if computation failed
  **/
 function getTemplateByLanguage(NotificationTarget $target, $user_infos = array(), $event, $options = array())
 {
     $lang = array();
     $language = $user_infos['language'];
     if (isset($user_infos['additionnaloption'])) {
         $additionnaloption = $user_infos['additionnaloption'];
     } else {
         $additionnaloption = array();
     }
     $tid = $language;
     $tid .= serialize($additionnaloption);
     $tid = sha1($tid);
     if (!isset($this->templates_by_languages[$tid])) {
         //Switch to the desired language
         $bak_dropdowntranslations = $_SESSION['glpi_dropdowntranslations'];
         $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($language);
         Session::loadLanguage($language);
         $bak_language = $_SESSION["glpilanguage"];
         $_SESSION["glpilanguage"] = $language;
         //If event is raised by a plugin, load it in order to get the language file available
         if ($plug = isPluginItemType(get_class($target->obj))) {
             Plugin::loadLang(strtolower($plug['plugin']), $language);
         }
         //Get template's language data for in this language
         $options['additionnaloption'] = $additionnaloption;
         $data =& $target->getForTemplate($event, $options);
         $footer_string = Html::entity_decode_deep(sprintf(__('Automatically generated by GLPI %s'), GLPI_VERSION));
         $add_header = Html::entity_decode_deep($target->getContentHeader());
         $add_footer = Html::entity_decode_deep($target->getContentFooter());
         //Restore default language
         $_SESSION["glpilanguage"] = $bak_language;
         Session::loadLanguage();
         $_SESSION['glpi_dropdowntranslations'] = $bak_dropdowntranslations;
         if ($plug = isPluginItemType(get_class($target->obj))) {
             Plugin::loadLang(strtolower($plug['plugin']));
         }
         if ($template_datas = $this->getByLanguage($language)) {
             //Template processing
             // Decode html chars to have clean text
             $template_datas['content_text'] = Html::entity_decode_deep($template_datas['content_text']);
             $save_data = $data;
             $data = Html::entity_decode_deep($data);
             $template_datas['subject'] = Html::entity_decode_deep($template_datas['subject']);
             $this->signature = Html::entity_decode_deep($this->signature);
             $lang['subject'] = $target->getSubjectPrefix($event) . self::process($template_datas['subject'], $data);
             $lang['content_html'] = '';
             //If no html content, then send only in text
             if (!empty($template_datas['content_html'])) {
                 // Encode in HTML all chars
                 $data_html = Html::entities_deep($data);
                 $data_html = Html::nl2br_deep($data_html);
                 // Restore HTML tags
                 if (count($target->html_tags)) {
                     foreach ($target->html_tags as $tag) {
                         if (isset($save_data[$tag])) {
                             $data_html[$tag] = $save_data[$tag];
                         }
                     }
                 }
                 $signature_html = Html::entities_deep($this->signature);
                 $signature_html = Html::nl2br_deep($signature_html);
                 $template_datas['content_html'] = self::process($template_datas['content_html'], $data_html);
                 $lang['content_html'] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n                        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>" . "<html>\n                        <head>\n                         <META http-equiv='Content-Type' content='text/html; charset=utf-8'>\n                         <title>" . Html::entities_deep($lang['subject']) . "</title>\n                         <style type='text/css'>\n                           " . $this->fields['css'] . "\n                         </style>\n                        </head>\n                        <body>\n" . (!empty($add_header) ? $add_header . "\n<br><br>" : '') . $template_datas['content_html'] . "<br><br>-- \n<br>" . $signature_html . "<br>{$footer_string}" . "<br><br>\n" . (!empty($add_footer) ? $add_footer . "\n<br><br>" : '') . "\n</body></html>";
             }
             $lang['content_text'] = (!empty($add_header) ? $add_header . "\n\n" : '') . Html::clean(self::process($template_datas['content_text'], $data) . "\n\n-- \n" . $this->signature . "\n" . Html::entity_decode_deep(sprintf(__('Automatically generated by GLPI %s'), GLPI_VERSION))) . "\n\n" . $add_footer;
             $this->templates_by_languages[$tid] = $lang;
         }
     }
     if (isset($this->templates_by_languages[$tid])) {
         return $tid;
     }
     return false;
 }
示例#4
0
 /**
  * Init session for the user is defined
  *
  * @param $auth Auth object to init session
  *
  * @return nothing
  **/
 static function init(Auth $auth)
 {
     global $CFG_GLPI;
     if ($auth->auth_succeded) {
         // Restart GLPi session : complete destroy to prevent lost datas
         $tosave = array('glpi_plugins', 'glpicookietest', 'phpCAS', 'glpicsrftokens', 'glpiskipMaintenance');
         $save = array();
         foreach ($tosave as $t) {
             if (isset($_SESSION[$t])) {
                 $save[$t] = $_SESSION[$t];
             }
         }
         self::destroy();
         session_regenerate_id();
         self::start();
         $_SESSION = $save;
         $_SESSION['valid_id'] = session_id();
         // Define default time :
         $_SESSION["glpi_currenttime"] = date("Y-m-d H:i:s");
         // Normal mode for this request
         $_SESSION["glpi_use_mode"] = self::NORMAL_MODE;
         // Check ID exists and load complete user from DB (plugins...)
         if (isset($auth->user->fields['id']) && $auth->user->getFromDB($auth->user->fields['id'])) {
             if (!$auth->user->fields['is_deleted'] && ($auth->user->fields['is_active'] && ($auth->user->fields['begin_date'] < $_SESSION["glpi_currenttime"] || is_null($auth->user->fields['begin_date'])) && ($auth->user->fields['end_date'] > $_SESSION["glpi_currenttime"] || is_null($auth->user->fields['end_date'])))) {
                 $_SESSION["glpiID"] = $auth->user->fields['id'];
                 $_SESSION["glpiname"] = $auth->user->fields['name'];
                 $_SESSION["glpirealname"] = $auth->user->fields['realname'];
                 $_SESSION["glpifirstname"] = $auth->user->fields['firstname'];
                 $_SESSION["glpidefault_entity"] = $auth->user->fields['entities_id'];
                 $_SESSION["glpiusers_idisation"] = true;
                 $_SESSION["glpiextauth"] = $auth->extauth;
                 $_SESSION["glpiauthtype"] = $auth->user->fields['authtype'];
                 $_SESSION["glpiroot"] = $CFG_GLPI["root_doc"];
                 $_SESSION["glpi_use_mode"] = $auth->user->fields['use_mode'];
                 $_SESSION["glpicrontimer"] = time();
                 // Default tab
                 //               $_SESSION['glpi_tab']=1;
                 $_SESSION['glpi_tabs'] = array();
                 $auth->user->computePreferences();
                 foreach ($CFG_GLPI['user_pref_field'] as $field) {
                     if (isset($auth->user->fields[$field])) {
                         $_SESSION["glpi{$field}"] = $auth->user->fields[$field];
                     }
                 }
                 // Do it here : do not reset on each page, cause export issue
                 if ($_SESSION["glpilist_limit"] > $CFG_GLPI['list_limit_max']) {
                     $_SESSION["glpilist_limit"] = $CFG_GLPI['list_limit_max'];
                 }
                 // Init not set value for language
                 if (empty($_SESSION["glpilanguage"])) {
                     $_SESSION["glpilanguage"] = $CFG_GLPI['language'];
                 }
                 $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);
                 self::loadLanguage();
                 // glpiprofiles -> other available profile with link to the associated entities
                 Plugin::doHook("init_session");
                 self::initEntityProfiles(self::getLoginUserID());
                 // Use default profile if exist
                 if (isset($_SESSION['glpiprofiles'][$auth->user->fields['profiles_id']])) {
                     self::changeProfile($auth->user->fields['profiles_id']);
                 } else {
                     // Else use first
                     self::changeProfile(key($_SESSION['glpiprofiles']));
                 }
                 if (!isset($_SESSION["glpiactiveprofile"]["interface"])) {
                     $auth->auth_succeded = false;
                     $auth->addToError(__("You don't have right to connect"));
                 }
             } else {
                 $auth->auth_succeded = false;
                 $auth->addToError(__("You don't have access to this application because your account was deactivated or removed"));
             }
         } else {
             $auth->auth_succeded = false;
             $auth->addToError(__("You don't have right to connect"));
         }
     }
 }
示例#5
0
 /**
  * @covers Dropdown::getDropdownName
  * @covers ::getTreeValueCompleteName
  */
 public function testGetDropdownName()
 {
     global $CFG_GLPI;
     $cat = getItemByTypeName('TaskCategory', '_cat_1');
     $subCat = getItemByTypeName('TaskCategory', '_subcat_1');
     // basic test returns string only
     $expected = $cat->fields['name'] . " > " . $subCat->fields['name'];
     $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID());
     $this->assertEquals($expected, $ret);
     // test of return with comments
     $expected = array('name' => $cat->fields['name'] . " > " . $subCat->fields['name'], 'comment' => "<span class='b'>Complete name</span>: " . $cat->fields['name'] . " > " . $subCat->fields['name'] . "<br><span class='b'>&nbsp;Comments&nbsp;</span>" . $subCat->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true);
     $this->assertEquals($expected, $ret);
     // test of return without $tooltip
     $expected = array('name' => $cat->fields['name'] . " > " . $subCat->fields['name'], 'comment' => $subCat->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
     // test of return with translations
     $CFG_GLPI['translate_dropdowns'] = 1;
     $_SESSION["glpilanguage"] = Session::loadLanguage('fr_FR');
     $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]);
     $expected = array('name' => 'FR - _cat_1 > FR - _subcat_1', 'comment' => 'FR - Commentaire pour sous-catégorie _subcat_1');
     $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
     // switch back to default language
     $_SESSION["glpilanguage"] = Session::loadLanguage('en_US');
     ////////////////////////////////
     // test for other dropdown types
     ////////////////////////////////
     ///////////
     // Computer
     $computer = getItemByTypeName('Computer', '_test_pc01');
     $ret = Dropdown::getDropdownName('glpi_computers', $computer->getID());
     $this->assertEquals($computer->getName(), $ret);
     $expected = array('name' => $computer->getName(), 'comment' => $computer->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_computers', $computer->getID(), true);
     $this->assertEquals($expected, $ret);
     //////////
     // Contact
     $contact = getItemByTypeName('Contact', '_contact01_name');
     $expected = $contact->getName();
     $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID());
     $this->assertEquals($expected, $ret);
     // test of return with comments
     $expected = array('name' => $contact->getName(), 'comment' => "Comment for contact _contact01_name<br><span class='b'>" . "Phone: </span>0123456789<br><span class='b'>Phone 2: </span>0123456788<br><span class='b'>" . "Mobile phone: </span>0623456789<br><span class='b'>Fax: </span>0123456787<br>" . "<span class='b'>Email: </span>_contact01_firstname._contact01_name@glpi.com");
     $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID(), true);
     $this->assertEquals($expected, $ret);
     // test of return without $tooltip
     $expected = array('name' => $contact->getName(), 'comment' => $contact->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
     ///////////
     // Supplier
     $supplier = getItemByTypeName('Supplier', '_suplier01_name');
     $expected = $supplier->getName();
     $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID());
     $this->assertEquals($expected, $ret);
     // test of return with comments
     $expected = array('name' => $supplier->getName(), 'comment' => "Comment for supplier _suplier01_name<br><span class='b'>Phone: </span>0123456789<br>" . "<span class='b'>Fax: </span>0123456787<br><span class='b'>Email: </span>info@_supplier01_name.com");
     $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID(), true);
     $this->assertEquals($expected, $ret);
     // test of return without $tooltip
     $expected = array('name' => $supplier->getName(), 'comment' => $supplier->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
     ///////////
     // Netpoint
     $netpoint = getItemByTypeName('Netpoint', '_netpoint01');
     $location = getItemByTypeName('Location', '_location01');
     $expected = $netpoint->getName() . " (" . $location->getName() . ")";
     $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID());
     $this->assertEquals($expected, $ret);
     // test of return with comments
     $expected = array('name' => $expected, 'comment' => "Comment for netpoint _netpoint01");
     $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID(), true);
     $this->assertEquals($expected, $ret);
     // test of return without $tooltip
     $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
     ///////////
     // Budget
     $budget = getItemByTypeName('Budget', '_budget01');
     $expected = $budget->getName();
     $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID());
     $this->assertEquals($expected, $ret);
     // test of return with comments
     $expected = array('name' => $budget->getName(), 'comment' => "Comment for budget _budget01<br><span class='b'>Location</span>: " . "_location01<br><span class='b'>Type</span>: _budgettype01<br><span class='b'>" . "Start date</span>: 2016-10-18 <br><span class='b'>End date</span>: 2016-12-31 ");
     $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID(), true);
     $this->assertEquals($expected, $ret);
     // test of return without $tooltip
     $expected = array('name' => $budget->getName(), 'comment' => $budget->fields['comment']);
     $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID(), true, true, false);
     $this->assertEquals($expected, $ret);
 }