/**
  * Takes the inputted form value, encrypts it,
  * and saves it back to the database
  * @param mixed $data Data to write to the DB
  * @return boolean Success
  * @version 2013052301
  * @since 2012101001
  */
 public function write_setting($data)
 {
     // If data value is an integer...
     if ($this->paramtype === PARAM_INT and $data === '') {
         // Do not complain if '' used instead of 0
         $data = 0;
     }
     // If data value is a string...
     $validated = $this->validate($data);
     if ($validated !== true) {
         return $validated;
     }
     // Encrypt value before write
     $encrypter = new MoodletxtEncryption();
     $key = get_config('moodletxt', 'EK');
     $encrypted = $encrypter->encrypt($key, $data);
     return $this->config_write($this->name, $encrypted) ? '' : get_string('errorsetting', 'admin');
 }
 /**
  * Builds authentication block.
  * Method to build an <Authentication> block for authenticating a request with the API
  * @param TxttoolsAccount $txttoolsAccount txttools account to authenticate against
  * @version 2011041501
  * @since 2010090101
  */
 private function buildAuthentication($txttoolsAccount)
 {
     $outPass = $this->encrypter->decrypt(get_config('moodletxt', 'EK'), $txttoolsAccount->getEncryptedPassword());
     // Set authentication block
     // Code written this way so the output is all nice and formatted
     $this->currentAuthentication = '
 ' . MoodletxtXMLConstants::$REQUEST_AUTHENTICATION_BLOCK . '
     ' . MoodletxtXMLConstants::$REQUEST_AUTHENTICATION_USERNAME . $txttoolsAccount->getUsername() . MoodletxtXMLConstants::$_REQUEST_AUTHENTICATION_USERNAME . '
     ' . MoodletxtXMLConstants::$REQUEST_AUTHENTICATION_PASSWORD . $outPass . MoodletxtXMLConstants::$_REQUEST_AUTHENTICATION_PASSWORD . '
 ' . MoodletxtXMLConstants::$_REQUEST_AUTHENTICATION_BLOCK;
 }
