Location: PHPKode > projects > phlyMail Lite > phlymail/handlers/files/api.php
<?php
/**
 * Offering API calls for interoperating with other handlers
 * @package phlyMail Nahariya 4.0+ Default branch
 * @subpackage Handler Files
 * @copyright 2004-2010 phlyLabs, Berlin (http://phlylabs.de)
 * @version 4.0.5 2010-06-09
 */
// Only valid within phlyMail
if (!defined('_IN_PHM_')) die();

class api_files
{
    protected $STOR = false;
    protected $foldersInited = false;

    /**
     * Constructor method
     *
     * @param  array reference  public settings structure
     * @param  int  ID of the user to perform the operation for
     * @return  boolean  true on success, false otherwise
     * @since 0.0.1
     */
    public function __construct(&$_PM_, $uid)
    {
        require_once($_PM_['path']['handler'].'/files/fs.php');
        $this->STOR = new files_storage($uid, '');
        return is_object($this->STOR);
    }

    /**
     * Allows other handlers to save an item in a desired folder within given
     * user context. Please be aware, that you will have to pass all necessary
     * meta fields by yourself.
     *
     * @see files_storage::file_item()
     * @sinxe 0.0.1
     */
    public function save_item($itemdata, $res = false)
    {
        return $this->STOR->file_item($itemdata, $res);
    }

    /**
     * Returns a list of existing folders for a given user
     * @param  void
     * @return  array  Folder list with various meta data
     * @since 0.0.9
     */
    public function give_folderlist()
    {
        if (!$this->foldersInited) {
    	   $this->foldersInited = $this->STOR->init_folders(false);
        }
        $icon_path = $GLOBALS['_PM_']['path']['theme'].'/icons/';
        $return = array();
    	foreach ($this->STOR->read_folders_flat(0) as $k => $v) {
            $v['is_trash'] = 0;
            // Find special icons for folders
            switch ($v['icon']) {
            case ':waste':
                $v['big_icon'] = $icon_path.'waste_big.gif';
                $v['icon'] = $icon_path.'waste.png';
                $v['is_trash'] = 1;
                break;
            case ':files':
                $v['big_icon'] = $icon_path.'files_big.gif';
                $v['icon'] = $icon_path.'files.png';
                break;
            case ':virtual':
                $v['big_icon'] = $icon_path.'virtualfolder_big.gif';
                $v['icon'] = $icon_path.'virtualfolder.png';
                break;
            }
            if (!file_exists($v['icon'])) $v['icon'] = $icon_path.'folder_def.png';
            if (!isset($v['big_icon']) || !file_exists($v['big_icon'])) $v['big_icon'] = $icon_path.'folder_def_big.gif';
            $return[$k] = $v;
    	}
    	return $return;
    }

    /**
     * Takes a path and tries to find out, whether the referenced item is a dir
     * or a file.
     *
     * @param string $path  Path to parse
     * @param bool  $ext  Extended mode, which returns the item, not just the type
     * @return 'f'|'d'|false  F for a file, d for a dir, false otherwise
     */
    public function resolve_path($path, $ext = false)
    {
        if (!$path || $path == '/') return 'd';
        if (!$this->foldersInited) {
    	   $this->foldersInited = $this->STOR->init_folders(false);
        }
        $parent = dirname($path);
        $me     = basename($path);
        $hit    = false;
        foreach($this->STOR->read_folders_flat() as $folder) {
            if ($folder['path_canon'].'/' == $path) {
                if (!$ext) return 'd';
                return array('type' => 'd', 'item' => $folder);
            }
            if ($folder['path_canon'] == $parent) {
                $hit = $folder;
            }
        }
        if ($hit) {
            if (!$ext) return 'f';
            foreach ($this->give_itemlist($hit['path']) as $file) {
                if ($me == $file['friendly_name']) {
                    return array('type' => 'f', 'item' => $file);
                }
            }
        }
        return false;
    }

    /**
     * Returns all items stored in a folder
     *
     *[@param int $id  ID of the folder]
     *[@param string $path  Canonical path of the folder]
     */
    public function give_itemlist($fid = null, $path = null)
    {
        if (!is_null($path)) {
            if (!$this->foldersInited) {
    	       $this->foldersInited = $this->STOR->init_folders(false);
            }
            foreach ($this->STOR->read_folders_flat() as $folder) {
                if ($folder['path_canon'] == $path) {
                    $fid = $folder['path'];
                    break;
                }
                if ($folder['path_canon'].'/' == $path) {
                    $fid = $folder['path'];
                    break;
                }
            }
        }
        if (is_null($fid)) return false;
        $return = array();
        $groesse = $this->STOR->init_items($fid);
        $anzahl = isset($groesse['items']) ? $groesse['items'] : 0;
        for ($i = 0; $i < $anzahl; ++$i) {
            $return[] = $this->STOR->get_item_info($i);
        }
        return $return;
    }

