<?php
class Helper{
	public static function reboot(){
		/* 2 verschiedene Prozesse werden gestartet
		* der erste Prozess führt ein rcK aus,
		* der zweite Prozess wartet 10 Sekunden und wenn der rcK Prozess
		* nicht richtig beendet wurde, wird diese gestoppt und die Karte wird
		* neugestartet. Per AJAX wird geprüft ob die Karte wieder UP ist, das
		* Anmeldungfenster wird dann wieder angezeigt.
		*/
		$pid = pcntl_fork();

		if($pid) {
		// this is the parent process
		// wait until the child has finished processing then end the script
			sleep(10);
			print("<script type='text/javascript'>
						window.setTimeout('setRequest()',5000);
				</script>");
			exec('/usr/bin/killall rcK');
			exec ('/sbin/reboot');
			//sleep(10);
			//break;
			exit;
		}

		// the child process runs its stuff here and then ends
		exec('/bin/sh /usr/local/init/rcK');

	}

	/* Funktion für das Löschen eines Moduls und der entsprechenden Dialplaneinträge,
	 * wenn die Belegung der Line Interfaces sich ändert
	 */
	public static function deleteModul($li,$name,$table){
		//echo $li.$name.$table;
		$ba= new beroAri();
		$query=$ba->select("select * from $table where lif='$li' and type='$name'");
		while ($entry=$ba->fetch_array($query)){
			$ba->delete("delete from dialplan where fromDirection='$table' and fromId='g:".$entry['gr']."'");
			$ba->delete("delete from dialplan where fromDirection='$table' and fromId='".$entry['port']."'");
			$ba->delete("delete from dialplan where toDirection='$table' and toId='g:".$entry['gr']."'");
			$ba->delete("delete from dialplan where toDirection='$table' and toId='".$entry['port']."'");
		}
		$ba->delete("delete from $table where lif='$li' and type='$name'");

		$ba->set('portswitch',0);

		$q=$ba->select("SELECT type, lif FROM isdn union SELECT type, lif FROM analog union SELECT type, lif FROM gsm order by lif");
		$i=0;
		while ($ent=$ba->fetch_array($q)){
			$lif[$i]=$ent['lif'];
			$type[$i]=$ent['type'];
			$i++;
		}

		$res=file_get_contents('/proc/cmdline');
		preg_match("/.*revision=(?<revision>.*)/",$res,$revision);
		$revision=$revision['revision'];
		if (($type[0]=="bf4S0" || $type[0]=="bf2S02FXS" || $type[0]=="bf4FXS" || $type[0]=="bf4FXO") && !isset($type[1]) && $revision!="1.80" && $revision!="1.8"){
			$ba->set('portswitch',1);
		}

		$query=$ba->select("select option from activate where id='activate'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
		}
		echo "<script>aktivieren(1,'state','3','".ACTIVATE."');</script>";


	}

	/* Funktion für das Erstellen eines Moduls
	 */
	public static function createModul(){
		$ba=new beroAri();

		$res=file_get_contents('/proc/li1');
		preg_match("/type: (?<li1>.*)/",$res,$li1);
		$li1=$li1['li1'];
			if ($li1=="xhfc-4su"){
				for ($i=0;$i<=3;$i++){
					$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf4S0','1','0','', 'BRI');");
				}
			}elseif($li1=="hfc-e1"){
				$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf1E1','1','0','', 'PRI');");
			}elseif($li1=="hfc-2e1"){
				for ($i=0;$i<=1;$i++){
					$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2E1','1','0','','PRI');");
				}
			}elseif($li1=="telit-2GSM"){
				for ($i=0;$i<=1;$i++){
					$ba->insert_("INSERT INTO gsm (type,lif,master, config, port_type) VALUES('bf2GSM','1','1','','GSM');");
				}
			}elseif($li1=="si-4fxs"){
				for ($i=0;$i<=3;$i++){
					$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXS','1','0','','FXS');");
				}
			}elseif($li1=="si-4fxo"){
				for ($i=0;$i<=3;$i++){
					$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXO','1','0','','FXO');");
				}
			}elseif($li1=="xhfcSi-2s02fxs"){
				for ($i=0;$i<=1;$i++){
					$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2S02FXS','1','0','','BRI');");
				}
				for ($i=0;$i<=1;$i++){
					$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf2S02FXS','1','0','','FXS');");
				}
			}

		if ($ba->is_error()){
			echo "<script>alert(\"".$ba->error()."\");</script>";
		}
		$query=$ba->select("select option from activate where id='activate'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
		}


	}

	public static function getIsdnPorts(){
		//Ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();
		//ISDN Ports suchen
		$qu = $ba->select("select port from isdn");
		$result=array();
		while ($entry = $ba->fetch_array($qu)){
			array_push($result,$entry['port']);
		}
		//Array zurückgeben
		return $result;
	}

	/* Gibt zurück die Bezeichnung einen Line interface
	 */
	public static function getLiName($name){
		 if ($name=="xhfc-4su") return 'bf4S0';
		 if ($name=="hfc-e1") return 'bf1E1';
	     if ($name=="hfc-2e1") return 'bf2E1';
		 if ($name=="telit-2GSM") return 'bf2GSM';
		 if ($name=="si-4fxs") return 'bf4FXS';
		 if ($name=="si-4fxo") return 'bf4FXO';
		 if ($name=="xhfcSi-2s02fxs") return 'bf2S02FXS';
		 if ($name=="unknown") return 'none';



	}
}
?>
