action.class.inc.php 12 KB

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