function Test_db()
 {
     require_once AK_CONTRIB_DIR . '/adodb/adodb.inc.php';
     $db =& Ak::db();
     $this->assertFalse(!$db, 'Connecting to the database. Please check your test_config.php file in order to set up a copy of $dns into $GLOBALS["ak_test_db_dns"]');
     $this->assertReference($db, Ak::db(), 'Checking db connection singleton');
 }
 function Test_db()
 {
     require_once AK_CONTRIB_DIR . '/adodb/adodb.inc.php';
     if (substr($GLOBALS['ak_test_db_dns'], 0, 6) == 'mysql:') {
         $GLOBALS['ak_test_db_dns'] = substr_replace($GLOBALS['ak_test_db_dns'], 'mysqlt:', 0, 6);
     }
     $db =& Ak::db($GLOBALS['ak_test_db_dns']);
     $this->assertFalse(!$db, 'Connecting to the database. Please check your test_config.php file in order to set up a copy of $dns into $GLOBALS["ak_test_db_dns"]');
     $this->assertReference($db, Ak::db($GLOBALS['ak_test_db_dns']), 'Checking db connection singleton');
 }
Exemple #3
0
 function AkInstaller($db_connection = null)
 {
     if (empty($db_connection)) {
         $this->db =& Ak::db();
     } else {
         $this->db =& $db_connection;
     }
     $this->available_tables = $this->getAvailableTables();
     $this->db->debug =& $this->debug;
     $this->data_dictionary = NewDataDictionary($this->db);
 }
Exemple #4
0
 public function test_all_session_handlers()
 {
     $cacheHandlers = array('cache_lite' => 1);
     if (!Ak::db() instanceof AkSqliteDbAdapter) {
         $cacheHandlers['akadodbcache'] = 2;
     }
     if (AkConfig::getOption('memcached_enabled', false)) {
         $cacheHandlers['akmemcache'] = 3;
     }
     $unitTests = array('_Test_open', '_Test_read_write', '_Test_destroy', '_Test_gc');
     foreach ($cacheHandlers as $class => $type) {
         foreach ($unitTests as $test) {
             $this->{$test}($type, $class);
         }
     }
 }
Exemple #5
0
 function createTable($table_name, $table_fields, $table_options, $add_sequence_table = true, $table_index_fields = null)
 {
     if (!isset($this->_db)) {
         $db =& Ak::db();
     } else {
         $db =& $this->_db;
     }
     $dict = NewDataDictionary($db);
     $sqlarray = $dict->CreateTableSQL($table_name, $table_fields, $table_options);
     $dict->ExecuteSQLArray($sqlarray);
     if (isset($table_index_fields)) {
         $sqlarray = $dict->CreateIndexSQL('idx_' . $table_name, $table_name, $table_index_fields);
         $dict->ExecuteSQLArray($sqlarray);
     }
     if ($add_sequence_table) {
         $db->CreateSequence('seq_' . $table_name);
     }
 }
Exemple #6
0
    function runFrameworkInstaller()
    {
        static $unique_dsn = array();
        require_once(AK_LIB_DIR.DS.'AkInstaller.php');
        require_once(AK_APP_DIR.DS.'installers'.DS.'framework_installer.php');

        foreach (array('production', 'development') as $mode){
            $dsn = $this->_getDsn($mode);
            if(!isset($unique_dsn[$dsn])){
                $db_conn =& Ak::db($dsn, $mode);
                $installer =& new FrameworkInstaller($db_conn);
                $installer->install(null, array('mode' => $mode));
                $unique_dsn[$dsn] = true;
            }
        }

        return true;
    }
Exemple #7
0
 function runFrameworkInstaller()
 {
     require_once AK_LIB_DIR . DS . 'AkInstaller.php';
     require_once AK_APP_DIR . DS . 'installers' . DS . 'framework_installer.php';
     foreach (array('development', 'production', 'testing') as $mode) {
         $db_conn = Ak::db($this->_getDsn($mode));
         $installer = new FrameworkInstaller($db_conn);
         $installer->install();
     }
     return true;
 }
Exemple #8
0
 /**
 * Set the connection for the class.
 */
 function setConnection($dns = null, $connection_id = null)
 {
     $this->_db =& Ak::db($dns, $connection_id);
 }
Exemple #9
0
// | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
// | Released under the GNU Lesser General Public License, see LICENSE.txt|
// +----------------------------------------------------------------------+
/**
 * @package ActiveSupport
 * @subpackage Scripts
 * @author Bermi Ferrer <bermi a.t akelos c.om>
 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
 */
