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(CONFIG_DIR."/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(CONFIG_DIR."/glpi/")){
134 print_red(sprintf(_("Missing directory '%s/glpi/' to store glpi uploads."),CONFIG_DIR));
135 }else{
136 $filen = CONFIG_DIR."/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 occurred
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(CONFIG_DIR."/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->SetPluginMode();
200 $divlist->SetHeader(array(
201 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
202 array("string" => _("Name")),
203 array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
204 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
206 $divlist->SetSummary(_("This table displays all available attachments."));
207 $divlist->SetEntriesPerPage(0);
209 $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
210 $editdel.= "<input type='image' name='delAttach_%s' src='images/edittrash.png'>";
212 /* Add all given attachments to divlist
213 */
214 foreach($attach as $entry){
216 /* Create display name*/
217 if((empty($entry['name']))||(empty($entry['comment']))){
219 /* In glpi it is possible to add entries without name
220 so i've added this block
221 */
222 if(empty($entry['name'])){
223 $str1 = "<i>"._("empty")."</i>";
224 }else{
225 $str1 = $entry['name'];
226 }
228 if(!empty($entry['comment'])){
229 $str1.= " [".$entry['comment']."]";
230 }
231 }else{
232 /* Both attributes given */
233 $str1 = $entry['name']." [".$entry['comment']."]";
234 }
236 $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
237 $str2 = $entry['mime']." ";
239 $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
240 "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
242 if(in_array($entry['ID'],$this->Selected)){
243 $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
244 }else {
245 $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
246 }
247 $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
248 $divlist->AddEntry(array(
249 array("string" => $chkbox,
250 "attach" => "style='text-align:center;width:20px;'"),
251 array("string"=> $str1),
252 array("string"=> $str2,"attach"=>"style='width:200px;'"),
253 array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
254 ));
256 }
258 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
259 " <input class='center' type='image'
260 src='images/fai_new_template.png' align='middle' title='"._("Create new attachment")."'
261 name='new_attach' alt='"._("New Attachment"). "'> ".
262 "</div>";
264 $smarty->assign("attachments", $divlist->DrawList());
265 $smarty->assign("attachmenthead", $listhead);
266 $smarty->assign("search_image", get_template_path('images/search.png'));
267 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
268 $smarty->assign("tree_image", get_template_path('images/tree.png'));
269 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
270 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
271 $smarty->assign("apply", apply_filter());
272 $smarty->assign("alphabet", generate_alphabet());
273 $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
275 $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
276 return($display);
277 }
279 function save()
280 {
281 return($this->Selected);
282 }
284 /* Adds new or saves edited entries */
285 function save_entry()
286 {
287 if($this->edit){
288 $tmp = array();
289 foreach($this->attributes as $attr){
290 $tmp[$attr] = $this->$attr;
291 }
292 $id = -1;
293 if(isset($this->entry['ID'])){
294 $id = $this->entry['ID'];
295 }
296 $this->parent->handle->saveAttachments($tmp,$id);
297 }
298 }
300 function save_object()
301 {
302 foreach($this->attributes as $attr){
303 if(isset($_POST[$attr])){
304 $this->$attr = $_POST[$attr];
305 }
306 }
308 /* Checkboxes are only posted, if they are checked
309 * To check if it was deselected, we check if wasOnPage
310 * was posted with given name, so we can deselect this entry
311 */
313 foreach($_POST as $name => $value){
314 if(preg_match("/wasOnPage_/",$name)){
315 $id=preg_replace("/wasOnPage_/","",$name);
316 if(isset($_POST["useMe_".$id])){
317 $this->Selected[$id]=$id;
318 }else{
319 if(isset($this->Selected[$id])){
320 unset($this->Selected[$id]);
321 }
322 }
323 }
324 }
325 }
327 /* Simple check */
328 function check()
329 {
330 /* Call common method to give check the hook */
331 $message= plugin::check();
333 if($this->edit){
335 /* check if given name is already in use */
336 $att = $this->parent->handle->getAttachments();
337 $ok = true;
338 $this->name=trim($this->name);
340 foreach($att as $val){
341 if(!isset($this->entry['ID'])){
342 if($val['name'] == $this->name){
343 $ok = false;
344 }
345 }else{
346 if(($val['name'] == $this->name)&&($this->entry['ID'] != $val['ID'])){
347 $ok = false;
348 }
349 }
350 }
351 if(!$ok){
352 $message[] = _("This name is already in use.");
353 }
354 if(empty($this->name)){
355 $message[] = _("Please specify a valid name for this attachment.");
356 }
357 }
358 return($message);
359 }
361 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
362 ?>