isUploadedFile()) { $filename = 'zone_'.date("Ymd_His"); if($zoneFile->moveUploadedFile(TEMP,$filename)) { require_once ADMIN_PATH.'telkom_zone_import.php'; global $db; if(import_zone(TEMP.$filename,$routefield,$db,$debug)) { unlink(TEMP.$filename); return 'Zone File Imported Successfully!'; } else { return 'Error processing file!'; } } else { return 'File could not be moved to temporary location for processing.'; } } } if(isset($args['tariffSubmit'])&&!empty($args['tariffSubmit'])) { global $tariffFile; if($tariffFile->isUploadedFile()) { $filename = 'tariff_'.date("Ymd_His"); $tariffFile->moveUploadedFile(TEMP,$filename); require_once ADMIN_PATH.'telkom_tariff_import.php'; global $db; if(import_tariffs(TEMP.$filename,$routefield,$db,$debug)) { unlink(TEMP.$filename); return 'Tariff File Imported Successfully!'; } else { return 'Error Processing File!'; } } else { return 'File could not be moved to temporary location for processing.'; } } if(isset($args['gpbSubmit'])&&!empty($args['gpbSubmit'])) { global $gpbFile; if($gpbFile->isUploadedFile()) { $filename = 'gpb_'.date("Ymd_His"); $gpbFile->moveUploadedFile(TEMP,$filename); global $db; $error = false; //{{{ do processing $fp = fopen(TEMP.$filename,"r"); while(!feof($fp)) { $line = fgetcsv($fp, 1000, ","); if($line !== false && !empty($line[0])) { //no errors encountered yet if(count($line) == 3) { //correct number of parameters $number = $line[0]; $name = $line[1]; $type = $line[2]; //{{{ now filter the telephone number to look correct //take out spaces, brackets and dashes $chars = array(' ','-','_','(',')','[',']','{','}','.'); $number = str_replace($chars,'',$number); if(strpos($number,'+')!== false) { $number = substr($number,1); } if(substr($number,0,2)=='27') { $number = '0'. substr($number,2); } //}}} //{{{ filter the type $type = trim($type); if(strlen($type)>1) { $type = substr($type,0,1); } $type = strtoupper($type); if($type != 'B' && $type != 'P' && $type != 'O') { $type = 'O'; } //}}} if(is_numeric($number)) { //check for an existing record in the db $check_query = "SELECT gpb_name,gpb_type FROM global_phone_book WHERE gpb_number='$number'"; $check_res = $db->query($check_query); if(handleError($check_res)) { if($check_res->numRows()>0) { $check_res->fetchInto($check_row); if($check_row['gpb_name']==$name && $check_row['gpb_type'] == $type) { //the entry in the database is the same, so do nothing } else { //do an update $upd_values = array($type,$name,$number); $upd_query = "UPDATE global_phone_book SET gpb_type = ?, gpb_name= ? WHERE gpb_number =?"; $upd_statement = $db->prepare($upd_query); $upd_res = $db->execute($upd_statement,$upd_values); if(handleError($upd_res)) { } else { $error = true; } } } else { //do an insert $field_values = array('gpb_number'=>$number,'gpb_name'=>$name,'gpb_type'=>$type); $res =& $db->autoExecute('global_phone_book',$field_values,DB_AUTOQUERY_INSERT); if(handleError($res)) { } else { $error = true; } } } else { $error = true; } } else { return "Not numeric, check order of variables. Expecting ,,"; } } else { if($debug) {echo "Line: ".PHP_EOL; var_dump($line);} return "Incorrect number of parameters. Expected parameter count is 3"; } } } //}}} if(!$error) { fclose($fp); unlink(TEMP.$filename); return 'Phone Book File Imported Successfully!'; } else { return 'Error Processing File!'; } } else { return 'File could not be moved to temporary location for processing.'; } } if(isset($args['extSubmit'])&&!empty($args['extSubmit'])) { $mailmsg=''; global $extFile; if($extFile->isUploadedFile()) { $filename = 'ext_'.date("Ymd_His"); $extFile->moveUploadedFile(TEMP,$filename); global $LUA; $error = false; $fp = fopen(TEMP.$filename,"r"); while(!feof($fp)) { $line = fgetcsv($fp,1000,','); if(count($line)==2||count($line)==3) { $number = $line[0]; $name = $line[1]; if(count($line)==3) { $email = $line[2]; $matches = array(); if(!preg_match("/^(.+)@([^();:,<>]+\.[a-zA-Z]{2,4})$/",$email,$matches)) { $mailmsg.= "
Warning: $email is an invalid Email address."; $email = ''; } else { } } else { $email = ''; } //take out spaces, brackets and dashes $chars = array(' ','-','_','(',')','[',']','{','}','.'); $number = str_replace($chars,'',$number); //detect if $number is already present in the system //first obtain auth_user_id $filter = array('filters'=>array('handle'=>$number),'fields'=>array('auth_user_id')); $user_id = $LUA->auth->getUsers($filter); //next get its perm_user_id if(!empty($user_id)) { $perm_filter = array('filters'=>array('auth_user_id'=>$user_id[0]['auth_user_id']),'fields'=>array('perm_user_id')); $perm_id = $LUA->perm->getUsers($perm_filter); //print_r($user_id); //print_r($perm_id); if(!empty($perm_id)) { //do an update $userdata = array('user_phone'=>$number, 'user_email'=>$email, 'user_alias'=>$name); $LUA->updateUser($userdata,$perm_id[0]['perm_user_id']); } else { //do nothing, we do not want to create users here } } } else { return "Incorrect number of parameters. Expected parameter count is 2 or 3"; } } if(!$error) { fclose($fp); unlink(TEMP.$filename); return 'Phone List File Imported Successfully!'.$mailmsg; } else { return 'Error Processing File!'; } } else { return 'File could not be moved to temporary location for processing.'; } } } // {{{ further initialisation $debug = $ini['System']['debug']; $form = new HTML_QuickForm(); // }}} if($LU->checkRight(ADMIN)) { $tpl->setTPL('importer.tpl.html'); //{{{ create quickform elements $form->addElement('header','hdrTop','Data File Importer'); //$routes = explode(',',str_replace("'",'',$ini['Translation']['TELKOMOUT'])); $routes = array_keys($ini['Billing']); $route_list = array(); foreach ($routes as $route) { $route_list[$route] = $route; } $sel_route =& $form->addElement('select','routeSelect','Select Channel Alias for Zone/Tariff File: ',$route_list); $zoneFile =& $form->addElement('file','zoneFile','Upload Telkom Zone File: '); $tariffFile =& $form->addElement('file','tariffFile','Upload Telkom Tariff File: '); $gpbFile =& $form->addElement('file','gpbFile','Upload Global Phone Book File: (<number>,<name>,<type>)'); $extFile =& $form->addElement('file','extFile','Upload Internal Phone List File: (<number>,<name>,<email>)'); $form->addElement('submit','zoneSubmit','Import Zone File'); $form->addElement('submit','tariffSubmit','Import Tariff File'); $form->addElement('submit','gpbSubmit','Import Phone Book'); $form->addElement('submit','extSubmit','Import Phone List'); //$form->addGroup(array($zoneFile,$tariffFile),'files'); //}}} //{{{ add rules //$form->addGroupRule('files','Please select a file','uploadedfile'); //$form->addRule('zoneFile','Please select a file','uploadedfile'); $form->addRule('zoneFile','The file you selected is too big','maxfilesize',2000000); //$form->addRule('tariffFile','Please select a file','uploadedfile'); $form->addRule('tariffFile','The file you selected is too big','maxfilesize',2000000); //}}} if ($form->validate()) { $result = $form->process('process_data',true); $tpl->assign('txtResult',$result); } else { } $tpl->display($form); } else { $tpl->setTPL('access_denied.tpl.html'); } ?>