Exemple #1
0
 public function generate(&$pack, &$order, $quantity, &$serials)
 {
     if (!isset($pack->seriesgen)) {
         return;
     }
     parent::pluginParams($pack->seriesgen);
     $offset = 1;
     if (!empty($order->hikaserial->formData['start_from'])) {
         $offset = (int) $order->hikaserial->formData['start_from'];
     } else {
         $this->db->setQuery('SELECT MAX(a.serial_data * 1) FROM ' . hikaserial::table('serial') . ' AS a WHERE serial_pack_id = ' . $pack->pack_id);
         $offset = (int) $this->db->loadResult() + 1;
     }
     if (!empty($this->plugin_params->min_value) && $offset < (int) $this->plugin_params->min_value) {
         $offset = (int) $this->plugin_params->min_value;
     }
     $max = -1;
     if (!empty($this->plugin_params->max_value) && (int) $this->plugin_params->max_value > 0 && (int) $this->plugin_params->min_value < (int) $this->plugin_params->max_value) {
         $max = (int) $this->plugin_params->max_value;
     }
     if ($max > 0 && $offset > $max) {
         return false;
     }
     for ($q = 0; $q < $quantity; $q++) {
         $serials[] = $offset++;
         if ($max > 0 && $offset > $max) {
             break;
         }
     }
     return true;
 }
Exemple #2
0
 public function onAfterSerialConsume(&$serial)
 {
     $user = hikaserial::loadUser(true);
     $cms_id = (int) @$user->user_cms_id;
     $serial_user = null;
     $ids = array();
     if (!empty($serial->serial_user_id)) {
         $userClass = hikaserial::get('shop.class.user');
         $serial_user = $userClass->get($serial->serial_user_id);
     }
     parent::listPlugins('pointsconsumer', $ids, false);
     foreach ($ids as $id) {
         parent::pluginParams($id);
         if (in_array($serial->serial_pack_id, $this->plugin_params->packs_id)) {
             $points = (int) $this->plugin_params->value;
             if (!empty($this->plugin_params->dynamic_value) && !empty($serial->serial_extradata['points_value'])) {
                 $points += (int) $serial->serial_extradata['points_value'];
             }
             $user_id = $cms_id;
             if (!empty($this->plugin_params->target_serial_user) && !empty($serial_user->serial_user_id)) {
                 $user_id = $serial_user->user_cms_id;
             }
             $this->givePoints($cms_id, $points);
         }
     }
 }
Exemple #3
0
 public function generate(&$pack, &$order, $quantity, &$serials)
 {
     parent::pluginParams(0, 'secureebook');
     $product_code = '';
     if (!empty($order->cart->products)) {
         foreach ($order->cart->products as $p) {
             if ($p->product_id == $pack->product_id) {
                 $product_code = $p->order_product_code;
                 break;
             }
         }
     }
     if (empty($product_code)) {
         return;
     }
     $data = '<' . '?xml version="1.0"?' . '>' . "\r\n" . '<request vendorcode="' . @$this->plugin_params->vendor_code . '" secureebookproductcode="' . $this->tobool(@$this->plugin_params->remote_product_code) . '" userdata="' . $order->order_id . '" testmode="' . $this->tobool(@$this->plugin_params->test_mode) . '">' . "\r\n" . '<product code="' . $product_code . '" qty="' . $quantity . '" />' . "\r\n" . '<orderinfo name="' . @$order->customer->name . '" email="' . @$order->customer->user_email . '" total="' . @$order->order_full_price . '" />' . "\r\n" . '</request>';
     $result = $this->send('xml=' . urlencode($data));
     $token = $this->extractElems('token', $result, true);
     if (isset($token[0]['token'])) {
         $token = $token[0]['token'];
     } else {
         $app = JFactory::getApplication();
         $app->enqueueMessage(JText::sprintf('SECURE_EBOOK_ERROR', 'no token. Invalid request for product [' . $product_code . ']'));
         return false;
     }
     $data = '<' . '?xml version="1.0"?' . '>' . "\r\n" . '<confirm code="' . strtolower(md5($token . $this->plugin_params->sdk_key)) . '"/>';
     $result = $this->send('xml=' . urlencode($data));
     if (strpos($result, '<success') !== false) {
         $keys = $this->extractElems('key', $result, true);
         $links = $this->extractElems('link', $result, true);
         $books = array();
         foreach ($keys as $key) {
             $code = $key['productcode'];
             if (!isset($books[$code])) {
                 $books[$code] = array();
             }
             $books[$code][] = array('key' => $key['key']);
         }
         foreach ($links as $link) {
             $code = $link['productcode'];
             if (isset($books[$code])) {
                 foreach ($books[$code] as $i => $v) {
                     $books[$code][$i]['link'] = $link['url'];
                 }
             }
         }
         foreach ($books as $book) {
             foreach ($book as $b) {
                 $serials[] = serialize($b);
             }
         }
     } else {
         $error = $this->extractContent('error', $result);
         $app = JFactory::getApplication();
         $app->enqueueMessage(JText::sprintf('SECURE_EBOOK_ERROR', $error[0]));
         return false;
     }
 }
