<?php

/**
 * exchange actions.
 *
 * @package    sf
 * @subpackage exchange
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class exchangeActions extends sfActions
{
    public function executeExchange(sfWebRequest $request)
    {
        if($_SERVER['PHP_AUTH_USER'] == sfConfig::get('app_import_auth_user') && $_SERVER['PHP_AUTH_PW'] == sfConfig::get('app_import_auth_pw')){
            $type = $request->getParameter('type');
            $mode = $request->getParameter('mode');

            $classNew = false;

            $className = 'ones' . ucfirst($type);

            if (class_exists($className)) {
                $classNew = new $className($request);
                if (method_exists($classNew, $mode)) {
                    file_put_contents(sfConfig::get('sf_log_dir') . '/exchange', date('Y-m-d H:i:s') . " " . $classNew . "_" . $mode . "\n", FILE_APPEND);
                    $classNew->$mode();
                } else {
                    $classNew = false;
                }
            }
            if ($classNew !== false) {
                $this->getResponse()->setContentType('text/plain');
                echo $classNew;
            }
        }else{
            $this->getResponse()->setHttpHeader('WWW-Authenticate',  'Basic realm="Need auth"');
        }
        return sfView::NONE;
    }
}
class onesCatalog
{
    private $request;
    private $response;
    private $dir;
    private $zip;

    public function __construct($request)
    {
        $this->request = $request;
        $this->response = array();
        $this->dir = sfConfig::get('sf_data_dir') . '/exchange';
        $this->zip = $this->dir . '/1c_exchange.zip';
    }

    public function __toString()
    {
        return implode("\n", $this->response);
    }

    public function checkauth()
    {
        $this->response[] = 'success';
        $this->response[] = session_name();
        $this->response[] = session_id();
    }

    public function init()
    {
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->dir), RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($objects as $name => $object) {
            if (basename($name) != '.' && basename($name) != '..') {
                if (is_dir($name)) {
                    @rmdir($name);
                } else {
                    @unlink($name);
                }
            }
        }
        $this->response[] = 'zip=yes';
        $this->response[] = 'file_limit=' . (1024 * 1024 * 512);
    }

    public function file()
    {
        $raw_post = file_get_contents('php://input');
        if ($raw_post != '') {
            file_put_contents($this->zip, $raw_post, FILE_APPEND | LOCK_EX);
        }
        $this->response[] = 'success';
    }

    public function import()
    {
        if (file_exists($this->zip)) {
            exec('unzip ' . $this->zip . ' -d ' . $this->dir . ' && chmod 0777 ' . $this->dir . ' -R');

            rename($this->zip, sfConfig::get('sf_log_dir') . '/' . time() . '_1c_exchange.zip');
            $this->response[] = 'progress';
        } else {
            $task = false;
            if ($this->request->getParameter('filename') == 'import.xml') {
                $task = 'exchange:import';
            } elseif ($this->request->getParameter('filename') == 'offers.xml') {
                $task = 'exchange:offers';
            }
            if ($task) {
                $cmd = sfConfig::get('sf_root_dir') . '/symfony ' . $task;

                file_put_contents(sfConfig::get('sf_log_dir') . '/exchange', date('Y-m-d H:i:s') . " " . $cmd . " start\n", FILE_APPEND);

                exec($cmd, $output);

                file_put_contents(sfConfig::get('sf_log_dir') . '/exchange', date('Y-m-d H:i:s') . " " . $cmd . "\n" . print_r($output, true) . "\n\n", FILE_APPEND);

                $this->response[] = 'success';
                $this->response[] = $cmd;
                $this->response = array_merge($this->response, $output);
            }
        }
    }
}
