public function save(MessageStack $errors)
 {
     $editing = isset($this->parameters()->{'root-element'}) ? $this->parameters()->{'root-element'} : false;
     // About info:
     if (!isset($this->about()->name) || empty($this->about()->name)) {
         $errors->append('about::name', __('This is a required field'));
     }
     try {
         $existing = self::loadFromHandle($this->handle);
     } catch (DataSourceException $e) {
         //	Datasource not found, continue!
     }
     if ($existing instanceof Datasource && $editing != $this->handle) {
         throw new DataSourceException(__('A Datasource with the name <code>%s</code> already exists', array($this->about()->name)));
     }
     // Save type:
     if ($errors->length() <= 0) {
         $user = Administration::instance()->User;
         if (!file_exists($this->getTemplate())) {
             $errors->append('write', __("Unable to find Data Source Type template '%s'.", array($this->getTemplate())));
             throw new DataSourceException(__("Unable to find Data Source Type template '%s'.", array($this->getTemplate())));
         }
         $this->parameters()->{'root-element'} = $this->handle;
         $classname = Lang::createHandle(ucwords($this->about()->name), '_', false, true, array('/[^a-zA-Z0-9_\\x7f-\\xff]/' => NULL), true);
         $pathname = DATASOURCES . "/" . $this->handle . ".php";
         $data = array($classname, var_export($this->about()->name, true), var_export($user->getFullName(), true), var_export(URL, true), var_export($user->email, true), var_export('1.0', true), var_export(DateTimeObj::getGMT('c'), true));
         foreach ($this->parameters() as $value) {
             $data[] = trim(General::var_export($value, true, is_array($value) ? 5 : 0));
         }
         if (General::writeFile($pathname, vsprintf(file_get_contents($this->getTemplate()), $data), Symphony::Configuration()->core()->symphony->{'file-write-mode'})) {
             if ($editing !== false && $editing != $this->handle) {
                 General::deleteFile(DATASOURCES . '/' . $editing . '.php');
             }
             return $pathname;
         }
         $errors->append('write', __('Failed to write datasource "%s" to disk.', array($filename)));
     }
     throw new DataSourceException('Errors were encountered whilst attempting to save.');
 }
