Fixed "Use of undefined constant" warnings
[squasher.git] / squasher.php
1 <?php
2 if (!$_SESSION['creds']['validated'] && !isset($_GET['f']))exit();
3
4 require_once("squasher.class.php");
5
6 $squashweb = new squashweb();
7
8 $squashweb->update_history();
9
10 //set root directory
11 $basedir = "../uploads";
12 if (isset($_GET['path']) && $_GET['path']!=NULL && strlen($_GET['path'])!=1){
13         $subf = $_GET['path'];
14         $path = $basedir . $subf;
15 }
16 else{
17         $path = $basedir;
18 }
19 $squashweb->set_root($basedir);
20
21 if ((isset($_GET['f']) && $_GET['f']!=NULL) || (isset($_GET['d']) && $_GET['d']!=NULL) || $_GET['show_all']==true || $_GET['tools']=='hide' || $_GET['tools']=='unhide' || $_GET['tools']=='delete'){
22         $getdeepfiles=true;
23         $populate=false;
24 }else{
25         $getdeepfiles=false;
26         $populate=true;
27 }
28
29 //update folder rights if form is submitted
30 if($submitted['edited_user'] > 0 && $submitted['formtype'] == 'folderrights') $squashweb->update_rights($submitted,$_SESSION['creds']['user_level']);
31
32 //set folder rights
33 if (isset($_GET['f']) && $_GET['f']!=NULL){
34         $squashweb->give_rights(2);
35 }else{
36         $squashweb->give_rights($_SESSION['creds']['user_id']);
37 }
38
39 if (isset($_GET['f']) && $_GET['f']!=NULL){
40         $f_q = "SELECT * FROM file_hash WHERE md5_hash = '".$_GET['f']."'";
41         $f_r = mysql_query($f_q);
42         $f_o = mysql_fetch_object($f_r);
43
44         //log downloads
45         @mysql_query("INSERT INTO log (hash,file,action,user_id,user_name,ip,date) VALUES ('".$_GET['f']."','{$f_o->file}','download','".$_SESSION['creds']['user_id']."','".$_SESSION['creds']['user_name']."','".$_SERVER['REMOTE_ADDR']."',NOW())");
46
47         $_hpath_arr=explode("/",$f_o->file);
48         $file=array_pop($_hpath_arr); //Remove filename
49         $path=implode("/",$_hpath_arr);
50         $squashweb->read_single_file($path,$file);
51         #$squashweb->read_directory($path,false,true,false,false);
52 }else{
53         //parse folders for readable files
54         $squashweb->read_directory($path,true,true,$getdeepfiles,$populate);
55 }
56
57 //check if a file is requested
58 if (isset($_GET['f']) && $_GET['f']!=NULL){
59         //get the config of requested file
60         $request = $squashweb->get_config($_GET['f']);
61         
62         //------------------------------------
63         //-check-file-and-request-type--start-
64         //------------------------------------
65         if (strpos($request['mime'],'ideo') && isset($_GET['d'])){ //embed video
66                 $squashweb->embed_video($_GET['f']);    
67         }
68         elseif (strpos($request['mime'],'ideo') && isset($_GET['x'])){  //show stream playlist
69                 $squashweb->create_playlist($_GET['f'],$_GET['x'],$request[2]);
70         }
71         else{   //show requested file
72         #       header('Cache-control: private');
73                 header('Content-Disposition: attachment; filename="'.$request[2].'"');
74                 header("Content-Type: ".$request['mime']);
75                 if($request[3])header('Content-Length: '.$request[3]);
76                 #@ob_flush();
77                 @flush();
78                 $squashweb->print_files($request['path'],$request[2]);
79         }
80         //---------------------------------
81         //-check-file-and-request-type-end-
82         //---------------------------------
83
84 }elseif ($tools=="hide" && isset($_GET['h'])){
85         if ($_SESSION['creds']['user_level'] > 99){
86                 $request = $squashweb->get_config($_GET['h']);
87                 $handle = fopen($request['path'].'/'.$request[2].'.hidden', 'x');
88                 fwrite($handle, 'hidden by '.$_SESSION['creds']['user_name']);
89                 fclose($handle);
90                 $path=substr($request['path'],strlen($basedir));
91                 header( 'Location: '.$_SERVER['PHP_SELF'].'?path='.$path) ;
92         }
93 }elseif ($tools=="unhide" && isset($_GET['h'])){
94         if ($_SESSION['creds']['user_level'] > 99){
95                 $request = $squashweb->get_config($_GET['h']);
96                 @unlink($request['path'].'/'.$request[2].'.hidden');
97                 $path=substr($request['path'],strlen($basedir));
98                 header( 'Location: '.$_SERVER['PHP_SELF'].'?path='.$path) ;
99         }
100 }elseif ($tools=="delete" && isset($_GET['h'])){
101         if ($_SESSION['creds']['user_level'] > 99){
102                 if ($_GET['h'] == "multiple"){
103                         $h_post = $_POST['h'];
104                         foreach ($h_post as $h_key => $h){
105                                 $request = $squashweb->get_config($h);
106                                 $squashweb->delete_file($h,$_SESSION['creds']);
107                         }
108                 }else{
109                         $h = $_GET['h'];
110                         $request = $squashweb->get_config($h);
111                         $squashweb->delete_file($h,$_SESSION['creds']);
112                 }
113                 $path=substr($request['path'],strlen($basedir));
114                 header( 'Location: '.$_SERVER['PHP_SELF'].'?path='.$path);
115         }
116 }elseif ($_GET['tools']=='access' && $_SESSION['creds']['user_level'] > 100){
117
118 require_once("smarty/Smarty.class.php");
119
120 $smarty = new Smarty;
121
122         $smarty->assign('user_level',$_SESSION['creds']['user_level']);
123         $smarty->assign('user_name',$_SESSION['creds']['user_name']);
124         $smarty->assign('users',$squashweb->get_users($_SESSION['creds']['user_level']));
125
126         $smarty->assign('edited_user',$_GET['user']);
127         
128         $tree = $squashweb->show_rights_tree($path,0,$_GET['user']);
129         
130         $smarty->assign('style',$tree['style']);
131         $smarty->assign('layout',$tree['layout']);
132         
133         $smarty->assign('debug',$vars['debug']);
134         $smarty->assign('folderrights',$squashweb->folderrights());
135         $smarty->assign('userrights',$squashweb->userrights());
136         $smarty->display('admin.tpl');
137         
138 }elseif ($_GET['tools']=='users' && $_SESSION['creds']['user_level'] > 100){
139
140 if ($submitted['type']=="update"){$squashweb->update_users($submitted);}
141 if ($submitted['type']=="disable"){$squashweb->disable_users($submitted);}
142 if ($submitted['type']=="delete"){$squashweb->remove_users($submitted);}
143 if ($submitted['type']=="new"){$squashweb->insert_users($submitted,$_SESSION['creds']['user_level']);}
144 require_once("smarty/Smarty.class.php");
145
146 $smarty = new Smarty;
147
148         $smarty->assign('user_level',$_SESSION['creds']['user_level']);
149         $smarty->assign('user_name',$_SESSION['creds']['user_name']);
150         $smarty->assign('users',$squashweb->get_users($_SESSION['creds']['user_level']));
151
152         if ($_SESSION['creds']['user_level'] > 199){
153         $smarty->assign('user_levels', array(155 => 'admin' , 55 => 'user' ) );
154         }else{
155         $smarty->assign('user_levels', array(55 => 'user' ) );
156         }
157         $smarty->assign('debug',$vars['debug']);
158
159         $smarty->display('udmin.tpl');
160
161 }elseif ($_GET['tools']=='logs' && $_SESSION['creds']['user_level'] > 100){
162         $logtype=($_GET['logtype']) ? $_GET['logtype'] : 'all';
163         require_once("smarty/Smarty.class.php");
164
165         $smarty = new Smarty;
166
167         $smarty->assign('user_level',$_SESSION['creds']['user_level']);
168         $smarty->assign('user_name',$_SESSION['creds']['user_name']);
169         $smarty->assign('logtype',$logtype);
170         $smarty->assign('logs',$squashweb->get_logs($logtype));
171
172         $smarty->display('logs.tpl');
173
174 }else{  //show overview
175
176 require_once("smarty/Smarty.class.php");
177
178 $smarty = new Smarty;
179
180         /***
181         *       $config:: array
182         *       [0]     ->      versioncode
183         *       [1]     ->      date&time
184         *       [2]     ->      filename
185         *       [3]     ->      filesize
186         *       [4]     ->      chunksize
187         *       [5]     ->      chunkcount
188         *       [6]     ->      CRC32 checksum
189         ***/
190         
191 $configs_num = $squashweb->get_configs();
192
193 $configs_sorted = named_records_sort($configs_num,'lastchange',true);
194
195 foreach ($configs_sorted AS $key => $value){
196         $configs[$key]['squashed'] = $value['squashed'];
197         $configs[$key]['version'] = $value[0];
198         //$configs[$key]['date'] = $value[1]; //removed because user local creation date is irrelevant
199         $configs[$key]['name'] = $value[2];
200         $configs[$key]['size'] = $value[3];
201         $configs[$key]['chunk_size'] = $value[4];
202         $configs[$key]['chunks'] = $value[5];
203         $configs[$key]['crc'] = $value[6];
204         $paths[substr($value['path'], strlen($basedir))]=array_pop(explode('/',$value['path']));
205         (strpos($value['mime'],'ideo')) ? $embedable=true : $embedable=false;   
206         (@array_sum($value['stats']) == $value[5]) ? $finished=true : $finished=false;
207
208         $configs[$key]['embedable'] = $embedable;
209         $configs[$key]['finished'] = $finished;
210         $configs[$key]['mime'] = $value['mime'];
211         $configs[$key]['status'] = $value['status'];
212         $configs[$key]['hidden'] = $value['hidden'];
213         $configs[$key]['chunks_finished'] = @array_sum($value['stats']);
214         if($finished){
215                 $configs[$key]['chunks_partial'] = $configs[$key]['chunks_finished'];
216         }else{
217                 $continue=true;
218                 foreach($value['stats'] AS $sk => $sv){
219                         if($continue){
220                                 $configs[$key]['chunks_partial']=$configs[$key]['chunks_partial']+$sv;
221                                 if($sv != '1.00')$continue=false;
222                         }
223                 }
224         }
225         $configs[$key]['date'] = date('d.m.y - H:i:s - T',$value['added']);
226         $configs[$key]['lastchange'] = date('d.m.y - H:i:s - T',$value['lastchange']);
227 }
228
229
230 //set base folders
231         $basepath['/']='&nbsp;top&nbsp;';
232 foreach (explode('/',$subf) AS $key => $value){
233         if ($value != ''){
234                 $bpath.='/'.$value;
235                 $basepath[$bpath]=$value;
236         }
237 }
238 $smarty->assign('debug',$vars['debug']);
239 $smarty->assign('user_level',$_SESSION['creds']['user_level']);
240 $smarty->assign('user_name',$_SESSION['creds']['user_name']);
241 $smarty->assign('folderrights',$squashweb->folderrights());
242
243 $smarty->assign('squashed',$configs);
244 $smarty->assign('paths',$paths);
245 $smarty->assign('base',$basepath);
246 $smarty->assign('currentfolder',$subf);
247 $subs = $squashweb->subfolders();
248 asort($subs);
249 $smarty->assign('subfolders',$subs);
250
251 //if($vars[debug])print_r($configs);
252 if(!$vars['newtpl'])$smarty->display('index.tpl'); //Display normal template
253 if($vars['newtpl'])$smarty->display('wip_massdelete.tpl'); //Display Work In Progress template
254
255 }
256 ?>