* [bug #67287] remove unused ai code to prevent SSRF vulnerability.

This commit is contained in:
sunhao
2025-11-17 11:01:23 +08:00
parent a28b52d597
commit 6169c5d228
-271
View File
@@ -60,233 +60,6 @@ class ai extends control
return $this->send(array('result' => 'fail', 'message' => $this->lang->error->accessDenied, 'locate' => 'back'));
}
/**
* List models.
*
* @param string $orderBy
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return void
*/
public function models($orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$this->app->loadClass('pager', $static = true);
$pager = new pager($recTotal, $recPerPage, $pageID);
/* Adapt order. */
$actualOrdering = $orderBy;
if(strpos(strtolower($actualOrdering), 'usesproxy') !== false) $actualOrdering = str_ireplace('usesproxy', 'proxy', $actualOrdering);
$models = $this->ai->getLanguageModels('', false, $pager, $actualOrdering);
/* Process model props to displayable format. */
foreach($models as $model)
{
$model->usesProxy = !empty($model->proxy) ? 'on' : 'off';
$model->vendor = $this->lang->ai->models->vendorList->{$model->type}[$model->vendor];
if(empty($model->name)) $model->name = $this->lang->ai->models->typeList[$model->type];
}
$this->view->models = $models;
$this->view->orderBy = $orderBy;
$this->view->pager = $pager;
$this->view->title = $this->lang->ai->models->title;
$this->display();
}
/**
* View language model configuration.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelView($modelID)
{
$model = $this->ai->getLanguageModel($modelID);
if(empty($model)) return $this->locate($this->createLink('ai', 'models'));
$this->view->model = $this->ai->unserializeModel($model);
$this->view->title = $this->lang->ai->models->view;
$this->display();
}
/**
* Create language model configuration.
*
* @access public
* @return void
*/
public function modelCreate()
{
if(strtolower($this->server->request_method) == 'post')
{
$modelConfig = fixer::input('post')->get();
/* Check for required credentials. */
$credentials = array();
if(!empty($this->config->ai->vendorList[$modelConfig->vendor]['credentials'])) $credentials = $this->config->ai->vendorList[$modelConfig->vendor]['credentials'];
foreach($credentials as $credKey)
{
if(empty($modelConfig->$credKey)) dao::$errors[$credKey][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$credKey);
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr)) dao::$errors['proxyAddr'][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
}
if(!empty(dao::$errors)) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
$result = $this->ai->createModel($modelConfig);
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('modelView', "modelID=$result") . '#app=admin'));
}
$this->view->title = $this->lang->ai->models->create;
$this->display();
}
/**
* Edit language model configuration.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelEdit($modelID)
{
if(strtolower($this->server->request_method) == 'post')
{
$modelConfig = fixer::input('post')->get();
/* Check for required credentials. */
$credentials = array();
if(!empty($this->config->ai->vendorList[$modelConfig->vendor]['credentials'])) $credentials = $this->config->ai->vendorList[$modelConfig->vendor]['credentials'];
foreach($credentials as $credKey)
{
if(empty($modelConfig->$credKey)) dao::$errors[$credKey][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$credKey);
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr)) dao::$errors['proxyAddr'][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
}
if(!empty(dao::$errors)) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
$result = $this->ai->updateModel($modelID, $modelConfig);
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('modelView', "modelID=$modelID") . '#app=admin'));
}
$model = $this->ai->getLanguageModel($modelID);
if(empty($model)) return $this->locate($this->createLink('ai', 'models'));
$modelData = $this->ai->unserializeModel($model);
if(empty($model->name)) $modelData->name = ''; // Set name to empty if it was not set.
$this->view->model = $modelData;
$this->view->title = $this->lang->ai->models->edit;
$this->display();
}
/**
* Enable language model.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelEnable($modelID)
{
$result = $this->ai->toggleModel($modelID, true);
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
return $this->sendSuccess(array('load' => true, 'message' => ''));
}
/**
* Disable language model.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelDisable($modelID)
{
$result = $this->ai->toggleModel($modelID, false);
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
return $this->sendSuccess(array('load' => true, 'message' => ''));
}
/**
* Delete language model.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelDelete($modelID)
{
$result = $this->ai->deleteModel($modelID);
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
return $this->send(array('result' => 'success', 'load' => array('locate' => inlink('models'))));
}
/**
* Test connection with model configuration.
*
* @param int $modelID
* @access public
* @return void
*/
public function modelTestConnection($modelID = 0)
{
$result = false;
if(strtolower($this->server->request_method) == 'post')
{
/* Test model connection from form if method is POST. */
$modelConfig = fixer::input('post')->get();
$currentVendor = empty($modelConfig->vendor) ? key($this->lang->ai->models->vendorList->{empty($modelConfig->type) ? key($this->lang->ai->models->typeList) : $modelConfig->type}) : $modelConfig->vendor;
$vendorRequiredFields = $this->config->ai->vendorList[$currentVendor]['credentials'];
$errors = array();
if(empty($modelConfig->type)) $errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->type);
foreach($vendorRequiredFields as $field)
{
if(empty($modelConfig->$field)) $errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$field);
}
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr))
{
$errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
}
if(!empty($errors)) return $this->send(array('result' => 'fail', 'message' => implode(' ', $errors)));
$this->ai->setModelConfig($modelConfig);
if($this->config->ai->models[$modelConfig->type] == 'ernie' || $currentVendor == 'azure' || $modelConfig->type == 'openai-gpt4' || $modelConfig->vendor == 'openaiCompatible')
{
$messages = array((object)array('role' => 'user', 'content' => 'test'));
$result = $this->ai->converse(null, $messages, array('maxTokens' => 1));
}
else
{
$result = $this->ai->complete(null, 'test', 1); // Test completing 'test' with length of 1.
}
}
else
{
/* Test model with id if not POST. */
$result = $this->ai->testModelConnection($modelID);
}
if($result === false)
{
return $this->send(array('result' => 'fail', 'message' => empty($this->ai->errors) ? $this->lang->ai->models->testConnectionResult->fail : sprintf($this->lang->ai->models->testConnectionResult->failFormat, implode(', ', $this->ai->errors))));
}
return $this->send(array('result' => 'success', 'message' => $this->lang->ai->models->testConnectionResult->success));
}
/**
* List mini programs.
@@ -993,48 +766,4 @@ class ai extends control
));
}
}
/**
* Chat with LLMs.
*
* @access public
* @return void
*/
public function chat()
{
$messages = array();
if(!empty($_POST))
{
$history = $this->post->history;
$message = $this->post->message;
$isRetry = $this->post->retry == 'true';
$messages = json_decode($history);
if(empty($messages)) $messages[] = (object)array('role' => 'system', 'content' => $this->lang->ai->chatSystemMessage);
if(!$isRetry) $messages[] = (object)array('role' => 'user', 'content' => $message);
if($this->ai->hasModelsAvailable())
{
$response = $this->ai->converse(null, $messages);
if(empty($response))
{
$this->view->error = $this->lang->ai->chatNoResponse;
}
else
{
$messages[] = (object)array('role' => 'assistant', 'content' => is_array($response) ? current($response) : $response);
}
}
else
{
$this->view->error = $this->lang->ai->models->noModelError;
}
}
$this->view->title = $this->lang->ai->chat;
$this->view->messages = $messages;
$this->display();
}
}