123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038 |
- <?php
- if (!defined('SMF'))
- die('Hacking attempt...');
- class xmlArray
- {
-
- public $array, $debug_level, $trim;
-
- public function __construct($data, $auto_trim = false, $level = null, $is_clone = false)
- {
-
- @ini_set('memory_limit', '32M');
-
- $this->debug_level = $level !== null ? $level : error_reporting();
- $this->trim = $auto_trim;
-
- if ($is_clone)
- {
- $this->array = $data;
- return;
- }
-
- if (is_array($data))
- $data = implode('', $data);
-
- $data = preg_replace('/<!--.*?-->/s', '', $this->_to_cdata(preg_replace(array('/^<\?xml.+?\?' . '>/is', '/<!DOCTYPE[^>]+?' . '>/s'), '', $data)));
-
- $this->array = $this->_parse($data);
- }
-
- public function name()
- {
- return isset($this->array['name']) ? $this->array['name'] : '';
- }
-
- public function fetch($path, $get_elements = false)
- {
-
- $array = $this->path($path);
- if ($array === false)
- return false;
-
- if ($get_elements && !is_string($array))
- {
- $temp = '';
-
- foreach ($array->array as $val)
- {
-
- if (is_array($val))
- $temp .= $this->_xml($val, null);
- }
-
- return $this->_to_cdata($temp);
- }
-
- return is_string($array) ? $array : $this->_fetch($array->array);
- }
-
- public function path($path, $return_full = false)
- {
-
- $path = explode('/', $path);
-
- $array = $this->array;
-
- foreach ($path as $el)
- {
-
- if (strpos($el, '[') !== false)
- {
- $lvl = (int) substr($el, strpos($el, '[') + 1);
- $el = substr($el, 0, strpos($el, '['));
- }
-
- elseif (substr($el, 0, 1) == '@')
- {
-
- if (isset($array[$el]))
- return $array[$el];
- else
- {
- $trace = debug_backtrace();
- $i = 0;
- while ($i < count($trace) && isset($trace[$i]['class']) && $trace[$i]['class'] == get_class($this))
- $i++;
- $debug = ' from ' . $trace[$i - 1]['file'] . ' on line ' . $trace[$i - 1]['line'];
-
- if ($this->debug_level & E_NOTICE)
- trigger_error('Undefined XML attribute: ' . substr($el, 1) . $debug, E_USER_NOTICE);
- return false;
- }
- }
- else
- $lvl = null;
-
- $array = $this->_path($array, $el, $lvl);
- }
-
- if ($return_full && (!isset($array['name']) || substr($array['name'], -1) != ']'))
- $array = array('name' => $el . '[]', $array);
-
- $newClass = get_class($this);
-
- return $array === false ? false : new $newClass($array, $this->trim, $this->debug_level, true);
- }
-
- public function exists($path)
- {
-
- $path = explode('/', $path);
-
- $array = $this->array;
-
- foreach ($path as $el)
- {
-
- if (strpos($el, '[') !== false)
- {
- $lvl = (int) substr($el, strpos($el, '[') + 1);
- $el = substr($el, 0, strpos($el, '['));
- }
-
- elseif (substr($el, 0, 1) == '@')
- return isset($array[$el]);
- else
- $lvl = null;
-
- $array = $this->_path($array, $el, $lvl, true);
- }
- return $array !== false;
- }
-
- public function count($path)
- {
-
- $temp = $this->path($path, true);
-
- $i = 0;
- foreach ($temp->array as $item)
- {
- if (is_array($item))
- $i++;
- }
- return $i;
- }
-
- public function set($path)
- {
-
- $array = array();
- $xml = $this->path($path, true);
- foreach ($xml->array as $val)
- {
-
- if (!is_array($val) || $val['name'] == '!')
- continue;
-
- $newClass = get_class($this);
-
- $array[] = new $newClass($val, $this->trim, $this->debug_level, true);
- }
- return $array;
- }
-
- public function create_xml($path = null)
- {
-
- if ($path !== null)
- {
- $path = $this->path($path);
-
- if ($path === false)
- return false;
- $path = $path->array;
- }
-
- else
- $path = $this->array;
-
- return '<?xml version="1.0"?' . '>' . $this->_xml($path, 0);
- }
-
- public function to_array($path = null)
- {
-
- if ($path !== null)
- {
- $path = $this->path($path);
-
- if ($path === false)
- return false;
- $path = $path->array;
- }
-
- else
- $path = $this->array;
- return $this->_array($path);
- }
-
- protected function _parse($data)
- {
-
- $current = array(
- );
-
- while ($data != '')
- {
-
- preg_match('/\A<([\w\-:]+)((?:\s+.+?)?)([\s]?\/)?' . '>/', $data, $match);
- if (isset($match[0]))
- $data = preg_replace('/' . preg_quote($match[0], '/') . '/s', '', $data, 1);
-
- if (!isset($match[1]) || $match[1] == '')
- {
-
- if (strpos($data, '<') === false)
- {
- $text_value = $this->_from_cdata($data);
- $data = '';
- if ($text_value != '')
- $current[] = array(
- 'name' => '!',
- 'value' => $text_value
- );
- }
-
- elseif (strpos($data, '<') > 0)
- {
- $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<')));
- $data = substr($data, strpos($data, '<'));
- if ($text_value != '')
- $current[] = array(
- 'name' => '!',
- 'value' => $text_value
- );
- }
-
- elseif (strpos($data, '<') !== false && strpos($data, '<') == 0)
- {
- if (strpos($data, '<', 1) !== false)
- {
- $text_value = $this->_from_cdata(substr($data, 0, strpos($data, '<', 1)));
- $data = substr($data, strpos($data, '<', 1));
- if ($text_value != '')
- $current[] = array(
- 'name' => '!',
- 'value' => $text_value
- );
- }
- else
- {
- $text_value = $this->_from_cdata($data);
- $data = '';
- if ($text_value != '')
- $current[] = array(
- 'name' => '!',
- 'value' => $text_value
- );
- }
- }
-
- continue;
- }
-
- $el = &$current[];
- $el['name'] = $match[1];
-
- if ((!isset($match[3]) || trim($match[3]) != '/') && (!isset($match[2]) || trim($match[2]) != '/'))
- {
-
- $last_tag_end = strpos($data, '</' . $match[1]. '>');
- if ($last_tag_end === false)
- continue;
- $offset = 0;
- while (1 == 1)
- {
-
- $next_tag_start = strpos($data, '<' . $match[1], $offset);
-
- if ($next_tag_start === false || $next_tag_start > $last_tag_end)
- break;
-
- $next_tag_end = strpos($data, '</' . $match[1]. '>', $offset);
-
- if ($next_tag_end === false)
- break;
- else
- {
- $last_tag_end = $next_tag_end;
- $offset = $next_tag_start + 1;
- }
- }
-
- $inner_match = substr($data, 0, $last_tag_end);
-
- $data = substr($data, $last_tag_end + strlen('</' . $match[1]. '>'));
- if (!empty($inner_match))
- {
-
- if (strpos($inner_match, '<') !== false)
- $el += $this->_parse($inner_match);
- elseif (trim($inner_match) != '')
- {
- $text_value = $this->_from_cdata($inner_match);
- if ($text_value != '')
- $el[] = array(
- 'name' => '!',
- 'value' => $text_value
- );
- }
- }
- }
-
- if (isset($match[2]) && $match[2] != '')
- {
-
- preg_match_all('/([\w:]+)="(.+?)"/', $match[2], $attr, PREG_SET_ORDER);
-
- foreach ($attr as $match_attr)
- $el['@' . $match_attr[1]] = $match_attr[2];
- }
- }
-
- return $current;
- }
-
- protected function _xml($array, $indent)
- {
- $indentation = $indent !== null ? '
- ' . str_repeat(' ', $indent) : '';
-
- if (is_array($array) && !isset($array['name']))
- {
- $temp = '';
- foreach ($array as $val)
- $temp .= $this->_xml($val, $indent);
- return $temp;
- }
-
- if ($array['name'] == '!')
- return $indentation . '<![CDATA[' . $array['value'] . ']]>';
- elseif (substr($array['name'], -2) == '[]')
- $array['name'] = substr($array['name'], 0, -2);
-
- $output = $indentation . '<' . $array['name'];
- $inside_elements = false;
- $output_el = '';
-
- foreach ($array as $k => $v)
- {
- if (substr($k, 0, 1) == '@')
- $output .= ' ' . substr($k, 1) . '="' . $v . '"';
- elseif (is_array($v))
- {
- $output_el .= $this->_xml($v, $indent === null ? null : $indent + 1);
- $inside_elements = true;
- }
- }
-
- if ($inside_elements)
- $output .= '>' . $output_el . $indentation . '</' . $array['name'] . '>';
- else
- $output .= ' />';
- return $output;
- }
-
- protected function _array($array)
- {
- $return = array();
- $text = '';
- foreach ($array as $value)
- {
- if (!is_array($value) || !isset($value['name']))
- continue;
- if ($value['name'] == '!')
- $text .= $value['value'];
- else
- $return[$value['name']] = $this->_array($value);
- }
- if (empty($return))
- return $text;
- else
- return $return;
- }
-
- function _to_cdata($data)
- {
- $inCdata = $inComment = false;
- $output = '';
- $parts = preg_split('~(<!\[CDATA\[|\]\]>|<!--|-->)~', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
- foreach ($parts as $part)
- {
-
- if (!$inCdata && $part === '<!--')
- $inComment = true;
- if ($inComment && $part === '-->')
- $inComment = false;
- elseif ($inComment)
- continue;
-
- elseif (!$inComment && $part === '<![CDATA[')
- $inCdata = true;
- elseif ($inCdata && $part === ']]>')
- $inCdata = false;
- elseif ($inCdata)
- $output .= htmlentities($part, ENT_QUOTES);
-
- else
- $output .= $part;
- }
- return $output;
- }
-
- protected function _from_cdata($data)
- {
-
- $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
-
- $data = strtr(preg_replace('~&#(\d{1,4});~e', "chr('\$1')", $data), $trans_tbl);
- return $this->trim ? trim($data) : $data;
- }
-
- protected function _fetch($array)
- {
-
- if (is_string($array))
- return '';
- $temp = '';
- foreach ($array as $text)
- {
-
- if (!isset($text['name']))
- continue;
-
- if ($text['name'] == '!')
- $temp .= $text['value'];
-
- else
- $temp .= $this->_fetch($text);
- }
-
- return $temp;
- }
-
- protected function _path($array, $path, $level, $no_error = false)
- {
-
- if (!is_array($array))
- return false;
-
- if ($path == '' || $path == '.')
- return $array;
- $paths = explode('|', $path);
-
- $show_all = in_array('*', $paths);
- $results = array();
-
- foreach ($array as $value)
- {
- if (!is_array($value) || $value['name'] === '!')
- continue;
- if ($show_all || in_array($value['name'], $paths))
- {
-
- if ($level !== null && $level > 0)
- $level--;
- else
- $results[] = $value;
- }
- }
-
- if (empty($results))
- {
- $trace = debug_backtrace();
- $i = 0;
- while ($i < count($trace) && isset($trace[$i]['class']) && $trace[$i]['class'] == get_class($this))
- $i++;
- $debug = ' from ' . $trace[$i - 1]['file'] . ' on line ' . $trace[$i - 1]['line'];
-
- if ($this->debug_level & E_NOTICE && !$no_error)
- trigger_error('Undefined XML element: ' . $path . $debug, E_USER_NOTICE);
- return false;
- }
-
- elseif (count($results) == 1 || $level !== null)
- return $results[0];
-
- else
- return $results + array('name' => $path . '[]');
- }
- }
- class ftp_connection
- {
- public $connection, $error, $last_message, $pasv;
-
- public function __construct($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = 'ftpclient@simplemachines.org')
- {
-
- $this->connection = 'no_connection';
- $this->error = false;
- $this->pasv = array();
- if ($ftp_server !== null)
- $this->connect($ftp_server, $ftp_port, $ftp_user, $ftp_pass);
- }
- public function connect($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = 'ftpclient@simplemachines.org')
- {
- if (strpos($ftp_server, 'ftp://') === 0)
- $ftp_server = substr($ftp_server, 6);
- elseif (strpos($ftp_server, 'ftps://') === 0)
- $ftp_server = 'ssl://' . substr($ftp_server, 7);
- if (strpos($ftp_server, 'http://') === 0)
- $ftp_server = substr($ftp_server, 7);
- $ftp_server = strtr($ftp_server, array('/' => '', ':' => '', '@' => ''));
-
- $this->connection = @fsockopen($ftp_server, $ftp_port, $err, $err, 5);
- if (!$this->connection)
- {
- $this->error = 'bad_server';
- return;
- }
-
- if (!$this->check_response(220))
- {
- $this->error = 'bad_response';
- return;
- }
-
- fwrite($this->connection, 'USER ' . $ftp_user . "\r\n");
- if (!$this->check_response(331))
- {
- $this->error = 'bad_username';
- return;
- }
-
- fwrite($this->connection, 'PASS ' . $ftp_pass . "\r\n");
- if (!$this->check_response(230))
- {
- $this->error = 'bad_password';
- return;
- }
- }
- public function chdir($ftp_path)
- {
- if (!is_resource($this->connection))
- return false;
-
- if ($ftp_path !== '/' && substr($ftp_path, -1) === '/')
- $ftp_path = substr($ftp_path, 0, -1);
- fwrite($this->connection, 'CWD ' . $ftp_path . "\r\n");
- if (!$this->check_response(250))
- {
- $this->error = 'bad_path';
- return false;
- }
- return true;
- }
- public function chmod($ftp_file, $chmod)
- {
- if (!is_resource($this->connection))
- return false;
- if ($ftp_file == '')
- $ftp_file = '.';
-
- fwrite($this->connection, 'SITE CHMOD ' . decoct($chmod) . ' ' . $ftp_file . "\r\n");
- if (!$this->check_response(200))
- {
- $this->error = 'bad_file';
- return false;
- }
- return true;
- }
- public function unlink($ftp_file)
- {
-
- if (!is_resource($this->connection))
- return false;
-
- fwrite($this->connection, 'DELE ' . $ftp_file . "\r\n");
- if (!$this->check_response(250))
- {
- fwrite($this->connection, 'RMD ' . $ftp_file . "\r\n");
-
- if (!$this->check_response(250))
- {
- $this->error = 'bad_file';
- return false;
- }
- }
- return true;
- }
- public function check_response($desired)
- {
-
- $time = time();
- do
- $this->last_message = fgets($this->connection, 1024);
- while ((strlen($this->last_message) < 4 || strpos($this->last_message, ' ') === 0 || strpos($this->last_message, ' ', 3) !== 3) && time() - $time < 5);
-
- return is_array($desired) ? in_array(substr($this->last_message, 0, 3), $desired) : substr($this->last_message, 0, 3) == $desired;
- }
- public function passive()
- {
-
- if (!is_resource($this->connection))
- return false;
-
- @fwrite($this->connection, 'PASV' . "\r\n");
- $time = time();
- do
- $response = fgets($this->connection, 1024);
- while (strpos($response, ' ', 3) !== 3 && time() - $time < 5);
-
- if (strpos($response, '227 ') !== 0)
- {
- $this->error = 'bad_response';
- return false;
- }
-
- if (preg_match('~\((\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))\)~', $response, $match) == 0)
- {
- $this->error = 'bad_response';
- return false;
- }
-
- $this->pasv = array('ip' => $match[1] . '.' . $match[2] . '.' . $match[3] . '.' . $match[4], 'port' => $match[5] * 256 + $match[6]);
- return true;
- }
- public function create_file($ftp_file)
- {
-
- if (!is_resource($this->connection))
- return false;
-
- if (!$this->passive())
- return false;
-
- fwrite($this->connection, 'STOR ' . $ftp_file . "\r\n");
-
- $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
- if (!$fp || !$this->check_response(150))
- {
- $this->error = 'bad_file';
- @fclose($fp);
- return false;
- }
-
- fclose($fp);
- if (!$this->check_response(226))
- {
- $this->error = 'bad_response';
- return false;
- }
- return true;
- }
- public function list_dir($ftp_path = '', $search = false)
- {
-
- if (!is_resource($this->connection))
- return false;
-
- if (!$this->passive())
- return false;
-
- fwrite($this->connection, 'LIST -1' . ($search ? 'R' : '') . ($ftp_path == '' ? '' : ' ' . $ftp_path) . "\r\n");
-
- $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
- if (!$fp || !$this->check_response(array(150, 125)))
- {
- $this->error = 'bad_response';
- @fclose($fp);
- return false;
- }
-
- $data = '';
- while (!feof($fp))
- $data .= fread($fp, 4096);
- fclose($fp);
-
- if (!$this->check_response(226))
- {
- $this->error = 'bad_response';
- return false;
- }
- return $data;
- }
- public function locate($file, $listing = null)
- {
- if ($listing === null)
- $listing = $this->list_dir('', true);
- $listing = explode("\n", $listing);
- @fwrite($this->connection, 'PWD' . "\r\n");
- $time = time();
- do
- $response = fgets($this->connection, 1024);
- while ($response[3] != ' ' && time() - $time < 5);
-
- if (preg_match('~^257 "(.+?)" ~', $response, $match) != 0)
- $current_dir = strtr($match[1], array('""' => '"'));
- else
- $current_dir = '';
- for ($i = 0, $n = count($listing); $i < $n; $i++)
- {
- if (trim($listing[$i]) == '' && isset($listing[$i + 1]))
- {
- $current_dir = substr(trim($listing[++$i]), 0, -1);
- $i++;
- }
-
- $listing[$i] = $current_dir . '/' . trim(strlen($listing[$i]) > 30 ? strrchr($listing[$i], ' ') : $listing[$i]);
- if ($file[0] == '*' && substr($listing[$i], -(strlen($file) - 1)) == substr($file, 1))
- return $listing[$i];
- if (substr($file, -1) == '*' && substr($listing[$i], 0, strlen($file) - 1) == substr($file, 0, -1))
- return $listing[$i];
- if (basename($listing[$i]) == $file || $listing[$i] == $file)
- return $listing[$i];
- }
- return false;
- }
- public function create_dir($ftp_dir)
- {
-
- if (!is_resource($this->connection))
- return false;
-
- fwrite($this->connection, 'MKD ' . $ftp_dir . "\r\n");
- if (!$this->check_response(257))
- {
- $this->error = 'bad_file';
- return false;
- }
- return true;
- }
- public function detect_path($filesystem_path, $lookup_file = null)
- {
- $username = '';
- if (isset($_SERVER['DOCUMENT_ROOT']))
- {
- if (preg_match('~^/home[2]?/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
- {
- $username = $match[1];
- $path = strtr($_SERVER['DOCUMENT_ROOT'], array('/home/' . $match[1] . '/' => '', '/home2/' . $match[1] . '/' => ''));
- if (substr($path, -1) == '/')
- $path = substr($path, 0, -1);
- if (strlen(dirname($_SERVER['PHP_SELF'])) > 1)
- $path .= dirname($_SERVER['PHP_SELF']);
- }
- elseif (strpos($filesystem_path, '/var/www/') === 0)
- $path = substr($filesystem_path, 8);
- else
- $path = strtr(strtr($filesystem_path, array('\\' => '/')), array($_SERVER['DOCUMENT_ROOT'] => ''));
- }
- else
- $path = '';
- if (is_resource($this->connection) && $this->list_dir($path) == '')
- {
- $data = $this->list_dir('', true);
- if ($lookup_file === null)
- $lookup_file = $_SERVER['PHP_SELF'];
- $found_path = dirname($this->locate('*' . basename(dirname($lookup_file)) . '/' . basename($lookup_file), $data));
- if ($found_path == false)
- $found_path = dirname($this->locate(basename($lookup_file)));
- if ($found_path != false)
- $path = $found_path;
- }
- elseif (is_resource($this->connection))
- $found_path = true;
- return array($username, $path, isset($found_path));
- }
- public function close()
- {
-
- fwrite($this->connection, 'QUIT' . "\r\n");
- fclose($this->connection);
- return true;
- }
- }
- ?>
|