response) { $this->response = new LECM_Connector_Response(); } return $this->response; } function getAdapter() { if (!$this->adapter) { $this->adapter = new LECM_Connector_Adapter(); } return $this->adapter; } function getAction() { if (!$this->action) { $this->action = new LECM_Connector_Action(); } return $this->action; } function run() { if (empty($_GET)) { echo $this->getConnectorInstalledMessage(); if (self::$dev_mode) { $this->devModeCheck(); } return; } if (!$this->checkToken()) { $response = $this->getResponse(); $response->token('Token is false !', null); return; } $action = $this->getAction(); $action->setConnector($this); $action->run(); return; } function checkToken() { if (isset($_GET['token']) && $_GET['token'] == LECM_TOKEN) { return true; } else { return false; } } public static function log($msg, $log_type = 'exception') { if(!self::$dev_mode){ return false; } $log_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . $log_type . '.log'; if (is_array($msg)) { $msg = serialize($msg); } $msg .= "\r\n"; $date_time = date('Y-m-d H:i:s'); @file_put_contents($log_file, $date_time . ' : ' . $msg, FILE_APPEND); } function devModeCheck() { echo "
"; echo "Developer mode is enabled. Status check:
"; $connector_folder = dirname(__FILE__); echo "Connector folder is writable ..."; if (is_writable($connector_folder)) { echo " ok"; } else { echo "failed"; } echo "
"; } private function getConnectorInstalledMessage() { return ' Connector is successfully Installed!
Connector is successfully installed!
'; } } class LECM_Connector_Response { function createResponse($result, $msg, $obj, $error = null) { $response = array(); $response['result'] = $result; $response['msg'] = $msg; $response['data'] = $obj; $response['error'] = $error; $res = json_encode($response); if(!isset($_GET['encode']) || $_GET['encode'] != 'no'){ $res = base64_encode(gzdeflate($res)); } echo $res; return; } function error($msg = null, $obj = null, $error = null) { $this->createResponse('error', $msg, $obj); } function token($msg = null, $obj = null, $error = null) { $this->createResponse('token', $msg, $obj); } function success($msg = null, $obj = null, $error = null) { $this->createResponse('success', $msg, $obj, $error); } } class LECM_Connector_Action { var $type = null; var $connector = null; function setConnector($connector) { $this->connector = $connector; } function run() { if (isset($_GET['action']) && $action = $this->getActionType($_GET['action'])) { $action->setConnector($this->connector); $action->run(); } else { $response = $this->connector->getResponse(); $response->createResponse('error', 'Action not found!', null); return; } } function getActionType($action_type) { $action = null; $action_type = strtolower($action_type); $class_name = __CLASS__ . '_' . ucfirst($action_type); if (class_exists($class_name)) { $action = new $class_name(); } return $action; } function getResponse() { return $this->connector->getResponse(); } function getAdapter() { return $this->connector->getAdapter(); } function getCart($check = false) { $adapter = $this->getAdapter(); $cart = $adapter->getCart($check); return $cart; } function getParams($key, $params, $default = null) { return isset($params[$key]) ? $params[$key] : $default; } function getRealPath($path) { $path = ltrim($path, '/'); $full_path = LECM_STORE_BASE_DIR . $path; return $full_path; } function createParentDir($path, $mode = 0777) { $result = true; if (!is_dir(dirname($path))) { $result = @mkdir(dirname($path), 0777, true); } return $result; } function createFileSuffix($file_path, $suffix, $character = '_') { $new_path = ''; $dir_name = pathinfo($file_path, PATHINFO_DIRNAME); $file_name = pathinfo($file_path, PATHINFO_FILENAME); $file_ext = pathinfo($file_path, PATHINFO_EXTENSION); if ($dir_name && $dir_name != '.') $new_path .= $dir_name . '/'; $new_path .= $file_name . $character . $suffix . '.' . $file_ext; return $new_path; } function request_decode($request){ return @gzinflate(base64_decode($request)); } } class LECM_Connector_Action_Check_Image extends LECM_Connector_Action{ var $_file; function run() { $url = $this->getParams('url', $_REQUEST); $save_path = $this->getParams('save_path', $_REQUEST); $rename = $this->getParams('rename', $_REQUEST, False); $file = $this->getActionFile(); $result = $file->download($save_path, array('url' => $url, 'rename'=> $rename)); var_dump($result);exit; return; } function getActionFile() { if (!$this->_file) { $this->_file = new LECM_Connector_Action_File(); $this->_file->setType('image'); } return $this->_file; } } class LECM_Connector_Action_Check extends LECM_Connector_Action { function run() { $response = $this->getResponse(); $adapter = $this->getAdapter(); $cart = $this->getCart(true); // $cart_type = $_REQUEST['cart_type']; // $list_cart_type = $adapter->detectCartType(); if ($cart) { $obj['cms'] = $adapter->getType(); $obj['image'] = $cart->imageDir; $obj['image_category'] = $cart->imageDirCategory; $obj['image_product'] = $cart->imageDirProduct; $obj['image_manufacturer'] = $cart->imageDirManufacturer; $obj['table_prefix'] = $cart->tablePrefix; $obj['version'] = $cart->version; $obj['charset'] = $cart->charset; $obj['cookie_key'] = $cart->cookie_key; $obj['extend'] = $cart->extend; $obj['connector_version'] = CONNECTOR_VERSION; $obj['download_image'] = function_exists('curl_init') || @ini_get('allow_url_fopen'); if(isset($cart->view)){ $obj['view'] = $cart->view; } if(isset($cart->site_id) && $cart->site_id){ $obj['site_id'] = $cart->site_id; } $dbConnect = LECM_Db::getInstance($cart); if ($dbConnect->getError()) { $obj['connect'] = array( 'result' => 'error', 'msg' => 'Not connect to database! Error: ' . $dbConnect->getError() ); } else { $obj['connect'] = array( 'result' => 'success', 'msg' => 'Successful connect to database!' ); } } $response->success('Successful check CMS!', $obj); return; } } class LECM_Connector_Action_Phpinfo extends LECM_Connector_Action { function run() { phpinfo(); return; } } class LECM_Connector_Action_Opcache extends LECM_Connector_Action { function run() { opcache_reset(); return; } } class LECM_Connector_Action_Directory extends LECM_Connector_Action { function run() { $data = array(); $response = $this->getResponse(); if (isset($_REQUEST['folders'])) { $folders = json_decode($this->request_decode($_REQUEST['folders']), true); foreach ($folders as $key => $folder) { $params = isset($folder['params']) ? $folder['params'] : array(); $data[$key] = $this->getDir($folder['type'], $folder['folder'], $params); } } return $response->success(null, $data); } function getDir($type, $folder, $params = array()) { $result = false; switch ($type) { case 'writable'; $result = $this->writable($folder, $params); break; case 'exists'; $result = $this->exists($folder, $params); break; case 'dir'; $result = $this->dir($folder, $params); break; case 'tree'; $result = $this->tree($folder, $params); break; case 'delete'; $result = $this->delete($folder, $params); break; case 'create'; $result = $this->create($folder, $params); break; default: break; } return $result; } function writable($folder, $params = array()) { $result = false; if (!$this->exists($folder)) { return $result; } $path = $this->getRealPath($folder); $result = is_writable($path); return $result; } function exists($folder, $params = array()) { $path = $this->getRealPath($folder); return is_dir($path); } function dir($folder, $params = array()) { $result = array(); if (!$this->exists($folder)) { return $result; } $path = $this->getRealPath($folder); $result = $this->readDir($path, false); return $result; } function tree($folder, $params = array()) { $result = array(); if (!$this->exists($folder)) { return $result; } $path = $this->getRealPath($folder); $result = $this->readDir($path, true); return $result; } function delete($folder, $params = array()) { $result = false; if (!$this->exists($folder)) { return true; } if (!$this->writable($folder)) { return $result; } $path = $this->getRealPath($folder); $self = $this->getParams('self', $params); $result = $this->deleteDir($path, $self); return $result; } function create($folder, $params = array()) { $result = true; if ($this->exists($folder)) { return $result; } $path = $this->getRealPath($folder); $result = @mkdir(dirname($path), 0777, true); return $result; } function deleteDir($path, $self = true) { $path = rtrim($path, '/\\'); $items = glob($path . '/*', GLOB_MARK); foreach ($items as $item) { if (is_dir($item)) { $this->deleteDir($item, true); } else { @unlink($item); } } if ($self) { @rmdir($path); } return true; } function readDir($path, $content = false) { $result = array(); $path = rtrim($path, '/\\'); $items = glob($path . '/*', GLOB_MARK); foreach ($items as $item) { if (is_dir($item)) { $folder_data = array( 'type' => 'folder', 'path' => basename($item), ); if ($content) { $folder_data['content'] = $this->readDir($item, true); } $result[] = $folder_data; } else { $result[] = array( 'type' => 'file', 'path' => basename($item) ); } } return $result; } } class LECM_Connector_Action_File extends LECM_Connector_Action { var $type = 'file'; const IMAGE_SUFFIX = 'nd'; function run() { $data = array(); $response = $this->getResponse(); if (isset($_REQUEST['files'])) { $files = json_decode($this->request_decode($_REQUEST['files']), true); foreach ($files as $key => $file) { $params = isset($file['params']) ? $file['params'] : array(); $data[$key] = $this->processFile($file['type'], $file['path'], $params); } } return $response->success(null, $data); } function setType($type){ $this->type = $type; } function getSize($file){ if($this->type == 'image'){ return @getimagesize($file); } return @filesize($file); } function processFile($type, $path, $params = array()) { $result = false; switch ($type) { case 'download': $result = $this->download($path, $params); break; case 'exists': $result = $this->exists($path, $params); break; case 'rename': $result = $this->rename($path, $params); break; case 'delete': $result = $this->delete($path, $params); break; case 'content': $result = $this->content($path, $params); break; case 'copy': $result = $this->copy($path, $params); break; case 'move': $result = $this->move($path, $params); break; default: break; } return $result; } function download($path, $params = array(), $time = 1) { $result = false; if (!$time) { return $result; } $override = $this->getParams('override', $params); $rename = $this->getParams('rename', $params); $url = $this->getParams('url', $params); $http_auth = $this->getParams('http_auth', $params); if (!$url) { return $result; } if ($this->exists($path)) { $full_path = $this->getRealPath($path); if(filesize($full_path)){ // if($time < 1){ // return $path; // } if ($rename) { $path = $this->rename($path, '2nd'); } else { if (!$override) { $path = ltrim($path, '/'); return $path; } $delete_file = $this->delete($path); if (!$delete_file) { return $result; } } }else{ $this->delete($path); } } $full_path = $this->getRealPath($path); $this->createParentDir($full_path); $fp = @fopen($full_path, 'wb'); if (!$fp) { return '[LECM_ERROR]' . "Can't open file ". $full_path; } if(function_exists('curl_init')){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/apng,*/*;q=0.8', 'accept-encoding: deflate', 'accept-language: en-US,en;q=0.8,uk;q=0.6', 'user-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201' )); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 20); if($http_auth){ $user = $this->getParams('user', $http_auth, ''); $pass = $this->getParams('pass', $http_auth, ''); curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass"); } curl_setopt($ch, CURLOPT_FILE, $fp); try{ $data = curl_exec($ch); }catch (Exception $e){ if($time <= 1){ return '[LECM_ERROR]' . $e->getMessage(); } $data = false; } if (curl_errno($ch) && $time <= 1) { return '[LECM_ERROR]' . curl_error($ch); } curl_close($ch); }elseif(@ini_get('allow_url_fopen')){ $context = null; if($http_auth){ $user = $this->getParams('user', $http_auth, ''); $pass = $this->getParams('pass', $http_auth, ''); $context = stream_context_create(array( 'http' => array( 'header' => "Authorization: Basic " . base64_encode("$user:$pass") ) )); } $content = @file_get_contents($url); if(!$content){ return '[LECM_ERROR]' . 'url not found or file get content error'; } $result = file_put_contents($full_path, $content); }else{ return '[LECM_ERROR]' . 'curl and file get contents error'; } @flush($fp); @fclose($fp); if ($this->getSize($full_path)) { $result = $path; }else{ $this->delete($path); } if (!$result) { $time--; $result = $this->download($path, $params, $time); } return $result; } function exists($path, $params = array()) { $full_path = $this->getRealPath($path); return file_exists($full_path); } function rename($path, $params = array()) { $path = ltrim($path, '/'); $new_path = $path; $full_path = $this->getRealPath($new_path); $dir_name = pathinfo($path, PATHINFO_DIRNAME); $file_name = pathinfo($path, PATHINFO_FILENAME); $file_ext = pathinfo($path, PATHINFO_EXTENSION); $i = 2; if(substr($file_name, -2) == self::IMAGE_SUFFIX){ $file_name_exp = explode('_', $file_name); if(is_numeric(str_replace(self::IMAGE_SUFFIX, '', $file_name_exp[count($file_name_exp) - 1]))){ $i = str_replace(self::IMAGE_SUFFIX, '', $file_name_exp[count($file_name_exp) - 1]) + 1; if(count($file_name_exp) > 1){ unset($file_name_exp[count($file_name_exp)-1]); } $file_name = implode('_', $file_name_exp); $new_path = ''; if ($dir_name && $dir_name != '.'){ $new_path .= $dir_name . '/'; } $new_path .= $file_name . '.' . $file_ext; $path = $new_path; } } while (file_exists($full_path)) { $new_path = $this->createFileSuffix($path, $i.self::IMAGE_SUFFIX); $full_path = $this->getRealPath($new_path); $i++; } return $new_path; } function delete($path, $params = array()) { $result = true; if (!$this->exists($path)) { return $result; } $full_path = $this->getRealPath($path); $result = @unlink($full_path); return $result; } function content($path, $params = array()) { $result = ''; $full_path = $this->getRealPath($path); if (!$this->exists($path)) { return $result; } $result = @file_get_contents($full_path); return $result; } function copy($path, $params = array()) { $result = false; $override = $this->getParams('override', $params); $copy_path = $this->getParams('copy', $params); if (!$copy_path) { return $result; } if (!$this->exists($path)) { return $result; } if ($this->exists($copy_path)) { if (!$override) { return $result; } $delete_file = $this->delete($copy_path); if (!$delete_file) { return $result; } } $full_path = $this->getRealPath($path); $full_copy_path = $this->getRealPath($copy_path); $this->createParentDir($full_copy_path); $result = @copy($full_path, $full_copy_path); return $result; } function move($path, $params = array()) { $result = false; $override = $this->getParams('override', $params); $move_path = $this->getParams('move', $params); $rename= $this->getParams('rename', $params); $url = $this->getParams('url', $params); if (!$move_path) { return $result; } if ($this->exists($move_path)) { if (!$override) { if($rename){ $move_path = $this->rename($move_path); }else{ return $move_path; } }else{ $delete_file = $this->delete($move_path); if (!$delete_file) { return $result; } } } if (!$this->exists($path) ) { if(!$url){ return $result; } $download_params = array( 'url' => $params['url'], 'override' => false, 'rename' => false, ); return $this->download($move_path, $download_params); } $full_path = $this->getRealPath($path); $full_move_path = $this->getRealPath($move_path); $this->createParentDir($full_move_path); $result = rename($full_path, $full_move_path); if($result){ return $move_path; }else{ return '[LECM_ERROR]' . ' Folder Image does not writable'; } } } class LECM_Connector_Action_Image extends LECM_Connector_Action { var $_file = null; function run() { $data = array(); $response = $this->getResponse(); $error = ''; if (isset($_REQUEST['images'])) { $images = json_decode($this->request_decode($_REQUEST['images']), true); foreach ($images as $key => $image) { $params = isset($image['params']) ? $image['params'] : array(); $result = $this->processImage($image['type'], $image['path'], $params); if(strpos($result, '[LECM_ERROR]') !== false){ $error = str_replace('[LECM_ERROR]', '', $result); $result = false; } $data[$key] = $result; } } return $response->success(null, $data, $error); } function processImage($type, $path, $params = array()) { $result = false; switch ($type) { case 'download': $result = $this->download($path, $params); break; case 'exists': $result = $this->exists($path, $params); break; case 'rename': $result = $this->rename($path, $params); break; case 'delete': $result = $this->delete($path, $params); break; case 'copy': $result = $this->copy($path, $params); break; case 'move': $result = $this->move($path, $params); break; case 'resize': $result = $this->resize($path, $params); break; default: break; } return $result; } function download($path, $params = array()) { $file = $this->getActionFile(); $result = $file->download($path, $params); return $result; } function exists($path, $params = array()) { $file = $this->getActionFile(); $result = $file->exists($path, $params); return $result; } function rename($path, $params = array()) { $file = $this->getActionFile(); $result = $file->rename($path, $params); return $result; } function delete($path, $params = array()) { $file = $this->getActionFile(); $result = $file->delete($path, $params); return $result; } function copy($path, $params = array()) { $file = $this->getActionFile(); $result = $file->copy($path, $params); return $result; } function move($path, $params = array()) { $file = $this->getActionFile(); $result = $file->move($path, $params); return $result; } function resize($path, $params = array()) { $desc_path = $this->getParams('desc', $params); $width = $this->getParams('width', $params); $height = $this->getParams('height', $params); $crop = $this->getParams('crop', $params); $proportional = $this->getParams('proportional', $params); $quality = $this->getParams('quality', $params); if (!$quality) { $quality = 100; } $result = $this->resizeImage($path, $desc_path, $width, $height, $crop, $proportional, $quality); return $result; } function getActionFile() { if (!$this->_file) { $this->_file = new LECM_Connector_Action_File(); $this->_file->setType('image'); } return $this->_file; } function resizeImage($src_path, $desc_path, $width = 0, $height = 0, $crop = false, $proportional = false, $quality = 100) { $result = false; if ($height <= 0 && $width <= 0) { return $result; } if ($this->exists($desc_path)) { $delete = $this->delete($desc_path); if (!$delete) { return $result; } } $src_image = $this->getRealPath($src_path); if (!$this->exists($src_path)) { return $result; } $desc_image = $this->getRealPath($desc_path); $imageInfo = getimagesize($src_image); list($src_width, $src_height) = $imageInfo; $cropHeight = $cropWidth = 0; if ($proportional) { if ($width == 0) { $factor = $height / $src_height; } elseif ($height == 0) { $factor = $width / $src_width; } else { $factor = min($width / $src_width, $height / $src_height); } $final_width = round($src_width * $factor); $final_height = round($src_height * $factor); } else { $final_width = ($width <= 0) ? $src_width : $width; $final_height = ($height <= 0) ? $src_height : $height; if ($crop) { $widthX = $src_width / $width; $heightX = $src_height / $height; $x = min($widthX, $heightX); $cropWidth = ($src_width - $width * $x) / 2; $cropHeight = ($src_height - $height * $x) / 2; } } switch ($imageInfo[2]) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($src_image); break; case IMAGETYPE_GIF: $image = imagecreatefromgif($src_image); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($src_image); break; default: return false; } $image_resize = imagecreatetruecolor($final_width, $final_height); if (($imageInfo[2] == IMAGETYPE_GIF) || ($imageInfo[2] == IMAGETYPE_PNG)) { $transparency = imagecolortransparent($image); $pallet_size = imagecolorstotal($image); if ($transparency >= 0 && $transparency < $pallet_size) { $transparent_color = imagecolorsforindex($image, $transparency); $transparency = imagecolorallocate($image_resize, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($image_resize, 0, 0, $transparency); imagecolortransparent($image_resize, $transparency); } elseif ($imageInfo[2] == IMAGETYPE_PNG) { imagealphablending($image_resize, false); $color = imagecolorallocatealpha($image_resize, 0, 0, 0, 127); imagefill($image_resize, 0, 0, $color); imagesavealpha($image_resize, true); } } imagecopyresampled($image_resize, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $src_width - 2 * $cropWidth, $src_height - 2 * $cropHeight); $this->createParentDir($desc_image); switch ($imageInfo[2]) { case IMAGETYPE_GIF: $result = imagegif($image_resize, $desc_image); break; case IMAGETYPE_JPEG: $result = imagejpeg($image_resize, $desc_image, $quality); break; case IMAGETYPE_PNG: $quality = 9 - (int)((0.9 * $quality) / 10.0); $result = imagepng($image_resize, $desc_image, $quality); break; default: return false; } return $result; } } class LECM_Connector_Action_Path extends LECM_Connector_Action{ function run() { $path = dirname(dirname(__FILE__)); $path = str_replace('le_connector', '', $path); echo $path; return; } } class LECM_Connector_Action_Adminer extends LECM_Connector_Action{ const URL_DOWNLOAD = 'https://github.com/vrana/adminer/releases/download/v4.7.5/adminer-4.7.5.php'; var $_file = null; function run() { $file_name = md5('litextension' . LECM_TOKEN) . '.php'; $path = $this->getPathFileAdminer() . 'le_connector'. DIRECTORY_SEPARATOR .$file_name; $full_path = $this->getRealPath($path); if(file_exists($full_path)){ return $this->login(); } $file = $this->getActionFile(); $params = array( 'url' => self::URL_DOWNLOAD, 'override' => true, 'rename' => false ); $result = $file->download($path, $params); if($result && strpos($result, '[LECM_ERROR]') === false){ return $this->login(); } echo $result; } function getPathFileAdminer(){ $special_uri = array('litextension-data-migration-to-woocommerce'); $cart_uri = ''; foreach ($special_uri as $uri){ if(stripos($_SERVER['REQUEST_URI'], $uri) !== false){ $cart_uri = $uri; break; } } $path = ''; if(MODULE_CONNECTOR){ switch ($cart_uri){ case 'litextension-data-migration-to-woocommerce': $path = 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $cart_uri . DIRECTORY_SEPARATOR; break; default: break; } } return $path; } function getActionFile() { if (!$this->_file) { $this->_file = new LECM_Connector_Action_File(); } return $this->_file; } function login(){ $file_name = md5('litextension' . LECM_TOKEN) . '.php'; $cart = $this->getCart(); echo '
'; } } class LECM_Connector_Action_Remove_Adminer extends LECM_Connector_Action_Adminer { var $_file = null; function run() { $file_name = md5('litextension' . LECM_TOKEN) . '.php'; $path = $this->getPathFileAdminer() . 'le_connector'. DIRECTORY_SEPARATOR .$file_name; $full_path = $this->getRealPath($path); $res = 'success'; if(file_exists($full_path)){ if(!@unlink($full_path)){ $res = 'fail'; } } echo $res; } function getPathFileAdminer(){ $special_uri = array('litextension-data-migration-to-woocommerce'); $cart_uri = ''; foreach ($special_uri as $uri){ if(stripos($_SERVER['REQUEST_URI'], $uri) !== false){ $cart_uri = $uri; break; } } $path = ''; if(MODULE_CONNECTOR){ switch ($cart_uri){ case 'litextension-data-migration-to-woocommerce': $path = 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $cart_uri . DIRECTORY_SEPARATOR; break; default: break; } } return $path; } function getActionFile() { if (!$this->_file) { $this->_file = new LECM_Connector_Action_File(); } return $this->_file; } function login(){ $file_name = md5('litextension' . LECM_TOKEN) . '.php'; $cart = $this->getCart(); echo '
'; } } class LECM_Connector_Action_Execute extends LECM_Connector_Action { function run(){ $query = isset($_REQUEST['query'])?$_REQUEST['query']:''; $res = ''; $type = ''; $error = ''; if($query){ $cart = $this->getCart(true); $dbConnect = LECM_Db::getInstance($cart); $query = strtolower($query); $type = explode(' ', $query); $type = $type[0]; if($type == 'select' && strpos($query, 'limit') === false){ $query = trim($query, ';,') . ' limit 50'; } if(!in_array($type, array('select', 'delete', 'insert', 'update'))){ $type = 'select'; } $processQuery = $dbConnect->processQuery($type, $query); if($processQuery['result']){ $res = $processQuery['data']; }else{ $res = $processQuery['msg']; } $error = $dbConnect->getError(); } $this->render($type, $query, $res,$error); } function render($type, $query, $res, $error = null){ echo '
'; if($error){ echo ''.$error.''; return; } if(!$res){ return; } if($type != 'select' ){ echo 'success'; }else{ $keys = array_keys($res[0]); echo ' '; foreach ($keys as $key){ echo ''; } echo ' '; foreach ($res as $row){ echo '
';
                foreach ($keys as $key){
                    echo '
'; } echo ''; } echo '
' . $key . '
' . $row[$key] . '
'; } } } class LECM_Connector_Action_Query extends LECM_Connector_Action { function run() { $obj = array(); $error = array(); $response = $this->getResponse(); $cart = $this->getCart(); if ($cart) { $dbConnect = LECM_Db::getInstance($cart); if (isset($_REQUEST['query']) && !$dbConnect->getError()) { $queries = @json_decode($this->request_decode($_REQUEST['query']), true); if($queries !== false){ $dbConnect->processQuery('query', "SET SESSION SQL_MODE = ''"); } if (isset($_REQUEST['serialize']) && $_REQUEST['serialize'] && $queries !== false) { foreach ($queries as $key => $query) { if (is_array($query) && isset($query['type'])) { $params = isset($query['params']) ? $query['params'] : null; $processQuery = $dbConnect->processQuery($query['type'], $query['query'], $params); $obj[$key] = $processQuery['data']; $error[$key] = ''; if($processQuery['msg']){ $error[$key] = $processQuery['msg']; } } else { $processQuery = $dbConnect->processQuery('select', $query); $obj[$key] = $processQuery['data']; $error[$key] = ''; if($processQuery['msg']){ $error[$key] = $processQuery['msg']; } } } } elseif ($queries !== false) { $query = $queries; $params = isset($query['params']) ? $query['params'] : null; $processQuery = $dbConnect->processQuery($query['type'], $query['query'], $params); $obj = $processQuery['data']; $error = ''; if($processQuery['msg']){ $error = $processQuery['msg']; } } else { $query = $this->request_decode($_REQUEST['query']); $processQuery = $dbConnect->processQuery('select', $query); $obj = $processQuery['data']; $error = ''; if($processQuery['msg']){ $error = $processQuery['msg']; } } $response->success(null, $obj, $error); return; } else { $response->error('Can\'t connect to database or not run query! Error: ' . $dbConnect->getError(), null); return; } } else { $response->error('CMS Cart not found!', null); return; } } } class LECM_Connector_Action_Clearcache extends LECM_Connector_Action { function run(){ $response = $this->getResponse(); $cart = $this->getCart(); if($cart){ $clear = $cart->clearCache(); if($clear['result'] != 'success'){ $response->error($clear['msg']); }else{ $response->success(); } }else{ $response->error('Not detect cart'); } return; } } abstract class LECM_Db { static $instance = null; static $servers = array(); var $server = 'localhost'; var $user = 'root'; var $password = ''; var $database = ''; var $charset = 'utf8'; var $link = null; var $response = null; var $error = null; abstract function connect(); abstract function query($query); abstract function select($query); abstract function disconnect(); abstract function getLastInsertId(); function insert($sql, $params = null) { $result = $this->query($sql); if ($result['data'] && isset($params['insert_id'])) { $result['data'] = $this->getLastInsertId(); } return $result; } function defaultResponse(){ return array( 'result' => true, 'msg' => '', 'data' => '' ); } function __construct($server, $user, $password, $database, $charset, $connect = true) { $this->server = $server; $this->user = $user; $this->password = $password; $this->database = $database; if($charset){ $this->charset = $charset; } $this->response = new LECM_Connector_Response(); if ($connect) { $this->connect(); } } function __destruct() { if ($this->link) { $this->disconnect(); } } static function getInstance($cart) { if (!self::$instance) { $class = LECM_Db::getClass(); self::$servers = array('server' => $cart->host, 'user' => $cart->username, 'password' => $cart->password, 'database' => $cart->database, 'charset' => @$cart->charset); self::$instance = new $class( self::$servers['server'], self::$servers['user'], self::$servers['password'], self::$servers['database'], self::$servers['charset'] ); } return self::$instance; } static function getClass() { if (function_exists('mysql_connect')) { $class = 'LECM_MySQL'; } elseif (class_exists('PDO') && extension_loaded('pdo_mysql')) { $class = 'LECM_Pdo'; } else { $class = 'LECM_MySQLi'; } return $class; } function getLink() { return $this->link; } function getError() { return $this->error; } function processQuery($type, $query, $params = null) { $result = null; switch (strtolower($type)) { case 'select': $result = $this->select($query); break; case 'insert': $result = $this->insert($query, $params); break; case 'query': $result = $this->query($query); break; default: $result = $this->query($query); break; } return $result; } } class LECM_MySQL extends LECM_Db { function connect() { if (!$this->link = @mysql_connect($this->server, $this->user, $this->password)) { $this->error = 'Link to database cannot be established.'; return; } if (!mysql_select_db($this->database, $this->link)) { $this->error = 'The database selection cannot be made.'; return; } if ($this->charset) { mysql_set_charset($this->charset, $this->link); } /* if (!mysql_query('SET NAMES \'utf8\'', $this->link)) { $this->error = 'No utf-8 support. Please check your server configuration.'; return; }*/ return $this->link; } function disconnect() { mysql_close($this->link); } function query($sql) { $response = $this->defaultResponse(); $res = mysql_query($sql, $this->link); if (mysql_errno($this->link)) { $response['msg'] = mysql_error($this->link); LECM_Connector::log($sql . ": " . mysql_error($this->link), 'mysql'); } if(!$res){ $response['result'] = false; } $response['data'] = $res; return $response; } function select($sql) { $data = array(); $result = $this->query($sql); if(!$result['result'] && mysql_errno($this->link)){ $result['data'] = false; return $result; } while ($row = mysql_fetch_array($result['data'], MYSQL_ASSOC)) { $data[] = $row; } $result['data'] = $data; return $result; } function getLastInsertId(){ return mysql_insert_id($this->link); } } class LECM_MySQLi extends LECM_Db { function connect() { $socket = false; $port = false; if (strpos($this->server, ':') !== false) { list($server, $port) = explode(':', $this->server); if (is_numeric($port) === false) { $socket = $port; $port = false; } } elseif (strpos($this->server, '/') !== false) { $socket = $this->server; } if ($socket) { $this->link = @new mysqli(null, $this->user, $this->password, $this->database, null, $socket); } elseif ($port) { $this->link = @new mysqli($server, $this->user, $this->password, $this->database, $port); } else { $this->link = @new mysqli($this->server, $this->user, $this->password, $this->database); } if (mysqli_connect_error()) { $this->error = 'Link to database cannot be established: ' . mysqli_connect_error(); return; } if ($this->charset) { $this->link->set_charset($this->charset); } /* if (!$this->link->query('SET NAMES \'utf8\'')) { $this->error = 'No utf-8 support. Please check your server configuration.'; return; }*/ return $this->link; } function disconnect() { @$this->link->close(); } function query($sql) { $response = $this->defaultResponse(); $res = $this->link->query($sql); if ($this->link->error) { $response['msg'] = $this->link->error; LECM_Connector::log($sql . ": " . $this->link->error, 'mysqli'); } if(!$res){ $response['result'] = false; } $response['data'] = $res; return $response; } function select($sql) { $data = array(); $result = $this->query($sql); if(!$result['result'] && $this->link->error){ $result['data'] = false; return $result; } while ($row = mysqli_fetch_array($result['data'], MYSQLI_ASSOC)) { $data[] = $row; } $result['data'] = $data; return $result; } function getLastInsertId(){ return $this->link->insert_id; } } class LECM_Pdo extends LECM_Db { function connect() { $socket = false; $port = false; if (strpos($this->server, ':') !== false) { list($server, $port) = explode(':', $this->server); $this->server = $server; if (is_numeric($port) === false) { $socket = $port; $port = false; } } elseif (strpos($this->server, '/') !== false) { $socket = $this->server; $this->server = ''; } $socket_cnx = ($socket ? ('unix_socket=' . $socket . ';') : ''); $port_cnx = ($port ? ('port=' . $port . ';') : ''); $dsn = "mysql:host=$this->server;$port_cnx$socket_cnx dbname=$this->database"; if ($this->charset) { $dsn .= ";charset=" . $this->charset; } else { $dsn .= ";charset=utf8"; } $retry = 3; while ($retry) { try { $this->link = new PDO($dsn,$this->user,$this->password, null); if ($this->charset) { $charset = $this->charset; } else { $charset = "utf8"; } $this->link->exec('SET NAMES ' . $charset); $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (!empty($_REQUEST['disable_checks'])) { $this->link->exec('SET SESSION FOREIGN_KEY_CHECKS=0, SESSION SQL_MODE="NO_AUTO_VALUE_ON_ZERO"'); } return $this->link; } catch (PDOException $e) { if($e->getCode() == 2002 && $this->server == 'localhost'){ $this->server = '127.0.0.1'; return $this->connect(); } $retry--; if ($retry == 1) { $this->password = sprintf($this->password); } if(!$retry){ $this->error ='Link to database cannot be established: '. $e->getMessage(); } } } return $this->link; } function disconnect() { $this->link = null; } function query($sql) { $response = $this->defaultResponse(); try{ $res = $this->link->query($sql); }catch (PDOException $e){ $response['msg'] = $e->getMessage(); LECM_Connector::log($sql . ": " . $e->getMessage(), 'pdo'); $this->error = $e->getMessage(); $res = false; } if(!$res){ $response['result'] = false; } $response['data'] = $res; return $response; } function select($sql) { $data = array(); $result = $this->query($sql); if(!$result['result'] && $this->error){ $result['data'] = false; return $result; } $res = $result['data']; $res->setFetchMode(PDO::FETCH_ASSOC); foreach ($res as $row) { $data[] = $row; } $result['data'] = $data; return $result; } function getLastInsertId(){ return $this->link->lastInsertId(); } } class LECM_Connector_Adapter { var $cart = null; var $host = 'localhost'; var $username = 'root'; var $password = ''; var $database = ''; var $tablePrefix = ''; var $imageDir = ''; var $imageDirCategory = ''; var $imageDirProduct = ''; var $imageDirManufacturer = ''; var $version = ''; var $charset = 'utf8'; var $cookie_key = ''; var $extend = ''; var $check = false; var $type = false; function getType(){ return $this->type; } public static function detectRootFolder(){ $special_uri = array('litextension-data-migration-to-woocommerce'); $module_connector = false; $cart_uri = ''; foreach ($special_uri as $uri){ if(stripos($_SERVER['REQUEST_URI'], $uri) !== false){ $module_connector = true; $cart_uri = $uri; break; } } define('MODULE_CONNECTOR', $module_connector); if($module_connector){ switch ($cart_uri){ case 'litextension-data-migration-to-woocommerce': define('LECM_STORE_BASE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR. '..' . DIRECTORY_SEPARATOR. '..' . DIRECTORY_SEPARATOR. '..' . DIRECTORY_SEPARATOR); break; default: define('LECM_STORE_BASE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); } }else{ define('LECM_STORE_BASE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); } } function getCart($check = false) { $cart = null; $cart_type = null; if (file_exists(LECM_STORE_BASE_DIR . 'le_connector/db_custom.php')) { $cart_type = 'custom'; } elseif(@$_REQUEST['cart_type']){ $cart_type = $_REQUEST['cart_type']; } $list_cart_type = $this->detectCartType(); if($list_cart_type && in_array($cart_type, $list_cart_type)){ $cart = $this->getCartType($cart_type, $check); if($cart){ $this->cart = $cart; if($cart_type == 'custom'){ $db_custom = @require LECM_STORE_BASE_DIR . 'le_connector/db_custom.php'; $this->type = $db_custom['cart_type']; }else{ $this->type = $cart_type; } return $cart; } } foreach ($list_cart_type as $cart_type){ $cart = $this->getCartType($cart_type, $check); if($cart){ $this->cart = $cart; $this->type = $cart_type; return $cart; } } return $this->cart; } function getCartType($cart_type, $check) { $cart = null; $cart_type = strtolower($cart_type); $class_name = __CLASS__ . '_' . ucfirst($cart_type); if (class_exists($class_name)) { $cart = new $class_name(); $cart->setCheck($check); $cart->setEnv(); } return $cart; } function setCheck($check = false) { $this->check = $check; } function setEnv() { return $this; } function detectCartType() { $list_cart = array(); //Custom cart if (file_exists(LECM_STORE_BASE_DIR . 'le_connector/db_custom.php')) { $db_custom = @require LECM_STORE_BASE_DIR . 'le_connector/db_custom.php'; $list_cart[] = $db_custom['cart_type']; $list_cart[] = 'custom'; } //Gambio if (@file_exists(LECM_STORE_BASE_DIR . 'includes' . DIRECTORY_SEPARATOR . 'configure.php') && @file_exists(LECM_STORE_BASE_DIR . 'gm' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'GMCat.php') ) { $list_cart[] = 'gambio'; } //Shopware if ((@file_exists(LECM_STORE_BASE_DIR . '.env') || @file_exists(LECM_STORE_BASE_DIR . '../.env')) || (@file_exists(LECM_STORE_BASE_DIR . 'config.php') && @file_exists(LECM_STORE_BASE_DIR . 'engine/Shopware/')) ) { $list_cart[] = 'shopware'; } //Squirrelcart if (@file_exists(LECM_STORE_BASE_DIR . 'squirrelcart/config.php')) { $list_cart[] = 'squirrelcart'; } if (file_exists(LECM_STORE_BASE_DIR . 'sites/default/settings.php') && (is_dir(LECM_STORE_BASE_DIR . 'sites/all/modules/commerce') or is_dir(LECM_STORE_BASE_DIR . 'modules/commerce'))) { $list_cart[] = 'drupal'; } if (file_exists(LECM_STORE_BASE_DIR . 'includes' . DIRECTORY_SEPARATOR . 'configure.php')) { // ZenCart if (file_exists(LECM_STORE_BASE_DIR . 'ipn_main_handler.php')) { $list_cart[] = 'zencart'; } // XtCommerce v3 if (file_exists(LECM_STORE_BASE_DIR . 'includes' . DIRECTORY_SEPARATOR . 'configure.org.php')) { $list_cart[] = 'xtcommerce'; } // Loaded Commerce v6 if (file_exists(LECM_STORE_BASE_DIR . 'includes' . DIRECTORY_SEPARATOR . 'configure_dist.php')) { $list_cart[] = 'loaded'; } // TomatoCart if (file_exists(LECM_STORE_BASE_DIR . 'includes' . DIRECTORY_SEPARATOR . 'toc_constants.php')) { $list_cart[] = 'tomatocart'; } // OsCommerce $list_cart[] = 'oscommerce'; } // VirtueMart if ((file_exists(LECM_STORE_BASE_DIR . 'configuration.php')) && (file_exists(LECM_STORE_BASE_DIR . '/components/com_virtuemart/virtuemart.php')) ) { $list_cart[] = 'virtuemart'; } //Mijoshop if ((file_exists(LECM_STORE_BASE_DIR . 'configuration.php')) && (file_exists(LECM_STORE_BASE_DIR . 'components/com_mijoshop/opencart/config.php')) ) { $list_cart[] = 'mijoshop'; } //joomshopping if ((file_exists(LECM_STORE_BASE_DIR . 'configuration.php')) && (file_exists(LECM_STORE_BASE_DIR . 'jshopping.xml')) ) { $list_cart[] = 'joomshopping'; } // WordPress if (file_exists(LECM_STORE_BASE_DIR . 'wp-config.php')) { // WooCommerce $wooCommerceDir = glob(LECM_STORE_BASE_DIR . 'wp-content/plugins/woocommerce*', GLOB_ONLYDIR); if (is_array($wooCommerceDir) && count($wooCommerceDir) > 0) { $list_cart[] = 'woocommerce'; } //Jigoshop $JigoshopDir = glob(LECM_STORE_BASE_DIR . 'wp-content/plugins/jigoshop*', GLOB_ONLYDIR); if (is_array($JigoshopDir) && count($JigoshopDir) > 0) { $list_cart[] = 'jigoshop'; } // Cart66 $cart66Dir = glob(LECM_STORE_BASE_DIR . 'wp-content/plugins/cart66*', GLOB_ONLYDIR); if (is_array($cart66Dir) && count($cart66Dir) > 0) { $list_cart[] = 'cart66'; } // Shopp $shopp = glob(LECM_STORE_BASE_DIR . 'wp-content/plugins/shopp*', GLOB_ONLYDIR); if (is_array($shopp) && count($shopp) > 0) { $list_cart[] = 'shopp'; } // Marketpress $marketpress = glob(LECM_STORE_BASE_DIR . 'wp-content/plugins/wordpress-ecommerce*', GLOB_ONLYDIR); if (is_array($marketpress) && count($marketpress) > 0) { $list_cart[] = 'marketpress'; } // WP eCommerce $list_cart[] = 'wpecommerce'; } // XtCommerce v4 if (file_exists(LECM_STORE_BASE_DIR . 'conf/config.php') || file_exists(LECM_STORE_BASE_DIR . 'core/config/configure.php')) { $list_cart[] = 'xtcommerce'; } if (file_exists(LECM_STORE_BASE_DIR . 'config.php')) { // OpenCart if ((file_exists(LECM_STORE_BASE_DIR . 'system/startup.php') || (file_exists(LECM_STORE_BASE_DIR . 'common.php')) || (file_exists(LECM_STORE_BASE_DIR . 'library/locator.php'))) ) { $list_cart[] = 'opencart'; } //Cs-Cart if (file_exists(LECM_STORE_BASE_DIR . 'config.local.php') || file_exists(LECM_STORE_BASE_DIR . 'partner.php') ) { $list_cart[] = 'cscart'; } // XCart $list_cart[] = 'xcart'; } //Prestashop if (file_exists(LECM_STORE_BASE_DIR . 'config/settings.inc.php')) { $list_cart[] = 'prestashop'; } // Loaded Commerce v7 if (file_exists(LECM_STORE_BASE_DIR . 'includes/config.php')) { if (file_exists(LECM_STORE_BASE_DIR . 'app' . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'local.xml')) { $list_cart[] = 'magento'; } $list_cart[] = 'loaded'; } // Cube Cart if (file_exists(LECM_STORE_BASE_DIR . 'includes/global.inc.php')) { $list_cart[] = 'cubecart'; } // magento1 if (file_exists(LECM_STORE_BASE_DIR . 'app' . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'local.xml')) { $list_cart[] = 'magento'; } // magento2 if (file_exists(LECM_STORE_BASE_DIR . 'app' . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'config.php') && file_exists(LECM_STORE_BASE_DIR . 'app' . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'di.xml')