PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/rulemailcollector.class.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 333 lines | 255 code | 22 blank | 56 comment | 27 complexity | 3af930a2c38cdcce4ddd9fb2d6141fed MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id$
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2015 Teclib'.
  7. http://glpi-project.org
  8. based on GLPI - Gestionnaire Libre de Parc Informatique
  9. Copyright (C) 2003-2014 by the INDEPNET Development Team.
  10. -------------------------------------------------------------------------
  11. LICENSE
  12. This file is part of GLPI.
  13. GLPI is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. GLPI is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with GLPI. If not, see <http://www.gnu.org/licenses/>.
  23. --------------------------------------------------------------------------
  24. */
  25. /** @file
  26. * @brief
  27. */
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. /// Rule class for Rights management
  32. class RuleMailCollector extends Rule {
  33. // From Rule
  34. static $rightname = 'rule_mailcollector';
  35. public $orderby = "name";
  36. public $can_sort = true;
  37. // Temproray hack for this class in 0.84
  38. static function getTable() {
  39. return 'glpi_rules';
  40. }
  41. /**
  42. * @see Rule::maxActionsCount()
  43. **/
  44. function maxActionsCount() {
  45. return 1;
  46. }
  47. /**
  48. * @see Rule::getTitle()
  49. **/
  50. function getTitle() {
  51. return __('Rules for assigning a ticket created through a mails receiver');
  52. }
  53. /**
  54. * @see Rule::getCriterias()
  55. **/
  56. function getCriterias() {
  57. static $criterias = array();
  58. if (count($criterias)) {
  59. return $criterias;
  60. }
  61. $criterias['mailcollector']['field'] = 'name';
  62. $criterias['mailcollector']['name'] = __('Mails receiver');
  63. $criterias['mailcollector']['table'] = 'glpi_mailcollectors';
  64. $criterias['mailcollector']['type'] = 'dropdown';
  65. $criterias['_users_id_requester']['field'] = 'name';
  66. $criterias['_users_id_requester']['name'] = __('Requester');
  67. $criterias['_users_id_requester']['table'] = 'glpi_users';
  68. $criterias['_users_id_requester']['type'] = 'dropdown';
  69. $criterias['subject']['name'] = __('Subject email header');
  70. $criterias['subject']['field'] = 'subject';
  71. $criterias['subject']['table'] = '';
  72. $criterias['subject']['type'] = 'text';
  73. $criterias['content']['name'] = __('Email body');
  74. $criterias['content']['table'] = '';
  75. $criterias['content']['type'] = 'text';
  76. $criterias['from']['name'] = __('From email header');
  77. $criterias['from']['table'] = '';
  78. $criterias['from']['type'] = 'text';
  79. $criterias['to']['name'] = __('To email header');
  80. $criterias['to']['table'] = '';
  81. $criterias['to']['type'] = 'text';
  82. $criterias['in_reply_to']['name'] = __('In-Reply-To email header');
  83. $criterias['in_reply_to']['table'] = '';
  84. $criterias['in_reply_to']['type'] = 'text';
  85. $criterias['x-priority']['name'] = __('X-Priority email header');
  86. $criterias['x-priority']['table'] = '';
  87. $criterias['x-priority']['type'] = 'text';
  88. $criterias['x-auto-response-suppress']['name'] = __('X-Auto-Response-Suppress email header');
  89. $criterias['x-auto-response-suppress']['table'] = '';
  90. $criterias['x-auto-response-suppress']['type'] = 'text';
  91. $criterias['auto-submitted']['name'] = __('Auto-Submitted email header');
  92. $criterias['auto-submitted']['table'] = '';
  93. $criterias['auto-submitted']['type'] = 'text';
  94. /// Renater spam matching : X-UCE-Status = Yes
  95. $criterias['x-uce-status']['name'] = __('X-UCE-Status email header');
  96. $criterias['x-uce-status']['table'] = '';
  97. $criterias['x-uce-status']['type'] = 'text';
  98. $criterias['received']['name'] = __('Received email header');
  99. $criterias['received']['table'] = '';
  100. $criterias['received']['type'] = 'text';
  101. $criterias['GROUPS']['table'] = 'glpi_groups';
  102. $criterias['GROUPS']['field'] = 'completename';
  103. $criterias['GROUPS']['name'] = sprintf(__('%1$s: %2$s'),__('User'),
  104. __('Group'));
  105. $criterias['GROUPS']['linkfield'] = '';
  106. $criterias['GROUPS']['type'] = 'dropdown';
  107. $criterias['GROUPS']['virtual'] = true;
  108. $criterias['GROUPS']['id'] = 'groups';
  109. $criterias['KNOWN_DOMAIN']['field'] = 'name';
  110. $criterias['KNOWN_DOMAIN']['name'] = __('Known mail domain');
  111. $criterias['KNOWN_DOMAIN']['table'] = 'glpi_entities';
  112. $criterias['KNOWN_DOMAIN']['type'] = 'yesno';
  113. $criterias['KNOWN_DOMAIN']['virtual'] = true;
  114. $criterias['KNOWN_DOMAIN']['id'] = 'entitydatas';
  115. $criterias['KNOWN_DOMAIN']['allow_condition'] = array(Rule::PATTERN_IS);
  116. $criterias['PROFILES']['field'] = 'name';
  117. $criterias['PROFILES']['name'] = __('User featuring the profile');
  118. $criterias['PROFILES']['table'] = 'glpi_profiles';
  119. $criterias['PROFILES']['type'] = 'dropdown';
  120. $criterias['PROFILES']['virtual'] = true;
  121. $criterias['PROFILES']['id'] = 'profiles';
  122. $criterias['PROFILES']['allow_condition'] = array(Rule::PATTERN_IS);
  123. if (Session::isMultiEntitiesMode()) {
  124. $criterias['UNIQUE_PROFILE']['field'] = 'name';
  125. $criterias['UNIQUE_PROFILE']['name'] = __('User featuring a single profile');
  126. $criterias['UNIQUE_PROFILE']['table'] = 'glpi_profiles';
  127. $criterias['UNIQUE_PROFILE']['type'] = 'dropdown';
  128. $criterias['UNIQUE_PROFILE']['virtual'] = true;
  129. $criterias['UNIQUE_PROFILE']['id'] = 'profiles';
  130. $criterias['UNIQUE_PROFILE']['allow_condition'] = array(Rule::PATTERN_IS);
  131. }
  132. $criterias['ONE_PROFILE']['field'] = 'name';
  133. $criterias['ONE_PROFILE']['name'] = __('User with a single profile');
  134. $criterias['ONE_PROFILE']['table'] = '';
  135. $criterias['ONE_PROFILE']['type'] = 'yesonly';
  136. $criterias['ONE_PROFILE']['virtual'] = true;
  137. $criterias['ONE_PROFILE']['id'] = 'profiles';
  138. $criterias['ONE_PROFILE']['allow_condition'] = array(Rule::PATTERN_IS);
  139. return $criterias;
  140. }
  141. /**
  142. * @see Rule::getActions()
  143. **/
  144. function getActions() {
  145. $actions = array();
  146. $actions['entities_id']['name'] = __('Entity');
  147. $actions['entities_id']['type'] = 'dropdown';
  148. $actions['entities_id']['table'] = 'glpi_entities';
  149. $actions['_affect_entity_by_domain']['name'] = __('Entity from domain');
  150. $actions['_affect_entity_by_domain']['type'] = 'text';
  151. $actions['_affect_entity_by_domain']['force_actions'] = array('regex_result');
  152. $actions['_affect_entity_by_tag']['name'] = __('Entity from TAG');
  153. $actions['_affect_entity_by_tag']['type'] = 'text';
  154. $actions['_affect_entity_by_tag']['force_actions'] = array('regex_result');
  155. $actions['_affect_entity_by_user_entity']['name'] = __("Entity based on user's profile");
  156. $actions['_affect_entity_by_user_entity']['type'] = 'yesonly';
  157. $actions['_affect_entity_by_user_entity']['table'] = '';
  158. $actions['_refuse_email_no_response']['name'] = sprintf(__('%1$s (%2$s)'), __('Reject email'),
  159. __('without email response'));
  160. $actions['_refuse_email_no_response']['type'] = 'yesonly';
  161. $actions['_refuse_email_no_response']['table'] = '';
  162. $actions['_refuse_email_with_response']['name'] = sprintf(__('%1$s (%2$s)'), __('Reject email'),
  163. __('with email response'));
  164. $actions['_refuse_email_with_response']['type'] = 'yesonly';
  165. $actions['_refuse_email_with_response']['table'] = '';
  166. return $actions;
  167. }
  168. /**
  169. * @see Rule::executeActions()
  170. **/
  171. function executeActions($output,$params) {
  172. if (count($this->actions)) {
  173. foreach ($this->actions as $action) {
  174. switch ($action->fields["action_type"]) {
  175. case "assign" :
  176. switch ($action->fields["field"]) {
  177. default :
  178. $output[$action->fields["field"]] = $action->fields["value"];
  179. break;
  180. case "_affect_entity_by_user_entity":
  181. //3 cases :
  182. //1 - rule contains a criteria like : Profil is XXXX
  183. // -> in this case, profiles_id is stored in
  184. // $this->criterias_results['PROFILES'] (one value possible)
  185. //2- rule contains criteria "User has only one profile"
  186. // -> in this case, profiles_id is stored in
  187. // $this->criterias_results['PROFILES'] (one value possible) (same as 1)
  188. //3 -> rule contains only one profile
  189. $profile = 0;
  190. //Case 2:
  191. if (isset($this->criterias_results['ONE_PROFILE'])) {
  192. $profile = $this->criterias_results['ONE_PROFILE'];
  193. //Case 3
  194. } else if (isset($this->criterias_results['UNIQUE_PROFILE'])) {
  195. $profile = $this->criterias_results['UNIQUE_PROFILE'];
  196. //Case 1
  197. } else if (isset($this->criterias_results['PROFILES'])) {
  198. $profile = $this->criterias_results['PROFILES'];
  199. }
  200. if ($profile) {
  201. $entities = array();
  202. if (isset($params['_users_id_requester'])) { // Not set when testing
  203. $entities
  204. = Profile_User::getEntitiesForProfileByUser($params['_users_id_requester'],
  205. $profile);
  206. }
  207. //Case 2 : check if there's only one profile for this user
  208. if ((isset($this->criterias_results['ONE_PROFILE'])
  209. && (count($entities) == 1))
  210. || !isset($this->criterias_results['ONE_PROFILE'])) {
  211. if (count($entities) == 1) {
  212. //User has right on only one entity
  213. $output['entities_id'] = array_pop($entities);
  214. } else if (isset($this->criterias_results['UNIQUE_PROFILE'])) {
  215. $output['entities_id'] = array_pop($entities);
  216. } else {
  217. //Rights on more than one entity : get the user's prefered entity
  218. if (isset($params['_users_id_requester'])) { // Not set when testing
  219. $user = new User();
  220. $user->getFromDB($params['_users_id_requester']);
  221. $tmpid = $user->getField('entities_id');
  222. // Retrieve all the entities (pref could be set on a child)
  223. $entities
  224. = Profile_User::getEntitiesForProfileByUser($params['_users_id_requester'],
  225. $profile, true);
  226. // If an entity is defined in user's preferences,
  227. // and this entity allowed for this profile, use this one
  228. // else do not set the rule as matched
  229. if (in_array($tmpid, $entities)) {
  230. $output['entities_id'] = $user->fields['entities_id'];
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. break;
  238. case "regex_result" :
  239. foreach ($this->regex_results as $regex_result) {
  240. $entity_found = -1;
  241. $res = RuleAction::getRegexResultById($action->fields["value"],
  242. $regex_result);
  243. if ($res != null) {
  244. switch ($action->fields["field"]) {
  245. case "_affect_entity_by_domain" :
  246. $entity_found = Entity::getEntityIDByDomain(addslashes($res));
  247. break;
  248. case "_affect_entity_by_tag" :
  249. $entity_found = Entity::getEntityIDByTag(addslashes($res));
  250. break;
  251. }
  252. //If an entity was found
  253. if ($entity_found > -1) {
  254. $output['entities_id'] = $entity_found;
  255. break;
  256. }
  257. }
  258. } // switch (field)
  259. break;
  260. }
  261. }
  262. }
  263. return $output;
  264. }
  265. }
  266. ?>