Karbowiak

Untitled

Sep 5th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * Application Form extension for the phpBB Forum Software package.
  5. *
  6. * @copyright (c) 2016 Rich McGirr (RMcGirr83)
  7. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  8. *
  9. */
  10.  
  11. namespace rmcgirr83\applicationform\controller;
  12.  
  13. use phpbb\exception\http_exception;
  14.  
  15. /**
  16. * Main controller
  17. */
  18. class main_controller
  19. {
  20.     /** @var \phpbb\config\config */
  21.     protected $config;
  22.  
  23.     /** @var \phpbb\db\driver\driver */
  24.     protected $db;
  25.  
  26.     /** @var \phpbb\controller\helper */
  27.     protected $helper;
  28.  
  29.     /* @var \phpbb\request\request */
  30.     protected $request;
  31.  
  32.     /** @var \phpbb\template\template */
  33.     protected $template;
  34.  
  35.     /** @var \phpbb\user */
  36.     protected $user;
  37.  
  38.     /** @var string phpBB root path */
  39.     protected $root_path;
  40.  
  41.     /** @var string phpEx */
  42.     protected $php_ext;
  43.  
  44.     /**
  45.     * Constructor
  46.     *
  47.     * @param \phpbb\config\config               $config         Config object
  48.     * @param \phpbb\db\driver\driver            $db             Database object
  49.     * @param \phpbb\controller\helper           $helper         Controller helper object
  50.     * @param \phpbb\request\request             $request        Request object
  51.     * @param \phpbb\template\template           $template       Template object
  52.     * @param \phpbb\user                        $user           User object
  53.     * @param string                             $root_path      phpBB root path
  54.     * @param string                             $php_ext        phpEx
  55.     * @access public
  56.     */
  57.     public function __construct(
  58.             \phpbb\config\config $config,
  59.             \phpbb\db\driver\driver_interface $db,
  60.             \phpbb\controller\helper $helper,
  61.             \phpbb\request\request $request,
  62.             \phpbb\template\template $template,
  63.             \phpbb\user $user,
  64.             $root_path,
  65.             $php_ext)
  66.     {
  67.         $this->config = $config;
  68.         $this->db = $db;
  69.         $this->helper = $helper;
  70.         $this->request = $request;
  71.         $this->template = $template;
  72.         $this->user = $user;
  73.         $this->root_path = $root_path;
  74.         $this->php_ext = $php_ext;
  75.  
  76.         include_once($this->root_path . 'includes/functions_posting.' . $this->php_ext);
  77.     }
  78.  
  79.     /**
  80.      * Display the form
  81.      *
  82.      * @access public
  83.      */
  84.     public function displayform()
  85.     {
  86.         $this->user->add_lang_ext('rmcgirr83/applicationform', 'application');
  87.         // user can't be a guest and can't be a bot
  88.         if ($this->user->data['is_bot'] || $this->user->data['user_id'] == ANONYMOUS)
  89.         {
  90.             throw new http_exception(401, 'LOGIN_APPLICATION_FORM');
  91.         }
  92.         add_form_key('appform');
  93.  
  94.         if ($this->request->is_set_post('submit'))
  95.         {
  96.             // Test if form key is valid
  97.             if (!check_form_key('appform'))
  98.             {
  99.                 trigger_error($this->user->lang['FORM_INVALID'], E_USER_WARNING);
  100.             }
  101.             if (utf8_clean_string($this->request->variable('name', '')) === '' || utf8_clean_string($this->request->variable('why', '')) === '')
  102.             {
  103.                 trigger_error($this->user->lang['APP_NOT_COMPLETELY_FILLED'], E_USER_WARNING);
  104.             }
  105.  
  106.             $sql = 'SELECT forum_name
  107.                 FROM ' . FORUMS_TABLE . '
  108.                 WHERE forum_id = ' . (int) $this->config['appform_forum_id'];
  109.             $result = $this->db->sql_query($sql);
  110.             $forum_name = $this->db->sql_fetchfield('forum_name');
  111.             $this->db->sql_freeresult($result);
  112.  
  113.             // Setting the variables we need to submit the post to the forum where all the applications come in
  114.             $subject    = sprintf($this->user->lang['APPLICATION_SUBJECT'], $this->user->data['username']);
  115.             $apply_post = sprintf(
  116.                 $this->user->lang['APPLICATION_MESSAGE'],
  117.                 get_username_string('full', $this->user->data['user_id'], $this->user->data['username'], $this->user->data['user_colour']),
  118.                 utf8_normalize_nfc($this->request->variable('name', '', true)),
  119.                 $this->user->data['user_email'],
  120.                 $this->request->variable('postion', '', true),
  121.                 // Add more here, for example
  122.                 //$this->request->variable('MYVARIABLE', '', true),
  123.                 utf8_normalize_nfc($this->request->variable('why', '', true))
  124.             );
  125.  
  126.             // variables to hold the parameters for submit_post
  127.             $uid = $bitfield = $options = '';
  128.  
  129.             generate_text_for_storage($apply_post, $uid, $bitfield, $options, true, true, true);
  130.  
  131.             $data = array(
  132.                 'forum_id'      => $this->config['appform_forum_id'],
  133.                 'icon_id'       => false,
  134.                 'poster_id'     => $this->user->data['user_id'],
  135.                 'enable_bbcode'     => true,
  136.                 'enable_smilies'    => true,
  137.                 'enable_urls'       => true,
  138.                 'enable_sig'        => true,
  139.  
  140.                 'message'           => $apply_post,
  141.                 'message_md5'       => md5($apply_post),
  142.  
  143.                 'bbcode_bitfield'   => $bitfield,
  144.                 'bbcode_uid'        => $uid,
  145.                 'poster_ip'         => $this->user->ip,
  146.  
  147.                 'post_edit_locked'  => 0,
  148.                 'topic_title'       => $subject,
  149.                 'notify_set'        => false,
  150.                 'notify'            => false,
  151.                 'post_time'         => time(),
  152.                 'forum_name'        => $forum_name,
  153.                 'enable_indexing'   => true,
  154.                 'force_approved_state'  => true,
  155.                 'force_visibility' => true,
  156.             );
  157.             $poll = array();
  158.  
  159.             // Submit the post!
  160.             submit_post('post', $subject, $this->user->data['username'], POST_NORMAL, $poll, $data);
  161.  
  162.             $message = $this->user->lang['APPLICATION_SEND'];
  163.             $message = $message . '<br /><br />' . sprintf($this->user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$this->root_path}index.$this->php_ext") . '">', '</a>');
  164.             trigger_error($message);
  165.         }
  166.         $this->template->assign_vars(array(
  167.             'APPLICATION_POSITIONS' => $this->display_positions(explode("\n", $this->config['appform_positions'])),
  168.         ));
  169.  
  170.         // Send all data to the template file
  171.         return $this->helper->render('appform_body.html', $this->user->lang('APPLICATION_PAGETITLE'));
  172.     }
  173.  
  174.     private function display_positions($input_ary)
  175.     {
  176.         // only accept arrays, no empty ones
  177.         if (!is_array($input_ary) || !sizeof($input_ary))
  178.         {
  179.             return;
  180.         }
  181.         $select = '';
  182.         foreach ($input_ary as $item)
  183.         {
  184.             $select .= '<option value="' . $item . '">' . $item . '</option>';
  185.         }
  186.         return $select;
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment