@@ -25,8 +25,8 @@ angular.module('mm.core')
2525 * @name $mmWS
2626 */
2727. factory ( '$mmWS' , function ( $http , $q , $log , $mmLang , $cordovaFileTransfer , $mmApp , $mmFS , mmCoreSessionExpired , $translate , $window ,
28- mmCoreUserDeleted , md5 , $timeout , mmWSTimeout , mmCoreUserPasswordChangeForced , mmCoreUserNotFullySetup ,
29- mmCoreSitePolicyNotAgreed ) {
28+ mmCoreUserDeleted , md5 , $timeout , mmWSTimeout , mmCoreUserPasswordChangeForced , mmCoreUserNotFullySetup , $mmText ,
29+ mmCoreSitePolicyNotAgreed , mmCoreUnicodeNotSupported ) {
3030
3131 $log = $log . getInstance ( '$mmWS' ) ;
3232
@@ -49,14 +49,13 @@ angular.module('mm.core')
4949 * - wstoken string The Webservice token.
5050 * - responseExpected boolean Defaults to true. Set to false when the expected response is null.
5151 * - typeExpected string Defaults to 'object'. Use it when you expect a type that's not an object|array.
52+ * - cleanUnicode boolean Defaults to false. Clean multibyte Unicode chars from data.
5253 * @return {Promise } Promise resolved with the response data in success and rejected with the error message if it fails.
5354 */
5455 self . call = function ( method , data , preSets ) {
5556
5657 var siteurl ;
5758
58- data = convertValuesToString ( data ) ;
59-
6059 if ( typeof preSets == 'undefined' || preSets === null ||
6160 typeof preSets . wstoken == 'undefined' || typeof preSets . siteurl == 'undefined' ) {
6261 return $mmLang . translateAndReject ( 'mm.core.unexpectederror' ) ;
@@ -69,6 +68,13 @@ angular.module('mm.core')
6968 preSets . responseExpected = true ;
7069 }
7170
71+ try {
72+ data = convertValuesToString ( data , preSets . cleanUnicode ) ;
73+ } catch ( e ) {
74+ // Empty cleaned text found.
75+ return $mmLang . translateAndReject ( 'mm.core.unicodenotsupportedcleanerror' ) ;
76+ }
77+
7278 data . wsfunction = method ;
7379 data . wstoken = preSets . wstoken ;
7480 siteurl = preSets . siteurl + '/webservice/rest/server.php?moodlewsrestformat=json' ;
@@ -133,6 +139,8 @@ angular.module('mm.core')
133139 return $q . reject ( mmCoreUserNotFullySetup ) ;
134140 } else if ( data . errorcode === 'sitepolicynotagreed' ) {
135141 return $q . reject ( mmCoreSitePolicyNotAgreed ) ;
142+ } else if ( data . errorcode === 'dmlwriteexception' && $mmText . hasUnicodeData ( ajaxData ) ) {
143+ return $q . reject ( mmCoreUnicodeNotSupported ) ;
136144 } else {
137145 return $q . reject ( data . message ) ;
138146 }
@@ -280,19 +288,27 @@ angular.module('mm.core')
280288 * Converts an objects values to strings where appropriate.
281289 * Arrays (associative or otherwise) will be maintained.
282290 *
283- * @param {Object } data The data that needs all the non-object values set to strings.
291+ * @param {Object } data The data that needs all the non-object values set to strings.
292+ * @param {Boolean } stripUnicode If Unicode long chars need to be stripped.
284293 * @return {Object } The cleaned object, with multilevel array and objects preserved.
285294 */
286- function convertValuesToString ( data ) {
295+ function convertValuesToString ( data , stripUnicode ) {
287296 var result = [ ] ;
288297 if ( ! angular . isArray ( data ) && angular . isObject ( data ) ) {
289298 result = { } ;
290299 }
291300 for ( var el in data ) {
292301 if ( angular . isObject ( data [ el ] ) ) {
293- result [ el ] = convertValuesToString ( data [ el ] ) ;
302+ result [ el ] = convertValuesToString ( data [ el ] , stripUnicode ) ;
294303 } else {
295- result [ el ] = data [ el ] + '' ;
304+ if ( typeof data [ el ] == "string" ) {
305+ result [ el ] = stripUnicode ? $mmText . stripUnicode ( data [ el ] ) : data [ el ] ;
306+ if ( stripUnicode && data [ el ] != result [ el ] && result [ el ] . trim ( ) . length == 0 ) {
307+ throw new Exception ( ) ;
308+ }
309+ } else {
310+ result [ el ] = data [ el ] + '' ;
311+ }
296312 }
297313 }
298314 return result ;
0 commit comments