source: branches/1.2/workflow/inc/class.bo_editor.inc.php @ 1349

Revision 1349, 8.0 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

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