function getTopicsEmailsCount($topics_ids, $unique = true)
 {
     global $application;
     if (empty($topics_ids)) {
         return array();
     }
     $tables = $this->getTables();
     $etable = 'subscription_email';
     $ecolumns =& $tables[$etable]['columns'];
     $query = new DB_Select($etable);
     if ($unique) {
         $query->addSelectField(DB_Select::fCountDistinct($ecolumns['email_id']), 'email_count');
     } else {
         $query->addSelectField(DB_Select::fCount($ecolumns['email_id']), 'email_count');
     }
     $query->Where($ecolumns['topic_id'], DB_IN, DBQuery::arrayToIn($topics_ids));
     $res = $application->db->getDB_Result($query);
     return $res[0]['email_count'];
 }
 /**
  * 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);
     }
 }
Beispiel #3
0
 /**
  *
  * @author Alexandr Girin
  */
 function getTablesAndRecordsCount($count_records = true)
 {
     global $application;
     $avactis_tables = array();
     $tables = $this->getTables();
     $module_tbl = 'module';
     $module_columns = $tables[$module_tbl]['columns'];
     $module_class_tbl = 'module_class';
     $module_class_columns = $tables[$module_class_tbl]['columns'];
     $db_select = new DB_Select();
     $db_select->addSelectField($module_columns['name']);
     $modules = $application->db->getDB_Result($db_select, QUERY_RESULT_NUM);
     $total_records = 0;
     foreach ($modules as $module) {
         if (method_exists($application->getInstance($module[0]), "getTables")) {
             $tables = modApiFunc($module[0], "getTables");
             if (sizeof($tables)) {
                 foreach ($tables as $tableName => $tableInfo) {
                     if (method_exists($application->getInstance($module[0]), "getIgnoredTablesForBackup")) {
                         $ignoredTables = modApiFunc($module[0], "getIgnoredTablesForBackup");
                         //print_r($ignoredTables);
                         if (in_array($tableName, $ignoredTables)) {
                             continue;
                         }
                     }
                     if ($count_records) {
                         $query = new DB_Select();
                         $query->addSelectTable($tableName);
                         $query->addSelectField($query->fCount('*'), 'count');
                         $result = $application->db->getDB_Result($query);
                         $avactis_tables[] = array('table_name' => $application->getAppIni('DB_TABLE_PREFIX') . $tableName, 'records_count' => $result[0]['count']);
                         $total_records += $result[0]['count'];
                     } else {
                         $avactis_tables[] = $application->getAppIni('DB_TABLE_PREFIX') . $tableName;
                     }
                 }
             }
         }
     }
     if ($count_records) {
         $avactis_tables['Total_Records'] = $total_records;
     }
     return $avactis_tables;
 }
 function countTempEmails($num)
 {
     global $application;
     $ntables = $this->getTables();
     $itable = 'newsletter_temp';
     $icolumns = $ntables[$itable]['columns'];
     $squery = new DB_Select($itable);
     $squery->addSelectField(DB_Select::fCount($icolumns['recipient_value']), 'emails_count');
     $squery->WhereValue($icolumns['recipient_num'], DB_EQ, $num);
     $res = $application->db->getDB_Result($squery);
     return $res[0]['emails_count'];
 }
 /**
  * Checks if options are used during InventoryTracking.
  *
  * @param array $oids - index array of option IDs
  * @return bool; true if at least one option is used, false no option is used
  */
 function __isUsedForIT($oids)
 {
     global $application;
     $tables = $this->getTables();
     $options_table = $tables['po_options']['columns'];
     $query = new DB_Select();
     $query->addSelectField($options_table['use_for_it'], 'use_for_it');
     $query->addSelectField($query->fCount('*'), 'uit_cnt');
     $query->Where($options_table['option_id'], DB_IN, "('" . implode("','", $oids) . "')");
     $query->SelectGroup($options_table['use_for_it']);
     $res = $application->db->getDB_Result($query);
     for ($i = 0; $i < count($res); $i++) {
         if ($res[$i]["use_for_it"] == "Y" and $res[$i]["uit_cnt"] > 0) {
             return true;
         }
     }
     return false;
 }
 /**
  * Counts the number of news in the database.
  *
  * @return integer - the number of records in the news table
  */
 function getNewsCount()
 {
     global $application;
     $tables = $this->getTables();
     $columns = $tables['news']['columns'];
     $query = new DB_Select();
     $query->addSelectField($query->fCount($columns['id']), 'NewsCount');
     $result = $application->db->getDB_Result($query);
     return intval($result[0]['NewsCount']);
 }