actions.class.php 3.98 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?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);
            }
        }
    }
}