source: sandbox/workflow/branches/609/inc/class.bo_editor.inc.php @ 2311

Revision 2311, 7.9 KB checked in by pedroerp, 14 years ago (diff)

Ticket #609 - Migrando instanciação das classes da engine para a factory.

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