b41d4a0b9a01e53d448736d98776836abccec570
1 <?php
3 class glpiAttachmentPool extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
12 var $attributes = array("name","comment","mime","filename");
13 var $objectclasses = array("whatever");
15 var $parent;
16 var $edit = false;
17 var $entry = false;
19 var $name ="";
20 var $comment ="";
21 var $mime ="";
22 var $filename ="";
24 var $Selected = array();
26 var $delAttach= "";
28 function glpiAttachmentPool ($config, $dn= NULL,$used=NULL)
29 {
30 plugin::plugin ($config, $dn);
31 if(!isset($_SESSION['GlpiAttachmentFilter'])){
32 $_SESSION['GlpiAttachmentFilter'] = array("filter"=>"*");
33 }
35 /* Assign used attributes */
36 if($used != NULL){
37 $this->Selected = $used;
38 }
39 }
41 function execute()
42 {
43 plugin::execute();
44 $attach = $this->parent->handle->getAttachments();
45 /* Fill templating stuff */
46 $smarty = get_smarty();
47 $display = "";
48 $only_once = true;
50 /* walk through all posted objects */
51 foreach($_POST as $name => $value){
53 /* Open dialog to create a new entry */
54 if(preg_match("/new_attach/",$name)){
55 $this->edit = true;
56 $this->entry=array();
58 /* Set default values */
59 foreach($this->attributes as $attr) {
60 $this->$attr = "";
61 $this->entry[$attr]="";
62 }
63 }
65 /* Remove attach*/
66 if((preg_match("/^delAttach_/",$name))&&($only_once)){
67 $only_once = false;
68 $str = preg_replace("/^delAttach_/","",$name);
69 $str = base64_decode(preg_replace("/_.*$/","",$str));
71 /* remove attach from db */
72 $this->delAttach = $str;
73 $smarty->assign("warning", sprintf(_("You're about to delete the glpi attachment component '%s'."), $attach[$str]['name']));
74 return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
75 }
77 /* Start editing entry */
78 if((preg_match("/^editAttach_/",$name))&&($only_once)){
79 $only_once = false;
80 $str = preg_replace("/^editAttach_/","",$name);
81 $str = base64_decode(preg_replace("/_.*$/","",$str));
83 /* Check if we have an attachment with given ID */
84 foreach($attach as $att ){
85 if($att['ID'] == $str ){
87 /* Entry was found, show dialog */
88 $this->edit = true;
89 $this->entry = $att;
91 foreach($att as $name => $value){
92 $this->$name = $value;
93 }
94 }
95 }
96 }
97 }
99 if((isset($_POST['delete_glpi_confirm']))&&(isset($attach[$this->delAttach]))){
100 $this->parent->handle->deleteAttachment($this->delAttach);
101 $attach = $this->parent->handle->getAttachments();
102 }
104 /* Someone tries to upload a file */
105 if(($this->edit == true)&&(isset($_POST['upload']))){
106 if(!isset($_FILES['filename'])){
107 print_red(_("There is no valid file uploaded."));
108 }else{
109 $FILE = $_FILES['filename'];
110 if(!isset($FILE['name'])){
111 print_red(_("There is no valid file uploaded."));
112 }else{
113 if($FILE['error']!=0) {
114 print_red(_("Upload wasn't successfull."));
115 }else{
116 if(!is_dir("/etc/gosa/glpi/")){
117 print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
118 }else{
119 $filen = "/etc/gosa/glpi/".$FILE['name'];
120 $fh = fopen($filen,"w");
121 if(!$fh){
122 print_red(sprintf(_("Can't create file '%s'."),$filen));
123 }else{
124 $str = file_get_contents($FILE['tmp_name']);
125 fwrite($fh,$str,strlen($str));
126 fclose($fh);
127 $this->mime = $FILE['type'];
128 $this->filename = $FILE['name'];
129 }
130 }
131 } // Check if any error occured
132 } // check if valid filename was uploaded
133 } // ende check if file was uploaded
134 }// upload post
136 /* save attachment*/
137 if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
138 if(count($this->check())==0){
139 $this->save_entry();
140 $this->edit= false;
141 $this->entry = array();
142 $attach = $this->parent->handle->getAttachments();
143 }else{
144 foreach($this->check() as $msg){
145 print_red($msg);
146 }
147 }
148 }
150 /* Abort editing/adding attachment*/
151 if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
152 $this->edit = false;
153 $this->entry = array();
154 }
156 /* If edit == true, we have to show a dialog to edit or add an attach
157 */
158 if($this->edit == true){
159 foreach($this->attributes as $attr){
160 $smarty->assign($attr,htmlentities($this->$attr));
161 }
162 if(!empty($this->filename)){
163 if(is_readable("/etc/gosa/glpi/".$this->filename)){
164 $status =_("File is available.");
165 }else{
166 $status =_("File is not readable, possibly the file is missing.");
167 }
168 }else{
169 $status = _("Currently no file uploaded.");
170 }
171 $smarty->assign("status",$status);
172 return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
173 }
175 /* Create list with checkboxes to select / deselect some attachents */
176 $divlist = new divlist("Attachment");
177 $divlist->SetHeader(array(
178 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
179 array("string" => _("Name")),
180 array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
181 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
183 $divlist->SetSummary(_("This table displays all available attachments."));
184 $divlist->SetEntriesPerPage(0);
186 $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
187 $editdel.= "<input type='image' name='delAttach_%s' src='images/edittrash.png'>";
189 /* Add all given attachments to divlist
190 */
191 foreach($attach as $entry){
193 /* Create display name*/
194 if((empty($entry['name']))||(empty($entry['comment']))){
196 /* In glpi it is possible to add entries without name
197 so i've added this block
198 */
199 if(empty($entry['name'])){
200 $str1 = "<i>"._("empty")."</i>";
201 }else{
202 $str1 = $entry['name'];
203 }
205 if(!empty($entry['comment'])){
206 $str1.= " [".$entry['comment']."]";
207 }
208 }else{
209 /* Both attributes given */
210 $str1 = $entry['name']." [".$entry['comment']."]";
211 }
213 $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
214 $str2 = $entry['mime']." ";
216 $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
217 "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
219 if(in_array($entry['ID'],$this->Selected)){
220 $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
221 }else {
222 $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
223 }
224 $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
225 $divlist->AddEntry(array(
226 array("string" => $chkbox,
227 "attach" => "style='text-align:center;width:20px;'"),
228 array("string"=> $str1),
229 array("string"=> $str2,"attach"=>"style='width:200px;'"),
230 array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
231 ));
233 }
235 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
236 " <input class='center' type='image'
237 src='images/new.png' align='middle' title='"._("Create new attachment")."'
238 name='new_attach' alt='"._("New Attachment"). "'> ".
239 "</div>";
241 $smarty->assign("attachments", $divlist->DrawList());
242 $smarty->assign("attachmenthead", $listhead);
243 $smarty->assign("search_image", get_template_path('images/search.png'));
244 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
245 $smarty->assign("tree_image", get_template_path('images/tree.png'));
246 $smarty->assign("infoimage", get_template_path('images/info.png'));
247 $smarty->assign("launchimage", get_template_path('images/launch.png'));
248 $smarty->assign("apply", apply_filter());
249 $smarty->assign("alphabet", generate_alphabet());
250 $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
252 $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
253 return($display);
254 }
256 function save()
257 {
258 return($this->Selected);
259 }
261 /* Adds new or saves edited entries */
262 function save_entry()
263 {
264 if($this->edit){
265 $tmp = array();
266 foreach($this->attributes as $attr){
267 $tmp[$attr] = $this->$attr;
268 }
269 $id = -1;
270 if(isset($this->entry['ID'])){
271 $id = $this->entry['ID'];
272 }
273 $this->parent->handle->saveAttachments($tmp,$id);
274 }
275 }
277 function save_object()
278 {
279 foreach($this->attributes as $attr){
280 if(isset($_POST[$attr])){
281 $this->$attr = $_POST[$attr];
282 }
283 }
285 /* Checkboxes are only posted, if they are checked
286 * To check if it was deselected, we check if wasOnPage
287 * was posted with given name, so we can deselect this entry
288 */
290 foreach($_POST as $name => $value){
291 if(preg_match("/wasOnPage_/",$name)){
292 $id=preg_replace("/wasOnPage_/","",$name);
293 if(isset($_POST["useMe_".$id])){
294 $this->Selected[$id]=$id;
295 }else{
296 if(isset($this->Selected[$id])){
297 unset($this->Selected[$id]);
298 }
299 }
300 }
301 }
302 }
304 /* Simple check */
305 function check()
306 {
307 $message = array();
308 if($this->edit){
309 if(empty($this->name)){
310 $message[] = _("Please specify a valid name for this attachment.");
311 }
312 }
313 return($message);
314 }
316 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
317 ?>