Exemple #1
0
 protected function _load()
 {
     $result = CM_Db_Db::select('cm_model_languagekey', 'name', 'name LIKE ".%"', 'name ASC');
     while ($section = $result->fetch()) {
         $this->_addLanguageNode($section['name']);
     }
 }
Exemple #2
0
 public function test_Get()
 {
     $user = CMTest_TH::createUser();
     $user->getRoles()->add(self::ROLE_A, 2000);
     $stamps = CM_Db_Db::select('cm_role', array('startStamp', 'expirationStamp'), array('userId' => $user->getId()))->fetch();
     $this->assertEquals($stamps['startStamp'], $user->getRoles()->getStartStamp(self::ROLE_A));
     $this->assertEquals($stamps['expirationStamp'], $user->getRoles()->getExpirationStamp(self::ROLE_A));
 }
Exemple #3
0
 /**
  * @param string                          $key
  * @param CM_Model_StreamChannel_Abstract $channel
  * @return CM_Model_Stream_Publish|null
  */
 public static function findByKeyAndChannel($key, CM_Model_StreamChannel_Abstract $channel)
 {
     $id = CM_Db_Db::select('cm_stream_publish', 'id', array('key' => (string) $key, 'channelId' => $channel->getId()))->fetchColumn();
     if (!$id) {
         return null;
     }
     return new static($id);
 }
Exemple #4
0
 public function findByData($type, array $data)
 {
     $result = CM_Db_Db::select($this->_getTableName($type), array('id'), $data)->fetch();
     if (false === $result) {
         $result = null;
     }
     return $result;
 }
Exemple #5
0
 /**
  * @param int $id
  * @throws CM_Exception_Nonexistent
  */
 public function __construct($id)
 {
     $this->_id = (int) $id;
     $this->_text = CM_Db_Db::select('cm_captcha', 'number', array('captcha_id' => $this->getId()))->fetchColumn();
     if (!$this->_text) {
         throw new CM_Exception_Nonexistent('Invalid captcha id `' . $id . '`', CM_Exception::WARN);
     }
     $this->_fontPath = CM_Util::getModulePath('CM') . 'resources/font/comicsans.ttf';
 }
Exemple #6
0
 /**
  * @param int $age
  */
 public static function deleteOlder($age)
 {
     $age = (int) $age;
     $result = CM_Db_Db::select('cm_tmp_userfile', 'uniqid', '`createStamp` < ' . (time() - $age));
     foreach ($result->fetchAllColumn() as $uniqid) {
         $tmpFile = new CM_File_UserContent_Temp($uniqid);
         $tmpFile->delete();
     }
 }
Exemple #7
0
 public function queueOutstanding()
 {
     $executeAtMax = time();
     $result = CM_Db_Db::select('cm_jobdistribution_delayedqueue', '*', '`executeAt` <= ' . $executeAtMax, '`executeAt` ASC');
     while ($row = $result->fetch()) {
         $job = $this->_instantiateJob($row['className']);
         if ($job) {
             $job->queue(CM_Params::decode($row['params'], true));
         }
     }
     CM_Db_Db::delete('cm_jobdistribution_delayedqueue', '`executeAt` <= ' . $executeAtMax);
 }
Exemple #8
0
 /**
  * @return array of arrays
  */
 public static function getDefaults()
 {
     $cacheKey = CM_CacheConst::User_Asset_Preferences_Defaults;
     $cache = CM_Cache_Local::getInstance();
     if (($defaults = $cache->get($cacheKey)) === false) {
         $defaults = array();
         $rows = CM_Db_Db::select('cm_user_preferenceDefault', array('section', 'key', 'preferenceId', 'defaultValue', 'configurable'))->fetchAll();
         foreach ($rows as $default) {
             if (!isset($defaults[$default['section']])) {
                 $defaults[$default['section']] = array();
             }
             $defaults[$default['section']][$default['key']] = array('id' => (int) $default['preferenceId'], 'value' => (bool) $default['defaultValue'], 'configurable' => (bool) $default['configurable']);
         }
         $cache->set($cacheKey, $defaults);
     }
     return $defaults;
 }