    /**
     * Used for getting a list of items in a given folder in a quite generic format
     *
     * @param int $fid  ID of the folder
     * @return array
     */
    public function selectfile_itemlist($fid)
    {
        $DB = &$GLOBALS['DB'];
        $_PM_ = &$GLOBALS['_PM_'];
        $passthrough = give_passthrough(1);
        require_once($_PM_['path']['lib'].'/phm_mime_handler.php');
        $MIME = new phm_mime_handler($_PM_['path']['conf'].'/mime.map.wpop', true);
        $return = array();
        $groesse = $this->STOR->init_items($fid);
        $anzahl = isset($groesse['items']) ? $groesse['items'] : 0;
        for ($i = 0; $i < $anzahl; ++$i) {
            $item = $this->STOR->get_item_info($i);
            $mimeicon = '';
            $item['l2'] = size_format($item['size']);
            if (substr($item['type'], 0, 6) == 'image/') {
                $thumb = $DB->thumb_get('files', $item['id'], 'fselect');
                if (false === $thumb) { // Try to create thumbnail
                    $thumb = basics::create_thumbnail($this->STOR->item_get_real_location($item['id'], true), 32, 32);
                    if (false !== $thumb) {
                        $DB->thumb_add('files', $item['id'], 'fselect', $thumb['mime'], $thumb['size'], $thumb['width'], $thumb['height'], $thumb['stream']);
                    }
                }
                if (is_array($thumb) && $thumb['mime'] && 0 != $thumb['size']) {
                    $mimeicon = htmlspecialchars(PHP_SELF.'?load=ilist&handler=files&'.$passthrough.'&getthumb='.$item['id'].'&type=fselect');
                }
                if ($item['img_w'] && $item['img_h']) {
                    $item['l2'] .= '; '.$item['img_w'].' x '.$item['img_h'];
                }
            }
            if (!$mimeicon) {
                $mimeicon = $MIME->get_icon_from_type($_PM_['path']['frontend'].'/filetypes/32', $item['type'], array('png', 'gif', 'jpg'));
                if (!$mimeicon) {
                    $mimeinfo = $MIME->get_type_from_name($item['friendly_name'], true);
                    $mimeicon = $MIME->get_icon_from_type($_PM_['path']['frontend'].'/filetypes/32', $mimeinfo[0], array('png', 'gif', 'jpg'));
                    $item['type'] = $mimeinfo[0];
                }
                $mimeicon = $_PM_['path']['frontend'].'/filetypes/32/'.$mimeicon;
            }
            $return[] = array
                    ('id' => $item['id']
                    ,'i32' => $mimeicon
                    ,'mime' => $item['type']
                    ,'l1' => $item['friendly_name']
                    ,'l2' => $item['l2'].'; '.$MIME->get_typename_from_type($item['type'], true)
                    );
        }
        return $return;
    }

    /**
     * Inits a SendTo handshake as the initiator of a SendTo. This method is called
     * by the receiving handler to get some info about the mail part it will receive.
     * This info usually is displayed to the user to allow some dedicated action by him.
     *
     * @param int $item  ID of the item you wish to address
     * @since 0.1.1
     */
    public function sendto_fileinfo($item)
    {
        $info = $this->STOR->get_item_info($item, true);
        if (false === $info || empty($info)) return false;
        return array('content_type' => $info['type'], 'encoding' => '8bit'
                ,'charset' => 'UTF-8', 'filename' => $info['friendly_name']
                ,'length' => $info['size']);
    }

    /**
     * SendTo handshake part 2: The receiver now tells us to initialise the sending process.
     *
     * @param int $item ID of the item we wish to read
     * @return bool TRUE on success, FALSE on failure to open the stream for reading from
     * @since 0.1.1
     */
    public function sendto_sendinit($item)
    {
        return $this->STOR->item_open_stream($item, 'r');
    }

    /**
     * Extending the inital SendTo protocol by methods for line or block wise reading.
     *
     *[@param int $len Number of bytes to read at once; Default: 0, which will return max. 1024B]
     * @return string
     * @since 0.1.1
     */
    public function sendto_sendline($len = 0)
    {
        return $this->STOR->item_read_stream($len);
    }

    /**
     * Closes the stream to the sent file again
     *
     * @param void
     * @return void
     * @since 0.1.1
     */
    public function sendto_finish()
    {
        $this->STOR->item_close_stream();
    }

    public function webapi_readfile($item)
    {
        $path = $this->STOR->item_get_real_location($item, true);
        if (!file_exists($path)) return false;
        return fopen($path, 'r');
    }
}
?>
Return current item: phlyMail Lite