action.class.inc.php 9.6 KB

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