[ Index ]

PHP Cross Reference of MyBB 1.8.21

title

Body

[close]

/admin/modules/config/ -> mycode.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  // Disallow direct access to this file for security reasons
  12  if(!defined("IN_MYBB"))
  13  {
  14      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  15  }
  16  
  17  $page->add_breadcrumb_item($lang->mycode, "index.php?module=config-mycode");
  18  
  19  $plugins->run_hooks("admin_config_mycode_begin");
  20  
  21  if($mybb->input['action'] == "toggle_status")
  22  {
  23      if(!verify_post_check($mybb->input['my_post_key']))
  24      {
  25          flash_message($lang->invalid_post_verify_key2, 'error');
  26          admin_redirect("index.php?module=config-mycode");
  27      }
  28  
  29      $query = $db->simple_select("mycode", "*", "cid='".$mybb->get_input('cid', MyBB::INPUT_INT)."'");
  30      $mycode = $db->fetch_array($query);
  31  
  32      if(!$mycode['cid'])
  33      {
  34          flash_message($lang->error_invalid_mycode, 'error');
  35          admin_redirect("index.php?module=config-mycode");
  36      }
  37  
  38      $plugins->run_hooks("admin_config_mycode_toggle_status");
  39  
  40      if($mycode['active'] == 1)
  41      {
  42          $new_status = 0;
  43          $phrase = $lang->success_deactivated_mycode;
  44      }
  45      else
  46      {
  47          $new_status = 1;
  48          $phrase = $lang->success_activated_mycode;
  49      }
  50      $mycode_update = array(
  51          'active' => $new_status,
  52      );
  53  
  54      $plugins->run_hooks("admin_config_mycode_toggle_status_commit");
  55  
  56      $db->update_query("mycode", $mycode_update, "cid='{$mycode['cid']}'");
  57  
  58      $cache->update_mycode();
  59  
  60      // Log admin action
  61      log_admin_action($mycode['cid'], $mycode['title'], $new_status);
  62  
  63      flash_message($phrase, 'success');
  64      admin_redirect('index.php?module=config-mycode');
  65  }
  66  
  67  if($mybb->input['action'] == "xmlhttp_test_mycode" && $mybb->request_method == "post")
  68  {
  69      $plugins->run_hooks("admin_config_mycode_xmlhttp_test_mycode_start");
  70  
  71      // Send no cache headers
  72      header("Expires: Sat, 1 Jan 2000 01:00:00 GMT");
  73      header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  74      header("Cache-Control: no-cache, must-revalidate");
  75      header("Pragma: no-cache");
  76      header("Content-type: text/html");
  77  
  78      $sandbox = test_regex($mybb->input['regex'], $mybb->input['replacement'], $mybb->input['test_value']);
  79  
  80      $plugins->run_hooks("admin_config_mycode_xmlhttp_test_mycode_end");
  81  
  82      echo $sandbox['actual'];
  83      exit;
  84  }
  85  
  86  if($mybb->input['action'] == "add")
  87  {
  88      $plugins->run_hooks("admin_config_mycode_add");
  89  
  90      if($mybb->request_method == "post")
  91      {
  92          if(!trim($mybb->input['title']))
  93          {
  94              $errors[] = $lang->error_missing_title;
  95          }
  96  
  97          if(!trim($mybb->input['regex']))
  98          {
  99              $errors[] = $lang->error_missing_regex;
 100          }
 101  
 102          $regex = str_replace("\x0", "", $mybb->input['regex']);
 103  
 104          if(check_existing_regex($regex))
 105          {
 106              $errors[] = $lang->error_regex_already_available;
 107          }
 108  
 109          if(!trim($mybb->input['replacement']))
 110          {
 111              $errors[] = $lang->error_missing_replacement;
 112          }
 113  
 114          if($mybb->input['test'])
 115          {
 116              $errors[] = $lang->changes_not_saved;
 117              $sandbox = test_regex($mybb->input['regex'], $mybb->input['replacement'], $mybb->input['test_value']);
 118          }
 119  
 120          if(!$errors)
 121          {
 122              $new_mycode = array(
 123                  'title'    => $db->escape_string($mybb->input['title']),
 124                  'description' => $db->escape_string($mybb->input['description']),
 125                  'regex' => $db->escape_string($regex),
 126                  'replacement' => $db->escape_string($mybb->input['replacement']),
 127                  'active' => $mybb->get_input('active', MyBB::INPUT_INT),
 128                  'parseorder' => $mybb->get_input('parseorder', MyBB::INPUT_INT)
 129              );
 130  
 131              $cid = $db->insert_query("mycode", $new_mycode);
 132  
 133              $plugins->run_hooks("admin_config_mycode_add_commit");
 134  
 135              $cache->update_mycode();
 136  
 137              // Log admin action
 138              log_admin_action($cid, $mybb->input['title']);
 139  
 140              flash_message($lang->success_added_mycode, 'success');
 141              admin_redirect('index.php?module=config-mycode');
 142          }
 143      }
 144  
 145      $sub_tabs['mycode'] = array(
 146          'title'    => $lang->mycode,
 147          'link' => "index.php?module=config-mycode",
 148          'description' => $lang->mycode_desc
 149      );
 150  
 151      $sub_tabs['add_new_mycode'] = array(
 152          'title'    => $lang->add_new_mycode,
 153          'link' => "index.php?module=config-mycode&amp;action=add",
 154          'description' => $lang->add_new_mycode_desc
 155      );
 156  
 157      $page->extra_header .= "
 158      <script type=\"text/javascript\">
 159      var my_post_key = '".$mybb->post_code."';
 160      lang.mycode_sandbox_test_error = \"{$lang->mycode_sandbox_test_error}\";
 161      </script>";
 162  
 163      $page->add_breadcrumb_item($lang->add_new_mycode);
 164      $page->output_header($lang->custom_mycode." - ".$lang->add_new_mycode);
 165      $page->output_nav_tabs($sub_tabs, 'add_new_mycode');
 166  
 167      if($errors)
 168      {
 169          $page->output_inline_error($errors);
 170      }
 171      else
 172      {
 173          $mybb->input['active'] = 1;
 174      }
 175  
 176      $form = new Form("index.php?module=config-mycode&amp;action=add", "post", "add");
 177      $form_container = new FormContainer($lang->add_mycode);
 178      $form_container->output_row($lang->title." <em>*</em>", '', $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 179      $form_container->output_row($lang->short_description, '', $form->generate_text_box('description', $mybb->input['description'], array('id' => 'description')), 'description');
 180      $form_container->output_row($lang->regular_expression." <em>*</em>", $lang->regular_expression_desc.'<br /><strong>'.$lang->example.'</strong> \[b\](.*?)\[/b\]', $form->generate_text_area('regex', $mybb->input['regex'], array('id' => 'regex')), 'regex');
 181      $form_container->output_row($lang->replacement." <em>*</em>", $lang->replacement_desc.'<br /><strong>'.$lang->example.'</strong> &lt;strong&gt;$1&lt;/strong&gt;', $form->generate_text_area('replacement', $mybb->input['replacement'], array('id' => 'replacement')), 'replacement');
 182      $form_container->output_row($lang->enabled." <em>*</em>", '', $form->generate_yes_no_radio('active', $mybb->input['active']));
 183      $form_container->output_row($lang->parse_order, $lang->parse_order_desc, $form->generate_numeric_field('parseorder', $mybb->input['parseorder'], array('id' => 'parseorder', 'min' => 0)), 'parseorder');
 184      $form_container->end();
 185  
 186      $buttons[] = $form->generate_submit_button($lang->save_mycode);
 187      $form->output_submit_wrapper($buttons);
 188  
 189      // Sandbox
 190      echo "<br />\n";
 191      $form_container = new FormContainer($lang->sandbox);
 192      $form_container->output_row($lang->sandbox_desc);
 193      $form_container->output_row($lang->test_value, $lang->test_value_desc, $form->generate_text_area('test_value', $mybb->input['test_value'], array('id' => 'test_value'))."<br />".$form->generate_submit_button($lang->test, array('id' => 'test', 'name' => 'test')), 'test_value');
 194      $form_container->output_row($lang->result_html, $lang->result_html_desc, $form->generate_text_area('result_html', $sandbox['html'], array('id' => 'result_html', 'disabled' => 1)), 'result_html');
 195      $form_container->output_row($lang->result_actual, $lang->result_actual_desc, "<div id=\"result_actual\">{$sandbox['actual']}</div>");
 196      $form_container->end();
 197      echo '<script type="text/javascript" src="./jscripts/mycode_sandbox.js"></script>';
 198      echo '<script type="text/javascript">
 199  //<![CDATA[
 200  $(function(){
 201      new MyCodeSandbox("./index.php?module=config-mycode&action=xmlhttp_test_mycode", $("#test"), $("#regex"), $("#replacement"), $("#test_value"), $("#result_html"), $("#result_actual"));
 202  });
 203  //]]>
 204  </script>';
 205  
 206      $form->end();
 207  
 208      $page->output_footer();
 209  }
 210  
 211  if($mybb->input['action'] == "edit")
 212  {
 213      $query = $db->simple_select("mycode", "*", "cid='".$mybb->get_input('cid', MyBB::INPUT_INT)."'");
 214      $mycode = $db->fetch_array($query);
 215  
 216      if(!$mycode['cid'])
 217      {
 218          flash_message($lang->error_invalid_mycode, 'error');
 219          admin_redirect("index.php?module=config-mycode");
 220      }
 221  
 222      $plugins->run_hooks("admin_config_mycode_edit");
 223  
 224      if($mybb->request_method == "post")
 225      {
 226          if(!trim($mybb->input['title']))
 227          {
 228              $errors[] = $lang->error_missing_title;
 229          }
 230  
 231          if(!trim($mybb->input['regex']))
 232          {
 233              $errors[] = $lang->error_missing_regex;
 234          }
 235  
 236          $regex = str_replace("\x0", "", $mybb->input['regex']);
 237  
 238          if(check_existing_regex($regex, $mycode))
 239          {
 240              $errors[] = $lang->error_regex_already_available;
 241          }
 242  
 243          if(!trim($mybb->input['replacement']))
 244          {
 245              $errors[] = $lang->error_missing_replacement;
 246          }
 247  
 248          if($mybb->input['test'])
 249          {
 250              $errors[] = $lang->changes_not_saved;
 251              $sandbox = test_regex($mybb->input['regex'], $mybb->input['replacement'], $mybb->input['test_value']);
 252          }
 253  
 254          if(!$errors)
 255          {
 256              $updated_mycode = array(
 257                  'title'    => $db->escape_string($mybb->input['title']),
 258                  'description' => $db->escape_string($mybb->input['description']),
 259                  'regex' => $db->escape_string($regex),
 260                  'replacement' => $db->escape_string($mybb->input['replacement']),
 261                  'active' => $mybb->get_input('active', MyBB::INPUT_INT),
 262                  'parseorder' => $mybb->get_input('parseorder', MyBB::INPUT_INT)
 263              );
 264  
 265              $plugins->run_hooks("admin_config_mycode_edit_commit");
 266  
 267              $db->update_query("mycode", $updated_mycode, "cid='{$mycode['cid']}'");
 268  
 269              $cache->update_mycode();
 270  
 271              // Log admin action
 272              log_admin_action($mycode['cid'], $mybb->input['title']);
 273  
 274              flash_message($lang->success_updated_mycode, 'success');
 275              admin_redirect('index.php?module=config-mycode');
 276          }
 277      }
 278  
 279      $sub_tabs['edit_mycode'] = array(
 280          'title'    => $lang->edit_mycode,
 281          'link' => "index.php?module=config-mycode&amp;action=edit",
 282          'description' => $lang->edit_mycode_desc
 283      );
 284  
 285      $page->extra_header .= "
 286      <script type=\"text/javascript\">
 287      var my_post_key = '".$mybb->post_code."';
 288      lang.mycode_sandbox_test_error = \"{$lang->mycode_sandbox_test_error}\";
 289      </script>";
 290  
 291      $page->add_breadcrumb_item($lang->edit_mycode);
 292      $page->output_header($lang->custom_mycode." - ".$lang->edit_mycode);
 293      $page->output_nav_tabs($sub_tabs, 'edit_mycode');
 294  
 295      $form = new Form("index.php?module=config-mycode&amp;action=edit", "post", "edit");
 296      echo $form->generate_hidden_field('cid', $mycode['cid']);
 297  
 298      if($errors)
 299      {
 300          $page->output_inline_error($errors);
 301      }
 302      else
 303      {
 304          $mybb->input = array_merge($mybb->input, $mycode);
 305      }
 306  
 307      $form_container = new FormContainer($lang->edit_mycode);
 308      $form_container->output_row($lang->title." <em>*</em>", '', $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 309      $form_container->output_row($lang->short_description, '', $form->generate_text_box('description', $mybb->input['description'], array('id' => 'description')), 'description');
 310      $form_container->output_row($lang->regular_expression." <em>*</em>", $lang->regular_expression_desc.'<br /><strong>'.$lang->example.'</strong> \[b\](.*?)\[/b\]', $form->generate_text_area('regex', $mybb->input['regex'], array('id' => 'regex')), 'regex');
 311      $form_container->output_row($lang->replacement." <em>*</em>", $lang->replacement_desc.'<br /><strong>'.$lang->example.'</strong> &lt;strong&gt;$1&lt;/strong&gt;', $form->generate_text_area('replacement', $mybb->input['replacement'], array('id' => 'replacement')), 'replacement');
 312      $form_container->output_row($lang->enabled." <em>*</em>", '', $form->generate_yes_no_radio('active', $mybb->input['active']));
 313      $form_container->output_row($lang->parse_order, $lang->parse_order_desc, $form->generate_numeric_field('parseorder', $mybb->input['parseorder'], array('id' => 'parseorder', 'min' => 0)), 'parseorder');
 314      $form_container->end();
 315  
 316      $buttons[] = $form->generate_submit_button($lang->save_mycode);
 317  
 318      $form->output_submit_wrapper($buttons);
 319  
 320      // Sandbox
 321      echo "<br />\n";
 322      $form_container = new FormContainer($lang->sandbox);
 323      $form_container->output_row($lang->sandbox_desc);
 324      $form_container->output_row($lang->test_value, $lang->test_value_desc, $form->generate_text_area('test_value', $mybb->input['test_value'], array('id' => 'test_value'))."<br />".$form->generate_submit_button($lang->test, array('id' => 'test', 'name' => 'test')), 'test_value');
 325      $form_container->output_row($lang->result_html, $lang->result_html_desc, $form->generate_text_area('result_html', $sandbox['html'], array('id' => 'result_html', 'disabled' => 1)), 'result_html');
 326      $form_container->output_row($lang->result_actual, $lang->result_actual_desc, "<div id=\"result_actual\">{$sandbox['actual']}</div>");
 327      $form_container->end();
 328      echo '<script type="text/javascript" src="./jscripts/mycode_sandbox.js"></script>';
 329      echo '<script type="text/javascript">
 330  
 331  $(function(){
 332  //<![CDATA[
 333      new MyCodeSandbox("./index.php?module=config-mycode&action=xmlhttp_test_mycode", $("#test"), $("#regex"), $("#replacement"), $("#test_value"), $("#result_html"), $("#result_actual"));
 334  });
 335  //]]>
 336  </script>';
 337  
 338      $form->end();
 339  
 340      $page->output_footer();
 341  }
 342  
 343  if($mybb->input['action'] == "delete")
 344  {
 345      $query = $db->simple_select("mycode", "*", "cid='".$mybb->get_input('cid', MyBB::INPUT_INT)."'");
 346      $mycode = $db->fetch_array($query);
 347  
 348      if(!$mycode['cid'])
 349      {
 350          flash_message($lang->error_invalid_mycode, 'error');
 351          admin_redirect("index.php?module=config-mycode");
 352      }
 353  
 354      $plugins->run_hooks("admin_config_mycode_delete");
 355  
 356      // User clicked no
 357      if($mybb->input['no'])
 358      {
 359          admin_redirect("index.php?module=config-mycode");
 360      }
 361  
 362      if($mybb->request_method == "post")
 363      {
 364          $db->delete_query("mycode", "cid='{$mycode['cid']}'");
 365  
 366          $plugins->run_hooks("admin_config_mycode_delete_commit");
 367  
 368          $cache->update_mycode();
 369  
 370          // Log admin action
 371          log_admin_action($mycode['cid'], $mycode['title']);
 372  
 373          flash_message($lang->success_deleted_mycode, 'success');
 374          admin_redirect("index.php?module=config-mycode");
 375      }
 376      else
 377      {
 378          $page->output_confirm_action("index.php?module=config-mycode&amp;action=delete&amp;cid={$mycode['cid']}", $lang->confirm_mycode_deletion);
 379      }
 380  }
 381  
 382  if(!$mybb->input['action'])
 383  {
 384      $plugins->run_hooks("admin_config_mycode_start");
 385  
 386      $page->output_header($lang->custom_mycode);
 387  
 388      $sub_tabs['mycode'] = array(
 389          'title'    => $lang->mycode,
 390          'link' => "index.php?module=config-mycode",
 391          'description' => $lang->mycode_desc
 392      );
 393  
 394      $sub_tabs['add_new_mycode'] = array(
 395          'title'    => $lang->add_new_mycode,
 396          'link' => "index.php?module=config-mycode&amp;action=add"
 397      );
 398  
 399      $page->output_nav_tabs($sub_tabs, 'mycode');
 400  
 401      $table = new Table;
 402      $table->construct_header($lang->title);
 403      $table->construct_header($lang->controls, array('class' => 'align_center', 'width' => 150));
 404  
 405      $query = $db->simple_select("mycode", "*", "", array('order_by' => 'parseorder'));
 406      while($mycode = $db->fetch_array($query))
 407      {
 408          if($mycode['active'] == 1)
 409          {
 410              $phrase = $lang->deactivate_mycode;
 411              $icon = "<img src=\"styles/{$page->style}/images/icons/bullet_on.png\" alt=\"({$lang->alt_enabled})\" title=\"{$lang->alt_enabled}\"  style=\"vertical-align: middle;\" /> ";
 412          }
 413          else
 414          {
 415              $phrase = $lang->activate_mycode;
 416              $icon = "<img src=\"styles/{$page->style}/images/icons/bullet_off.png\" alt=\"({$lang->alt_disabled})\" title=\"{$lang->alt_disabled}\"  style=\"vertical-align: middle;\" /> ";
 417          }
 418  
 419          if($mycode['description'])
 420          {
 421              $mycode['description'] = "<small>".htmlspecialchars_uni($mycode['description'])."</small>";
 422          }
 423  
 424          $table->construct_cell("<div>{$icon}<strong><a href=\"index.php?module=config-mycode&amp;action=edit&amp;cid={$mycode['cid']}\">".htmlspecialchars_uni($mycode['title'])."</a></strong><br />{$mycode['description']}</div>");
 425  
 426          $popup = new PopupMenu("mycode_{$mycode['cid']}", $lang->options);
 427          $popup->add_item($lang->edit_mycode, "index.php?module=config-mycode&amp;action=edit&amp;cid={$mycode['cid']}");
 428          $popup->add_item($phrase, "index.php?module=config-mycode&amp;action=toggle_status&amp;cid={$mycode['cid']}&amp;my_post_key={$mybb->post_code}");
 429          $popup->add_item($lang->delete_mycode, "index.php?module=config-mycode&amp;action=delete&amp;cid={$mycode['cid']}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_mycode_deletion}')");
 430          $table->construct_cell($popup->fetch(), array('class' => 'align_center'));
 431          $table->construct_row();
 432      }
 433  
 434      if($table->num_rows() == 0)
 435      {
 436          $table->construct_cell($lang->no_mycode, array('colspan' => 2));
 437          $table->construct_row();
 438      }
 439  
 440      $table->output($lang->custom_mycode);
 441  
 442      $page->output_footer();
 443  }
 444  
 445  /**
 446   * @param string $regex
 447   * @param string $replacement
 448   * @param string $test
 449   *
 450   * @return array
 451   */
 452  function test_regex($regex, $replacement, $test)
 453  {
 454      $array = array();
 455      $array['actual'] = @preg_replace("#".str_replace("\x0", "", $regex)."#si", $replacement, $test);
 456      $array['html'] = htmlspecialchars_uni($array['actual']);
 457      return $array;
 458  }
 459  
 460  /**
 461   * Checks if a regex is already available
 462   *
 463   * @param string $regex The regex to check
 464   * @param array $current The currently edited MyCode
 465   *
 466   * @return bool True if already available, false otherwise
 467   */
 468  function check_existing_regex($regex='', $current=array())
 469  {
 470      global $cache;
 471  
 472      if(!empty($current) && $current['regex'] == $regex)
 473      {
 474          return false;
 475      }
 476  
 477      $mycodes = $cache->read('mycode');
 478  
 479      foreach($mycodes as $mycode)
 480      {
 481          if($mycode['regex'] == $regex)
 482          {
 483              return true;
 484          }
 485      }
 486  
 487      return false;
 488  }


2005 - 2019 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref 0.7.1