1feea3d98e5f95932654c7288fe2790ce0df834d
[acontent.git] / docs / file_manager / delete.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 define('TR_INCLUDE_PATH', '../include/');
14 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
15 require_once(TR_INCLUDE_PATH.'classes/DAO/PrimaryResourcesDAO.class.php');
16 require_once(TR_INCLUDE_PATH.'classes/DAO/PrimaryResourcesTypesDAO.class.php');
17 require_once(TR_INCLUDE_PATH.'classes/DAO/SecondaryResourcesDAO.class.php');
18 require_once(TR_INCLUDE_PATH.'classes/DAO/SecondaryResourcesTypesDAO.class.php');
19 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
20
21 global $_course_id;
22 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
23
24 $current_path = TR_CONTENT_DIR.$_course_id.'/';
25
26 $popup = $_REQUEST['popup'];
27 $framed = $_REQUEST['framed'];
28
29 if (isset($_POST['submit_no'])) {
30         $msg->addFeedback('CANCELLED');
31         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'cp='.$_POST['cp'].SEP.'cid='.$_POST['cid'].SEP.'pid='.$_POST['pid'].SEP.'a_type='.$_POST['a_type'].SEP.'_course_id='.$_course_id);
32         exit;
33 }
34
35 if (isset($_POST['submit_yes'])) {
36         /* delete files and directories */
37         /* delete the file  */
38         $pathext = $_POST['pathext'];
39         if (isset($_POST['listoffiles']))  {
40                 $checkbox = explode(',',$_POST['listoffiles']);
41                 $count = count($checkbox);
42                 $result=true;
43                 for ($i=0; $i<$count; $i++) {
44                         $filename=$checkbox[$i];
45
46                         if (FileUtility::course_realpath($current_path . $pathext . $filename) == FALSE) {
47                                 $msg->addError('FILE_NOT_DELETED');
48                                 $result=false;
49                                 break;
50                         } else if (!(@unlink($current_path.$pathext.$filename))) {
51                                 $msg->addError('FILE_NOT_DELETED');
52                                 $result=false;
53                                 break;
54                         }                       
55                 }
56                 if ($result)
57                 { 
58                         // delete according definition of primary resources and alternatives for adapted content
59                         $filename = '../'.$pathext.$filename;
60                         
61                         // 1. delete secondary resources types
62                         $secondaryResourcesTypesDAO = new SecondaryResourcesTypesDAO();
63                         $secondaryResourcesTypesDAO->DeleteByResourceName($filename);
64                         
65                         // 2. delete secondary resources 
66                         $secondaryResourcesDAO = new SecondaryResourcesDAO();
67                         $secondaryResourcesDAO->DeleteByResourceName($filename);
68                         
69                         // 3. delete primary resources types
70                         $primaryResourcesTypesDAO = new PrimaryResourcesTypesDAO();
71                         $primaryResourcesTypesDAO->DeleteByResourceName($filename);
72                         
73                         // 4. delete primary resources 
74                         $primaryResourcesDAO = new PrimaryResourcesDAO();
75                         $primaryResourcesDAO->DeleteByResourceName($filename);
76                         
77                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
78                 }
79         }
80         /* delete directory */
81         if (isset($_POST['listofdirs'])) {
82                                 
83                 $checkbox = explode(',',$_POST['listofdirs']);
84                 $count = count($checkbox);
85                 $result=true;
86                 for ($i=0; $i<$count; $i++) {
87                         $filename=$checkbox[$i];
88                                 
89                         if (strpos($filename, '..') !== false) {
90                                 $msg->addError('UNKNOWN');
91                                 $result=false;
92                                 header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'cp='.$_POST['cp'].SEP.'cid='.$_POST['cid'].SEP.'pid='.$_POST['pid'].SEP.'a_type='.$_POST['a_type'].SEP.'_course_id='.$_course_id);
93                                 exit;
94                         } else if (!is_dir($current_path.$pathext.$filename)) {
95                                 $msg->addError(array('DIR_NOT_DELETED',$filename));
96                                 $result=false;
97                                 header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'cp='.$_POST['cp'].SEP.'cid='.$_POST['cid'].SEP.'pid='.$_POST['pid'].SEP.'a_type='.$_POST['a_type'].SEP.'_course_id='.$_course_id);
98                                 exit;
99                         } else if (!($result = FileUtility::clr_dir($current_path.$pathext.$filename))) { 
100                                 $msg->addError('DIR_NO_PERMISSION');
101                                 $result=false;
102                                 header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'cp='.$_POST['cp'].SEP.'cid='.$_POST['cid'].SEP.'pid='.$_POST['pid'].SEP.'a_type='.$_POST['a_type'].SEP.'_course_id='.$_course_id);
103                                 exit;
104                         } 
105                 }
106                 if ($result)
107                         $msg->addFeedback('DIR_DELETED');
108         }
109         
110         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'cp='.$_POST['cp'].SEP.'cid='.$_POST['cid'].SEP.'pid='.$_POST['pid'].SEP.'a_type='.$_POST['a_type'].SEP.'_course_id='.$_course_id);
111         exit;
112 }
113
114         require(TR_INCLUDE_PATH.'header.inc.php');
115         // find the files and directories to be deleted 
116         $total_list = explode(',', $_GET['list']);
117         $pathext = $_GET['pathext']; 
118         $popup   = $_GET['popup'];
119         $framed  = $_GET['framed'];
120         $cp = $_GET['cp'];
121         $cid = $_GET['cid'];
122         $pid = $_GET['pid'];
123         $a_type = $_GET['a_type'];
124         
125         $count = count($total_list);
126         $countd = 0;
127         $countf = 0;
128         
129         foreach ($total_list as $list_item) {
130                 if (is_dir($current_path.$pathext.$list_item)) {
131                         $_dirs[$countd]  = $list_item;
132                         $countd++;
133                 } else {
134                         $_files[$countf] = $list_item;
135                         $countf++;
136                 }
137         }
138                                 
139         $hidden_vars['pathext'] = $pathext;
140         $hidden_vars['popup']   = $popup;
141         $hidden_vars['framed']  = $framed;
142         $hidden_vars['cp']  = $cp;
143         $hidden_vars['cid']  = $cid;
144         $hidden_vars['pid']  = $pid;
145         $hidden_vars['a_type']  = $a_type;
146         $hidden_vars['_course_id']  = $_course_id;
147         
148         if (isset($_files)) {
149                 $list_of_files = implode(',', $_files);
150                 $hidden_vars['listoffiles'] = $list_of_files;
151
152                 foreach ($_files as $file) {
153                         $file_list_to_print .= '<li>'.$file.'</li>';
154                 }
155                 $msg->addConfirm(array('FILE_DELETE', $file_list_to_print), $hidden_vars);
156         }
157                 
158         if (isset($_dirs)) {
159                 $list_of_dirs = implode(',', $_dirs);
160                 $hidden_vars['listofdirs'] = $list_of_dirs;
161
162                 foreach ($_dirs as $dir) {
163                         $dir_list_to_print .= '<li>'.$dir.'</li>';
164                 }
165
166                 $msg->addConfirm(array('DIR_DELETE',$dir_list_to_print), $hidden_vars);
167         }
168
169         $msg->printConfirm();
170         
171         require(TR_INCLUDE_PATH.'footer.inc.php');
172 ?>