root/devel/mod/file/lib.php

Revision 1550, 22.8 kB (checked in by dramirez, 1 year ago)

Remove all the code related to the river plugin to make it a selfcontained functionality

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 require_once($CFG->dirroot.'lib/filelib.php');
4
5 function file_pagesetup() {
6     // register links --
7     global $profile_id;
8     global $PAGE;
9     global $CFG;
10     global $metatags;
11     
12     require_once (dirname(__FILE__)."/lib/file_config.php");
13     $page_owner = $profile_id;
14     
15     if (isloggedin()) {
16         if (defined("context") && context == "files" && $page_owner == $_SESSION['userid']) {
17             $PAGE->menu[] = array( 'name' => 'files',
18                                    'html' => "<li><a href=\"{$CFG->wwwroot}{$_SESSION['username']}/files/\" class=\"selected\" >" .__gettext("Your Files").'</a></li>');
19         } else {
20             $PAGE->menu[] = array( 'name' => 'files',
21                                    'html' => "<li><a href=\"{$CFG->wwwroot}{$_SESSION['username']}/files/\" >" .__gettext("Your Files").'</a></li>');
22         }
23     }
24
25     if (defined("context") && context == "files") {
26         
27         $files_username = user_info('username', $page_owner);
28
29         if ($page_owner != -1) {
30             if ($page_owner == $_SESSION['userid'] && $page_owner != -1) {
31                 $PAGE->menu_sub[] = array( 'name' => 'file:rss',
32             'html' => '<a href="' . $CFG->wwwroot . $_SESSION['username'] . '/files/rss/"><img src="' . $CFG->wwwroot . 'mod/template/icons/rss.png" border="0" alt="rss" /></a>');
33             }
34         }
35
36         if ($page_owner == $_SESSION['userid'] && $page_owner != -1) {
37             $PAGE->menu_sub[] = array( 'name' => 'file:add',
38                                        'html' => a_href( "#addFile",
39                                                           __gettext("Add a file or a folder")));
40     }
41     }
42   // Adding the file's selector wizard
43   $options = array('options'=> 'width=600,height=300,left=20,top=20,scrollbars=yes,resizable=yes',
44                    'name'=> 'mediapopup',
45                    // 'url' => $CFG->wwwroot."mod/file/file_include_wizard.php?owner=".$_SESSION['userid']);
46                    'url' => $CFG->wwwroot."mod/file/file_include_wizard.php?owner=".page_owner());
47   add_content_tool_button("mediapopup",__gettext("Add File"), "image.png", "f", $options);
48
49 }
50
51 function file_init() {
52     global $CFG;
53     global $function;
54     global $metatags;
55
56     // Styles for file icons
57         $metatags .= "<style type=\"text/css\">";
58         $metatags .= str_replace("{{url}}", $CFG->wwwroot, file_get_contents(dirname(__FILE__). "/file-icons.css"));
59         $metatags .= "</style>";
60         
61     // Functions to perform upon initialisation
62         $function['files:init'][] = $CFG->dirroot . "mod/file/lib/files_init.php";
63         $function['files:init'][] = $CFG->dirroot . "mod/file/lib/metadata_defaults.php";
64         $function['files:init'][] = $CFG->dirroot . "mod/file/lib/inline_mimetypes.php";
65         $function['init'][] = $CFG->dirroot . "mod/file/default_templates.php";
66     
67     // Mime-type init
68         $function['files:metadata:init'][] = $CFG->dirroot . "mod/file/lib/inline_mimetypes.php";
69         
70     // Actions to perform
71         $function['files:init'][] = $CFG->dirroot . "mod/file/lib/files_actions.php";
72
73     // Init for search
74         $function['search:init'][] = $CFG->dirroot . "mod/file/lib/files_init.php";
75         $function['search:all:tagtypes'][] = $CFG->dirroot . "mod/file/lib/function_search_all_tagtypes.php";
76         
77     // Function to search through weblog posts
78         $function['search:display_results'][] = $CFG->dirroot . "mod/file/lib/function_search.php";
79         $function['search:display_results:rss'][] = $CFG->dirroot . "mod/file/lib/function_search_rss.php";
80         
81     // Determines whether or not a file should be displayed in the browser
82         $function['files:mimetype:inline'][] = $CFG->dirroot . "mod/file/lib/files_mimetype_inline.php";
83         
84     // View files
85         $function['files:view'][] = $CFG->dirroot . "mod/file/lib/files_view.php";
86
87     // View the contents of a specific folder
88         $function['files:folder:view'][] = $CFG->dirroot . "mod/file/lib/folder_view.php";
89         
90     // Edit the contents of a specific folder
91         $function['files:folder:edit'][] = $CFG->dirroot . "mod/file/lib/edit_folder.php";
92
93     // Add files through the wizard
94         $function['files:wizard:add:file'][] = $CFG->dirroot . "mod/file/lib/add_file.php";
95         
96     // Edit the metadata for a specific file
97         $function['files:edit'][] = $CFG->dirroot . "mod/file/lib/edit_file.php";
98         $function['folder:select'][] = $CFG->dirroot . "mod/file/lib/select_folder.php";
99     
100     // Edit metadata
101         $function['metadata:edit'][] = $CFG->dirroot . "mod/file/lib/metadata_edit.php";
102         
103     // Turn file ID into a link
104         $function['files:links:make'][] = $CFG->dirroot . "mod/file/lib/files_links_make.php";
105         
106     // Allow users to embed files in weblog posts
107         $function['weblogs:text:process'][] = $CFG->dirroot . "mod/file/lib/weblogs_text_process.php";
108
109         $function['weblogs:posts:add:fields'][] = $CFG->dirroot . "mod/file/lib/weblogs_posts_add_fields.php";
110         $function['weblogs:posts:edit:fields'][] = $CFG->dirroot . "mod/file/lib/weblogs_posts_add_fields.php";
111                     
112     // Log on bar down the right hand side
113         $function['display:sidebar'][] = $CFG->dirroot . "mod/file/lib/files_user_info_menu.php";
114         
115     // Template preview
116         $function['templates:preview'][] = $CFG->dirroot . "mod/file/lib/templates_preview.php";
117
118     // Establish permissions
119         $function['permissions:check'][] = $CFG->dirroot . "mod/file/lib/permissions_check.php";
120
121     // Actions to perform when an access group is deleted
122         $function['groups:delete'][] = $CFG->dirroot . "mod/file/lib/groups_delete.php";
123
124     // Publish static RSS file of files
125         $function['files:rss:getitems'][] = $CFG->dirroot . "mod/file/lib/function_rss_getitems.php";
126         $function['files:rss:publish'][] = $CFG->dirroot . "mod/file/lib/function_rss_publish.php";
127     
128     // Has the $CFG->files->default_handler been set? If not, set it to local
129         if (empty($CFG->files->default_handler)) {
130             $CFG->files->default_handler = "elgg";
131         }
132         if (empty($CFG->folders->default_handler)) {
133             $CFG->folders->default_handler = "elgg";
134         }
135         $CFG->folders->handler["elgg"]['menuitem'] = __gettext("Default file folder");
136         $CFG->folders->handler["elgg"]['view'] = "file_folder_view";
137         $CFG->folders->handler["elgg"]['preview'] = "file_folder_preview";
138         
139         $CFG->widgets->list[] = array(
140                                         'name' => __gettext("Files widget"),
141                                         'description' => __gettext("Displays images of some of your files."),
142                                         'type' => "file::files"
143                                 );
144         
145    // Delete users
146         listen_for_event("user","delete","file_user_delete");
147                 
148     // Register a display object function
149     display_set_display_function('file', 'file_displayobject');
150 }
151
152 function file_permissions_check($objecttype, $owner){
153     $run_result = null;
154
155     if ($objecttype == "files" || $objecttype == "files:edit") {
156         if (logged_on && $owner == $_SESSION['userid']) {
157             $run_result = true;
158         }
159     }
160     return $run_result;
161 }
162     
163 function file_displayobject($object_id,$object_type)
164 {
165     global $page_owner, $CFG;
166         $owner_username = user_info('username', $page_owner);
167
168     $return = "";
169
170     if ($object_type=="file::file")
171     {
172         if ($file = get_record_select('files', "ident=$object_id"))
173         {
174             if (run("users:access_level_check",$file->access) == true || $file->owner == $_SESSION['userid'])
175             {
176     
177                 $username = $owner_username;
178                 $ident = (int) $file->ident;
179                 $folder->ident = $file->folder;
180                 $title = get_access_description($file->access);
181                 $title .= stripslashes($file->title);
182                 $description = nl2br(stripslashes($file->description));
183                 $filetitle = urlencode($title);
184                 $originalname = stripslashes($file->originalname);
185                 $filemenu = round(($file->size / 1048576),4) . "MB ";
186                 $icon = $CFG->wwwroot . "_icon/file/" . $file->ident;
187                 $filepath = $CFG->wwwroot . "$username/files/$folder->ident/$ident/" . urlencode($originalname);
188                 
189                 $mimetype = mimeinfo('type',$file->originalname);
190     
191                 if ($mimetype == "audio/mpeg" || $mimetype == "audio/mp3") {
192                     $filemenu .= " <object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"
193                     codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"
194                     width=\"17\" height=\"17\" >
195                     <param name=\"allowScriptAccess\" value=\"sameDomain\" />
196                     <param name=\"movie\" value=\"" . $CFG->wwwroot . "mod/file/mp3player/musicplayer.swf?song_url=$filepath&amp;song_title=$filetitle\" />
197                     <param name=\"quality\" value=\"high\" />
198                     <embed src=\"" . $CFG->wwwroot . "mod/file/mp3player/musicplayer.swf?song_url=$filepath&amp;song_title=$filetitle\"
199                     quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\"
200                     type=\"application/x-shockwave-flash\"
201                     pluginspage=\"http://www.macromedia.com/go/getflashplayer\"
202                     align=\"center\" height=\"17\" width=\"17\" />
203                     </object>";
204                 }
205     
206     
207                 $keywords = display_output_field(array("","keywords","file","file",$ident,$file->owner));
208                 if ($keywords) {
209                     $keywords = __gettext("Keywords: ") . $keywords;
210                 }
211     
212                 $return = <<< END
213                     <table>
214                         <tr>
215                             <td><img src="$icon" alt="$originalname" /></td>
216                             <td>
217                                 <p><b>$title</b></p>
218                                 <p>$description</p>
219                                 <p>$originalname</p>
220                             </td>
221                         </tr>
222                     </table>
223 END;
224     
225             }
226         }
227     }
228     
229     
230     return $return;
231
232 }
233
234     function file_user_delete($object_type, $event, $object) {
235         global $CFG;
236         if (!empty($object->ident) && $object_type == "user" && $event == "delete") {
237             if ($files = get_records_sql("select * from {$CFG->prefix}files where owner = {$object->ident} or files_owner = {$object->ident}")) {
238                 foreach($files as $file) {
239                     $file = plugin_hook("file","delete",$file);
240                     if (!empty($file)) {
241                         @unlink(stripslashes($CFG->dataroot.$file->location));
242                         delete_records('files','ident',$file->ident);
243                         delete_records('tags','tagtype','file','ref',$file->ident);
244                     }
245                 }
246             }
247             @unlink($CFG->dataroot."files/".substr($object->username,0,1)."/".$object->username);
248             if ($folders = get_records_sql("select * from {$CFG->prefix}file_folders where owner = {$object->ident} or files_owner = {$object->ident}")) {
249                 foreach($folders as $folder) {
250                     $folder = plugin_hook("folder","delete",$folder);
251                     if (!empty($folder)) {
252                         set_field('file_folders','parent',-1,'parent',$folder->ident);
253                         set_field('files','folder',-1,'folder',$folder->ident);
254                         delete_records('file_folders','ident',$folder->ident);
255                         delete_records('tags','tagtype','folder','ref',$folder->ident);
256                     }
257                 }
258             }
259         }
260
261         return $object;
262     }
263
264 function file_widget_display($widget) {
265     global $CFG;
266     $latest_files = clean_param(widget_get_data("latest_files",$widget->ident),PARAM_INT);
267     $html = "<p>" . __gettext("No files found.") . "</p>";
268     if ($widget->type == "file::files") {
269         $where1 = run("users:access_level_sql_where",$_SESSION['userid']);
270         if ($files = get_records_sql("select * from {$CFG->prefix}files where files_owner = {$widget->owner} and (".$where1.") order by time_uploaded desc limit {$latest_files}")) {
271             $html = "";
272             foreach ($files as $file) {
273                 $keywords = display_output_field(array("","keywords","file","file",$file->ident,$file->owner));
274                     if ($keywords) {
275                         $keywords = __gettext("Keywords: ") . $keywords;
276                     }
277                 $html .= templates_draw(array(
278                                                   'context' => 'file',
279                                                   'username' => user_info("username",$file->files_owner),
280                                                   'title' => $file->title,
281                                                   'ident' => $file->ident,
282                                                   'folder' => $file->folder,
283                                                   'description' => $file->description,
284                                                   'originalname' => $file->originalname,
285                                                   'url' => $CFG->wwwroot . user_info("username",$file->files_owner) . "/files/$file->folder/$file->ident/" . urlencode($file->originalname),
286                                                   'menu' => "",
287                                                   'icon' => $CFG->wwwroot . "_icon/file/" . $file->ident,
288                                                   'keywords' => $keywords
289                                                   )
290                                             );
291             }
292         }
293     }
294     return array("title" => __gettext("Latest files") , "content" => $html);
295 }
296
297 function file_widget_edit($widget) {
298     $latest_files = clean_param(widget_get_data("latest_files",$widget->ident),PARAM_INT);
299     $body = "";
300     $body = "<h2>" . __gettext("Files widget") . "</h2>";
301     $body .= "<p>" . __gettext("This widget displays the last couple of files from this account. Simply select the number of latest files below:") . "</p>";
302     $body .= "<p><input type=\"text\" name=\"widget_data[latest_files]\" value=\"" . $latest_files . "\" /></p>";
303     return $body;
304 }
305
306
307
308     function file_folder_view($folder) {
309         
310         global $CFG;
311         /*
312          *    View a specific folder
313          *    (Access rights are presumed)
314          */
315         
316         // Find out who's the owner
317             
318         global $page_owner;
319         $owner_username = user_info('username', $page_owner);
320         
321         // If we're not in the parent folder, provide a link to return to the parent
322         
323         /*
324         if ($folder->ident != -1) {
325             $folder->name = stripslashes($folder->name);
326         }
327         */
328                 
329         $body = "<h2>" . $folder->name . "</h2>";
330         
331         // Firstly, get a list of folders
332         // Display folders we actually have access to
333         if ($folder->idents = get_records_select('file_folders',"parent = $folder->ident AND (". run("users:access_level_sql_where") . ") and files_owner = $page_owner")) {
334             $subFolders = __gettext("Subfolders"); // gettext variable
335             $body .= <<< END
336                 
337                             <h3>
338                                 $subFolders
339                             </h3>
340         
341 END;
342             
343             foreach($folder->idents as $folder->ident_details) {
344                 if (run("users:access_level_check",$folder->ident_details->access) == true) {
345                     $username = $owner_username;
346                     $ident = (int) $folder->ident_details->ident;
347                     $name = get_access_description($folder->ident_details->access);
348                     $name .= stripslashes($folder->ident_details->name);
349                     $folder->identmenu = file_folder_edit_links($folder->ident_details);
350                     $keywords = display_output_field(array("","keywords","folder","folder",$ident,$folder->ident_details->owner));
351                     if ($keywords) {
352                         $keywords = __gettext("Keywords: ") . $keywords;
353                     }
354                     $body .= templates_draw(array(
355                                                   'context' => 'folder',
356                                                   'username' => $username,
357                                                   'url' => $CFG->wwwroot . "$username/files/$ident",
358                                                   'ident' => $ident,
359                                                   'name' => $name,
360                                                   'menu' => $folder->identmenu,
361                                                   'icon' => $CFG->wwwroot . "mod/file/folder.png",
362                                                   'keywords' => $keywords
363                                                   )
364                                             );
365             
366
367                 }
368                 
369             }
370         }
371             
372         // Then get a list of files
373         // View files we actually have access to
374         if ($files = get_records_select('files',"folder = ? AND files_owner = ?",array($folder->ident,$page_owner))) {
375             foreach($files as $file) {
376                 
377                 if (run("users:access_level_check",$file->access) == true || $file->owner == $_SESSION['userid']) {
378                     $username = $owner_username;
379                     $ident = (int) $file->ident;
380                     $folder->ident = $file->folder;
381                     $title = get_access_description($file->access);
382                     $title .= stripslashes($file->title);
383                     $description = nl2br(stripslashes($file->description));
384                     $filetitle = urlencode($title);
385                     $originalname = stripslashes($file->originalname);
386                     $filemenu = round(($file->size / 1048576),4) . "MB ";
387                     $icon = $CFG->wwwroot . "_icon/file/" . $file->ident;
388                     $filepath = $CFG->wwwroot . "$username/files/$folder->ident/$ident/" . urlencode($originalname);
389                     
390                     $mimetype = mimeinfo('type',$file->originalname);
391                     if ($mimetype == "audio/mpeg" || $mimetype == "audio/mp3") {
392                         $filemenu .= " <object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"
393         codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"
394         width=\"17\" height=\"17\" >
395             <param name=\"allowScriptAccess\" value=\"sameDomain\" />
396    &n