[795] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * -------------------------------------------- * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 7 | * under the terms of the GNU General Public License as published by the * |
---|
| 8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 9 | * option) any later version. * |
---|
| 10 | \**************************************************************************/ |
---|
| 11 | |
---|
| 12 | require_once('class.bo_ajaxinterface.inc.php'); |
---|
| 13 | /** |
---|
| 14 | * @package Workflow |
---|
| 15 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 16 | * @author Rodrigo Daniel C de Lira - rodrigo.lira@gmail.com |
---|
| 17 | * @author Sidnei Augusto Drovetto Junior - drovetto@gmail.com |
---|
| 18 | */ |
---|
| 19 | class bo_editor extends bo_ajaxinterface |
---|
| 20 | { |
---|
| 21 | /** |
---|
| 22 | * Contructor |
---|
| 23 | * @access public |
---|
| 24 | * @return object |
---|
| 25 | */ |
---|
| 26 | function bo_editor() { |
---|
| 27 | parent::bo_ajaxinterface(); |
---|
| 28 | } |
---|
| 29 | /** |
---|
| 30 | * Get the source |
---|
| 31 | * @param string $proc_name process name |
---|
| 32 | * @param string $file_name file name |
---|
| 33 | * @param string $type type |
---|
| 34 | * @access public |
---|
| 35 | * @return string source data |
---|
| 36 | */ |
---|
| 37 | function get_source($proc_name, $file_name, $type) |
---|
| 38 | { |
---|
| 39 | if ((strpos($file_name,'/') !== false) || (strpos($file_name,'/') !== false)) |
---|
| 40 | exit(0); |
---|
| 41 | if ((strpos($proc_name,'/') !== false) || (strpos($proc_name,'/') !== false)) |
---|
| 42 | exit(0); |
---|
| 43 | |
---|
| 44 | switch($type) |
---|
| 45 | { |
---|
| 46 | case 'atividade': |
---|
| 47 | $path = 'activities' . SEP . $file_name; |
---|
| 48 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 49 | break; |
---|
| 50 | case 'template': |
---|
| 51 | $path = 'templates' . SEP . $file_name; |
---|
| 52 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 53 | break; |
---|
| 54 | case 'include': |
---|
| 55 | $path = $file_name; |
---|
| 56 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 57 | break; |
---|
| 58 | case 'resource': |
---|
| 59 | $complete_path = GALAXIA_PROCESSES . '/' . $proc_name . '/resources/' . $file_name; |
---|
| 60 | break; |
---|
| 61 | } |
---|
| 62 | if (!file_exists($complete_path)) |
---|
| 63 | exit(0); |
---|
| 64 | |
---|
| 65 | if (!$file_size = filesize($complete_path)) return ''; |
---|
| 66 | $fp = fopen($complete_path, 'r'); |
---|
| 67 | $data = fread($fp, $file_size); |
---|
| 68 | fclose($fp); |
---|
| 69 | |
---|
| 70 | //if ($type != 'template') |
---|
| 71 | //{ |
---|
| 72 | $data = str_replace("\r", "", $data); |
---|
| 73 | $data = str_replace("\\", "\\\\", $data); |
---|
| 74 | $data = str_replace("\n", "\\n", $data); |
---|
| 75 | $data = str_replace('"', '" +String.fromCharCode(34)+ "', $data); |
---|
| 76 | $data = str_replace('\'', '" +String.fromCharCode(39)+ "', $data); |
---|
| 77 | $data = str_replace('<', '" +String.fromCharCode(60)+ "', $data); |
---|
| 78 | $data = str_replace('>', '" +String.fromCharCode(62)+ "', $data); |
---|
| 79 | //$data = addslashes($data); |
---|
| 80 | $data = str_replace("\t", "\\t", $data); |
---|
| 81 | //$data = str_replace("\n", "\\n", $data); |
---|
| 82 | //} |
---|
| 83 | |
---|
| 84 | return $data; |
---|
| 85 | } |
---|
| 86 | /** |
---|
| 87 | * Save the source |
---|
| 88 | * @param string $proc_name process name |
---|
| 89 | * @param string $file_name file name |
---|
| 90 | * @param string $type type |
---|
| 91 | * @param string $source |
---|
| 92 | * @access public |
---|
| 93 | * @return string |
---|
| 94 | */ |
---|
| 95 | function save_source($proc_name, $file_name, $type, $source) |
---|
| 96 | { |
---|
| 97 | if ((strpos($file_name,'/') !== false) || (strpos($file_name,'/') !== false)) |
---|
| 98 | return 'Não foi possível executar a operação solicitada'; |
---|
| 99 | if ((strpos($proc_name,'/') !== false) || (strpos($proc_name,'/') !== false)) |
---|
| 100 | return 'Não foi possível executar a operação solicitada'; |
---|
| 101 | |
---|
| 102 | // in case code was filtered |
---|
| 103 | //if (!$source) $source = @$GLOBALS['egw_unset_vars']['_POST[source]']; |
---|
| 104 | |
---|
| 105 | switch($type) |
---|
| 106 | { |
---|
| 107 | case 'atividade': |
---|
| 108 | $path = 'activities' . SEP . $file_name; |
---|
| 109 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 110 | break; |
---|
| 111 | case 'template': |
---|
| 112 | $path = 'templates' . SEP . $file_name; |
---|
| 113 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 114 | break; |
---|
| 115 | case 'include': |
---|
| 116 | $path = $file_name; |
---|
| 117 | $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path; |
---|
| 118 | break; |
---|
| 119 | case 'resource': |
---|
| 120 | $complete_path = GALAXIA_PROCESSES . '/' . $proc_name . '/resources/' . $file_name; |
---|
| 121 | break; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // In case you want to be warned when source code is changed: |
---|
| 125 | // mail('yourmail@domain.com', 'source changed', "PATH: $complete_path \n\n SOURCE: $source"); |
---|
| 126 | |
---|
| 127 | $erro = false; |
---|
| 128 | if ($fp = fopen($complete_path, 'w')) |
---|
| 129 | { |
---|
| 130 | $erro = !fwrite($fp, $source); |
---|
| 131 | fclose($fp); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | $erro = true; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return ($erro ? 'Erro ao salvar o arquivo.' : 'Arquivo salvo com sucesso.'); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * Check process |
---|
| 143 | * |
---|
| 144 | * @param integer $pid pid |
---|
| 145 | * @param object $activity_manager |
---|
| 146 | * @param string $error_str error string |
---|
| 147 | * @access public |
---|
| 148 | * @return string |
---|
| 149 | */ |
---|
| 150 | function check_process($pid, &$activity_manager, &$error_str) |
---|
| 151 | { |
---|
| 152 | $valid = $activity_manager->validate_process_activities($pid); |
---|
| 153 | if (!$valid) |
---|
| 154 | { |
---|
| 155 | $errors = $activity_manager->get_error(true); |
---|
| 156 | $error_str = '<b>Os seguintes items devem ser corrigidos para ativar o processo:</b>'; |
---|
| 157 | foreach ($errors as $error) |
---|
| 158 | { |
---|
| 159 | if (strlen($error) > 0) |
---|
| 160 | { |
---|
| 161 | $error_str .= '<li>'. $error . '<br/>'; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | $error_str .= '</ul></small>'; |
---|
| 165 | return 'n'; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | $error_str = ''; |
---|
| 170 | return 'y'; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Save template source |
---|
| 176 | * |
---|
| 177 | * @param array $p process |
---|
| 178 | * @access public |
---|
| 179 | * @return string |
---|
| 180 | */ |
---|
| 181 | function save_template_source($p) |
---|
| 182 | { |
---|
| 183 | |
---|
| 184 | $proc_name = $p['proc_name']; |
---|
| 185 | $file_name = $p['file_name']; |
---|
| 186 | $type = $p['tipo_codigo']; |
---|
| 187 | $source = $p['code']; |
---|
| 188 | $msg = array(); |
---|
| 189 | |
---|
| 190 | $msg[] = $this->save_source($p['proc_name'],$p['file_name'], $p['tipo_codigo'], $p['code']); |
---|
| 191 | |
---|
| 192 | return implode('<br />',$msg); |
---|
| 193 | } |
---|
| 194 | /** |
---|
| 195 | * Save resource |
---|
| 196 | * |
---|
| 197 | * @param array $p process |
---|
| 198 | * @access public |
---|
| 199 | * @return string |
---|
| 200 | */ |
---|
| 201 | function save_resource($p) |
---|
| 202 | { |
---|
| 203 | |
---|
| 204 | $proc_name = $p['proc_name']; |
---|
| 205 | $file_name = $p['file_name']; |
---|
| 206 | $type = $p['tipo_codigo']; |
---|
| 207 | $source = $p['code']; |
---|
| 208 | $msg = array(); |
---|
| 209 | |
---|
| 210 | $msg[] = $this->save_source($p['proc_name'],$p['file_name'], $p['tipo_codigo'], $p['code']); |
---|
| 211 | |
---|
| 212 | return implode('<br />',$msg); |
---|
| 213 | } |
---|
| 214 | /** |
---|
| 215 | * Save php souurce |
---|
| 216 | * |
---|
| 217 | * @param array $p process |
---|
| 218 | * @access public |
---|
| 219 | * @return string |
---|
| 220 | */ |
---|
| 221 | function save_php_source($p) |
---|
| 222 | { |
---|
| 223 | $proc_name = $p['proc_name']; |
---|
| 224 | $file_name = $p['file_name']; |
---|
| 225 | $type = $p['tipo_codigo']; |
---|
| 226 | $source = $p['code']; |
---|
| 227 | $msg = array(); |
---|
| 228 | $error_str = ''; |
---|
| 229 | |
---|
| 230 | $msg[] = $this->save_source($p['proc_name'],$p['file_name'], $p['tipo_codigo'], $p['code']); |
---|
| 231 | |
---|
| 232 | if ($p['tipo_codigo'] != 'include') |
---|
| 233 | { |
---|
[3167] | 234 | $activity_manager = &Factory::newInstance('ActivityManager'); |
---|
[795] | 235 | |
---|
| 236 | if ($this->check_process($p['proc_id'], &$activity_manager, &$error_str) == 'n') |
---|
| 237 | $msg[] = $error_str; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | return implode('<br />',$msg); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Check syntax |
---|
| 245 | * |
---|
| 246 | * @param array $p process |
---|
| 247 | * @access public |
---|
| 248 | * @return string |
---|
| 249 | */ |
---|
| 250 | function check_syntax($p) |
---|
| 251 | { |
---|
| 252 | $code = $p['code']; |
---|
| 253 | $errors = "Check syntax failed."; |
---|
| 254 | $fp = fopen('/tmp/check_syn.tmp', 'w'); |
---|
| 255 | if ($fp) { |
---|
| 256 | fwrite($fp, $code); |
---|
| 257 | $errors = `php -l /tmp/check_syn.tmp`; |
---|
| 258 | $errors = str_replace("in /tmp/check_syn.tmp","",$errors); |
---|
| 259 | $errors = str_replace("parsing /tmp/check_syn.tmp","parsing file",$errors); |
---|
| 260 | $errors = str_replace("/tmp/check_syn.tmp","",$errors); |
---|
| 261 | fclose($fp); |
---|
| 262 | unlink('/tmp/check_syn.tmp'); |
---|
| 263 | } |
---|
| 264 | return $errors; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | } |
---|
| 268 | ?> |
---|