summaryrefslogtreecommitdiffstats
path: root/lib/migrate.php
blob: f3dd3080d300899817613182a9a60a309952b6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
 * ownCloud
 *
 * @author Tom Needham
 * @copyright 2012 Tom Needham tom@owncloud.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


/**
 * provides an interface to all search providers
 */
class OC_Migrate{
	static private $providers=array();
	
	/**
	 * register a new migration provider
	 * @param OC_Migrate_Provider $provider
	 */
	public static function registerProvider($provider){
		self::$providers[]=$provider;
	}
	
	/**
	 * export app data for a user
	 * @param string userid
	 * @return string xml of app data
	 */
	public static function export($uid){
		
		// Only export database users, otherwise we get chaos
		if(OC_User_Database::userExists($uid)){
				
			$data = array();
			$data['userid'] = OC_User::getUser();
			
			$query = OC_DB::prepare( "SELECT uid, password FROM *PREFIX*users WHERE uid LIKE ?" );
			$result = $query->execute( array( $uid));
	
			$row = $result->fetchRow();
			if($row){
				$data['hash'] = $row['password'];
			} else {
				return false;
				exit();	
			}
			
			foreach(self::$providers as $provider){
				
				$data['apps'][$prodider->appid]['info'] = OC_App::getAppInfo($provider->appid);
				$data['apps'][$provider->appid]['data'] = $provider->export($uid);
	
			}
	
			return self::indent(json_encode($data));
		
		} else {
			return false;	
		}
		
	}
	
	/**
	 * @breif imports a new user
	 * @param $data json data for the user
	 * @param $uid optional uid to use
	 * @return json reply
	 */
	 public function import($data,$uid=null){
	 	
	 	// Import the data
	 	$data = json_decode($data);
	 	if(is_null($data)){
	 		// TODO LOG
	 		return false;
	 		exit();	
	 	}
	 	
	 	// Specified user or use original
	 	$uid = !is_null($uid) ? $uid : $data['userid'];
	 	
	 	// Check if userid exists
	 	if(OC_User::userExists($uid)){
	 		// TODO LOG
	 		return false;
	 		exit();	
	 	}
	 	
	 	// Create the user
	 	$query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
		$result = $query->execute( array( $uid, $data['hash']));
		if(!$result){
			// TODO LOG
			return false;
			exit();	
		}
	 	
	 	foreach($data['app'] as $app){
	 		// Check if supports migration and is enabled
	 		if(in_array($app, self::$providers)){
	 			if(OC_App::isEnabled($app)){
	 				$provider->import($data['app'][$app],$uid);	
	 			}
	 		}
	 			
	 	}
	 		
	 }
	 
	 private static function indent($json){

		$result      = '';
		$pos         = 0;
		$strLen      = strlen($json);
		$indentStr   = '  ';
		$newLine     = "\n";
		$prevChar    = '';
		$outOfQuotes = true;
		
		for ($i=0; $i<=$strLen; $i++) {
		
		    // Grab the next character in the string.
		    $char = substr($json, $i, 1);
		
		    // Are we inside a quoted string?
		    if ($char == '"' && $prevChar != '\\') {
		        $outOfQuotes = !$outOfQuotes;
		    
		    // If this character is the end of an element, 
		    // output a new line and indent the next line.
		    } else if(($char == '}' || $char == ']') && $outOfQuotes) {
		        $result .= $newLine;
		        $pos --;
		        for ($j=0; $j<$pos; $j++) {
		            $result .= $indentStr;
		        }
		    }
		    
		    // Add the character to the result string.
		    $result .= $char;
		
		    // If the last character was the beginning of an element, 
		    // output a new line and indent the next line.
		    if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
		        $result .= $newLine;
		        if ($char == '{' || $char == '[') {
		            $pos ++;
		        }
		        
		        for ($j = 0; $j < $pos; $j++) {
		            $result .= $indentStr;
		        }
		    }
		    
		    $prevChar = $char;
		}
		
		return $result;	
	 }
	
}