Code

Check for invalid names first before trimming name, catching trailing
[gosa.git] / trunk / 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         $this->FAIscript = $str;
66       }
67     }
68   
69      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
70      * If we post the escaped strings they will be escaped again
71      */
72     foreach($this->attributes as $attrs){
73       if(get_magic_quotes_gpc()){
74         $smarty->assign($attrs,(stripslashes($this->$attrs)));
75       }else{
76         $smarty->assign($attrs,($this->$attrs));
77       }
78     }
80     /* File download requested */
81     if(isset($_GET['getFAIScript'])){
82       send_binary_content(stripslashes($this->FAIscript),$this->cn.".FAIscript");
83     }
85     /* Create download button*/
86     if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
87       $smarty->assign("DownMe","<a href='?plug=".$_GET['plug']."&getFAIScript'>
88         <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0 class='center'>
89         </a>");
90     }else{
91       $smarty->assign("DownMe","");
92     }
94     for($i =0 ; $i < 100 ; $i++){
95       $FAIprioritys[$i]=$i;
96     }
98     $tmp = $this->plInfo();
99     foreach($tmp['plProvidedAcls'] as $name => $translated){
100       $acl = $this->getacl($name,preg_match("/freeze/i",$this->FAIstate));
101       $smarty->assign($name."ACL",$acl);
102     }
104     if(get_magic_quotes_gpc()){
105       $smarty->assign("FAIscript" , htmlentities(stripslashes($this->FAIscript)));
106     }else{
107       $smarty->assign("FAIscript" , htmlentities($this->FAIscript));
108     }
109     $smarty->assign("FAIprioritys",$FAIprioritys);
110     $display.= $smarty->fetch(get_template_path('faiScriptEntry.tpl', TRUE));
111     return($display);
112   }
114   /* Save data to object */
115   function save_object()
116   {
117     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
118       foreach($this->attributes as $attrs){
119         if($this->acl_is_writeable($attrs)){
120           if(isset($_POST[$attrs])){
121             $this->$attrs = $_POST[$attrs];
122           }else{
123             $this->$attrs = "";
124           }
125         }
126       }
127     }
128   }
130   /* Check supplied data */
131   function check()
132   {
133     /* Call common method to give check the hook */
134     $message= plugin::check();
135   
136     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
137       $message[] = msgPool::duplicated(_("Name"));
138     }
140     if(preg_match("/[^a-z0-9_\-]/i",$this->cn)){
141       $message[] = msgPool::invalid(_("Name"),$this->cn,"/[a-z0-9_\-]/i");
142     }
144     if(trim($this->cn)  == ""){
145       $message[] = msgPool::required(_("Name"));
146     }
147     $s = trim($this->FAIscript);
148     if($s == ""){
149       $message[]= msgPool::required(_("Script"));
150     }
152     return ($message);
153   }
154  
155   function save()
156   {
157     $tmp=array();
158     foreach($this->attributes as $attrs){ 
159       $tmp[$attrs] = $this->$attrs;
160     }
162     /* Strip out dos newlines */
163     $tmp['FAIscript']= strtr($this->FAIscript, array("\x0D" => ""));
165     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
166       $tmp['remove']['from']  = $this->orig_cn;
167       $tmp['remove']['to']    = $tmp['cn'];
168     }
169   
170     $tmp['dn']      = $this->dn;  
171     $tmp['status']  = $this->status; 
172     return($tmp);
173   }
175   
176   /* Return plugin informations for acl handling */
177   static function plInfo()
178   {
179     return (array(
180           "plShortName" => _("Script entry"),
181           "plDescription" => _("FAI script entry"),
182           "plSelfModify"  => FALSE,
183           "plDepends"     => array(),
184           "plPriority"    => 19,
185           "plSection"     => array("administration"),
186           "plCategory"    => array("fai"),
187           "plProvidedAcls" => array(
188             "cn"              => _("Name"),
189             "description"     => _("Description"),
190             "FAIscript"       => _("Script entry"),
191             "FAIpriority"     => _("Script Priority"))
192           ));
193   }
197 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
198 ?>