function getActionKey()
 {
     global $application;
     $tables = Subscriptions::getTables();
     $table = 'subscription_temp';
     $columns =& $tables[$table]['columns'];
     while (1) {
         $key = mt_rand(1, 0x7fffffff);
         $query = new DB_Select($table);
         $query->addSelectField('action_key');
         $query->WhereValue($columns['action_key'], DB_EQ, $key);
         $query->SelectLimit(0, 1);
         $result = $application->db->getDB_Result($query);
         if (sizeof($result) == 0) {
             break;
         }
     }
     return $key;
 }
 /**
  * Reencrypts temporary data on the server. The step of replacing RSA keys.
  * It selects encrypted data by chunks from the database. It reencrypts it and
  * saves back to the temporary table. If all data are reencrypted, returns
  * b_finished =true in the returned array, false otherwise.
  *
  * @param string $rsa_private_key_cryptrsa_format old RSA private key, which
  * was used to encrypt data, stored in the DB
  * @param string $new_rsa_public_key_asc_format new RSA public key, which is
  * used to encrypt data, decrypted by the old RSA private key.
  */
 function ReplaceRSAKeyPairStep2ReencryptTmpData($rsa_private_key_cryptrsa_format, $new_rsa_public_key_asc_format)
 {
     global $application;
     $new_rsa_public_key_cryptrsa_format = modApiFunc("Crypto", "convert_rsa_public_key_from_asc_into_cryptrsa_format", $new_rsa_public_key_asc_format);
     /**
      * Read out from the temporary table 500 records at a time (empirical
      * value).
      *
      * Reencrypt by chunks, that have the same Blowfish key, it is about
      * 10 database records. The decryption of one blowfish key (RSA), if no
      * mathematical libraries exist, can take 10 sec.
      * Check the timeout after each chunk - 2 sec.
      * If no records are left and the timeout is over, exit.
      *
      * Write what has been reencrypted to the database.
      */
     $tmp_table_name = "order_person_data" . $this->getTmpTableSuffix();
     // TableInfo only, but not data. Refer to the table using AVACTIS.
     $opd_tmp_info = clone_db_table_info("Checkout", "order_person_data", $tmp_table_name);
     $opd_tmp = $opd_tmp_info['columns'];
     # get Person Info data. Total record number.
     $query = new DB_Select();
     $query->addSelectField($query->fCount('*'), 'count');
     $query->Where($opd_tmp['b_encrypted'], DB_EQ, "1");
     $result = $application->db->getDB_Result($query);
     $n_total = $result[0]['count'];
     # get Person Info data.
     $query = new DB_Select();
     $query->addSelectField($opd_tmp['id'], 'id');
     $query->addSelectField($opd_tmp['value'], 'value');
     $query->addSelectField($opd_tmp['encrypted_secret_key'], 'encrypted_secret_key');
     $query->addSelectField($opd_tmp['rsa_public_key_asc_format'], 'rsa_public_key_asc_format');
     $query->Where($opd_tmp['b_encrypted'], DB_EQ, "1");
     $query->WhereAnd();
     $query->Where($opd_tmp['id'], DB_GTE, $this->ReplaceRSAKeyPairStep2ReencryptTmpDataOrderPersonDataId);
     $query->SelectOrder($opd_tmp['id']);
     $query->SelectLimit(0, 500);
     $_person_data = $application->db->getDB_Result($query);
     if (sizeof($_person_data) == 0) {
         //No unreencrypted data is left. The reencryption is completed.
         return array("error_msg" => "", "b_finished" => true, "progress_position" => 1.0);
     } else {
         $i = 0;
         // a number of record from order_person_data
         $start_time = time();
         while (time() - $start_time < 2) {
             //Process one block with the same blowfish key.
             $rsa_encrypted_blowfish_key = $_person_data[$i]['encrypted_secret_key'];
             /*
             If the loaded Private key doesn't match the Public key storing in the database  -
             output an error message. Don't rewrite anything in the database.
             */
             $old_rsa_public_key_asc_format = $_person_data[$i]['rsa_public_key_asc_format'];
             $old_rsa_public_key_cryptrsa_format = modApiFunc("Crypto", "convert_rsa_public_key_from_asc_into_cryptrsa_format", $old_rsa_public_key_asc_format);
             if (modApiFunc("Crypto", "rsa_do_public_key_match_private_key", $old_rsa_public_key_cryptrsa_format, $rsa_private_key_cryptrsa_format) === true) {
                 //BEGIN decrypt blowfish key.
                 $rsa_obj = new Crypt_RSA();
                 $blowfish_key = $rsa_obj->decrypt($rsa_encrypted_blowfish_key, $rsa_private_key_cryptrsa_format);
                 $new_blowfish_key = modApiFunc("Crypto", "blowfish_gen_blowfish_key");
                 $new_encrypted_blowfish_key = $rsa_obj->encrypt($new_blowfish_key, $new_rsa_public_key_cryptrsa_format);
                 //END decrypt blowfish key.
                 //Bulk INSERT will increase the rate greatly!
                 for (; $i < sizeof($_person_data) && $_person_data[$i]['encrypted_secret_key'] == $rsa_encrypted_blowfish_key; $i++) {
                     $decrypted_value = modApiFunc("Crypto", "blowfish_decrypt", base64_decode($_person_data[$i]['value']), $blowfish_key);
                     //Store decrypted data:
                     $query = new DB_Update($tmp_table_name);
                     $query->addUpdateValue($opd_tmp['value'], base64_encode(modApiFunc("Crypto", "blowfish_encrypt", $decrypted_value, $new_blowfish_key)));
                     $query->addUpdateValue($opd_tmp['encrypted_secret_key'], $new_encrypted_blowfish_key);
                     $query->addUpdateValue($opd_tmp['rsa_public_key_asc_format'], $new_rsa_public_key_asc_format);
                     $query->WhereValue($opd_tmp['id'], DB_EQ, $_person_data[$i]['id']);
                     $application->db->getDB_Result($query);
                     $this->ReplaceRSAKeyPairStep2ReencryptTmpDataOrderPersonDataId = $_person_data[$i]['id'] + 1;
                     $this->saveState();
                     //Don't lose reencrypted data and save correct number
                     //of the last processed record. Otherwise the timeout can occur during the
                     //SQL query and data in the session will be incorrect.
                 }
                 if ($i >= sizeof($_person_data)) {
                     break;
                 }
             } else {
                 //Report an error: keys don't match.
                 $MessageResources =& $application->getInstance('MessageResources');
                 $msg = $MessageResources->getMessage('CRYPTO_RSA_PUBLIC_PRIVATE_KEYS_MISMATCH_DECRYPT_ERROR');
                 return array("error_msg" => $msg, "b_finished" => false, "progress_position" => 0.0);
             }
         }
         # get Person Info data. Total record count.
         $query = new DB_Select();
         $query->addSelectField($query->fCount('*'), 'count');
         $query->Where($opd_tmp['b_encrypted'], DB_EQ, "1");
         $query->WhereAnd();
         $query->Where($opd_tmp['id'], DB_LT, $this->ReplaceRSAKeyPairStep2ReencryptTmpDataOrderPersonDataId);
         $result = $application->db->getDB_Result($query);
         $n_done = $result[0]['count'];
         return array("error_msg" => "", "b_finished" => false, "progress_position" => 1.0 * $n_done / $n_total);
     }
 }
 function sendMessagesPortion3($num)
 {
     global $application;
     loadCoreFile('ascHtmlMimeMail.php');
     $mailer = new ascHtmlMimeMail();
     $tables = $this->getTables();
     $table = 'newsletter_temp';
     $columns =& $tables[$table]['columns'];
     $query = new DB_Select($table);
     $query->addSelectField($columns['recipient_value']);
     $query->addSelectField($columns['key_unsubscribe']);
     $query->addSelectField($columns['lng']);
     $query->WhereValue($columns['recipient_num'], DB_EQ, $num);
     $query->SelectOrder($columns['recipient_id'], 'ASC');
     $query->SelectLimit(0, PORTION_MAX_MESSAGES_NUM);
     $res = $application->db->getDB_Result($query);
     $addr_num = count($res);
     $start_time = $this->microtime_float();
     $sent_count = 0;
     // getting the default language
     $default_language = modApiFunc('MultiLang', 'getDefaultLanguage');
     // saving the current language
     $current_language = modApiFunc('MultiLang', 'getLanguage');
     // storing the current letter_id
     $letter_id = $this->_currentMessage['letter_id'];
     while ($this->microtime_float() - $start_time < PORTION_MAX_EXPORT_TIME && $sent_count < $addr_num) {
         //
         //
         //
         // setting the language
         if (!$res[$sent_count]['lng']) {
             $res[$sent_count]['lng'] = $default_language;
         }
         modApiFunc('MultiLang', 'setLanguage', $res[$sent_count]['lng']);
         // reading the newsletter for the language
         $this->_currentMessage = $this->getMessageInfo($letter_id);
         $from = $this->_currentMessage['letter_from_name'] . ' <' . $this->_currentMessage['letter_from_email'] . '>';
         $mailer->setFrom($from);
         $mailer->setSubject($this->_currentMessage['letter_subject']);
         $html_tmpl = "<html><head><title>{$this->_currentMessage['letter_subject']}</title></head><body>{$this->_currentMessage['letter_html']}</body></html>";
         $html_log = str_replace('%KEY_UNSUBSCRIBE%', $res[$sent_count]['key_unsubscribe'], $this->_currentMessage['letter_html']);
         $mailer->setHtml(str_replace('%KEY_UNSUBSCRIBE%', $res[$sent_count]['key_unsubscribe'], $html_tmpl));
         $result = $mailer->send(array($res[$sent_count]['recipient_value']));
         $mailer->resetMessageBuilt();
         $this->addNewsletterToTimeline($res[$sent_count]['recipient_value'], $this->_currentMessage['letter_subject'], $html_log, $result);
         $sent_count++;
         // :
         /*debug*/
         //usleep(200000);
     }
     // restoring the current language
     modApiFunc('MultiLang', 'setLanguage', $current_language);
     if ($sent_count) {
         $this->_sentCountTotal += $sent_count;
         $this->removeEmails($num, $sent_count);
     }
     if ($this->_sentCountTotal < $this->_totalRecipients) {
         $sending_status = 'PROCESSING';
     } else {
         $sending_status = 'COMPLETED';
         $this->_sentCountTotal = $this->_totalRecipients;
         $this->updateMessage($this->_currentMessage['letter_id'], array('letter_sent_date' => date('Y-m-d G:i:s')));
     }
     return array('errors' => '', 'warnings' => '', 'sent_total' => $this->_sentCountTotal, 'sending_status' => $sending_status);
 }
 /**
  * Gets a page from the Inventory table for the entity.
  *
  * @param string $parent_entity - entity name
  * @param int $entity_id - ID of the entity, the page should be gotten for
  * @param int $page_number - page number
  * @return array with the elements of the type
  * array(
  *  'it_id' => int - ID of table element
  * ,'parent_entity' => enum('product','ptype',...)
  * ,'entity_id' => int - ID of the entity
  * ,'combination' => string - serialize combination (see the method _serialize_combination)
  * ,'sku' => string - SKU of the combination
  * ,'quantity' => int - quantity of available products with this combination
  * )
  */
 function getInventoryPage($parent_entity, $entity_id, $page_number)
 {
     if ($page_number != 'last_page') {
         $page_number = intval($page_number);
         if ($page_number <= 0) {
             $page_number = 1;
         }
     }
     global $application;
     $tables = $this->getTables();
     $it_table = $tables['po_inventory']['columns'];
     $query = new DB_Select();
     $query->addSelectTable('po_inventory');
     $query->addSelectField($query->fCount($it_table['it_id']), 'it_count');
     $query->WhereValue($it_table['parent_entity'], DB_EQ, $parent_entity);
     $query->WhereAND();
     $query->WhereValue($it_table['entity_id'], DB_EQ, $entity_id);
     $res = $application->db->getDB_Result($query);
     $it_count = $res[0]['it_count'];
     $sets = $this->getOptionsSettingsForEntity($parent_entity, $entity_id);
     $it_per_page = $sets["INV_PER_PAGE"];
     $full_pages_count = floor($it_count / $it_per_page);
     $on_last_page = $it_count % $it_per_page;
     if ($on_last_page > 0) {
         $pages_count = $full_pages_count + 1;
     } else {
         $pages_count = $full_pages_count;
     }
     if ($page_number == 'last_page') {
         $page_number = $pages_count;
     }
     if ($page_number > $pages_count) {
         $page_number = $pages_count;
     }
     $query = new DB_Select();
     $query->addSelectTable('po_inventory');
     $query->addSelectField('*');
     $query->WhereValue($it_table['parent_entity'], DB_EQ, $parent_entity);
     $query->WhereAND();
     $query->WhereValue($it_table['entity_id'], DB_EQ, $entity_id);
     $query->SelectOrder($it_table['sort_order'], 'ASC');
     $query->SelectLimit(($page_number - 1) * $it_per_page, $it_per_page);
     $page_content = $application->db->getDB_Result($query);
     return array("pages_count" => $pages_count, "page_number" => $page_number, "page_content" => $page_content, "inv_per_page" => $it_per_page, "inv_count" => $it_count);
 }
