/** * This methods will be called at application startup * @param $appInstance * @return void */ public static function addRouteDefinitions(Slim $appInstance) { $appInstance->post('/ajax', function () use(&$appInstance) { $exceptionContentType = 'text/plain'; $appInstance->response->headers->set('Cache-Control', 'no-store'); try { $contentType = EmaRpcApi::slimCallback($appInstance); $appInstance->response->headers->set('Content-Type', $contentType); } catch (SecurityException $e) { $appInstance->response->setStatus(401); $appInstance->response->headers->set('Content-Type', $exceptionContentType); print "Unauthorized.\n" . $e->getMessage(); } catch (\RuntimeException $e) { $appInstance->response->setStatus(400); $appInstance->response->headers->set('Content-Type', $exceptionContentType); print $e->getMessage(); $logger = new DbLogger(); $logger->writeException($e); } catch (\Exception $e) { $logger = new DbLogger(); $logger->writeException($e); $appInstance->response->setStatus(500); $appInstance->response->headers->set('Content-Type', $exceptionContentType); $msg = "Server Error Occurred. Please contact us. Error code is: " . $e->getCode(); if (EMA_DEBUG === true) { $msg = $e->getMessage() . ";\n Code: " . $e->getCode() . "\n\n\n" . $e->getTraceAsString(); } print $msg; } }); if (EMA_REST_API) { $appInstance->map('/rest/:path+', function ($path) use($appInstance) { $appInstance->response->headers->set('Cache-Control', 'no-store'); $appInstance->response->headers->set('Content-Type', 'application/json'); $printoutError = function (\Exception $e, $status = 500) use($appInstance) { $appInstance->response->setStatus($status); print EmaRestApi::getErrorOutput($e); }; try { $rpc = EmaRestApi::rpcFactory($path, $appInstance->request->getMethod(), $appInstance); $result = EmaRestApi::rpcCheckAndRun($rpc, $appInstance); if (EmaRestApi::$isAddition) { $appInstance->response->setStatus(201); $appInstance->response->headers->set('Location', EmaRestApi::$additionRouteBase); } print json_encode($result); } catch (InputError $e) { $printoutError($e, 400); } catch (SecurityException $e) { $printoutError($e, 403); } catch (NotFound $e) { $printoutError($e, 404); } catch (Unsupported $e) { $printoutError($e, 415); } catch (\Exception $e) { $printoutError($e, 500); } })->via('GET', 'POST', 'DELETE'); } }
public function save($data) { /* F*****g hindi cocksuckers */ $data['active'] = $data['active'] == 1 ? 1 : 0; $data['group'] = 1; $data['name'] = $data['username']; unset($data['username']); $password = $this->generatePasswordHashAndSalt($data['password']); $data = array_merge($data, $password); if ($this->db->has(self::USERS_TABLE, array('memberid' => $data['memberid']))) { $result = $this->db->update(self::USERS_TABLE, $data, array('memberid' => $data['memberid'])); } else { /*Fix for medoo 1.1.2 */ $data['`name`'] = $data['name']; $data['`password`'] = $data['password']; $data['`group`'] = $data['group']; unset($data['password'], $data['group'], $data['name']); $result = $this->db->insert(self::USERS_TABLE, $data); } if (self::LOG_SAVE_QUERIES) { $logger = new DbLogger(); $logger->write('Trying to write a new member with result: ' . $result . "\n\n\n" . print_r($data, true) . "\n\n\n" . end($this->db->log())); unset($logger); } if (empty($result)) { $error = $this->db->error(); throw new FatalError('Member data has not been wrote to users DB: ' . $error[2]); } return 'Member data saved successfully'; }
public function run() { if (in_array($this->action, self::$skipActions, true)) { return 'Just skipping this action'; } if (self::LOG_CALLS) { $logger = new DbLogger(); /** Adding a date to the first value. **/ $message = 'A `' . $this->action . "` action is performed. Data passed:\n\n"; /** Looping through all request variables. If it is an array, we loop within. **/ $message .= print_r($_REQUEST, true); /** Adding a return message to the log **/ $message .= "\n"; $logger->write($message); unset($logger, $message); } if ($this->action !== 'CHECK') { $this->filteredData = $this->isRequiredFieldsPresent(); if ($this->filteredData === false) { throw new FatalError('Some of required fields are not present in the request'); } } switch ($this->action) { case 'ADD': case 'CHANGE': case 'MANUALADD': case 'ACTIVATE': case 'TRIALTOFULL': case 'EXPIRE': return $this->writeChanges(); } return $this->{$this->action}(); }
protected function writeMembersLogin($memberID) { $data = array('member_id' => $memberID, 'ip' => $_SERVER['REMOTE_ADDR'], 'ua' => $_SERVER['HTTP_USER_AGENT'], 'local' => $_SERVER['HTTP_ACCEPT_LANGUAGE'], 'logintime' => time()); $result = $this->db->insert(self::LOGIN_TABLE, $data); if (empty($result)) { $logger = new DbLogger(); $logger->write('Cannot write to logins table. ' . print_r($data, true)); unset($logger); throw new FatalError('Cannot write to logins table'); } return $result; }
/** * Logs performed SQL queries into DB logger. * EMA_DEBUG and EMA_LOG_SQL_QUERIES constants should be set to true * @param string $type * @param array $args */ private function logSqlQuery($type = 'select', array $args) { if (EMA_DEBUG && EMA_LOG_SQL_QUERIES) { ob_start(); call_user_func_array(array($this->dbConnection->debug(), $type), $args); $sqlQuery = ob_get_contents(); ob_end_clean(); $logger = new DbLogger(); $logger->write("A SQL QUERY performed: \n\n" . $sqlQuery); unset($sqlQuery, $logger); } }
/** * @param $filesToSave * @param array $filteredModel * @param array $existingFiles * @return array * @throws \Exception */ protected function initFileProcessing($filesToSave, array $filteredModel, array $existingFiles = array()) { foreach ($filesToSave as $fieldName => $filesStack) { $workingFilesStack = array(); $modelData = $filteredModel[$fieldName]; if (!empty($modelData['multiple']) && $modelData['multiple'] === 'true') { if (isset($modelData['max-files']) && (int) $modelData['max-files'] > 0) { if (!empty($existingFiles[$fieldName])) { $numOfExistedFiles = count($existingFiles[$fieldName]); } else { $numOfExistedFiles = 0; } $possibleFileToSave = (int) $modelData['max-files'] - $numOfExistedFiles; foreach ($filesStack as $files) { if ($possibleFileToSave > 0) { $workingFilesStack[] = $files; $possibleFileToSave--; } else { break; } } } else { $workingFilesStack = $filesToSave[$fieldName]; } } else { $workingFilesStack[] = end($filesStack); } $filesToSave[$fieldName] = array(); if (!empty($workingFilesStack)) { $filesToSave[$fieldName] = $workingFilesStack; } } $savedFiles = array(); foreach ($filesToSave as $fieldName => $filesStack) { foreach ($filesStack as $key => $files) { $checkStack = array(); if (!empty($filteredModel[$fieldName]['multiple']) && $filteredModel[$fieldName]['multiple'] === 'true') { if (isset($existingFiles[$fieldName])) { $checkStack = $existingFiles[$fieldName]; } } try { $savedStack = $this->saveImages($files, $filteredModel[$fieldName], $checkStack); } catch (FatalError $e) { $logger = new DbLogger(); $logger->write('Error during saving images: ' . $e->getMessage()); continue; } if (!empty($savedStack)) { $savedFiles[$fieldName][$key] = $savedStack; } } } foreach ($savedFiles as $fieldName => $filesStack) { if (!empty($filteredModel[$fieldName]['multiple']) && $filteredModel[$fieldName]['multiple'] === 'true') { if (isset($existingFiles[$fieldName])) { $existingFiles[$fieldName] = array_merge($existingFiles[$fieldName], $this->filterSavedFiles($existingFiles[$fieldName], $filesStack)); } else { $existingFiles[$fieldName] = $filesStack; } } else { $files = end($filesStack); if (!empty($existingFiles[$fieldName])) { $this->deleteFileFromDisk($existingFiles[$fieldName]); } $existingFiles[$fieldName] = $files; } } return $existingFiles; }