public function processing($datasource, $options, $dbspec, $debug) { $dbProxyInstance = new DB_Proxy(); $this->db = $dbProxyInstance; $dbProxyInstance->initialize($datasource, $options, $dbspec, $debug, $_POST["_im_contextname"]); if (!isset($options['media-root-dir'])) { if (isset($_POST["_im_redirect"])) { header("Location: {$_POST["_im_redirect"]}"); } else { $dbProxyInstance->logger->setErrorMessage("'media-root-dir' isn't specified"); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } // requires media-root-dir specification. $fileRoot = $options['media-root-dir']; if (substr($fileRoot, strlen($fileRoot) - 1, 1) != '/') { $fileRoot .= '/'; } if (count($_FILES) < 1) { if (isset($_POST["_im_redirect"])) { header("Location: {$_POST["_im_redirect"]}"); } else { $dbProxyInstance->logger->setErrorMessage("No file wasn't uploaded."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } foreach ($_FILES as $fn => $fileInfo) { } $fileRoot = $options['media-root-dir']; if (substr($fileRoot, strlen($fileRoot) - 1, 1) != '/') { $fileRoot .= '/'; } $filePathInfo = pathinfo(str_replace('\\0', '', basename($fileInfo['name']))); $dirPath = str_replace('.', '_', urlencode($_POST["_im_contextname"])) . '/' . str_replace('.', '_', urlencode($_POST["_im_keyfield"])) . "=" . str_replace('.', '_', urlencode($_POST["_im_keyvalue"])) . '/' . str_replace('.', '_', urlencode($_POST["_im_field"])); $rand4Digits = rand(1000, 9999); $filePartialPath = $dirPath . '/' . $filePathInfo['filename'] . '_' . $rand4Digits . '.' . $filePathInfo['extension']; $filePath = $fileRoot . $filePartialPath; if (strpos($filePath, $fileRoot) !== 0) { $dbProxyInstance->logger->setErrorMessage("Invalid Path Error."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); return; } if (!file_exists($fileRoot . $dirPath)) { $result = mkdir($fileRoot . $dirPath, 0744, true); if (!$result) { $dbProxyInstance->logger->setErrorMessage("Can't make directory. [{$dirPath}]"); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); return; } } $result = move_uploaded_file($fileInfo['tmp_name'], $filePath); if (!$result) { if (isset($_POST["_im_redirect"])) { header("Location: {$_POST["_im_redirect"]}"); } else { $dbProxyInstance->logger->setErrorMessage("Fail to move the uploaded file in the media folder."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } $targetFieldName = $_POST["_im_field"]; $dbProxyContext = $dbProxyInstance->dbSettings->getDataSourceTargetArray(); if (isset($dbProxyContext['file-upload'])) { foreach ($dbProxyContext['file-upload'] as $item) { if (isset($item['field']) && !isset($item['context'])) { $targetFieldName = $item['field']; } } } $dbKeyValue = $_POST["_im_keyvalue"]; $dbProxyInstance = new DB_Proxy(); $dbProxyInstance->initialize($datasource, $options, $dbspec, $debug, $_POST["_im_contextname"]); $dbProxyInstance->dbSettings->addExtraCriteria($_POST["_im_keyfield"], "=", $dbKeyValue); $dbProxyInstance->dbSettings->setTargetFields(array($targetFieldName)); $dbProxyInstance->dbSettings->setValue(array($filePath)); $fileContent = file_get_contents($filePath, false, null, 0, 30); $headerTop = strpos($fileContent, "data:"); $endOfHeader = strpos($fileContent, ","); if ($headerTop === 0 && $endOfHeader > 0) { $tempFilePath = $filePath . ".temp"; rename($filePath, $tempFilePath); $step = 1024; if (strpos($fileContent, ";base64") !== false) { $fw = fopen($filePath, "w"); $fp = fopen($tempFilePath, "r"); fread($fp, $endOfHeader + 1); while ($str = fread($fp, $step)) { fwrite($fw, base64_decode($str)); } fclose($fp); fclose($fw); unlink($tempFilePath); } } $dbProxyInstance->processingRequest($options, "update"); $relatedContext = null; if (isset($dbProxyContext['file-upload'])) { foreach ($dbProxyContext['file-upload'] as $item) { if ($item['field'] == $_POST["_im_field"]) { $relatedContext = new DB_Proxy(); $relatedContext->initialize($datasource, $options, $dbspec, $debug, isset($item['context']) ? $item['context'] : null); $relatedContextInfo = $relatedContext->dbSettings->getDataSourceTargetArray(); $fields = array(); $values = array(); if (isset($relatedContextInfo["query"])) { foreach ($relatedContextInfo["query"] as $cItem) { if ($cItem['operator'] == "=" || $cItem['operator'] == "eq") { $fields[] = $cItem['field']; $values[] = $cItem['value']; } } } if (isset($relatedContextInfo["relation"])) { foreach ($relatedContextInfo["relation"] as $cItem) { if ($cItem['operator'] == "=" || $cItem['operator'] == "eq") { $fields[] = $cItem['foreign-key']; $values[] = $dbKeyValue; } } } $fields[] = "path"; $values[] = $filePartialPath; $relatedContext->dbSettings->setTargetFields($fields); $relatedContext->dbSettings->setValue($values); $relatedContext->processingRequest($options, "new", true); // $relatedContext->finishCommunication(true); // $relatedContext->exportOutputDataAsJSON(); } } } // echo "dbresult='{$filePath}';"; $dbProxyInstance->addOutputData('dbresult', $filePath); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); if (isset($_POST["_im_redirect"])) { header("Location: {$_POST["_im_redirect"]}"); } }
function IM_Entry($datasource, $options, $dbspecification, $debug = false) { global $g_dbInstance, $g_serverSideCall; // check required PHP extensions $requiredFunctions = array('mbstring' => 'mb_internal_encoding'); if (isset($options) && is_array($options)) { foreach ($options as $key => $option) { if ($key == 'authentication' && isset($option['user']) && is_array($option['user']) && array_search('database_native', $option['user']) !== false) { // Native Authentication requires BC Math functions $requiredFunctions = array_merge($requiredFunctions, array('bcmath' => 'bcadd')); break; } } } foreach ($requiredFunctions as $key => $value) { if (!function_exists($value)) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); $generator->generateErrorMessageJS("PHP extension \"" . $key . "\" is required for running INTER-Mediator."); return; } } if ($debug) { $dc = new DefinitionChecker(); $defErrorMessage = $dc->checkDefinitions($datasource, $options, $dbspecification); if (strlen($defErrorMessage) > 0) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); $generator->generateErrorMessageJS($defErrorMessage); return; } } // file_put_contents("/tmp/php2.log", "POST: " . var_export($_POST, true), FILE_APPEND); // file_put_contents("/tmp/php2.log", "GET: " . var_export($_GET, true), FILE_APPEND); // file_put_contents("/tmp/php2.log", "FILES: " . var_export($_FILES, true), FILE_APPEND); // file_put_contents("/tmp/php2.log", "SERVER: " . var_export($_SERVER, true), FILE_APPEND); if (isset($g_serverSideCall) && $g_serverSideCall) { $dbInstance = new DB_Proxy(); $dbInstance->initialize($datasource, $options, $dbspecification, $debug); $dbInstance->processingRequest("NON"); $g_dbInstance = $dbInstance; } else { if (!isset($_POST['access']) && isset($_GET['uploadprocess'])) { $fileUploader = new FileUploader(); $fileUploader->processInfo(); } else { if (!isset($_POST['access']) && isset($_GET['media'])) { $dbProxyInstance = new DB_Proxy(); $dbProxyInstance->initialize($datasource, $options, $dbspecification, $debug); $mediaHandler = new MediaAccess(); if (isset($_GET['attach'])) { $mediaHandler->asAttachment(); } $mediaHandler->processing($dbProxyInstance, $options, $_GET['media']); } else { if (isset($_POST['access']) && $_POST['access'] == 'uploadfile' || isset($_GET['access']) && $_GET['access'] == 'uploadfile') { $fileUploader = new FileUploader(); if (IMUtil::guessFileUploadError()) { $fileUploader->processingAsError($datasource, $options, $dbspecification, $debug); } else { $fileUploader->processing($datasource, $options, $dbspecification, $debug); } } else { if (!isset($_POST['access']) && !isset($_GET['media'])) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); } else { $dbInstance = new DB_Proxy(); $dbInstance->initialize($datasource, $options, $dbspecification, $debug); $util = new IMUtil(); if ($util->protectCSRF() === TRUE) { $dbInstance->processingRequest(); $dbInstance->finishCommunication(false); } else { $dbInstance->addOutputData('debugMessages', 'Invalid Request Error.'); $dbInstance->addOutputData('errorMessages', array('Invalid Request Error.')); } $dbInstance->exportOutputDataAsJSON(); } } } } } }
function IM_Entry($datasource, $options, $dbspecification, $debug = false) { global $g_dbInstance, $g_serverSideCall; spl_autoload_register('loadClass'); // check required PHP extensions $requiredFunctions = array('mbstring' => 'mb_internal_encoding'); if (isset($options) && is_array($options)) { foreach ($options as $key => $option) { if ($key == 'authentication' && isset($option['user']) && is_array($option['user']) && array_search('database_native', $option['user']) !== false) { // Native Authentication requires BC Math functions $requiredFunctions = array_merge($requiredFunctions, array('bcmath' => 'bcadd')); break; } } } foreach ($requiredFunctions as $key => $value) { if (!function_exists($value)) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); $generator->generateErrorMessageJS("PHP extension \"" . $key . "\" is required for running INTER-Mediator."); return; } } if ($debug) { $dc = new DefinitionChecker(); $defErrorMessage = $dc->checkDefinitions($datasource, $options, $dbspecification); if (strlen($defErrorMessage) > 0) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); $generator->generateErrorMessageJS($defErrorMessage); return; } } if (isset($g_serverSideCall) && $g_serverSideCall) { $dbInstance = new DB_Proxy(); $dbInstance->initialize($datasource, $options, $dbspecification, $debug); $dbInstance->processingRequest($options, "NON"); $g_dbInstance = $dbInstance; } else { if (!isset($_POST['access']) && isset($_GET['uploadprocess'])) { $fileUploader = new FileUploader(); $fileUploader->processInfo(); } else { if (!isset($_POST['access']) && isset($_GET['media'])) { $dbProxyInstance = new DB_Proxy(); $dbProxyInstance->initialize($datasource, $options, $dbspecification, $debug); $mediaHandler = new MediaAccess(); if (isset($_GET['attach'])) { $mediaHandler->asAttachment(); } $mediaHandler->processing($dbProxyInstance, $options, $_GET['media']); } else { if (isset($_POST['access']) && $_POST['access'] == 'uploadfile' || isset($_GET['access']) && $_GET['access'] == 'uploadfile') { $fileUploader = new FileUploader(); $fileUploader->processing($datasource, $options, $dbspecification, $debug); } else { if (!isset($_POST['access']) && !isset($_GET['media'])) { $generator = new GenerateJSCode(); $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug); } else { $dbInstance = new DB_Proxy(); $dbInstance->initialize($datasource, $options, $dbspecification, $debug); if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') { $dbInstance->processingRequest($options); $dbInstance->finishCommunication(false); } else { $dbInstance->addOutputData('debugMessages', 'Invalid Request Error.'); $dbInstance->addOutputData('errorMessages', array('Invalid Request Error.')); } $dbInstance->exportOutputDataAsJSON(); } } } } } }
public function processing($datasource, $options, $dbspec, $debug) { $dbProxyInstance = new DB_Proxy(); $this->db = $dbProxyInstance; $dbProxyInstance->initialize($datasource, $options, $dbspec, $debug, $_POST["_im_contextname"]); $useContainer = FALSE; $dbProxyContext = $dbProxyInstance->dbSettings->getDataSourceTargetArray(); if ($dbspec['db-class'] === 'FileMaker_FX' && isset($dbProxyContext['file-upload'])) { foreach ($dbProxyContext['file-upload'] as $item) { if (isset($item['container']) && (bool) $item['container'] === TRUE) { $useContainer = TRUE; } } } $url = NULL; if (isset($_POST['_im_redirect'])) { $url = $this->getRedirectUrl($_POST['_im_redirect']); if (is_null($url)) { header("HTTP/1.1 500 Internal Server Error"); $dbProxyInstance->logger->setErrorMessage('Header may not contain more than a single header, new line detected.'); $dbProxyInstance->processingRequest($options, 'noop'); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); return; } } if (!isset($options['media-root-dir']) && $useContainer === FALSE) { if (!is_null($url)) { header('Location: ' . $url); } else { $dbProxyInstance->logger->setErrorMessage("'media-root-dir' isn't specified"); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } if ($useContainer === FALSE) { // requires media-root-dir specification. $fileRoot = $options['media-root-dir']; if (substr($fileRoot, strlen($fileRoot) - 1, 1) !== '/') { $fileRoot .= '/'; } } if (count($_FILES) < 1) { if (!is_null($url)) { header('Location: ' . $url); } else { $dbProxyInstance->logger->setErrorMessage("No file wasn't uploaded."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } foreach ($_FILES as $fn => $fileInfo) { } $util = new IMUtil(); $filePathInfo = pathinfo($util->removeNull(basename($fileInfo['name']))); if ($useContainer === FALSE) { $fileRoot = $options['media-root-dir']; if (substr($fileRoot, strlen($fileRoot) - 1, 1) != '/') { $fileRoot .= '/'; } $dirPath = str_replace('.', '_', urlencode($_POST["_im_contextname"])) . '/' . str_replace('.', '_', urlencode($_POST["_im_keyfield"])) . "=" . str_replace('.', '_', urlencode($_POST["_im_keyvalue"])) . '/' . str_replace('.', '_', urlencode($_POST["_im_field"])); $rand4Digits = rand(1000, 9999); $filePartialPath = $dirPath . '/' . $filePathInfo['filename'] . '_' . $rand4Digits . '.' . $filePathInfo['extension']; $filePath = $fileRoot . $filePartialPath; if (strpos($filePath, $fileRoot) !== 0) { $dbProxyInstance->logger->setErrorMessage("Invalid Path Error."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); return; } if (!file_exists($fileRoot . $dirPath)) { $result = mkdir($fileRoot . $dirPath, 0744, true); if (!$result) { $dbProxyInstance->logger->setErrorMessage("Can't make directory. [{$dirPath}]"); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); return; } } } if ($useContainer === TRUE) { // for uploading to FileMaker's container field $fileName = $filePathInfo['filename'] . '.' . $filePathInfo['extension']; $tmpDir = ini_get('upload_tmp_dir'); if ($tmpDir === '') { $tmpDir = sys_get_temp_dir(); } if (mb_substr($tmpDir, 1) === DIRECTORY_SEPARATOR) { $filePath = $tmpDir . $fileName; } else { $filePath = $tmpDir . DIRECTORY_SEPARATOR . $fileName; } } $result = move_uploaded_file($util->removeNull($fileInfo['tmp_name']), $filePath); if (!$result) { if (!is_null($url)) { header('Location: ' . $url); } else { $dbProxyInstance->logger->setErrorMessage("Fail to move the uploaded file in the media folder."); $dbProxyInstance->processingRequest($options, "noop"); $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); } return; } $targetFieldName = $_POST["_im_field"]; if ($useContainer === FALSE) { $dbProxyContext = $dbProxyInstance->dbSettings->getDataSourceTargetArray(); if (isset($dbProxyContext['file-upload'])) { foreach ($dbProxyContext['file-upload'] as $item) { if (isset($item['field']) && !isset($item['context'])) { $targetFieldName = $item['field']; } } } } $dbKeyValue = $_POST["_im_keyvalue"]; $dbProxyInstance = new DB_Proxy(); $dbProxyInstance->initialize($datasource, $options, $dbspec, $debug, $_POST["_im_contextname"]); $dbProxyInstance->dbSettings->addExtraCriteria($_POST["_im_keyfield"], "=", $dbKeyValue); $dbProxyInstance->dbSettings->setTargetFields(array($targetFieldName)); $fileContent = file_get_contents($filePath, false, null, 0, 30); $headerTop = strpos($fileContent, "data:"); $endOfHeader = strpos($fileContent, ","); if ($headerTop === 0 && $endOfHeader > 0) { $tempFilePath = $filePath . ".temp"; rename($filePath, $tempFilePath); $step = 1024; if (strpos($fileContent, ";base64") !== false) { $fw = fopen($filePath, "w"); $fp = fopen($tempFilePath, "r"); fread($fp, $endOfHeader + 1); while ($str = fread($fp, $step)) { fwrite($fw, base64_decode($str)); } fclose($fp); fclose($fw); unlink($tempFilePath); } } if ($useContainer === FALSE) { $dbProxyInstance->dbSettings->setValue(array($filePath)); } else { $dbProxyInstance->dbSettings->setValue(array($fileName . "\n" . base64_encode(file_get_contents($filePath)))); } $dbProxyInstance->processingRequest($options, "update"); $relatedContext = null; if ($useContainer === FALSE) { if (isset($dbProxyContext['file-upload'])) { foreach ($dbProxyContext['file-upload'] as $item) { if ($item['field'] == $_POST["_im_field"]) { $relatedContext = new DB_Proxy(); $relatedContext->initialize($datasource, $options, $dbspec, $debug, isset($item['context']) ? $item['context'] : null); $relatedContextInfo = $relatedContext->dbSettings->getDataSourceTargetArray(); $fields = array(); $values = array(); if (isset($relatedContextInfo["query"])) { foreach ($relatedContextInfo["query"] as $cItem) { if ($cItem['operator'] == "=" || $cItem['operator'] == "eq") { $fields[] = $cItem['field']; $values[] = $cItem['value']; } } } if (isset($relatedContextInfo["relation"])) { foreach ($relatedContextInfo["relation"] as $cItem) { if ($cItem['operator'] == "=" || $cItem['operator'] == "eq") { $fields[] = $cItem['foreign-key']; $values[] = $dbKeyValue; } } } $fields[] = "path"; $values[] = $filePartialPath; $relatedContext->dbSettings->setTargetFields($fields); $relatedContext->dbSettings->setValue($values); $relatedContext->processingRequest($options, "create", true); // $relatedContext->finishCommunication(true); // $relatedContext->exportOutputDataAsJSON(); } } } } if ($useContainer === FALSE) { $dbProxyInstance->addOutputData('dbresult', $filePath); } else { $dbProxyInstance->addOutputData('dbresult', '/fmi/xml/cnt/' . $fileName . '?-db=' . urlencode($dbProxyInstance->dbSettings->getDbSpecDatabase()) . '&-lay=' . urlencode($datasource[0]['name']) . '&-recid=' . intval($_POST['_im_keyvalue']) . '&-field=' . urlencode($targetFieldName)); } $dbProxyInstance->finishCommunication(); $dbProxyInstance->exportOutputDataAsJSON(); if (!is_null($url)) { header('Location: ' . $url); } }