Exemple #4
0
 public function onAfterSerialConsume(&$serial)
 {
     $user = hikaserial::loadUser(true);
     $cms_id = $user->user_cms_id;
     $ids = array();
     parent::listPlugins('groupconsumer', $ids, false);
     foreach ($ids as $id) {
         parent::pluginParams($id);
         if ($this->plugin_params->pack_id == $serial->serial_pack_id) {
             $this->updateGroup($cms_id, $this->plugin_params->group_id);
         }
     }
 }
 public function onBeforeSerialMailSend(&$mail, &$mailer, &$serials, $order)
 {
     $ids = array();
     parent::listPlugins('mailtocustomfield', $ids, false);
     if (empty($ids)) {
         return;
     }
     $fullOrder = null;
     foreach ($ids as $id) {
         parent::pluginParams($id);
         $customfield_type = null;
         foreach ($serials as $k => $serial) {
             if (is_string($this->plugin_params->packs) && strpos(',' . $this->plugin_params->packs . ',', ',' . $serial->serial_pack_id . ',') === false) {
                 continue;
             }
             if (is_array($this->plugin_params->packs) && !in_array($serial->serial_pack_id, $this->plugin_params->packs)) {
                 continue;
             }
             $custom_field = $this->plugin_params->custom_field;
             if (empty($custom_field)) {
                 continue;
             }
             list($customfield_type, $custom_field) = explode('.', $custom_field, 2);
             if ($fullOrder === null) {
                 $orderClass = hikaserial::get('shop.class.order');
                 $fullOrder = $orderClass->loadFullOrder($order->order_id, true, false);
             }
             if ($customfield_type == 'order' && !isset($fullOrder->{$custom_field})) {
                 continue;
             }
             if ($customfield_type == 'item') {
                 $f = false;
                 foreach ($fullOrder->products as $k => $product) {
                     if (isset($product->{$custom_field})) {
                         $f = true;
                         break;
                     }
                 }
                 if (!$f) {
                     continue;
                 }
             }
             if ($customfield_type == 'item') {
                 $this->sendCustomEmail($serial, $fullOrder);
             } else {
                 $this->sendCustomEmail($serials, $fullOrder);
                 break;
             }
         }
     }
 }
 public function onBeforeSerialConsume(&$serial, $user_id, &$do, &$extra_data)
 {
     $user = hikaserial::loadUser(true);
     $ids = array();
     parent::listPlugins('groupfilterconsumer', $ids, false);
     if (!$do) {
         return;
     }
     foreach ($ids as $id) {
         parent::pluginParams($id);
         if (in_array($serial->serial_pack_id, $this->plugin_params->packs_id)) {
             if (empty($user) || empty($user->user_cms_id)) {
                 $do = false;
             }
             if (!empty($this->plugin_params->groups)) {
                 $valid_groups = explode(',', trim($this->plugin_params->groups, ','));
                 foreach ($valid_groups as &$g) {
                     $g = (int) $g;
                 }
                 unset($g);
                 if (!HIKASHOP_J16) {
                     $joomla_user = clone JFactory::getUser($user->user_cms_id);
                     $userGroups = array($joomla_user->gid);
                 } else {
                     jimport('joomla.access.access');
                     $userGroups = JAccess::getGroupsByUser($user->user_cms_id, true);
                 }
                 $f = false;
                 foreach ($userGroups as $g) {
                     if (in_array($g, $valid_groups)) {
                         $f = true;
                         break;
                     }
                 }
                 if (!$f) {
                     $do = false;
                 }
             }
         }
     }
 }