error_reporting(defined('AK_ERROR_REPORTING_ON_SCRIPTS') ? AK_ERROR_REPORTING_ON_SCRIPTS : 0);
require_once AK_LIB_DIR . DS . 'Ak.php';
require_once AK_LIB_DIR . DS . 'AkObject.php';
require_once AK_LIB_DIR . DS . 'AkInflector.php';
require_once AK_LIB_DIR . DS . 'AkPhpParser.php';
defined('AK_SKIP_DB_CONNECTION') && AK_SKIP_DB_CONNECTION ? $dsn = '' : Ak::db(&$dsn);
defined('AK_RECODE_UTF8_ON_CONSOLE_TO') ? null : define('AK_RECODE_UTF8_ON_CONSOLE_TO', false);
require_once AK_LIB_DIR . DS . 'AkActiveRecord.php';
require_once AK_LIB_DIR . DS . 'AkActionMailer.php';
require_once AK_APP_DIR . DS . 'shared_model.php';
require_once AK_LIB_DIR . DS . 'utils' . DS . 'generators' . DS . 'AkelosGenerator.php';
require_once AK_LIB_DIR . DS . 'AkInstaller.php';
if ($id_dir = opendir(AK_MODELS_DIR . DS)) {
    while (false !== ($file = readdir($id_dir))) {
        if (substr($file, 0, 1) != '.' && $file[0] != '_' && substr($file, -12, 8) != '_service') {
            if (!is_dir(AK_MODELS_DIR . DS . $file) && preg_match('/^[a-z0-9_]+\\.php$/', $file)) {
                include_once AK_MODELS_DIR . DS . $file;
            }
        }
    }
    closedir($id_dir);
Exemple #10
0
<?php

error_reporting(E_ALL);
defined('AK_ENVIRONMENT') ? null : define('AK_ENVIRONMENT', 'testing');
defined('AK_TEST_DIR') ? null : define('AK_TEST_DIR', str_replace(DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php', '', __FILE__));
defined('AK_APP_DIR') ? null : define('AK_APP_DIR', AK_TEST_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'app');
defined('AK_PUBLIC_DIR') ? null : define('AK_PUBLIC_DIR', AK_TEST_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'public');
defined('AK_TEST_HELPERS_DIR') ? null : define('AK_TEST_HELPERS_DIR', AK_TEST_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'helpers');
define('AK_SITE_URL_SUFFIX', str_replace(array(join(DIRECTORY_SEPARATOR, array_diff((array) @explode(DIRECTORY_SEPARATOR, AK_TEST_DIR), (array) @explode('/', @$_SERVER['REQUEST_URI']))), DIRECTORY_SEPARATOR), array('', '/'), AK_TEST_DIR));
defined('AK_ENABLE_AKELOS_ARGS') ? null : define('AK_ENABLE_AKELOS_ARGS', true);
//define('AK_SKIP_DB_CONNECTION',isset($db) && $db === false);
defined('AK_URL_REWRITE_ENABLED') ? null : define('AK_URL_REWRITE_ENABLED', true);
include_once substr(AK_TEST_DIR, 0, -5) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';
defined('AK_APP_LOCALES') ? null : define('AK_APP_LOCALES', 'en,es');
defined('AK_PUBLIC_LOCALES') ? null : define('AK_PUBLIC_LOCALES', AK_APP_LOCALES);
defined('AK_ACTIVE_RECORD_INTERNATIONALIZE_MODELS_BY_DEFAULT') ? null : define('AK_ACTIVE_RECORD_INTERNATIONALIZE_MODELS_BY_DEFAULT', true);
defined('AK_ACTIVE_RECORD_ENABLE_AUTOMATIC_SETTERS_AND_GETTERS') ? null : define('AK_ACTIVE_RECORD_ENABLE_AUTOMATIC_SETTERS_AND_GETTERS', true);
defined('AK_TESTING_URL') ? null : define('AK_TESTING_URL', rtrim(AK_URL, '/') . '/test/fixtures/public');
defined('AK_TESTING_REWRITE_BASE') ? null : define('AK_TESTING_REWRITE_BASE', false);
defined('AK_LIB_TESTS_DIRECTORY') ? null : define('AK_LIB_TESTS_DIRECTORY', AK_TEST_DIR . DS . 'unit' . DS . 'lib');
if (AK_TESTING_REWRITE_BASE) {
    Ak::file_put_contents(AK_BASE_DIR . '/test/fixtures/public/.htaccess', str_replace('# RewriteBase /test/fixtures/public', 'RewriteBase ' . AK_TESTING_REWRITE_BASE, Ak::file_get_contents(AK_BASE_DIR . '/test/fixtures/public/.htaccess')));
}
if (defined('AK_TEST_DATABASE_ON')) {
    $default_profile = array_key_exists($argv[count($argv) - 1], $database_settings) ? array_pop($argv) : AK_ENVIRONMENT;
    define('AK_DEFAULT_DATABASE_PROFILE', $default_profile);
    include_once AK_LIB_DIR . DS . 'Ak.php';
    Ak::db();
    unset($default_profile);
}
require_once AK_LIB_DIR . DS . 'AkUnitTest.php';
Exemple #11
0
 /**
  * Log a system message into the database.
  *
  * @param $type The category to which this message belongs. Usually a model or a controller.
  * @param $message The message to store in the log.
  * @param $severity The severity of the message. One of the following values:
  * - AK_LOG_NOTICE
  * - AK_LOG_WARNING
  * - AK_LOG_ERROR
  * @param $link A link to associate with the message.
  */
 function log($type, $message, $severity = AK_LOG_NOTICE, $link = NULL)
 {
     $DB =& Ak::db();
     $DB->debug = true;
     /**
      * @todo use a function like Ak::get_user_id() to fetch user id and insert it into the log.
      * meanwhile we will use user 0;
      */
     $user_id = 0;
     $sql = "INSERT INTO log ( user_id, type, message, severity, link, location, hostname, created )\n                VALUES (" . $user_id . ', ' . $DB->qstr($type) . ', ' . $DB->qstr($message) . ', ' . $severity . ', ' . $DB->qstr($link) . ', ' . $DB->qstr(AK_REQUEST_URI) . ', ' . $DB->qstr($_SERVER['REMOTE_ADDR']) . ', ' . $DB->DBTimeStamp(Ak::time()) . ');';
     if ($DB->Execute($sql) === false) {
         trigger_error('Error inserting: ' . $DB->ErrorMsg(), E_USER_WARNING);
     }
 }
Exemple #12
0
 /**
  * Session open handler
  *
  * @access protected
  * @return boolean
  */
 function _open()
 {
     $this->_db =& Ak::db();
     return true;
 }
Exemple #13
0
 public function dropTables($tables = array())
 {
     $installer = new AkInstaller();
     if (is_string($tables) && $tables == 'all') {
         $tables = Ak::db()->getAvailableTables();
     }
     foreach ($tables as $table) {
         $installer->dropTable($table, array('sequence' => true));
     }
 }
Exemple #14
0
<?php

# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
$doc_db_file = AK_DOCS_DIR . DS . 'akelos' . DS . 'doc.sqlite';
$installed = file_exists($doc_db_file);
$db = Ak::db('sqlite://' . urlencode($doc_db_file) . '/?persist');
AkConfig::rebaseApp(AK_PLUGINS_DIR . DS . 'doc_builder');
if (!$installed) {
    $DocInstaller = new DocInstaller($db);
    $DocInstaller->install();
}
$SourceAnalyzer = new SourceAnalyzer();
$SourceAnalyzer->db = $db;
$SourceAnalyzer->storeFilesForIndexing(AK_ACTION_MAILER_DIR);
$SourceAnalyzer->indexFiles();
//unlink($doc_db_file);
return;
$dir_iterator = new RecursiveDirectoryIterator(AK_ACTION_MAILER_DIR);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
    if ($file->isFile()) {
        echo $file, "\n";
        $original_file = file_get_contents($file);
        $Reflection = new AkReflectionFile($file);
        foreach ($Reflection->getClasses() as $Class) {
            echo 'class: ' . $Class->getName() . "\n";
            $defined_methods = array('public' => array(), 'protected' => array(), 'private' => array());
            foreach ($Class->getMethods() as $Method) {
                $name = $Method->getName();
Exemple #15
0
 /**
  * Set the connection for the class.
  */
 function setConnection($dns = null)
 {
     $this->_db =& Ak::db($dns);
 }
Exemple #16
0
 static function uninstall()
 {
     $db = Ak::db();
     if ($db->tableExists('sessions')) {
         $Installer = new AkInstaller($db);
         $Installer->dropTable('sessions');
     }
 }
if(isset($_GET['expire'])){
    define('AK_SESSION_EXPIRE', (int)$_GET['expire']);
}

define('AK_ENVIRONMENT', 'testing');

defined('AK_TEST_DIR') ? null : define('AK_TEST_DIR', str_replace(DIRECTORY_SEPARATOR.'mocks'.DIRECTORY_SEPARATOR.'test_script_AkDbSession.php','',__FILE__));
defined('AK_APP_DIR') ? null : define('AK_APP_DIR', AK_TEST_DIR.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'app');

define('AK_SITE_URL_SUFFIX',str_replace(array(join(DIRECTORY_SEPARATOR,array_diff((array)@explode(DIRECTORY_SEPARATOR,AK_TEST_DIR),
(array)@explode('/',@$_SERVER['REQUEST_URI']))),DIRECTORY_SEPARATOR),array('','/'),AK_TEST_DIR));

include('..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

require_once(AK_LIB_DIR.DS.'Ak.php');
Ak::db(&$dsn);

require_once(AK_LIB_DIR.DS.'AkDbSession.php');

$AkDbSession = new AkDbSession();
$AkDbSession->session_life = AK_SESSION_EXPIRE;
session_set_save_handler (
array(&$AkDbSession, '_open'),
array(&$AkDbSession, '_close'),
array(&$AkDbSession, '_read'),
array(&$AkDbSession, '_write'),
array(&$AkDbSession, '_destroy'),
array(&$AkDbSession, '_gc')
);

session_start();
Exemple #18
0
 /**
  * Akelos version of pcntl_fork wich prevents forked processes from killing the database connection.
  * 
  * See http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
  * 
  * This is done by closing the connection before forking and reconnecting on the child & parent process.
  * 
  * @return Same as pcntl_fork (PID of the children to the parent, 0 to the children process and -1 if fails).
  */
 public function pcntl_fork()
 {
     $db = Ak::db();
     $can_connect = (isset($db->connection) && method_exists($db->connection, 'connect'));
     // Disconnect on the parent so we we don't have a zombie connection once the child closes the reused connection
     $can_connect && $db->connection->close();
     $pid = pcntl_fork();
     // Connecting on the child process
     $can_connect && $db->connection->connect();
     // Reconect on the parent
     $pid > 0 && $can_connect && $db->connection->connect();
     return $pid;
 }
Exemple #19
0
    /**
    * Class constructor (ALA Akelos Framework)
    *
    * @access public
    * @param    array    $options    
    * <code>
    * $options = array(
    * //This options are valid for both cache contains (database and file based)
    * 'lifeTime' => cache lifetime in seconds (int),
    * 'memoryCaching' => enable / disable memory caching (boolean),
    * 'automaticSerialization' => enable / disable automatic serialization (boolean)
    * );
    * </code>
    * @return void
    */
    function init($options = array())
    {
        $this->_db =& Ak::db();

        $available_options = array('memoryCaching', 'lifeTime', 'automaticSerialization');
        foreach($options as $key => $value) {
            if(in_array($key, $available_options)) {
                $property = '_'.$key;
                $this->$property = $value;
            }
        }
        $this->_refreshTime = time() - $this->_lifeTime;
    }
Exemple #20
0
 function _saveLogInDatabase($type, $error_number, $error_message, $filename, $line_number)
 {
     $db =& Ak::db();
     $message = $this->_getLogFormatedAsRawText($type, $error_number, $error_message, $filename, $line_number);
     $sql = 'INSERT INTO log (user_id, type, message, severity, location, hostname, created) ' . " VALUES (0, " . $db->qstr($type) . ", " . $db->qstr($message) . ', ' . ($this->mode & AK_MODE_DIE ? 100 : 0) . ', ' . $db->qstr(AK_CURRENT_URL) . ', ' . $db->qstr($_SERVER['REMOTE_ADDR']) . ', ' . $db->qstr(Ak::getTimestamp()) . ');';
     if ($db->Execute($sql) === false) {
         die($this->internalError($this->t('Error inserting: ') . $db->ErrorMsg(), __FILE__, __LINE__));
     }
 }
Exemple #21
0
 public function runTaskAsDaemon($task_name, $options = array())
 {
     $this->_ensurePosixAndPcntlAreAvailable();
     require_once 'System/Daemon.php';
     $app_name = AkInflector::underscore($task_name);
     $pid_file = AK_BASE_DIR . DS . 'run' . DS . $app_name . DS . $app_name . '.pid';
     $log_file = AK_LOG_DIR . DS . 'daemons' . DS . $app_name . '.log';
     if (!file_exists($pid_file)) {
         if (empty($options['attributes']['kill'])) {
             AkFileSystem::file_put_contents($pid_file, '');
             AkFileSystem::file_delete($pid_file);
         } else {
             $this->error("Could not kill process for {$task_name}", true);
         }
     } else {
         $pid = (int) file_get_contents($pid_file);
         if ($pid > 0) {
             if (!empty($options['attributes']['kill'])) {
                 $this->message("Killing process {$pid}");
                 `kill {$pid}`;
                 AkFileSystem::file_delete($pid_file);
                 die;
             } elseif (!empty($options['attributes']['restart'])) {
                 $this->message("Restarting {$task_name}.");
                 $this->message(`kill {$pid}`);
             } else {
                 $this->error("Daemon for {$task_name} still running ({$pid_file}).\nTask aborted.", true);
             }
         }
     }
     if (!empty($options['attributes']['kill']) && empty($pid)) {
         $this->error("No daemon running for task {$task_name}", true);
     }
     unset($options['attributes']['restart']);
     if (!file_exists($log_file)) {
         AkFileSystem::file_put_contents($log_file, '');
     }
     System_Daemon::setOption('appName', $app_name);
     System_Daemon::setOption('appDir', AK_BASE_DIR);
     System_Daemon::setOption('logLocation', $log_file);
     System_Daemon::setOption('appRunAsUID', posix_geteuid());
     System_Daemon::setOption('appRunAsGID', posix_getgid());
     System_Daemon::setOption('appPidLocation', $pid_file);
     $this->message("Staring daemon. ({$log_file})");
     System_Daemon::start();
     $dsn = Ak::getStaticVar('dsn');
     defined('AK_SKIP_DB_CONNECTION') && AK_SKIP_DB_CONNECTION ? null : Ak::db($dsn);
     $this->runTask($task_name, $options);
     System_Daemon::stop();
     AkFileSystem::file_delete($pid_file);
     die;
 }
Exemple #22
0
 function _connectToDatabase()
 {
     global $dsn;
     if(AK_AUTOMATIC_DB_CONNECTION){
         if(!empty($dsn)){
             Ak::db($dsn);
         }
     }
 }
 public function test_init()
 {
     $this->db = Ak::db();
     $this->WeHaveAPostgreSqlEnvironment = $this->db->type() === 'postgre';
 }
Exemple #24
0
<?php

$db = Ak::db();
$charset = "utf8";
$collation = "utf8_unicode_ci";
$rs = $db->execute('SHOW FULL TABLES');
echo "\n";
while (!$rs->EOF) {
    $table_name = array_shift($rs->fields);
    $table_type = array_shift($rs->fields);
    if ('BASE TABLE' == $table_type) {
        echo "Checking table {$table_name} ";
        $tcs = $db->execute("SHOW CREATE TABLE {$table_name}");
        $sql = array_pop($tcs->fields);
        if (!preg_match('/ENGINE=.+ (DEFAULT CHARSET=' . $charset . ' COLLATE=' . $collation . ')/', $sql, $matches)) {
            echo " fixing charset ";
            $db->execute('ALTER TABLE ' . $table_name . ' DEFAULT CHARACTER SET ' . $charset . ' COLLATE ' . $collation);
        } else {
            echo " charset OK ";
        }
        $rst = $db->execute('SHOW FULL COLUMNS FROM ' . $table_name);
        while (!$rst->EOF) {
            if (!empty($rst->fields['Collation']) && !strstr($rst->fields['Collation'], $collation)) {
                echo " fixing column {$rst->fields['Field']} ";
                $db->execute("ALTER TABLE {$table_name} MODIFY {$rst->fields['Field']} {$rst->fields['Type']} character set {$charset} collate {$collation} " . ($rst->fields['Null'] == 'Yes' ? '' : ' not null ') . (empty($rst->fields['Default']) ? $rst->fields['Null'] == 'Yes' ? ' default NULL' : '' : ' default ' . $rst->fields['Default']));
            }
            $rst->MoveNext();
        }
        echo "\n";
    }
    $rs->MoveNext();
Exemple #25
0
 function _connectToDatabase()
 {
     if (AK_AUTOMATIC_DB_CONNECTION) {
         Ak::db(AK_DEFAULT_DATABASE_PROFILE);
     }
 }
Exemple #26
0
 /**
  * This methods are used before triggering an Application Controller
  */
 function __connectToDatabase()
 {
     global $dsn;
     if (!empty($dsn)) {
         Ak::db($dsn);
         $this->__database_connection_available = AK_DATABASE_CONNECTION_AVAILABLE;
     }
 }