00001 <?php 00002 // auto_create_tags.php - Auto create an incident from the incoming email 00003 // depending on tags found for skills 00004 // 00005 // SiT (Support Incident Tracker) - Support call tracking system 00006 // 00007 // This software may be used and distributed according to the terms 00008 // of the GNU General Public License, incorporated herein by reference. 00009 // 00010 // @ author Nicolaas du Toit 00011 // 00012 // ***Please note that for this plugin to work you need to make a change to your 00013 // inboundemail.php script!! 00014 // Please see the readme! 00015 // 00016 00017 00018 $PLUGININFO['auto_create_tags']['version'] = 1.89; 00019 $PLUGININFO['auto_create_tags']['description'] = 'Auto create incidents according to skill tags'; 00020 $PLUGININFO['auto_create_tags']['author'] = 'Nicolaas du Toit'; 00021 $PLUGININFO['auto_create_tags']['legal'] = 'GPL includes nameparse.php(v02a) copyright[2003 04 07] [Jonathon Hill] GPL v2'; 00022 $PLUGININFO['auto_create_tags']['sitminversion'] = 3.60; 00023 $PLUGININFO['auto_create_tags']['sitmaxversion'] = 3.69; 00024 00025 00026 plugin_register('email_arrived', 'auto_create_tags_function'); 00027 include (APPLICATION_PLUGINPATH.'auto_create_tags'.DIRECTORY_SEPARATOR.'nameparse.php'); 00028 00029 // //INL// I commented this next line out, the user can copy of the contents of that 00030 // file into config.inc.php if they have v3.51 or earlier 00031 //include (APPLICATION_PLUGINPATH.'auto_create_tags'.DIRECTORY_SEPARATOR.'config_auto_create_tags.php'); 00032 00033 00050 function auto_create_tags_function($params) 00051 { 00052 unset ($GLOBALS['plugin_reason']); 00053 $incidentid = cleanvar($params['incidentid']); 00054 $contactid = cleanvar($params['contactid']); 00055 $subject = cleanvar($params['subject']); 00056 $decoded = $params['decoded']; 00057 $send_email = 1; 00058 00059 00060 global $CONFIG, $dbIncidents, $dbSoftwareProducts, $dbMaintenance, $dbUpdates, $now, $dbSites; 00061 debug_log("incident ID : ".$incidentid." \n Contactid: ".$contactid."\n"."Subject : ".$subject); 00062 00063 //Subject already has incidentid - return 00064 if ($incidentid > 0) 00065 { 00066 return $incidentid; 00067 } 00068 00069 if (!isset($CONFIG['act_auto_new_contact'])) 00070 { 00071 $CONFIG['act_auto_new_contact'] = FALSE;//Default 00072 } 00073 if ($contactid == 0 AND $CONFIG['act_auto_new_contact']) 00074 { 00075 $contactid = act_create_unknown_contact($decoded); 00076 } 00077 00078 //Check if contact is blocked 00079 if ($contactid > 0) 00080 { 00081 if (!empty($CONFIG['auto_create_contact_exclude']) AND in_array($contactid, $CONFIG['auto_create_contact_exclude'])) 00082 { 00083 debug_log("For this client : {$contactid} autocreate is forbidden! see the config file"); 00084 $GLOBALS['plugin_reason'] = $GLOBALS['straActContactBlocked']; 00085 return; 00086 } 00087 } 00088 00089 //Check to see of contact is known 00090 if ($contactid < 1) 00091 { 00092 $GLOBALS['plugin_reason'] = $GLOBALS['straActContactUnknown']; 00093 return; 00094 } 00095 00096 //Check to see if the the support email was in CC (may mean it is not a new case just an information) 00097 $cc = act_find_cc_decoded($decoded); 00098 if (!isset($CONFIG['auto_create_carbon_copy'])) $CONFIG['auto_create_carbon_copy'] = TRUE; 00099 00100 if ($CONFIG['auto_create_carbon_copy']) 00101 { 00102 $support_email1 = $CONFIG['support_email']; 00103 $support_email2 = $CONFIG['auto_create_carbon_copy_email']; 00104 00105 if (stristr($cc, $support_email1)) 00106 { 00107 debug_log("Support email {$support_email1} was in the copy of the email!!"); 00108 $GLOBALS['plugin_reason'] = $GLOBALS['straActSupportInCc']; 00109 return; 00110 } 00111 //Check the second support email as well if set 00112 if (isset($CONFIG['auto_create_carbon_copy_email']) AND (stristr($cc, $support_email2))) 00113 { 00114 debug_log("Support email {$support_email2} was in the copy of the email!!"); 00115 $GLOBALS['plugin_reason'] = $GLOBALS['straActSupportInCc']; 00116 return; 00117 } 00118 } 00119 00120 //debug_log("Redirecting to function checking for duplicates ... "); 00121 if (!isset($CONFIG['auto_create_check_duplicates'])) $CONFIG['auto_create_check_duplicates'] = TRUE; 00122 00123 if ($CONFIG['auto_create_check_duplicates']) 00124 { 00125 //Check if duplicates exists in the incidents DB 00126 $create_incident = act_check_for_duplicate_title($subject, $contactid); 00127 debug_log("Create incident after duplicate check = {$create_incident}(1=True 0=False)"); 00128 00129 if($create_incident == FALSE) $create = 1;//Duplicates were found 00130 00131 if($create_incident == TRUE) $create = 2;//No duplicates - continue 00132 } 00133 elseif (!$CONFIG['auto_create_check_duplicates']) $create = 2; 00134 00135 switch($create) 00136 { 00137 case 1: 00138 //Duplicates were found 00139 $duplicates_ids = $GLOBALS['act_dup_casenumbers']; 00140 $duplicates_count = $GLOBALS['act_dup_count']; 00141 $incident_link = ''; 00142 00143 foreach ($duplicates_ids as $id) 00144 { 00145 //$i_title = incident_title($id); 00146 $incident_link .= "<a href=\"javascript:incident_details_window('{$id}')\" class='notice'>{$id}</a> - "; 00147 echo $incident_link; 00148 } 00149 $GLOBALS['plugin_reason'] = $duplicates_count." ".$GLOBALS['straActDuplicate']."(s)<br>".mysql_real_escape_string($incident_link); 00150 return; 00151 00152 case 2://Continue now - all other pre-checks seems to be ok 00153 $ccemail = $cc; 00154 $chosen_skill = ''; 00155 $origsubject = cleanvar($params['subject']); 00156 $subject = strtolower ($subject); 00157 //Begin checking for tags .. 00158 $chosen_skill = act_score_tags_in_subject($contactid, $subject); 00159 00160 if (!$chosen_skill OR $chosen_skill == '') 00161 { 00162 if (!isset($CONFIG['auto_create_search_bodytext'])) $CONFIG['auto_create_search_bodytext'] = FALSE; 00163 if ($CONFIG['auto_create_search_bodytext']) 00164 { 00165 $bodytext = $decoded[0]['Parts'][1]['Body']; 00166 //remove style stuff .. argh 00167 $bodytext = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $bodytext ); 00168 $bodytext = strip_tags($bodytext);//remove all other tags 00169 $bodytext = $subject." ".$bodytext; 00170 //echo "<br> {$bodytext}"; 00171 //Do the tags search now with the bodytext as well 00172 $chosen_skill = act_score_tags_in_subject($contactid, $bodytext); 00173 } 00174 00175 if (!$chosen_skill OR $chosen_skill == '') 00176 { 00177 //Added this plugin context in order to be able to do further searches 00178 //The return from this plugin has to be a string: "{$skillid},{$productid}" 00179 $further_search = plugin_do('auto_create_tags_no_tags_found', array('subject' => $subject, 00180 'decoded' => $decoded, 'contactid' => $contactid)); 00181 if ($further_search) 00182 { 00183 $chosen_skill = explode(",", $further_search); 00184 } 00185 else 00186 { 00187 $chosen_skill = ''; 00188 } 00189 } 00190 } 00191 00192 if (!isset($CONFIG['auto_create_force_to_default_skill'])) 00193 { 00194 $CONFIG['auto_create_force_to_default_skill'] = FALSE; 00195 } 00196 00197 if (!$chosen_skill AND !$CONFIG['auto_create_force_to_default_skill']) 00198 { 00199 //Plugin reason set in function that scores the tags 00200 return; 00201 } 00202 elseif (!$chosen_skill AND $CONFIG['auto_create_force_to_default_skill']) 00203 { 00204 //Set the default force skill id if not set or 0 00205 if (empty($CONFIG['auto_create_force_to_default_skill_id']) OR 00206 ($CONFIG['auto_create_force_to_default_skill_id'] == 0)) 00207 { 00208 $CONFIG['auto_create_force_to_default_skill_id'] = 1; 00209 } 00210 00211 $default_skill = $CONFIG['auto_create_force_to_default_skill_id']; 00212 00213 $sql = "SELECT * FROM `{$dbSoftwareProducts}` WHERE softwareid = '{$default_skill}' "; 00214 $result = mysql_query($sql); 00215 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00216 00217 $product = mysql_fetch_object($result); 00218 $chosen_skill = array($default_skill, $product->productid); 00219 } 00220 00221 if (is_array($chosen_skill) AND $chosen_skill != '') 00222 { 00223 $product = $chosen_skill[1];//the product id from the tags scoring function or the plugin 00224 $software = $chosen_skill[0];//The skill id from the tags scoring function or the plugin 00225 debug_log("The software tag returned is : ".$software); 00226 00227 //Check to see if a specific priority is set for this skill 00228 if (in_array($software, $CONFIG['auto_create_priority_low_array'])) 00229 { 00230 $priority = 1; 00231 } 00232 elseif (in_array($software, $CONFIG['auto_create_priority_medium_array'])) 00233 { 00234 $priority = 2; 00235 } 00236 elseif (in_array($software, $CONFIG['auto_create_priority_high_array'])) 00237 { 00238 $priority = 3; 00239 } 00240 elseif (in_array($software, $CONFIG['auto_create_priority_critical_array'])) 00241 { 00242 $priority = 4; 00243 } 00244 else 00245 { 00246 if (!isset($CONFIG['auto_create_default_priority'])) 00247 { 00248 $CONFIG['auto_create_default_priority'] = 2;//Default medium 00249 } 00250 else 00251 { 00252 $priority = $CONFIG['auto_create_default_priority']; 00253 } 00254 00255 } 00256 00257 $servicelevel = $CONFIG['default_service_level']; 00258 $siteid = contact_siteid($contactid); 00259 00260 $sql = "SELECT id FROM `{$dbMaintenance}` WHERE site='{$siteid}' AND product='{$product}' "; 00261 $result = mysql_query($sql); 00262 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00263 $row = mysql_fetch_row($result); 00264 00265 $contrid = $row[0]; 00266 //Create the incident based on the info we have 00267 $incidentid=''; 00268 debug_log("Creating incident with: Subj = {$origsubject} Contactid={$contactid} SL={$servicelevel} Cid={$contrid} Prod={$product} Soft={$software} Prio={$priority}"); 00269 $incidentid = create_incident($origsubject, $contactid, $servicelevel, $contrid, $product, 00270 $software, $priority, $owner = 0, $status = 1, 00271 $productversion = '', $productservicepacks = '', 00272 $opened = '', $lastupdated = ''); 00273 00274 debug_log("Incident ID created : ".$incidentid); 00275 debug_log("CC address(es) found : ".$ccemail); 00276 00277 //If we have some cc addresses, then we can update them into the case 00278 if (!isset($CONFIG['auto_create_import_cc_addresses'])) $CONFIG['auto_create_import_cc_addresses'] = FALSE; 00279 00280 if ($CONFIG['auto_create_import_cc_addresses']) 00281 { 00282 if ($ccemail) 00283 { 00284 $ccemail = strtolower($ccemail); 00285 $ccemail = explode(",", $ccemail); 00286 $new_ccemail = ''; 00287 00288 foreach($ccemail as $cc_addr) 00289 { 00290 $email_valid = act_email_validate(trim($cc_addr)); 00291 if ($email_valid AND (mb_strlen($new_ccemail) <=200))//TODO: need to find abetter way here 00292 { 00293 debug_log("Valid - ".trim($cc_addr)); 00294 $new_ccemail .= $cc_addr.","; 00295 } 00296 } 00297 00298 $new_ccemail = mb_substr($new_ccemail, 0, -1);//remove the last comma 00299 debug_log("New cc email: {$new_ccemail}"); 00300 //Update the Db with the cc address 00301 00302 $sql = "UPDATE `{$dbIncidents}` "; 00303 $sql .= "SET ccemail='$new_ccemail', lastupdated='$now' WHERE id='$incidentid'"; 00304 $result = mysql_query($sql); 00305 00306 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00307 00308 if (!$result) debug_log("Update to the incident {$incidentid} for cc email succesfull!!"); 00309 } 00310 } 00311 00312 if ($incidentid > 0) 00313 { 00314 // Insert the first SLA update, this indicates the start of an incident 00315 $sql = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, timestamp, currentowner, currentstatus, customervisibility, sla, bodytext) "; 00316 $sql .= "VALUES ('{$incidentid}', '{$sit[2]}', 'slamet', '{$now}', '{$sit[2]}', '1', 'show', 'opened','The incident is open and awaiting action.')"; 00317 mysql_query($sql); 00318 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00319 00320 // Insert the first Review update, this indicates the review period of an incident has started 00321 $sql = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, timestamp, currentowner, currentstatus, customervisibility, sla, bodytext) "; 00322 $sql .= "VALUES ('{$incidentid}', '{$sit[2]}', 'reviewmet', '{$now}', '{$sit[2]}', '1', 'hide', 'opened','')"; 00323 mysql_query($sql); 00324 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00325 00326 $send_email = 1; 00327 $t = new TriggerEvent('TRIGGER_INCIDENT_CREATED', array('incidentid' => $incidentid, 'sendemail' => $send_email)); 00328 debug_log("Succesfully created incident: ".$incidentid); 00329 00330 if (!isset($CONFIG['auto_create_first_response_auto'])) 00331 { 00332 $CONFIG['auto_create_first_response_auto'] = FALSE;//Default 00333 } 00334 00335 if ($CONFIG['auto_create_first_response_auto']) 00336 { 00337 //Insert the initial response as we see the first email as the initial response (can be disabled) 00338 $bodytext = $GLOBALS['straActFirstResponseText']; 00339 00340 $sql = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, timestamp, currentowner, currentstatus, customervisibility, sla, bodytext) "; 00341 $sql .= "VALUES ('$incidentid', '{$sit[2]}', 'slamet', '$now', '{$owner}', '1', 'show', 'initialresponse','{$bodytext}')"; 00342 mysql_query($sql); 00343 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00344 } 00345 00346 } 00347 00348 //In case we have no incident ID it means the function failed - FALSE returned by function 00349 if (!$incidentid) 00350 { 00351 debug_log("Incident auto create failed: ".$subject); 00352 $GLOBALS['plugin_reason'] = $GLOBALS['straActAutoCreateFailed']; 00353 return; 00354 } 00355 00356 $send_email = 1; 00357 $owner = suggest_reassign_userid($incidentid, $exceptuserid = 0); 00358 00359 if ($owner > 0) 00360 { 00361 //Update owner in incidents 00362 $sql = "UPDATE `{$dbIncidents}` SET owner='$owner', lastupdated='$now' WHERE id='$incidentid'"; 00363 mysql_query($sql); 00364 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00365 00366 trigger('TRIGGER_INCIDENT_ASSIGNED', array('userid' => $owner, 'incidentid' => $incidentid)); 00367 00368 // add update 00369 $sql = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, timestamp, currentowner, currentstatus, nextaction) "; 00370 $sql .= "VALUES ('{$incidentid}', '{$sit[2]}', 'reassigning', '{$now}', '{$owner}', '1', '{$nextaction}')"; 00371 $result = mysql_query($sql); 00372 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR); 00373 00374 debug_log("Incident re-assigned to: ".$owner); 00375 } 00376 00377 return $incidentid; 00378 } 00379 } 00380 } 00381 00393 function act_count_tags_in_subject_ticket($subject, $skillid) 00394 { 00395 global $dbTags, $dbSetTags, $dbSoftware, $dbSoftwareProducts, $CONFIG; 00396 00397 $lower_subject = strtolower($subject); 00398 00399 $sql = "SELECT st.id, st.tagid, t.tagid, t.name FROM `{$dbSetTags}` as st, "; 00400 $sql .= "`{$dbTags}` as t "; 00401 $sql .= "WHERE st.tagid = t.tagid AND st.id = '{$skillid}' "; 00402 $result = mysql_query($sql); 00403 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00404 00405 $count = 0; 00406 $nmbresult = mysql_num_rows($result); 00407 $i = 0; 00408 $count = 0; 00409 $ticket = ''; 00410 00411 while ($row=mysql_fetch_object($result)) 00412 { 00413 $tag_name[] = $row->name; 00414 $tag_id[] = $row->tagid; 00415 00416 if ($CONFIG['auto_create_tag_seperator']) 00417 { 00418 $tag_seperator = $CONFIG['auto_create_tag_seperator']; 00419 $tag = str_replace("{$tag_seperator}", " ", "$tag_name[$i]"); 00420 } 00421 else 00422 { 00423 $tag = $tag_name[$i]; 00424 } 00425 //echo "<br> Tag= {$tag}"; 00426 $tagid = $tag_id[$i]; 00427 00428 if(preg_match("/".preg_quote($tag, '/')."/i",$lower_subject, $matches)) 00429 { 00430 $tag_array[] = $tagid; 00431 $count++; 00432 $ticket[] = $skillid; 00433 //echo "<br> Tag= {$tag}"; 00434 //echo " *Match*"; 00435 } 00436 $i++; 00437 } 00438 00439 return $ticket; 00440 } 00441 00442 function act_find_contact_contract($subject) 00443 { 00444 global $dbTags, $dbSetTags, $dbSoftware, $dbSoftwareProducts; 00445 00446 $lower_subject = strtolower($subject); 00447 $sql = "SELECT name, tagid FROM `{$dbTags}` "; 00448 $result = mysql_query($sql); 00449 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00450 00451 $count=0; 00452 $nmbresult= mysql_num_rows($result); 00453 debug_log ("There are : ".$nmbresult."Tags found in the db"); 00454 $i = 0; 00455 $count = 0; 00456 00457 while ($row=mysql_fetch_object($result)) 00458 { 00459 $tag_name[]=$row->name; 00460 $tag_id[]=$row->tagid; 00461 $tag = str_replace("|", " ", "$tag_name[$i]"); 00462 $tagid = $tag_id[$i]; 00463 if(preg_match("/".preg_quote($tag, '/')."/i",$lower_subject, $matches)) 00464 { 00465 $tag_array[] = $tagid; 00466 $count++; 00467 } 00468 00469 $i++; 00470 } 00471 00472 debug_log ("There were : ".$count." matches found in the subject"); 00473 $this_tag = $tag_array[0]; 00474 00475 $sql = "SELECT * FROM `{$dbSetTags}` WHERE tagid = '$this_tag'"; 00476 $result = mysql_query($sql); 00477 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 00478 $obj = mysql_fetch_object($result); 00479 $productid = $obj->id; 00480 00481 $sql = "SELECT s.id, s.name, sp.softwareid, sp.productid FROM `{$dbSoftware}` AS s, "; 00482 $sql .= "`{$dbSoftwareProducts}` AS sp WHERE s.id = sp.softwareid AND s.id = '{$productid}' "; 00483 $resultskill = mysql_query($sql); 00484 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 00485 00486 if (mysql_num_rows($resultskill) > 0) 00487 { 00488 $objskill = mysql_fetch_object($resultskill); 00489 $skillname = $objskill->name; 00490 $skillid = $objskill->id; 00491 $prod_id = $objskill->productid; 00492 debug_log("Skill name from tag function: ".$skillname); 00493 return array($skillname, $skillid, $prod_id); 00494 } 00495 else return; 00496 00497 } 00498 00499 function act_create_software_array($id) 00500 { 00501 global $dbSoftwareProducts, $dbSoftware, $dbMaintenance, $dbSites, $dbResellers, $dbLicenceTypes; 00502 00503 $sql = "SELECT m.*, m.notes AS maintnotes, s.name AS sitename, "; 00504 $sql .= "r.name AS resellername, lt.name AS licensetypename "; 00505 $sql .= "FROM `{$dbMaintenance}` AS m, `{$dbSites}` AS s, "; 00506 $sql .= "`{$dbResellers}` AS r, `{$dbLicenceTypes}` AS lt "; 00507 $sql .= "WHERE s.id = m.site "; 00508 $sql .= "AND m.id='{$id}' "; 00509 $sql .= "AND m.reseller = r.id "; 00510 $sql .= "AND (m.licence_type IS NULL OR m.licence_type = lt.id) "; 00511 //if ($mode == 'external') $sql .= "AND m.site = '{$_SESSION['siteid']}'"; 00512 00513 $maintresult = mysql_query($sql); 00514 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00515 00516 $maint = mysql_fetch_object($maintresult); 00517 00518 // supported software 00519 $sql = "SELECT * FROM `{$dbSoftwareProducts}` AS sp, `{$dbSoftware}` AS s "; 00520 $sql .= "WHERE sp.softwareid = s.id AND productid='{$maint->product}' "; 00521 $result = mysql_query($sql); 00522 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00523 $count = mysql_num_rows($result); 00524 $loop = 0; 00525 if ($count > 0) 00526 { 00527 $soft_list = ''; 00528 while ($software = mysql_fetch_object($result)) 00529 { 00530 $software->lifetime_end = mysql2date($software->lifetime_end); 00531 //$html .= "<tr><td> ".icon('skill', 16)." "; 00532 00533 if ($software->lifetime_end == 0 OR $software->lifetime_end > $now) 00534 { 00535 //$soft_list = ''; 00536 $soft_list .= $software->softwareid.","; 00537 } 00538 } 00539 } 00540 00541 return $soft_list; 00542 } 00543 00544 00545 function act_max_in_array_key($array) 00546 { 00547 foreach ($array as $key => $val) 00548 { 00549 if ($val == max($array)) return $key; 00550 } 00551 } 00552 00553 00561 function act_check_for_duplicate_title($subject, $contactid) 00562 { 00563 //Revert to second function that does the preg_match 00564 $count_dup = act_count_duplicate_title($subject, $contactid); 00565 00566 //There are matches for the title 00567 00568 if($count_dup >= 1) 00569 { 00570 $create = FALSE; 00571 return $create; 00572 } 00573 00574 if($count_dup == 0) 00575 { 00576 $create = TRUE; 00577 return $create; 00578 } 00579 } 00580 00581 00593 function act_count_duplicate_title($subject, $contactid) 00594 { 00595 global $dbIncidents, $now; 00596 00597 $timelimit = $now - 15552000;//We need to limit the results to the last 6 months 00598 debug_log("The time limit = {$timelimit}"); 00599 $lower_subject = strtolower($subject); 00600 00601 //Check if we have a FW: or RE: etc.. 00602 if (stristr($lower_subject, ':')) 00603 { 00604 $start = stripos($lower_subject, ':')+1; 00605 $length = mb_strlen($lower_subject)-$start; 00606 $db_test_subject = ltrim((mb_substr($lower_subject, $start, $length))," "); 00607 $db_test_subject = mb_substr($db_test_subject, 0, -6);//Strip some characters off the end in case something was added after 00608 debug_log("The string with : stuff removed is--{$db_test_subject}--"); 00609 } 00610 //Else check the length and strip some characters of the beginning in case something was added 00611 else 00612 { 00613 if (mb_strlen($lower_subject) > 30) 00614 { 00615 $db_test_subject = mb_substr($lower_subject, 8); 00616 $db_test_subject = mb_substr($db_test_subject, 0, -6);//Strip some characters off the end in case something was added after 00617 } 00618 else 00619 { 00620 $db_test_subject = $lower_subject; 00621 } 00622 00623 } 00624 00625 debug_log("Db test subject = {$db_test_subject}"); 00626 00627 //May 2010 NDT: Changed this to test with all contact's title's and within the last 6 months 00628 $sql = "SELECT title, id FROM `{$dbIncidents}` WHERE opened > {$timelimit} "; 00629 $sql .= "AND title LIKE '%{$db_test_subject}%' "; 00630 $result = mysql_query($sql); 00631 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00632 00633 $count = mysql_num_rows($result); 00634 00635 $id_array = ''; 00636 unset ($GLOBALS['act_dup_casenumbers']); 00637 00638 if ($count > 0) 00639 { 00640 while ($dup_id = mysql_fetch_object($result)) 00641 { 00642 $id_array[] = $dup_id->id; 00643 } 00644 00645 $GLOBALS['act_dup_casenumbers'] = $id_array; 00646 $GLOBALS['act_dup_count'] = $count; 00647 } 00648 00649 /* NDT: June 2010: Need to redo this function but for now i am removing it as it fails to often 00650 * and instead we just count the results of the DB search. 00651 * while ($row=mysql_fetch_object($result)) 00652 { 00653 $search_pattern = preg_quote($row->title, '/'); 00654 $compare_result = preg_match("/".preg_quote($row->title, '/')."/i",$lower_subject); 00655 //$compare_result = strcasecmp($subject, $row->title); -> This is not good enough! 00656 debug_log("Search pattern: >>{$search_pattern}<<"); 00657 00658 if ($compare_result == 1) 00659 { 00660 $count = $count + 1; 00661 //debug_log("Search pattern: >>{$search_pattern}<<"); 00662 debug_log(" Subject{$lower_subject} "); 00663 debug_log("--> MATCH ****"); 00664 } 00665 }*/ 00666 00667 debug_log("The count of duplicates was : ".$count); 00668 return $count; 00669 } 00670 00671 00689 function act_score_tags_in_subject ($contactid, $subject) 00690 { 00691 global $dbSoftwareProducts,$CONFIG; 00692 00693 $site = contact_site($contactid); 00694 $site_id = contact_siteid($contactid); 00695 $winner_combo = ''; 00696 $checkvisible = FALSE; 00697 $contact_contracts = act_get_site_contracts($site_id); 00698 $soft_list = ''; 00699 00700 if ($contact_contracts) 00701 { 00702 foreach ($contact_contracts as $c_id) 00703 { 00704 $soft_list .= act_create_software_array($c_id); 00705 } 00706 00707 debug_log("The contracts for: {$contactid} is : {$contact_contracts}"); 00708 $soft_array = explode(",", $soft_list); 00709 $soft_count = count($soft_array); 00710 unset ($soft_array[--$soft_count]); 00711 $fin_result = ''; 00712 $result = ''; 00713 debug_log("The software list = {$soft_list}"); 00714 00715 foreach ($soft_array as $skill) 00716 { 00717 $result = act_count_tags_in_subject_ticket($subject, $skill); 00718 if (is_array($result) AND $result != 0) 00719 { 00720 $fin_result = array_merge((array)$fin_result,(array)$result); 00721 } 00722 } 00723 } 00724 else 00725 { 00726 $GLOBALS['plugin_reason'] = $GLOBALS['straActNoSkillsSite']; 00727 } 00728 00729 if ($fin_result == '') 00730 { 00731 $GLOBALS['plugin_reason'] = $GLOBALS['straActNoTagMatch']; 00732 return; 00733 } 00734 00735 $fin_result = array_filter($fin_result); 00736 $counted = array_count_values($fin_result); 00737 $biggest_key = act_max_in_array_key($counted); 00738 $biggest_value = max($counted); 00739 $value_to_remove = 0; 00740 00741 //Cycle through array and remove all the values that are smaller than the highest 00742 while ($value_to_remove < $biggest_value) 00743 { 00744 $counted = array_diff($counted, array($value_to_remove)); 00745 $value_to_remove++; 00746 } 00747 00748 reset ($counted); 00749 //print_r ($counted); 00750 00751 if (count(array_unique($counted)) < count($counted)) 00752 { 00753 //echo 'Duplicate entry in array - do not AUTO create'; 00754 $GLOBALS['plugin_reason'] = $GLOBALS['straActMultiTagMatches']; 00755 return; 00756 } 00757 else 00758 { 00759 $winner = key($counted); 00760 00761 $sql = "SELECT * FROM `{$dbSoftwareProducts}` WHERE softwareid = '{$winner}' "; 00762 $result = mysql_query($sql); 00763 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 00764 00765 $product = mysql_fetch_object($result); 00766 $winner_combo[0] = $winner; 00767 $winner_combo[1] = $product->productid; 00768 return $winner_combo; 00769 } 00770 } 00771 00772 00773 function act_email_validate($email) 00774 { 00775 if (eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $email)) 00776 { 00777 return TRUE; 00778 } 00779 else 00780 { 00781 return FALSE; 00782 } 00783 } 00784 00785 00794 function act_find_cc_decoded($decoded) 00795 { 00796 if (is_array($decoded[0]['ExtractedAddresses']['cc:'])) 00797 { 00798 $cur = 1; 00799 foreach ($decoded[0]['ExtractedAddresses']['cc:'] as $var) 00800 { 00801 $num = count($decoded[0]['ExtractedAddresses']['cc:']); 00802 //echo "<br> Number of CC's ".$num; 00803 $cc .= $var['address']; 00804 if ($cur != $num) $cc .= ", "; 00805 $cur++; 00806 } 00807 } 00808 00809 return $cc; 00810 } 00811 00818 function act_get_site_contracts($siteid) 00819 { 00820 global $dbMaintenance, $dbContacts; 00821 00822 $sql = "SELECT DISTINCT m.id AS id FROM `$dbMaintenance` AS m WHERE m.site={$siteid} "; 00823 00824 if ($result = mysql_query($sql)) 00825 { 00826 while ($row = mysql_fetch_object($result)) 00827 { 00828 $contractsarray[] = $row->id; 00829 } 00830 } 00831 return $contractsarray; 00832 } 00833 00834 plugin_register('cfgvar', 'auto_create_tags_config'); 00835 00839 function auto_create_tags_config() 00840 { 00841 global $CFGTAB, $CFGCAT, $CFGVAR; 00842 $CFGTAB['plugins'] = array_merge((array)$CFGTAB['plugins'], array('auto_create_tags')); 00843 $CFGCAT['auto_create_tags'] = array('auto_create_contact_exclude', 00844 'auto_create_carbon_copy', 00845 'auto_create_carbon_copy_email', 00846 'auto_create_check_duplicates', 00847 'auto_create_import_cc_addresses', 00848 'auto_create_force_to_default_skill', 00849 'auto_create_force_to_default_skill_id', 00850 'auto_create_first_response_auto', 00851 'auto_create_tag_seperator', 00852 'auto_create_default_priority', 00853 'auto_create_priority_low_array', 00854 'auto_create_priority_medium_array', 00855 'auto_create_priority_high_array', 00856 'auto_create_priority_critical_array', 00857 'auto_create_search_bodytext', 00858 'act_auto_new_contact', 00859 'act_auto_new_contact_ldap', 00860 'act_auto_new_contact_sitbase', 00861 'act_auto_new_contact_ldap_selective_domain', 00862 'act_auto_new_contact_ldap_field_to_use', 00863 'act_auto_new_contact_excluded_domains', 00864 ); 00865 00866 00867 $CFGVAR['auto_create_contact_exclude']['title'] = 'Contacts to exclude'; 00868 $CFGVAR['auto_create_contact_exclude']['help'] = "Comma separated list of contacts that are <em class='updatednow'> excluded</em> from this function (i.e. if a contacts ID is in this list, the function will not auto create the incident)"; 00869 $CFGVAR['auto_create_contact_exclude']['type'] = '1darray'; 00870 00871 $CFGVAR['auto_create_carbon_copy']['title'] = 'Check if Support in CC'; 00872 $CFGVAR['auto_create_carbon_copy']['help'] = 'If enabled the plugin checks to see if the support email was in CC field, in this case the email may not be a real customer email but a copy on some info'; 00873 $CFGVAR['auto_create_carbon_copy']['type'] = 'select'; 00874 00875 $CFGVAR['auto_create_carbon_copy_email']['title'] = 'Second support email address'; 00876 $CFGVAR['auto_create_carbon_copy_email']['help'] = "The plugin normally uses the email address specified in <code>support_email</code> under the <b>outbound email</b> settings. This setting allows you to add a second support email address."; 00877 $CFGVAR['auto_create_carbon_copy_email']['unit'] = 'Second support email - leave blank if not used'; 00878 $CFGVAR['auto_create_carbon_copy_email']['type'] = 'text'; 00879 00880 $CFGVAR['auto_create_check_duplicates']['title'] = 'Check for cases with duplicate titles'; 00881 $CFGVAR['auto_create_check_duplicates']['help'] = 'If enabled the plugin checks the database to see if a case exists with the same title as the subject of the email'; 00882 $CFGVAR['auto_create_check_duplicates']['type'] = 'select'; 00883 00884 $CFGVAR['auto_create_import_cc_addresses']['title'] = 'Import CC addresses automatically'; 00885 $CFGVAR['auto_create_import_cc_addresses']['help'] = 'If enabled the plugin imports the CC adresses automatically into the newly created case'; 00886 $CFGVAR['auto_create_import_cc_addresses']['type'] = 'select'; 00887 00888 $CFGVAR['auto_create_force_to_default_skill']['title'] = 'Force incident creation to default skill'; 00889 $CFGVAR['auto_create_force_to_default_skill']['help'] = 'If enabled, and no clear tag match is found, the case will be created to a default skill (This skill has to be created in the database first)'; 00890 $CFGVAR['auto_create_force_to_default_skill']['type'] = 'select'; 00891 00892 $CFGVAR['auto_create_force_to_default_skill_id']['title'] = 'Default skill'; 00893 $CFGVAR['auto_create_force_to_default_skill_id']['help'] = 'The ID in the sit_software table of the default skill (see above)'; 00894 $CFGVAR['auto_create_force_to_default_skill_id']['type'] = 'number'; 00895 00896 $CFGVAR['auto_create_first_response_auto']['title'] = 'Auto first response'; 00897 $CFGVAR['auto_create_first_response_auto']['help'] = 'If enabled, automatic email sent after the incident creation is considered as the first response'; 00898 $CFGVAR['auto_create_first_response_auto']['type'] = 'select'; 00899 00900 $CFGVAR['auto_create_tag_seperator']['title'] = 'Tag separator'; 00901 $CFGVAR['auto_create_tag_seperator']['help'] = "In SiT! tags are normally single words, if you want to use phrases as tags you can separate them with a separator (e.g. <code>|</code>). In this case the plugin will search for the phrase by replacing the separator by a ' ' (space)"; 00902 $CFGVAR['auto_create_tag_seperator']['type'] = 'text'; 00903 00904 $CFGVAR['auto_create_default_priority']['title'] = 'Default priority'; 00905 $CFGVAR['auto_create_default_priority']['help'] = 'The default priority to use when creating an incident'; 00906 $CFGVAR['auto_create_default_priority']['type'] = 'number'; 00907 00908 $CFGVAR['auto_create_priority_low_array']['title'] = 'Low priority skills list'; 00909 $CFGVAR['auto_create_priority_low_array']['help'] = "An array list of skills to create with priority <em class='updatednow'>Low</em>"; 00910 $CFGVAR['auto_create_priority_low_array']['type'] = '1darray'; 00911 00912 $CFGVAR['auto_create_priority_medium_array']['title'] = 'Medium priority skills list'; 00913 $CFGVAR['auto_create_priority_medium_array']['help'] = "An array list of skills to create with priority <em class='updatednow'>Medium</em>"; 00914 $CFGVAR['auto_create_priority_medium_array']['type'] = '1darray'; 00915 00916 $CFGVAR['auto_create_priority_high_array']['title'] = 'High priority skills list'; 00917 $CFGVAR['auto_create_priority_high_array']['help'] = "An array list of skills to create with priority <em class='updatednow'>High</em>"; 00918 $CFGVAR['auto_create_priority_high_array']['type'] = '1darray'; 00919 00920 $CFGVAR['auto_create_priority_critical_array']['title'] = 'Critical priority skills list'; 00921 $CFGVAR['auto_create_priority_critical_array']['help'] = "An array list of skills to create with priority <em class='updatednow'>Critical</em>"; 00922 $CFGVAR['auto_create_priority_critical_array']['type'] = '1darray'; 00923 00924 $CFGVAR['auto_create_search_bodytext']['title'] = 'Search bodytext'; 00925 $CFGVAR['auto_create_search_bodytext']['help'] = 'If enabled, and no clear result is found in the tags search of the email <code>Subject</code>, then the plugin will try to do the same search but with the bodytext of the email as well'; 00926 $CFGVAR['auto_create_search_bodytext']['type'] = 'select'; 00927 00928 $CFGVAR['act_auto_new_contact']['title'] = 'Auto_create new contact'; 00929 $CFGVAR['act_auto_new_contact']['help'] = 'If enabled, and the contact is <code>Unknown</code>, then the plugin will try to autocreate the contact before autocreating the incident.'; 00930 $CFGVAR['act_auto_new_contact']['type'] = 'select'; 00931 00932 $CFGVAR['act_auto_new_contact_ldap']['title'] = 'Use LDAP to search for unknown contact'; 00933 $CFGVAR['act_auto_new_contact_ldap']['help'] = 'If enabled, the plugin will ty to search LDAP with the <code>email</code> address to try and find the contact.'; 00934 $CFGVAR['act_auto_new_contact_ldap']['type'] = 'select'; 00935 00936 $CFGVAR['act_auto_new_contact_sitbase']['title'] = 'Search for unknown contact in the sitbase'; 00937 $CFGVAR['act_auto_new_contact_sitbase']['help'] = 'If enabled, the plugin will ty to search for exisitng users with the same domain.'; 00938 $CFGVAR['act_auto_new_contact_sitbase']['type'] = 'select'; 00939 00940 $CFGVAR['act_auto_new_contact_ldap_selective_domain']['title'] = 'Selective domain for LDAP search'; 00941 $CFGVAR['act_auto_new_contact_ldap_selective_domain']['help'] = 'When the unknown contact is from the domain specified here, the plugin will search the LDAP directory (If not specified NO LDAP search is performed).'; 00942 $CFGVAR['act_auto_new_contact_ldap_selective_domain']['type'] = 'text'; 00943 00944 $CFGVAR['act_auto_new_contact_ldap_field_to_use']['title'] = 'LDAP result field to use for site identifier'; 00945 $CFGVAR['act_auto_new_contact_ldap_field_to_use']['unit'] = "for e.g. 'company' or 'departement'"; 00946 $CFGVAR['act_auto_new_contact_ldap_field_to_use']['help'] = "The name of the field in the AD that specifiezs the 'site name' ."; 00947 $CFGVAR['act_auto_new_contact_ldap_field_to_use']['type'] = 'text'; 00948 00949 $CFGVAR['act_auto_new_contact_excluded_domains']['title'] = 'Domains excluded from the function search'; 00950 $CFGVAR['act_auto_new_contact_excluded_domains']['help'] = "A comma seperated list of domains that are <code>Excluded</code>, from this function: e.g. 'hotmail.com' or 'gmail.com'."; 00951 $CFGVAR['act_auto_new_contact_excluded_domains']['unit'] = "(Exclusive) If the incoming email is from a user in this(these) domain(s), NO attempt will be made to automatically create the contact"; 00952 $CFGVAR['act_auto_new_contact_excluded_domains']['type'] = '1darray'; 00953 00954 } 00955 00965 function act_create_unknown_contact($decoded) 00966 { 00967 global $dbSites, $CONFIG; 00968 00969 $email = strtolower($decoded[0]['ExtractedAddresses']['from:'][0]['address']); 00970 //Selective domain to search LDAP - If user email is in this domain (and LDAP search is enabled) then search LDAP 00971 if ($CONFIG['act_auto_new_contact_ldap_selective_domain']) 00972 { 00973 $ldap_selective_domain = strtolower($CONFIG['act_auto_new_contact_ldap_selective_domain']); 00974 } 00975 else 00976 { 00977 debug_log("No domain selected for LDAP search - no LDAP search! Please see the config page and set the domain"); 00978 } 00979 00980 //When an email is FROM: a user in one of these domains the function exits (no contact can be created) 00981 $excluded_domains = $CONFIG['act_auto_new_contact_excluded_domains']; 00982 $contactid = ''; 00983 $ldap_fail = 0; 00984 unset ($detail_found); 00985 $domain = strtolower(act_get_domain_from_email($email)); 00986 debug_log("Domain from email address = {$domain} --- Domain from act_settings = {$ldap_selective_domain}"); 00987 00988 if (!empty($excluded_domains)) 00989 { 00990 if (in_array($domain, $excluded_domains)) 00991 { 00992 debug_log("The domain is in the array of excluded domains! exiting search ..."); 00993 return FALSE; 00994 } 00995 } 00996 00997 //If LDAP search is enabled AND the domain is the selected domain 00998 if (!isset($CONFIG['act_auto_new_contact_ldap'])) 00999 { 01000 $CONFIG['act_auto_new_contact_ldap'] = FALSE;//Default 01001 } 01002 01003 if ($CONFIG['act_auto_new_contact_ldap'] AND $domain == $ldap_selective_domain) 01004 { 01005 debug_log("checking with the LDAP directory ..."); 01006 $check_ldap = act_check_ldap_with_email($email); 01007 01008 if (!$check_ldap) 01009 { 01010 $ldap_fail++; 01011 debug_log("No details found in AD/LDAP for {$email}"); 01012 } 01013 else 01014 { 01015 $site_name = $check_ldap['search_string']; 01016 $contact_surname = cleanvar($check_ldap['surname']); 01017 $contact_forenames = cleanvar($check_ldap['forenames']); 01018 $contact_jobtitle = cleanvar($check_ldap['jobtitle']); 01019 $contact_courtesy = cleanvar($check_ldap['courtesy']); 01020 $contact_username = $check_ldap['username']; 01021 $contact_fullname = cleanvar($check_ldap['fullname']); 01022 01023 if ($site_name) 01024 { 01025 $sql="SELECT id, name FROM `{$dbSites}` WHERE name='$site_name' "; 01026 $siteresult = mysql_query($sql); 01027 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01028 $siterow = mysql_fetch_array($siteresult); 01029 01030 $site_id = $siterow['id']; 01031 01032 if (!$site_id) 01033 { 01034 $ldap_fail++; 01035 debug_log("No site matched for sitename {$site_name} - Site does not exist in SiT"); 01036 } 01037 } 01038 01039 if (!$contact_forenames) 01040 { 01041 $parse_fullname = parse_name($contact_fullname); 01042 $contact_forenames = $parse_fullname['first']; 01043 } 01044 if (!$contact_surname) 01045 { 01046 $parse_fullname = parse_name($contact_fullname); 01047 $contact_surname = $parse_fullname['last']; 01048 } 01049 if (!$contact_forenames OR !$contact_surname) 01050 { 01051 $ldap_fail++; 01052 } 01053 01054 if ($ldap_fail == 0) 01055 { 01056 $detail_found = array ('title' => $contact_courtesy, 01057 'first' => $contact_forenames, 01058 'last' => $contact_surname, 01059 'email' => $email, 01060 'siteid' => $site_id, 01061 'jobtitle' => $contact_jobtitle, 01062 'username' => $contact_username 01063 ); 01064 } 01065 else 01066 { 01067 $detail_found = FALSE; 01068 } 01069 } 01070 } 01071 01072 if (!isset($CONFIG['act_auto_new_contact_sitbase'])) $CONFIG['act_auto_new_contact_sitbase'] = FALSE; 01073 01074 if (!$detail_found AND $domain != $ldap_selective_domain AND $CONFIG['act_auto_new_contact_sitbase'] == TRUE) 01075 { 01076 debug_log("checking with the SiT exisitng base for the same domain ..."); 01077 $check_sitbase = act_check_sitbase_with_email($decoded); 01078 01079 if (!$check_sitbase) 01080 { 01081 debug_log("There was no match from the SiT database"); 01082 return FALSE; 01083 } 01084 else 01085 { 01086 $detail_found = $check_sitbase; 01087 } 01088 } 01089 //Create the new contact 01090 if ($detail_found['last'] AND $detail_found['first'] AND $detail_found['siteid'] AND $detail_found['email']) 01091 { 01092 $contact = new Contact(); 01093 $contact->username = $detail_found['username']; 01094 $contact->surname = $detail_found['last']; 01095 $contact->forenames = $detail_found['first']; 01096 $contact->jobtitle = $detail_found['jobtitle']; 01097 $contact->email = $detail_found['email']; 01098 $contact->courtesytitle = $detail_found['title']; 01099 $contact->siteid = $detail_found['siteid']; 01100 $contact->source = 'act_plugin'; 01101 01102 $status = $contact->add(); 01103 01104 debug_log("New contact with ID= {$status} created"); 01105 return $status; 01106 } 01107 } 01108 01116 function act_check_ldap_with_email($email) 01117 { 01118 global $CONFIG; 01119 //We are using the LDAP settings already in SiT 01120 $ldap_host = $CONFIG['ldap_host']; 01121 $ldap_port = $CONFIG['ldap_port']; 01122 $ldap_protocol = $CONFIG['ldap_protocol']; 01123 $ldap_security = $CONFIG['ldap_security']; 01124 $ldap_user = $CONFIG['ldap_bind_user']; 01125 $ldap_password = $CONFIG['ldap_bind_pass']; 01126 $ldap_base = $CONFIG['ldap_user_base']; 01127 01128 if ($CONFIG['ldap_type']) 01129 { 01130 $ldap_mapping = strtoupper($CONFIG['ldap_type']); 01131 } 01132 else 01133 { 01134 debug_log("LDAP type not set! Please see LDAP settings in the config pages!"); 01135 } 01136 01137 $ldap_selective_domain = $CONFIG['act_auto_new_contact_ldap_selective_domain']; 01138 $ldap_email_displayname = strtolower(constant("LDAP_{$ldap_mapping}_EMAIL")); 01139 $ldap_surname_displayname = strtolower(constant("LDAP_{$ldap_mapping}_SURNAME")); 01140 $ldap_forenames_displayname = strtolower(constant("LDAP_{$ldap_mapping}_FORENAMES")); 01141 $ldap_jobtitle_displayname = strtolower(constant("LDAP_{$ldap_mapping}_JOBTITLE")); 01142 $ldap_courtesy_displayname = strtolower(constant("LDAP_{$ldap_mapping}_COURTESYTITLE")); 01143 $ldap_username_displayname = strtolower(constant("LDAP_{$ldap_mapping}_USERATTRIBUTE")); 01144 $ldap_fullname_displayname = strtolower(constant("LDAP_{$ldap_mapping}_REALNAME")); 01145 01146 //Field to use as site identifier to create the new contact 01147 if (!isset($CONFIG['act_auto_new_contact_ldap_field_to_use'])) 01148 { 01149 $CONFIG['act_auto_new_contact_ldap_field_to_use'] = "departement"; 01150 } 01151 if ($CONFIG['act_auto_new_contact_ldap_field_to_use']) 01152 { 01153 $ldap_field_used = $CONFIG['act_auto_new_contact_ldap_field_to_use']; 01154 } 01155 else 01156 { 01157 debug_log("No field defined to use to identify the site in the DB. Please see the plugin config!"); 01158 return FALSE; 01159 } 01160 01161 unset ($result); 01162 01163 // connect to server 01164 //This code cannot be executed on the same server as AD is installed on!!! 01165 01166 //Connect 01167 $ad = ldap_connect($ldap_host); 01168 if (!$ad) debug_log("Could not connect to LDAP server "); 01169 else debug_log("Connection to {$ldap_host} succesful"); 01170 01171 //Set some variables 01172 ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3); 01173 ldap_set_option($ad, LDAP_OPT_REFERRALS, 0); 01174 01175 //Bind to the ldap directory 01176 $bd = ldap_bind($ad,$ldap_user,$ldap_password); 01177 if (!$bd) debug_log("Could not bind to LDAP server with credentials '$user'"); 01178 else debug_log("Binding to -{$ldap_host}- with -{$ldap_user}- succesfull"); 01179 01180 //Search the directory 01181 $result = ldap_search($ad, $ldap_base, $ldap_email_displayname."=".$email); 01182 if (!$result) 01183 { 01184 debug_log("The LDAP search for >>{$ldap_email_displayname}={$email}<< retuned no results"); 01185 } 01186 01187 //Create result set 01188 $entries = ldap_get_entries($ad, $result); 01189 //print_r($entries[0]); 01190 01191 //Sort and print 01192 debug_log("User count: {$entries['count']}"); 01193 01194 //Can only manage with one result, if there are duplicates we cannot risk continuing 01195 if ($entries['count']==1) 01196 { 01197 $result = array('search_string' => $entries[0]["{$ldap_field_used}"][0], 01198 'surname' => $entries[0]["{$ldap_surname_displayname}"][0], 01199 'forenames' => $entries[0]["{$ldap_forenames_displayname}"][0], 01200 'jobtitle' => $entries[0]["{$ldap_jobtitle_displayname}"][0], 01201 'courtesy' => $entries[0]["{$ldap_courtesy_displayname}"][0], 01202 'username' => $entries[0]["{$ldap_username_displayname}"][0], 01203 'fullname' => $entries[0]["{$ldap_fullname_displayname}"] 01204 ); 01205 debug_log("User details:".print_r($result, TRUE)); 01206 return $result; 01207 } 01208 elseif ($entries['count']>1) 01209 { 01210 debug_log("{$entries['count']} contacts with mail={$email} was found in AD/LDAP"); 01211 return FALSE; 01212 } 01213 //unbind 01214 ldap_unbind($ad); 01215 } 01216 01226 function act_check_sitbase_with_email($decoded) 01227 { 01228 global $dbContacts; 01229 01230 //Get name details 01231 $name_full = $decoded[0]['ExtractedAddresses']['from:'][0]['name']; 01232 $email = $decoded[0]['ExtractedAddresses']['from:'][0]['address']; 01233 $domain = strtolower(act_get_domain_from_email($email)); 01234 debug_log("Domain from email - {$domain} for - {$name_full} email address - {$email} ..."); 01235 01236 if (!$name_full) 01237 { 01238 debug_log("No name in email header "); 01239 return FALSE; 01240 } 01241 else 01242 { 01243 $parsed_name = parse_name($name_full); 01244 debug_log("The parsed name is:".print_r($parsed_name,TRUE)); 01245 } 01246 01247 //exit here if either first or last name is not found 01248 if (!$parsed_name['first'] OR !$parsed_name['last']) 01249 { 01250 debug_log("No first or Last name found in email header. Exiting the function."); 01251 return FALSE; 01252 } 01253 else 01254 { 01255 //search db for similar domain 01256 $dom_sql = "SELECT id, siteid, email FROM `{$dbContacts}` WHERE `email` LIKE '%{$domain}' LIMIT 1 "; 01257 $result = mysql_query($dom_sql); 01258 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 01259 $domain_result = mysql_fetch_object($result); 01260 01261 //get siteid of same site contact 01262 if (!$domain_result) 01263 { 01264 debug_log("There was no match in the DB for this domain."); 01265 return FALSE; 01266 } 01267 else 01268 { 01269 $same_site_id = $domain_result->siteid; 01270 01271 $detail_found = array ('title' => $parsed_name['title'], 01272 'first' => $parsed_name['first']." ".$parsed_name['middle'], 01273 'last' => $parsed_name['last'], 01274 'email' => $email, 01275 'siteid' => $same_site_id, 01276 'jobtitle' => '', 01277 'username' => '' 01278 ); 01279 debug_log("Returned result from SiT base:".print_r($detail_found,TRUE)); 01280 return $detail_found; 01281 } 01282 } 01283 } 01284 01291 function act_get_domain_from_email($email) 01292 { 01293 //Get domain from email address 01294 //TODO: Need to improve this to check the passed email address is Ok 01295 $dom = split("@",$email); 01296 $domain = strtolower($dom[1]); 01297 return $domain; 01298 } 01299 ?>
For more help developing with SiT! see http://sitracker.org/wiki/DevelopmentHowTo