/** * Initialize the application * * @param Application $application * @throws \Phire\Exception * @return Module */ public function register(Application $application) { parent::register($application); // Set the database if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); $db = count($this->application->getService('database')->getTables()) > 0; } else { $db = false; } $this->application->mergeConfig(['db' => $db]); // Check PHP version if (version_compare(PHP_VERSION, '5.4.0') < 0) { throw new \Phire\Exception('Error: Phire CMS requires PHP 5.4.0 or greater.'); } // Add route params for the controllers if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'console' => new Console(120, ' ')]); } // Set up triggers to check the application session $this->application->on('app.route.post', 'Phire\\Event\\Db::check', 1000); $this->application->on('app.route.pre', function () { if (isset($_SERVER['argv'][1]) && ($_SERVER['argv'][1] != 'sql' && $_SERVER['argv'][1] != 'archive')) { echo PHP_EOL . ' Phire Console' . PHP_EOL; echo ' =============' . PHP_EOL . PHP_EOL; } }, 1000); $this->application->on('app.dispatch.post', function () { echo PHP_EOL; }, 1000); return $this; }
public function bootstrap($autoloader = null) { parent::bootstrap($autoloader); $this->on('app.init', function ($application) { Record::setDb($application->services['database']); }); }
/** * Register module * * @param Application $application * @throws Exception * @return Module */ public function register(Application $application) { parent::register($application); // Add route params for the controllers if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'console' => new \Pop\Console\Console(120, ' ')]); } if (!empty($this->application->config()['database']) && !empty($this->application->config()['database']['adapter'])) { $adapter = $this->application->config()['database']['adapter']; $options = ['database' => $this->application->config()['database']['database'], 'username' => $this->application->config()['database']['username'], 'password' => $this->application->config()['database']['password'], 'host' => $this->application->config()['database']['host'], 'type' => $this->application->config()['database']['type']]; $check = \Pop\Db\Db::check($adapter, $options); if (null !== $check) { throw new Exception('DB ' . $check); } $this->application->services()->set('database', ['call' => 'Pop\\Db\\Db::connect', 'params' => ['adapter' => $adapter, 'options' => $options]]); } if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); } // Set up triggers to check the application session $this->application->on('app.route.pre', function () { if (isset($_SERVER['argv'][1])) { echo PHP_EOL . ' App Console' . PHP_EOL; echo ' ===========' . PHP_EOL . PHP_EOL; } }, 1000); $this->application->on('app.dispatch.post', function () { echo PHP_EOL; }, 1000); return $this; }
/** * Register module * * @param Application $application * @throws Exception * @return Module */ public function register(Application $application) { parent::register($application); if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'request' => new Request(), 'response' => new Response()]); } if (!empty($this->application->config()['database']) && !empty($this->application->config()['database']['adapter'])) { $adapter = $this->application->config()['database']['adapter']; $options = ['database' => $this->application->config()['database']['database'], 'username' => $this->application->config()['database']['username'], 'password' => $this->application->config()['database']['password'], 'host' => $this->application->config()['database']['host'], 'type' => $this->application->config()['database']['type']]; $check = \Pop\Db\Db::check($adapter, $options); if (null !== $check) { throw new Exception('DB ' . $check); } $this->application->services()->set('database', ['call' => 'Pop\\Db\\Db::connect', 'params' => ['adapter' => $adapter, 'options' => $options]]); } if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); } if (isset($this->config['forms'])) { $this->application->mergeConfig(['forms' => $this->config['forms']]); } if (isset($this->config['resources'])) { $this->application->mergeConfig(['resources' => $this->config['resources']]); } $this->application->on('app.route.pre', 'App\\Event\\Ssl::check', 1000)->on('app.dispatch.pre', 'App\\Event\\Session::check', 1001)->on('app.dispatch.pre', 'App\\Event\\Acl::check', 1000); $this->initNav(); return $this; }
public function __construct(array $columns = null, $table = null, Adapter\AbstractAdapter $db = null) { if (null === $db) { $db = new Adapter\Sqlite(['database' => __DIR__ . '/../../data/stats.sqlite']); } parent::__construct($columns, $table, $db); }
/** * Initialize the application * * @param Application $application * @throws Exception * @return Module */ public function register(Application $application) { parent::register($application); // Set the database if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); $db = count($this->application->getService('database')->getTables()) > 0; } else { $db = false; } $this->application->mergeConfig(['db' => $db]); // Load assets, if they haven't been loaded already $this->loadAssets(__DIR__ . '/../data/assets', 'phire'); if ($db) { $systemTheme = Table\Config::findById('system_theme')->value; if (file_exists(CONTENT_ABS_PATH . '/phire/themes/' . $systemTheme)) { $this->loadAssets(CONTENT_ABS_PATH . '/phire/themes/' . $systemTheme, $systemTheme); } else { if (file_exists(__DIR__ . '/../data/themes/' . $systemTheme)) { $this->loadAssets(__DIR__ . '/../data/themes/' . $systemTheme, $systemTheme); } } } else { $this->loadAssets(__DIR__ . '/../data/themes/default', 'default'); } sort($this->assets['js']); sort($this->assets['css']['link']); sort($this->assets['css']['import']); // Load any custom/override assets $this->loadAssets(CONTENT_ABS_PATH . '/phire/assets', 'phire-custom', true); // Check PHP version if (version_compare(PHP_VERSION, '5.4.0') < 0) { throw new Exception('Error: Phire CMS requires PHP 5.4.0 or greater.'); } // Add route params for the controllers if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'request' => new Request(), 'response' => new Response()]); } // Set up triggers to check the application session $this->application->on('app.route.pre', 'Phire\\Event\\Ssl::check', 1000)->on('app.route.post', 'Phire\\Event\\Db::check', 1000)->on('app.dispatch.pre', 'Phire\\Event\\Session::check', 1001)->on('app.dispatch.pre', 'Phire\\Event\\Acl::check', 1000); // Add roles to user nav $this->addRoles(); // Register modules $this->registerModules(); return $this; }
public function bootstrap($autoloader = null) { parent::bootstrap($autoloader); $this->on('app.init', function ($application) { Record::setDb($application->services['database']); }); if ($this->router->isCli()) { $this->on('app.route.pre', function () { echo PHP_EOL; echo ' Pop Tutorial CLI' . PHP_EOL; echo ' ----------------' . PHP_EOL . PHP_EOL; }); $this->on('app.dispatch.post', function () { echo PHP_EOL; echo ' ----------------' . PHP_EOL; echo ' Complete!' . PHP_EOL . PHP_EOL; }); } }
/** * Delete dynamic field files * * @param int $fieldId * @param int $modelId * @param string $model * @param boolean $encrypt * @param Application $app * @param string $uploadFolder * @param string $mediaLibrary * @return void */ protected static function saveFiles($fieldId, $modelId, $model, $encrypt, $app, $uploadFolder, $mediaLibrary = null) { $field = T\Fields::findById($fieldId); if (isset($field->id)) { $time = time(); $newValues = []; $oldValues = new Record(); $oldValues->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $oldValues->findRecordsBy(['model_id' => $modelId, 'model' => $model], ['order' => 'id ASC']); $old = $oldValues->rows(false); foreach ($_FILES as $key => $file) { $id = substr_count($key, '_') == 2 ? substr($key, strrpos($key, '_') + 1) : 0; if (!empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (null !== $mediaLibrary) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $old[$id]['value'])) { $media = new \Phire\Media\Model\Media(); $media->getByFile($old[$id]['value']); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } else { $newValues[] = $value; } copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $app->module('phire-fields')->config()['max_size'], $app->module('phire-fields')->config()['disallowed_types'], $app->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } } } else { $newValues[] = $value; } } } } foreach ($newValues as $v) { if (!empty($v)) { $fv = new Record(['model_id' => $modelId, 'model' => $model, 'timestamp' => $time, 'revision' => 0, 'value' => $v]); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->save(); $fvs = new Record(); $fvs->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $sql = $fvs->getSql(); $sql->update(['timestamp' => ':timestamp'])->where('model_id = :model_id')->where('model = :model'); $fvs->execute($sql, ['timestamp' => $time, 'model_id' => $modelId, 'model' => $model]); } } } }
/** * Constructor * * Instantiate a project object * * @param mixed $config * @param array $module * @param \Pop\Mvc\Router $router * @return \Pop\Project\Project */ public function __construct($config = null, array $module = null, Router $router = null) { if (null !== $config) { $this->loadConfig($config); } if (null !== $module) { $this->loadModule($module); } if (null !== $router) { $this->loadRouter($router); } $this->events = new \Pop\Event\Manager(); $this->services = new \Pop\Service\Locator(); if (isset($this->config->log)) { if (!file_exists($this->config->log)) { touch($this->config->log); chmod($this->config->log, 0777); } $this->logger = new \Pop\Log\Logger(new \Pop\Log\Writer\File(realpath($this->config->log))); } if (isset($this->config->defaultDb)) { $default = $this->config->defaultDb; \Pop\Db\Record::setDb($this->config->databases->{$default}); } }
public function testGetDbException() { $this->setExpectedException('Pop\\Db\\Exception'); $this->assertInstanceOf('Pop\\Db\\Db', Record::getDb()); }
/** * Method to write to the log * * @param array $logEntry * @param array $options * @return \Pop\Log\Writer\Db */ public function writeLog(array $logEntry, array $options = array()) { $this->table->setValues($logEntry)->save(); return $this; }
public function testSetValuesException() { $this->setExpectedException('Pop\\Db\\Exception'); $r = new Record(); $r->setValues(123); }
/** * Delete dynamic field values * * @param AbstractController $controller * @param Application $application * * @return void */ public static function delete(AbstractController $controller, Application $application) { if ($_POST) { $uploadFolder = BASE_PATH . CONTENT_PATH . '/files'; $mediaLibrary = $application->module('phire-fields')->config()['media_library']; foreach ($_POST as $key => $value) { if (substr($key, 0, 3) == 'rm_' && is_array($value)) { $fields = Table\Fields::findBy(); $fieldIds = []; $fieldTypes = []; $fieldStorage = []; foreach ($fields->rows() as $field) { $fieldIds[$field->id] = $field->name; $fieldTypes[$field->id] = $field->type; $fieldStorage[$field->id] = $field->storage; } foreach ($value as $id) { foreach ($fieldIds as $fieldId => $fieldName) { if ($fieldStorage[$fieldId] == 'eav') { $fv = Table\FieldValues::findBy(['model_id' => (int) $id]); if ($fv->hasRows()) { foreach ($fv->rows() as $f) { $fValue = json_decode($f->value, true); if (!is_array($fValue)) { $fValue = [$fValue]; } foreach ($fValue as $f) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f); } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f)) { $media = new \Phire\Media\Model\Media(); $media->getByFile($f); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } } } $fv = new Table\FieldValues(); $fv->delete(['model_id' => (int) $id]); } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $fieldName); $fv->findRecordsBy(['model_id' => (int) $id]); if ($fieldTypes[$fieldId] == 'file' && $fv->hasRows()) { foreach ($fv->rows() as $f) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value); } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f->value)) { $media = new \Phire\Media\Model\Media(); $media->getByFile($f->value); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } } $fv->delete(['model_id' => (int) $id]); } } } } } } }
/** * JSON models action method * * @param mixed $model * @param mixed $fid * @param mixed $marked * @return void */ public function json($model = null, $fid = null, $marked = null) { $json = []; // Get field validators and models if ($model == 0 && null !== $fid) { $field = Table\Fields::findById($fid); if (isset($field->id)) { $json['validators'] = null != $field->validators ? unserialize($field->validators) : []; $json['models'] = null != $field->models ? unserialize($field->models) : []; } // Get field values } else { if (null !== $fid && null == $marked && null !== $this->request->getQuery('model')) { $field = Table\Fields::findById($fid); if ($field->dynamic) { if ($field->storage == 'eav') { $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]); if (!empty($fv->value)) { $values = json_decode($fv->value, true); if (is_array($values)) { array_shift($values); } } else { $values = []; } } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model')], ['order' => 'id ASC']); $values = []; if ($fv->hasRows() && $fv->count() > 1) { $rows = $fv->rows(); for ($i = 1; $i < count($rows); $i++) { $values[] = $rows[$i]->value; } } } $json['values'] = $values; } // Get field history values } else { if (null !== $fid && null !== $marked && null !== $this->request->getQuery('model')) { $field = Table\Fields::findById($fid); $value = ''; if (isset($field->id)) { if ($field->storage == 'eav') { $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]); if (isset($fv->field_id) && null !== $fv->history) { $history = json_decode($fv->history, true); if (isset($history[$marked])) { $value = $history[$marked]; $f = Table\Fields::findById($fid); if ($f->encrypt) { $value = (new \Pop\Crypt\Mcrypt())->decrypt($value); } } } $json['fieldId'] = $fid; $json['modelId'] = $model; $json['model'] = $this->request->getQuery('model'); $json['value'] = $value; } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model'), 'timestamp' => $marked], ['order' => 'id ASC']); if (isset($fv->model_id)) { $value = $fv->value; if ($field->encrypt) { $value = (new \Pop\Crypt\Mcrypt())->decrypt($value); } } $json['fieldId'] = $fid; $json['modelId'] = $model; $json['model'] = $this->request->getQuery('model'); $json['value'] = $value; } } // Get field models } else { $model = rawurldecode($model); $models = $this->application->module('phire-fields')->config()['models']; if (isset($models[$model])) { $json = $models[$model]; } } } } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }
<?php /* * 2015 Lace Cart * * @author LaceCart Dev <*****@*****.**> * @copyright 2015 LaceCart Team * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of LaceCart Team */ use Pop\Db\Db as DB; use Pop\Db\Record as Adapter; return ['services' => ['session' => ['call' => 'Pop\\Web\\Session::getInstance'], 'db' => ['call' => function () use($config) { Adapter::setDb(DB::connect($config->database->adapter, ['database' => $config->database->database, 'username' => $config->database->username, 'password' => $config->database->password, 'host' => $config->database->host])); }], 'config' => ['call' => function () use($config) { return $config; }], 'nav' => ['call' => function () use($nav) { return $nav; }]]];
/** * Get field values for a model object * * @param mixed $model * @param int $id * @param array $filters * @return mixed */ public static function getModelObjectValues($model, $id = null, array $filters = []) { if (is_string($model)) { $class = $model; } else { $class = get_class($model); if (isset($model->id)) { $id = $model->id; } } $fieldValues = []; $sql = Table\Fields::sql(); $sql->select()->where('models LIKE :models'); $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%' . $class . '%' : '%' . addslashes($class) . '%'; $fields = Table\Fields::execute((string) $sql, ['models' => $value]); if (null !== $id && $fields->count() > 0) { foreach ($fields->rows() as $field) { $fValue = ''; if ($field->storage == 'eav') { $fv = Table\FieldValues::findById([$field->id, $id, $class]); if (isset($fv->field_id)) { $fValue = json_decode($fv->value); foreach ($filters as $filter => $params) { if (null !== $params && count($params) > 0) { $params = array_merge([$fValue], $params); } else { $params = [$fValue]; } $fValue = call_user_func_array($filter, $params); } if ($field->encrypt) { if (is_array($fValue)) { foreach ($fValue as $k => $fv) { $fValue = (new Mcrypt())->decrypt($fValue); } } else { $fValue = (new Mcrypt())->decrypt($fValue); } } } } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->findRecordsBy(['model_id' => $id, 'model' => $class, 'revision' => 0]); if ($fv->hasRows()) { if ($fv->count() > 1) { $fValue = []; foreach ($fv->rows() as $f) { $fValue[] = $field->encrypt ? (new Mcrypt())->decrypt($f->value) : $f->value; } } else { $fValue = $field->encrypt ? (new Mcrypt())->decrypt($fv->value) : $fv->value; } } } if (is_object($model)) { $model->{$field->name} = self::parse($fValue); } else { $fieldValues[$field->name] = self::parse($fValue); } } } return is_object($model) ? $model : $fieldValues; }
public function testGetDefaultDb() { Record::setDb(Db::factory('Sqlite', array('database' => __DIR__ . '/../tmp/test.sqlite')), true); $this->assertInstanceOf('Pop\\Db\\Db', Record::getDb()); }