Example #1
0
	/**
	 * Sets up the fixture, for example, opens a network connection.
	 * This method is called before a test is executed.
	 *
	 * @access protected
	 */
	protected function module_setup()
	{
		$user = User::get_by_name( 'posts_test' );
		if ( !$user ) {
			$user = User::create( array (
				'username'=>'posts_test',
				'email'=>'*****@*****.**',
				'password'=>md5('q' . rand( 0,65535 ) ),
			) );
		}
		$this->user = $user;
		$post = Post::create( array(
			'title' => 'Test Post',
			'content' => 'These tests expect there to be at least one post.',
			'user_id' => $user->id,
			'status' => Post::status( 'published' ),
			'content_type' => Post::type( 'entry' ),
		) );

		$this->post_id = $post->id;
		$this->paramarray = array(
			'id' => 'foofoo',
			'post_id' => $this->post_id,
			'name' => 'test',
			'email' => '*****@*****.**',
			'url' => 'http://example.org',
			'ip' => ip2long('127.0.0.1'),
			'content' => 'test content',
			'status' => Comment::STATUS_UNAPPROVED,
			'date' => HabariDateTime::date_create(),
			'type' => Comment::COMMENT
		);
		$this->comment = Comment::create( $this->paramarray );
	}
Example #2
0
	protected function teardown()
	{
		foreach ( $this->posts as $post ) {
			$post->delete();
		}
		unset($this->posts);
		$user = User::get_by_name( 'posts_test' );
		$user->delete;
	}
Example #3
0
 protected function setUp()
 {
     set_time_limit(0);
     $user = User::get_by_name('posts_test');
     if (!$user) {
         $user = User::create(array('username' => 'posts_test', 'email' => '*****@*****.**', 'password' => md5('q' . rand(0, 65535))));
     }
     $this->user = $user;
 }
Example #4
0
 /**
  * Set up for the whole test suite
  */
 protected function module_setup()
 {
     set_time_limit(0);
     if ($user = User::get_by_name('eventlog_test')) {
         $this->user = $user;
         //$this->skip_all("User {$user->id} is required 'posts_test' user.");
     } else {
         $this->user = User::create(array('username' => 'eventlog_test', 'email' => '*****@*****.**', 'password' => md5('q' . rand(0, 65535))));
     }
     $this->types = array('testing' => 'unit_tests', 'testing2' => 'unit_tests_2', 'testing3' => 'unit_tests_3', 'testing4' => 'unit_tests_4', 'testing5' => 'unit_tests_5');
     foreach ($this->types as $type => $module) {
         EventLog::register_type($type, $module);
     }
 }
Example #5
0
 public function action_auth_ajax_wp_import_users()
 {
     // get the values post'd in
     $inputs = $_POST->filter_keys(array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'category_import', 'import_index'));
     $inputs = $inputs->getArrayCopy();
     // make sure we have all our default values
     $inputs = array_merge($this->default_values, $inputs);
     // get the wpdb
     $wpdb = $this->wp_connect($inputs['db_host'], $inputs['db_name'], $inputs['db_user'], $inputs['db_pass']);
     // if we couldn't connect, error out
     if (!$wpdb) {
         EventLog::log(_t('Failed to import from "%s"', array($inputs['db_name'])));
         Session::error(_t('Failed to import from "%s"', array($inputs['db_name'])));
         echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
     }
     // we connected just fine, let's get moving!
     // begin a transaction. if we error out at any point, we want to roll back to before import began
     DB::begin_transaction();
     // fetch all the users from the wordpress database
     $wp_users = $wpdb->get_results('select id, user_login, user_pass, user_email, user_url, display_name from ' . $inputs['db_prefix'] . 'users');
     echo '<p>' . _t('Importing Users&hellip;') . '</p>';
     foreach ($wp_users as $wp_user) {
         // see if a user with this username already exists
         $user = User::get_by_name($wp_user->user_login);
         if ($user !== false) {
             // if the user exists, save their old ID into an info attribute
             $user->info->wp_id = intval($wp_user->id);
             // and update
             $user->update();
             echo '<p>' . _t('Associated imported user %1$s with existing user %2$s', array($wp_user->user_login, $user->username)) . '</p>';
             EventLog::log(_t('Associated imported user %1$s with existing user %2$s', array($wp_user->user_login, $user->username)));
         } else {
             // no user exists, we need to create one
             try {
                 $u = new User();
                 $u->username = $wp_user->user_login;
                 $u->email = $wp_user->user_email;
                 // set their password so the user will be able to login. they're auto-added to the 'authenticated' ACL group
                 $u->password = Utils::crypt($wp_user->user_pass);
                 $u->info->wp_id = intval($wp_user->id);
                 $u->info->displayname = $wp_user->display_name;
                 if ($wp_user->user_url != '') {
                     $u->info->url = $wp_user->user_url;
                 }
                 // and save it
                 $u->insert();
                 echo '<p>' . _t('Created new user %1$s. Their old ID was %2$d.', array($u->username, $wp_user->id)) . '</p>';
                 EventLog::log(_t('Created new user %1$s. Their old ID was %2$d.', array($u->username, $wp_user->id)));
             } catch (Exception $e) {
                 // no idea why we might error out, but catch it if we do
                 EventLog::log($e->getMessage, 'err');
                 echo '<p class="error">' . _t('There was an error importing user %s. See the EventLog for the error message. ', array($wp_user->user_login)) . '</p>';
                 echo '<p>' . _t('Rolling back changes&hellip;') . '</p>';
                 // rollback all changes before we return so the import hasn't changed anything yet
                 DB::rollback();
                 // and return so they don't get AJAX to send them on to the next step
                 return false;
             }
         }
     }
     // if we've finished without an error, commit the import
     DB::commit();
     // get the next ajax url
     $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_posts'));
     // and spit out ajax to send them to the next step - posts!
     echo $this->get_ajax($ajax_url, $inputs);
 }
