[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare Workflow - Mail SMTP Agent Connector - business layer * 4 * ------------------------------------------------------------------------ * 5 * This program is free software; you can redistribute it and/or modify it * 6 * under the terms of the GNU General Public License as published * 7 * by the Free Software Foundation; either version 2 of the License, or * 8 * any later version. * 9 \**************************************************************************/ 10 11 /* $Id: class.bo_agent_mail_smtp.inc.php 20178 2005-12-23 20:36:40Z regis_glc $ */ 12 13 14 /** 15 * * Mail-SMTP Agent : business layer 16 * * 17 * * This class connects the workflow agent to the egroupware phpmailer and emailadmin 18 * * This let the workflow activities send emails. It contains some logic to replace 19 * * known tokens by workflow information (user, owner, activity name, etc...) 20 * * 21 * * @package workflow 22 * * @author regis.leroy@glconseil.com 23 * * GPL 24 */ 25 26 27 require_once(dirname(__FILE__) . SEP . 'class.bo_agent.inc.php'); 28 29 //some define for the send mode 30 if (!defined('_SMTP_MAIL_AGENT_SND_COMP')) define('_SMTP_MAIL_AGENT_SND_COMP', 0); 31 if (!defined('_SMTP_MAIL_AGENT_SND_POST')) define('_SMTP_MAIL_AGENT_SND_POST', 1); 32 if (!defined('_SMTP_MAIL_AGENT_SND_AUTO_PRE')) define('_SMTP_MAIL_AGENT_SND_AUTO_PRE', 2); 33 if (!defined('_SMTP_MAIL_AGENT_SND_AUTO_POS')) define('_SMTP_MAIL_AGENT_SND_AUTO_POS', 3); 34 35 class bo_agent_mail_smtp extends bo_agent 36 { 37 var $public_functions = array( 38 'bo_agent_mail_smtp' => true, 39 'load' => true, 40 'save' => true, 41 'getAdminActivityOptions' => true, 42 'decode_fields_in_final_array' => true, 43 ); 44 45 //the phpmailer object used at runtime to send email 46 var $mail = null; 47 //the emailadmin bo object to retriev egroupware mail configuration 48 var $bo_emailadmin = null; 49 //the emailadmin profile id 50 var $profileID; 51 // some maybe usefull egroupware or engine objects. Vars usefull to create only the first time 52 // to avoid multiple SQL queries 53 var $role_manager; 54 var $account; 55 var $process_name = ''; 56 var $process_version = ''; 57 var $process_id = ''; 58 var $activity_id = ''; 59 var $instance_id = ''; 60 //array containing part or this->fields recomputed to handle real email address and real values 61 var $final_array = Array(); 62 // can be usefull to test mails building without sending them 63 var $debugmode = false; 64 65 function bo_agent_mail_smtp() 66 { 67 parent::bo_agent(); 68 $this->so_agent =& CreateObject('workflow.so_agent_mail_smtp'); 69 $this->bo_emailadmin =& CreateObject('emailadmin.bo'); 70 //the showProcessConfigurationFields is not done here, quite harder to build 71 $this->ProcessConfigurationFieldsdefault = array( 72 'mail_smtp_profile' => false, 73 'mail_smtp_signature' => lang('Mail automatically sent by Mail SMTP Agent for eGroupware\'s Workflow'), 74 'mail_smtp_local_link_prefix' => '', 75 'mail_smtp_debug' => false, 76 ); 77 78 $this->title = lang('Mail Smtp Agent'); 79 $this->description = lang('This agent gives the activity the possibility to send an SMTP message (mail)'); 80 $this->help = lang('Use <a href="%1">EmailAdmin</a> to create mail profiles', $GLOBALS['egw']->link('/index.php',array('menuaction' => 'emailadmin.ui.listProfiles'))); 81 $this->help .= "<br />\n".lang('Mails can be sent at the begining or at the end of the activity, For interactive activities only it can be sent after completion.'); 82 $this->help .= "<br />\n".lang('Be carefull with interactive activity, end and start of theses activities are multiple.'); 83 $this->help .= "<br />\n".lang('You can use special values with this mail agent:'); 84 $this->help .= "<ul>\n"; 85 $this->help .= '<li>'.lang('<strong>%user%</strong> is the instance user email')."\n"; 86 $this->help .= '<li>'.lang('<strong>%owner%</strong> is the instance owner email')."\n"; 87 $this->help .= '<li>'.lang('<strong>%roles%</strong> are the emails of all users mapped to any role on this activity')."\n"; 88 $this->help .= '<li>'.lang('<strong>%role_XX%</strong> are all the emails of all users mapped to the role XX')."\n"; 89 $this->help .= '<li>'.lang('<strong>%user_XX%</strong> is the email of the acount XX')."\n"; 90 $this->help .= '<li>'.lang('<strong>%property_XX%</strong> is the content of the instance\'s property XX')."\n"; 91 $this->help .= '<li>'.lang('<strong>%signature%</strong> is the agent signature defined in the process configuration')."\n"; 92 $this->help .= '<li>'.lang('see as well <strong>%instance_name%</strong>, <strong>%activity_name%</strong>, <strong>%process_name%</strong>,<strong>%process_version%</strong>, <strong>%instance_id%</strong>, <strong>%activity_id%</strong> and <strong>%process_id%</strong>')."\n"; 93 $this->help .= '<li>'.lang('finally you have links with <strong>%link_XX|YY%</strong> syntax, XX is the address part, YY the text part.')."\n"; 94 $this->help .= lang('Special known values for local links address are <strong>userinstance</strong>, <strong>viewinstance</strong>, <strong>viewniceinstance</strong> and <strong>admininstance</strong>.'); 95 $this->help .= lang('Link addresses are considered local if not containing <strong>http://</strong>. They will get appended the configured local prefix and scanned by egroupware link engine'); 96 $this->help .= "</ul>\n"; 97 $this->fields = array( 98 'wf_to' => array( 99 'type' => 'text', 100 'label' => lang('To:'), 101 'size' => 255, 102 'value' => '', 103 ), 104 'wf_cc' => array( 105 'type' => 'text', 106 'label' => lang('Cc:'), 107 'size' => 255, 108 'value' => '', 109 ), 110 'wf_bcc' => array( 111 'type' => 'text', 112 'label' => lang('Bcc:'), 113 'size' => 255, 114 'value' => '', 115 ), 116 'wf_from' => array( 117 'type' => 'text', 118 'label' => lang('From:'), 119 'size' => 255, 120 'value' => '', 121 ), 122 'wf_replyto' => array( 123 'type' => 'text', 124 'label' => lang('ReplyTo:'), 125 'size' => 255, 126 'value' => '', 127 ), 128 'wf_subject' => array( 129 'type' => 'text', 130 'label' => lang('Subject:'), 131 'size' => 255, 132 'value' => '', 133 ), 134 'wf_message' => array( 135 'type' => 'textarea', 136 'label' => lang('Message:'), 137 'value' => '', 138 ), 139 'wf_send_mode' => array( 140 'type' => 'select', 141 'label' => lang('When to send the Message:'), 142 'value' => '', 143 'values'=> array( 144 _SMTP_MAIL_AGENT_SND_COMP => lang('send after interactive activity is completed'), 145 /*_SMTP_MAIL_AGENT_SND_POST => lang("send when wf_agent_mail_smtp['submit_send'] is posted"),*/ 146 _SMTP_MAIL_AGENT_SND_AUTO_PRE => lang("send when the activity is starting"), 147 _SMTP_MAIL_AGENT_SND_AUTO_POS => lang("send when the activity is ending"), 148 ), 149 ), 150 ); 151 152 } 153 154 /** 155 * * Factory: Load the agent values stored somewhere in the agent object and retain the agent id 156 * * @param $agent_id is the agent id 157 * * @param $really_load boolean, true by default, if false the data wont be loaded from database and 158 * * the only thing done by this function is storing the agent_id (usefull if you know you wont need actual data) 159 * * @return false if the agent cannot be loaded, true else 160 */ 161 function load($agent_id, $really_load=true) 162 { 163 //read values from the so_object 164 if ($really_load) 165 { 166 $values =& $this->so_agent->read($agent_id); 167 foreach($values as $key => $value) 168 { 169 //load only known fields 170 if (isset($this->fields[$key])) 171 { 172 $this->fields[$key]['value'] = $value; 173 //echo "<br> DEBUG loading value $value for $key"; 174 } 175 } 176 } 177 //store the id 178 $this->agent_id = $agent_id; 179 } 180 181 /** 182 * * Save the agent 183 * * @return false if the agent cannot be saved, true else 184 */ 185 function save() 186 { 187 //make a simplified version of $this->fields with just values 188 $simplefields = Array(); 189 foreach ($this->fields as $field => $arrayfield) 190 { 191 $simplefields[$field] = $arrayfield['value']; 192 } 193 return $this->so_agent->save($this->agent_id, $simplefields); 194 } 195 196 197 /** 198 * * this function lists activity level options avaible for the agent 199 * * @return an associative array which can be empty 200 */ 201 function getAdminActivityOptions () 202 { 203 return $this->fields; 204 } 205 206 /** 207 * * This function tell the engine which process level options have to be set 208 * * for the agent. Theses options will be initialized for all processes by the engine 209 * * and can be different for each process. 210 * * @return an array which can be empty 211 */ 212 function listProcessConfigurationFields() 213 { 214 $profile_list = $this->bo_emailadmin->getProfileList(); 215 foreach($profile_list as $profile) 216 { 217 $my_profile_list[$profile['profileID']] = $profile['description']; 218 } 219 $this->showProcessConfigurationFields = array( 220 'Mail SMTP Agent' => 'title', 221 'mail_smtp_profile' => $my_profile_list, 222 'mail_smtp_signature' => 'text', 223 'mail_smtp_local_link_prefix' => 'text', 224 'mail_smtp_debug' => 'yesno', 225 ); 226 return $this->showProcessConfigurationFields; 227 } 228 229 /** 230 * * return the SMTP config values stored by the emailadmin egw application 231 * * @return an associative array containing the'emailConfigValid' token at true if 232 * * it was ok, and at false else 233 */ 234 function getSMTPConfiguration() 235 { 236 $data =Array(); 237 $this->profileID = $this->conf['mail_smtp_profile']; 238 $data['emailConfigValid'] = true; 239 //code inspired by felamimail bo_preferences 240 $profileData = $this->bo_emailadmin->getProfile($this->profileID); 241 if(!is_array($profileData)) 242 { 243 $data['emailConfigValid'] = false; 244 return $data; 245 } 246 elseif ($this->profileID != $profileData['profileID']) 247 { 248 $this->profileID = $profileData['profileID']; 249 } 250 251 // set values to the global values 252 $data['defaultDomain'] = $profileData['defaultDomain']; 253 $data['smtpServerAddress'] = $profileData['smtpServer']; 254 $data['smtpPort'] = $profileData['smtpPort']; 255 $data['smtpAuth'] = $profileData['smtpAuth']; 256 $data['smtpType'] = $profileData['smtpType']; 257 $useremail = $this->bo_emailadmin->getAccountEmailAddress($GLOBALS['egw_info']['user']['userid'], $this->profileID); 258 $data['emailAddress'] = $useremail[0]['address']; 259 return $data; 260 } 261 262 263 //initialize objects we will need for the mailing and retrieve the conf 264 function init() 265 { 266 $this->mail =& CreateObject('phpgwapi.phpmailer'); 267 //set the $this->conf 268 $this->getProcessConfigurationFields($this->activity->getProcessId()); 269 if ($this->conf['mail_smtp_debug']) $this->debugmode = true; 270 271 } 272 273 /** 274 * * @return true if the conf says that we send email on POSTed forms, else false. 275 */ 276 function sendOnPosted() 277 { 278 return ($this->fields['wf_send_mode']['value']== _SMTP_MAIL_AGENT_SND_POST); 279 } 280 281 /** 282 * * If this activity is defined as an activity sending the email when starting we'll send it now 283 * * WARNING : on interactive queries the user code is parsed several times and this function is called 284 * * each time you reach the begining of the code, this means at least the first time when you show the form 285 * * and every time you loop on the form + the last time when you complete the code (if the user did not cancel). 286 * * @return true if everything was ok, false if something went wrong 287 */ 288 function send_start() 289 { 290 if ($this->fields['wf_send_mode']['value']== _SMTP_MAIL_AGENT_SND_AUTO_PRE) 291 { 292 if ($this->debugmode) $this->error[] = 'Sending at the start of the activity'; 293 if (!($this->prepare_mail())) return false; 294 return $this->send(); 295 } 296 else 297 { 298 if ($this->debugmode) $this->error[] = 'Not sending at the start of the activity'; 299 return true; 300 } 301 } 302 303 304 /** 305 * * If this activity is defined as an activity sending the email when finishing the code we'll send it now 306 * * WARNING : on interactive queries the user code is parsed several times and this function is called 307 * * each time you reach the end of the code without completing, this means at least the first time 308 * * and every time you loop on the form. 309 * * @return true if everything was ok, false if something went wrong 310 */ 311 function send_end() 312 { 313 if ($this->fields['wf_send_mode']['value']== _SMTP_MAIL_AGENT_SND_AUTO_POS) 314 { 315 if ($this->debugmode) $this->error[] = 'Sending at the end of the activity'; 316 if (!($this->prepare_mail())) return false; 317 return $this->send(); 318 } 319 else 320 { 321 if ($this->debugmode) $this->error[] = 'Not sending at the end of the activity'; 322 return true; 323 } 324 } 325 326 /** 327 * * If this activity is defined as an activity sending the email when the user post a command for it 328 * * we'll send it now 329 * * @return true if everything was ok, false if something went wrong 330 */ 331 function send_post() 332 { 333 if ($this->fields['wf_send_mode']['value']== _SMTP_MAIL_AGENT_SND_POST) 334 { 335 if ($this->debugmode) $this->error[] = 'Sending at POST in the activity'; 336 if (!($this->prepare_mail())) return false; 337 return $this->send(); 338 } 339 else 340 { 341 if ($this->debugmode) $this->error[] = 'Not sending at POST in the activity'; 342 return true; 343 } 344 } 345 346 /** 347 * * If this activity is defined as an activity sending the email when completing we'll send it now 348 * * @return true if everything was ok, false if something went wrong 349 */ 350 function send_completed() 351 { 352 if ($this->fields['wf_send_mode']['value']== _SMTP_MAIL_AGENT_SND_COMP) 353 { 354 if ($this->debugmode) $this->error[] = 'Sending when completing activity'; 355 if (!($this->prepare_mail())) return false; 356 return $this->send(); 357 } 358 else 359 { 360 if ($this->debugmode) $this->error[] = 'Not Sending when completing activity'; 361 return true; 362 } 363 } 364 365 //! Buid the email fields 366 function prepare_mail() 367 { 368 $userLang = $GLOBALS['egw_info']['user']['preferences']['common']['lang']; 369 $langFile = EGW_SERVER_ROOT."/phpgwapi/setup/phpmailer.lang-$userLang.php"; 370 if(file_exists($langFile)) 371 { 372 $this->mail->SetLanguage($userLang, EGW_SERVER_ROOT."/phpgwapi/setup/"); 373 } 374 else 375 { 376 $this->mail->SetLanguage("en", EGW_SERVER_ROOT."/phpgwapi/setup/"); 377 } 378 $this->mail->PluginDir = EGW_SERVER_ROOT."/phpgwapi/inc/"; 379 $this->mail->IsSMTP(); 380 381 //SMTP Conf 382 $smtpconf =& $this->getSMTPConfiguration(); 383 if (!($smtpconf['emailConfigValid'])) 384 { 385 $this->error[] = lang('The SMTP configuration cannot be loaded by the mail_smtp workflow agent'); 386 return false; 387 } 388 $this->mail->Host = $smtpconf['smtpServerAddress']; 389 $this->mail->Port = $smtpconf['smtpPort']; 390 //SMTP Auth? 391 if ($smtpconf['smtpAuth']) 392 { 393 $this->mail->SMTPAuth = true; 394 $this->mail->Username = $GLOBALS['egw_info']['user']['userid']; 395 $this->mail->Password = $GLOBALS['egw_info']['user']['passwd']; 396 } 397 398 $this->mail->Encoding = '8bit'; 399 //TODO: handle Charset 400 //$this->mail->CharSet = $this->displayCharset; 401 $this->mail->AddCustomHeader("X-Mailer: Egroupware Workflow"); 402 $this->mail->WordWrap = 76; 403 //we need HTMl for handling nicely links 404 $this->mail->IsHTML(true); 405 //compute $this->final_fields if not done already 406 if (!( $this->decode_fields_in_final_fields($smtpconf['defaultDomain']) )) 407 { 408 $this->error[] = lang('We were not able to build the message'); 409 return false; 410 } 411 $this->mail->From = $this->mail->EncodeHeader($this->final_fields['wf_from']); 412 $this->mail->FromName = $this->activity->getName(); 413 $this->mail->Subject = $this->mail->EncodeHeader($this->final_fields['wf_subject']); 414 $this->mail->Body = str_replace("\n",'<br />',html_entity_decode($this->final_fields['wf_message'])); 415 $this->mail->AltBody = $this->final_fields['wf_message']; 416 $this->mail->ClearAllRecipients(); 417 foreach ($this->final_fields['wf_to'] as $email) 418 { 419 if (!(empty($email))) $this->mail->AddAddress($email); 420 } 421 foreach ($this->final_fields['wf_cc'] as $email) 422 { 423 if (!(empty($email))) $this->mail->AddCC($email); 424 } 425 foreach ($this->final_fields['wf_bcc'] as $email) 426 { 427 if (!(empty($email))) $this->mail->AddBCC($email); 428 } 429 $email = $this->final_fields['wf_replyto']; 430 if (!(empty($email))) $this->mail->AddReplyTo($email); 431 return true; 432 } 433 434 /** 435 * * This function is used to decode admin instructions about the final value or the activity 436 * * fields. i.e.: decoding %user% in toto@foo.com for example 437 * * * If you call this function twice the final result will NOT be recalculated. except with the $force 438 * * parameter. This is done so that you can call this function sooner than the engine and add or remove 439 * * emails from final fields. The engine will not recompute automatically theses fields if you done it already. 440 * * @param $defaultDomain is the default mail Domain, used with empty domains 441 * * @param $force is falmse by default, if true the final are recalculated even if they are already there 442 * * @return true/false and set the $this->final_fields array containing the fields with the 'real' final value and for 443 * * the wf_to, wf_bcc and wf_cc fields you'll have arrays with email values. 444 */ 445 function decode_fields_in_final_fields($defaultDomain, $force=false) 446 { 447 if ($force || (!(isset($this->final_fields['calculated']))) ) 448 { 449 $res = Array(); 450 $result = Array(); 451 $address_array = Array(); 452 $email_list = Array(); 453 foreach ($this->fields as $key => $value) 454 { 455 $res[$key] =& $this->replace_tokens($value['value']); 456 //for all adresse fields we make an email array to detect repetitions 457 if (($key=='wf_to') || ($key=='wf_cc') || ($key=='wf_bcc')) 458 { 459 //_debug_array($res[$key]);//DEBUG 460 //clean ',,' or ', ,' or starting or ending by ',' 461 $res[$key] = $this->cleanup_adress_string($res[$key]); 462 //_debug_array($res[$key]);//DEBUG 463 464 //warning, need to handle < and > as valid chars for emails 465 $address_array = imap_rfc822_parse_adrlist(str_replace('>','>',str_replace('<','<',$res[$key])),''); 466 //_debug_array($address_array);//DEBUG 467 if (is_array($address_array) && (!(empty($address_array)))) 468 { 469 foreach ($address_array as $val) 470 { 471 //we retain this email is used in To or Bcc or Cc 472 //and we affect this email only the first time 473 //first detect errors 474 if ($val->host == '.SYNTAX-ERROR.') 475 { 476 $this->error[] = lang("at least one email address cannot be validated."); 477 if ($this->debugmode) 478 { 479 $this->error[] = $res[$key]; 480 } 481 return false; 482 } 483 //detect empty domains 484 if (empty($val->host)) 485 { 486 $val->host = $defaultDomain; 487 } 488 //build email adress 489 $his_email = $val->mailbox.'@'. $val->host; 490 if (!isset($email_list[$his_email])) 491 { 492 $email_list[$his_email] = $key; 493 $result[$key][]= $his_email; 494 } 495 } 496 } 497 else 498 { 499 $result[$key] = Array(); 500 } 501 } 502 elseif ( ($key=='wf_from') || ($key=='wf_replyto')) 503 { 504 //warning, need to handle < and > as valid chars for emails 505 $result[$key] = str_replace('>','>',str_replace('<','<',$res[$key])); 506 } 507 else 508 { 509 $result[$key] = $res[$key]; 510 } 511 } 512 $this->final_fields =& $result; 513 $this->final_fields['calculated']=true; 514 } 515 return true; 516 } 517 518 /** 519 * * This function will clean ',,' or ', ,' or starting or ending by ',' 520 * * in the email address string list. 521 * * @param $address_string is the string we should clean 522 * * @return the cleaned up string 523 */ 524 function cleanup_adress_string($address_string) 525 { 526 //in PHP5 we could ve been using the count parameter to stop recursivity 527 $new = str_replace(array(', ,' , ',,'),array(',', ','),trim(trim($address_string),',')); 528 if ($new == $address_string) 529 { 530 //it did nothing, lets stop recursivity 531 return $new; 532 } 533 { 534 //we made sime changes, lets verify the new string is syntaxically correct, recursivity 535 return $this->cleanup_adress_string($new); 536 } 537 } 538 539 540 /** 541 * * This function is used to find and replace tokens in the fields 542 * * @param $string is the string to analyse 543 * * @return the modified string 544 */ 545 function replace_tokens(&$string) 546 { 547 //first we need to escape the \% before the analysis 548 $string = str_replace('\%','&workflowpourcent;',$string); 549 $matches = Array(); 550 preg_match_all("/%([^%]+)%/",$string, $matches); 551 $final = $string; 552 if ($this->activity_id =='') $this->activity_id = $this->activity->getActivityId(); 553 if ($this->instance_id =='') $this->instance_id = $this->instance->getInstanceId(); 554 if ($this->process_id =='') $this->process_id = $this->activity->getProcessId(); 555 foreach($matches[1] as $key => $value) 556 { 557 //$value is our %token% 558 switch($value) 559 { 560 case 'signature': 561 $matches[1][$key] = $this->conf['mail_smtp_signature']; 562 break; 563 case 'instance_name' : 564 $matches[1][$key] = $this->instance->getName(); 565 break; 566 case 'activity_name' : 567 $matches[1][$key] = $this->activity->getName(); 568 break; 569 case 'process_name' : 570 if ($this->process_name=='') 571 { 572 $process =& CreateObject('workflow.workflow_process'); 573 $process->getProcess($this->process_id); 574 $this->process_name = $process->getName(); 575 $this->process_version = $process->getVersion(); 576 unset ($process); 577 } 578 $matches[1][$key] = $this->process_name; 579 break; 580 case 'process_version' : 581 if ($this->process_version=='') 582 { 583 $process =& CreateObject('workflow.workflow_process'); 584 $process->getProcess($this->process_id); 585 $this->process_name = $process->getName(); 586 $this->process_version = $process->getVersion(); 587 unset ($process); 588 } 589 $matches[1][$key] = $this->process_version; 590 break; 591 case 'process_id' : 592 $matches[1][$key] = $this->process_id; 593 break; 594 case 'instance_id' : 595 $matches[1][$key] = $this->instance_id; 596 break; 597 case 'activity_id' : 598 $matches[1][$key] = $this->activity_id; 599 break; 600 case 'user' : 601 //the current instance/activity user which is in fact running 602 //this class actually 603 $matches[1][$key] = $GLOBALS['egw_info']['user']['email']; 604 break; 605 case 'owner' : 606 //the owner of the instance 607 if (!is_object($this->account)) 608 { 609 $this->account =& CreateObject('phpgwapi.accounts'); 610 } 611 $ask_user = $this->instance->getOwner(); 612 $matches[1][$key] = $this->account->id2name($ask_user, 'account_email'); 613 break; 614 case 'roles' : 615 //all users having at least one role on this activity 616 if (!is_object($this->role_manager)) 617 { 618 $this->role_manager =& CreateObject('workflow.workflow_rolemanager'); 619 } 620 if (!is_object($this->account)) 621 { 622 $this->account =& CreateObject('phpgwapi.accounts'); 623 } 624 $my_subset = array('wf_activity_name' => $this->activity->getName()); 625 $listing =& $this->role_manager->list_mapped_users($this->instance->getProcessId(),true, $my_subset); 626 $matches[1][$key] = ''; 627 foreach ($listing as $user_id => $user_name) 628 { 629 $user_email = $this->account->id2name($user_id); 630 if ($matches[1][$key] == '') 631 { 632 $matches[1][$key] = $this->account->id2name($user_id, 'account_email'); 633 } 634 else 635 { 636 $matches[1][$key] .= ', '.$this->account->id2name($user_id, 'account_email'); 637 } 638 } 639 break; 640 default: 641 //Now we need to handle role_foo or property_bar or user_foobar 642 $matches2 = Array(); 643 //echo "<br>2nd analysis on ".$value; 644 preg_match_all("/([^_]+)([_])([A-z0-9\|:\/\.\?\=\'\&\; ]*)/",$value, $matches2); 645 $first_part = $matches2[1][0]; 646 $second_part = $matches2[3][0]; 647 switch ($first_part) 648 { 649 case 'user' : 650 //we retrieve the asked user email 651 if (!is_object($this->account)) 652 { 653 $this->account =& CreateObject('phpgwapi.accounts'); 654 } 655 $ask_user = $this->account->name2id($second_part); 656 $matches[1][$key] = $this->account->id2name($ask_user, 'account_email'); 657 break; 658 case 'property' : 659 //we take the content of the given property on the instance 660 $matches[1][$key] = $this->instance->get($second_part); 661 break; 662 case 'role' : 663 //all user mapped to this role 664 if (!is_object($this->role_manager)) 665 { 666 $this->role_manager =& CreateObject('workflow.workflow_rolemanager'); 667 } 668 if (!is_object($this->account)) 669 { 670 $this->account =& CreateObject('phpgwapi.accounts'); 671 } 672 $my_subset = array('wf_role_name' => $second_part); 673 $listing =& $this->role_manager->list_mapped_users($this->instance->getProcessId(),true, $my_subset); 674 $matches[1][$key] = ''; 675 foreach ($listing as $user_id => $user_name) 676 { 677 $user_email = $this->account->id2name($user_id); 678 if ($matches[1][$key] == '') 679 { 680 $matches[1][$key] = $this->account->id2name($user_id, 'account_email'); 681 } 682 else 683 { 684 $matches[1][$key] .= ', '.$this->account->id2name($user_id, 'account_email'); 685 } 686 } 687 break; 688 case 'link' : 689 //we want a link 690 //the HTML characters are escaped, so we need this function 691 //and we now some usefull links: 692 // * link to the ui_userinstance with instance filter 693 // * link to the ui_admininstance for this instance 694 // * link to the ui_userviewinstance for this instance 695 //$second_part should be in this form link adress|text 696 $matches3 = Array(); 697 //echo "<br>3rd analysis on ".$second_part; 698 preg_match_all("/([^\|]+)([\|])([A-z0-9 \'\&\;]*)/",$second_part, $matches3); 699 $link_part = $matches3[1][0]; 700 $text_part = $matches3[3][0]; 701 //need something in the text 702 if (empty($text_part)) $text_part=$link_part; 703 //and something in the link 704 switch ($link_part) 705 { 706 case 'userinstance' : 707 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link('/index.php',array( 708 'menuaction' => 'workflow.ui_userinstances.form', 709 'filter_instance' => $this->instance_id, 710 ) 711 ); 712 break; 713 case 'viewinstance' : 714 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link('/index.php',array( 715 'menuaction' => 'workflow.ui_userviewinstance.form', 716 'iid' => $this->instance_id, 717 ) 718 ); 719 break; 720 case 'viewniceinstance' : 721 $GUI =& CreateObject('workflow.workflow_gui'); 722 $view_activity = $GUI->gui_get_process_view_activity($this->process_id); 723 unset($GUI); 724 if (!($view_activity)) 725 {//link on default view 726 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link('/index.php',array( 727 'menuaction' => 'workflow.ui_userviewinstance.form', 728 'iid' => $this->instance_id, 729 ) 730 ); 731 } 732 else 733 {//link on this special activity 734 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link('/index.php',array( 735 'menuaction' => 'workflow.run_activity.go', 736 'iid' => $this->instance_id, 737 'activity_id' => $view_activity, 738 ) 739 ); 740 } 741 break; 742 case 'admininstance' : 743 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link('/index.php',array( 744 'menuaction' => 'workflow.ui_admininstance.form', 745 'iid' => $this->instance_id, 746 ) 747 ); 748 break; 749 default: 750 //now it can be an external or local link 751 if (substr($link_part,0,7)=='http://') 752 {//external link 753 $my_link = $link_part; 754 } 755 else 756 {//local link 757 $my_link = $this->conf['mail_smtp_local_link_prefix'].$GLOBALS['egw']->link($link_part); 758 } 759 } 760 $matches[1][$key] = '<a href="'.$my_link.'">'.$text_part.'</a>'; 761 break; 762 763 default: 764 $matches[1][$key] = ''; 765 } 766 } 767 $final = str_replace($matches[0][$key],$matches[1][$key],$final); 768 } 769 //now get back the % escaped before the analysis 770 $final = str_replace('&workflowpourcent;','%',$final); 771 return $final; 772 } 773 774 function Send() 775 { 776 //$this->mail->SMTPDebug = 10; 777 if (!($this->debugmode)) 778 { 779 if(!$this->mail->Send()) 780 { 781 $this->error[] = $this->mail->ErrorInfo; 782 return false; 783 } 784 } 785 else 786 { 787 //_debug_array($this->mail); 788 $this->error[] = 'DEBUG mode: '.lang('if not in debug mail_smtp agent would have sent this email:'); 789 $this->error[] = 'DEBUG mode: Host:'.$this->mail->Host; 790 $this->error[] = 'DEBUG mode: Port:'.$this->mail->Port; 791 $this->error[] = 'DEBUG mode: From:'.htmlentities($this->mail->From); 792 $this->error[] = 'DEBUG mode: FromName:'.htmlentities($this->mail->FromName); 793 $msg = 'DEBUG mode: ReplyTo:'; 794 foreach ($this->mail->ReplyTo as $address) 795 { 796 $msg .= htmlentities($address[0]); 797 } 798 $this->error[] = $msg; 799 $msg = 'DEBUG mode: To:'; 800 foreach ($this->mail->to as $address) 801 { 802 $msg .= htmlentities($address[0]); 803 } 804 $this->error[] = $msg; 805 $msg = 'DEBUG mode: Cc:'; 806 foreach ($this->mail->cc as $address) 807 { 808 $msg .= htmlentities($address[0]); 809 } 810 $this->error[] = $msg; 811 $msg = 'DEBUG mode: Bcc:'; 812 foreach ($this->mail->bcc as $address) 813 { 814 $msg .= ' '.htmlentities($address[0]); 815 } 816 $this->error[] = $msg; 817 $this->error[] = 'DEBUG mode: Subject:'.htmlentities($this->mail->Subject); 818 $this->error[] = 'DEBUG mode: AltBody:'.htmlentities($this->mail->AltBody); 819 $this->error[] = 'DEBUG mode: Body (hmtl):'.$this->mail->Body; 820 } 821 return true; 822 } 823 824 } 825 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |