d36409ea5f25dabe808c4ebccb1fe006c4d1ff3c
1 <?php
3 class glpiAttachmentPool extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array("name","comment","mime","filename","date_mod");
8 var $objectclasses = array("whatever");
10 var $parent;
11 var $edit = false;
12 var $entry = false;
13 var $date_mod ="";
14 var $name ="";
15 var $comment ="";
16 var $mime ="";
17 var $filename ="";
19 var $Selected = array();
21 var $delAttach= "";
23 function glpiAttachmentPool ($config, $dn= NULL,$used=NULL)
24 {
25 plugin::plugin ($config, $dn);
26 if(!isset($_SESSION['GlpiAttachmentFilter'])){
27 $_SESSION['GlpiAttachmentFilter'] = array("filter"=>"*");
28 }
30 /* Assign used attributes */
31 if($used != NULL){
32 $this->Selected = $used;
33 }
34 }
36 function execute()
37 {
38 plugin::execute();
39 $attach = $this->parent->handle->getAttachments();
40 /* Fill templating stuff */
41 $smarty = get_smarty();
42 $display = "";
43 $only_once = true;
45 /* walk through all posted objects */
46 foreach($_POST as $name => $value){
48 /* Open dialog to create a new entry */
49 if(preg_match("/new_attach/",$name)){
50 $this->edit = true;
51 $this->entry=array();
53 /* Set default values */
54 foreach($this->attributes as $attr) {
55 $this->$attr = "";
56 $this->entry[$attr]="";
57 }
58 }
60 /* Remove attach*/
61 if((preg_match("/^delAttach_/",$name))&&($only_once)){
62 $only_once = false;
63 $str = preg_replace("/^delAttach_/","",$name);
64 $str = base64_decode(preg_replace("/_.*$/","",$str));
66 /* remove attach from db */
67 $this->delAttach = $str;
68 $smarty->assign("warning", sprintf(_("You're about to delete the glpi attachment component '%s'."), $attach[$str]['name']));
69 return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
70 }
72 /* Start editing entry */
73 if((preg_match("/^editAttach_/",$name))&&($only_once)){
74 $only_once = false;
75 $str = preg_replace("/^editAttach_/","",$name);
76 $str = base64_decode(preg_replace("/_.*$/","",$str));
78 /* Check if we have an attachment with given ID */
79 foreach($attach as $att ){
80 if($att['ID'] == $str ){
82 /* Entry was found, show dialog */
83 $this->edit = true;
84 $this->entry = $att;
86 foreach($att as $name => $value){
87 $this->$name = $value;
88 }
89 }
90 }
91 }
92 }
94 if((isset($_POST['delete_glpi_confirm']))&&(isset($attach[$this->delAttach]))){
95 if($this->parent->handle->is_attachmentUsed($this->delAttach)){
96 $tmp = $this->parent->handle->is_attachmentUsed($this->delAttach);
98 $names = "";
99 foreach($tmp as $name){
100 $names .= ", ".$name;
101 }
102 $names = preg_replace("/^, /","",$names);
103 $names = trim($names);
104 if(count($tmp) == 3){
105 $names .= " ...";
106 }
107 print_red(sprintf(_("You can't delete this attachment, it is still in use by these system(s) '%s'"),$names));
109 }else{
110 $this->parent->handle->deleteAttachment($this->delAttach);
111 @unlink(CONFIG_DIR."/glpi/".$this->filename);
112 $attach = $this->parent->handle->getAttachments();
113 }
114 }
116 /* Someone tries to upload a file */
117 if(($this->edit == true)&&(isset($_POST['upload']))){
118 if(!isset($_FILES['filename'])){
119 print_red(_("There is no valid file uploaded."));
120 }else{
121 $FILE = $_FILES['filename'];
122 if(!isset($FILE['name'])){
123 print_red(_("There is no valid file uploaded."));
124 }else{
125 if($FILE['error']!=0) {
126 print_red(_("Upload wasn't successfull."));
127 }else{
128 if(!is_dir(CONFIG_DIR."/glpi/")){
129 print_red(sprintf(_("Missing directory '%s/glpi/' to store glpi uploads."),CONFIG_DIR));
130 }else{
131 $filen = CONFIG_DIR."/glpi/".$FILE['name'];
132 if(file_exists($filen)){
133 print_red(_("There is already a file with the same name uploaded."));
134 }else{
135 $fh = fopen($filen,"w");
136 if(!$fh){
137 print_red(sprintf(_("Can't create file '%s'."),$filen));
138 }else{
139 $str = file_get_contents($FILE['tmp_name']);
140 fwrite($fh,$str,strlen($str));
141 fclose($fh);
142 $this->mime = $FILE['type'];
143 $this->filename = $FILE['name'];
144 }
145 } // File already exists
146 }
147 } // Check if any error occured
148 } // check if valid filename was uploaded
149 } // ende check if file was uploaded
150 }// upload post
152 /* save attachment*/
153 if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
154 if(count($this->check())==0){
155 $this->date_mod = date("Y-m-d H:i:s");
156 $this->save_entry();
157 $this->edit= false;
158 $this->entry = array();
159 $attach = $this->parent->handle->getAttachments();
160 }else{
161 foreach($this->check() as $msg){
162 print_red($msg);
163 }
164 }
165 }
167 /* Abort editing/adding attachment*/
168 if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
169 $this->edit = false;
170 $this->entry = array();
171 }
173 /* If edit == true, we have to show a dialog to edit or add an attach
174 */
175 if($this->edit == true){
176 foreach($this->attributes as $attr){
177 $smarty->assign($attr,htmlentities(utf8_decode($this->$attr)));
178 }
179 if(!empty($this->filename)){
180 if(is_readable(CONFIG_DIR."/glpi/".$this->filename)){
181 $status =_("File is available.");
182 }else{
183 $status =_("File is not readable, possibly the file is missing.");
184 }
185 }else{
186 $status = _("Currently no file uploaded.");
187 }
188 $smarty->assign("status",$status);
189 return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
190 }
192 /* Create list with checkboxes to select / deselect some attachents */
193 $divlist = new divlist("Attachment");
194 $divlist->SetHeader(array(
195 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
196 array("string" => _("Name")),
197 array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
198 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
200 $divlist->SetSummary(_("This table displays all available attachments."));
201 $divlist->SetEntriesPerPage(0);
203 $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
204 $editdel.= "<input type='image' name='delAttach_%s' src='images/edittrash.png'>";
206 /* Add all given attachments to divlist
207 */
208 foreach($attach as $entry){
210 /* Create display name*/
211 if((empty($entry['name']))||(empty($entry['comment']))){
213 /* In glpi it is possible to add entries without name
214 so i've added this block
215 */
216 if(empty($entry['name'])){
217 $str1 = "<i>"._("empty")."</i>";
218 }else{
219 $str1 = $entry['name'];
220 }
222 if(!empty($entry['comment'])){
223 $str1.= " [".$entry['comment']."]";
224 }
225 }else{
226 /* Both attributes given */
227 $str1 = $entry['name']." [".$entry['comment']."]";
228 }
230 $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
231 $str2 = $entry['mime']." ";
233 $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
234 "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
236 if(in_array($entry['ID'],$this->Selected)){
237 $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
238 }else {
239 $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
240 }
241 $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
242 $divlist->AddEntry(array(
243 array("string" => $chkbox,
244 "attach" => "style='text-align:center;width:20px;'"),
245 array("string"=> $str1),
246 array("string"=> $str2,"attach"=>"style='width:200px;'"),
247 array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
248 ));
250 }
252 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
253 " <input class='center' type='image'
254 src='images/fai_new_template.png' align='middle' title='"._("Create new attachment")."'
255 name='new_attach' alt='"._("New Attachment"). "'> ".
256 "</div>";
258 $smarty->assign("attachments", $divlist->DrawList());
259 $smarty->assign("attachmenthead", $listhead);
260 $smarty->assign("search_image", get_template_path('images/search.png'));
261 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
262 $smarty->assign("tree_image", get_template_path('images/tree.png'));
263 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
264 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
265 $smarty->assign("apply", apply_filter());
266 $smarty->assign("alphabet", generate_alphabet());
267 $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
269 $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
270 return($display);
271 }
273 function save()
274 {
275 return($this->Selected);
276 }
278 /* Adds new or saves edited entries */
279 function save_entry()
280 {
281 if($this->edit){
282 $tmp = array();
283 foreach($this->attributes as $attr){
284 $tmp[$attr] = $this->$attr;
285 }
286 $id = -1;
287 if(isset($this->entry['ID'])){
288 $id = $this->entry['ID'];
289 }
290 $this->parent->handle->saveAttachments($tmp,$id);
291 }
292 }
294 function save_object()
295 {
296 foreach($this->attributes as $attr){
297 if(isset($_POST[$attr])){
298 $this->$attr = $_POST[$attr];
299 }
300 }
302 /* Checkboxes are only posted, if they are checked
303 * To check if it was deselected, we check if wasOnPage
304 * was posted with given name, so we can deselect this entry
305 */
307 foreach($_POST as $name => $value){
308 if(preg_match("/wasOnPage_/",$name)){
309 $id=preg_replace("/wasOnPage_/","",$name);
310 if(isset($_POST["useMe_".$id])){
311 $this->Selected[$id]=$id;
312 }else{
313 if(isset($this->Selected[$id])){
314 unset($this->Selected[$id]);
315 }
316 }
317 }
318 }
319 }
321 /* Simple check */
322 function check()
323 {
324 /* Call common method to give check the hook */
325 $message= plugin::check();
327 if($this->edit){
329 /* check if given name is already in use */
330 $att = $this->parent->handle->getAttachments();
331 $ok = true;
332 $this->name=trim($this->name);
334 foreach($att as $val){
335 if(!isset($this->entry['ID'])){
336 if($val['name'] == $this->name){
337 $ok = false;
338 }
339 }else{
340 if(($val['name'] == $this->name)&&($this->entry['ID'] != $val['ID'])){
341 $ok = false;
342 }
343 }
344 }
345 if(!$ok){
346 $message[] = _("This name is already in use.");
347 }
348 if(empty($this->name)){
349 $message[] = _("Please specify a valid name for this attachment.");
350 }
351 }
352 return($message);
353 }
355 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
356 ?>