Example #6
0
 public function activate_plugins()
 {
     // extract checked plugin IDs from $_POST
     $plugin_ids = array();
     foreach ($this->handler_vars as $id => $activate) {
         if (preg_match('/plugin_([a-f0-9]{8})/u', $id, $matches) && $activate) {
             $plugin_ids[] = $matches[1];
         } elseif (preg_match('/plugin_(.+)/u', $id, $matches) && $activate) {
             $plugin_ids[] = $matches[1];
         }
     }
     if (count($plugin_ids) == 0) {
         return;
     }
     // set the user_id in the session in case plugin activation methods need it
     if (!($u = User::get_by_name($this->handler_vars['admin_username']))) {
         // @todo die gracefully
         die(_t('No admin user found'));
     }
     $u->remember();
     // loop through all plugins to find matching plugin files
     $plugin_files = Plugins::list_all();
     foreach ($plugin_files as $file) {
         if (in_array(basename($file), $plugin_ids)) {
             Plugins::activate_plugin($file);
             continue;
         }
         $id = Plugins::id_from_file($file);
         if (in_array($id, $plugin_ids)) {
             Plugins::activate_plugin($file);
         }
     }
     // unset the user_id session variable
     Session::clear_userid($_SESSION['user_id']);
     unset($_SESSION['user_id']);
 }
 private function get_user($username)
 {
     // should we create users if they don't exist? default to false
     $create = Options::get('passwdlogins__create', false);
     $group = Options::get('passwdlogins__group');
     $user = User::get_by_name($username);
     // if the user doesn't exist
     if ($user == false) {
         // do we want to create it?
         if ($create == true) {
             $user = User::create(array('username' => $username, 'password' => Utils::random_password(200)));
             // add them to the default group, if one has been selected - otherwise it'll just default to authenticated anyway
             if ($group) {
                 $user->add_to_group($group);
             }
             return $user;
         } else {
             // don't create it, it doesn't exist, so fail
             return false;
         }
     } else {
         // the user already exists, return it
         return $user;
     }
 }
Example #8
0
 /**
  * A validation function that returns an error if the the passed username is unavailable
  *
  * @param string $text A value to test as username
  * @param FormControl $control The control that defines the value
  * @param FormContainer $form The container that holds the control
  * @param string $allowed_name An optional name which overrides the check and is always allowed
  * @param string $warning An optional error message
  * @return array An empty array if the value exists, or an array with strings describing the errors
  */
 public static function validate_username($value, $control, $form, $allowed_name = null, $warning = null)
 {
     if (isset($allowed_name) && $value == $allowed_name) {
         return array();
     }
     if (User::get_by_name($value)) {
         $warning = empty($warning) ? _t('That username supplied for %s is already in use.', array($control->get_label())) : $warning;
         return array($warning);
     }
     return array();
 }
Example #9
0
 private function get_user()
 {
     $user = User::get_by_name('lipsum');
     // if it doesn't exist, create it instead
     if ($user == false) {
         $user = User::create(array('username' => 'lipsum', 'email' => '*****@*****.**', 'password' => Utils::random_password(16)));
     }
     // return the user, however we got it
     return $user;
 }
    /**
     * The plugin sink for the auth_ajax_drupal_import_posts hook.
     * Responds via authenticated ajax to requests for post importing.
     *
     * @param mixed $handler
     * @return
     */
    public function action_auth_ajax_drupal_import_users($handler)
    {
        $valid_fields = array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'import_comments', 'userindex', 'entry_type', 'page_type', 'tag_vocab');
        $inputs = array_intersect_key($_POST->getArrayCopy(), array_flip($valid_fields));
        extract($inputs);
        $drupaldb = $this->drupal_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix);
        if ($drupaldb) {
            $drupal_users = $drupaldb->get_results("\n\t\t\t\tSELECT\n\t\t\t\t\tuid,\n\t\t\t\t\tname as username,\n\t\t\t\t\tpass as password,\n\t\t\t\t\tmail as email\n\t\t\t\tFROM {$db_prefix}users\n\t\t\t\tWHERE uid > 0\n\t\t\t", array(), 'User');
            $usercount = 0;
            _e('<p>Importing users...</p>');
            foreach ($drupal_users as $user) {
                $habari_user = User::get_by_name($user->username);
                // If username exists
                if ($habari_user instanceof User) {
                    $habari_user->info->drupal_uid = $user->uid;
                    $habari_user->update();
                } else {
                    try {
                        $user->info->drupal_uid = $user->uid;
                        // This should probably remain commented until we implement ACL more,
                        // or any imported user will be able to log in and edit stuff
                        //$user->password = '******' . $user->password;
                        $user->exclude_fields(array('uid', 'drupal_uid'));
                        $user->insert();
                        $usercount++;
                    } catch (Exception $e) {
                        EventLog::log($e->getMessage(), 'err', null, null, print_r(array($user, $e), 1));
                        $errors = Options::get('import_errors');
                        $errors[] = $user->username . ' : ' . $e->getMessage();
                        Options::set('import_errors', $errors);
                    }
                }
            }
            $ajax_url = URL::get('auth_ajax', array('context' => 'drupal_import_posts'));
            echo <<<DRUPAL_IMPORT_USERS1
\t\t\t<script type="text/javascript">
\t\t\t// A lot of ajax stuff goes here.
\t\t\t\$( document ).ready( function(){
\t\t\t\t\$( '#import_progress' ).load(
\t\t\t\t\t"{$ajax_url}",
\t\t\t\t\t{
\t\t\t\t\t\tdb_host: "{$db_host}",
\t\t\t\t\t\tdb_name: "{$db_name}",
\t\t\t\t\t\tdb_user: "******",
\t\t\t\t\t\tdb_pass: "******",
\t\t\t\t\t\tdb_prefix: "{$db_prefix}",
\t\t\t\t\t\timport_comments: "{$import_comments}",
\t\t\t\t\t\tentry_type: "{$entry_type}",
\t\t\t\t\t\tpage_type: "{$page_type}",
\t\t\t\t\t\ttag_vocab: "{$tag_vocab}",
\t\t\t\t\t\tpostindex: 0
\t\t\t\t\t}
\t\t\t\t );
\t\t\t} );
\t\t\t</script>
DRUPAL_IMPORT_USERS1;
        } else {
            EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit');
            echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
        }
    }
    /**
     * The plugin sink for the auth_ajax_chyrp_import_posts hook.
     * Responds via authenticated ajax to requests for post importing.
     *
     * @param mixed $handler
     * @return
     */
    public function action_auth_ajax_chyrp_import_users($handler)
    {
        $inputs = $_POST->filter_keys('db_type', 'db_name', 'db_host', 'db_user', 'db_pass', 'userindex');
        foreach ($inputs as $key => $value) {
            ${$key} = $value;
        }
        $connect_string = $this->get_connect_string($db_type, $db_host, $db_name);
        $db = $this->chryp_connect($connect_string, $db_user, $db_pass);
        if ($db) {
            DB::begin_transaction();
            $new_users = $db->get_results("\r\n\t\t\t\t\tSELECT\r\n\t\t\t\t\t\tlogin as username,\r\n\t\t\t\t\t\tpassword,\r\n\t\t\t\t\t\temail,\r\n\t\t\t\t\t\tusers.id as old_id\r\n\t\t\t\t\tFROM users\r\n\t\t\t\t\tINNER JOIN posts ON posts.user_id = users.id\r\n\t\t\t\t\tGROUP BY users.id\r\n\t\t\t\t", array(), 'User');
            $usercount = 0;
            _e('<p>Importing users...</p>');
            foreach ($new_users as $user) {
                $habari_user = User::get_by_name($user->username);
                // If username exists
                if ($habari_user instanceof User) {
                    $habari_user->info->old_id = $user->old_id;
                    $habari_user->update();
                } else {
                    try {
                        $user->info->old_id = $user->old_id;
                        // This should probably remain commented until we implement ACL more,
                        // or any imported user will be able to log in and edit stuff
                        //$user->password = '******' . $user->password;
                        $user->exclude_fields(array('old_id'));
                        $user->insert();
                        $usercount++;
                    } catch (Exception $e) {
                        EventLog::log($e->getMessage(), 'err', null, null, print_r(array($user, $e), 1));
                        Session::error($e->getMessage());
                        $errors = Options::get('import_errors');
                        $errors[] = $user->username . ' : ' . $e->getMessage();
                        Options::set('import_errors', $errors);
                    }
                }
            }
            if (DB::in_transaction()) {
                DB::commit();
            }
            $ajax_url = URL::get('auth_ajax', array('context' => 'chyrp_import_posts'));
            $vars = Utils::addslashes(array('type' => $db_type, 'host' => $db_host, 'name' => $db_name, 'user' => $db_user, 'pass' => $db_pass));
            echo <<<IMPORT_POSTS
\t\t\t<script type="text/javascript">
\t\t\t// A lot of ajax stuff goes here.
\t\t\t\$( document ).ready( function(){
\t\t\t\t\$( '#import_progress' ).load(
\t\t\t\t\t"{$ajax_url}",
\t\t\t\t\t{
\t\t\t\t\t\tdb_type: "{$db_type}",
\t\t\t\t\t\tdb_host: "{$vars['host']}",
\t\t\t\t\t\tdb_name: "{$vars['name']}",
\t\t\t\t\t\tdb_user: "******",
\t\t\t\t\t\tdb_pass: "******",
\t\t\t\t\t\tpostindex: 0
\t\t\t\t\t}
\t\t\t\t );
\t\t\t} );
\t\t\t</script>
IMPORT_POSTS;
        } else {
            EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit');
            Session::error($e->getMessage());
            echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
        }
    }
Example #12
0
	public function activate_plugins()
	{
		// extract checked plugin IDs from $_POST
		$plugin_ids = array();
		foreach ( $_POST as $id => $activate ) {
			if ( preg_match( '/plugin_\w+/u', $id ) && $activate ) {
				$id = substr( $id, 7 );
				$plugin_ids[] = $id;
			}
		}

		// set the user_id in the session in case plugin activation methods need it
		if ( ! $u = User::get_by_name( $this->handler_vars['admin_username'] ) ) {
			// @todo die gracefully
			die( _t( 'No admin user found' ) );
		}
		$u->remember();

		// loop through all plugins to find matching plugin files
		$plugin_files = Plugins::list_all();
		foreach ( $plugin_files as $file ) {
			$id = Plugins::id_from_file( $file );
			if ( in_array( $id, $plugin_ids ) ) {
				Plugins::activate_plugin( $file );
			}
		}

		// unset the user_id session variable
		Session::clear_userid( $_SESSION['user_id'] );
		unset( $_SESSION['user_id'] );
	}
 public function action_plugin_activation($file)
 {
     if (!User::get_by_name('github_hook')) {
         User::create(array('username' => 'github_hook', 'email' => '*****@*****.**', 'password' => sha1(rand(0, pow(2, 32)))));
     }
 }
 /**
  * The plugin sink for the auth_ajax_hab_import_users hook.
  * Responds via authenticated ajax to requests for user importing.
  *
  * @param mixed $handler
  * @return
  */
 public function action_auth_ajax_hab_import_users($handler)
 {
     $inputs = $_POST->filter_keys('db_type', 'db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'userindex', 'tag_import');
     $inputs = $inputs->getArrayCopy($inputs);
     $inputs = array_merge($this->default_values, $inputs);
     $connect_string = $this->get_connect_string($inputs['db_type'], $inputs['db_host'], $inputs['db_name']);
     $db = $this->hab_connect($connect_string, $inputs['db_user'], $inputs['db_pass']);
     if (!$db) {
         EventLog::log(sprintf(_t('Failed to import from "%s"'), $inputs['db_name']), 'crit');
         Session::error($e->getMessage());
         echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
     }
     DB::begin_transaction();
     $new_users = $db->get_results("SELECT username, password, email, {$inputs['db_prefix']}users.id as old_id\r\n\t\t\t\tFROM {$inputs['db_prefix']}users\r\n\t\t\t\tINNER JOIN {$inputs['db_prefix']}posts ON {$inputs['db_prefix']}posts.user_id = {$inputs['db_prefix']}users.id\r\n\t\t\t\tGROUP BY {$inputs['db_prefix']}users.id", array(), 'User');
     $usercount = 0;
     _e('<p>Importing users...</p>');
     foreach ($new_users as $user) {
         $habari_user = User::get_by_name($user->username);
         // If username exists
         if ($habari_user instanceof User) {
             $habari_user->info->old_id = $user->old_id;
             $habari_user->update();
         } else {
             // Add a new user
             try {
                 $user->info->old_id = $user->old_id;
                 // This should probably remain commented until we implement ACL more,
                 // or any imported user will be able to log in and edit stuff
                 //$user->password = '******' . $user->password;
                 $user->exclude_fields(array('old_id'));
                 $user->insert();
                 $usercount++;
             } catch (Exception $e) {
                 EventLog::log($e->getMessage(), 'err', null, null, print_r(array($user, $e), 1));
                 Session::error($e->getMessage());
                 $errors = Options::get('import_errors');
                 $errors[] = $user->username . ' : ' . $e->getMessage();
                 Options::set('import_errors', $errors);
             }
         }
     }
     if (DB::in_transaction()) {
         DB::commit();
     }
     $ajax_url = URL::get('auth_ajax', array('context' => 'hab_import_posts'));
     $inputs['import_index'] = 0;
     echo $this->get_ajax($ajax_url, $inputs);
 }
Example #15
0
	/**
	 * A validation function that returns an error if the the passed username is unavailable
	 *
	 * @param string $text A value to test as username
	 * @param FormControl $control The control that defines the value
	 * @param FormContainer $form The container that holds the control
	 * @param string $allowed_name An optional name which overrides the check and is always allowed
	 * @param string $warning An optional error message
	 * @return array An empty array if the value exists, or an array with strings describing the errors
	 */
	public static function validate_username( $value, $control, $form, $allowed_name = null, $warning = null )
	{
		if ( isset( $allowed_name ) && ( $value == $allowed_name ) ) {
			return array();
		}
		if ( User::get_by_name( $value ) ) {
			$warning = empty( $warning ) ? _t( 'That username is already in use!' ) : $warning;
			return array( $warning );
		}
		return array();
	}
Example #16
0
 /**
  * Teardown for the whole test suite
  */
 protected function module_teardown()
 {
     $user = User::get_by_name('posts_test');
     $user->delete();
 }
Example #17
0
 /**
  * Handles POST requests from the Users listing (ie: creating a new user)
  */
 public function post_users()
 {
     $this->fetch_users();
     $extract = $this->handler_vars->filter_keys('newuser', 'delete', 'new_pass1', 'new_pass2', 'new_email', 'new_username');
     foreach ($extract as $key => $value) {
         ${$key} = $value;
     }
     if (isset($newuser)) {
         $action = 'newuser';
     } elseif (isset($delete)) {
         $action = 'delete';
     }
     $error = '';
     if (isset($action) && 'newuser' == $action) {
         if (!isset($new_pass1) || !isset($new_pass2) || empty($new_pass1) || empty($new_pass2)) {
             Session::error(_t('Password is required.'), 'adduser');
         } else {
             if ($new_pass1 !== $new_pass2) {
                 Session::error(_t('Password mis-match.'), 'adduser');
             }
         }
         if (!isset($new_email) || empty($new_email) || !strstr($new_email, '@')) {
             Session::error(_t('Please supply a valid email address.'), 'adduser');
         }
         if (!isset($new_username) || empty($new_username)) {
             Session::error(_t('Please supply a user name.'), 'adduser');
         }
         // safety check to make sure no such username exists
         $user = User::get_by_name($new_username);
         if (isset($user->id)) {
             Session::error(_t('That username is already assigned.'), 'adduser');
         }
         if (!Session::has_errors('adduser')) {
             $user = new User(array('username' => $new_username, 'email' => $new_email, 'password' => Utils::crypt($new_pass1)));
             if ($user->insert()) {
                 Session::notice(sprintf(_t("Added user '%s'"), $new_username));
             } else {
                 $dberror = DB::get_last_error();
                 Session::error($dberror[2], 'adduser');
             }
         } else {
             $settings = array();
             if (isset($username)) {
                 $settings['new_username'] = $new_username;
             }
             if (isset($new_email)) {
                 $settings['new_email'] = $new_email;
             }
             $this->theme->assign('settings', $settings);
         }
     } else {
         if (isset($action) && 'delete' == $action) {
             $this->update_users($this->handler_vars);
         }
     }
     $this->theme->display('users');
 }
    /**
     * The plugin sink for the auth_ajax_mt_import_users hook.
     * Responds via authenticated ajax to requests for post importing.
     *
     * @access public
     * @param mixed $handler
     * @return
     */
    public function action_auth_ajax_mt_mysql_import_users($handler)
    {
        $valid_fields = array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'userindex', 'blog_id');
        $inputs = Controller::get_handler_vars()->filter_keys($valid_fields)->getArrayCopy();
        extract($inputs);
        $mtdb = $this->mt_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix);
        if (!$mtdb) {
            EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit');
            echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
            return;
        }
        $mt_users = $mtdb->get_results("SELECT author_id AS mt_id, author_name AS username, author_email AS email FROM {$db_prefix}author;", array(), 'User');
        $usercount = 0;
        echo _t('<p>Importing users...</p>');
        @reset($mt_users);
        while (list(, $user) = @each($mt_users)) {
            $habari_user = User::get_by_name($user->username);
            // If username exists
            if (!$habari_user instanceof User) {
                try {
                    $user->info->mt_id = $user->mt_id;
                    // This should probably remain commented until we implement ACL more,
                    // or any imported user will be able to log in and edit stuff
                    //$user->password = '******' . $user->password;
                    $user->exclude_fields(array('mt_id'));
                    $user->insert();
                    $usercount++;
                } catch (Exception $e) {
                    EventLog::log($e->getMessage(), 'err', null, null, print_r(array($user, $e), 1));
                    $errors = Options::get('import_errors');
                    $errors[] = $user->username . ' : ' . $e->getMessage();
                    Options::set('import_errors', $errors);
                }
            }
        }
        $ajax_url = URL::get('auth_ajax', array('context' => 'mt_mysql_import_posts'));
        ?>
<script type="text/javascript">
// A lot of ajax stuff goes here.
$( document ).ready( function(){
	$( '#import_progress' ).load(
		"<?php 
        echo $ajax_url;
        ?>
",
		{
		db_host: "<?php 
        echo htmlspecialchars($db_host);
        ?>
",
		db_name: "<?php 
        echo htmlspecialchars($db_name);
        ?>
",
		db_user: "******",
		db_pass: "******",
		db_prefix: "<?php 
        echo htmlspecialchars($db_prefix);
        ?>
",
		blog_id: "<?php 
        echo htmlspecialchars($blog_id);
        ?>
",
		postindex: 0
		}
	 );
});
</script>
<?php 
    }
Example #19
0
    /**
     * The plugin sink for the auth_ajax_wp_import_posts hook.
     * Responds via authenticated ajax to requests for post importing.
     *
     * @param mixed $handler
     * @return
     */
    public function action_auth_ajax_wp_import_users($handler)
    {
        $valid_fields = array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'userindex', 'category_import', 'utw_import');
        $inputs = array_intersect_key($_POST->getArrayCopy(), array_flip($valid_fields));
        extract($inputs);
        $wpdb = $this->wp_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix);
        if ($wpdb) {
            if (!DB::in_transaction()) {
                DB::begin_transaction();
            }
            $wp_users = $wpdb->get_results("\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tuser_login as username,\n\t\t\t\t\t\tuser_pass as password,\n\t\t\t\t\t\tuser_email as email,\n\t\t\t\t\t\tuser_url as wp_url,\n\t\t\t\t\t\t{$db_prefix}users.id as wp_id\n\t\t\t\t\tFROM {$db_prefix}users\n\t\t\t\t\tINNER JOIN {$db_prefix}posts ON {$db_prefix}posts.post_author = {$db_prefix}users.id\n\t\t\t\t\tGROUP BY {$db_prefix}users.id\n\t\t\t\t", array(), 'User');
            $usercount = 0;
            _e('<p>Importing users...</p>');
            foreach ($wp_users as $user) {
                $habari_user = User::get_by_name($user->username);
                // If username exists
                if ($habari_user instanceof User) {
                    $habari_user->info->wp_id = $user->wp_id;
                    $habari_user->info->url = $user->wp_url;
                    $habari_user->update();
                } else {
                    try {
                        $user->info->wp_id = $user->wp_id;
                        if ($user->wp_url != '') {
                            $user->info->url = $user->wp_url;
                        }
                        // This should probably remain commented until we implement ACL more,
                        // or any imported user will be able to log in and edit stuff
                        //$user->password = '******' . $user->password;
                        $user->exclude_fields(array('wp_id', 'wp_url'));
                        $user->insert();
                        $usercount++;
                    } catch (Exception $e) {
                        EventLog::log($e->getMessage(), 'err', null, null, print_r(array($user, $e), 1));
                        Session::error($e->getMessage());
                        $errors = Options::get('import_errors');
                        $errors[] = $user->username . ' : ' . $e->getMessage();
                        Options::set('import_errors', $errors);
                    }
                }
            }
            if (DB::in_transaction()) {
                DB::commit();
            }
            $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_posts'));
            $vars = Utils::addslashes(array('host' => $db_host, 'name' => $db_name, 'user' => $db_user, 'pass' => $db_pass, 'prefix' => $db_prefix));
            echo <<<WP_IMPORT_USERS1
\t\t\t<script type="text/javascript">
\t\t\t// A lot of ajax stuff goes here.
\t\t\t\$( document ).ready( function(){
\t\t\t\t\$( '#import_progress' ).load(
\t\t\t\t\t"{$ajax_url}",
\t\t\t\t\t{
\t\t\t\t\t\tdb_host: "{$vars['host']}",
\t\t\t\t\t\tdb_name: "{$vars['name']}",
\t\t\t\t\t\tdb_user: "******",
\t\t\t\t\t\tdb_pass: "******",
\t\t\t\t\t\tdb_prefix: "{$vars['prefix']}",
\t\t\t\t\t\tcategory_import: "{$category_import}",
\t\t\t\t\t\tutw_import: "{$utw_import}",
\t\t\t\t\t\tpostindex: 0
\t\t\t\t\t}
\t\t\t\t );
\t\t\t} );
\t\t\t</script>
WP_IMPORT_USERS1;
        } else {
            EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit');
            Session::error($e->getMessage());
            echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
        }
    }