Exemple #9
0
 public function testFlushVariationCache()
 {
     $test = CM_Model_Splittest::create('foo', ['v1', 'v2']);
     $variation1 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v1'])->fetchColumn());
     $variation2 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v2'])->fetchColumn());
     $variation2->setEnabled(false);
     $fixture = $this->mockClass('CM_Splittest_Fixture')->newInstanceWithoutConstructor();
     $fixture->mockMethod('getId')->set(1);
     $fixture->mockMethod('getFixtureType')->set(1);
     CMTest_TH::timeForward(1);
     $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]);
     $this->assertSame('v1', $variation);
     $test->flush();
     $variation2->setEnabled(true);
     $variation1->setEnabled(false);
     $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]);
     $this->assertSame('v2', $variation);
 }
Exemple #10
0
 /**
  * @param CM_Cli_Command $command
  * @return array|null
  */
 protected function _findLock(CM_Cli_Command $command)
 {
     $commandName = $command->getName();
     $lock = CM_Db_Db::select('cm_cli_command_manager_process', array('hostId', 'processId'), array('commandName' => $commandName))->fetch();
     if (false === $lock) {
         return null;
     }
     return $lock;
 }
Exemple #11
0
<?php

if (CM_Db_Db::existsTable('cm_model_location_city_ip') && !CM_Db_Db::existsTable('cm_model_location_ip')) {
    CM_Db_Db::exec('RENAME TABLE `cm_model_location_city_ip` TO `cm_model_location_ip`');
}
if (CM_Db_Db::existsTable('cm_model_location_ip')) {
    if (CM_Db_Db::existsIndex('cm_model_location_ip', 'cityId')) {
        CM_Db_Db::exec('DROP INDEX `cityId` ON `cm_model_location_ip`');
    }
    if (CM_Db_Db::existsColumn('cm_model_location_ip', 'cityId') && !CM_Db_Db::existsColumn('cm_model_location_ip', 'id')) {
        CM_Db_Db::exec('ALTER TABLE `cm_model_location_ip` CHANGE COLUMN `cityId` `id` int(10) unsigned NOT NULL ');
    }
    if (!CM_Db_Db::existsColumn('cm_model_location_ip', 'level')) {
        CM_Db_Db::exec('ALTER TABLE `cm_model_location_ip` ADD COLUMN `level` int(10) unsigned NOT NULL AFTER `id`');
    }
    if (CM_Db_Db::existsColumn('cm_model_location_ip', 'level')) {
        CM_Db_Db::update('cm_model_location_ip', array('level' => CM_Model_Location::LEVEL_CITY), array('level' => 0));
    }
    if (CM_Db_Db::existsTable('cm_model_location_country_ip')) {
        $result = CM_Db_Db::select('cm_model_location_country_ip', array('countryId', 'ipStart', 'ipEnd'));
        foreach ($result->fetchAll() as $row) {
            CM_Db_Db::insert('cm_model_location_ip', array('id' => $row['countryId'], 'level' => CM_Model_Location::LEVEL_COUNTRY, 'ipStart' => $row['ipStart'], 'ipEnd' => $row['ipEnd']));
        }
        CM_Db_Db::exec('DROP TABLE `cm_model_location_country_ip`');
    }
}
Exemple #12
0
<?php

