Simple status box for the sidebar.
[elgg_plugins.git] / filecrawler / lib.php
blobcc9ac8c3e4dc73fad22b2d719f4ce35e38f141df
1 <?php
2 /*
3 Filecrawler plugin for Elgg
4 Initial author: Sven Edge <sven@elgg.net> September 2006
6 Plugin to crawl user-uploaded files, and
7 - for files that exist on the filesystem but not in the db, either add to the user's root filestore folder or delete them from the filesystem
8 - for files with entries in the db but not on the filesystem, purge them from the database
10 Config options:
12 $CFG->filecrawler_add_fs_orphans_to_db = true;
13 $CFG->filecrawler_purge_fs_orphans = false;
14 $CFG->filecrawler_purge_db_orphans = true;
15 $CFG->filecrawler_fix_fs_permissions = true;
19 function filecrawler_pagesetup() {
21 } // end function filecrawler_pagesetup
25 function filecrawler_init() {
27 // global $function;
28 global $CFG;
29 if (isset($CFG->filecrawler_purge_db_orphans) && $CFG->filecrawler_purge_db_orphans == false) {
30 $CFG->filecrawler_purge_db_orphans = false;
31 } else {
32 $CFG->filecrawler_purge_db_orphans = true;
35 if (isset($CFG->filecrawler_fix_fs_permissions) && $CFG->filecrawler_fix_fs_permissions == false) {
36 $CFG->filecrawler_fix_fs_permissions = false;
37 } else {
38 $CFG->filecrawler_fix_fs_permissions = true;
41 // default to adding, but purge if explicitly told to.
42 if (isset($CFG->filecrawler_add_fs_orphans_to_db) && $CFG->filecrawler_add_fs_orphans_to_db == false) {
43 if (empty($CFG->filecrawler_purge_fs_orphans)) {
44 $CFG->filecrawler_purge_fs_orphans = false;
45 } else {
46 $CFG->filecrawler_purge_fs_orphans = true;
48 } else {
49 $CFG->filecrawler_add_fs_orphans_to_db = true;
50 $CFG->filecrawler_purge_fs_orphans = false;
53 } // end function filecrawler_init
57 // called by cron.php
58 function filecrawler_cron() {
60 if (!empty($CFG->filecrawler_lastcron) && (time() - (60*60*24)) < $CFG->filecrawler_lastcron) {
61 return true;
62 } else {
63 filecrawler_crawl();
66 set_config('filecrawler_lastcron',time());
68 } // end function filecrawler_cron
72 // perambulate the user filestore, or just one directory
73 function filecrawler_crawl($limitinitial = "") {
75 global $CFG;
76 if (is_dir($CFG->dataroot . 'files/')) {
77 chdir($CFG->dataroot . 'files/');
79 $limitinitial = substr($limitinitial, 0, 1);
80 if ($limitinitial) {
81 $initials = array($limitinitial);
82 } else {
83 $initials = glob("*", GLOB_ONLYDIR); // GLOB_ONLYDIR only on windows php > 4.3.3
86 $userdirs = array();
87 if (is_array($initials)) {
88 foreach ($initials as $aninitial) {
89 chdir($CFG->dataroot . 'files/' . $aninitial);
90 $userdirs = array_merge($userdirs , glob("*", GLOB_ONLYDIR)); // GLOB_ONLYDIR only on windows php > 4.3.3
94 foreach ($userdirs as $adir) {
95 filecrawler_process_dir($adir);
99 } // end function filecrawler_crawl
103 // process a (user's) directory's files.
104 function filecrawler_process_dir($username) {
106 global $CFG;
107 $parentdir = $CFG->dataroot . 'files/';
108 $username = trim($username);
109 $initial = substr($username, 0, 1);
110 $userdir = $initial . '/' . $username . '/';
112 if ($username && is_dir($parentdir . $userdir) && $userid = user_info_username('ident',$username)) {
114 if ($CFG->filecrawler_purge_db_orphans == true) {
115 $dbfiles = get_records('files', 'files_owner', $userid);
116 foreach ($dbfiles as $key => $adbfile) {
117 if (!file_exists($CFG->dataroot . $adbfile->location)) {
119 mtrace('Deleting files table entry for missing file ' . $adbfile->location . '.');
120 // how safe to be...?
121 //delete_records('files', 'ident', $adbfile['ident']);
122 delete_records('files', 'ident', $adbfile->ident, 'files_owner', $userid);
128 if ($CFG->filecrawler_add_fs_orphans_to_db || $CFG->filecrawler_purge_fs_orphans) {
130 // query repetition less efficient but safer
131 $dbfiles = get_records('files', 'files_owner', $userid);
132 foreach ($dbfiles as $adbfile) {
133 $dbfilelocations[] = $CFG->dataroot . $adbfile->location;
136 //get list of files
137 chdir($parentdir . $userdir);
138 $fsfiles = glob("*");
139 if (is_array($fsfiles)) {
140 foreach ($fsfiles as $afsfile) {
141 if (!in_array($parentdir . $userdir . $afsfile, $dbfilelocations)) {
142 if ($CFG->filecrawler_add_fs_orphans_to_db) {
144 mtrace('Adding ' . $userdir . $afsfile . ' to files table.');
145 //add it
146 $f = new StdClass;
147 $f->owner = $userid;
148 $f->files_owner = $userid;
149 $f->folder = '-1';
150 $f->originalname = $afsfile;
151 $f->title = 'Auto-added file';
152 $f->description = '';
153 $f->location = 'files/' . $userdir . $afsfile;
154 if ($CFG->default_access == 'PRIVATE') {
155 $f->access = 'user' . $userid;
156 } else {
157 $f->access = $CFG->default_access;
159 $f->size = filesize($CFG->dataroot . $f->location);
160 $f->time_uploaded = time();
161 $file_id = insert_record('files', $f);
162 if ($file_id) {
163 $rssresult = run("files:rss:publish", array($userid, false));
164 $rssresult = run("profile:rss:publish", array($userid, false));
167 } elseif ($CFG->filecrawler_purge_fs_orphans) {
169 mtrace('Deleting ' . $userdir . $afsfile . '.');
170 unlink($CFG->dataroot . $afsfile);
174 } // end if
175 } // end foreach
176 } // end if
180 } else {
181 return false;
184 if ($CFG->filecrawler_fix_fs_permissions) {
185 //mtrace('setting perms on ' . $parentdir . $userdir);
186 chmod($parentdir . $userdir, $CFG->directorypermissions);
187 chdir($parentdir . $userdir);
188 $fsfiles = glob("*");
189 if (is_array($fsfiles)) {
190 foreach ($fsfiles as $afsfile) {
191 //mtrace('setting perms on ' . $parentdir . $userdir . $afsfile);
192 chmod($parentdir . $userdir . $afsfile, $CFG->filepermissions);
197 } // end function filecrawler_process_dir