eca6b9d019505173343456d84b0dbf2078f48227
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(!session::is_set('GlpiAttachmentFilter')){
27 session::set('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",msgPool::deleteInfo($attach[$str]['name'],_("attachment")));
69 return($smarty->fetch(get_template_path('remove_glpi.tpl',TRUE,dirname(__FILE__))));
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 occurred
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,dirname(__FILE__))));
190 }
192 /* Create list with checkboxes to select / deselect some attachents */
193 $divlist = new divlist("Attachment");
194 $divlist->SetPluginMode();
195 $divlist->SetHeader(array(
196 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
197 array("string" => _("Name")),
198 array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
199 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
201 $divlist->SetSummary(_("This table displays all available attachments."));
202 $divlist->SetEntriesPerPage(0);
204 $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
205 $editdel.= "<input type='image' name='delAttach_%s' src='images/edittrash.png'>";
207 /* Add all given attachments to divlist
208 */
209 foreach($attach as $entry){
211 /* Create display name*/
212 if((empty($entry['name']))||(empty($entry['comment']))){
214 /* In glpi it is possible to add entries without name
215 so i've added this block
216 */
217 if(empty($entry['name'])){
218 $str1 = "<i>"._("empty")."</i>";
219 }else{
220 $str1 = $entry['name'];
221 }
223 if(!empty($entry['comment'])){
224 $str1.= " [".$entry['comment']."]";
225 }
226 }else{
227 /* Both attributes given */
228 $str1 = $entry['name']." [".$entry['comment']."]";
229 }
231 $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
232 $str2 = $entry['mime']." ";
234 $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
235 "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
237 if(in_array($entry['ID'],$this->Selected)){
238 $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
239 }else {
240 $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
241 }
242 $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
243 $divlist->AddEntry(array(
244 array("string" => $chkbox,
245 "attach" => "style='text-align:center;width:20px;'"),
246 array("string"=> $str1),
247 array("string"=> $str2,"attach"=>"style='width:200px;'"),
248 array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
249 ));
251 }
253 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
254 " <input class='center' type='image'
255 src='images/fai_new_template.png' align='middle' title='"._("Create new attachment")."'
256 name='new_attach' alt='"._("New Attachment"). "'> ".
257 "</div>";
259 $GlpiAttachmentFilter = session::get('GlpiAttachmentFilter');
261 $smarty->assign("attachments", $divlist->DrawList());
262 $smarty->assign("attachmenthead", $listhead);
263 $smarty->assign("search_image", get_template_path('images/search.png'));
264 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
265 $smarty->assign("tree_image", get_template_path('images/tree.png'));
266 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
267 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
268 $smarty->assign("apply", apply_filter());
269 $smarty->assign("alphabet", generate_alphabet());
270 $smarty->assign("attachment_regex", $GlpiAttachmentFilter['filter']);
272 $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl',TRUE,dirname(__FILE__)));
273 return($display);
274 }
276 function save()
277 {
278 return($this->Selected);
279 }
281 /* Adds new or saves edited entries */
282 function save_entry()
283 {
284 if($this->edit){
285 $tmp = array();
286 foreach($this->attributes as $attr){
287 $tmp[$attr] = $this->$attr;
288 }
289 $id = -1;
290 if(isset($this->entry['ID'])){
291 $id = $this->entry['ID'];
292 }
293 $this->parent->handle->saveAttachments($tmp,$id);
294 }
295 }
297 function save_object()
298 {
299 foreach($this->attributes as $attr){
300 if(isset($_POST[$attr])){
301 $this->$attr = $_POST[$attr];
302 }
303 }
305 /* Checkboxes are only posted, if they are checked
306 * To check if it was deselected, we check if wasOnPage
307 * was posted with given name, so we can deselect this entry
308 */
310 foreach($_POST as $name => $value){
311 if(preg_match("/wasOnPage_/",$name)){
312 $id=preg_replace("/wasOnPage_/","",$name);
313 if(isset($_POST["useMe_".$id])){
314 $this->Selected[$id]=$id;
315 }else{
316 if(isset($this->Selected[$id])){
317 unset($this->Selected[$id]);
318 }
319 }
320 }
321 }
322 }
324 /* Simple check */
325 function check()
326 {
327 /* Call common method to give check the hook */
328 $message= plugin::check();
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 ?>