Exemple #7
0
 public function onSerialOrderPreUpdate($new, &$order, &$order_serial_params)
 {
     if (!$new) {
         return;
     }
     $ids = array();
     parent::listPlugins('serialperorder', $ids, false);
     foreach ($ids as $id) {
         parent::pluginParams($id);
         if ((int) $this->plugin_params->quantity == 0 || (int) $this->plugin_params->pack_id == 0) {
             continue;
         }
         if (empty($order_serial_params['order'][(int) $this->plugin_params->pack_id])) {
             $order_serial_params['order'][(int) $this->plugin_params->pack_id] = array(0, 0);
         }
         if (!is_array($order_serial_params['order'][(int) $this->plugin_params->pack_id])) {
             $order_serial_params['order'][(int) $this->plugin_params->pack_id] = array($order_serial_params['order'][(int) $this->plugin_params->pack_id], 0);
         }
         $order_serial_params['order'][(int) $this->plugin_params->pack_id][1] += (int) $this->plugin_params->quantity;
     }
 }
Exemple #8
0
 public function generate(&$pack, &$order, $quantity, &$serials)
 {
     if (!isset($pack->coupongen)) {
         return;
     }
     parent::pluginParams($pack->coupongen);
     if (empty($this->plugin_params->format) || !preg_match_all('#\\\\[|\\\\]|\\[[^]]+\\]\\{.*\\}|\\[.*\\]|.#iU', $this->plugin_params->format, $matches)) {
         $matches = array(array('[a-zA-Z0-9]{size}'));
     }
     $config = hikaserial::config();
     $fastRandom = (int) $config->get('use_fast_random', 0);
     for ($q = 0; $q < $quantity; $q++) {
         $serial = '';
         $serialObj = new stdClass();
         if (!HIKASHOP_J16 || $fastRandom) {
             $stat = @stat(__FILE__);
             if (empty($stat) || !is_array($stat)) {
                 $stat = array(php_uname());
             }
             mt_srand(crc32(microtime() . implode('|', $stat)));
         } else {
             if (empty($this->plugin_params->size) || $this->plugin_params->size == 0) {
                 $this->plugin_params->size = 15;
             }
             $rndCpt = 1;
             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
             $shift = ord($random[0]);
         }
         foreach ($matches[0] as $m) {
             if (strlen($m) == 1) {
                 $serial .= $m;
             } else {
                 $repeat = 1;
                 $format = $m;
                 if (strpos($m, '{') !== false) {
                     list($format, $repeat) = explode('{', $m);
                     $repeat = trim(trim($repeat, '}'));
                     if (empty($repeat) || (int) $repeat == 0) {
                         $repeat = $this->plugin_params->size;
                     } else {
                         $repeat = (int) $repeat;
                     }
                 }
                 $format = substr($format, 1, -1);
                 $list = '';
                 $l = strlen($format);
                 for ($i = 0; $i < $l; $i++) {
                     if ($i + 2 < $l) {
                         if ($format[$i + 1] == '-') {
                             $s = $format[$i];
                             $e = $format[$i + 2];
                             $s1 = $s >= 'a' && $s <= 'z';
                             $s2 = $s >= 'A' && $s <= 'Z';
                             $s3 = $s >= '0' && $s <= '9';
                             $e1 = $e >= 'a' && $e <= 'z';
                             $e2 = $e >= 'A' && $e <= 'Z';
                             $e3 = $e >= '0' && $e <= '9';
                             if (!$s1 && !$s2 && !$s3) {
                                 $list .= $s . '-';
                                 $i++;
                                 // Skip '-'
                                 continue;
                             }
                             if ($s1 && $e1 || $s2 && $e2 || $s3 && $e3) {
                                 if ($s > $e) {
                                     $c = $s;
                                     $s = $e;
                                     $e = $c;
                                 }
                                 for ($c = $s; $c < $e; $c++) {
                                     $list .= $c;
                                 }
                                 $i += 2;
                             } else {
                                 if ($s1 && $e2) {
                                     for ($c = $s; $c < 'z'; $c++) {
                                         $list .= $c;
                                     }
                                     for ($c = 'A'; $c < $e; $c++) {
                                         $list .= $c;
                                     }
                                     $i += 2;
                                 } else {
                                     $list .= $s . '-';
                                     $i++;
                                     // Skip '-'
                                 }
                             }
                         } else {
                             $list .= $format[$i];
                         }
                     } else {
                         $list .= $format[$i];
                     }
                 }
                 $base = strlen($list);
                 if (!HIKASHOP_J16 || $fastRandom) {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[mt_rand(0, $base - 1)];
                     }
                 } else {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[($shift + ord($random[$rndCpt])) % $base];
                         $shift += ord($random[$rndCpt++]);
                         if ($rndCpt == $this->plugin_params->size) {
                             $rndCpt = 1;
                             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
                             $shift = ord($random[0]);
                         }
                     }
                 }
             }
         }
         $discount_id = (int) $this->plugin_params->discount_id;
         $result = true;
         if (!$this->test && !empty($discount_id)) {
             $discountClass = hikaserial::get('shop.class.discount');
             $data = $discountClass->get($discount_id);
             if ($data) {
                 unset($data->discount_id);
                 $data->discount_code = $serial;
                 $data->discount_published = 1;
                 $data->discount_used_times = 0;
                 if (!empty($this->plugin_params->validity_period) && !empty($this->plugin_params->validity_value) && (int) $this->plugin_params->validity_value > 0) {
                     $date_d = date("d");
                     $date_m = date("m");
                     $date_y = date("Y");
                     $v = (int) $this->plugin_params->validity_value;
                     switch ($this->plugin_params->validity_period) {
                         case 'year':
                             $data->discount_end = mktime(0, 0, 0, $date_m, $date_d, $date_y + $v);
                             break;
                         case 'month':
                             $data->discount_end = mktime(0, 0, 0, $date_m + $v, $date_d, $date_y);
                             break;
                         case 'day':
                             $data->discount_end = mktime(0, 0, 0, $date_m, $date_d + $v, $date_y);
                             break;
                     }
                     if (!isset($serialObj->extradata)) {
                         $serialObj->extradata = array();
                     }
                     $serialObj->extradata['discount_end'] = $data->discount_end;
                 }
                 if (!empty($this->plugin_params->discount_percent)) {
                     $v = hikaserial::toFloat(trim($this->plugin_params->discount_percent));
                     $product_price = 0;
                     if (!empty($order->cart->products)) {
                         foreach ($order->cart->products as $p) {
                             if ($p->product_id == $pack->product_id || isset($pack->order_product_id) && $p->order_product_id == $pack->order_product_id) {
                                 $product_price = hikaserial::toFloat($p->order_product_price);
                                 if (!empty($this->plugin_params->discount_percent_tax)) {
                                     $product_price += hikaserial::toFloat($p->order_product_tax);
                                 }
                                 break;
                             }
                         }
                     }
                     if (!empty($product_price)) {
                         if (!empty($order->order_currency_id)) {
                             $data->discount_currency_id = (int) $order->order_currency_id;
                         } else {
                             $data->discount_currency_id = (int) $order->old->order_currency_id;
                         }
                         $data->discount_flat_amount = $product_price * $v / 100;
                         $data->discount_percent_amount = 0.0;
                     }
                 }
                 if (!$discountClass->save($data)) {
                     $result = false;
                 }
             }
         }
         if (!$result) {
             $app = JFactory::getApplication();
             $app->enqueueMessage(JText::_('ERR_CREATING_DISCOUNT_COUPON'));
         }
         if (!empty($serialObj) && !empty($serialObj->extradata)) {
             $serialObj->data = $serial;
             $serials[] = $serialObj;
         } else {
             $serials[] = $serial;
         }
     }
 }
