我已经构建了一个OpenCart模块来向网站添加“文章”。我通过克隆“信息”模块然后根据我的需要调整它来完成这项工作。
我已经取得了很多进展 - 几乎完成了我的目标 - 并且我的本地开发服务器上的一切正常。问题是我把它推到了实时服务器上当我输入文章详细信息并按保存(在管理区域中)时,它会立即将我退出,并且根本不会保存任何内容。
我的感觉是,它与我登录时添加的令牌有关,因为当我保存并且我注销时,令牌号(附加到网址)是不同的。
我不太了解php(在C#和.NET中有背景),但一般都了解正在发生的事情。我希望有人可以提供帮助。
文章的控制器文件:(如果需要任何其他文件/信息,请告诉我)
class ControllerCatalogArticle extends Controller {
private $error = array();
public function index() {
$this->load->language('catalog/article');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('catalog/article');
$this->getList();
}
public function insert() {
$this->load->language('catalog/article');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('catalog/article');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
$this->model_catalog_article->addArticle($this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->redirect($this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
}
$this->getForm();
}
public function update() {
$this->load->language('catalog/article');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('catalog/article');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
$this->model_catalog_article->editArticle($this->request->get['article_id'], $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->redirect($this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
}
$this->getForm();
}
public function delete() {
$this->load->language('catalog/article');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('catalog/article');
if (isset($this->request->post['selected']) && $this->validateDelete()) {
foreach ($this->request->post['selected'] as $article_id) {
$this->model_catalog_article->deleteArticle($article_id);
}
$this->session->data['success'] = $this->language->get('text_success');
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->redirect($this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
}
$this->getList();
}
private function getList() {
if (isset($this->request->get['sort'])) {
$sort = $this->request->get['sort'];
} else {
$sort = 'id.title';
}
if (isset($this->request->get['order'])) {
$order = $this->request->get['order'];
} else {
$order = 'ASC';
}
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL'),
'separator' => ' :: '
);
$this->data['insert'] = $this->url->link('catalog/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
$this->data['delete'] = $this->url->link('catalog/article/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
$this->data['articles'] = array();
$data = array(
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $this->config->get('config_admin_limit'),
'limit' => $this->config->get('config_admin_limit')
);
$article_total = $this->model_catalog_article->getTotalArticles();
$results = $this->model_catalog_article->getArticles($data);
$this->load->model('catalog/article_category');
foreach ($results as $result) {
$action = array();
$action[] = array(
'text' => $this->language->get('text_edit'),
'href' => $this->url->link('catalog/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $result['article_id'] . $url, 'SSL')
);
$this->data['articles'][] = array(
'article_id' => $result['article_id'],
'category_id' => $result['category_id'],
'title' => $result['title'],
'sort_order' => $result['sort_order'],
'selected' => isset($this->request->post['selected']) && in_array($result['article_id'], $this->request->post['selected']),
'action' => $action,
'article_category' => $this->model_catalog_article_category->getArticleCategoryTitle($result['category_id'])
);
}
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_no_results'] = $this->language->get('text_no_results');
$this->data['column_title'] = $this->language->get('column_title');
$this->data['column_article_category'] = $this->language->get('column_article_category');
$this->data['column_sort_order'] = $this->language->get('column_sort_order');
$this->data['column_action'] = $this->language->get('column_action');
$this->data['button_insert'] = $this->language->get('button_insert');
$this->data['button_delete'] = $this->language->get('button_delete');
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
if (isset($this->session->data['success'])) {
$this->data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$this->data['success'] = '';
}
$url = '';
if ($order == 'ASC') {
$url .= '&order=DESC';
} else {
$url .= '&order=ASC';
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->data['sort_title'] = $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . '&sort=id.title' . $url, 'SSL');
$this->data['sort_sort_order'] = $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . '&sort=i.sort_order' . $url, 'SSL');
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
$pagination = new Pagination();
$pagination->total = $article_total;
$pagination->page = $page;
$pagination->limit = $this->config->get('config_admin_limit');
$pagination->text = $this->language->get('text_pagination');
$pagination->url = $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
$this->data['pagination'] = $pagination->render();
$this->data['sort'] = $sort;
$this->data['order'] = $order;
$this->template = 'catalog/article_list.tpl';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
private function getForm() {
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_default'] = $this->language->get('text_default');
$this->data['text_enabled'] = $this->language->get('text_enabled');
$this->data['text_disabled'] = $this->language->get('text_disabled');
$this->data['entry_title'] = $this->language->get('entry_title');
$this->data['entry_description'] = $this->language->get('entry_description');
$this->data['entry_meta_title'] = $this->language->get('entry_meta_title');
$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
$this->data['entry_meta_keywords'] = $this->language->get('entry_meta_keywords');
$this->data['entry_abstract'] = $this->language->get('entry_abstract');
$this->data['entry_article_category'] = $this->language->get('entry_article_category');
$this->data['entry_store'] = $this->language->get('entry_store');
$this->data['entry_keyword'] = $this->language->get('entry_keyword');
$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
$this->data['entry_status'] = $this->language->get('entry_status');
$this->data['entry_layout'] = $this->language->get('entry_layout');
$this->data['button_save'] = $this->language->get('button_save');
$this->data['button_cancel'] = $this->language->get('button_cancel');
$this->data['tab_general'] = $this->language->get('tab_general');
$this->data['tab_data'] = $this->language->get('tab_data');
$this->data['tab_design'] = $this->language->get('tab_design');
$this->data['token'] = $this->session->data['token'];
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
if (isset($this->error['title'])) {
$this->data['error_title'] = $this->error['title'];
} else {
$this->data['error_title'] = array();
}
if (isset($this->error['description'])) {
$this->data['error_description'] = $this->error['description'];
} else {
$this->data['error_description'] = array();
}
if (isset($this->error['category'])) {
$this->data['error_category'] = $this->error['category'];
} else {
$this->data['error_category'] = array();
}
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL'),
'separator' => ' :: '
);
if (!isset($this->request->get['article_id'])) {
$this->data['action'] = $this->url->link('catalog/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
} else {
$this->data['action'] = $this->url->link('catalog/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $this->request->get['article_id'] . $url, 'SSL');
}
$this->data['cancel'] = $this->url->link('catalog/article', 'token=' . $this->session->data['token'] . $url, 'SSL');
if (isset($this->request->get['article_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$article_info = $this->model_catalog_article->getArticle($this->request->get['article_id']);
}
$this->load->model('localisation/language');
$this->data['languages'] = $this->model_localisation_language->getLanguages();
if (isset($this->request->post['article_description'])) {
$this->data['article_description'] = $this->request->post['article_description'];
} elseif (isset($this->request->get['article_id'])) {
$this->data['article_description'] = $this->model_catalog_article->getArticleDescriptions($this->request->get['article_id']);
} else {
$this->data['article_description'] = array();
}
$this->load->model('catalog/article');
$this->data['article_categories'] = $this->model_catalog_article->getArticleCategoriesSort();
if (isset($this->request->post['select_categories'])) {
$this->data['category_id'] = $this->request->post['select_categories'];
} elseif (!empty($article_info)) {
$this->data['category_id'] = $article_info['category_id'];
} else {
$this->data['category_id'] = 0;
}
if (isset($this->request->post['status'])) {
$this->data['status'] = $this->request->post['status'];
} elseif (!empty($article_info)) {
$this->data['status'] = $article_info['status'];
} else {
$this->data['status'] = 1;
}
$this->load->model('setting/store');
$this->data['stores'] = $this->model_setting_store->getStores();
if (isset($this->request->post['article_store'])) {
$this->data['article_store'] = $this->request->post['article_store'];
} elseif (isset($this->request->get['article_id'])) {
$this->data['article_store'] = $this->model_catalog_article->getArticleStores($this->request->get['article_id']);
} else {
$this->data['article_store'] = array(0);
}
if (isset($this->request->post['keyword'])) {
$this->data['keyword'] = $this->request->post['keyword'];
} elseif (!empty($article_info)) {
$this->data['keyword'] = $article_info['keyword'];
} else {
$this->data['keyword'] = '';
}
if (isset($this->request->post['sort_order'])) {
$this->data['sort_order'] = $this->request->post['sort_order'];
} elseif (!empty($article_info)) {
$this->data['sort_order'] = $article_info['sort_order'];
} else {
$this->data['sort_order'] = '';
}
if (isset($this->request->post['article_layout'])) {
$this->data['article_layout'] = $this->request->post['article_layout'];
} elseif (isset($this->request->get['article_id'])) {
$this->data['article_layout'] = $this->model_catalog_article->getArticleLayouts($this->request->get['article_id']);
} else {
$this->data['article_layout'] = array();
}
$this->load->model('design/layout');
$this->data['layouts'] = $this->model_design_layout->getLayouts();
$this->template = 'catalog/article_form.tpl';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
private function validateForm() {
if (!$this->user->hasPermission('modify', 'catalog/article')) {
$this->error['warning'] = $this->language->get('error_permission');
}
foreach ($this->request->post['article_description'] as $language_id => $value) {
if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 64)) {
$this->error['title'][$language_id] = $this->language->get('error_title');
}
if (utf8_strlen($value['description']) < 3) {
$this->error['description'][$language_id] = $this->language->get('error_description');
}
}
if ($this->error && !isset($this->error['warning'])) {
$this->error['warning'] = $this->language->get('error_warning');
}
if (!$this->error) {
return true;
} else {
return false;
}
}
private function validateDelete() {
if (!$this->user->hasPermission('modify', 'catalog/article')) {
$this->error['warning'] = $this->language->get('error_permission');
}
$this->load->model('setting/store');
foreach ($this->request->post['selected'] as $article_id) {
if ($this->config->get('config_account_id') == $article_id) {
$this->error['warning'] = $this->language->get('error_account');
}
if ($this->config->get('config_checkout_id') == $article_id) {
$this->error['warning'] = $this->language->get('error_checkout');
}
if ($this->config->get('config_affiliate_id') == $article_id) {
$this->error['warning'] = $this->language->get('error_affiliate');
}
$store_total = $this->model_setting_store->getTotalStoresByArticleId($article_id);
if ($store_total) {
$this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
}
}
if (!$this->error) {
return true;
} else {
return false;
}
}
}