/**
  * @param $is_ajax whether the edit is called by ajax or not
  * @param string $u_action phpbb acp-u_action
  */
 private function handle_edit($u_action, $is_ajax = true)
 {
     if (!$is_ajax) {
         $new_tag_name = $this->request->variable('new_tag_name', '');
         if (empty($new_tag_name)) {
             $tag_id = $this->request->variable('tag_id', 0);
             $old_tag_name = $this->tags_manager->get_tag_by_id($tag_id);
             $this->template->assign_vars(array('U_ACTION' => $this->get_tag_link($u_action, $tag_id) . '&action=edit_non_ajax', 'S_EDIT_TAG_NON_AJAX' => true, 'OLD_TAG_NAME' => $old_tag_name));
             return;
         }
         // now new_tag_name and old_tag_name are set and the normal procedure can take place.
     }
     $old_tag_name = $this->request->variable('old_tag_name', '');
     $new_tag_name = $this->request->variable('new_tag_name', '');
     if (empty($old_tag_name) || empty($new_tag_name)) {
         $error_msg = $this->user->lang('TOPICTAGS_MISSING_TAG_NAMES');
         $this->simple_response($u_action, $error_msg, false);
     } else {
         if ($is_ajax) {
             $old_tag_name = rawurldecode(base64_decode($old_tag_name));
             $new_tag_name = rawurldecode(base64_decode($new_tag_name));
         }
         if ($old_tag_name == $new_tag_name) {
             $error_msg = $this->user->lang('TOPICTAGS_NO_MODIFICATION', $old_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         $old_ids = $this->tags_manager->get_existing_tags(array($old_tag_name), true);
         if (empty($old_ids)) {
             $error_msg = $this->user->lang('TOPICTAGS_TAG_DOES_NOT_EXIST', $old_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         // if we reach here, we know that we got a single valid old tag
         $old_id = $old_ids[0];
         $new_tag_name_clean = $this->tags_manager->clean_tag($new_tag_name);
         $is_valid = $this->tags_manager->is_valid_tag($new_tag_name_clean, true);
         if (!$is_valid) {
             $error_msg = $this->user->lang('TOPICTAGS_TAG_INVALID', $new_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         // old tag exist and new tag is valid
         $new_ids = $this->tags_manager->get_existing_tags(array($new_tag_name), true);
         if (!empty($new_ids)) {
             // new tag exist -> merge
             $new_id = $new_ids[0];
             $new_tag_count = $this->tags_manager->merge($old_id, $new_tag_name, $new_id);
             $msg = $this->user->lang('TOPICTAGS_TAG_MERGED', $new_tag_name_clean);
             $this->simple_response($u_action, $msg, true, array('success' => true, 'merged' => true, 'new_tag_count' => $new_tag_count, 'msg' => base64_encode(rawurlencode($msg))));
         }
         // old tag exist and new tag is valid and does not exist -> rename it
         $this->tags_manager->rename($old_id, $new_tag_name_clean);
         $msg = $this->user->lang('TOPICTAGS_TAG_CHANGED');
         $this->simple_response($u_action, $msg);
     }
 }
Ejemplo n.º 2
0
 public function test_is_valid_tag()
 {
     global $table_prefix;
     $this->assertTrue($this->tags_manager->is_valid_tag("tag"));
     // clean tags must be trimed (note the trailing space)
     $this->assertFalse($this->tags_manager->is_valid_tag("tag ", true));
     $this->assertFalse($this->tags_manager->is_valid_tag("ta"), 'tag is too short');
     $this->assertFalse($this->tags_manager->is_valid_tag("abcdefghijabcdefghijabcdefghija", true), 'tag is too long');
     // will be cleaned and thereby trimmed to 30 chars
     $this->assertTrue($this->tags_manager->is_valid_tag("abcdefghijabcdefghijabcdefghija", false));
     // enable blacklist and whitelist
     global $table_prefix;
     $config = new \phpbb\config\config(array(prefixes::CONFIG . '_allowed_tags_regex' => '/^[a-z]{3,30}$/i', prefixes::CONFIG . '_whitelist_enabled' => true, prefixes::CONFIG . '_blacklist_enabled' => true));
     $this->config_text->set_array(array(prefixes::CONFIG . '_blacklist' => json_encode(array("blacktag", "blackwhitetag")), prefixes::CONFIG . '_whitelist' => json_encode(array("whitetag", "blackwhitetag"))));
     $db_helper = new \robertheim\topictags\service\db_helper($this->db);
     $this->tags_manager = new tags_manager($this->db, $config, $this->config_text, $this->auth, $db_helper, $table_prefix);
     $this->assertFalse($this->tags_manager->is_valid_tag("blacktag", true), 'tag is on blacklist');
     $this->assertFalse($this->tags_manager->is_valid_tag("notwhitetag", true), 'tag is not on whitelist');
     $this->assertTrue($this->tags_manager->is_valid_tag("whitetag", true), 'tag is not blacklisted and on whitelist');
     $this->assertFalse($this->tags_manager->is_valid_tag("blackwhitetag", true), 'blacklist must be given priority');
     $this->config_text->delete_array(array(prefixes::CONFIG . '_blacklist', prefixes::CONFIG . '_whitelist'));
 }