All pastes #93645 Raw Edit

Untitled

public text v1 · immutable
#93645 ·published 2006-07-20 17:21 UTC
rendered paste body
<?php

class extensions {
	/** The config
	 * array(section=>array(extension=>array( array('basetag'=>basetag,'tag'=>tag,'addpri'=>addpri,'cmd'=>cmd) )))
	 * ( $_exts[$section][$extension][$priority] )
	 */
	var $_exts;
	
	/** Hints 
	 * special cases of priorities
	 */
	var $_hints;
	
	var $_sorted;
	
	/** The filename to write this configuration to
	*/
	function get_filename() {
		return "extensions_additional.conf";
	}
	
	/** Add an entry to the extensions file
	* @param $section    The section to be added to
	* @param $extension  The extension used
	* @param $tag        A tag to use (to reference with basetag), use false or '' if none
	* @param $command    The command to execute
	* @param $basetag    The tag to base this on. Only used in conjunction with $addpriority
	*                    priority. Defaults to false.
	* @param $addpriority  Finds the priority of the tag called $basetag, and adds this 
	*			value to it to use as the priority for this command.
	* @return 
	*/
	function add($section, $extension, $tag, $command, $basetag = false, $addpriority = false) {
		
		if ($basetag || $addpriority) {
			if (!is_int($addpriority) || ($addpriority < 1)) {
				trigger_error(E_ERROR, "\$addpriority must be >= 1 in extensions::add()");
				return false;
			}
			if (empty($basetag)) {
				trigger_error(E_ERROR, "\$basetag is required with \$addpriority in extensions::add()");
				return false;
			}
		}
		
		if (empty($basetag)) {
			// no basetag, we need to make one
			
			if (!isset($this->_exts[$section][$extension])) {
				// first entry, use 1
				$basetag = '1';
			} else {
				// anything else just n
				$basetag = 'n';
			}
		}
		
		$new = array(
			'basetag' => $basetag,
			'tag' => $tag,
			'addpri' => $addpriority,
			'cmd' => $command,
		);
		
		$this->_exts[$section][$extension][] = $new;
	}
	
	/** Sort sections, extensions and priorities alphabetically
	 */
	function sort() {
		foreach (array_keys($this->_exts) as $section) {
			foreach (array_keys($this->_exts[$section]) as $extension) {
				// sort priorities
				ksort($this->_exts[$section][$extension]);
			}
			// sort extensions
			ksort($this->_exts[$section]);
		}
		// sort sections
		ksort($this->_exts);
		
		$this->_sorted = true;
	}
	
	function addHint($section, $extension, $hintvalue) {
		$this->_hints[$section][$extension][] = $hintvalue;
	}
	
	function addGlobal($globvar, $globval) {
		$this->_globals[$globvar] = $globval;
	}
	
	function addInclude($section, $incsection) {
		$this->_includes[$section][] = $incsection;
	}
	
	/* This function allows new priorities to be injected into already generated dialplan
	*  usage: $ext->splice($context, $exten, $priority_number, new ext_goto('1','s','ext-did'));
	*/
	function splice($section, $extension, $priority, $command) {
		if($priority == 0) {
			$basetag = '1';
			// we'll be defining a new pri "1", so change existing "1" to "n"
			$this->_exts[$section][$extension][0]['basetag'] = 'n';
		} else {
			$basetag = 'n';
		}
		$newcommand = array(
			'basetag' => $basetag,
			'tag' => '',
			'addpri' => '',
			'cmd' => $command
		);

		/* This little routine from http://ca.php.net/array_splice overcomes 
		*  problems that array_splice has with multidmentional arrays
		*/
			$array = $this->_exts[$section][$extension];
			$ky = $priority;
			$val = $newcommand;
			$n = $ky; 
			 foreach($array as $key => $value) 
			   { 
				$backup_array[$key] = $array[$key]; 
			   } 
			 $upper_limit = count($array); 
			 while($n <= $upper_limit) 
			   { 
				if($n == $ky) 
				  { 
			 $array[$n] = $val; 
			// echo $n; 
				  } 
				else 
				  { 
			 $i = $n - "1"; 
			 $array[$n] = $backup_array[$i]; 
				  } 
				$n++; 
			   } 
		
		// apply our newly modified array
		//echo "Splicing [$section] $extension\n";
		$this->_exts[$section][$extension] = $array;		
		
		//print_r($this->_exts[$section][$extension]);
	}
	
	/** Generate the file
	* @return A string containing the extensions.conf file
	*/