action.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. require_once('../core/email.class.inc.php');
  3. /**
  4. * A user defined action, to customize the application
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. abstract class Action extends cmdbAbstractObject
  15. {
  16. public static function Init()
  17. {
  18. $aParams = array
  19. (
  20. "category" => "core/cmdb",
  21. "key_type" => "autoincrement",
  22. "key_label" => "",
  23. "name_attcode" => "name",
  24. "state_attcode" => "",
  25. "reconc_keys" => array(),
  26. "db_table" => "priv_action",
  27. "db_key_field" => "id",
  28. "db_finalclass_field" => "realclass",
  29. "display_template" => "",
  30. );
  31. MetaModel::Init_Params($aParams);
  32. //MetaModel::Init_InheritAttributes();
  33. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  34. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  35. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum(array('test'=>'Being tested' ,'enabled'=>'In production', 'disabled'=>'Inactive')), "sql"=>"status", "default_value"=>"test", "is_null_allowed"=>false, "depends_on"=>array())));
  36. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("related_triggers", array("linked_class"=>"lnkTriggerAction", "ext_key_to_me"=>"action_id", "ext_key_to_remote"=>"trigger_id", "allowed_values"=>null, "count_min"=>0, "count_max"=>0, "depends_on"=>array())));
  37. //MetaModel::Init_InheritFilters();
  38. MetaModel::Init_AddFilterFromAttribute("name");
  39. MetaModel::Init_AddFilterFromAttribute("description");
  40. // Display lists
  41. MetaModel::Init_SetZListItems('details', array('name', 'description', 'status')); // Attributes to be displayed for the complete details
  42. MetaModel::Init_SetZListItems('list', array('finalclass', 'name', 'description', 'status')); // Attributes to be displayed for a list
  43. // Search criteria
  44. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  45. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  46. }
  47. abstract public function DoExecute($oTrigger, $aContextArgs);
  48. public function IsActive()
  49. {
  50. switch($this->Get('status'))
  51. {
  52. case 'enabled':
  53. case 'test':
  54. return true;
  55. default:
  56. return false;
  57. }
  58. }
  59. public function IsBeingTested()
  60. {
  61. switch($this->Get('status'))
  62. {
  63. case 'test':
  64. return true;
  65. default:
  66. return false;
  67. }
  68. }
  69. }
  70. /**
  71. * A notification
  72. *
  73. * @package iTopORM
  74. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  75. * @author Denis Flaven <denisflave@free.fr>
  76. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  77. * @link www.itop.com
  78. * @since 1.0
  79. * @version 1.1.1.1 $
  80. */
  81. abstract class ActionNotification extends Action
  82. {
  83. public static function Init()
  84. {
  85. $aParams = array
  86. (
  87. "category" => "core/cmdb",
  88. "key_type" => "autoincrement",
  89. "key_label" => "",
  90. "name_attcode" => "name",
  91. "state_attcode" => "",
  92. "reconc_keys" => array(),
  93. "db_table" => "priv_action_notification",
  94. "db_key_field" => "id",
  95. "db_finalclass_field" => "",
  96. "display_template" => "",
  97. );
  98. MetaModel::Init_Params($aParams);
  99. MetaModel::Init_InheritAttributes();
  100. MetaModel::Init_InheritFilters();
  101. // Display lists
  102. MetaModel::Init_SetZListItems('details', array('name', 'description', 'status')); // Attributes to be displayed for the complete details
  103. MetaModel::Init_SetZListItems('list', array('finalclass', 'name', 'description', 'status')); // Attributes to be displayed for a list
  104. // Search criteria
  105. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  106. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  107. }
  108. }
  109. /**
  110. * An email notification
  111. *
  112. * @package iTopORM
  113. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  114. * @author Denis Flaven <denisflave@free.fr>
  115. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  116. * @link www.itop.com
  117. * @since 1.0
  118. * @version 1.1.1.1 $
  119. */
  120. class ActionEmail extends ActionNotification
  121. {
  122. public static function Init()
  123. {
  124. $aParams = array
  125. (
  126. "category" => "core/cmdb",
  127. "key_type" => "autoincrement",
  128. "key_label" => "",
  129. "name_attcode" => "name",
  130. "state_attcode" => "",
  131. "reconc_keys" => array(),
  132. "db_table" => "priv_action_email",
  133. "db_key_field" => "id",
  134. "db_finalclass_field" => "",
  135. "display_template" => "",
  136. );
  137. MetaModel::Init_Params($aParams);
  138. MetaModel::Init_InheritAttributes();
  139. MetaModel::Init_AddAttribute(new AttributeEmailAddress("test_recipient", array("allowed_values"=>null, "sql"=>"test_recipient", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  140. MetaModel::Init_AddAttribute(new AttributeString("from", array("allowed_values"=>null, "sql"=>"from", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  141. MetaModel::Init_AddAttribute(new AttributeString("reply_to", array("allowed_values"=>null, "sql"=>"reply_to", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  142. MetaModel::Init_AddAttribute(new AttributeOQL("to", array("allowed_values"=>null, "sql"=>"to", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  143. MetaModel::Init_AddAttribute(new AttributeOQL("cc", array("allowed_values"=>null, "sql"=>"cc", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  144. MetaModel::Init_AddAttribute(new AttributeOQL("bcc", array("allowed_values"=>null, "sql"=>"bcc", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  145. MetaModel::Init_AddAttribute(new AttributeTemplateString("subject", array("allowed_values"=>null, "sql"=>"subject", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  146. MetaModel::Init_AddAttribute(new AttributeTemplateText("body", array("allowed_values"=>null, "sql"=>"body", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  147. MetaModel::Init_AddAttribute(new AttributeEnum("importance", array("allowed_values"=>new ValueSetEnum('low,normal,high'), "sql"=>"importance", "default_value"=>'normal', "is_null_allowed"=>false, "depends_on"=>array())));
  148. MetaModel::Init_InheritFilters();
  149. // Display lists
  150. MetaModel::Init_SetZListItems('details', array('name', 'description', 'status', 'test_recipient', 'from', 'reply_to', 'to', 'cc', 'bcc', 'subject', 'body', 'importance')); // Attributes to be displayed for the complete details
  151. MetaModel::Init_SetZListItems('list', array('name', 'status', 'to', 'subject')); // Attributes to be displayed for a list
  152. // Search criteria
  153. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  154. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  155. }
  156. // count the recipients found
  157. protected $m_iRecipients;
  158. // Errors management : not that simple because we need that function to be
  159. // executed in the background, while making sure that any issue would be reported clearly
  160. protected $m_aMailErrors; //array of strings explaining the issue
  161. // returns a the list of emails as a string, or a detailed error description
  162. protected function FindRecipients($sRecipAttCode, $aArgs)
  163. {
  164. $sOQL = $this->Get($sRecipAttCode);
  165. if (strlen($sOQL) == '') return '';
  166. try
  167. {
  168. $oSearch = DBObjectSearch::FromOQL($sOQL);
  169. }
  170. catch (OqlException $e)
  171. {
  172. $this->m_aMailErrors[] = "query syntax error for recipient '$sRecipAttCode'";
  173. return $e->getMessage();
  174. }
  175. $sClass = $oSearch->GetClass();
  176. // Determine the email attribute (the first one will be our choice)
  177. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  178. {
  179. if ($oAttDef instanceof AttributeEmailAddress)
  180. {
  181. $sEmailAttCode = $sAttCode;
  182. // we've got one, exit the loop
  183. break;
  184. }
  185. }
  186. if (!isset($sEmailAttCode))
  187. {
  188. $this->m_aMailErrors[] = "wrong target for recipient '$sRecipAttCode'";
  189. return "The objects of the class '$sClass' do not have any email attribute";
  190. }
  191. $oSet = new DBObjectSet($oSearch, array() /* order */, $aArgs);
  192. $aRecipients = array();
  193. while ($oObj = $oSet->Fetch())
  194. {
  195. $aRecipients[] = $oObj->Get($sEmailAttCode);
  196. $this->m_iRecipients++;
  197. }
  198. return implode(', ', $aRecipients);
  199. }
  200. public function DoExecute($oTrigger, $aContextArgs)
  201. {
  202. $this->m_iRecipients = 0;
  203. $this->m_aMailErrors = array();
  204. $bRes = false; // until we do succeed in sending the email
  205. try
  206. {
  207. // Determine recicipients
  208. //
  209. $sTo = $this->FindRecipients('to', $aContextArgs);
  210. $sCC = $this->FindRecipients('cc', $aContextArgs);
  211. $sBCC = $this->FindRecipients('bcc', $aContextArgs);
  212. $sFrom = $this->Get('from');
  213. $sReplyTo = $this->Get('reply_to');
  214. $sSubject = MetaModel::ApplyParams($this->Get('subject'), $aContextArgs);
  215. $sBody = MetaModel::ApplyParams($this->Get('body'), $aContextArgs);
  216. $oEmail = new Email();
  217. if ($this->IsBeingTested())
  218. {
  219. $oEmail->SetSubject('TEST['.$sSubject.']');
  220. $sTestBody = $sBody;
  221. $sTestBody .= "<div style=\"border: dashed;\">\n";
  222. $sTestBody .= "<h1>Testing email notification ".$this->GetHyperlink()."</h1>\n";
  223. $sTestBody .= "<p>The email should be sent with the following properties\n";
  224. $sTestBody .= "<ul>\n";
  225. $sTestBody .= "<li>TO: $sTo</li>\n";
  226. $sTestBody .= "<li>CC: $sCC</li>\n";
  227. $sTestBody .= "<li>BCC: $sBCC</li>\n";
  228. $sTestBody .= "<li>From: $sFrom</li>\n";
  229. $sTestBody .= "<li>Reply-To: $sReplyTo</li>\n";
  230. $sTestBody .= "</ul>\n";
  231. $sTestBody .= "</p>\n";
  232. $sTestBody .= "</div>\n";
  233. $oEmail->SetBody($sTestBody);
  234. $oEmail->SetRecipientTO($this->Get('test_recipient'));
  235. $oEmail->SetRecipientFrom($this->Get('test_recipient'));
  236. }
  237. else
  238. {
  239. $oEmail->SetSubject($sSubject);
  240. $oEmail->SetBody($sBody);
  241. $oEmail->SetRecipientTO($sTo);
  242. $oEmail->SetRecipientCC($sCC);
  243. $oEmail->SetRecipientBCC($sBCC);
  244. $oEmail->SetRecipientFrom($sFrom);
  245. $oEmail->SetRecipientReplyTo($sReplyTo);
  246. }
  247. if (empty($this->m_aMailErrors))
  248. {
  249. if ($this->m_iRecipients == 0)
  250. {
  251. $this->m_aMailErrors[] = 'No recipient';
  252. }
  253. else
  254. {
  255. $this->m_aMailErrors = array_merge($this->m_aMailErrors, $oEmail->Send());
  256. }
  257. }
  258. }
  259. catch (Exception $e)
  260. {
  261. $this->m_aMailErrors[] = $e->getMessage();
  262. }
  263. $oLog = new EventNotificationEmail();
  264. if (empty($this->m_aMailErrors))
  265. {
  266. if ($this->IsBeingTested())
  267. {
  268. $oLog->Set('message', 'TEST - Notification sent ('.$this->Get('test_recipient').')');
  269. }
  270. else
  271. {
  272. $oLog->Set('message', 'Notification sent');
  273. }
  274. }
  275. else
  276. {
  277. if (is_array($this->m_aMailErrors) && count($this->m_aMailErrors) > 0)
  278. {
  279. $sError = implode(', ', $this->m_aMailErrors);
  280. }
  281. else
  282. {
  283. $sError = 'Unknown reason';
  284. }
  285. if ($this->IsBeingTested())
  286. {
  287. $oLog->Set('message', 'TEST - Notification was not sent: '.$sError);
  288. }
  289. else
  290. {
  291. $oLog->Set('message', 'Notification was not sent: '.$sError);
  292. }
  293. }
  294. $oLog->Set('userinfo', UserRights::GetUser());
  295. $oLog->Set('trigger_id', $oTrigger->GetKey());
  296. $oLog->Set('action_id', $this->GetKey());
  297. $oLog->Set('object_id', $aContextArgs['this->id']);
  298. // Note: we have to secure this because those values are calculated
  299. // inside the try statement, and we would like to keep track of as
  300. // many data as we could while some variables may still be undefined
  301. if (isset($sTo)) $oLog->Set('to', $sTo);
  302. if (isset($sCC)) $oLog->Set('cc', $sCC);
  303. if (isset($sBCC)) $oLog->Set('bcc', $sBCC);
  304. if (isset($sFrom)) $oLog->Set('from', $sFrom);
  305. if (isset($sSubject)) $oLog->Set('subject', $sSubject);
  306. if (isset($sBody)) $oLog->Set('body', $sBody);
  307. $oLog->DBInsertNoReload();
  308. }
  309. }
  310. ?>