action.class.inc.php 12 KB

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