$nameList[] = 'Your browser is no longer supported';
$nameList[] = 'We recommend upgrading to the latest Internet Explorer, Google Chrome, Firefox, or Opera. Click here for <a href="{$url}">more information</a>';
$nameList[] = 'If you are using IE 9 or later, make sure you <a href="{$url}">turn off "Compatibility View"</a>';
foreach ($nameList as $name) {
    $id = CM_Db_Db::select('cm_languageKey', 'id', array('name' => $name))->fetchColumn();
    if ($id) {
        CM_Db_Db::update('cm_languageKey', array('name' => $name . '.'), array('id' => $id));
        CM_Db_Db::exec('UPDATE `cm_languageValue` SET `value`= CONCAT(`value`, ".") WHERE `languageKeyId` = ' . $id);
    }
}
Exemple #13
0
 protected function _verify($countryDataExpected, $regionDataExpected, $cityDataExpected, $zipCodeDataExpected, $ipDataExpected)
 {
     $countryDataActual = CM_Db_Db::select('cm_model_location_country', '*')->fetchAll();
     $this->assertEquals($countryDataExpected, $countryDataActual);
     $regionDataActual = CM_Db_Db::select('cm_model_location_state', '*')->fetchAll();
     $this->assertEquals($regionDataExpected, $regionDataActual);
     $cityDataActual = CM_Db_Db::select('cm_model_location_city', '*')->fetchAll();
     $this->assertEquals($cityDataExpected, $cityDataActual);
     $zipCodeDataActual = CM_Db_Db::select('cm_model_location_zip', '*')->fetchAll();
     $this->assertEquals($zipCodeDataExpected, $zipCodeDataActual);
     $ipDataActual = CM_Db_Db::select('cm_model_location_ip', '*')->fetchAll();
     $this->assertEquals($ipDataExpected, $ipDataActual);
 }
Exemple #14
0
 /**
  * @param CM_Model_Location $location
  * @return CM_Model_Currency|null
  */
 public static function findByLocation(CM_Model_Location $location)
 {
     $country = $location->get(CM_Model_Location::LEVEL_COUNTRY);
     if (null === $country) {
         return null;
     }
     $cache = CM_Cache_Local::getInstance();
     $cacheKey = CM_CacheConst::Currency_CountryId . '_countryId:' . $country->getId();
     if (false === ($currencyId = $cache->get($cacheKey))) {
         $currencyId = CM_Db_Db::select('cm_model_currency_country', 'currencyId', ['countryId' => $country->getId()])->fetchColumn();
         $currencyId = $currencyId ? (int) $currencyId : null;
         $cache->set($cacheKey, $currencyId);
     }
     if (null === $currencyId) {
         return null;
     }
     return new self($currencyId);
 }
Exemple #15
0
<?php

