File: hddelcron.php

package info (click to toggle)
owl-dms 0.90-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,292 kB
  • ctags: 10,919
  • sloc: php: 48,457; sql: 3,603; sh: 363; perl: 204; makefile: 73
file content (263 lines) | stat: -rw-r--r-- 9,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
 * admin/hddelcron.php
 *
 * Written by: B0zz
 *
 * Copyright (c) 1999-2003 The Owl Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 */



// *******************************************************************************************
// This script is NOT desiged to work with
// DB only stored files ie owl_use_fs = false
// 
// This script will NOT work with Postgresql
//
// You can run this from your browser on demand or
// you  can setup a cron job to run this script
// at your leisure.
//
// This script was designer to replace the LookAtHD Delete
// Feature. As at some very large site, the LookAtHD Delete
// Feature can degrade the webservers performance. 
//
// Here is an examble of a cron job that runs every half hour.
//
// 59,29 * * * * lynx -dump http://localhost/intranet/admin/tools/hddelcron.php?type=both > /dev/null
//
// This would clean up all delete files and folders from that database
// that have been deleted from the file system.
//
// Hope this is usefull to someone.
//
//	Usage: hddelcron.php?type=ActionType&verbose=DetailLevel
//
//	DetailLevel
//	1 Display Deleted Items
//	0 Silent
//
//	Action Type
// 	file	To delete files from the Database that have been removed from the File System
//	folder 	To delete folders from the Database that have been removed from the File System
//	both 	To delete files and Folders from the Database that have been removed from the File System
//
//
// 	PLEASE BACKUP YOUR DATABASE THIS SCRIPT HAS THE POTENTIAL TO
//   	DELETE ALL FILES AND ALL FOLDER ENTRIES IN YOUR DATABASE.
//
//	mysqldump -u username -p > mydbdump.sql
//
// *******************************************************************************************


// **************************************
// Globals as per config/owl.php
// **************************************

$default->owl_files_table       	= "files";
$default->owl_folders_table     	= "folders";
$default->owl_monitored_file_table  	= "monitored_file";
$default->owl_monitored_folder_table  	= "monitored_folder";
$default->owl_comment_table  		= "comments";

$default->owl_FileDir           =  "/var/www/html/intranet";

$default->owl_db_user           = "someuser";
$default->owl_db_pass           = "";
$default->owl_db_host           = "localhost";
$default->owl_db_name           = "intranet";


// **************************************
// Register Globals = Off
// **************************************

if (substr(phpversion(),0,5) >= "4.1.0")
        import_request_variables('pgc');
 else {
        if (!EMPTY($_POST)) {
                extract($_POST);
        } else {
                extract($HTTP_POST_VARS);
        }
        if (!EMPTY($_GET)) {
                extract($_GET);
        } else {
                extract($HTTP_GET_VARS);
        }
        if (!EMPTY($_FILE)) {
                extract($_FILE);
        } else {
                extract($HTTP_POST_FILES);
        }
}

if(!file_exists($default->owl_FileDir)) {
	print("<font size=4 color=red>SEVERE ERROR:</font><font size=4>  \$default->owl_FileDir was not found '$default->owl_FileDir'<br /><br /> Script Terminating</font>");
	exit;
}

	


// **************************************
// MAIN BEGIN
// **************************************
if(isset($type)) {
	if($type == "file") {
		$somethingwasdeleted = DeleteDBFolderzNotInDB($default->owl_files_table,$verbose,$type);
	} elseif ($type == "folder") {
		$somethingwasdeleted = DeleteDBFolderzNotInDB($default->owl_folders_table,$verbose,$type);
	} elseif ($type == "both") {
		$somethingwasdeleted = DeleteDBFolderzNotInDB($default->owl_files_table,$verbose,$type);
		$somethingwasdeleted = DeleteDBFolderzNotInDB($default->owl_folders_table,$verbose,$type);
	} else {
		pringUsage(); 
	}
} else {
	pringUsage(); 
}

// **************************************
// MAIN END
// **************************************


// **************************************
// Functions Section BEGIN
// **************************************

// **************************************
// Main function that does the work
// **************************************

function DeleteDBFolderzNotInDB($table, $verbose,$type) {
	global $default;
	if($verbose == 1) {
		if($default->owl_files_table == $table) {
			print("<h2>Begin File Script...</h2>");
		}
		if($default->owl_folders_table == $table) {
			print("<h2>Begin Folder Script...</h2>");
		}
	}
  	$somethingwasdeleted = false;

	$dblink = mysql_connect("$default->owl_db_host","$default->owl_db_user","$default->owl_db_pass") or die ("could not connect") ;

  	if ($table == $default->owl_files_table) {
  		$query = "select id,parent,filename from $table where url <> 1";
	} else {
  		$query = "select id,name from $table order by parent desc";
	}

	$get = mysql_db_query($default->owl_db_name,$query,$dblink) or die ("GET QUERY FAILED");

	while($getrow = mysql_fetch_row ($get)) {
		if ($table == $default->owl_files_table) {
      			$dbfolder = $default->owl_FileDir . "/" . get_dirpath($getrow[1]) . "/" . $getrow[2];
   		} else {
      			$dbfolder = $default->owl_FileDir . "/" . get_dirpath($getrow[0]);
   		}
   		if(!file_exists($dbfolder)) {
     			$delid = $getrow[0];
			$db_del_link = mysql_connect("$default->owl_db_host","$default->owl_db_user","$default->owl_db_pass") or die ("could not connect") ;
  			$delquery = "delete from $table where id = '$delid'";
			$del = mysql_db_query($default->owl_db_name,$delquery,$db_del_link) or die ("DELETE QUERY FAILED");
			 if ($table == $default->owl_files_table) {
                                // Clean up all monitored files with that id
                                $delquery = "DELETE from $default->owl_monitored_file_table where fid = '$delid'";
				$del = mysql_db_query($default->owl_db_name,$delquery,$db_del_link) or die ("DELETE MONITORED FILE QUERY FAILED");
                                // Clean up all comments with this file 
                                $delquery = "DELETE from $default->owl_comment_table where fid = '$delid'";
				$del = mysql_db_query($default->owl_db_name,$delquery,$db_del_link) or die ("DELETE COMMENT QUERY FAILED");
                        } else {
                                $delquery = "DELETE from $default->owl_monitored_folder_table where fid = '$delid'";
				$del = mysql_db_query($default->owl_db_name,$delquery,$db_del_link) or die ("DELETE MONITORED FOLDERQUERY FAILED");
                        }


     			$somethingwasdeleted = true;
			if($verbose == 1) {
					
				if($default->owl_files_table == $table) {
					print("Deleted file '$dbfolder' from the database<br />");
				}
				if($default->owl_folders_table == $table) {
					print("Deleted file '$dbfolder' from the database<br />");
				}
			}
   		}
  	}
    	mysql_free_result($get);
	if($verbose == 1) {
		if($default->owl_files_table == $table) {
			print("<h2>End File Script...</h2>");
		}
		if($default->owl_folders_table == $table) {
			print("<h2>End Folder Script...</h2>");
		}
	}

  return $somethingwasdeleted;
}


// **************************************
// Function that returns the path 
// of the file or folder
// **************************************

function get_dirpath($parent) {
        global $default;
        $name = fid_to_name($parent);
        $navbar = "$name";
        $new = $parent; 
        while ($new != "1") {
        	$dblink = mysql_connect("$default->owl_db_host","$default->owl_db_user","$default->owl_db_pass") or die ("could not connect") ;
		$query = "select parent from $default->owl_folders_table where id = '$new'";
        	$getparent = mysql_db_query($default->owl_db_name,$query,$dblink) or die ("QUERY FAILED");
		$row = mysql_fetch_row ($getparent);
    		mysql_free_result($getparent);
    		mysql_close($dblink);
                $newparentid = $row[0];
                if($newparentid == "") break;
                $name = fid_to_name($newparentid);
                $navbar = "$name/" . $navbar;
                $new = $newparentid;
        }
        return $navbar;
}

function fid_to_name($parent) {
        global $default;

        $dblink = mysql_connect("$default->owl_db_host","$default->owl_db_user","$default->owl_db_pass") or die ("could not connect") ;
	$query = "select name from $default->owl_folders_table where id = '$parent'";
        $getname = mysql_db_query($default->owl_db_name,$query,$dblink) or die ("QUERY FAILED");
	$row = mysql_fetch_row ($getname);
    	mysql_free_result($getname);
    	mysql_close($dblink);
        return $row[0];;
}

function pringUsage () {
	print("<B>Usage:<B> hddelcron.php?type=ActionType&verbose=DetailLevel<br /><br />");
	print("<B>DetailLevel<B><br />");
	print("<TABLE>\n");
	print("<TR><TD width=90 align=left><B><font color=red>1</font></B></TD><TD>Display Deleted Items</TD></TR>");
	print("<TR><TD width=90 align=left><B><font color=red>0</font></B></TD><TD>Silent</TD></TR>");
	print("</TABLE><br />");
	
	print("<B>Action Type<B><br />");
	print("<TABLE>\n");
	print("<TR><TD width=90 align=left><B><font color=red>file</font></B></TD><TD>To delete files from the Database that have been removed from the File System </TD></TR>");
	print("<TR><TD width=90 align=left><B><font color=red>folder</font></B></TD><TD>To delete folders from the Database that have been removed from the File System </TD></TR>");
	print("<TR><TD width=90 align=left><B><font color=red>both</font></B></TD><TD>To delete files and Folders from the Database that have been removed from the File System </TD></TR>");
	print("</TABLE>");
}
?>