[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/inc/plugins/ -> hello.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  // Make sure we can't access this file directly from the browser.
  12  if(!defined('IN_MYBB'))
  13  {
  14      die('This file cannot be accessed directly.');
  15  }
  16  
  17  // cache templates - this is important when it comes to performance
  18  // THIS_SCRIPT is defined by some of the MyBB scripts, including index.php
  19  if(defined('THIS_SCRIPT'))
  20  {
  21      global $templatelist;
  22  
  23      if(isset($templatelist))
  24      {
  25          $templatelist .= ',';
  26      }
  27  
  28      if(THIS_SCRIPT== 'index.php')
  29      {
  30          $templatelist .= 'hello_index, hello_message';
  31      }
  32      elseif(THIS_SCRIPT== 'showthread.php')
  33      {
  34          $templatelist .= 'hello_post, hello_message';
  35      }
  36  }
  37  
  38  if(defined('IN_ADMINCP'))
  39  {
  40      // Add our hello_settings() function to the setting management module to load language strings.
  41      $plugins->add_hook('admin_config_settings_manage', 'hello_settings');
  42      $plugins->add_hook('admin_config_settings_change', 'hello_settings');
  43      $plugins->add_hook('admin_config_settings_start', 'hello_settings');
  44      // We could hook at 'admin_config_settings_begin' only for simplicity sake.
  45  }
  46  else
  47  {
  48      // Add our hello_index() function to the index_start hook so when that hook is run our function is executed
  49      $plugins->add_hook('index_start', 'hello_index');
  50  
  51      // Add our hello_post() function to the postbit hook so it gets executed on every post
  52      $plugins->add_hook('postbit', 'hello_post');
  53  
  54      // Add our hello_new() function to the misc_start hook so our misc.php?action=hello inserts a new message into the created DB table.
  55      $plugins->add_hook('misc_start', 'hello_new');
  56  }
  57  
  58  function hello_info()
  59  {
  60      global $lang;
  61      $lang->load('hello');
  62  
  63      /**
  64       * Array of information about the plugin.
  65       * name: The name of the plugin
  66       * description: Description of what the plugin does
  67       * website: The website the plugin is maintained at (Optional)
  68       * author: The name of the author of the plugin
  69       * authorsite: The URL to the website of the author (Optional)
  70       * version: The version number of the plugin
  71       * compatibility: A CSV list of MyBB versions supported. Ex, '121,123', '12*'. Wildcards supported.
  72       * codename: An unique code name to be used by updated from the official MyBB Mods community.
  73       */
  74      return array(
  75          'name'            => 'Hello World!',
  76          'description'    => $lang->hello_desc,
  77          'website'        => 'https://mybb.com',
  78          'author'        => 'MyBB Group',
  79          'authorsite'    => 'https://mybb.com',
  80          'version'        => '2.0',
  81          'compatibility'    => '18*',
  82          'codename'        => 'hello'
  83      );
  84  }
  85  
  86  /*
  87   * _activate():
  88   *    Called whenever a plugin is activated via the Admin CP. This should essentially make a plugin
  89   *    'visible' by adding templates/template changes, language changes etc.
  90  */
  91  function hello_activate()
  92  {
  93      global $db, $lang;
  94      $lang->load('hello');
  95  
  96      // Add a new template (hello_index) to our global templates (sid = -1)
  97      $templatearray = array(
  98      'index' => '<br />
  99  <table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
 100      <thead>
 101          <tr>
 102              <td class="thead">
 103                  <strong>{$lang->hello}</strong>
 104              </td>
 105          </tr>
 106      </thead>
 107      <tbody>
 108          <tr>
 109              <td class="tcat">
 110                  <form method="POST" action="misc.php">
 111                      <input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
 112                      <input type="hidden" name="action" value="hello" />
 113                      {$lang->hello_add_message}: <input type="text" name="message" class="textbox" /> <input type="submit" name="submit" class="button" value="{$lang->hello_add}" />
 114                  </form>
 115              </td>
 116          </tr>
 117          <tr>
 118              <td class="trow1">
 119                  {$messages}
 120              </td>
 121          </tr>
 122      </tbody>
 123  </table>
 124  <br />',
 125      'post' => '<br /><br /><strong>{$lang->hello}:</strong><br />{$messages}',
 126      'message' => '<br /> - {$message}'
 127      );
 128  
 129      $group = array(
 130          'prefix' => $db->escape_string('hello'),
 131          'title' => $db->escape_string('Hello World!')
 132      );
 133  
 134      // Update or create template group:
 135      $query = $db->simple_select('templategroups', 'prefix', "prefix='{$group['prefix']}'");
 136  
 137      if($db->fetch_field($query, 'prefix'))
 138      {
 139          $db->update_query('templategroups', $group, "prefix='{$group['prefix']}'");
 140      }
 141      else
 142      {
 143          $db->insert_query('templategroups', $group);
 144      }
 145  
 146      // Query already existing templates.
 147      $query = $db->simple_select('templates', 'tid,title,template', "sid=-2 AND (title='{$group['prefix']}' OR title LIKE '{$group['prefix']}=_%' ESCAPE '=')");
 148  
 149      $templates = $duplicates = array();
 150  
 151      while($row = $db->fetch_array($query))
 152      {
 153          $title = $row['title'];
 154          $row['tid'] = (int)$row['tid'];
 155  
 156          if(isset($templates[$title]))
 157          {
 158              // PluginLibrary had a bug that caused duplicated templates.
 159              $duplicates[] = $row['tid'];
 160              $templates[$title]['template'] = false; // force update later
 161          }
 162          else
 163          {
 164              $templates[$title] = $row;
 165          }
 166      }
 167  
 168      // Delete duplicated master templates, if they exist.
 169      if($duplicates)
 170      {
 171          $db->delete_query('templates', 'tid IN ('.implode(",", $duplicates).')');
 172      }
 173  
 174      // Update or create templates.
 175      foreach($templatearray as $name => $code)
 176      {
 177          if(strlen($name))
 178          {
 179              $name = "hello_{$name}";
 180          }
 181          else
 182          {
 183              $name = "hello";
 184          }
 185  
 186          $template = array(
 187              'title' => $db->escape_string($name),
 188              'template' => $db->escape_string($code),
 189              'version' => 1,
 190              'sid' => -2,
 191              'dateline' => TIME_NOW
 192          );
 193  
 194          // Update
 195          if(isset($templates[$name]))
 196          {
 197              if($templates[$name]['template'] !== $code)
 198              {
 199                  // Update version for custom templates if present
 200                  $db->update_query('templates', array('version' => 0), "title='{$template['title']}'");
 201  
 202                  // Update master template
 203                  $db->update_query('templates', $template, "tid={$templates[$name]['tid']}");
 204              }
 205          }
 206          // Create
 207          else
 208          {
 209              $db->insert_query('templates', $template);
 210          }
 211  
 212          // Remove this template from the earlier queried list.
 213          unset($templates[$name]);
 214      }
 215  
 216      // Remove no longer used templates.
 217      foreach($templates as $name => $row)
 218      {
 219          $db->delete_query('templates', "title='{$db->escape_string($name)}'");
 220      }
 221  
 222      // Settings group array details
 223      $group = array(
 224          'name' => 'hello',
 225          'title' => $db->escape_string($lang->setting_group_hello),
 226          'description' => $db->escape_string($lang->setting_group_hello_desc),
 227          'isdefault' => 0
 228      );
 229  
 230      // Check if the group already exists.
 231      $query = $db->simple_select('settinggroups', 'gid', "name='hello'");
 232  
 233      if($gid = (int)$db->fetch_field($query, 'gid'))
 234      {
 235          // We already have a group. Update title and description.
 236          $db->update_query('settinggroups', $group, "gid='{$gid}'");
 237      }
 238      else
 239      {
 240          // We don't have a group. Create one with proper disporder.
 241          $query = $db->simple_select('settinggroups', 'MAX(disporder) AS disporder');
 242          $disporder = (int)$db->fetch_field($query, 'disporder');
 243  
 244          $group['disporder'] = ++$disporder;
 245  
 246          $gid = (int)$db->insert_query('settinggroups', $group);
 247      }
 248  
 249      // Deprecate all the old entries.
 250      $db->update_query('settings', array('description' => 'HELLODELETEMARKER'), "gid='{$gid}'");
 251  
 252      // add settings
 253      $settings = array(
 254      'display1'    => array(
 255          'optionscode'    => 'yesno',
 256          'value'            => 1
 257      ),
 258      'display2'    => array(
 259          'optionscode'    => 'yesno',
 260          'value'            => 1
 261      ));
 262  
 263      $disporder = 0;
 264  
 265      // Create and/or update settings.
 266      foreach($settings as $key => $setting)
 267      {
 268          // Prefix all keys with group name.
 269          $key = "hello_{$key}";
 270  
 271          $lang_var_title = "setting_{$key}";
 272          $lang_var_description = "setting_{$key}_desc";
 273  
 274          $setting['title'] = $lang->{$lang_var_title};
 275          $setting['description'] = $lang->{$lang_var_description};
 276  
 277          // Filter valid entries.
 278          $setting = array_intersect_key($setting,
 279              array(
 280                  'title' => 0,
 281                  'description' => 0,
 282                  'optionscode' => 0,
 283                  'value' => 0,
 284          ));
 285  
 286          // Escape input values.
 287          $setting = array_map(array($db, 'escape_string'), $setting);
 288  
 289          // Add missing default values.
 290          ++$disporder;
 291  
 292          $setting = array_merge(
 293              array('description' => '',
 294                  'optionscode' => 'yesno',
 295                  'value' => 0,
 296                  'disporder' => $disporder),
 297          $setting);
 298  
 299          $setting['name'] = $db->escape_string($key);
 300          $setting['gid'] = $gid;
 301  
 302          // Check if the setting already exists.
 303          $query = $db->simple_select('settings', 'sid', "gid='{$gid}' AND name='{$setting['name']}'");
 304  
 305          if($sid = $db->fetch_field($query, 'sid'))
 306          {
 307              // It exists, update it, but keep value intact.
 308              unset($setting['value']);
 309              $db->update_query('settings', $setting, "sid='{$sid}'");
 310          }
 311          else
 312          {
 313              // It doesn't exist, create it.
 314              $db->insert_query('settings', $setting);
 315              // Maybe use $db->insert_query_multiple somehow
 316          }
 317      }
 318  
 319      // Delete deprecated entries.
 320      $db->delete_query('settings', "gid='{$gid}' AND description='HELLODELETEMARKER'");
 321  
 322      // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
 323      rebuild_settings();
 324  
 325      // Include this file because it is where find_replace_templatesets is defined
 326      require_once  MYBB_ROOT.'inc/adminfunctions_templates.php';
 327  
 328      // Edit the index template and add our variable to above {$forums}
 329      find_replace_templatesets('index', '#'.preg_quote('{$forums}').'#', "{\$hello}\n{\$forums}");
 330  }
 331  
 332  /*
 333   * _deactivate():
 334   *    Called whenever a plugin is deactivated. This should essentially 'hide' the plugin from view
 335   *    by removing templates/template changes etc. It should not, however, remove any information
 336   *    such as tables, fields etc - that should be handled by an _uninstall routine. When a plugin is
 337   *    uninstalled, this routine will also be called before _uninstall() if the plugin is active.
 338  */
 339  function hello_deactivate()
 340  {
 341      require_once  MYBB_ROOT.'inc/adminfunctions_templates.php';
 342  
 343      // remove template edits
 344      find_replace_templatesets('index', '#'.preg_quote('{$hello}').'#', '');
 345  }
 346  
 347  /*
 348   * _install():
 349   *   Called whenever a plugin is installed by clicking the 'Install' button in the plugin manager.
 350   *   If no install routine exists, the install button is not shown and it assumed any work will be
 351   *   performed in the _activate() routine.
 352  */
 353  function hello_install()
 354  {
 355      global $db;
 356  
 357      // Create our table collation
 358      $collation = $db->build_create_table_collation();
 359  
 360      // Create table if it doesn't exist already
 361      if(!$db->table_exists('hello_messages'))
 362      {
 363          switch($db->type)
 364          {
 365              case "pgsql":
 366                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."hello_messages (
 367                      mid serial,
 368                      message varchar(100) NOT NULL default '',
 369                      PRIMARY KEY (mid)
 370                  );");
 371                  break;
 372              case "sqlite":
 373                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."hello_messages (
 374                      mid INTEGER PRIMARY KEY,
 375                      message varchar(100) NOT NULL default ''
 376                  );");
 377                  break;
 378              default:
 379                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."hello_messages (
 380                      mid int unsigned NOT NULL auto_increment,
 381                      message varchar(100) NOT NULL default '',
 382                      PRIMARY KEY (mid)
 383                  ) ENGINE=MyISAM{$collation};");
 384                  break;
 385          }
 386      }
 387  }
 388  
 389  /*
 390   * _is_installed():
 391   *   Called on the plugin management page to establish if a plugin is already installed or not.
 392   *   This should return TRUE if the plugin is installed (by checking tables, fields etc) or FALSE
 393   *   if the plugin is not installed.
 394  */
 395  function hello_is_installed()
 396  {
 397      global $db;
 398  
 399      // If the table exists then it means the plugin is installed because we only drop it on uninstallation
 400      return $db->table_exists('hello_messages');
 401  }
 402  
 403  /*
 404   * _uninstall():
 405   *    Called whenever a plugin is to be uninstalled. This should remove ALL traces of the plugin
 406   *    from the installation (tables etc). If it does not exist, uninstall button is not shown.
 407  */
 408  function hello_uninstall()
 409  {
 410      global $db, $mybb;
 411  
 412      if($mybb->request_method != 'post')
 413      {
 414          global $page, $lang;
 415          $lang->load('hello');
 416  
 417          $page->output_confirm_action('index.php?module=config-plugins&action=deactivate&uninstall=1&plugin=hello', $lang->hello_uninstall_message, $lang->hello_uninstall);
 418      }
 419  
 420      // Delete template groups.
 421      $db->delete_query('templategroups', "prefix='hello'");
 422  
 423      // Delete templates belonging to template groups.
 424      $db->delete_query('templates', "title='hello' OR title LIKE 'hello_%'");
 425  
 426      // Delete settings group
 427      $db->delete_query('settinggroups', "name='hello'");
 428  
 429      // Remove the settings
 430      $db->delete_query('settings', "name IN ('hello_display1','hello_display2')");
 431  
 432      // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
 433      rebuild_settings();
 434  
 435      // Drop tables if desired
 436      if(!isset($mybb->input['no']))
 437      {
 438          $db->drop_table('hello_messages');
 439      }
 440  }
 441  
 442  /*
 443   * Loads the settings language strings.
 444  */
 445  function hello_settings()
 446  {
 447      global $lang;
 448  
 449      // Load our language file
 450      $lang->load('hello');
 451  }
 452  
 453  /*
 454   * Displays the list of messages on index and a form to submit new messages - depending on the setting of course.
 455  */
 456  function hello_index()
 457  {
 458      global $mybb;
 459  
 460      // Only run this function is the setting is set to yes
 461      if($mybb->settings['hello_display1'] == 0)
 462      {
 463          return;
 464      }
 465  
 466      global $db, $lang, $templates, $hello, $theme;
 467  
 468      // Load our language file
 469      $lang->load('hello');
 470  
 471      // Retreive all messages from the database
 472      $messages = '';
 473      $query = $db->simple_select('hello_messages', 'message', '', array('order_by' => 'mid', 'order_dir' => 'DESC'));
 474      while($message = $db->fetch_field($query, 'message'))
 475      {
 476          // htmlspecialchars_uni is similar to PHP's htmlspecialchars but allows unicode
 477          $message = htmlspecialchars_uni($message);
 478          $messages .= eval($templates->render('hello_message'));
 479      }
 480  
 481      // If no messages were found, display that notice.
 482      if(empty($messages))
 483      {
 484          $message = $lang->hello_empty;
 485          $messages = eval($templates->render('hello_message'));
 486      }
 487  
 488      // Set $hello as our template and use eval() to do it so we can have our variables parsed
 489      #eval('$hello = "'.$templates->get('hello_index').'";');
 490      $hello = eval($templates->render('hello_index'));
 491  }
 492  
 493  /*
 494   * Displays the list of messages under every post - depending on the setting.
 495   * @param $post Array containing information about the current post. Note: must be received by reference otherwise our changes are not preserved.
 496  */
 497  function hello_post(&$post)
 498  {
 499      global $settings;
 500  
 501      // Only run this function is the setting is set to yes
 502      if($settings['hello_display2'] == 0)
 503      {
 504          return;
 505      }
 506  
 507      global $lang, $templates;
 508  
 509      // Load our language file
 510      if(!isset($lang->hello))
 511      {
 512          $lang->load('hello');
 513      }
 514  
 515      static $messages;
 516  
 517      // Only retreive messages from the database if they were not retreived already
 518      if(!isset($messages))
 519      {
 520          global $db;
 521  
 522          // Retreive all messages from the database
 523          $messages = '';
 524          $query = $db->simple_select('hello_messages', 'message', '', array('order_by' => 'mid', 'order_dir' => 'DESC'));
 525          while($message = $db->fetch_field($query, 'message'))
 526          {
 527              // htmlspecialchars_uni is similar to PHP's htmlspecialchars but allows unicode
 528              $message = htmlspecialchars_uni($message);
 529              $messages .= eval($templates->render('hello_message'));
 530          }
 531  
 532          // If no messages were found, display that notice.
 533          if(empty($messages))
 534          {
 535              $message = $lang->hello_empty;
 536              $messages = eval($templates->render('hello_message'));
 537          }
 538      }
 539  
 540      // Alter the current post's message
 541      $post['message'] .= eval($templates->render('hello_post'));
 542  }
 543  
 544  /*
 545  * This is where new messages get submitted.
 546  */
 547  function hello_new()
 548  {
 549      global $mybb;
 550  
 551      // If we're not running the 'hello' action as specified in our form, get out of there.
 552      if($mybb->get_input('action') != 'hello')
 553      {
 554          return;
 555      }
 556  
 557      // Only accept POST
 558      if($mybb->request_method != 'post')
 559      {
 560          error_no_permission();
 561      }
 562  
 563      global $lang;
 564  
 565      // Correct post key? This is important to prevent CSRF
 566      verify_post_check($mybb->get_input('my_post_key'));
 567  
 568      // Load our language file
 569      $lang->load('hello');
 570  
 571      $message = trim($mybb->get_input('message'));
 572  
 573      // Message cannot be empty
 574      if(!$message || my_strlen($message) > 100)
 575      {
 576          error($lang->hello_message_empty);
 577      }
 578  
 579      global $db;
 580  
 581      // Escape input data
 582      $message = $db->escape_string($message);
 583  
 584      // Insert into database
 585      $db->insert_query('hello_messages', array('message' => $message));
 586  
 587      // Redirect to index.php with a message
 588      redirect('index.php', $lang->hello_done);
 589  }


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