示例#2
0
 public static function save(self $view, MessageStack &$messages, $simulate = false)
 {
     if (!isset($view->title) || strlen(trim($view->title)) == 0) {
         $messages->append('title', __('Title is required.'));
     }
     $pathname = sprintf('%s/%s/%s.config.xml', VIEWS, $view->path, $view->handle);
     if (file_exists($pathname)) {
         $existing = self::loadFromPath($view->path);
         if ($existing->guid != $view->guid) {
             $messages->append('handle', 'A view with that handle already exists.');
         }
         unset($existing);
     }
     if (isset($view->types) && is_array($view->types) && (bool) array_intersect($view->types, array('index', '404', '403'))) {
         foreach ($view->types as $t) {
             switch ($t) {
                 case 'index':
                 case '404':
                 case '403':
                     $views = self::findFromType($t);
                     if (isset($views[$view->guid])) {
                         unset($views[$view->guid]);
                     }
                     if (!empty($views)) {
                         $messages->append('types', __('A view of type "%s" already exists.', array($t)));
                         break 2;
                     }
                     break;
             }
         }
     }
     if (strlen(trim($view->template)) == 0) {
         $messages->append('template', 'Template is required, and cannot be empty.');
     } elseif (!General::validateXML($view->template, $errors)) {
         $fragment = Administration::instance()->Page->createDocumentFragment();
         $fragment->appendChild(new DOMText(__('This document is not well formed. The following error was returned: ')));
         $fragment->appendChild(Administration::instance()->Page->createElement('code', $errors->current()->message));
         $messages->append('template', $fragment);
     }
     if ($messages->length() > 0) {
         throw new ViewException(__('View could not be saved. Validation failed.'), self::ERROR_MISSING_OR_INVALID_FIELDS);
     }
     if ($simulate != true) {
         if (!is_dir(dirname($pathname)) && !mkdir(dirname($pathname), intval(Symphony::Configuration()->core()->symphony->{'directory-write-mode'}, 8), true)) {
             throw new ViewException(__('Could not create view directory. Please check permissions on <code>%s</code>.', $view->path), self::ERROR_FAILED_TO_WRITE);
         }
         // Save the config
         if (!General::writeFile($pathname, (string) $view, Symphony::Configuration()->core()->symphony->{'file-write-mode'})) {
             throw new ViewException(__('View configuration XML could not be written to disk. Please check permissions on <code>%s</code>.', $view->path), self::ERROR_FAILED_TO_WRITE);
         }
         // Save the template file
         $result = General::writeFile(sprintf('%s/%s/%s.xsl', VIEWS, $view->path, $view->handle), $view->template, Symphony::Configuration()->core()->symphony->{'file-write-mode'});
         if (!$result) {
             throw new ViewException(__('Template could not be written to disk. Please check permissions on <code>%s</code>.', $view->path), self::ERROR_FAILED_TO_WRITE);
         }
     }
     return true;
 }
 public static function save(self $role, MessageStack &$errors)
 {
     // Validation
     if (strlen(trim($role->name)) == 0) {
         $errors->append('name', __('Name is required.'));
     } elseif (Symphony::Database()->query("SELECT `id` FROM `tbl_uac_roles` WHERE `name` = '%s' %s", array($role->name, isset($role->id) ? "AND `id` != {$role->id} " : NULL))->length() > 0) {
         $errors->append('name', __('A role with that name already exists.'));
     }
     if ($errors->length() > 0) {
         throw new RoleException('Errors were encountered whist attempting to save.');
     }
     // Saving
     $result = Symphony::Database()->insert('tbl_uac_roles', array('id' => $role->id, 'name' => $role->name, 'description' => $role->description), Database::UPDATE_ON_DUPLICATE);
     if (!isset($role->id)) {
         $role->id = $result;
     }
     Symphony::Database()->delete('tbl_uac_permissions', array($role->id), '`role_id` = %d');
     foreach ($role->permissions as $name => $level) {
         list($key, $type) = preg_split('/\\./', $name, 2, PREG_SPLIT_NO_EMPTY);
         Symphony::Database()->insert('tbl_uac_permissions', array('id' => NULL, 'role_id' => $role->id, 'key' => $key, 'type' => $type, 'level' => $level));
     }
     return $result;
 }
示例#4
0
 public function validateSettings(MessageStack $messages, $checkForDuplicates = true)
 {
     $parent_section = $this->{'parent-section'};
     if (!isset($this->name) || strlen(trim($this->name)) == 0) {
         $messages->append('name', __('This is a required field.'));
     }
     if (!isset($this->{'element-name'}) || strlen(trim($this->{'element-name'})) == 0) {
         $messages->append('element-name', __('This is a required field.'));
     } else {
         if (!preg_match('/^[A-z]([\\w\\d-_\\.]+)?$/i', $this->{'element-name'})) {
             $messages->append('element-name', __('Invalid element name. Must be valid QName.'));
         }
     }
     /*
     TODO: Replace this with something:
     else if($checkForDuplicates) {
     	$sql_id = ($this->id ? " AND f.id != '".$this->id."' " : '');
     
     	$query = sprintf("
     			SELECT
     				f.*
     			FROM
     				`tbl_fields` AS f
     			WHERE
     				f.element_name = '%s'
     				%s
     				AND f.parent_section = '%s'
     			LIMIT
     				1
     		",
     		$element_name,
     		$sql_id,
     		$parent_section
     	);
     
     	if (Symphony::Database()->query($query)->valid()) {
     		$messages->append("field::{$index}::element-name", __('A field with that element name already exists. Please choose another.'));
     	}
     }
     */
     if ($messages->length() > 0) {
         return Field::STATUS_ERROR;
     }
     return Field::STATUS_OK;
 }