Example #3
0
function xmldb_block_moodletxt_upgrade($oldversion = 0)
{
    global $CFG, $DB;
    $dbman = $DB->get_manager();
    // Get database manager
    $BLOCK_RECORD = $DB->get_record('block', array('name' => 'moodletxt'));
    /**
     * Any release before 2.4 is invalid. User must
     * be on at least moodletxt 2.4 before upgrading to 3.0.
     */
    if ($oldversion < 2011032901) {
        throw new upgrade_exception('moodletxt', 2012052901, 'Upgrading to moodletxt 3.0 from below moodletxt 2.4 is not supported. Please ensure you are running at least moodletxt 2.4 before upgrading.');
    }
    /**
     * moodletxt 2.4.1 - the inevitable round
     * of bugfixes from the big release in April
     * Mainly processing fixes. Corrected a bug in the admin panel,
     * some backporting issues, and added GPLv3 licencing headers.
     */
    if ($oldversion < 2011101101) {
        // This database field somehow snuck into the upgrade script
        // and started causing trouble, despite the fact that it's never used.
        // Ever. Not once.  So let's murder it to death!
        $table = new xmldb_table('block_mtxt_outbox');
        $field = new xmldb_field('suppresunicode', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null);
        // Spelling is correct...
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Save point!
        upgrade_block_savepoint(true, 2011101101, 'moodletxt');
    }
    /**
     * moodletxt 2.4.2 - Changes required for plugin validation
     * in the new repository. I've got to rename all my tables!
     * Again! Be nice if I had any space left in 
     * 30 characters after the three mandatory prefixes, but whatever.
     * Apologies to Oracle users everywhere.
     */
    if ($oldversion < 2011101201) {
        $table = new xmldb_table('block_mtxt_ab');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_ab');
        }
        $table = new xmldb_table('block_mtxt_ab_entry');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_ab_entry');
        }
        $table = new xmldb_table('block_mtxt_ab_groups');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_ab_group');
        }
        $table = new xmldb_table('block_mtxt_ab_users');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_ab_u');
        }
        $table = new xmldb_table('block_mtxt_accounts');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_accounts');
        }
        $table = new xmldb_table('block_mtxt_config');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_config');
        }
        $table = new xmldb_table('block_mtxt_filter');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_filter');
        }
        $table = new xmldb_table('block_mtxt_in_ab');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_in_ab');
        }
        $table = new xmldb_table('block_mtxt_in_filter');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_in_fil');
        }
        $table = new xmldb_table('block_mtxt_in_folders');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_in_fold');
        }
        $table = new xmldb_table('block_mtxt_in_mess');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_in_mess');
        }
        $table = new xmldb_table('block_mtxt_in_user');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_in_u');
        }
        $table = new xmldb_table('block_mtxt_inbox');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_inbox');
        }
        $table = new xmldb_table('block_mtxt_outbox');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_outbox');
        }
        $table = new xmldb_table('block_mtxt_rss');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_rss');
        }
        $table = new xmldb_table('block_mtxt_sent');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_sent');
        }
        $table = new xmldb_table('block_mtxt_sent_ab');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_sent_ab');
        }
        $table = new xmldb_table('block_mtxt_sent_user');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_sent_u');
        }
        $table = new xmldb_table('block_mtxt_stats');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_stats');
        }
        $table = new xmldb_table('block_mtxt_status');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_status');
        }
        $table = new xmldb_table('block_mtxt_templates');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_templ');
        }
        $table = new xmldb_table('block_mtxt_uconfig');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_uconfig');
        }
        upgrade_block_savepoint(true, 2011101201, 'moodletxt');
    }
    if ($oldversion < 2011101202) {
        $table = new xmldb_table('block_mtxt_ab_grpmem');
        if ($dbman->table_exists($table)) {
            $dbman->rename_table($table, 'block_moodletxt_ab_gmem');
        }
        upgrade_block_savepoint(true, 2011101202, 'moodletxt');
    }
    /**
     * 2012052301 is 2.4.3
     * Upgrade script patches from 2.4.2 - no DB upgrades 
     */
    if ($oldversion < 2012052301) {
        // Nothing to do
        upgrade_block_savepoint(true, 2012052301, 'moodletxt');
    }
    /**
     * moodletxt 3.0 beta 1
     * Full re-write to Moodle 2.x
     * New fancy inbox page with tags instead of folders
     */
    if ($oldversion < 2012052901) {
        $configTable = new xmldb_table('block_moodletxt_config');
        if ($dbman->table_exists($configTable)) {
            // Port all config to Moodle config table
            $configRecords = $DB->get_records('block_moodletxt_config');
            foreach ($configRecords as $configRecord) {
                set_config($configRecord->setting, $configRecord->value, 'moodletxt');
            }
            // Drop old table
            $dbman->drop_table($configTable);
        }
        $passwordsAlreadyUpgraded = get_config('moodletxt', 'Passwords_Upgraded_3_0');
        if ($passwordsAlreadyUpgraded === false || $passwordsAlreadyUpgraded != 1) {
            // Upgrade all passwords to be compatible with new
            // encryption class.  Might as well improve the
            // encryption key while we're here
            $oldKey = get_config('moodletxt', 'EK');
            $newKey = substr(md5(mt_rand()), 0, 10);
            $legacyEncrypter = new MoodletxtLegacyEncryption();
            $newEncrypter = new MoodletxtEncryption();
            $accountRecords = $DB->get_records('block_moodletxt_accounts');
            foreach ($accountRecords as $accountRecord) {
                $rawPassword = $legacyEncrypter->decrypt($oldKey, $accountRecord->password);
                $accountRecord->password = $newEncrypter->encrypt($newKey, $rawPassword, 20);
                $DB->update_record('block_moodletxt_accounts', $accountRecord);
            }
            // Save new EK
            set_config('EK', $newKey, 'moodletxt');
            set_config('Passwords_Upgraded_3_0', 1, 'moodletxt');
        }
        // Kill off old ENUM fields - no longer supported in Moodle 2.0
        $table = new xmldb_table('block_moodletxt_filter');
        $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '7', null, XMLDB_NOTNULL, null, 'KEYWORD', 'account');
        if ($dbman->field_exists($table, $field) && $dbman->check_constraint_exists($table, $field)) {
            $dbman->drop_enum_from_field($table, $field);
        }
        $table = new xmldb_table('block_moodletxt_ab');
        $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '7', null, XMLDB_NOTNULL, null, 'global', 'name');
        if ($dbman->field_exists($table, $field) && $dbman->check_constraint_exists($table, $field)) {
            $dbman->drop_enum_from_field($table, $field);
        }
        // Split sent name on sent messages table into first and last names
        // This is for compatibility with the new recipient set within moodletxt
        $table = new xmldb_table('block_moodletxt_sent');
        $field = new xmldb_field('sendfirstname', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, get_string('fragunknownname', 'block_moodletxt'), 'destination');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('sendlastname', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, get_string('fragunknownname', 'block_moodletxt'), 'sendfirstname');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Move records into new structure and drop the old field
        $field = new xmldb_field('sendname');
        if ($dbman->field_exists($table, $field)) {
            $sentMessages = $DB->get_records('block_moodletxt_sent');
            if (count($sentMessages) > 0) {
                foreach ($sentMessages as $sentMessage) {
                    $nameFrags = explode(" ", $sentMessage->sendname);
                    $sentMessage->sendlastname = str_replace(",", " ", $nameFrags[0]);
                    $sentMessage->sendfirstname = $nameFrags[1];
                }
                $DB->update_record('block_moodletxt_sent', $sentMessage, true);
            }
            $dbman->drop_field($table, $field);
        }
        // Split source name on inbox messages table into first name and last name
        // Again, for compatibility with new recipient set
        $table = new xmldb_table('block_moodletxt_in_mess');
        $field = new xmldb_field('sourcefirstname', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, get_string('fragunknownname', 'block_moodletxt'), 'sourcename');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('sourcelastname', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, get_string('fragunknownname', 'block_moodletxt'), 'sourcefirstname');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Move records into new structure and drop old field
        $field = new xmldb_field('sourcename');
        if ($dbman->field_exists($table, $field)) {
            $receivedMessages = $DB->get_records('block_moodletxt_in_mess');
            if (count($receivedMessages) > 0) {
                foreach ($receivedMessages as $receivedMessage) {
                    $nameFrags = explode(" ", $receivedMessage->sourcename);
                    $receivedMessage->sourcelastname = str_replace(",", " ", $nameFrags[0]);
                    $receivedMessage->sourcefirstname = $nameFrags[1];
                }
                $DB->update_record('block_moodletxt_in_mess', $receivedMessage, true);
            }
            $dbman->drop_field($table, $field);
        }
        // Create new table to restrict outbound access on user accounts
        $table = new xmldb_table('block_moodletxt_restrict');
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('txttoolsaccount', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('moodleuser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('fk-txttoolsaccount', XMLDB_KEY_FOREIGN, array('txttoolsaccount'), 'block_moodletxt_accounts', array('id'));
        $table->add_key('fk-moodleuser', XMLDB_KEY_FOREIGN, array('moodleuser'), 'user', array('id'));
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        /*
         * RE-WORKING INBOXES
         * Dropping folders and replacing them with tags.
         */
        // Add any new fields and keys to message table
        $table = new xmldb_table('block_moodletxt_in_mess');
        $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
            // Grrr, why doesn't ADODB have a method for checking key existence??
            $key = new xmldb_key('fk-message-user', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
            $dbman->add_key($table, $key);
        }
        // Add any new fields and keys to filter link table
        $table = new xmldb_table('block_moodletxt_in_fil');
        $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'filter');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
            $key = new xmldb_key('fk-filter-user', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
            $dbman->add_key($table, $key);
        }
        // Adds any new fields and keys to accounts table
        $table = new xmldb_table('block_moodletxt_accounts');
        $field = new xmldb_field('defaultuser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
            // This key needs to be dropped earlier than we would normally drop it,
            // as fk-default-inbox and fk-default-user get converted to the same
            // key name at the database level!
            $key = new xmldb_key('fk-default-inbox', XMLDB_KEY_FOREIGN, array('defaultinbox'), 'block_moodletxt_inbox', array('id'));
            $dbman->drop_key($table, $key);
            $key = new xmldb_key('fk-default-user', XMLDB_KEY_FOREIGN, array('defaultuser'), 'user', array('id'));
            $dbman->add_key($table, $key);
        }
        // Create new table for inbox tags
        $table = new xmldb_table('block_moodletxt_tags');
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
        $table->add_field('colour', XMLDB_TYPE_CHAR, '7', null, null, null, null);
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('fk-tag-owner', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Create new table to link tags to messages
        $table = new xmldb_table('block_moodletxt_in_tag');
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('message', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('tag', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Move message data into new structure
        $sql = 'SELECT folders.*, usertable.id as userid
                FROM {block_moodletxt_in_fold} folders
                INNER JOIN {block_moodletxt_inbox} inbox
                    ON folders.inbox = inbox.id
                INNER JOIN {user} usertable
                    ON inbox.userid = usertable.id';
        $folderSet = $DB->get_records_sql($sql);
        foreach ($folderSet as $folder) {
            $DB->set_field('block_moodletxt_in_mess', 'userid', $folder->userid, array('folderid' => $folder->id));
        }
        // Move filter and account data into new structure
        $sql = 'SELECT inbox.id as inboxid, usertable.id as userid
                FROM {block_moodletxt_inbox} inbox                
                INNER JOIN {user} usertable
                    ON inbox.userid = usertable.id';
        $inboxSet = $DB->get_records_sql($sql);
        foreach ($inboxSet as $inbox) {
            $DB->set_field('block_moodletxt_in_fil', 'userid', $inbox->userid, array('inbox' => $inbox->inboxid));
            $DB->set_field('block_moodletxt_accounts', 'defaultuser', $inbox->userid, array('defaultinbox' => $inbox->inboxid));
        }
        // Change folders into tags - heavy update
        foreach ($folderSet as $folder) {
            // Write new tag record
            $tagObject = new object();
            $tagObject->userid = $folder->userid;
            $tagObject->name = $folder->name;
            $tagObject->colour = '#ffffff';
            // Default to white highlighting - non-disruptive
            $tagId = $DB->insert_record('block_moodletxt_tags', $tagObject);
            $messagesWithThisTag = $DB->get_records('block_moodletxt_in_mess', array('folderid' => $folder->id), '', 'id');
            foreach ($messagesWithThisTag as $message) {
                // Write new link record
                $linkObject = new object();
                $linkObject->message = $message->id;
                $linkObject->tag = $tagId;
                $DB->insert_record('block_moodletxt_in_tag', $linkObject);
            }
        }
        // Drop old fields and keys from message table
        $table = new xmldb_table('block_moodletxt_in_mess');
        $key = new xmldb_key('fk-message-folder', XMLDB_KEY_FOREIGN, array('folderid'), 'block_moodletxt_in_fold', array('id'));
        // ->drop_key() has no return value. Consistency!
        $dbman->drop_key($table, $key);
        $field = new xmldb_field('folderid');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Drop old fields and keys from filter link table
        $table = new xmldb_table('block_moodletxt_in_fil');
        $key = new xmldb_key('fk-filter-inbox', XMLDB_KEY_FOREIGN, array('inbox'), 'moodletxt_inbox', array('id'));
        $dbman->drop_key($table, $key);
        $field = new xmldb_field('inbox');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Drop old fields and keys from user account table
        $table = new xmldb_table('block_moodletxt_accounts');
        // Foreign key for default inbox field has already been dropped above for bugfix
        $field = new xmldb_field('defaultinbox');
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Drop inbox folder table
        $table = new xmldb_table('block_moodletxt_in_fold');
        if ($dbman->table_exists($table)) {
            $dbman->drop_table($table);
        }
        // Drop inbox parent table
        $table = new xmldb_table('block_moodletxt_inbox');
        if ($dbman->table_exists($table)) {
            $dbman->drop_table($table);
        }
        // We have to drop keys pointing at the messages table before we
        // can rename it, then re-apply the keys
        $table = new xmldb_table('block_moodletxt_in_ab');
        $key = new xmldb_key('fk-receivedmessage', XMLDB_KEY_FOREIGN, array('receivedmessage'), 'block_moodletxt_in_mess', array('id'));
        $dbman->drop_key($table, $key);
        $table = new xmldb_table('block_moodletxt_in_u');
        $key = new xmldb_key('fk-receivedmessage', XMLDB_KEY_FOREIGN, array('receivedmessage'), 'block_moodletxt_in_mess', array('id'));
        $dbman->drop_key($table, $key);
        // Rename messages table to be the inbox table
        $table = new xmldb_table('block_moodletxt_in_mess');
        $dbman->rename_table($table, 'block_moodletxt_inbox');
        // Re-apply foreign keys into renamed messages table
        $table = new xmldb_table('block_moodletxt_in_ab');
        $key = new xmldb_key('fk-receivedmessage', XMLDB_KEY_FOREIGN, array('receivedmessage'), 'block_moodletxt_inbox', array('id'));
        $dbman->add_key($table, $key);
        $table = new xmldb_table('block_moodletxt_in_u');
        $key = new xmldb_key('fk-receivedmessage', XMLDB_KEY_FOREIGN, array('receivedmessage'), 'block_moodletxt_inbox', array('id'));
        $dbman->add_key($table, $key);
        // Save point!
        upgrade_block_savepoint(true, 2012052901, 'moodletxt');
    }
    /**
     * moodletxt 3.0 beta 2
     * Fixing code errors with users missing phone numbers 
     */
    if ($oldversion < 2012060101) {
        upgrade_block_savepoint(true, 2012060101, 'moodletxt');
    }
    /**
     * moodletxt 3.0 final
     * Adding support for moodletxt+ and encrypted settings
     */
    if ($oldversion < 2012103001) {
        // Initialise event messaging settings if they don't exist
        if (get_config('moodletxt', 'Event_Messaging_Account') === false) {
            set_config('Event_Messaging_Account', '0', 'moodletxt');
        }
        // Add a flag to the outbox to show whether this is an event-generated message
        $table = new xmldb_table('block_moodletxt_outbox');
        $field = new xmldb_field('fromevent', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'type');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // In the betas, password values coming from settings.php were not encrypted.
        // This is now supported, so let's encrypt any existing passwords.
        $passwordsAlreadyUpgraded = get_config('moodletxt', 'Settings_Encrypted_3_0');
        if ($passwordsAlreadyUpgraded === false || $passwordsAlreadyUpgraded != 1) {
            $key = get_config('moodletxt', 'EK');
            $pushPassword = get_config('moodletxt', 'Push_Password');
            $proxyPassword = get_config('moodletxt', 'Proxy_Password');
            $encrypter = new MoodletxtEncryption();
            if ($pushPassword != '') {
                $pushPassword = $encrypter->encrypt($key, $pushPassword);
                set_config('Push_Password', $pushPassword, 'moodletxt');
            }
            if ($proxyPassword != '') {
                $proxyPassword = $encrypter->encrypt($key, $proxyPassword);
                set_config('Proxy_Password', $proxyPassword, 'moodletxt');
            }
            set_config('Settings_Encrypted_3_0', '1', 'moodletxt');
        }
        upgrade_block_savepoint(true, 2012103001, 'moodletxt');
    }
    /**
     * 3.0.1 release
     * Patch related to running the block on MySQL 5.1 and above
     */
    if ($oldversion < 2012110501) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2012110501, 'moodletxt');
    }
    /**
     * 3.0.2 release
     * Patched permissions error on sent page, updated CSS image links,
     * removed disabled accounts from filter management listing.
     */
    if ($oldversion < 2012112901) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2012112901, 'moodletxt');
    }
    /**
     * 3.0.3 release
     * Patches for Moodle 2.4 compatibility (table display library).
     * Fixed error with scheduling on the send page.
     */
    if ($oldversion < 2013011001) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013011001, 'moodletxt');
    }
    /**
     * 3.0.4 release
     * Patching two Javascript issues:
     *  -Number of accounts incorrectly calculated in control panel 
     *   (table display library in 2.4)
     *  -Javascript caching broken by malformed CDATA comment in lib.js
     */
    if ($oldversion < 2013032101) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013032101, 'moodletxt');
    }
    /**
     * 3.0.5-rc1 release
     * Prepping Moodletxt for Moodlerooms distribution and updating
     * for better compatibility with Moodle 2.5
     * AJAX fixes on admin panel
     * Group display fix on compose page
     * Users can now blank names when editing addressbook contacts
     * Fixed rare installation issue with settings.php being called before run
     */
    if ($oldversion < 2013061901) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013061901, 'moodletxt');
    }
    /**
     * 3.0.5-rc2 release
     * Prepping Moodletxt for Moodlerooms distribution
     * Added missing 'addinstance' capability
     * Removed unused 'viewmoodletxtpluslogs' capability
     * Removed deprecated get_context_* calls in Moodle 2.2 and above
     */
    if ($oldversion < 2013070203) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013070203, 'moodletxt');
    }
    /**
     * 3.0.5 release
     * No changes since RC2
     */
    if ($oldversion < 2013071001) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013071001, 'moodletxt');
    }
    /**
     * 3.0.6 RC1 release
     * Put out for testing of changes to cron job
     */
    if ($oldversion < 2013081601) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013081601, 'moodletxt');
    }
    /**
     * 3.0.6 RC2 release
     * Put out for testing of inbound messaging fix
     */
    if ($oldversion < 2013102301) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013102301, 'moodletxt');
    }
    /**
     * 3.0.6 final release
     * Couple of minor UI fixes added since RC2
     */
    if ($oldversion < 2013112901) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2013112901, 'moodletxt');
    }
    /**
     * 3.0.7 final release
     * Various compatibility and rendering fixes
     */
    if ($oldversion < 2014071401) {
        // Delete config keys that are no longer required
        set_config('jQuery_Include_Enabled', null, 'moodletxt');
        set_config('jQuery_UI_Include_Enabled', null, 'moodletxt');
        upgrade_block_savepoint(true, 2014071401, 'moodletxt');
    }
    /**
     * 3.0.8 final release
     * "Compose a Message" user interface "ballooning" in Moodlerooms Snap Theme
     * requires revisions to send.php.
     */
    if ($oldversion < 2015021301) {
        // Nothing to do - code patches only
        upgrade_block_savepoint(true, 2015021301, 'moodletxt');
    }
    /**
     * 3.1.0 final release
     * Added support for US ConnectTxt accounts
     */
    if ($oldversion < 2015070301) {
        // Alter accounts table to store ConnectTxt server's url and location, modify unique key
        $table = new xmldb_table('block_moodletxt_accounts');
        $urlField = new xmldb_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'www.bbconnecttxt.com', 'lastupdate');
        $locationField = new xmldb_field('location', XMLDB_TYPE_CHAR, '2', null, XMLDB_NOTNULL, null, 'UK', 'url');
        // Conditionally launch add field url.
        if (!$dbman->field_exists($table, $urlField)) {
            $dbman->add_field($table, $urlField);
        }
        if (!$dbman->field_exists($table, $locationField)) {
            $dbman->add_field($table, $locationField);
        }
        $oldUniqueUsernameKey = new xmldb_key('username', XMLDB_KEY_UNIQUE, array('username'));
        $newUniqueUsernameKey = new xmldb_key('username', XMLDB_KEY_UNIQUE, array('username', 'url'));
        $dbman->drop_key($table, $oldUniqueUsernameKey);
        $dbman->add_key($table, $newUniqueUsernameKey);
        // Create blacklist table
        $table = new xmldb_table('block_moodletxt_blacklist');
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('account_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('phone_number', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('is_blacklisted', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('blacklist_account_fk', XMLDB_KEY_FOREIGN, array('account_id'), 'block_moodletxt_accounts', array('id'));
        $table->add_key('account_phonenumber_unique', XMLDB_KEY_UNIQUE, array('account_id', 'phone_number'));
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Changing precision of field statusmessage on table block_moodletxt_status to (255).
        $table = new xmldb_table('block_moodletxt_status');
        $statusMessageField = new xmldb_field('statusmessage', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'status');
        $statusField = new xmldb_field('status', XMLDB_TYPE_INTEGER, 1, false, XMLDB_NOTNULL, null, null, 'ticketnumber');
        // Launch change of precision for field statusmessage.
        $dbman->change_field_precision($table, $statusMessageField);
        $dbman->change_field_precision($table, $statusField);
        // Moodletxt savepoint reached.
        upgrade_block_savepoint(true, 2015070301, 'moodletxt');
    }
    return true;
}
                 }
             }
         }
         // if there already are other accounts saved in the DB use the same URL and location for all new ones
     } else {
         $acc = $accountDAO->getUrlAndLocation();
         $location = $acc->location;
         $url = $acc->url;
     }
     if ($accountDAO->accountNameExists((string) $formData->accountName, $url)) {
         $formErrors[] = get_string('erroraccountexists', 'block_moodletxt');
     } else {
         if ($formData->accountCtxtInstance !== TxttoolsAccount::$UK_LOCATION && $formData->accountCtxtInstance !== TxttoolsAccount::$US_LOCATION && ($formData->accountCtxtInstance !== 'URL' && $formData->accountUrl)) {
             $formErrors[] = get_string('errorinvalidlocationorurl', 'block_moodletxt');
         } else {
             $encrypter = new MoodletxtEncryption();
             $key = get_config('moodletxt', 'EK');
             $xmlController = MoodletxtOutboundControllerFactory::getOutboundController(MoodletxtOutboundControllerFactory::$CONTROLLER_TYPE_XML);
             $defaultUser = $userDAO->getUserById($formData->accountDefaultInbox);
             $txttoolsAccount = new TxttoolsAccount((string) $formData->accountName, (string) $formData->accountDescription, $defaultUser, $url, $location);
             $txttoolsAccount->setEncryptedPassword($encrypter->encrypt($key, $formData->accountPassword1, 20));
             if ($location === 'URL') {
                 $location = $xmlController->getServerLocation($txttoolsAccount);
                 $txttoolsAccount->setLocation($location);
             }
             $txttoolsAccount = $xmlController->updateAccountInfo($txttoolsAccount);
             $accountDAO->saveTxttoolsAccount($txttoolsAccount);
             redirect($CFG->wwwroot . '/blocks/moodletxt/settings_accounts.php', get_string('redirectaccountsfound', 'block_moodletxt'));
         }
     }
 } catch (MoodletxtRemoteProcessingException $ex) {
Example #5
0
 * @version 2013052301
 * @since 2012101001
 */
require_once '../../config.php';
require_once $CFG->dirroot . '/blocks/moodletxt/lib/MoodletxtEncryption.php';
require_once $CFG->dirroot . '/blocks/moodletxt/connect/xml/MoodletxtXMLParser.php';
require_once $CFG->dirroot . '/blocks/moodletxt/dao/TxttoolsSentMessageDAO.php';
require_once $CFG->dirroot . '/blocks/moodletxt/dao/TxttoolsReceivedMessageDAO.php';
require_once $CFG->dirroot . '/blocks/moodletxt/inbound/MoodletxtInboundFilterManager.php';
// Read in POST variables
$inPushUser = required_param('u', PARAM_ALPHANUM);
$inPushPass = required_param('p', PARAM_ALPHANUM);
$inPayload = required_param('x', PARAM_RAW);
// Assuming we have the right params, set up for parsing
$parser = new MoodletxtXMLParser();
$decrypter = new MoodletxtEncryption();
$sentMessagesDAO = new TxttoolsSentMessageDAO();
$receivedMessagesDAO = new TxttoolsReceivedMessageDAO();
$inboundFilterManager = new MoodletxtInboundFilterManager();
$key = get_config('moodletxt', 'EK');
$pushUsername = get_config('moodletxt', 'Push_Username');
$pushPassword = $decrypter->decrypt($key, get_config('moodletxt', 'Push_Password'));
// Check credentials against those stored in Moodle
if ($inPushUser === $pushUsername && $inPushPass === $pushPassword) {
    $parsedInboundMessages = array();
    $parsedStatusUpdates = array();
    try {
        $parsedObjects = $parser->parse($inPayload);
    } catch (Exception $ex) {
        // Invalid XML from remote system
        die;
 /**
  * Updates some details of a ConnectTxt account
  * based on a user's input rather than a remote call
  * @param int $accountId ConnectTxt account ID
  * @param string $description ConnectTxt account description
  * @param object $decodedJson JSON request
  * @return string JSON response
  * @throws MoodletxtAJAXException
  * @version 2013061701
  * @since 2012100801
  */
 private function updateAccountFromUser($accountId, $description, $newPassword)
 {
     $txttoolsAccount = $this->accountDAO->getTxttoolsAccountById($accountId);
     // Account must exist within system
     if (!is_object($txttoolsAccount)) {
         throw new MoodletxtAJAXException(get_string('errorinvalidaccountid', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_CODE_BAD_ACCOUNT_ID, null, false, $accountId);
     }
     $txttoolsAccount->setDescription($description);
     // Set new encrypted password on object and attempt
     // to connect to ConnectTxt with it
     // (There's a method to check account validity but
     // we might as well update info while we're at it.)
     if ($newPassword != '') {
         $encrypter = new MoodletxtEncryption();
         $key = get_config('moodletxt', 'EK');
         $txttoolsAccount->setEncryptedPassword($encrypter->encrypt($key, $newPassword, 20));
         // Update from ConnectTxt server
         try {
             $this->connector->updateAccountInfo($txttoolsAccount);
         } catch (MoodletxtRemoteProcessingException $ex) {
             throw new MoodletxtAJAXException(get_string('errorconn' . $ex->getCode(), 'block_moodletxt'), $ex->getCode(), null, true, $txttoolsAccount->getId());
         } catch (Exception $ex) {
             throw new MoodletxtAJAXException(get_string('errorconndefault', 'block_moodletxt'), $ex->getCode(), null, true, $txttoolsAccount->getId());
         }
     }
     $this->accountDAO->saveTxttoolsAccount($txttoolsAccount);
     return $this->buildResponse($txttoolsAccount);
 }