Esempio n. 5
0
 /**
  * Deletes old records in the news table.
  * After that <= NEWS_MAX_COUNT records remain.
  */
 function deleteOldNews()
 {
     global $application;
     $tables = $this->getTables();
     $columns = $tables['news']['columns'];
     # select the latest date piece of news among the remain ones in the table...
     $query = new DB_Select('news');
     $query->addSelectField($columns['date'], 'NewsDate');
     $query->SelectOrder($columns['date'], 'DESC');
     $query->SelectLimit($this->settings[NEWS_MAX_COUNT] - 1, 1);
     $result = $application->db->getDB_Result($query);
     if ($result == NULL) {
         return;
     }
     # ...delete all the latest news @ check this line
     $query = new DB_Delete('news');
     $query->WhereValue($columns['date'], DB_LT, $result[0]['NewsDate']);
     $query->WhereAND();
     $query->WhereValue($columns['type'], DB_EQ, 'avactis');
     $application->db->getDB_Result($query);
 }
 /**
  * @param int $category_id - ID
  * @param array $period = ('begin' => timestamp, 'end' => timestamp) -
  *
  * @param int $limit -                   (
  *                          ,    STAT_NO_LIMIT)
  * @param int $what_category = STAT_CATEGORY_THIS_ONLY ||
  * STAT_CATEGORY_RECURSIVE -
  *
  * @param int $what_products = STAT_PRODUCTS_ALL ||
  * STAT_PRODUCTS_EXISTS_ONLY -                                 ,
  *
  */
 function getProductsSellingStat($category_id, $period, $limit = STAT_NO_LIMIT, $what_category = STAT_CATEGORY_THIS_ONLY, $what_products = STAT_PRODUCTS_EXISTS_ONLY)
 {
     global $application;
     $tables = $this->getTables();
     $ps_table = $tables['stat_products_sold']['columns'];
     $categories_ids = array();
     if ($what_category == STAT_CATEGORY_RECURSIVE) {
         $categories = modApiFunc('Catalog', 'getSubcategoriesFullListWithParent', $category_id, false, false);
         foreach ($categories as $cat_info) {
             $categories_ids[] = $cat_info['id'];
         }
     } else {
         $categories_ids[] = $category_id;
     }
     $query = new DB_Select();
     $query->addSelectField($ps_table['product_id'], 'product_id');
     $query->addSelectField($query->fSum($ps_table['quantity']), 'sum_quantity');
     $query->addSelectTable('stat_products_sold');
     $query->WhereValue($ps_table['categories_ids'], DB_REGEXP, '[[.vertical-line.]]' . implode('|', $categories_ids) . '[[.vertical-line.]]');
     $query->WhereAND();
     $query->Where($ps_table['time'], DB_GTE, $period['begin']);
     $query->WhereAND();
     $query->Where($ps_table['time'], DB_LTE, $period['end']);
     if ($what_products == STAT_PRODUCTS_EXISTS_ONLY) {
         $catalog_tables = modApiStaticFunc('Catalog', 'getTables');
         $query->addSelectTable('products');
         $query->WhereAND();
         $query->WhereField($ps_table['product_id'], DB_EQ, $catalog_tables['products']['columns']['id']);
     }
     $query->SelectGroup('product_id');
     $query->SelectOrder('sum_quantity', 'DESC');
     if ($limit != STAT_NO_LIMIT) {
         $query->SelectLimit(0, $limit);
     }
     return $application->db->getDB_Result($query);
 }