First simple implementation of tftp_provisioner.php
Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
include_once("logger.php");
|
include_once("logger.php");
|
||||||
include_once("utils.php");
|
include_once("utils.php");
|
||||||
|
//$base_path = !empty($_SERVER['DOCUMENT_ROOT']) ? realpath($_SERVER['DOCUMENT_ROOT'] . "/.."): realpath(getcwd() . "/..");
|
||||||
|
$base_path = realpath(__DIR__ . DIRECTORY_SEPARATOR . "..");
|
||||||
|
|
||||||
$base_path = !empty($_SERVER['DOCUMENT_ROOT']) ? realpath($_SERVER['DOCUMENT_ROOT'] . "/../"): realpath(getcwd()."/../");
|
|
||||||
$base_config = Array(
|
$base_config = Array(
|
||||||
'main' => Array(
|
'main' => Array(
|
||||||
'debug' => TRUE,
|
'debug' => true,
|
||||||
'default_language' => 'English_United_States',
|
'default_language' => 'English_United_States',
|
||||||
'log_type' => "NULL",
|
'log_type' => "NULL",
|
||||||
'log_level' => 'LOG_EMERG'
|
'log_level' => 'LOG_EMERG'
|
||||||
@@ -51,7 +52,7 @@ $base_config = Array(
|
|||||||
'charset' => 'utf-8'
|
'charset' => 'utf-8'
|
||||||
),
|
),
|
||||||
'urls' => Array(
|
'urls' => Array(
|
||||||
'security' => FALSE,
|
'security' => false,
|
||||||
'information' => NULL,
|
'information' => NULL,
|
||||||
'authentication' => NULL,
|
'authentication' => NULL,
|
||||||
'services' => NULL,
|
'services' => NULL,
|
||||||
@@ -62,19 +63,19 @@ $base_config = Array(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$tree_base = Array(
|
$tree_base = Array(
|
||||||
'settings' => array('path' => 'tftproot', "strip" => TRUE),
|
'settings' => array('path' => 'tftproot', "strip" => true),
|
||||||
'wallpapers' => array('path' => 'tftproot', "strip" => FALSE),
|
'wallpapers' => array('path' => 'tftproot', "strip" => false),
|
||||||
'ringtones' => array('path' => 'tftproot', "strip" => TRUE),
|
'ringtones' => array('path' => 'tftproot', "strip" => true),
|
||||||
'locales' => array('path' => 'tftproot', "strip" => TRUE),
|
'locales' => array('path' => 'tftproot', "strip" => true),
|
||||||
'firmware' => array('path' => 'tftproot', "strip" => TRUE),
|
'firmware' => array('path' => 'tftproot', "strip" => true),
|
||||||
'languages' => array('path' => 'locales', "strip" => FALSE),
|
'languages' => array('path' => 'locales', "strip" => false),
|
||||||
'countries' => array('path' => 'locales', "strip" => FALSE),
|
'countries' => array('path' => 'locales', "strip" => false),
|
||||||
'default_language' => array('path' => 'locales', "strip" => TRUE),
|
'default_language' => array('path' => 'locales', "strip" => true),
|
||||||
);
|
);
|
||||||
|
|
||||||
# Merge config
|
# Merge config
|
||||||
//$ini_array = parse_ini_file("$base_path/config.ini", TRUE, INI_SCANNER_TYPED);
|
//$ini_array = parse_ini_file("$base_path/config.ini", true, INI_SCANNER_TYPED);
|
||||||
$ini_array = parse_ini_file_multi("$base_path/config.ini", TRUE, INI_SCANNER_TYPED);
|
$ini_array = parse_ini_file_multi("$base_path/config.ini", true, INI_SCANNER_TYPED);
|
||||||
if (!empty($ini_array)) {
|
if (!empty($ini_array)) {
|
||||||
$config = array_merge($base_config, $ini_array);
|
$config = array_merge($base_config, $ini_array);
|
||||||
}
|
}
|
||||||
|
101
tftp_provisioner.php
Executable file
101
tftp_provisioner.php
Executable file
@@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
require_once("lib/tftp.php");
|
||||||
|
require_once("lib/config.php");
|
||||||
|
require_once("lib/resolver.php");
|
||||||
|
|
||||||
|
class TFTPProvisioner extends TFTPServer
|
||||||
|
{
|
||||||
|
private $_debug;
|
||||||
|
private $_resolver;
|
||||||
|
private $_filename;
|
||||||
|
|
||||||
|
function __construct($server_url, $config, $logger = NULL, $debug = false)
|
||||||
|
{
|
||||||
|
$this->_config = $config;
|
||||||
|
if (!$logger) {
|
||||||
|
$logger = new Logger_NULL('LOG_ERROR');
|
||||||
|
}
|
||||||
|
parent::__construct($server_url, $logger);
|
||||||
|
$this->_debug = $debug;
|
||||||
|
$this->max_put_size = 60000000;
|
||||||
|
$this->_resolver = new Resolver($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists($peer, $req_filename)
|
||||||
|
{
|
||||||
|
if (($this->_filename = $this->_resolver->resolve($req_filename))) {
|
||||||
|
return file_exists($this->_filename);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function readable($peer, $req_filename)
|
||||||
|
{
|
||||||
|
return is_readable($this->_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($peer, $req_filename, $mode)
|
||||||
|
{
|
||||||
|
return file_get_contents($this->_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function writable($peer, $req_filename)
|
||||||
|
{
|
||||||
|
// check $req_filename starts with 'settings/' (SPA phones can write to tftpboot)
|
||||||
|
$settings_path = $this->_config['main']['base_path'] . DIRECTORY_SEPARATOR
|
||||||
|
. $this->_config['subdirs']['settings']['path'] . DIRECTORY_SEPARATOR;
|
||||||
|
$filename = $settings_path . basename($req_filename);
|
||||||
|
if (is_writable($filename) || (!file_exists($filename) && is_writable($settings_path))) {
|
||||||
|
$this->_filename = $filename;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function put($peer, $filename, $mode, $content)
|
||||||
|
{
|
||||||
|
return file_put_contents($this->_filename, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* STDOUT Log functions
|
||||||
|
*/
|
||||||
|
private function log($peer, $level, $message)
|
||||||
|
{
|
||||||
|
echo(date("H:i:s") . " $level $peer $message\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function log_debug($peer, $message)
|
||||||
|
{
|
||||||
|
if($this->_debug)
|
||||||
|
$this->log($peer, "D", $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function log_info($peer, $message)
|
||||||
|
{
|
||||||
|
$this->log($peer, "I", $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function log_warning($peer, $message)
|
||||||
|
{
|
||||||
|
$this->log($peer, "W", $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function log_error($peer, $message)
|
||||||
|
{
|
||||||
|
$this->log($peer, "E", $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$host = "127.0.0.1";
|
||||||
|
$port = 10069;
|
||||||
|
$url = "udp://$host:$port";
|
||||||
|
|
||||||
|
echo "\nStarting TFTP Provisioner...\n";
|
||||||
|
$server = new TFTPProvisioner($url, $config, $logger);
|
||||||
|
if(!$server->loop($error))
|
||||||
|
die("$error\n");
|
||||||
|
?>
|
||||||
|
|
136
tftpserver.php
136
tftpserver.php
@@ -1,136 +0,0 @@
|
|||||||
#!/usr/bin/env php
|
|
||||||
<?php
|
|
||||||
/*
|
|
||||||
* Functional test for TFTPServer
|
|
||||||
*
|
|
||||||
* Copyright (c) 2011 <mattias.wadman@gmail.com>
|
|
||||||
*
|
|
||||||
* MIT License:
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
require_once("lib/tftp.php");
|
|
||||||
|
|
||||||
class TestTFTPServer extends TFTPServer
|
|
||||||
{
|
|
||||||
private $_files = array();
|
|
||||||
private $_debug;
|
|
||||||
|
|
||||||
function __construct($server_url, $logger = NULL, $debug = false)
|
|
||||||
{
|
|
||||||
parent::__construct($server_url, $logger);
|
|
||||||
$this->_debug = $debug;
|
|
||||||
$this->max_put_size = 60000000;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function log($peer, $level, $message)
|
|
||||||
{
|
|
||||||
echo
|
|
||||||
date("H:i:s") . " " .
|
|
||||||
$level . " " .
|
|
||||||
$peer . " " .
|
|
||||||
$message . "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_debug($peer, $message)
|
|
||||||
{
|
|
||||||
if(!$this->_debug)
|
|
||||||
return;
|
|
||||||
|
|
||||||
$this->log($peer, "D", $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_info($peer, $message)
|
|
||||||
{
|
|
||||||
$this->log($peer, "I", $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_warning($peer, $message)
|
|
||||||
{
|
|
||||||
$this->log($peer, "W", $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_error($peer, $message)
|
|
||||||
{
|
|
||||||
$this->log($peer, "E", $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function exists($peer, $filename)
|
|
||||||
{
|
|
||||||
if($filename == "not_writable" || $filename == "not_readable")
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return isset($this->_files[$filename]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function readable($peer, $filename)
|
|
||||||
{
|
|
||||||
if($filename == "not_readable")
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return isset($this->_files[$filename]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($peer, $filename, $mode)
|
|
||||||
{
|
|
||||||
if(isset($this->_files[$filename]))
|
|
||||||
return $this->_files[$filename];
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function writable($peer, $filename)
|
|
||||||
{
|
|
||||||
if($filename == "not_writable")
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function put($peer, $filename, $mode, $content)
|
|
||||||
{
|
|
||||||
$this->_files[$filename] = $content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$host = "127.0.0.1";
|
|
||||||
$port = 1196;
|
|
||||||
$url = "udp://$host:$port";
|
|
||||||
|
|
||||||
if(count($_SERVER["argv"]) > 1) {
|
|
||||||
$server = new TestTFTPServer($url, true);
|
|
||||||
if(!$server->loop($error))
|
|
||||||
die("$error\n");
|
|
||||||
} else {
|
|
||||||
|
|
||||||
$pid = pcntl_fork();
|
|
||||||
$logger = new Logger_Stdout('LOG_DEBUG');
|
|
||||||
if($pid == 0) {
|
|
||||||
$server = new TestTFTPServer($url, $logger);
|
|
||||||
if(!$server->loop($error))
|
|
||||||
die("$error\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
usleep(100000);
|
|
||||||
// kill server
|
|
||||||
posix_kill($pid, SIGINT);
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
Reference in New Issue
Block a user