Source for file FileDrop.php
Documentation is available at FileDrop.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Christian Weiske <cweiske@php.net> |
// +----------------------------------------------------------------------+
// $Id: FileDrop.php 304748 2010-10-25 09:44:01Z clockwerx $
require_once('MIME/Type.php');
require_once('PEAR.php');
* @const GTK_FILEDROP_WIDGET_NOT_SUPPORTED
define('GTK_FILEDROP_WIDGET_NOT_SUPPORTED', 1 );
* A class which makes it easy to
* make a GtkWidget accept the dropping
* @author Christian Weiske <cweiske@cweiske.de>
* - reject files when moving the dragging mouse over the widget, just like opera does
* how does this work? I don't know, but I suppose I should
* Simply change the text of a widget
* (accept files with MIME-Types text/plain and text/html and files with .sgml extension):
* Gtk_FileDrop::attach($entry, array('text/plain', 'text/html', '.sgml'));
* Call a callback, and don't change the text (accept directories only):
* Gtk_FileDrop::attach($entry, array( 'inode/directory'), array( &$this, 'callback'), false);
* prepares a widget to accept file drops
* @param GtkWidget The widget which shall accept files
* @param array List of MIME-Types to accept OR extensions, beginning with a dot "."
* @param mixed Callback to call when a drop with valid files happened
* @param boolean If the widget's text/label/content shall be changed automatically
* @return boolean If all was ok
function attach($widget, $arTypes, $objCallback = null , $bSetText = true )
$widget->drag_dest_set (GTK_DEST_DEFAULT_ALL , array (array ('text/uri-list', 0 , 0 )), GDK_ACTION_COPY | GDK_ACTION_MOVE );
$widget->connect ('drag-data-received', array ( &$fd, 'dragDataReceived'));
function Gtk_FileDrop ( $arTypes, $objCallback = null , $bSetText = true )
$this->arTypes = $arTypes;
$this->objCallback = $objCallback;
$this->bSetText = $bSetText;
* prepares a widget to accept directories only
* Just a shortcut for the exhausted programmer
* @param GtkWidget The widget which shall accept directories
* @return boolean If all was ok
* Data have been dropped over the widget
* @param GtkWidget The widget on which the data have been dropped
* @param GdkDragContext The context of the drop
* @param int Info parameter (0 in our case)
* @param int The time on which the event happened
$arData = explode("\n", $data->data );
foreach ($arData as $strLine) {
$strLine = trim($strLine);
foreach ($this->arTypes as $strType) {
if ($strType == 'inode/directory') {
|| $strType == $strFileMime || MIME_Type ::wildcardMatch ($strType, $strFileMime)) {
$arAccepted[] = $strFile;
$arRejected[] = $strFile;
//make directories from the files if dirs are accepted
//this is done here to give native directories first places on the list
if ($bDirectories && count($arRejected) > 0 ) {
foreach ($arRejected as $strFile) {
$arAccepted[] = dirname( $strFile);
if (count($arAccepted) == 0 ) {
$widget->set_text ($arAccepted[0 ]);
$childs = $widget->children ();
$child->set_text ($arAccepted[0 ]);
$entry->set_text ($arAccepted[0 ]);
$widget->set_filename ($arAccepted[0 ]);
foreach ($arAccepted as $strFile) {
$items[] = & new GtkListItem ($strFile);
$widget->append_items ($items);
if ($this->objCallback !== null ) {
* converts a file path gotten from a text/uri-list
* drop to a usable local filepath
* Php functions like parse_url can't be used as it is
* likely that the dropped URI is no real URI but a
* strange thing which tries to look like one
* See the explanation at:
* http://gtk.php.net/manual/en/tutorials.filednd.urilist.php
* @param string The line from the uri-list
* @return string The usable local filepath
$strUriFile = urldecode($strUriFile); //should be URL-encoded
if (substr($strUriFile, 0 , 5 ) == 'file:') {
//(maybe buggy) file protocol
if (substr($strUriFile, 0 , 17 ) == 'file://localhost/') {
$strFile = substr($strUriFile, 16 );
} else if (substr($strUriFile, 0 , 8 ) == 'file:///') {
//no hostname, but three slashes - nearly correct
$strFile = substr($strUriFile, 7 );
} else if ($strUriFile[5 ] == '/') {
//theoretically, the hostname should be the first
//but no one implements it
$strUriFile = substr($strUriFile, 5 );
for( $n = 1; $n < 5; $n++ ) {
if ($strUriFile[$n] != '/') {
$strUriFile = substr($strUriFile, $n - 1 );
//perhaps a correct implementation with hostname???
$strUriFile = $strUriFileNoHost;
//NO slash after "file:" - what is that for a crappy program?
$strFile = substr ($strUriFile, 5 );
} else if (strstr($strUriFile, '://')) {
//real protocol, but not file
if (!$bUrl && $strFile[2 ] == ':' && $strFile[0 ] == '/') {
* returns the extension if a filename
* including the leading dot
* @param string The filename
* @return string The extension with a leading dot
if (strpos($strExt, '/') !== false ) {
* determines the mime-type for the given file
* @param string The file name
* @return string The MIME type or FALSE in the case of an error
//MIME_Type doesn't return the right type for directories
//The underlying functions MIME_Type used don't return it right,
//so there is no chance to fix MIME_Type itself
|| substr($strFile, -1 ) == '/') {
return 'inode/directory';
$strMime = MIME_Type ::autoDetect ($strFile);
if (!PEAR ::isError ($strMime)) {
//determine by extension | as MIME_TYPE doesn't support this, I have to do this myself
Documentation generated on Mon, 11 Mar 2019 15:40:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|