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","date_mod");
13 var $objectclasses = array("whatever");
15 var $parent;
16 var $edit = false;
17 var $entry = false;
18 var $date_mod ="";
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 if($this->parent->handle->is_attachmentUsed($this->delAttach)){
101 $tmp = $this->parent->handle->is_attachmentUsed($this->delAttach);
103 $names = "";
104 foreach($tmp as $name){
105 $names .= ", ".$name;
106 }
107 $names = preg_replace("/^, /","",$names);
108 $names = trim($names);
109 if(count($tmp) == 3){
110 $names .= " ...";
111 }
112 print_red(sprintf(_("You can't delete this attachment, it is still in use by these system(s) '%s'"),$names));
114 }else{
115 $this->parent->handle->deleteAttachment($this->delAttach);
116 @unlink("/etc/gosa/glpi/".$this->filename);
117 $attach = $this->parent->handle->getAttachments();
118 }
119 }
121 /* Someone tries to upload a file */
122 if(($this->edit == true)&&(isset($_POST['upload']))){
123 if(!isset($_FILES['filename'])){
124 print_red(_("There is no valid file uploaded."));
125 }else{
126 $FILE = $_FILES['filename'];
127 if(!isset($FILE['name'])){
128 print_red(_("There is no valid file uploaded."));
129 }else{
130 if($FILE['error']!=0) {
131 print_red(_("Upload wasn't successfull."));
132 }else{
133 if(!is_dir("/etc/gosa/glpi/")){
134 print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
135 }else{
136 $filen = "/etc/gosa/glpi/".$FILE['name'];
137 if(file_exists($filen)){
138 print_red(_("There is already a file with the same name uploaded."));
139 }else{
140 $fh = fopen($filen,"w");
141 if(!$fh){
142 print_red(sprintf(_("Can't create file '%s'."),$filen));
143 }else{
144 $str = file_get_contents($FILE['tmp_name']);
145 fwrite($fh,$str,strlen($str));
146 fclose($fh);
147 $this->mime = $FILE['type'];
148 $this->filename = $FILE['name'];
149 }
150 } // File already exists
151 }
152 } // Check if any error occured
153 } // check if valid filename was uploaded
154 } // ende check if file was uploaded
155 }// upload post
157 /* save attachment*/
158 if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
159 if(count($this->check())==0){
160 $this->date_mod = date("Y-m-d H:i:s");
161 $this->save_entry();
162 $this->edit= false;
163 $this->entry = array();
164 $attach = $this->parent->handle->getAttachments();
165 }else{
166 foreach($this->check() as $msg){
167 print_red($msg);
168 }
169 }
170 }
172 /* Abort editing/adding attachment*/
173 if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
174 $this->edit = false;
175 $this->entry = array();
176 }
178 /* If edit == true, we have to show a dialog to edit or add an attach
179 */
180 if($this->edit == true){
181 foreach($this->attributes as $attr){
182 $smarty->assign($attr,htmlentities(utf8_decode($this->$attr)));
183 }
184 if(!empty($this->filename)){
185 if(is_readable("/etc/gosa/glpi/".$this->filename)){
186 $status =_("File is available.");
187 }else{
188 $status =_("File is not readable, possibly the file is missing.");
189 }
190 }else{
191 $status = _("Currently no file uploaded.");
192 }
193 $smarty->assign("status",$status);
194 return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
195 }
197 /* Create list with checkboxes to select / deselect some attachents */
198 $divlist = new divlist("Attachment");
199 $divlist->SetHeader(array(
200 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
201 array("string" => _("Name")),
202 array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
203 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
205 $divlist->SetSummary(_("This table displays all available attachments."));
206 $divlist->SetEntriesPerPage(0);
208 $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
209 $editdel.= "<input type='image' name='delAttach_%s' src='images/edittrash.png'>";
211 /* Add all given attachments to divlist
212 */
213 foreach($attach as $entry){
215 /* Create display name*/
216 if((empty($entry['name']))||(empty($entry['comment']))){
218 /* In glpi it is possible to add entries without name
219 so i've added this block
220 */
221 if(empty($entry['name'])){
222 $str1 = "<i>"._("empty")."</i>";
223 }else{
224 $str1 = $entry['name'];
225 }
227 if(!empty($entry['comment'])){
228 $str1.= " [".$entry['comment']."]";
229 }
230 }else{
231 /* Both attributes given */
232 $str1 = $entry['name']." [".$entry['comment']."]";
233 }
235 $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
236 $str2 = $entry['mime']." ";
238 $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
239 "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
241 if(in_array($entry['ID'],$this->Selected)){
242 $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
243 }else {
244 $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
245 }
246 $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
247 $divlist->AddEntry(array(
248 array("string" => $chkbox,
249 "attach" => "style='text-align:center;width:20px;'"),
250 array("string"=> $str1),
251 array("string"=> $str2,"attach"=>"style='width:200px;'"),
252 array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
253 ));
255 }
257 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
258 " <input class='center' type='image'
259 src='images/new.png' align='middle' title='"._("Create new attachment")."'
260 name='new_attach' alt='"._("New Attachment"). "'> ".
261 "</div>";
263 $smarty->assign("attachments", $divlist->DrawList());
264 $smarty->assign("attachmenthead", $listhead);
265 $smarty->assign("search_image", get_template_path('images/search.png'));
266 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
267 $smarty->assign("tree_image", get_template_path('images/tree.png'));
268 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
269 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
270 $smarty->assign("apply", apply_filter());
271 $smarty->assign("alphabet", generate_alphabet());
272 $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
274 $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
275 return($display);
276 }
278 function save()
279 {
280 return($this->Selected);
281 }
283 /* Adds new or saves edited entries */
284 function save_entry()
285 {
286 if($this->edit){
287 $tmp = array();
288 foreach($this->attributes as $attr){
289 $tmp[$attr] = $this->$attr;
290 }
291 $id = -1;
292 if(isset($this->entry['ID'])){
293 $id = $this->entry['ID'];
294 }
295 $this->parent->handle->saveAttachments($tmp,$id);
296 }
297 }
299 function save_object()
300 {
301 foreach($this->attributes as $attr){
302 if(isset($_POST[$attr])){
303 $this->$attr = $_POST[$attr];
304 }
305 }
307 /* Checkboxes are only posted, if they are checked
308 * To check if it was deselected, we check if wasOnPage
309 * was posted with given name, so we can deselect this entry
310 */
312 foreach($_POST as $name => $value){
313 if(preg_match("/wasOnPage_/",$name)){
314 $id=preg_replace("/wasOnPage_/","",$name);
315 if(isset($_POST["useMe_".$id])){
316 $this->Selected[$id]=$id;
317 }else{
318 if(isset($this->Selected[$id])){
319 unset($this->Selected[$id]);
320 }
321 }
322 }
323 }
324 }
326 /* Simple check */
327 function check()
328 {
329 $message = array();
330 if($this->edit){
332 /* check if given name is already in use */
333 $att = $this->parent->handle->getAttachments();
334 $ok = true;
335 $this->name=trim($this->name);
337 foreach($att as $val){
338 if(!isset($this->entry['ID'])){
339 if($val['name'] == $this->name){
340 $ok = false;
341 }
342 }else{
343 if(($val['name'] == $this->name)&&($this->entry['ID'] != $val['ID'])){
344 $ok = false;
345 }
346 }
347 }
348 if(!$ok){
349 $message[] = _("This name is already in use.");
350 }
351 if(empty($this->name)){
352 $message[] = _("Please specify a valid name for this attachment.");
353 }
354 }
355 return($message);
356 }
358 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
359 ?>