1 /**
  2  * Socketbug - Web Socket Remote Debugging
  3  * 
  4  * Copyright (c) 2011 Manifest Interactive, LLC
  5  *
  6  * Licensed under the LGPL v3 licenses.
  7  *
  8  * @version v0.2.1 ( 7/4/2011 )
  9  *
 10  * @author <a href="http://www.socketbug.com">Website</a>
 11  * @author <a href="http://www.vimeo.com/user7532036/videos">Video Tutorials ( HD )</a>
 12  * @author <a href="http://www.twitter.com/socketbug_dev">Twitter</a>
 13  * @author <a href="http://github.com/manifestinteractive/socketbug">Source Code</a>
 14  * @author <a href="http://socketbug.userecho.com">Support & Feature Requests</a>
 15  */
 16 
 17 if(typeof(socketbug) === 'undefined')
 18 {	
 19 	/**
 20 	 * @private
 21 	 */
 22 	var _encryption_salt = 'Ch4ng3^M3';
 23 	
 24 	/**
 25 	 * @namespace Socketbug Console 
 26 	 */
 27 	var socketbug = {
 28 
 29 		/** 
 30 		 * Check if we're connected to Socketbug 
 31 		 * 
 32 		 * @param {Boolean} connected
 33 		 */
 34 		connected: false,
 35 		
 36 		/**
 37 		 * Store Socketbug Session ID 
 38 		 * 
 39 		 * @param {String} session_id
 40 		 */
 41 		session_id: null,
 42 		
 43 		/** 
 44 		 * Define Group Data 
 45 		 * 
 46 		 * @param {Object} group
 47 		 */
 48 		group:
 49 		{
 50 			'id': hex_md5(_encryption_salt + _sbs.group_id),
 51 			'name': _sbs.group_name
 52 		}, 
 53 		
 54 		/** 
 55 		 * Define Application Data 
 56 		 * 
 57 		 * @param {Object} application
 58 		 */
 59 		application:
 60 		{
 61 			'id': hex_md5(_encryption_salt + _sbs.application_id),
 62 			'name': _sbs.application_name
 63 		}, 
 64 		
 65 		/** 
 66 		 * Define Client Data 
 67 		 * 
 68 		 * @param {Object} client
 69 		 */
 70 		client:
 71 		{
 72 			'id': hex_md5(_encryption_salt + GUID.create()),
 73 			'name': ''
 74 		}, 
 75 		
 76 		/** 
 77 		 * Debug Level
 78 		 * 
 79 		 * @example 5 = log, debug, info, warn, & error
 80 		 * @example 4 = debug, info, warn, & error
 81 		 * @example 3 = info, warn, & error
 82 		 * @example 2 = warn, & error
 83 		 * @example 1 = error
 84 		 * @example 0 = disable all debug messages
 85 		 * 
 86 		 * @param {Number} debug_level This is set in the HTML Configuration
 87 		 */
 88 		debug_level: _sbs.debug_level,
 89 		
 90 		/** Socketbug Server Comminication */
 91 		sb_manager: io.connect(_sbs.host + ':' + _sbs.port + '/sb_manager'),
 92 		
 93 		/** Socketbug Appliction Comminication */
 94 		sb_application: io.connect(_sbs.host + ':' + _sbs.port + '/sb_application'),
 95 		
 96 		/** Socketbug Console Comminication */
 97 		sb_console: io.connect(_sbs.host + ':' + _sbs.port + '/sb_console'),
 98 		
 99 		/** 
100 		 * Setup Ouput Log for Console 
101 		 * 
102 		 * @function
103 		 * @param {String} message This is the message to Log
104 		 * @param {String} level The is the Debug Level
105 		 */
106 		log: function(message, level)
107 		{
108 			/**
109 			 * Using CONSOLE here and NOT DEBUG to prevent looping back 
110 			 * to Socketbug since DEBUG has a callback handler 
111 			 * to return data to Socketbug 
112 			 */
113 			switch(level)
114 			{
115 				case 'log':
116 					if(_sbs.debug_level == 5)
117 					{
118 						console.log(message);
119 					}
120 					break;
121 					
122 				case 'debug':
123 					if(_sbs.debug_level >= 4)
124 					{
125 						console.debug(message);
126 					}
127 					break;
128 					
129 				case 'info':
130 					if(_sbs.debug_level >= 3)
131 					{
132 						console.info(message);
133 					}
134 					break;
135 				
136 				case 'warn':
137 					if(_sbs.debug_level >= 2)
138 					{
139 						console.warn(message);
140 					}
141 					break;
142 					
143 				case 'error':
144 					if(_sbs.debug_level >= 1)
145 					{
146 						console.error(message);
147 					}
148 					break;
149 			}			
150 		},
151 		
152 		/** 
153 		 * Capture All Debug Events and send them through Socketbug
154 		 * 
155 		 * @function
156 		 * @param {String} level The is the Debug Level
157 		 */
158 		debug: function(level)
159 		{
160 			var args = Array.prototype.slice.call(arguments, 1);
161 		
162 			socketbug.sb_console.emit('debug', level, args, 
163 				function()
164 				{
165 					socketbug.log('Sent Debug info to Remote Console.', 'log');
166 				}
167 			);			
168 		}
169 	};
170 	
171 	/* ================================================== */
172 	/*            Socketbug Socket.IO Manager             */
173 	/* ================================================== */
174 	
175 	/** Capture Connect Event */
176 	socketbug.sb_manager.on('connect', function()
177 	{		
178 		if(socketbug.connected === false)
179 		{				
180 			if( !GUID.is_valid(_sbs.group_id))
181 			{
182 				socketbug.log('Invalid Socketbug Group ID', 'error');
183 			}
184 			else if( !GUID.is_valid(_sbs.application_id))
185 			{
186 				socketbug.log('Invalid Socketbug Application ID', 'error');
187 			}
188 			else
189 			{
190 				var date = new Date();
191 			
192 				socketbug.log('Socketbug Connected');
193 				socketbug.connected = true;
194 		
195 				socketbug.sb_manager.emit('connection_manager', socketbug.group, socketbug.application, socketbug.client, 
196 					function(session_id)
197 					{ 
198 						socketbug.log('Connected to Socketbug with Session ID: '+session_id);
199 						
200 						socketbug.session_id = session_id;
201 						
202 						/* Do Callback if one set */
203 						if(typeof(_sbs.connect_callback) == 'function')
204 						{
205 							_sbs.connect_callback(session_id);
206 						}  
207 					}
208 				);
209 			}
210 		}
211 	});
212 	
213 	/** Capture Failed Authentication Event */
214 	socketbug.sb_manager.on('authentication_failed', function (group_valid, application_valid)
215 	{		
216 		if( !group_valid)
217 		{
218 			alert('You are Not Authorized to use Socketbug. Your Group ID is Invalid.');
219 		}
220 		if( !application_valid)
221 		{
222 			alert('You are Not Authorized to use Socketbug. Your Application ID is Invalid.');
223 		}
224 		
225 		/** Disconnect Client from Socketbug Services */
226 		socketbug.sb_manager.disconnect();
227 		socketbug.sb_application.disconnect();
228 		socketbug.sb_console.disconnect();
229 	});
230 	
231 	/** Capture Failed Authentication Event */
232 	socketbug.sb_application.on('execute_js', function (javascript)
233 	{		
234 		try
235 		{
236 			/** I know... this is an eval() ... I am pure EVIL() */
237 			eval(javascript);
238 		}
239 		catch(error)
240 		{
241 			socketbug.log(error, 'error');
242 		}
243 	});
244 	
245 	/** Capture Failed Authentication Event */
246 	socketbug.sb_application.on('fetch_source', function ()
247 	{		
248 		try
249 		{
250 			/** Fetch Source Code */
251 			var source_code = document.getElementsByTagName("html")[0].innerHTML;
252 			
253 			/** Replace Characters that can cause issues on some browsers */
254 			source_code = source_code.replace(/</g,'<');
255 			source_code = source_code.replace(/>/g,'>');
256 			
257 			/** Send to Specific Client */
258 			socketbug.sb_console.emit('view_source', source_code);
259 			
260 			socketbug.log('Console Requested Source Code', 'info');
261 		}
262 		catch(error)
263 		{
264 			socketbug.log(error, 'error');
265 		}
266 	});
267 	
268 	/** Capture Connect Event */
269 	socketbug.sb_manager.on('disconnect', function()
270 	{
271 		/* Do Callback if one set */
272 		if(typeof(_sbs.disconnect_callback) === "function")
273 		{
274 			_sbs.disconnect_callback();
275 		}
276 	});
277 	
278 	/** Capture Responses from Socketbug Manager */
279 	socketbug.sb_manager.on('manager_response', function (message, level)
280 	{
281 		socketbug.log(message, level);
282 	});	
283 	
284 	/** Configure Callback Handler to use Socketbug */
285 	debug.setCallback(socketbug.debug, true);
286 	
287 	/** Set Debug Level for Socketbug Console */
288 	debug.setLevel(socketbug.debug_level);
289 }