Exemple #9
0
 public function generate(&$pack, &$order, $quantity, &$serials)
 {
     if (!isset($pack->randomgen)) {
         return;
     }
     parent::pluginParams($pack->randomgen);
     if (empty($this->plugin_params->format) || !preg_match_all('#\\\\[|\\\\]|\\[[^]]+\\]\\{.*\\}|\\[.*\\]|.#iU', $this->plugin_params->format, $matches)) {
         $matches = array(array('[a-zA-Z0-9]{size}'));
     }
     $config = hikaserial::config();
     $fastRandom = (int) $config->get('use_fast_random', 0);
     for ($q = 0; $q < $quantity; $q++) {
         $serial = '';
         if (!HIKASHOP_J16 || $fastRandom) {
             $stat = @stat(__FILE__);
             if (empty($stat) || !is_array($stat)) {
                 $stat = array(php_uname());
             }
             mt_srand(crc32(microtime() . implode('|', $stat)));
         } else {
             if (empty($this->plugin_params->size) || $this->plugin_params->size == 0) {
                 $this->plugin_params->size = 15;
             }
             $rndCpt = 1;
             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
             $shift = ord($random[0]);
         }
         foreach ($matches[0] as $m) {
             if (strlen($m) == 1) {
                 $serial .= $m;
             } else {
                 $repeat = 1;
                 $format = $m;
                 if (strpos($m, '{') !== false) {
                     list($format, $repeat) = explode('{', $m);
                     $repeat = trim(trim($repeat, '}'));
                     if (empty($repeat) || (int) $repeat == 0) {
                         $repeat = $this->plugin_params->size;
                     } else {
                         $repeat = (int) $repeat;
                     }
                 }
                 $format = substr($format, 1, -1);
                 $list = '';
                 $l = strlen($format);
                 for ($i = 0; $i < $l; $i++) {
                     if ($i + 2 < $l) {
                         if ($format[$i + 1] == '-') {
                             $s = $format[$i];
                             $e = $format[$i + 2];
                             $s1 = $s >= 'a' && $s <= 'z';
                             $s2 = $s >= 'A' && $s <= 'Z';
                             $s3 = $s >= '0' && $s <= '9';
                             $e1 = $e >= 'a' && $e <= 'z';
                             $e2 = $e >= 'A' && $e <= 'Z';
                             $e3 = $e >= '0' && $e <= '9';
                             if (!$s1 && !$s2 && !$s3) {
                                 $list .= $s . '-';
                                 $i++;
                                 // Skip '-'
                                 continue;
                             }
                             if ($s1 && $e1 || $s2 && $e2 || $s3 && $e3) {
                                 if ($s > $e) {
                                     $c = $s;
                                     $s = $e;
                                     $e = $c;
                                 }
                                 for ($c = $s; $c < $e; $c++) {
                                     $list .= $c;
                                 }
                                 $i += 2;
                             } else {
                                 if ($s1 && $e2) {
                                     for ($c = $s; $c < 'z'; $c++) {
                                         $list .= $c;
                                     }
                                     for ($c = 'A'; $c < $e; $c++) {
                                         $list .= $c;
                                     }
                                     $i += 2;
                                 } else {
                                     $list .= $s . '-';
                                     $i++;
                                     // Skip '-'
                                 }
                             }
                         } else {
                             $list .= $format[$i];
                         }
                     } else {
                         $list .= $format[$i];
                     }
                 }
                 $base = strlen($list);
                 if (!HIKASHOP_J16 || $fastRandom) {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[mt_rand(0, $base - 1)];
                     }
                 } else {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[($shift + ord($random[$rndCpt])) % $base];
                         $shift += ord($random[$rndCpt++]);
                         if ($rndCpt == $this->plugin_params->size) {
                             $rndCpt = 1;
                             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
                             $shift = ord($random[0]);
                         }
                     }
                 }
             }
         }
         $serials[] = $serial;
     }
     return true;
 }