Example #20
0
 /**
  * Handles GET requests of a user page.
  */
 public function get_user()
 {
     $permission = false;
     // Check if the user is editing their own profile
     if ($this->handler_vars['user'] == '') {
         $edit_user = User::identify();
         $self = true;
     } else {
         $edit_user = User::get_by_name($this->handler_vars['user']);
         $self = $edit_user->id == User::identify()->id;
     }
     // Redirect to the users management page if we're trying to edit a non-existent user
     if (!$edit_user) {
         Session::error(_t('No such user!'));
         Utils::redirect(URL::get('display_users'));
     }
     // Check permissions to see this page
     if ($self && (User::identify()->can('manage_self') || User::identify()->can('manage_users'))) {
         $permission = true;
     } elseif (User::identify()->can('manage_users')) {
         $permission = true;
     }
     // No permission? Show blank page.
     if (!$permission) {
         Session::error(_t('Access to that page has been denied by the administrator.'));
         $this->get_blank();
         return;
     }
     $this->theme->edit_user = $edit_user;
     $form = FormUI::build('edit_user', 'edit_user', array('edit_user' => $edit_user));
     $this->theme->form = $form->get();
     $this->theme->admin_page = $self ? _t('My Profile') : _t('User Profile for %s', array(Utils::htmlspecialchars($edit_user->username)));
     $this->theme->display('user');
 }