action.class.inc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * A user defined action, to customize the application
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. abstract class Action extends cmdbAbstractObject
  14. {
  15. public static function Init()
  16. {
  17. $aParams = array
  18. (
  19. "category" => "core/cmdb",
  20. "name" => "action",
  21. "description" => "Custom action",
  22. "key_type" => "autoincrement",
  23. "key_label" => "",
  24. "name_attcode" => "name",
  25. "state_attcode" => "",
  26. "reconc_keys" => array(),
  27. "db_table" => "priv_action",
  28. "db_key_field" => "id",
  29. "db_finalclass_field" => "realclass",
  30. "display_template" => "",
  31. );
  32. MetaModel::Init_Params($aParams);
  33. //MetaModel::Init_InheritAttributes();
  34. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"Name", "description"=>"label", "allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  35. MetaModel::Init_AddAttribute(new AttributeString("description", array("label"=>"Description", "description"=>"one line description", "allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  36. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("related_triggers", array("label"=>"Related Triggers", "description"=>"Triggers linked to this action", "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')); // Attributes to be displayed for the complete details
  42. MetaModel::Init_SetZListItems('list', array('name', 'description')); // 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. }
  49. /**
  50. * A notification
  51. *
  52. * @package iTopORM
  53. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  54. * @author Denis Flaven <denisflave@free.fr>
  55. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  56. * @link www.itop.com
  57. * @since 1.0
  58. * @version 1.1.1.1 $
  59. */
  60. abstract class ActionNotification extends Action
  61. {
  62. public static function Init()
  63. {
  64. $aParams = array
  65. (
  66. "category" => "core/cmdb",
  67. "name" => "notification",
  68. "description" => "Notification (abstract)",
  69. "key_type" => "autoincrement",
  70. "key_label" => "",
  71. "name_attcode" => "name",
  72. "state_attcode" => "",
  73. "reconc_keys" => array(),
  74. "db_table" => "priv_action_notification",
  75. "db_key_field" => "id",
  76. "db_finalclass_field" => "",
  77. "display_template" => "",
  78. );
  79. MetaModel::Init_Params($aParams);
  80. MetaModel::Init_InheritAttributes();
  81. MetaModel::Init_InheritFilters();
  82. // Display lists
  83. MetaModel::Init_SetZListItems('details', array('name', 'description')); // Attributes to be displayed for the complete details
  84. MetaModel::Init_SetZListItems('list', array('finalclass', 'name', 'description')); // Attributes to be displayed for a list
  85. // Search criteria
  86. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  87. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  88. }
  89. }
  90. /**
  91. * An email notification
  92. *
  93. * @package iTopORM
  94. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  95. * @author Denis Flaven <denisflave@free.fr>
  96. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  97. * @link www.itop.com
  98. * @since 1.0
  99. * @version 1.1.1.1 $
  100. */
  101. class ActionEmail extends ActionNotification
  102. {
  103. public static function Init()
  104. {
  105. $aParams = array
  106. (
  107. "category" => "core/cmdb",
  108. "name" => "email notification",
  109. "description" => "Action: Email notification",
  110. "key_type" => "autoincrement",
  111. "key_label" => "",
  112. "name_attcode" => "name",
  113. "state_attcode" => "",
  114. "reconc_keys" => array(),
  115. "db_table" => "priv_action_email",
  116. "db_key_field" => "id",
  117. "db_finalclass_field" => "",
  118. "display_template" => "",
  119. );
  120. MetaModel::Init_Params($aParams);
  121. MetaModel::Init_InheritAttributes();
  122. MetaModel::Init_AddAttribute(new AttributeString("from", array("label"=>"From", "description"=>"Will be sent into the email header", "allowed_values"=>null, "sql"=>"from", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  123. MetaModel::Init_AddAttribute(new AttributeString("reply_to", array("label"=>"Reply to", "description"=>"Will be sent into the email header", "allowed_values"=>null, "sql"=>"reply_to", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  124. MetaModel::Init_AddAttribute(new AttributeOQL("to", array("label"=>"To", "description"=>"Destination of the email", "allowed_values"=>null, "sql"=>"to", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  125. MetaModel::Init_AddAttribute(new AttributeOQL("cc", array("label"=>"Cc", "description"=>"Carbon Copy", "allowed_values"=>null, "sql"=>"cc", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  126. MetaModel::Init_AddAttribute(new AttributeOQL("bcc", array("label"=>"bcc", "description"=>"Blind Carbon Copy", "allowed_values"=>null, "sql"=>"bcc", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  127. MetaModel::Init_AddAttribute(new AttributeTemplateString("subject", array("label"=>"subject", "description"=>"Title of the email", "allowed_values"=>null, "sql"=>"subject", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  128. MetaModel::Init_AddAttribute(new AttributeTemplateText("body", array("label"=>"body", "description"=>"Contents of the email", "allowed_values"=>null, "sql"=>"body", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  129. MetaModel::Init_AddAttribute(new AttributeEnum("importance", array("label"=>"importance", "description"=>"Importance flag", "allowed_values"=>new ValueSetEnum('low,normal,high'), "sql"=>"importance", "default_value"=>'normal', "is_null_allowed"=>false, "depends_on"=>array())));
  130. MetaModel::Init_InheritFilters();
  131. // Display lists
  132. MetaModel::Init_SetZListItems('details', array('name', 'description', 'from', 'reply_to', 'to', 'cc', 'bcc', 'subject', 'body', 'importance')); // Attributes to be displayed for the complete details
  133. MetaModel::Init_SetZListItems('list', array('name', 'to', 'subject')); // Attributes to be displayed for a list
  134. // Search criteria
  135. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  136. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  137. }
  138. // args: a search object
  139. // returns an array of emails
  140. protected function FindRecipients($sAttCode, $aArgs)
  141. {
  142. $sOQL = $this->Get($sAttCode);
  143. if (strlen($sOQL) == '') return '';
  144. $oSearch = DBObjectSearch::FromOQL($sOQL);
  145. $sClass = $oSearch->GetClass();
  146. // Determine the email attribute (the first one will be our choice)
  147. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  148. {
  149. if ($oAttDef instanceof AttributeEmailAddress)
  150. {
  151. $sEmailAttCode = $sAttCode;
  152. // we've got one, exit the loop
  153. break;
  154. }
  155. }
  156. $oSet = new DBObjectSet($oSearch, array() /* order */, $aArgs);
  157. $aRecipients = array();
  158. while ($oObj = $oSet->Fetch())
  159. {
  160. $aRecipients[] = $oObj->Get($sEmailAttCode);
  161. }
  162. return implode(', ', $aRecipients);
  163. }
  164. public function DoExecute($oTrigger, $aContextArgs)
  165. {
  166. // Determine recicipients
  167. //
  168. $sTo = $this->FindRecipients('to', $aContextArgs);
  169. $sCC = $this->FindRecipients('cc', $aContextArgs);
  170. $sBCC = $this->FindRecipients('bcc', $aContextArgs);
  171. $sFrom = $this->Get('from');
  172. $sReplyTo = $this->Get('reply_to');
  173. $sSubject = MetaModel::ApplyParams($this->Get('subject'), $aContextArgs);
  174. $sBody = MetaModel::ApplyParams($this->Get('body'), $aContextArgs);
  175. // To send HTML mail, the Content-type header must be set
  176. $sHeaders = 'MIME-Version: 1.0' . "\r\n";
  177. $sHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  178. // Additional headers
  179. if (strlen($sFrom) > 0)
  180. {
  181. $sHeaders .= "From: $sFrom\r\n";
  182. // This is required on Windows because otherwise I would get the error
  183. // "sendmail_from" not set in php.ini" even if it is correctly working
  184. // (apparently, once it worked the SMTP server won't claim anymore for it)
  185. ini_set("sendmail_from", $sFrom);
  186. }
  187. if (strlen($sReplyTo) > 0)
  188. {
  189. $sHeaders .= "Reply-To: $sReplyTo\r\n";
  190. }
  191. if (strlen($sCC) > 0)
  192. {
  193. $sHeaders .= "Cc: $sCC\r\n";
  194. }
  195. if (strlen($sBCC) > 0)
  196. {
  197. $sHeaders .= "Bcc: $sBCC\r\n";
  198. }
  199. $oLog = new EventNotificationEmail();
  200. if (mail($sTo, $sSubject, $sBody, $sHeaders))
  201. {
  202. $oLog->Set('message', 'Notification sent');
  203. }
  204. else
  205. {
  206. $aLastError = error_get_last();
  207. $oLog->Set('message', 'Mail could not be sent: '.$aLastError['message']);
  208. //throw new CoreException('mail not sent', array('action'=>$this->GetKey(), 'to'=>$sTo, 'subject'=>$sSubject, 'headers'=>$sHeaders));
  209. }
  210. $oLog->Set('userinfo', UserRights::GetUser());
  211. $oLog->Set('trigger_id', $oTrigger->GetKey());
  212. $oLog->Set('action_id', $this->GetKey());
  213. $oLog->Set('object_id', $aContextArgs['this->id']);
  214. $oLog->Set('from', $sFrom);
  215. $oLog->Set('to', $sTo);
  216. $oLog->Set('cc', $sCC);
  217. $oLog->Set('bcc', $sBCC);
  218. $oLog->Set('subject', $sSubject);
  219. $oLog->Set('body', $sBody);
  220. $oLog->DBInsertNoReload();
  221. }
  222. }
  223. ?>