Exemple #10
0
    public function populateForm(&$pack)
    {
        $ret = '';
        if (isset($pack->pointsgen)) {
            parent::pluginParams($pack->pointsgen);
        }
        if (isset($this->plugin_params)) {
            $ret .= '<tr>
				<td class="key"><label>' . JText::_('POINTS_VALUE') . '</label></td>
				<td><strong>' . $this->plugin_params->value . '</strong></td>
			</tr>';
            if (!empty($this->plugin_params->currency_rate)) {
                $shopConfig = hikaserial::config(false);
                $main_currency = $shopConfig->get('main_currency', 1);
                $currencyClass = hikaserial::get('shop.class.currency');
                $currency = $currencyClass->get($main_currency);
                $ret .= '<tr>
					<td class="key"><label>' . JText::_('POINTS_CURRENCY_RATE') . '</label></td>
					<td><strong>' . $this->plugin_params->currency_rate . ' ' . JText::sprintf('POINTS_CONVERSION', $currency->currency_code . ' ' . $currency->currency_symbol) . '</strong></td>
				</tr>';
            }
            $ret .= '<tr>
				<td class="key"><label>' . JText::_('ADD_POINTS') . '</label></td>
				<td><input type="text" name="data[points_value]" value="0"/></td>
			</tr>';
        } else {
            $ret .= '<tr>
				<td class="key"><label>' . JText::_('POINTS_VALUE') . '</label></td>
				<td><input type="text" name="data[points_value]" value=""/></td>
			</tr>';
        }
        return $ret;
    }
