示例#1
0
 /**
  * Initialize this Database.
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.5
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     if ($matches = preg_grep('/^\\s*SET\\s+NAMES\\b/i', (array) $this->getParameter('init_queries'))) {
         throw new AgaviDatabaseException(sprintf('Depending on your MySQL server configuration, it may not be safe to use "SET NAMES" to configure the connection encoding, as the underlying MySQL client library will not be aware of the changed character set. As a result, string escaping may be applied incorrectly, leading to potential attack vectors in combination with certain multi-byte character sets such as GBK or Big5.' . "\n\n" . 'Please remove the "%s" statement from the "init_queries" configuration parameter in databases.xml and use the configuration parameter "charset" instead.' . "\n\n" . 'The associated PHP bug ticket http://bugs.php.net/47802 contains further information (describes PDO, but the basic issue is the same).', $matches[0]));
     }
 }
 /**
  * Initialize the Doctrine2 ORM.
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.6
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     if (!class_exists('Doctrine\\Common\\ClassLoader')) {
         // no soup for you!
         require 'Doctrine/Common/ClassLoader.php';
         // let's assume Doctrine2 is on ze include path...
     }
     // iterate over all declared class loaders and register them if necessary (checks performed to avoid duplicates for Doctrine's own namespaces)
     // by default, we assume an install via PEAR, with all of Doctrine in one folder and on the include path
     // if people want to do the smart thing and ship a Doctrine release with their app, they just need to point the entire "Doctrine" namespace to the path
     // for bleeding edge git stuff or similar, the paths for the namespaces can be given individually, see the Doctrine manual for examples
     foreach ((array) $this->getParameter('class_loaders', array('Doctrine' => null)) as $namespace => $includePath) {
         if ($namespace == 'Doctrine' && class_exists('Doctrine\\ORM\\Version')) {
             // the ORM namespace's Version class exists or could be autloaded; let's assume that the class loader for any Doctrine stuff won't need registration then
             continue;
         }
         if (strpos($namespace, 'Doctrine\\') === 0 && class_exists($namespace . '\\Version')) {
             // it is a Doctrine namespace, and the namespace's Version class exists or could be autloaded; let's assume that the class loader won't need registration then
             continue;
         }
         // register the class loader for this namespace without further checks (there's unlikely to be further duplicates)
         $cl = new \Doctrine\Common\ClassLoader($namespace, $includePath);
         $cl->register();
     }
 }
 /**
  * Load Propel config
  * 
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      0.10.0
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     $configPath = AgaviToolkit::expandDirectives($this->getParameter('config'));
     $datasource = $this->getParameter('datasource', null);
     $use_as_default = $this->getParameter('use_as_default', false);
     $config = (require $configPath);
     if ($datasource === null || $datasource == 'default') {
         if (isset($config['propel']['datasources']['default'])) {
             $datasource = $config['propel']['datasources']['default'];
         } elseif (isset($config['datasources']['default'])) {
             $datasource = $config['datasources']['default'];
         } else {
             throw new AgaviDatabaseException('No datasource given for Propel connection, and no default datasource specified in runtime configuration file.');
         }
     }
     if (!class_exists('Propel')) {
         include 'propel/Propel.php';
     }
     if (!Propel::isInit()) {
         Propel::init($configPath);
     }
     $is13 = version_compare(Propel::VERSION, '1.4', '<');
     // grab the configuration values and inject possibly defined overrides for this data source
     if ($is13) {
         // old-style config array; PropelConfiguration was added after 1.3.0, http://trac.agavi.org/ticket/1195
         $config = Propel::getConfiguration();
         $config['datasources'][$datasource]['adapter'] = $this->getParameter('overrides[adapter]', $config['datasources'][$datasource]['adapter']);
         $config['datasources'][$datasource]['connection'] = array_merge($config['datasources'][$datasource]['connection'], $this->getParameter('overrides[connection]', array()));
         // also the autoload classes
         $config['datasources'][$datasource]['classes'] = array_merge($config['datasources'][$datasource]['classes'], $this->getParameter('overrides[classes]', array()));
         // and init queries
         if (!isset($config['datasources'][$datasource]['connection']['settings']['queries']['query'])) {
             $config['datasources'][$datasource]['connection']['settings']['queries']['query'] = array();
         }
         // array cast because "query" might be a string if just one init query was given, http://trac.agavi.org/ticket/1194
         $config['datasources'][$datasource]['connection']['settings']['queries']['query'] = array_merge((array) $config['datasources'][$datasource]['connection']['settings']['queries']['query'], (array) $this->getParameter('init_queries'));
         // set the new config
         Propel::setConfiguration($config);
     } else {
         $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
         $overrides = (array) $this->getParameter('overrides');
         // set override values
         foreach ($overrides as $key => $value) {
             $config->setParameter($key, $value);
         }
         // handle init queries in a cross-adapter fashion (they all support the "init_queries" param)
         $queries = (array) $config->getParameter('datasources.' . $datasource . '.connection.settings.queries.query', array());
         // yes... it's one array, [connection][settings][queries][query], with all the init queries from the config, so we append to that
         $queries = array_merge($queries, (array) $this->getParameter('init_queries'));
         $config->setParameter('datasources.' . $datasource . '.connection.settings.queries.query', $queries);
     }
     if (true === $this->getParameter('enable_instance_pooling')) {
         Propel::enableInstancePooling();
     } elseif (false === $this->getParameter('enable_instance_pooling')) {
         Propel::disableInstancePooling();
     }
 }
示例#4
0
 /**
  * Initialize this Database.
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.5
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     if ($this->getParameter('warn_mysql_charset', true) && strpos($this->getParameter('dsn'), 'mysql:') === 0) {
         if ($matches = preg_grep('/^\\s*SET\\s+NAMES\\b/i', (array) $this->getParameter('init_queries'))) {
             throw new AgaviDatabaseException(sprintf('Depending on your MySQL server configuration, it may not be safe to use "SET NAMES" to configure the connection encoding, as the underlying MySQL client library will not be aware of the changed character set.' . 'As a result, string escaping may be applied incorrectly, leading to potential attack vectors in combination with certain multi-byte character sets such as GBK or Big5.' . "\n\n" . 'Please use the "charset" DSN option instead and remove the "%s" statement from the "init_queries" configuration parameter in databases.xml.' . "\n\n" . 'The associated PHP bug ticket http://bugs.php.net/47802 contains further information.', $matches[0]));
         }
         if (strpos($this->getParameter('dsn'), ';charset=') !== false && version_compare(PHP_VERSION, '5.3.6', '<')) {
             throw new AgaviDatabaseException('The "charset" option in a PDO_MYSQL DSN has no effect in PHP versions prior to 5.3.6. In combination with certain multi-byte character sets such as GBK or Big5, this may cause incorrectly escaped characters in prepared statements and quoted strings, potentially leading to vulnerabilities in application code.' . "\n\n" . 'There are two ways of working around this problem:' . "\n" . '1) Upgrade to PHP 5.3.6 or later :)' . "\n" . '2) Double-check your my.cnf configuration to make sure the default connection charset is compatible with the charset you wish to set (for example, latin1 as the connection default in combination with "SET NAMES utf8" is safe), then revert to using "SET NAMES" in "init_queries" and set the "warn_mysql_charset" configuration parameter on this connection to false. In this case, it is recommended to use native prepared statements by setting the flag PDO::ATTR_EMULATE_PREPARES to 0 in "options" or "attributes", but be advised that per-statement attributes can override this setting, and calls to PDO::quote() might still yield incorrectly escaped strings.' . "\n\n" . 'The associated PHP bug ticket http://bugs.php.net/47802 contains further information.');
         }
     }
 }
 /**
  * Initialize this Database.
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     if (!class_exists('phpcouch\\Phpcouch')) {
         // assume it's on the include path
         include 'phpcouch/Phpcouch.php';
     }
     // call bootstrap regardless. it won't re-initialize if it's already been done
     \phpcouch\Phpcouch::bootstrap();
     if (!$this->hasParameter('database') && ($database = trim(parse_url($this->getParameter('uri'), PHP_URL_PATH), '/'))) {
         $this->setParameter('database', $database);
     }
 }
 /**
  * Initialize this Database.
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @throws     <b>AgaviInitializationException</b> If an error occurs while
  *                                                 initializing this Database.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.5
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     if (!$this->hasParameter('factory_class')) {
         $this->setParameter('factory_class', 'Zend_Cloud_DocumentService_Factory');
     }
     if (!class_exists($this->getParameter('factory_class'))) {
         if (!class_exists('Zend_Loader')) {
             require 'Zend/Loader.php';
         }
         Zend_Loader::loadClass($this->getParameter('factory_class'));
     }
     $factoryOptions = array();
     foreach ((array) $this->getParameter('factory_options', array()) as $name => $value) {
         // resolve constants like "Zend_Cloud_DocumentService_Factory::DOCUMENT_ADAPTER_KEY"
         if (strpos($name, '::') && defined($name)) {
             $name = constant($name);
         }
         $factoryOptions[$name] = $value;
     }
     $this->setParameter('factory_options', $factoryOptions);
 }
 /**
  * Initialize Doctrine set the autoloading
  *
  * @param      AgaviDatabaseManager The database manager of this instance.
  * @param      array                An assoc array of initialization params.
  *
  * @author     David Zülke <*****@*****.**>
  * @author     Ross Lawley <*****@*****.**>
  * @author     TANAKA Koichi <*****@*****.**>
  * @since      0.11.0
  */
 public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array())
 {
     parent::initialize($databaseManager, $parameters);
     $name = $this->getName();
     // try to autoload doctrine
     if (!class_exists('Doctrine')) {
         // okay that didn't work. last resort: include it. we assume it's on the include path by default
         require 'Doctrine.php';
     }
     $is12 = version_compare(Doctrine::VERSION, '1.2', '>=');
     // in any case, it's loaded now. maybe we need to register the autoloading stuff for it!
     // we need this list further down
     $splAutoloadFunctions = spl_autoload_functions();
     if (!in_array(array('Doctrine', 'autoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'autoload'), $splAutoloadFunctions)) {
         // we do
         spl_autoload_register(array('Doctrine', 'autoload'));
     }
     // cool. Assign the Doctrine Manager instance
     $this->doctrineManager = Doctrine_Manager::getInstance();
     // now we're in business. we will set up connections right away, as Doctrine is handling the lazy-connecting stuff for us.
     // that way, you can just start using classes in your code
     try {
         $dsn = $this->getParameter('dsn');
         if ($dsn === null) {
             // missing required dsn parameter
             $error = 'Database configuration specifies method "dsn", but is missing dsn parameter';
             throw new AgaviDatabaseException($error);
         }
         $this->connection = $this->doctrineManager->openConnection($dsn, $name);
         // do not assign the resource here. that would connect to the database
         // $this->resource = $this->connection->getDbh();
         // set our event listener that, on connect, sets the configured charset and runs init queries
         $this->connection->setListener(new AgaviDoctrineDatabaseEventListener($this));
         $this->connection->setPrefix($this->getParameter('prefix', ""));
         // set the context instance as a connection parameter
         $this->connection->setParam('context', $databaseManager->getContext(), 'org.agavi');
         // date format
         if ($this->hasParameter('date_format')) {
             $this->connection->setDateFormat($this->getParameter('date_format'));
         }
         // options
         foreach ((array) $this->getParameter('options') as $optionName => $optionValue) {
             $this->connection->setOption($optionName, $optionValue);
         }
         foreach (array('manager_attributes' => $this->doctrineManager, 'attributes' => $this->connection) as $attributesKey => $attributesDestination) {
             foreach ((array) $this->getParameter($attributesKey, array()) as $attributeName => $attributeValue) {
                 if ($is12) {
                     if (!strpos($attributeName, '::')) {
                         throw new AgaviDatabaseException(sprintf('For Doctrine 1.2 and newer, attribute names (and, if desired to be resolved against a constant, values) must be fully qualified, e.g. "Doctrine_Core::ATTR_VALIDATE" and "Doctrine_Core::VALIDATE_NONE". Given attribute with name "%s" in collection "%s" does not match this condition.', $attributeName, $attributesKey));
                     }
                     if (!defined($attributeName)) {
                         throw new AgaviDatabaseException(sprintf('Unknown Attribute "%s"', $attributeName));
                     }
                 }
                 // resolve from constant if possible
                 if (strpos($attributeName, '::') && defined($attributeName)) {
                     $attributeName = constant($attributeName);
                 }
                 // resolve from constant if possible
                 if (strpos($attributeValue, '::') && defined($attributeValue)) {
                     $attributeValue = constant($attributeValue);
                 } elseif (ctype_digit($attributeValue)) {
                     $attributeValue = (int) $attributeValue;
                 }
                 $attributesDestination->setAttribute($attributeName, $attributeValue);
             }
         }
         foreach ((array) $this->getParameter('impls', array()) as $templateName => $className) {
             $this->connection->setImpl($templateName, $className);
         }
         foreach ((array) $this->getParameter('manager_impls', array()) as $templateName => $className) {
             $this->doctrineManager->setImpl($templateName, $className);
         }
         // load models (that'll just work with empty values too)
         Doctrine::loadModels($this->getParameter('load_models'));
         // for 1.2, handle model autoloading and base paths
         if ($is12 && ($this->hasParameter('load_models') || $this->hasParameter('models_directory'))) {
             if (!in_array(array('Doctrine', 'modelsAutoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'modelsAutoload'), $splAutoloadFunctions)) {
                 spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
             }
             if ($this->hasParameter('models_directory')) {
                 Doctrine_Core::setModelsDirectory($this->getParameter('models_directory'));
             }
         }
         // for 1.2, handle extension autoloading, base paths and registration
         if ($is12 && ($this->hasParameter('extensions_path') || $this->hasParameter('register_extensions'))) {
             if (!in_array(array('Doctrine', 'extensionsAutoload'), $splAutoloadFunctions) && !in_array(array('Doctrine_Core', 'extensionsAutoload'), $splAutoloadFunctions)) {
                 spl_autoload_register(array('Doctrine_Core', 'extensionsAutoload'));
             }
             if ($this->hasParameter('extensions_path')) {
                 Doctrine_Core::setExtensionsPath($this->getParameter('extensions_path'));
             }
             foreach ((array) $this->getParameter('register_extensions', array()) as $extensionName) {
                 if (is_array($extensionName)) {
                     call_user_func_array(array($this->doctrineManager, 'registerExtension'), $extensionName);
                 } else {
                     $this->doctrineManager->registerExtension($extensionName);
                 }
             }
         }
         foreach ((array) $this->getParameter('bind_components', array()) as $componentName) {
             $this->doctrineManager->bindComponent($componentName, $name);
         }
     } catch (Doctrine_Exception $e) {
         // the connection's foobar'd
         throw new AgaviDatabaseException($e->getMessage());
     }
 }