Actions

Source Code of This Project

/apps/frontend/modules/source/actions/actions.class.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * Source actions
 *
 * @package    pwp
 * @subpackage source
 * @author     Victor Rad'
 */
class sourceActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $sourceFiles = sfConfig::get('app_source_files');
        if (array_key_exists($request->getParameter('sources'), $sourceFiles)){
            $this->sourceFiles = $sourceFiles[$request->getParameter('sources')];
        }else{
            $this->forward404();
        }
    }

    /**
     * Create archive of source code of project and upload to user
     *
     * @param sfWebRequest $request
     */
    public function executeDownload(sfWebRequest $request)
    {
        $filesProj = $this->filesProject();

        // create archive
        $zip = new ZipArchive();
        $fileZip = sfConfig::get('app_project_name').'.zip';
        $fileZipFs = sfConfig::get('sf_data_dir').'/'.$fileZip;
        if (is_file($fileZipFs)){
            unlink($fileZipFs);
        }
        $zip->open($fileZipFs, ZIPARCHIVE::CREATE);
        foreach ($filesProj as $filePj){
            $zip->addFile($filePj, str_replace(sfConfig::get('sf_root_dir'), sfConfig::get('app_project_name'), $filePj));
        }
        $zip->close();

        if (is_file($fileZipFs)) {
            // set headers and send ziped source
            $this->getResponse()->clearHttpHeaders();
            $this->getResponse()->addCacheControlHttpHeader('No-cache');
            $this->getResponse()->setContentType('application/zip');
            $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
            $this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename="'.$fileZip.'"');
            $this->getResponse()->setHttpHeader('Content-Length', filesize($fileZipFs), true);
            $this->getResponse()->sendHttpHeaders();
            $this->getResponse()->setContent(file_get_contents($fileZipFs));
        }else{
            $this->redirect('@source_index');
        }

        return sfView::NONE;
    }

    /**
     * Browse source code
     *
     * @param sfWebRequest $request
     */
    public function executeBrowse(sfWebRequest $request)
    {
        $filesProj = $this->filesProject();
        $path = '/'.$request->getParameter('path');
        if (in_array(sfConfig::get('sf_root_dir').$path, $filesProj)){
            $this->sourceFiles = array($path);
        }else{
            $this->forward404();
        }
    }

    /**
     * First column on page
     */
    public function postExecute()
    {
        $this->filesProj = $this->filesProject('tree');
        sfContext::getInstance()->getConfiguration()->loadHelpers('MyUrl');
        $this->getResponse()->setSlot('colOne', $this->getPartial('colOne'));
    }

    /**
     * Build array of files of project
     *
     * @param string $mode of return array (flat or tree)
     * @return array
     */
    private function filesProject($mode = 'flat')
    {
        // prepare data
        sfContext::getInstance()->getConfiguration()->loadHelpers('MyUtil');
        $filters = sfConfig::get('app_source_filter');
        $filters = array_map('preg_quote', $filters);

        // load file paths
        if('flat' == $mode) {
            $filesProj = my_scandir(sfConfig::get('sf_root_dir'), array(
                    'flat' => array(
                            'fullPath' => true,
                            'recursive' => true
                    )), $filters);
        }
        elseif('tree' == $mode) {
            $filesProj = my_scandir(sfConfig::get('sf_root_dir'), array(
                    'tree' => array()), $filters);
        }

        return $filesProj;
    }
}