Exemple #11
0
 public function generate(&$pack, &$order, $quantity, &$serials)
 {
     if (!isset($pack->timelimitedgen)) {
         return;
     }
     parent::pluginParams($pack->timelimitedgen);
     if (empty($this->plugin_params->format) || !preg_match_all('#\\\\[|\\\\]|\\[[^]]+\\]\\{.*\\}|\\[.*\\]|.#iU', $this->plugin_params->format, $matches)) {
         $matches = array(array('[a-zA-Z0-9]{size}'));
     }
     $config = hikaserial::config();
     $fastRandom = (int) $config->get('use_fast_random', 0);
     for ($q = 0; $q < $quantity; $q++) {
         $serial = '';
         $serialObj = new stdClass();
         if (!HIKASHOP_J16 || $fastRandom) {
             $stat = @stat(__FILE__);
             if (empty($stat) || !is_array($stat)) {
                 $stat = array(php_uname());
             }
             mt_srand(crc32(microtime() . implode('|', $stat)));
         } else {
             if (empty($this->plugin_params->size) || $this->plugin_params->size == 0) {
                 $this->plugin_params->size = 15;
             }
             $rndCpt = 1;
             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
             $shift = ord($random[0]);
         }
         foreach ($matches[0] as $m) {
             if (strlen($m) == 1) {
                 $serial .= $m;
             } else {
                 $repeat = 1;
                 $format = $m;
                 if (strpos($m, '{') !== false) {
                     list($format, $repeat) = explode('{', $m);
                     $repeat = trim(trim($repeat, '}'));
                     if (empty($repeat) || (int) $repeat == 0) {
                         $repeat = $this->plugin_params->size;
                     } else {
                         $repeat = (int) $repeat;
                     }
                 }
                 $format = substr($format, 1, -1);
                 $list = '';
                 $l = strlen($format);
                 for ($i = 0; $i < $l; $i++) {
                     if ($i + 2 < $l) {
                         if ($format[$i + 1] == '-') {
                             $s = $format[$i];
                             $e = $format[$i + 2];
                             $s1 = $s >= 'a' && $s <= 'z';
                             $s2 = $s >= 'A' && $s <= 'Z';
                             $s3 = $s >= '0' && $s <= '9';
                             $e1 = $e >= 'a' && $e <= 'z';
                             $e2 = $e >= 'A' && $e <= 'Z';
                             $e3 = $e >= '0' && $e <= '9';
                             if (!$s1 && !$s2 && !$s3) {
                                 $list .= $s . '-';
                                 $i++;
                                 // Skip '-'
                                 continue;
                             }
                             if ($s1 && $e1 || $s2 && $e2 || $s3 && $e3) {
                                 if ($s > $e) {
                                     $c = $s;
                                     $s = $e;
                                     $e = $c;
                                 }
                                 for ($c = $s; $c < $e; $c++) {
                                     $list .= $c;
                                 }
                                 $i += 2;
                             } else {
                                 if ($s1 && $e2) {
                                     for ($c = $s; $c < 'z'; $c++) {
                                         $list .= $c;
                                     }
                                     for ($c = 'A'; $c < $e; $c++) {
                                         $list .= $c;
                                     }
                                     $i += 2;
                                 } else {
                                     $list .= $s . '-';
                                     $i++;
                                     // Skip '-'
                                 }
                             }
                         } else {
                             $list .= $format[$i];
                         }
                     } else {
                         $list .= $format[$i];
                     }
                 }
                 $base = strlen($list);
                 if (!HIKASHOP_J16 || $fastRandom) {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[mt_rand(0, $base - 1)];
                     }
                 } else {
                     for ($i = 1; $i <= $repeat; $i++) {
                         $serial .= $list[($shift + ord($random[$rndCpt])) % $base];
                         $shift += ord($random[$rndCpt++]);
                         if ($rndCpt == $this->plugin_params->size) {
                             $rndCpt = 1;
                             $random = JCrypt::genRandomBytes($this->plugin_params->size + 1);
                             $shift = ord($random[0]);
                         }
                     }
                 }
             }
         }
         if (!empty($this->plugin_params->validity_period) && !empty($this->plugin_params->validity_value) && (int) $this->plugin_params->validity_value > 0) {
             $date_d = date("d");
             $date_m = date("m");
             $date_y = date("Y");
             $v = (int) $this->plugin_params->validity_value;
             $discount_end = -1;
             switch ($this->plugin_params->validity_period) {
                 case 'year':
                     $discount_end = mktime(0, 0, 0, $date_m, $date_d, $date_y + $v);
                     break;
                 case 'month':
                     $discount_end = mktime(0, 0, 0, $date_m + $v, $date_d, $date_y);
                     break;
                 case 'day':
                     $discount_end = mktime(0, 0, 0, $date_m, $date_d + $v, $date_y);
                     break;
             }
             if ($discount_end > 0) {
                 if (!isset($serialObj->extradata)) {
                     $serialObj->extradata = array();
                 }
                 $serialObj->extradata['validity_end'] = $discount_end;
             }
         }
         if (empty($this->test) && !empty($serialObj) && !empty($serialObj->extradata)) {
             $serialObj->data = $serial;
             $serials[] = $serialObj;
         } else {
             $serials[] = $serial;
         }
     }
     return true;
 }
 public function onBeforeSerialConsume(&$serial, $user_id, &$do, &$extra_data)
 {
     if (empty($user_id)) {
         $user_id = hikaserial::loadUser();
     }
     $app = JFactory::getApplication();
     $products_added = array();
     $ids = array();
     $product_qty = array();
     $current_products = $app->getUserState('com_hikaserial.plg_productaddconsumer.products', array());
     if (!empty($current_products)) {
         $current_products[$serial->serial_id] = array();
         foreach ($current_products as $serial_id => $products) {
             foreach ($products as $product_id) {
                 if (empty($product_qty[$product_id])) {
                     $product_qty[$product_id] = 0;
                 }
                 $product_qty[$product_id]++;
             }
         }
     }
     parent::listPlugins('productaddconsumer', $ids, false);
     foreach ($ids as $id) {
         parent::pluginParams($id);
         if ($this->plugin_params->pack_id == $serial->serial_pack_id) {
             if (empty($user_id)) {
                 $app->enqueueMessage(JText::_('PLEASE_LOGIN'));
                 $do = false;
                 return;
             } elseif ($serial->serial_user_id != 0 && $serial->serial_user_id != $user_id) {
                 $app->enqueueMessage(JText::_('NOT_YOUR_SERIAL'));
                 $do = false;
                 return;
             }
             if (empty($product_qty[$this->plugin_params->product_id])) {
                 $product_qty[$this->plugin_params->product_id] = 1;
             } else {
                 $product_qty[$this->plugin_params->product_id]++;
             }
             $this->addProductToCart($this->plugin_params->product_id, $product_qty[$this->plugin_params->product_id]);
             if (empty($products_added[$serial->serial_id])) {
                 $products_added[$serial->serial_id] = array();
             }
             $products_added[$serial->serial_id][] = $this->plugin_params->product_id;
         }
     }
     if (!empty($products_added)) {
         if (!empty($current_products)) {
             foreach ($current_products as $serial_id => $products) {
                 if (empty($products_added[$serial_id])) {
                     $products_added[$serial_id] = $products;
                 } else {
                     $products_added[$serial_id] = array_merge($products_added[$serial_id], $products);
                 }
             }
         }
         $app->enqueueMessage(JText::_('PRODUCT_ADDED_TO_YOUR_CART'));
         $app->setUserState('com_hikaserial.plg_productaddconsumer.products', $products_added);
         $serial->managed = true;
         if (empty($serial->serial_user_id)) {
             $updateSerial = new stdClass();
             $updateSerial->serial_id = $serial->serial_id;
             $updateSerial->serial_user_id = $user_id;
             $serialClass = hikaserial::get('class.serial');
             $serialClass->save($updateSerial);
         }
     }
 }