summaryrefslogtreecommitdiffstats
path: root/debian/missing-sources/swfupload/Flash/ExternalCall.as
blob: 091f5ecf26b7f7140f1671d3fde010722c674498 (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
package {
	import flash.external.ExternalInterface;

	internal class ExternalCall
	{
		
		/*public function ExternalCall()
		{

		}
		*/
		
		public static function Simple(callback:String):void {
			ExternalInterface.call(callback);
		}
		public static function FileQueued(callback:String, file_object:Object):void {
			ExternalInterface.call(callback, EscapeMessage(file_object));
		}
		public static function FileQueueError(callback:String, error_code:Number, file_object:Object, message:String):void {
			
			ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(error_code), EscapeMessage(message));
			
		}
		public static function FileDialogComplete(callback:String, num_files_selected:Number, num_files_queued:Number, total_num_files_queued:Number):void {
			
			ExternalInterface.call(callback, EscapeMessage(num_files_selected), EscapeMessage(num_files_queued), EscapeMessage(total_num_files_queued));
			
		}
		
		public static function UploadStart(callback:String, file_object:Object):void  {
			ExternalInterface.call(callback, EscapeMessage(file_object));
		}
		
		public static function UploadProgress(callback:String, file_object:Object, bytes_loaded:uint, bytes_total:uint):void {
			
			ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(bytes_loaded), EscapeMessage(bytes_total));
			
		}
		public static function UploadSuccess(callback:String, file_object:Object, server_data:String, responseReceived:Boolean):void {
			
			ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(server_data), EscapeMessage(responseReceived));
			
		}
		public static function UploadError(callback:String, error_code:Number, file_object:Object, message:String):void {
			
			ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(error_code), EscapeMessage(message));
			
		}
		public static function UploadComplete(callback:String, file_object:Object):void {
			
			ExternalInterface.call(callback, EscapeMessage(file_object));
			
		}
		public static function Debug(callback:String, message:String):void {
			
			ExternalInterface.call(callback, EscapeMessage(message));
		}
		
		public static function Bool(callback:String):Boolean {
			return ExternalInterface.call(callback);
		}
		
		
		/* Escapes all the backslashes which are not translated correctly in the Flash -> JavaScript Interface
		 * 
		 * These functions had to be developed because the ExternalInterface has a bug that simply places the
		 * value a string in quotes (except for a " which is escaped) in a JavaScript string literal which
		 * is executed by the browser.  These often results in improperly escaped string literals if your
		 * input string has any backslash characters. For example the string:
		 * 		"c:\Program Files\uploadtools\"
		 * is placed in a string literal (with quotes escaped) and becomes:
		 * 		var __flash__temp = "\"c:\Program Files\uploadtools\\"";
		 * This statement will cause errors when executed by the JavaScript interpreter:
		 * 	1) The first \" is succesfully transformed to a "
		 *  2) \P is translated to P and the \ is lost
		 *  3) \u is interpreted as a unicode character and causes an error in IE
		 *  4) \\ is translated to \
		 *  5) leaving an unescaped " which causes an error
		 * 
		 * I fixed this by escaping \ characters in all outgoing strings.  The above escaped string becomes:
		 * 		var __flash__temp = "\"c:\\Program Files\\uploadtools\\\"";
		 * which contains the correct string literal.
		 * 
		 * Note: The "var __flash__temp = " portion of the example is part of the ExternalInterface not part of
		 * my escaping routine.
		 */
		private static function EscapeMessage(message:*):* {
			if (message is String) {
				message = EscapeString(message);
			}
			else if (message is Array) {
				message = EscapeArray(message);
			}
			else if (message is Object) {
				message = EscapeObject(message);
			}
			
			return message;
		}
		
		private static function EscapeString(message:String):String {
			var replacePattern:RegExp = /\\/g; //new RegExp("/\\/", "g");
			return message.replace(replacePattern, "\\\\");
		}
		private static function EscapeArray(message_array:Array):Array {
			var length:uint = message_array.length;
			var i:uint = 0;
			for (i; i < length; i++) {
				message_array[i] = EscapeMessage(message_array[i]);
			}
			return message_array;
		}
		private static function EscapeObject(message_obj:Object):Object {
			for (var name:String in message_obj) {
				message_obj[name] = EscapeMessage(message_obj[name]);
			}
			return message_obj;
		}

	}
}