changed git call from https to git readonly
[atutor.git] / mods / patcher / include / json.inc.php
1 <?php
2 // my simple JSON encode/decode function, similar to PHP5.2 
3 function json_encode_result($phpdata) {
4         if(gettype($phpdata) == "resource") 
5                 return php2js_sqlresult($phpdata);
6         else if(is_array($phpdata)) 
7                 return php2js_array($phpdata);
8         else
9                 return php2js_object($phpdata);
10 }
11
12
13 //function json_decode($jsonata) {
14 //      return $phpdata;
15 //}
16
17
18 // convert a PHP object to javascript object
19 function php2js_object($phpobj) {
20         $str = ""; 
21         
22         if (!is_array($phpobj)) return "[]";
23         
24         foreach($phpobj as $col => $val) {
25           if($str == "")
26             $str = $col .":'" . escapeString($val) . "'";
27           else
28             $str = $str . "," . $col .":'" . escapeString($val) . "'";
29         }
30         
31         return "{" . $str . "}";
32 }
33
34 // convert a PHP object to javascript object
35 function php2js_array($phparr) {
36         $str = "";
37
38   if (!is_array($phparr)) return "[]";
39
40         foreach ($phparr as $e) {
41           if($str == "") 
42                         $str = php2js_object($e) ;
43           else
44             $str = $str . "," . php2js_object($e);
45         }
46         
47         return "[" . $str . "]";
48 }
49
50 // convert a SQL result object to javascript object
51 function php2js_sqlresult($phpsql) {
52         // Printing results
53         $rows = array();
54         while ($line = mysql_fetch_assoc($phpsql)) {
55                 $rows[] = $line;
56         }
57         mysql_free_result($phpsql);
58         return php2js_array($rows);
59 }
60
61 function escapeString($string) {
62     $escape = array(
63     "\r\n" => '\n',
64     "\r"    => '\n',
65     "\n"    => '\n'
66     );
67
68     return str_replace(array_keys($escape), array_values($escape), addslashes($string));
69 }       
70
71 ?>