if (!CM_Db_Db::existsTable('cm_requestClientCounter')) {
    CM_Db_Db::exec('
        CREATE TABLE `cm_requestClientCounter` (
          `counter` int(10) unsigned NOT NULL,
          PRIMARY KEY (`counter`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ');
    CM_Db_Db::insert('cm_requestClientCounter', array('counter' => 0));
}
if (CM_Db_Db::existsTable('cm_requestClient')) {
    $highestEntry = (int) CM_Db_Db::select('cm_requestClient', 'id', null, array('id' => 'DESC'))->fetchColumn();
    CM_Db_Db::update('cm_requestClientCounter', array('counter' => $highestEntry));
    CM_Db_Db::exec('DROP TABLE IF EXISTS `cm_requestClient`;');
}
Exemple #16
0
 protected function _loadData()
 {
     return CM_Db_Db::select('entityMock', array('userId', 'foo'), array('id' => $this->getId()))->fetch();
 }
Exemple #17
0
 /**
  * @return CM_ModelThasIsAnAssetMock
  */
 public function get()
 {
     if (($modelMock = $this->_cacheGet('modelMock')) === false) {
         try {
             $modelMockId = CM_Db_Db::select('modelThasIsAnAssetMock', 'id', array('modelMockId' => $this->_model->getId()))->fetchColumn();
             $modelMock = new CM_ModelThasIsAnAssetMock($modelMockId);
             $this->_cacheSet('modelMock', $modelMock);
         } catch (CM_Exception_Nonexistent $ex) {
             $modelMock = null;
         }
     }
     return $modelMock;
 }
Exemple #18
0
 public function testReplaceTable()
 {
     $this->assertInstanceOf('CM_Db_Exception', $this->catchException(function () {
         CM_Db_Db::replaceTable('test', 'test_new');
     }));
     CM_Db_Db::exec('CREATE TABLE `test_new` (`id` INT(10) UNSIGNED NOT NULL)');
     CM_Db_Db::insert('test_new', ['id' => 123]);
     CM_Db_Db::replaceTable('test', 'test_new');
     $this->assertSame([['id' => '123']], CM_Db_Db::select('test', '*')->fetchAll());
     $this->assertSame(false, CM_Db_Db::existsTable('test_new'));
 }
Exemple #19
0
 protected function _loadData()
 {
     $data = CM_Db_Db::select('cm_splitfeature', '*', array('name' => $this->getName()))->fetch();
     return $data;
 }
Exemple #20
0
 protected function _loadData()
 {
     return CM_Db_Db::select('cm_splittestVariation', '*', array('id' => $this->getId()))->fetch();
 }
Exemple #21
0
 public function testGetDistance()
 {
     $winterthur = (int) CM_Db_Db::select('cm_model_location_city', 'id', array('name' => 'Winterthur'))->fetchColumn();
     $basel = (int) CM_Db_Db::select('cm_model_location_city', 'id', array('name' => 'Basel'))->fetchColumn();
     $location = new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $basel);
     $locationAgainst = new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $winterthur);
     $this->assertSame(86720, $location->getDistance($locationAgainst));
 }
Exemple #22
0
 protected function _loadData()
 {
     $data = CM_Db_Db::select('cm_splittest', '*', array('name' => $this->getName()))->fetch();
     if ($data) {
         $data['variations'] = CM_Db_Db::select('cm_splittestVariation', array('id', 'name'), array('splittestId' => $data['id']))->fetchAllTree();
     }
     return $data;
 }
Exemple #23
0
 public function testCustomHeadersQueue()
 {
     $mail = new CM_Mail();
     $subject = uniqid();
     $mail->setSubject($subject);
     $mail->addCustomHeader('X-Foo', 'bar');
     $mail->addCustomHeader('X-Bar', 'foo');
     $mail->addCustomHeader('X-Foo', 'baz');
     $mail->addTo('test');
     $mail->setText('bla');
     $mail->send(true);
     $result = CM_Db_Db::select('cm_mail', 'customHeaders', array('subject' => $subject));
     $row = $result->fetch();
     $this->assertEquals(unserialize($row['customHeaders']), array('X-Foo' => ['bar', 'baz'], 'X-Bar' => ['foo']));
 }
Exemple #24
0
<?php

if (!CM_Db_Db::existsColumn('cm_languageKey', 'variables')) {
    CM_Db_Db::exec("ALTER TABLE `cm_languageKey` ADD `variables` text CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL AFTER name");
}
if (CM_Db_Db::existsTable('cm_languageKey_variable')) {
    $results = CM_Db_Db::select('cm_languageKey_variable', '*')->fetchAll();
    $variableList = array();
    foreach ($results as $result) {
        $variableList[$result['languageKeyId']][] = $result['name'];
    }
    foreach ($variableList as $languageKeyId => $variables) {
        CM_Db_Db::update('cm_languageKey', array('variables' => json_encode($variables)), array('id' => $languageKeyId));
    }
    CM_Db_Db::exec('DROP TABLE `cm_languageKey_variable`');
}
Exemple #25
0
 /**
  * @deprecated Usually checking DB rows is not desired, rather test the public interface
  *
  * @param string            $table
  * @param array|string|null $where WHERE conditions: ('attr' => 'value', 'attr2' => 'value')
  * @param int|null          $rowCount
  */
 public static function assertRow($table, $where = null, $rowCount = null)
 {
     if (null === $rowCount) {
         $rowCount = 1;
     }
     $result = CM_Db_Db::select($table, '*', $where);
     $rowCountActual = count($result->fetchAll());
     self::assertEquals($rowCount, $rowCountActual);
 }
Exemple #26
0
// 1) Duplicate entry for the city "Saint John's"
// Setting the region code to "Newfoundland and Labrador" for one of them
// to achieve consistency with the updated GeoIP data
CM_Db_Db::update('cm_model_location_city', array('stateId' => 2524), array('id' => 22256));
// 2) Duplicate entries for the cities "Apo"/"Fpo" (U.S. Army post offices)
// Adding region entries for the U.S. Armed Forces Americas & Pacific
$idUS = CM_Db_Db::select('cm_model_location_country', 'id', array('abbreviation' => 'US'))->fetchColumn();
if (false === $idUS) {
    throw new CM_Exception_Invalid('No country with abbreviation `US` found');
}
$idUS = (int) $idUS;
$armedForcesRegionList = array('AA' => 'Armed Forces Americas', 'AE' => 'Armed Forces Europe, Middle East, & Canada', 'AP' => 'Armed Forces Pacific');
$idArmedForcesList = array();
foreach ($armedForcesRegionList as $regionCode => $regionName) {
    $idArmedForces = CM_Db_Db::select('cm_model_location_state', 'id', array('countryId' => $idUS, 'abbreviation' => $regionCode))->fetchColumn();
    if (false === $idArmedForces) {
        $idArmedForces = CM_Db_Db::insert('cm_model_location_state', array('countryId' => $idUS, 'abbreviation' => $regionCode, 'name' => $regionName));
    } else {
        CM_Db_Db::update('cm_model_location_state', array('name' => $regionName), array('countryId' => $idUS, 'abbreviation' => $regionCode));
    }
    $idArmedForcesList[$regionCode] = (int) $idArmedForces;
}
// Moving the duplicate cities to the correct regions
$armedForcesCityList = array('AA' => array(173158, 173159), 'AE' => array(173160, 173161), 'AP' => array(173944, 173945));
foreach ($armedForcesCityList as $regionCode => $cityIdList) {
    $regionId = $idArmedForcesList[$regionCode];
    foreach ($cityIdList as $cityId) {
        CM_Db_Db::update('cm_model_location_city', array('stateId' => $regionId), array('id' => $cityId));
    }
}
Exemple #27
0
 protected function _upgradeIpBlocks()
 {
     if ($this->_withoutIpBlocks) {
         return;
     }
     $this->_streamOutput->writeln('Updating IP blocks database…');
     $ipBlocksReader = $this->_getIpBlocksReader();
     CM_Db_Db::exec('DROP TABLE IF EXISTS `cm_model_location_ip_new`');
     CM_Db_Db::exec('CREATE TABLE `cm_model_location_ip_new` LIKE `cm_model_location_ip`');
     $infoListWarning = array();
     $stream = $ipBlocksReader['stream'];
     $count = $ipBlocksReader['lineCount'];
     $item = 0;
     $batch = [];
     while (false !== ($row = fgetcsv($stream))) {
         if ($item >= 2 && count($row) >= 3) {
             // Skip copyright, column names and empty lines
             list($ipStart, $ipEnd, $maxMind) = $row;
             $ipStart = (int) $ipStart;
             $ipEnd = (int) $ipEnd;
             $maxMind = (int) $maxMind;
             $level = null;
             $id = null;
             if (isset($this->_zipCodeIdListByMaxMind[$maxMind])) {
                 $level = CM_Model_Location::LEVEL_ZIP;
                 $id = $this->_zipCodeIdListByMaxMind[$maxMind];
             } elseif (isset($this->_cityIdList[$maxMind])) {
                 $level = CM_Model_Location::LEVEL_CITY;
                 $id = $this->_cityIdList[$maxMind];
             } elseif (isset($this->_regionIdListByMaxMind[$maxMind])) {
                 $level = CM_Model_Location::LEVEL_STATE;
                 $id = $this->_regionIdListByMaxMind[$maxMind];
             } elseif (isset($this->_countryCodeListByMaxMind[$maxMind])) {
                 $level = CM_Model_Location::LEVEL_COUNTRY;
                 $countryCode = $this->_countryCodeListByMaxMind[$maxMind];
                 if (isset($this->_countryIdList[$countryCode])) {
                     $id = $this->_countryIdList[$countryCode];
                 }
             }
             if ($level && $id) {
                 $batch[] = [$id, $level, $ipStart, $ipEnd];
                 if (1000 === count($batch)) {
                     CM_Db_Db::insert('cm_model_location_ip_new', ['id', 'level', 'ipStart', 'ipEnd'], $batch);
                     $batch = [];
                 }
             } else {
                 $infoListWarning['Ignoring unknown locations'][] = $maxMind;
             }
         }
         $this->_printProgressCounter(++$item, $count);
     }
     if (!empty($batch)) {
         CM_Db_Db::insert('cm_model_location_ip_new', ['id', 'level', 'ipStart', 'ipEnd'], $batch);
     }
     unset($batch);
     unset($this->_countryIdList);
     unset($this->_countryCodeListByMaxMind);
     unset($this->_regionIdListByMaxMind);
     unset($this->_cityIdList);
     unset($this->_zipCodeIdListByMaxMind);
     $this->_printInfoList($infoListWarning, '!');
     $this->_streamOutput->writeln('Checking overlapping of IP blocks…');
     $result = CM_Db_Db::select('cm_model_location_ip_new', ['ipStart', 'ipEnd'], null, 'ipEnd ASC');
     $ipStartPrevious = $ipEndPrevious = 0;
     $count = $result->getAffectedRows();
     $item = 0;
     while (false !== ($row = $result->fetch())) {
         list($ipStart, $ipEnd) = array_values($row);
         if ($ipStart <= $ipEndPrevious) {
             $infoListWarning['Overlapping IP blocks'][] = "{$ipStartPrevious}-{$ipEndPrevious} and {$ipStart}-{$ipEnd}";
         }
         $ipStartPrevious = $ipStart;
         $ipEndPrevious = $ipEnd;
         $this->_printProgressCounter(++$item, $count);
     }
     CM_Db_Db::replaceTable('cm_model_location_ip', 'cm_model_location_ip_new');
     $this->_printInfoList($infoListWarning, '!');
 }
Exemple #28
0
 /**
  * @param string $key
  * @param int    $adapterType
  * @return CM_Model_StreamChannel_Abstract|null
  */
 public static function findByKeyAndAdapter($key, $adapterType)
 {
     $key = (string) $key;
     $adapterType = (int) $adapterType;
     $cacheKey = CM_CacheConst::StreamChannel_Id . '_key' . $key . '_adapterType:' . $adapterType;
     $cache = CM_Cache_Shared::getInstance();
     if (false === ($result = $cache->get($cacheKey))) {
         $result = CM_Db_Db::select('cm_streamChannel', array('id', 'type'), array('key' => $key, 'adapterType' => $adapterType))->fetch();
         if (false === $result) {
             $result = null;
         }
         $cache->set($cacheKey, $result);
     }
     if (!$result) {
         return null;
     }
     try {
         $streamChannel = self::factory($result['id'], $result['type']);
     } catch (CM_Exception_Nonexistent $ex) {
         $cache->delete($cacheKey);
         return null;
     }
     return $streamChannel;
 }
Exemple #29
0
 /**
  * @param string $id
  * @return array|null
  */
 private static function _findDataById($id)
 {
     $cacheKey = self::_getCacheKey($id);
     $cache = CM_Cache_Shared::getInstance();
     if (($data = $cache->get($cacheKey)) === false) {
         $data = CM_Db_Db::select('cm_session', array('data', 'expires'), array('sessionId' => $id))->fetch();
         if (!$data) {
             return null;
         }
         $cache->set($cacheKey, $data, self::LIFETIME_DEFAULT);
     }
     return $data;
 }
Exemple #30
0
 /**
  * @param CM_Model_LanguageKey $languageKey
  * @return CM_Model_LanguageKey
  */
 protected static function _replaceWithExisting(CM_Model_LanguageKey $languageKey)
 {
     $name = $languageKey->getName();
     $languageKeyIdList = CM_Db_Db::select('cm_model_languagekey', 'id', array('name' => $name), 'id ASC')->fetchAllColumn();
     if (count($languageKeyIdList) > 1) {
         $languageKeyId = array_shift($languageKeyIdList);
         CM_Db_Db::exec("DELETE FROM `cm_model_languagekey` WHERE `name` = ? AND `id` != ?", array($name, $languageKeyId));
         $languageKey = new self($languageKeyId);
     }
     return $languageKey;
 }