00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 function cm_convert_all_web_vars_to_array($prefix, $remove=true)
00030 {
00031 return array_merge(cm_web_to_array($_GET, $prefix, $remove), cm_web_to_array($_POST, $prefix, $remove));
00032 }
00033
00034 function cm_web_to_array($web, $prefix, $remove=true)
00035 {
00036 if ($prefix=="")
00037 $r=$web;
00038 else
00039 $r=array();
00040 foreach ($web as $key => $value)
00041 if ($prefix==substr($key, 0, strlen($prefix)))
00042 {
00043 if ($remove) $key=substr($key,strlen($prefix));
00044 $r[$key]=$value;
00045 }
00046 return $r;
00047 }
00048
00049 function cm_access($array, $output=true)
00050 {
00051 # Convert to cM format and call cm front-end
00052 # FGG: in the future we may want to connect through socket
00053
00054 $module=$array["cm_run_module_uoa"]; unset($array["cm_run_module_uoa"]);
00055 $action=$array["cm_action"]; unset($array["cm_action"]);
00056
00057 $str=json_encode($array);
00058 $n=tempnam("", "cm-");
00059 $f=fopen($n, "w"); fwrite($f, $str); fclose($f);
00060
00061 $cm_root=getenv("CM_ROOT"); if ($cm_root=="") $cm_root=getcwd();
00062 $cm_dir_bin="bin";
00063
00064 # Prepare call to CM plugin (hardwired)
00065 $cmd=$cm_root.DIRECTORY_SEPARATOR.$cm_dir_bin.DIRECTORY_SEPARATOR.$cmd="cm ".$module." ".$action." @".$n;
00066
00067 #Add cmd if Windows (FGG:TODO maybe can be done cleaner?)
00068 if (substr(strtoupper(PHP_OS),0,3)=="WIN")
00069 $cmd="cmd /c ".$cmd;
00070
00071 #FGG: important note for Windows: stderr pipe should be "a" not "w"
00072 # see http://www.php.net/manual/en/function.proc-open.php Luceo 28-Mar-2010 07:39
00073 $tmpfname = tempnam("", "cmerr-");
00074 $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", $tmpfname, "w"));
00075
00076 $process = proc_open($cmd, $descriptorspec, $pipes, NULL, NULL);
00077
00078 $text1='';
00079 $text2='';
00080 if (is_resource($process))
00081 {
00082 fclose($pipes[0]);
00083
00084 $text1=stream_get_contents($pipes[1]);
00085 fclose($pipes[1]);
00086
00087 $return_value = proc_close($process);
00088 }
00089
00090 $text2=file_get_contents($tmpfname);
00091 unlink($tmpfname);
00092
00093 if ($output)
00094 {
00095 echo $text1;
00096 echo $text2;
00097 }
00098
00099 if (file_exists($n)) unlink($n);
00100
00101 return $text1.$text2;
00102 }
00103
00104 function prepare_json_from_array($a, $prefix)
00105 {
00106 $b=array();
00107 foreach ($a as $key => $value)
00108 {
00109 if ($prefix!="")
00110 {
00111 $x=strpos($key, $prefix);
00112 if (($x!==false) && ($x==0))
00113 $key=substr($key,strlen($prefix));
00114 }
00115 $b[$key]=$value;
00116 }
00117
00118 return $b;
00119 }
00120
00121 function prepare_str_from_array($a, $prefix)
00122 {
00123 $str="";
00124 foreach ($a as $key => $value)
00125 {
00126 if ($prefix!="")
00127 {
00128 $x=strpos($key, $prefix);
00129 if (($x!==false) && ($x==0))
00130 $key=substr($key,strlen($prefix));
00131 }
00132 $str.=" "+$key."=".$value;
00133 }
00134
00135 return $b;
00136 }
00137
00138 function get_var($a, $p)
00139 {
00140 if (!array_key_exists($p, $a)) return NULL;
00141 return $a[$p];
00142 }
00143
00144 function check_cm_json($s)
00145 {
00146 $s=str_replace('^22^', '"', $s);
00147 $s=str_replace('%22', '"', $s);
00148 $s=str_replace('%26quot;', '"', $s);
00149 $s=str_replace('"', '"', $s);
00150
00151 $r=json_decode($s,TRUE);
00152
00153 return $r;
00154 }
00155
00156 function urlsafe_b64decode($s)
00157 {
00158 # return str_replace(array('-','_','.'), array('+','/','='), $s);
00159 return base64_decode(str_replace(array('-','_'), array('+','/'), $s));
00160 }
00161
00162 ?>