public static function install($data, &$fail, &$errno, &$error) { // Datenbank einrichten if (!isset($data['action']) || $data['action'] != 'update') { if (!$fail && (isset($data['DB']['db_override']) && $data['DB']['db_override'] === 'override')) { $sql = "DROP SCHEMA IF EXISTS `" . $data['DB']['db_name'] . "`;"; $oldName = $data['DB']['db_name']; $data['DB']['db_name'] = null; $result = DBRequest::request($sql, false, $data); if ($result["errno"] !== 0) { $fail = true; $errno = $result["errno"]; $error = isset($result["error"]) ? $result["error"] : ''; } $data['DB']['db_name'] = $oldName; } } if (!$fail) { $add = isset($data['DB']['db_ignore']) && $data['DB']['db_ignore'] === 'ignore' || isset($data['action']) && $data['action'] == 'update' ? 'IF NOT EXISTS ' : ''; $sql = "CREATE SCHEMA {$add}`" . $data['DB']['db_name'] . "` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;"; $oldName = $data['DB']['db_name']; $data['DB']['db_name'] = null; $result = DBRequest::request($sql, false, $data); if ($result["errno"] !== 0) { $fail = true; $errno = $result["errno"]; $error = isset($result["error"]) ? $result["error"] : ''; } $data['DB']['db_name'] = $oldName; } // CControl+DBQuery2 einrichten $res = array(); if (!$fail) { $list = array('DB/CControl', 'DB/DBQuery2'); $platform = Installation::PlattformZusammenstellen($data); for ($i = 0; $i < count($list); $i++) { $url = $list[$i]; //$data['PL']['init']; // inits all components $result = Request::post($data['PL']['url'] . '/' . $url . '/platform', array(), Platform::encodePlatform($platform)); $res[$url] = array(); if (isset($result['content']) && isset($result['status']) && $result['status'] === 201) { $res[$url]['status'] = 201; } else { $res[$url]['status'] = 409; $fail = true; if (isset($result['status'])) { $errno = $result['status']; $res[$url]['status'] = $result['status']; } ///if (isset($result['content'])) echo $result['content']; } } } return $res; }
public static function install($data, &$fail, &$errno, &$error) { if (!$fail) { $auth = new Authentication(); $salt = $auth->generateSalt(); $passwordHash = $auth->hashPassword($data['DB']['db_passwd_insert'], $salt); $sql = "INSERT INTO `User` (`U_id`, `U_username`, `U_email`, `U_lastName`, `U_firstName`, `U_title`, `U_password`, `U_flag`, `U_salt`, `U_failed_logins`, `U_externalId`, `U_studentNumber`, `U_isSuperAdmin`, `U_comment`) VALUES (NULL, '{$data['DB']['db_user_insert']}', '{$data['DB']['db_email_insert']}', '{$data['DB']['db_last_name_insert']}', '{$data['DB']['db_first_name_insert']}', NULL, '{$passwordHash}', 1, '{$salt}', 0, NULL, NULL, 1, NULL);"; $result = DBRequest::request($sql, false, $data); if ($result["errno"] !== 0) { $fail = true; $errno = $result["errno"]; $error = isset($result["error"]) ? $result["error"] : ''; } } return null; }
public function postQuery($callName, $input, $par = array()) { $par = DBJson::mysql_real_escape_string($par); $params = array(); extract($par, EXTR_OVERWRITE); $config = parse_ini_file(dirname(__FILE__) . '/config.ini', TRUE); $obj = $input; $answer = DBRequest::request2($obj->getRequest(), $obj->getCheckSession(), $config); $result = Model::isOK(); $result['content'] = array(); $res = array(); foreach ($answer as $query_result) { $obj = new Query(); if ($query_result['errno'] != 0) { if (isset($query_result['errno']) && $query_result['errno'] != 0) { Logger::Log('GET queryResult failed errno: ' . $query_result['errno'] . ' error: ' . $query_result['error'], LogLevel::ERROR); } if (!isset($query_result['content']) || !$query_result['content']) { Logger::Log('GET queryResult failed, no content', LogLevel::ERROR); } if (isset($query_result['errno']) && $query_result['errno'] == 401) { $result = Model::isRejected(); } else { $result = Model::isProblem(); } } elseif (gettype($query_result['content']) == 'boolean') { $obj->setResponse(array()); if (isset($query_result['affectedRows'])) { $obj->setAffectedRows($query_result['affectedRows']); } if (isset($query_result['insertId'])) { $obj->setInsertId($query_result['insertId']); } if (isset($query_result['errno'])) { $obj->setErrno($query_result['errno']); } if (isset($query_result['numRows'])) { $obj->setNumRows($query_result['numRows']); } if (isset($query_result['errno']) && $query_result['errno'] > 0) { $result = Model::isProblem(); } else { $result = Model::isCreated(); } } else { $data = array(); if (isset($query_result['numRows']) && $query_result['numRows'] > 0) { $data = $query_result['content']; } $obj->setResponse($data); if (isset($query_result['affectedRows'])) { $obj->setAffectedRows($query_result['affectedRows']); } if (isset($query_result['insertId'])) { $obj->setInsertId($query_result['insertId']); } if (isset($query_result['errno'])) { $obj->setErrno($query_result['errno']); } if (isset($query_result['numRows'])) { $obj->setNumRows($query_result['numRows']); } if (isset($query_result['errno']) && $query_result['errno'] > 0) { $result = Model::isProblem(); } else { $result = Model::isCreated(); } //$result = Model::isOK(); } $res[] = $obj; } if (count($res) == 1) { $res = $res[0]; } $result['content'] = $res; return $result; }
/** * Adds the component to a course */ public function addCourse($pre = '') { $this->loadConfig($pre); $pre = ($pre === '' ? '' : '_') . $pre; Logger::Log('starts POST AddCourse', LogLevel::DEBUG); // decode the received course data, as an object $insert = Course::decodeCourse($this->_app->request->getBody()); // always been an array $arr = true; if (!is_array($insert)) { $insert = array($insert); $arr = false; } // this array contains the indices of the inserted objects $res = array(); foreach ($insert as $in) { // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, dirname(__FILE__) . '/Sql/AddCourse.sql', array('object' => $in, 'pre' => $pre)); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Query::decodeQuery($result['content']); $res[] = $in; $this->_app->response->setStatus(201); if (isset($result['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']); } } else { Logger::Log('POST AddCourse failed', LogLevel::ERROR); $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409); $this->_app->response->setBody(Course::encodeCourse($res)); $this->_app->stop(); } } if (!$arr && count($res) == 1) { $this->_app->response->setBody(Course::encodeCourse($res[0])); } else { $this->_app->response->setBody(Course::encodeCourse($res)); } }
public static function install($data, &$fail, &$errno, &$error) { if (!$fail && (isset($data['action']) && $data['action'] == 'update' || isset($data['DB']['db_user_override_operator']) && $data['DB']['db_user_override_operator'] === 'override')) { $oldName = $data['DB']['db_name']; $data['DB']['db_name'] = null; $sql = "DROP USER '{$data['DB']['db_user_operator']}'@'%';"; $sql2 = "DROP USER '{$data['DB']['db_user_operator']}'@'localhost';"; $result = DBRequest::request2($sql, false, $data); $result = DBRequest::request2($sql2, false, $data); /*if ($result["errno"] !== 0){ $fail = true; $errno = $result["errno"];$error = isset($result["error"]) ? $result["error"] : ''; }*/ $data['DB']['db_name'] = $oldName; } $userExists = false; if (!$fail) { $oldName = $data['DB']['db_name']; $data['DB']['db_name'] = null; $sql = "SELECT count(1) as 'exists' FROM mysql.user WHERE user = '******'DB']['db_user_operator']}';"; $result = DBRequest::request($sql, false, $data); if ($result["errno"] !== 0 || !isset($result["content"])) { $fail = true; $errno = $result["errno"]; $error = isset($result["error"]) ? $result["error"] : ''; } else { $result = DBJson::getRows($result['content']); if (count($result) > 0 && isset($result[0]['exists']) && $result[0]['exists'] > 0) { $userExists = true; } } $data['DB']['db_name'] = $oldName; } if (!$fail && !$userExists) { $oldName = $data['DB']['db_name']; $data['DB']['db_name'] = null; $sql = "GRANT CREATE VIEW,EXECUTE,ALTER ROUTINE,CREATE ROUTINE,SHOW VIEW,CREATE TEMPORARY TABLES,INDEX,ALTER,SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,TRIGGER " . "ON `{$oldName}`.* " . "TO '{$data['DB']['db_user_operator']}'@'%' " . "IDENTIFIED BY '{$data['DB']['db_passwd_operator']}';"; $sql .= "GRANT CREATE VIEW,EXECUTE,ALTER ROUTINE,CREATE ROUTINE,SHOW VIEW,CREATE TEMPORARY TABLES,INDEX,ALTER,SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,TRIGGER " . "ON `{$oldName}`.* " . "TO '{$data['DB']['db_user_operator']}'@'localhost' " . "IDENTIFIED BY '{$data['DB']['db_passwd_operator']}';"; $result = DBRequest::request2($sql, false, $data); if ($result[0]["errno"] !== 0 && (count($result) < 2 || $result[1]["errno"] !== 0)) { $fail = true; $errno = $result[0]["errno"]; $error = isset($result[0]["error"]) ? $result[0]["error"] : ''; } $data['DB']['db_name'] = $oldName; } elseif ($userExists) { $fail = true; $errno = 0; $error = 'user already exists'; } return null; }
public static function install($data, &$fail, &$errno, &$error) { $serverFiles = Installation::GibServerDateien(); $installComponentDefsResult['components'] = array(); foreach ($serverFiles as $sf) { $sf = pathinfo($sf)['filename']; $tempData = Einstellungen::ladeEinstellungenDirekt($sf, $data); if ($tempData === null) { $fail = true; $error = Language::Get('generateComponents', 'noAccess'); return; } $componentList = Zugang::Ermitteln('actionInstallComponentDefs', 'KomponentenErstellen::installiereKomponentenDefinitionen', $tempData, $fail, $errno, $error); if (isset($componentList['components'])) { $installComponentDefsResult['components'] = array_merge($installComponentDefsResult['components'], $componentList['components']); } } // Komponenten erzeugen $comList = array(); $setDBNames = array(); $ComponentList = array(); // zunächst die Komponentenliste nach Namen sortieren $ComponentListInput = array(); foreach ($installComponentDefsResult['components'] as $key => $input) { if (!isset($input['name'])) { continue; } if (!isset($ComponentListInput[$input['name']])) { $ComponentListInput[$input['name']] = array(); } $ComponentListInput[$input['name']][$key] = $input; } for ($zz = 0; $zz < 2; $zz++) { $tempList = array(); foreach ($ComponentListInput as $key2 => $ComNames) { foreach ($ComNames as $key => $input) { if (!isset($input['name'])) { continue; } if (!isset($input['type']) || $input['type'] == 'normal') { // normale Komponente if (!isset($input['registered'])) { $comList[] = "('{$input['name']}', '{$input['urlExtern']}/{$input['path']}', '" . (isset($input['option']) ? $input['option'] : '') . "', '" . implode(';', isset($input['def']) ? $input['def'] : array()) . "')"; // Verknüpfungen erstellen $setDBNames[] = " SET @{$key}_{$input['name']} = (select CO_id from Component where CO_address='{$input['urlExtern']}/{$input['path']}' limit 1); "; $input['dbName'] = $key . '_' . $input['name']; $input['registered'] = '1'; } if (!isset($tempList[$key2])) { $tempList[$key2] = array(); } $tempList[$key2][] = $input; } elseif (isset($input['type']) && $input['type'] == 'clone') { // Komponente basiert auf einer bestehenden if (!isset($input['base'])) { continue; } if (!isset($input['baseURI'])) { $input['baseURI'] = ''; } if (isset($ComponentListInput[$input['base']])) { foreach ($ComponentListInput[$input['base']] as $key3 => $input2) { if (!isset($input2['name'])) { continue; } // pruefe, dass die Eintraege nicht doppelt erstellt werden $found = false; if (isset($ComponentListInput[$input['name']])) { foreach ($ComponentListInput[$input['name']] as $input3) { if ((!isset($input3['type']) || $input3['type'] == 'normal') && $input['name'] == $input3['name'] && "{$input3['urlExtern']}/{$input3['path']}" == "{$input2['urlExtern']}/{$input2['path']}{$input['baseURI']}") { $found = true; break; } } } if ($found) { continue; } if (isset($tempList[$input['name']])) { foreach ($tempList[$input['name']] as $input3) { if ($input['name'] == $input3['name'] && "{$input3['urlExtern']}/{$input3['path']}" == "{$input2['urlExtern']}/{$input2['path']}{$input['baseURI']}") { $found = true; break; } } } if ($found) { continue; } $input2['path'] = "{$input2['path']}{$input['baseURI']}"; $input2['def'] = array_merge($input2['def'], $input['def']); $input2['links'] = array_merge(isset($input2['links']) ? $input2['links'] : array(), isset($input['links']) ? $input['links'] : array()); $input2['connector'] = array_merge(isset($input2['connector']) ? $input2['connector'] : array(), isset($input['connector']) ? $input['connector'] : array()); if (isset($input['option'])) { $input2['option'] = $input['option']; } $input2['name'] = $input['name']; $input2['registered'] = null; if (!isset($tempList[$key2])) { $tempList[$key2] = array(); } $tempList[$key2][] = $input2; } } } } } $ComponentListInput = $tempList; } $sql = "START TRANSACTION;SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;TRUNCATE TABLE `ComponentLinkage`;SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;COMMIT;"; //TRUNCATE TABLE `Component`; DBRequest::request2($sql, false, $data, true); $sql = "UPDATE `Component` SET `CO_status` = '0';"; DBRequest::request2($sql, false, $data, true); $sql = "START TRANSACTION;SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;INSERT INTO `Component` (`CO_name`, `CO_address`, `CO_option`, `CO_def`) VALUES "; $installComponentDefsResult['componentsCount'] = count($comList); $sql .= implode(',', $comList); unset($comList); $sql .= " ON DUPLICATE KEY UPDATE CO_status='1', CO_address=VALUES(CO_address), CO_option=VALUES(CO_option), CO_def=VALUES(CO_def);SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;COMMIT;"; //echo $sql; DBRequest::request2($sql, false, $data, true); //echo $sql; $sql = "START TRANSACTION;SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;"; $sql .= implode('', $setDBNames); unset($setDBNames); $links = array(); foreach ($ComponentListInput as $key2 => $ComNames) { foreach ($ComNames as $key => $input) { if (isset($input['type']) && $input['type'] != 'normal') { continue; } if (isset($input['dbName'])) { // prüfe nun alle Verknüpfungen dieser Komponente und erstelle diese if (isset($input['links'])) { foreach ($input['links'] as $link) { if (!isset($link['target'])) { $link['target'] = ''; } if (!is_array($link['target'])) { $link['target'] = array($link['target']); } foreach ($link['target'] as $tar) { // $tar -> der Name der Zielkomponente if (!isset($ComponentListInput[$tar])) { continue; } foreach ($ComponentListInput[$tar] as $target) { // $target -> das Objekt der Zielkomponente if (!isset($target['dbName'])) { continue; } if (!isset($input['link_type']) || $input['link_type'] == 'local' || $input['link_type'] == '') { if ($input['urlExtern'] == $target['urlExtern']) { $priority = isset($input['priority']) ? ", CL_priority = {$input['priority']}" : ''; $relevanz = isset($input['relevanz']) ? $input['relevanz'] : ''; $sql .= " INSERT INTO `ComponentLinkage` SET CO_id_owner = @{$input['dbName']}, CL_name = '{$link['name']}', CL_relevanz = '{$relevanz}', CO_id_target = @{$target['dbName']} {$priority};"; $links[] = 1; } } elseif ($input['link_type'] == 'full') { if ($input['urlExtern'] == $target['urlExtern'] || isset($target['link_availability']) && $target['link_availability'] == 'full') { $priority = isset($input['priority']) ? ", CL_priority = {$input['priority']}" : ''; $relevanz = isset($input['relevanz']) ? $input['relevanz'] : ''; $sql .= " INSERT INTO `ComponentLinkage` SET CO_id_owner = @{$input['dbName']}, CL_name = '{$link['name']}', CL_relevanz = '{$relevanz}', CO_id_target = @{$target['dbName']} {$priority};"; $links[] = 1; } } } } } } if (isset($input['connector'])) { foreach ($input['connector'] as $link) { if (!isset($link['target'])) { $link['target'] = ''; } if (!is_array($link['target'])) { $link['target'] = array($link['target']); } if (!isset($link['links'])) { $link['links'] = array('a' => null); } foreach ($link['links'] as $callKey => $call) { foreach ($link['target'] as $tar) { // $tar -> der Name der Zielkomponente if (!isset($ComponentListInput[$tar])) { continue; } foreach ($ComponentListInput[$tar] as $target) { // $target -> das Objekt der Zielkomponente if (!isset($target['dbName'])) { continue; } if (!isset($input['link_type']) || $input['link_type'] == 'local' || $input['link_type'] == '') { if ($input['urlExtern'] == $target['urlExtern']) { $priority = isset($link['priority']) ? ", CL_priority = {$link['priority']}" : ''; $method = isset($call['method']) ? $call['method'] : 'GET'; $path = isset($call['path']) ? ", CL_path = '{$method} {$call['path']}'" : ''; $relevanz = isset($link['relevanz']) ? $link['relevanz'] : ''; $sql .= " INSERT INTO `ComponentLinkage` SET CO_id_owner = @{$target['dbName']}, CL_name = '{$link['name']}', CL_relevanz = '{$relevanz}', CO_id_target = @{$input['dbName']} {$priority} {$path};"; $links[] = 1; } } elseif ($input['link_type'] == 'full') { if ($input['urlExtern'] == $target['urlExtern'] || isset($input['link_availability']) && $input['link_availability'] == 'full') { $priority = isset($link['priority']) ? ", CL_priority = {$link['priority']}" : ''; $method = isset($call['method']) ? $call['method'] : 'GET'; $path = isset($call['path']) ? ", CL_path = '{$method} {$call['path']}'" : ''; $relevanz = isset($link['relevanz']) ? $link['relevanz'] : ''; $sql .= " INSERT INTO `ComponentLinkage` SET CO_id_owner = @{$target['dbName']}, CL_name = '{$link['name']}', CL_relevanz = '{$relevanz}', CO_id_target = @{$input['dbName']} {$priority} {$path};"; $links[] = 1; } } } } } } } } } } $installComponentDefsResult['linksCount'] = count($links); $sql .= " SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;COMMIT;"; DBRequest::request2($sql, false, $data, true); $installComponentDefsResult['components'] = $ComponentListInput; return $installComponentDefsResult; }
/** * Sendet $sql an $linkName und behandelt die Antwort * * @param string $linkName Der Name des Ausgangs * @param string $sql Der zu verwendende SQL Inhalt * @param int $positiveStatus Der Status, welcher als erfolgreiche Antwort gesehen wird (Bsp.: 200) * @param callable $positiveMethod Im positiven Fall wird diese Methode aufgerufen * @param mixed[] $positiveParams Die Werte, welche an die positive Funktion übergeben werden * @param callable $negativeMethod Im negativen Fall wird diese Methode aufgerufen * @param mixed[] $negativeParams Die Werte, welche an die negative Funktion übergeben werden * @param bool $checkSession Ob die Sessiondaten in der Datenbank geprüft werden sollen * @return mixed Das Ergebnis der aufgerufenen Resultatfunktion */ public function callSql($linkName, $sql, $positiveStatus, callable $positiveMethod, $positiveParams, callable $negativeMethod, $negativeParams, $checkSession = true) { $link = CConfig::getLink($this->_conf->getLinks(), $linkName); // starts a query, by using given sql statements/statement $result = DBRequest::getRoutedSql($link, $sql, $checkSession); // checks the correctness of the query if ($result['status'] == $positiveStatus) { // die Antwort war so, wie wir sie erwartet haben $queryResult = Query::decodeQuery($result['content']); if (!is_array($queryResult)) { $queryResult = array($queryResult); } // rufe nun die positive Methode auf return call_user_func_array($positiveMethod, array_merge(array("input" => $queryResult), $positiveParams)); } // ansonsten rufen wir die negative Methode auf return call_user_func_array($negativeMethod, $negativeParams); }
/** * Adds the component to the platform * * Called when this component receives an HTTP POST request to * /platform. */ public function addPlatform() { Logger::Log('starts POST AddPlatform', LogLevel::DEBUG); // decode the received course data, as an object $insert = Platform::decodePlatform($this->_app->request->getBody()); // always been an array $arr = true; if (!is_array($insert)) { $insert = array($insert); $arr = false; } // this array contains the indices of the inserted objects $res = array(); foreach ($insert as $in) { $file = dirname(__FILE__) . '/config.ini'; $text = "[DB]\n" . "db_path = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getDatabaseUrl()) . "\"\n" . "db_user = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getDatabaseOperatorUser()) . "\"\n" . "db_passwd = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getDatabaseOperatorPassword()) . "\"\n" . "db_name = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getDatabaseName()) . "\"\n" . "[PL]\n" . "urlExtern = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getExternalUrl()) . "\"\n" . "url = \"" . str_replace(array("\\", "\""), array("\\\\", "\\\""), $in->getBaseUrl()) . "\""; if (!@file_put_contents($file, $text)) { Logger::Log('POST AddPlatform failed, config.ini no access', LogLevel::ERROR); $this->_app->response->setStatus(409); $this->_app->stop(); } // starts a query ob_start(); eval("?>" . file_get_contents(dirname(__FILE__) . '/Sql/AddPlatform.sql')); $sql = ob_get_contents(); ob_end_clean(); $result = DBRequest::request2($sql, false, parse_ini_file(dirname(__FILE__) . '/config.ini', TRUE)); // checks the correctness of the query if (!isset($result['errno']) || !$result['errno']) { $platform = new Platform(); $platform->setStatus(201); $res[] = $platform; $this->_app->response->setStatus(201); } else { Logger::Log('POST AddPlatform failed', LogLevel::ERROR); $this->_app->response->setStatus(409); $this->_app->response->setBody(Platform::encodePlatform($res)); $this->_app->stop(); } } if (!$arr && count($res) == 1) { $this->_app->response->setBody(Platform::encodePlatform($res[0])); } else { $this->_app->response->setBody(Platform::encodePlatform($res)); } }