provision.php (5848B)
1 <?php 2 //error_reporting(E_ALL); 3 4 // Initialization 5 require_once ('lib/Spyc.php'); 6 7 $DEVICES_YML = 'devices-'.$_SERVER['HTTP_HOST'].'.yml'; 8 9 if ( !is_file( $DEVICES_YML )) 10 $DEVICES_YML = 'devices.yml'; 11 12 $devices = Spyc::YAMLLoad($DEVICES_YML); 13 14 $contactsCSV = @$devices['phonebook']['file']; 15 16 if ( ! is_file($contactsCSV)) 17 throw new RuntimeException('file does not exist: '.$contactsCSV); 18 19 $handle = fopen($contactsCSV, "r"); 20 $contacts = []; 21 while (($contacts[] = fgetcsv($handle)) !== FALSE) { 22 } 23 24 $keys = array_shift($contacts); 25 foreach ($contacts as $i=> $row) { 26 $contacts[$i] = @array_combine($keys, $row); 27 } 28 29 $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]"; 30 31 32 33 if ( $_GET['action']=='settings') 34 { 35 // Step 1: Create Phone settings 36 header('Content-Type: text/xml'); 37 38 $doc = new DOMDocument('1.0','UTF-8'); 39 $settings = $doc->createElement('settings'); 40 $doc->appendChild($settings); 41 42 // Phone settings 43 foreach( $devices['devices'] as $device ) { 44 if (str_replace(':','',@$device['mac'])==$_GET['mac']) { 45 46 47 $phoneSettings = [ 48 'language' => $devices['system']['language'], 49 'web_language' => $devices['system']['language'], 50 'setting_server'=> $actual_link.'?action=settings&mac={mac}', 51 'settings_refresh_timer'=> '1800', 52 'update_policy' => 'settings_only', 53 'ip_adr' => $device['ip'], 54 'netmask' => $devices['system']['netmask'], 55 'dns_domain' => $devices['system']['domain'], 56 'dns_server1' => $devices['system']['gateway'], 57 'dhcp' => 'off', 58 'gateway' => $devices['system']['gateway'], 59 'phone_name' => $device['host'], 60 'http_user' => $devices['system']['admin']['user'], 61 'http_pass' => $devices['system']['admin']['password'], 62 'admin_mode_password' => $devices['system']['admin']['password'], 63 'admin_mode_password_confirm' => $devices['system']['admin']['password'] 64 ]; 65 if ( @$device['ip'] ) 66 { 67 $phoneSettings['ip_adr'] = $device['ip']; 68 $phoneSettings['dhcp' ] = 'off'; 69 }else { 70 $phoneSettings['dhcp' ] = 'on'; 71 } 72 if ( @$devices['system']['ntp'] ) 73 $phoneSettings['ntp_server'] = $devices['system']['ntp']; 74 75 $phonesettings = new DOMElement('phone-settings'); 76 $settings->appendChild( $phonesettings ); 77 $phonesettings->setAttribute('e','2'); 78 79 foreach( $phoneSettings as $name=>$value ) 80 { 81 $e = new DOMElement($name,$value); 82 $phonesettings->appendChild($e); 83 $e->setAttribute('perm','RW'); 84 } 85 86 $userIndex = 1; 87 foreach( $device['users'] as $username ) 88 { 89 $user = @$devices['accounts'][$username]; 90 if ( !$user ) 91 $user = []; 92 //$user = array_merge($devices['system'],$user); 93 94 $userSettings = [ 95 'active'=>'on', 96 'realname'=>@$user['label']?$user['label']:$username, 97 'pass'=>@$user['password']?$user['password']:@$devices['system']['proxy'], 98 'name'=>@$user['user']?$user['user']:$username, 99 'host'=>@$user['proxy']?$user['proxy']:@$devices['system']['proxy'] 100 ]; 101 102 foreach( $userSettings as $name=>$value ) 103 { 104 $u = new DOMElement('user_'.$name,$value); 105 $phonesettings->appendChild($u); 106 $u->setAttribute('perm','RW'); 107 $u->setAttribute('idx',$userIndex); 108 } 109 $userIndex++; 110 } 111 } 112 } 113 114 115 // Step 2: Phone book 116 117 $book = new DOMElement('tbook'); 118 $settings->appendChild($book); 119 $book->setAttribute('e','2'); 120 $settings->appendChild($book); 121 122 $idx=1; 123 foreach($contacts as $contact) { 124 for( $p = 1; $p <= 6; $p++ ) { 125 126 if ( !@$contact['Phone '.$p.' - Value']) continue; // Must have a telephone number 127 128 $item = new DOMElement('item'); 129 $book->appendChild($item); 130 $item->setAttribute('context',''); 131 $item->setAttribute('type',''); 132 $item->setAttribute('fav','false'); 133 $item->setAttribute('mod','true'); 134 $item->setAttribute('index',$idx++); 135 136 $phone = [ 137 'name'=>$contact['Name'], 138 'first_name'=>$contact['Given Name'], 139 'last_name'=>$contact['Family Name'], 140 'number'=>$contact['Phone '.$p.' - Value'], 141 // TODO: number_type "/sip/mobile/fixed/home/business" 142 'email'=>$contact['E-mail 1 - Value'], 143 'note'=>$contact['Address 1 - Formatted'], 144 'organization'=>$contact['Organization 1 - Name'] 145 ]; 146 if (@$contact['Birthday']) 147 $phone['birthday']=date("m/d/Y", strtotime($contact['Birthday'])); 148 149 foreach( $phone as $name=>$value) 150 $item->appendChild( new DOMElement($name,$value) ); 151 } 152 } 153 echo $doc->saveXML(); 154 } 155 156 157 elseif ( $_GET['action']=='debug') 158 { 159 header('Content-Type: text/plain'); 160 echo "\n\nDevice configuration:\n\n"; 161 print_r($devices); 162 echo "\n\nPhonebook:\n\n"; 163 print_r($contacts); 164 } 165 166 else { 167 header('Content-Type: text/plain'); 168 169 echo "Error: No valid action parameter"; 170 }