Code

Removed carriage return from uploaded FAI files.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiScriptEntry.inc
1 <?php
3 class faiScriptEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("cn","description","FAIpriority","FAIscript");
8   var $objectclasses= array();
10   var $orig_cn              = "";
12   var $dn            = "";
13   var $cn            = "";
14   var $FAIpriority   = "0";
15   var $FAIscript     = "";
16   var $description   = "";
17   var $status        = "new";
18   var $parent        = NULL;
19   var $FAIstate      = "";
20   
21   function faiScriptEntry (&$config, $dn= NULL,$object=false)
22   {
23     plugin::plugin ($config, NULL);
24     if($dn != "new"){
25       $this->orig_cn= $object['cn'];
26       $this->dn=$object['dn'];
27       foreach($object as $name=>$value){
28         $oname = $name;
29         $this->$oname=$value;
30       }
31     }else{
32       if(is_array($object)&&count($object)){
33         $this->orig_cn= $object['cn'];
34         $this->dn=$object['dn'];
35         foreach($object as $name=>$value){
36           $oname = $name;
37           $this->$oname=$value;
38         }
39       }else{
41         $this->status = "new";
42         $this->orig_cn       = false;
43       }
44     }
45   }
47   function execute()
48   {
49     /* Call parent execute */
50     plugin::execute();
52     /* Fill templating stuff */
53     $smarty     = get_smarty();
54     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
55     $display = "";
56   
57     if(isset($_POST['ImportUpload']) && $this->acl_is_writeable("FAIscript")){
58       if(($_FILES['ImportFile']['error']!=0)){
59         msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
60       }else
61       if(($_FILES['ImportFile']['size']==0)){
62         msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
63       }else{
64         $str = utf8_encode(addslashes(file_get_contents($_FILES['ImportFile']['tmp_name'])));
65         $str = preg_replace("/[\r]/","",$str);
66         $this->FAIscript = $str;
67       }
68     }
69   
70      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
71      * If we post the escaped strings they will be escaped again
72      */
73     foreach($this->attributes as $attrs){
74       if(get_magic_quotes_gpc()){
75         $smarty->assign($attrs,(stripslashes($this->$attrs)));
76       }else{
77         $smarty->assign($attrs,($this->$attrs));
78       }
79     }
81     /* File download requested */
82     if(isset($_GET['getFAIScript'])){
83       send_binary_content(stripslashes($this->FAIscript),$this->cn.".FAIscript");
84     }
86     /* Create download button*/
87     if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
88       $smarty->assign("DownMe","<a href='?plug=".$_GET['plug']."&getFAIScript'>
89         <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0 class='center'>
90         </a>");
91     }else{
92       $smarty->assign("DownMe","");
93     }
95     for($i =0 ; $i < 100 ; $i++){
96       $FAIprioritys[$i]=$i;
97     }
99     $tmp = $this->plInfo();
100     foreach($tmp['plProvidedAcls'] as $name => $translated){
101       $acl = $this->getacl($name,preg_match("/freeze/i",$this->FAIstate));
102       $smarty->assign($name."ACL",$acl);
103     }
105     if(get_magic_quotes_gpc()){
106       $smarty->assign("FAIscript" , htmlentities(stripslashes($this->FAIscript)));
107     }else{
108       $smarty->assign("FAIscript" , htmlentities($this->FAIscript));
109     }
110     $smarty->assign("FAIprioritys",$FAIprioritys);
111     $display.= $smarty->fetch(get_template_path('faiScriptEntry.tpl', TRUE));
112     return($display);
113   }
115   /* Save data to object */
116   function save_object()
117   {
118     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
119       foreach($this->attributes as $attrs){
120         if($this->acl_is_writeable($attrs)){
121           if(isset($_POST[$attrs])){
122             $this->$attrs = $_POST[$attrs];
123           }else{
124             $this->$attrs = "";
125           }
126         }
127       }
128     }
129   }
131   /* Check supplied data */
132   function check()
133   {
134     /* Call common method to give check the hook */
135     $message= plugin::check();
136   
137     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
138       $message[] = msgPool::duplicated(_("Name"));
139     }
141     $c = trim($this->cn);
142     if($c == ""){
143       $message[] = msgPool::required(_("Name"));
144     }
145     if(preg_match("/[^a-z0-9_\-]/i",$c)){
146       $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
147     }
149     $s = trim($this->FAIscript);
150     if($s == ""){
151       $message[]= msgPool::required(_("Script"));
152     }
154     return ($message);
155   }
156  
157   function save()
158   {
159     $tmp=array();
160     foreach($this->attributes as $attrs){ 
161       $tmp[$attrs] = $this->$attrs;
162     }
164     /* Strip out dos newlines */
165     $tmp['FAIscript']= strtr($this->FAIscript, array("\x0D" => ""));
167     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
168       $tmp['remove']['from']  = $this->orig_cn;
169       $tmp['remove']['to']    = $tmp['cn'];
170     }
171   
172     $tmp['dn']      = $this->dn;  
173     $tmp['status']  = $this->status; 
174     return($tmp);
175   }
177   
178   /* Return plugin informations for acl handling */
179   static function plInfo()
180   {
181     return (array(
182           "plShortName" => _("Script entry"),
183           "plDescription" => _("FAI script entry"),
184           "plSelfModify"  => FALSE,
185           "plDepends"     => array(),
186           "plPriority"    => 19,
187           "plSection"     => array("administration"),
188           "plCategory"    => array("fai"),
189           "plProvidedAcls" => array(
190             "cn"              => _("Name"),
191             "description"     => _("Description"),
192             "FAIscript"       => _("Script entry"),
193             "FAIpriority"     => _("Script Priority"))
194           ));
195   }
199 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
200 ?>