Share Pastebin
Guest
Public paste!

vigge_sWe

By: a guest | Mar 19th, 2010 | Syntax: PHP | Size: 15.47 KB | Hits: 42 | Expires: Never
Copy text to clipboard
  1. <?php
  2. /**
  3. *
  4. * @package - phpbb3 UPS Easy Shop
  5. * @version $Id: functions_shop.php 169 2010-03-04 06:31:32Z Wuerzi $
  6. * @copyright (c) Wuerzi (http://spieleresidenz.de), (c) femu (http://die-muellers.org)
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10.  
  11. /*
  12.  * @ignore
  13. */
  14. if (!defined('IN_PHPBB'))
  15. {
  16.         exit;
  17. }
  18.  
  19.  
  20. /**
  21. * Set shop config value. Creates missing config entry.
  22. */
  23. function set_shop_config($config_name, $config_value)
  24. {
  25.         global $db, $cache, $config, $shop_config;
  26.  
  27.         $sql = 'UPDATE ' . SHOP_CONFIG_TABLE . "
  28.                 SET config_value = '" . $db->sql_escape($config_value) . "'
  29.                 WHERE config_name = '" . $db->sql_escape($config_name) . "'";
  30.         $db->sql_query($sql);
  31.  
  32.         if (!$db->sql_affectedrows() && !isset($shop_config[$config_name]))
  33.         {
  34.                 $sql = 'INSERT INTO ' . SHOP_CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  35.                         'config_name'   => $config_name,
  36.                         'config_value'  => $config_value,
  37.                         ));
  38.                 $db->sql_query($sql);
  39.         }
  40.  
  41.         $shop_config[$config_name] = $config_value;
  42. }
  43.  
  44. class item
  45. {
  46.         var $data;      //holds everything taken from phpbb_items (eg name, description, price, quantity)
  47.         var $notrash = false; //set to true by buy function when instant_used so it isn't removed from inventory
  48.  
  49.         function item($db_info)
  50.         {
  51.                 $this->data     = $db_info;
  52.                 // Here, we load custom config values
  53.                 $this->init();
  54.         }
  55.  
  56.         function init()
  57.         {
  58.                 // Blank by default
  59.         }
  60.  
  61.         function get_actions()
  62.         {
  63.                 global $user, $shop, $db, $config, $shop_config;
  64.  
  65.                 $actions = array();
  66.                 if ($this->data['can_sellback'])
  67.                 {
  68.                         $actions['sellback'] = array(
  69.                                 'name'          => sprintf($user->lang['SHOP_ITEM_SELLBACK'], $shop->cash->format_cash_string($this->data['sell_price']), $config['points_name']),
  70.                                 'confirm'       => sprintf($user->lang['SHOP_ITEM_CONFIRM_SELL'], $this->data['name'], $shop->cash->format_cash_string($this->data['sell_price']), $config['points_name']),
  71.                                 'function'      => 'sellback'
  72.                         );
  73.                 }
  74.  
  75.                 if ($this->data['can_trash'])
  76.                 {
  77.                         $actions['trash'] = array(
  78.                                 'name'          => $user->lang['SHOP_ITEM_TRASH'],
  79.                                 'confirm'       => sprintf($user->lang['SHOP_ITEM_CONFIRM_TRASH'], $this->data['name']),
  80.                                 'function'      => 'trash'
  81.                         );
  82.                 }
  83.  
  84.                 if ($this->data['can_display'] && $config['shop_shelf_size_sig'] != 0)
  85.                 {
  86.                         // Find out how many items are currently on the shelf
  87.                         $sql = 'SELECT *
  88.                                 FROM ' . SHOP_ITEMS_USER_TABLE . '
  89.                                 WHERE user = ' . (int) $user->data['user_id'] . '
  90.                                 AND shelf_display = true';
  91.                         $db->sql_query($sql);
  92.  
  93.                         if ($this->data['shelf_display'])
  94.                         {
  95.                                 $actions['display'] = array(
  96.                                         'name'          => $user->lang['SHOP_ITEM_DISPLAY_REMOVE'],
  97.                                         'confirm'       => false,
  98.                                         'function'      => 'display',
  99.                                 );
  100.                         }
  101.                         elseif ($db->sql_affectedrows() == $config['shop_shelf_size_sig'])
  102.                         {
  103.                                 $actions['display'] = array(
  104.                                         'name'          => $user->lang['SHOP_ITEM_DISPLAY_FULL'],
  105.                                         'confirm'       => false,
  106.                                         'function'      => 'none',
  107.                                 );
  108.                         }
  109.                         else
  110.                         {
  111.                                 $actions['display'] = array(
  112.                                         'name'          => $user->lang['SHOP_ITEM_DISPLAY'],
  113.                                         'confirm'       => false,
  114.                                         'function'      => 'display',
  115.                                 );
  116.                         }
  117.                 }
  118.                 return $actions;
  119.         }
  120.  
  121.         // This removes one instance of an item from the user's inventory.
  122.         function remove_item()
  123.         {
  124.                 global $db, $user, $shop, $phpbb_root_path, $phpEx;
  125.  
  126.                 if ($this->notrash)
  127.                 {
  128.                         return false;
  129.                 }
  130.  
  131.                 $sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
  132.                         SET quantity_user = quantity_user - 1
  133.                         WHERE item=' . (int) $this->data['item'] . '
  134.                                 AND user=' . (int) $user->data['user_id'];
  135.                 $db->sql_query($sql);
  136.  
  137.                 $sql = 'DELETE FROM ' . SHOP_ITEMS_USER_TABLE . '
  138.                         WHERE quantity_user = 0';
  139.                 $db->sql_query($sql);
  140.         }
  141.  
  142.         function action_handler($action, $confirm = true)
  143.         {
  144.                 $actions = $this->get_actions();
  145.  
  146.                 if (!isset($actions[$action]))
  147.                 {
  148.                         return false;
  149.                 }
  150.  
  151.                 if (!$confirm or !$actions[$action]['confirm'] or confirm_box(true))
  152.                 {
  153.                         if ($actions[$action]['function'] == 'none')
  154.                         {
  155.                                 return;
  156.                         }
  157.                         else
  158.                         {
  159.                                 return $this->$actions[$action]['function'](); // Greatest single line of code I've ever written
  160.                         }
  161.                 }
  162.                 else
  163.                 {
  164.                         $s_hidden_fields = build_hidden_fields(array(
  165.                                 'submit'        => true,
  166.                                 )
  167.                         );
  168.  
  169.                         confirm_box(false, $actions[$action]['confirm'], $s_hidden_fields);
  170.                         return false;
  171.                 }
  172.  
  173.         }
  174.  
  175.         function display()
  176.         {
  177.                 global $db, $user;
  178.  
  179.                 if ($this->data['shelf_display'] == 1)
  180.                 {
  181.                         $this->data['shelf_display'] = 0;
  182.                 }
  183.                 else
  184.                 {
  185.                         $this->data['shelf_display'] = 1;
  186.                 }
  187.  
  188.                 $sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
  189.                         SET shelf_display=' . $this->data['shelf_display'] . '
  190.                         WHERE item=' . (int) $this->data['item'] . '
  191.                                 AND user=' . (int) $user->data['user_id'];
  192.                 $db->sql_query($sql);
  193.  
  194.                 return sprintf(($this->data['shelf_display'] == 1)?$user->lang['SHOP_ITEM_SHELF_ADDED']:$user->lang['SHOP_ITEM_SHELF_REMOVED'], $this->data['name']);
  195.         }
  196.  
  197.         function trash()
  198.         {
  199.                 global $db, $user, $shop, $phpbb_root_path, $phpEx;
  200.  
  201.                 $this->remove_item();
  202.                 return sprintf($user->lang['SHOP_ITEM_TRASHED'], $this->data['name']);
  203.         }
  204.  
  205.         function sellback()
  206.         {
  207.                 global $db, $user, $shop, $phpbb_root_path, $phpEx;
  208.  
  209.                 // This part is exactly like throwing the item out
  210.                 $this->remove_item();
  211.                 // But then, they get points!!!
  212.                 $shop->cash->deposit_user($this->data['sell_price']);
  213.                 // And put it back in the shop
  214.                 $sql = 'UPDATE ' . SHOP_ITEMS_TABLE . '
  215.                         SET quantity = quantity + 1
  216.                         WHERE item=' . (int) $this->data['item'];
  217.                 $db->sql_query($sql);
  218.  
  219.                 return sprintf($user->lang['SHOP_ITEM_SOLD'], $this->data['name']);
  220.         }
  221.  
  222.         // This function sees if the user is able to buy and item and charges the user if so
  223.         // it doesn't move the item from the shop to the user's inventory
  224.         // if item can't be bought, it won't return
  225.         function prebuy()
  226.         {
  227.                 global $user, $shop, $db, $phpEx, $phpbb_root_path, $config, $shop_config;
  228.  
  229.                 // Eventually, we'll have an auth check here too.  for now, all registered users can use the shop
  230.                 if (!$user->data['is_registered'])
  231.                 {
  232.                         $db->sql_transaction('rollback');
  233.                         trigger_error('SHOP_NOT_PERMITTED');
  234.                 }
  235.  
  236.                 if (!$this->data['quantity'])
  237.                 {
  238.                         $db->sql_transaction('rollback');
  239.                         $meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
  240.                         meta_refresh(3, $meta_info);
  241.                         $message = $user->lang['SHOP_SOLD_OUT'] . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
  242.                         trigger_error($message);
  243.                 }
  244.  
  245.                 // Charge the user
  246.                 if (!$shop->cash->charge_user($this->data['price']))
  247.                 {
  248.                         $db->sql_transaction('rollback');
  249.                         $meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
  250.                         meta_refresh(3, $meta_info);
  251.                         $message = sprintf($user->lang['SHOP_INSUFFICIENT_FUNDS'], $config['points_name']) . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
  252.                         trigger_error($message);
  253.                 }
  254.  
  255.                 // Decrease the quantity in the shop
  256.                 $sql = 'UPDATE ' . SHOP_ITEMS_TABLE . '
  257.                         SET quantity = quantity - 1
  258.                         WHERE item=' . (int) $this->data['item'];
  259.                 $db->sql_query($sql);
  260.         }
  261.  
  262.         function use_item()
  263.         {
  264.                 // We only define this function so the program doesn't crash
  265.                 // if somebody decides to make an item that doesn't have a use action set
  266.                 // an instant use item
  267.                 return false;
  268.         }
  269.  
  270.         function buy()
  271.         {
  272.                 global $template, $user, $shop, $db, $phpEx, $phpbb_root_path, $config, $shop_config;
  273.                 $db->sql_transaction('begin');
  274.                 $this->prebuy(); // Returns if purchase is allowed, user was charged, and quantity was decreased
  275.  
  276.                 // Read out config data
  277.                 $sql = 'SELECT config_name, config_value
  278.                         FROM ' . SHOP_CONFIG_TABLE;
  279.                 $result = $db->sql_query($sql);
  280.  
  281.                 while ($row = $db->sql_fetchrow($result))
  282.                 {
  283.                         $shop_config[$row['config_name']] = $row['config_value'];
  284.                 }
  285.                 $db->sql_freeresult($result);
  286.  
  287.                 // Send PM to user if that option is on
  288.                 if ($shop_config['shop_pm_on_buy'] == 1)
  289.                 {
  290.                         require($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
  291.  
  292.                         $my_subject = utf8_normalize_nfc($user->lang['SHOP_ITEM_PM_SUBJECT']);
  293.                         $my_text = utf8_normalize_nfc(sprintf($user->lang['SHOP_ITEM_PM_MESSAGE'], $user->data['username'], $this->data['name'], $shop->cash->format_cash_string($this->data['price']), $config['points_name'], $config['shop_name']));
  294.  
  295.                         $pm_admin = $shop_config['shop_pm_admin'];
  296.  
  297.                         $poll = $uid = $bitfield = $options = '';
  298.                         generate_text_for_storage($my_subject, $uid, $bitfield, $options, false, false, false);
  299.                         generate_text_for_storage($my_text, $uid, $bitfield, $options, true, true, true);
  300.  
  301.                         $data = array(
  302.                         'address_list'          => array ('u' => array($user->data['user_id'] => 'to')),
  303.                         'from_user_id'          => $pm_admin,
  304.                         'from_username'         => $user->data['username'],
  305.                         'icon_id'                       => 0,
  306.                         'from_user_ip'          => '',
  307.  
  308.                         'enable_bbcode'         => true,
  309.                         'enable_smilies'        => true,
  310.                         'enable_urls'           => true,
  311.                         'enable_sig'            => true,
  312.  
  313.                         'message'                       => $my_text,
  314.                         'bbcode_bitfield'       => $bitfield,
  315.                         'bbcode_uid'            => $uid,
  316.                         );
  317.  
  318.                         submit_pm('post', $my_subject, $data, false);
  319.                 }
  320.  
  321.                 if ($this->data['instant_use'])
  322.                 {
  323.                         $this->notrash = true;
  324.                         // This way, if it returns false, we still buy the item like normal
  325.                         if ($message = $this->use_item())
  326.                         {
  327.                                 $meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
  328.                                 meta_refresh(3, $meta_info);
  329.                                 $message = $message . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
  330.                                 trigger_error($message);
  331.                         }
  332.                 }
  333.  
  334.                 $sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
  335.                         SET quantity_user = quantity_user + 1
  336.                         WHERE item=' . (int) $this->data['item'] . '
  337.                                 AND user=' . (int) $user->data['user_id'];
  338.                 $db->sql_query($sql);
  339.  
  340.                 if ($db->sql_affectedrows() != 1)
  341.                 {
  342.                         $sql = 'INSERT INTO ' . SHOP_ITEMS_USER_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  343.                                 'item' => $this->data['item'],
  344.                                 'user' => $user->data['user_id'],
  345.                                 'quantity_user' => 1));
  346.                         $db->sql_query($sql);
  347.                 }
  348.                 $db->sql_transaction('commit');
  349.  
  350.                 // Everything worked, send a success message
  351.                 $meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
  352.                 meta_refresh(3, $meta_info);
  353.                 $message = sprintf($user->lang['SHOP_ITEM_BOUGHT'], $this->data['name']) . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
  354.                 trigger_error($message);
  355.         }
  356. }
  357.  
  358. class shop
  359. {
  360.         var $cash;
  361.         // Constructor
  362.         function shop()
  363.         {
  364.                 global $config;
  365.  
  366.                 // Select money system
  367.                 $this->cash = new ups_points;
  368.         }
  369.  
  370.         function get_item_by_row($row)
  371.         {
  372.                 global $phpbb_root_path, $phpEx;
  373.  
  374.                 include_once($phpbb_root_path . 'includes/shop/items/' . $row['backend'] . '.' . $phpEx);
  375.  
  376.                 return new $row['backend']($row);
  377.         }
  378.  
  379.         function get_item($item_id, $condition = false)
  380.         {
  381.                 global $db;
  382.  
  383.                 $condition = $condition?' && ' . $condition:'';
  384.  
  385.                 $sql = 'SELECT *
  386.                         FROM ' . SHOP_ITEMS_TABLE . '
  387.                         WHERE item=' . (int) $item_id . $condition;
  388.                 $result = $db->sql_query($sql);
  389.  
  390.                 if ($db->sql_affectedrows() != 1)
  391.                 {
  392.                         return false;
  393.                 }
  394.                 return  $this->get_item_by_row($db->sql_fetchrow($result));
  395.         }
  396.  
  397.         function get_item_list($condition = false)
  398.         {
  399.                 global $db, $shop_config;
  400.  
  401.                 // Read out config data
  402.                 $sql = 'SELECT config_name, config_value
  403.                         FROM ' . SHOP_CONFIG_TABLE;
  404.                 $result = $db->sql_query($sql);
  405.  
  406.                 while ($row = $db->sql_fetchrow($result))
  407.                 {
  408.                         $shop_config[$row['config_name']] = $row['config_value'];
  409.                 }
  410.                 $db->sql_freeresult($result);
  411.  
  412.                 $start          = request_var('start', 0);
  413.                 $number         = $shop_config['items_per_page'];
  414.                 $condition      = $condition?' WHERE ' . $condition:'';
  415.  
  416.                 $sql = 'SELECT *
  417.                         FROM ' . SHOP_ITEMS_TABLE . $condition . '
  418.                         ORDER BY item_order';
  419.                 $result = $db->sql_query_limit($sql, $number, $start);
  420.  
  421.                 $items = array();
  422.  
  423.                 if ($db->sql_affectedrows() != 0)
  424.                 {
  425.                         while($item_row = $db->sql_fetchrow($result))
  426.                         {
  427.                                 $items[] = $this->get_item_by_row($item_row);
  428.                         }
  429.                         return $items;
  430.                 }
  431.                 else
  432.                 {
  433.                         return false;
  434.                 }
  435.         }
  436.  
  437.         function get_item_user($item_id, $user_id = NULL,  $condition = false)
  438.         {
  439.                 global $db, $user;
  440.  
  441.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  442.                 $condition = $condition?' && ' . $condition:'';
  443.  
  444.                 $sql = 'SELECT *
  445.                         FROM ( ' . SHOP_ITEMS_TABLE . '
  446.                         INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
  447.                                 ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
  448.                         WHERE user = ' . (int) $user_id . '
  449.                                 AND item_user=' . (int) $item_id . $condition;
  450.                 $result = $db->sql_query($sql);
  451.  
  452.                 if ($db->sql_affectedrows() != 1)
  453.                 {
  454.                         return false;
  455.                 }
  456.                 return $this->get_item_by_row($db->sql_fetchrow($result));
  457.         }
  458.  
  459.         function get_item_list_ucp($user_id = NULL, $condition = false)
  460.         {
  461.                 global $db, $user;
  462.  
  463.                 // Read out config data
  464.                 $sql = 'SELECT config_name, config_value
  465.                         FROM ' . SHOP_CONFIG_TABLE;
  466.                 $result = $db->sql_query($sql);
  467.  
  468.                 while ($row = $db->sql_fetchrow($result))
  469.                 {
  470.                         $shop_config[$row['config_name']] = $row['config_value'];
  471.                 }
  472.                 $db->sql_freeresult($result);
  473.  
  474.                 $start          = request_var('start', 0);
  475.                 $number         = $shop_config['items_per_page'];
  476.  
  477.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  478.                 $condition = $condition?' && ' . $condition:'';
  479.  
  480.                 $sql = 'SELECT *
  481.                         FROM ( ' . SHOP_ITEMS_TABLE . '
  482.                         INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
  483.                                 ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
  484.                         WHERE user = ' . (int) $user_id . $condition . '
  485.                         ORDER BY item_order';
  486.                 $result = $db->sql_query_limit($sql, $number, $start);
  487.  
  488.                 $items = array();
  489.  
  490.                 if ($db->sql_affectedrows() != 0)
  491.                 {
  492.                         while ($item_row = $db->sql_fetchrow($result))
  493.                         {
  494.                                 $items[] = $this->get_item_by_row($item_row);
  495.                         }
  496.                         return $items;
  497.                 }
  498.                 else
  499.                 {
  500.                         return false;
  501.                 }
  502.         }
  503.  
  504.         function get_item_list_user($user_id = NULL, $condition = false)
  505.         {
  506.                 global $db, $user;
  507.  
  508.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  509.                 $condition = $condition?' && ' . $condition:'';
  510.  
  511.                 $sql = 'SELECT *
  512.                         FROM ( ' . SHOP_ITEMS_TABLE . '
  513.                         INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
  514.                                 ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
  515.                         WHERE user = ' . (int) $user_id . $condition . '
  516.                         ORDER BY item_order';
  517.                 $result = $db->sql_query($sql);
  518.  
  519.                 $items = array();
  520.  
  521.                 if ($db->sql_affectedrows() != 0)
  522.                 {
  523.                         while ($item_row = $db->sql_fetchrow($result))
  524.                         {
  525.                                 $items[] = $this->get_item_by_row($item_row);
  526.                         }
  527.                         return $items;
  528.                 }
  529.                 else
  530.                 {
  531.                         return false;
  532.                 }
  533.         }
  534. }
  535.  
  536.  
  537. class ups_points
  538. {
  539.         // Constructor
  540.         function ups_points()
  541.         {
  542.                 global $phpbb_root_path, $phpEx;
  543.  
  544.                 if (!class_exists('ups_points'))
  545.                 {
  546.                         require($phpbb_root_path . 'includes/points/functions_points.' . $phpEx);
  547.                 }
  548.                 $this->money_on = true;
  549.         }
  550.  
  551.         function format_cash_string($amount = NULL)
  552.         {
  553.                 $amount = is_null($amount) ? $this->user_get_cash_amount() : $amount;
  554.                 return number_format_points($amount);
  555.         }
  556.  
  557.         function deposit_user($amount, $user_id = NULL)
  558.         {
  559.                 global $user;
  560.  
  561.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  562.                 add_points($user_id, $amount);
  563.                 return true;
  564.         }
  565.  
  566.         function charge_user($amount, $user_id = NULL)
  567.         {
  568.                 global $user;
  569.  
  570.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  571.  
  572.                 if ($amount > $user->data['user_points'])
  573.                 {
  574.                         return false;
  575.                 }
  576.                 substract_points($user_id, $amount);
  577.                 return true;
  578.         }
  579.  
  580.         function user_get_cash_amount($user_id = NULL)
  581.         {
  582.                 global $user;
  583.  
  584.                 $user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
  585.                 return $user->data['user_points'];
  586.         }
  587. }
  588.  
  589. ?>