示例#5
0
 $settings['user'] = array_map('trim', $settings['user']);
 // Missing username
 // Missing password
 // Missing first name
 // Missing Last name
 // Missing Email Address
 if (missing(array($settings['user']['username'], $settings['user']['password'], $settings['user']['first-name'], $settings['user']['last-name'], $settings['user']['email-address']))) {
     $errors->append('user', 'Username, Password, First Name, Last Name and Email Address are all required fields.');
 } elseif (preg_match('/[\\s]/i', $settings['user']['username'])) {
     $errors->append('user', 'Username is invalid.');
 } elseif ($settings['user']['password'] != $settings['user']['confirm-password']) {
     $errors->append('user', 'Passwords do not match.');
 } elseif (!preg_match('/^\\w(?:\\.?[\\w%+-]+)*@\\w(?:[\\w-]*\\.)+?[a-z]{2,}$/i', $settings['user']['email-address'])) {
     $errors->append('user', 'Email Address is invalid.');
 }
 if ($errors->length() == 0) {
     /// Create a DB connection --------------------------------------------------------------------------
     $db = new DBCMySQLProfiler();
     $db->character_encoding = 'utf8';
     $db->character_set = 'utf8';
     $db->force_query_caching = false;
     $db->prefix = $settings['database']['table-prefix'];
     $connection_string = sprintf('mysql://%s:%s@%s:%s/%s/', $settings['database']['username'], $settings['database']['password'], $settings['database']['host'], $settings['database']['port'], $settings['database']['database']);
     try {
         $db->connect($connection_string);
     } catch (DatabaseException $e) {
         $errors->append('database', 'Could not establish database connection. The following error was returned: ' . $e->getMessage());
     }
     if ($errors->length() == 0) {
         $permission = intval($settings['server-preferences']['directory-permissions'], 8);
         /// Create the .htaccess ------------------------------------------------------------------
示例#6
0
 public static function save(Section $section, MessageStack $messages, $essentials = null, $simulate = false)
 {
     $pathname = sprintf('%s/%s.xml', $section->path, $section->handle);
     // Check to ensure all the required section fields are filled
     if (!isset($section->name) || strlen(trim($section->name)) == 0) {
         $messages->append('name', __('This is a required field.'));
     } elseif (file_exists($pathname)) {
         $existing = self::load($pathname);
         if (isset($existing->guid) and $existing->guid != $section->guid) {
             $messages->append('name', __('A Section with the name <code>%s</code> already exists', array($section->name)));
         }
         unset($existing);
     }
     ## Check to ensure all the required section fields are filled
     if (!isset($section->{'navigation-group'}) || strlen(trim($section->{'navigation-group'})) == 0) {
         $messages->append('navigation-group', __('This is a required field.'));
     }
     if (is_array($section->fields) && !empty($section->fields)) {
         foreach ($section->fields as $index => $field) {
             $field_stack = new MessageStack();
             if ($field->validateSettings($field_stack, false, false) != Field::STATUS_OK) {
                 $messages->append("field::{$index}", $field_stack);
             }
         }
     }
     if ($messages->length() > 0) {
         throw new SectionException(__('Section could not be saved. Validation failed.'), self::ERROR_MISSING_OR_INVALID_FIELDS);
     }
     if ($simulate) {
         return true;
     }
     $section->sanitizeLayout();
     return file_put_contents($pathname, (string) $section);
 }
示例#7
0
 public function validate(MessageStack $errors)
 {
     if (is_null($this->first_name)) {
         $errors->append('first_name', __('First name is required'));
     }
     if (is_null($this->last_name)) {
         $errors->append('last_name', __('Last name is required'));
     }
     if (is_null($this->email)) {
         $errors->append('email', __('E-mail address is required'));
     } elseif (!General::validateString($this->email, '/^[^@]+@[^\\.@]+\\.[^@]+$/i')) {
         $errors->append('email', __('E-mail address entered is invalid'));
     }
     if (is_null($this->username)) {
         $errors->append('username', __('Username is required'));
     } elseif ($this->id) {
         $result = Symphony::Database()->query("SELECT `username` FROM `tbl_users` WHERE `id` = %d", array($this->id));
         $current_username = $result->current()->username;
         if ($current_username != $this->username && Symphony::Database()->query("SELECT `id` FROM `tbl_users` WHERE `username` = '%s'", array($this->username))->valid()) {
             $errors->append('username', __('Username is already taken'));
         }
     } elseif (Symphony::Database()->query("SELECT `id` FROM `tbl_users` WHERE `username` = '%s'", array($this->username))->valid()) {
         $errors->append('username', __('Username is already taken'));
     }
     if (is_null($this->password)) {
         $errors->append('password', __('Password is required'));
     }
     return $errors->length() == 0;
 }