[Added import/export for SIP domains Adrian Georgescu **20130125102610 Ignore-this: 522b1468a98734837cce6b63c6f1a39f ] hunk ./library/ngnpro_client.php 458 + if (strlen($this->soapEngines[$this->soapEngine]['voicemail_engine'])) { + $this->voicemail_engine=$this->soapEngines[$this->soapEngine]['voicemail_engine']; + } + hunk ./library/ngnpro_client.php 593 + if ($this->voicemail_engine) { + $this->SOAPloginVoicemail = array( + "username" => $this->soapEngines[$this->voicemail_engine]['username'], + "password" => $this->soapEngines[$this->voicemail_engine]['password'], + "admin" => true, + "impersonate" => intval($this->reseller) + ); + + $this->SoapAuthVoicemail = array('auth', $this->SOAPloginVoicemail , 'urn:AGProjects:NGNPro', 0, ''); + + $this->SOAPurlVoicemail = $this->soapEngines[$this->voicemail_engine]['url']; + $this->soapclientVoicemail = new WebService_NGNPro_VoicemailPort($this->SOAPurlVoicemail); + + $this->soapclientVoicemail->setOpt('curl', CURLOPT_SSL_VERIFYPEER, 0); + $this->soapclientVoicemail->setOpt('curl', CURLOPT_SSL_VERIFYHOST, 0); + + if (strlen($this->soapEngines[$this->voicemail_engine]['timeout'])) { + $this->soapclientVoicemail->_options['timeout'] = intval($this->soapEngines[$this->voicemail_engine]['timeout']); + } else { + $this->soapclientVoicemail->_options['timeout'] = $this->timeout; + } + } + hunk ./library/ngnpro_client.php 1663 - hunk ./library/ngnpro_client.php 1665 + if ($_REQUEST['action'] == 'Export' and $this->rows) { + $this->exportDomain($result->domains[0]->domain); + return; + } + + hunk ./library/ngnpro_client.php 1709 - $_url = $this->url.sprintf("&service=%s&action=Delete&domain_filter=%s", + $delete_url = $this->url.sprintf("&service=%s&action=Delete&domain_filter=%s", hunk ./library/ngnpro_client.php 1716 - $_url .= "&confirm=1"; - $actionText = "Confirm"; + $delete_url .= "&confirm=1"; + $deleteText = "Confirm"; + } else { + $deleteText = "Delete"; + } + + if ($_REQUEST['action'] == 'Delete' && + $_REQUEST['domain_filter'] == $domain->domain) { + $delete_url .= "&confirm=1"; + $deleteText = "Confirm"; hunk ./library/ngnpro_client.php 1727 - $actionText = "Delete"; + $deleteText = "Delete"; hunk ./library/ngnpro_client.php 1749 - - printf(" - - %s - %s.%s - %s - Sip accounts - Sip aliases - %s - %s - ", - $index, - $_customer_url, - $domain->customer, - $domain->reseller, - $_sip_domains_url, - $domain->domain, - $_sip_accounts_url, - $_sip_aliases_url, - $domain->changeDate, - $_url, - $actionText - ); + if ($this->adminonly) { + $export_url = $this->url.sprintf("&service=%s&action=Export&domain_filter=%s", + urlencode($this->SoapEngine->service), + urlencode($domain->domain) + ); + + printf(" + + %s + %s.%s + %s + Sip accounts + Sip aliases + %s + %s + Export + ", + $index, + $_customer_url, + $domain->customer, + $domain->reseller, + $_sip_domains_url, + $domain->domain, + $_sip_accounts_url, + $_sip_aliases_url, + $domain->changeDate, + $delete_url, + $deleteText, + $export_url + ); + } else { + printf(" + + %s + %s.%s + %s + Sip accounts + Sip aliases + %s + %s + ", + $index, + $_customer_url, + $domain->customer, + $domain->reseller, + $_sip_domains_url, + $domain->domain, + $_sip_accounts_url, + $_sip_aliases_url, + $domain->changeDate, + $delete_url, + $deleteText + ); + } hunk ./library/ngnpro_client.php 1824 + function exportRecord($dictionary=array()) { + } + hunk ./library/ngnpro_client.php 1856 - printf ("
",$_SERVER['PHP_SELF']); + printf ("",$_SERVER['PHP_SELF']); hunk ./library/ngnpro_client.php 1870 - + printf (" Import SIP domain from file: + +
+
+
+ +
+ + Select file + Change + + Remove + +
+
+ " + ); hunk ./library/ngnpro_client.php 1893 + if ($this->adminonly && $_FILES['import_file']['tmp_name']) { + $content=fread(fopen($_FILES['import_file']['tmp_name'], "r"), $_FILES['import_file']['size']); + //print_r($content); hunk ./library/ngnpro_client.php 1897 - if ($dictionary['domain']) { - $domain = $dictionary['domain']; - } else { - $domain = trim($_REQUEST['domain']); - } + if (!$imported_data=json_decode($content, true)) { + printf ("

Error: reading imported data. "); + return false; + } hunk ./library/ngnpro_client.php 1902 - list($customer,$reseller)=$this->customerFromLogin($dictionary); + //print_r($imported_data); hunk ./library/ngnpro_client.php 1904 - if (!$this->validDomain($domain)) { - print "Error: invalid domain name"; - return false; - } + if (!in_array('sip_domains', array_keys($imported_data))) { + printf ("

Error: Missing SIP domains in imported data. "); + return false; + } hunk ./library/ngnpro_client.php 1909 - $domainStructure = array('domain' => strtolower($domain), - 'customer' => intval($customer), - 'reseller' => intval($reseller) - ); - $function=array('commit' => array('name' => 'addDomain', - 'parameters' => array($domainStructure), - 'logs' => array('success' => sprintf('SIP domain %s has been added',$domain))) - ); + if (!in_array('sip_accounts', array_keys($imported_data))) { + return false; + printf ("

Error: Missing SIP accounts in imported data. "); + } hunk ./library/ngnpro_client.php 1914 - return $this->SoapEngine->execute($function,$this->html); + foreach($imported_data['customers'] as $customer) { + // Insert credetials + $this->SoapEngine->soapclientCustomers->addHeader($this->SoapEngine->SoapAuth); + + $customer['credit'] = floatval($customer['credit']); + $customer['balance'] = floatval($customer['balance']); + // Call function + $result = $this->SoapEngine->soapclientCustomers->addAccount($customer); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + if ($error_fault->detail->exception->errorcode == 5001) { + $result = $this->SoapEngine->soapclientCustomers->updateCustomer($customer); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + } else { + printf('

Customer %s has been updated',$customer['id']); + } + } else { + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + } + } else { + printf('

Customer %s has been added',$customer['id']); + } + + } + + foreach($imported_data['sip_domains'] as $domain) { + flush(); + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->addDomain($domain); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + if ($error_fault->detail->exception->errorcode == 1001) { + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->updateDomain($domain); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + } else { + printf('

SIP domain %s has been updated',$domain['domain']); + } + } else { + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + } + } else { + printf('

SIP domain %s has been added',$domain['domain']); + } + + } + $i = 0; + $added = 0; + $updated = 0; + $failed = 0; + foreach($imported_data['sip_accounts'] as $account) { + $i+=1; + flush(); + $account['callLimit'] = intval($account['callLimit']); + $account['prepaid'] = intval($account['prepaid']); + $account['quota'] = intval($account['quota']); + $account['owner'] = intval($account['owner']); + $account['timeout'] = intval($account['timeout']); hunk ./library/ngnpro_client.php 1989 + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->addAccount($account); + + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + if ($error_fault->detail->exception->errorcode == 1011) { + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->updateAccount($account); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + $failed += 1; + } else { + printf('

%d SIP account %s@%s has been updated',$i,$account['id']['username'], $account['id']['domain']); + $updated += 1; + } + } else { + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + $failed += 1; + } + } else { + printf('

%d SIP account %s@%s has been added',$i, $account['id']['username'], $account['id']['domain']); + $added += 1; + } + } + if ($added) { + printf('

%d SIP accounts added',$added); + } + if ($updated ) { + printf('

%d SIP accounts updated',$updated); + } + if ($failed) { + printf('

%d SIP accounts failed',$failed); + } + + $added = 0; + foreach($imported_data['sip_aliases'] as $alias) { + flush(); + + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->addAlias($alias); + + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + printf ("

Error: $log"); + } else { + $added += 1; + } + } + + if ($added) { + printf('

%d SIP aliases added',$added); + } + + return true; + } else { + if ($dictionary['domain']) { + $domain = $dictionary['domain']; + } else { + $domain = trim($_REQUEST['domain']); + } + + list($customer,$reseller)=$this->customerFromLogin($dictionary); + + if (!$this->validDomain($domain)) { + print "Error: invalid domain name"; + return false; + } + + $domainStructure = array('domain' => strtolower($domain), + 'customer' => intval($customer), + 'reseller' => intval($reseller) + ); + $function=array('commit' => array('name' => 'addDomain', + 'parameters' => array($domainStructure), + 'logs' => array('success' => sprintf('SIP domain %s has been added',$domain))) + ); + + return $this->SoapEngine->execute($function,$this->html); + } hunk ./library/ngnpro_client.php 2295 + function hide_html() { + if ($_REQUEST['action'] == 'Export') { + return true; + } else { + return false; + } + } + + function exportDomain($domain) { + $exported_data= array(); + // Filter + $filter=array( + 'domain' => $domain, + 'customer' => intval($this->filters['customer']), + 'reseller' => intval($this->filters['reseller']) + ); + + // Range + $range=array('start' => 0, + 'count' => 1000 + ); + // Compose query + $Query=array('filter' => $filter, + 'range' => $range + ); + + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->getDomains($Query); + + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + syslog(LOG_NOTICE, $log); + return false; + } else { + $i = 0 ; + + while ($i < $result->total) { + $domain = $result->domains[$i]; + if (!in_array($domain->customer, $export_customers)) { + $export_customers[]=$domain->customer; + } + if (!in_array($domain->reseller, $export_customers)) { + $export_customers[]=$domain->reseller; + } + $i+=1; + $exported_data['sip_domains'][] = objectToArray($domain); + } + } + + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + // Call function + $result = call_user_func_array(array($this->SoapEngine->soapclient,'getAccounts'),array($Query)); + + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + syslog(LOG_NOTICE, $log); + return false; + } else { + $exported_data['sip_accounts'] = objectToArray($result->accounts); + foreach ($result->accounts as $account) { + if (!in_array($account->owner, $export_customers)) { + $export_customers[]=$account->owner; + } + + $sipId=array("username" => $account->id->username, + "domain" => $account->id->domain + ); + $this->SoapEngine->soapclientVoicemail->addHeader($this->SoapEngine->SoapAuthVoicemail); + $result = $this->SoapEngine->soapclientVoicemail->getAccount($sipId); + + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + if ($error_fault->detail->exception->errorcode != "2000" && $error_fault->detail->exception->errorcode != "1010") { + printf ("

Error (VoicemailPort): %s (%s): %s",$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + } + } else { + $exported_data['voicemail_accounts'][] = $result; + } + + // Filter + $filter=array('targetUsername' => $account->id->username, + 'targetDomain' => $account->id->domain + ); + + // Range + $range=array('start' => 0, + 'count' => 20 + ); + + // Compose query + $Query=array('filter' => $filter, + 'range' => $range + ); + + // Call function + $this->SoapEngine->soapclient->addHeader($this->SoapEngine->SoapAuth); + $result = $this->SoapEngine->soapclient->getAliases($Query); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + printf ("

Error (SipPort): %s (%s): %s",$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + } else { + foreach ($result->aliases as $alias) { + $exported_data['sip_aliases'][] = objectToArray($alias); + } + } + } + } + + foreach ($export_customers as $customer) { + if (!$customer) { + continue; + } + $filter=array( + 'customer' => intval($customer), + ); + + // Compose query + $Query=array('filter' => $filter + ); + + // Insert credetials + $this->SoapEngine->soapclientCustomers->addHeader($this->SoapEngine->SoapAuth); + + // Call function + $result = $this->SoapEngine->soapclientCustomers->getCustomers($Query); + if (PEAR::isError($result)) { + $error_msg = $result->getMessage(); + $error_fault= $result->getFault(); + $error_code = $result->getCode(); + $log=sprintf("SOAP request error from %s: %s (%s): %s",$this->SoapEngine->SOAPurl,$error_msg, $error_fault->detail->exception->errorcode,$error_fault->detail->exception->errorstring); + syslog(LOG_NOTICE, $log); + return false; + } else { + $exported_data['customers'] = objectToArray($result->accounts); + } + } + //print_r($exported_data['customers']); + + print_r(json_encode($exported_data)); + } hunk ./library/ngnpro_client.php 2598 - // Insert credetials + // Insert credentials hunk ./library/sip_settings.php 4192 - $result->timeout=intval($result->timeout); + if ($result->owner == '') { + $this->somethingChanged=1; + $result->owner=0; + } + + if ($result->callLimit == '') { + $this->somethingChanged=1; + $result->callLimit=0; + } + + if ($result->quota == '') { + $this->somethingChanged=1; + $result->quota=0; + } + + if ($result->timeout == '') { + $this->somethingChanged=1; + $result->timeout=35; + } hunk ./library/sip_settings.php 5406 - 'owner' => $this->owner + 'owner' => intval($this->owner) hunk ./provisioning.phtml 38 - Header("Content-type: text/plain"); - Header("Content-Disposition: attachment; filename=export.txt"); + Header("Content-type: application/json"); + Header("Content-Disposition: attachment; filename=ngnpro_export.json");