Mygento_Geoip_Model_Abstract::update()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 8.4905
c 0
b 0
f 0
cc 6
nc 6
nop 0
1
<?php
2
3
/**
4
 *
5
 *
6
 * @category Mygento
7
 * @package Mygento_Geoip
8
 * @copyright Copyright © 2015 NKS LLC. (http://www.mygento.ru)
9
 */
10
class Mygento_Geoip_Model_Abstract
11
{
12
13
    protected $local_dir;
14
    protected $local_file;
15
    protected $local_archive;
16
    protected $remote_archive;
17
18
    public function __construct()
19
    {
20
        $this->local_dir = Mage::getBaseDir('var') . DS . 'geoip';
21
        $this->local_file = $this->local_dir . DS . 'SxGeoCity.dat';
22
        $this->local_archive = $this->local_dir . DS . 'SxGeoCity_utf8.zip';
23
        $this->remote_archive = 'http://sypexgeo.net/files/SxGeoCity_utf8.zip';
24
    }
25
26
    public function isFileExists()
27
    {
28
        $io = new Varien_Io_File();
29
        return $io->fileExists($this->local_file, true);
30
    }
31
32
    public function getArchivePath()
33
    {
34
        return $this->local_archive;
35
    }
36
37
    public function checkFilePermissions()
38
    {
39
        $io = new Varien_Io_File();
40
        $io->checkAndCreateFolder($this->local_dir);
41
        if (!$io->isWriteable($this->local_dir)) {
42
            return 'folder is not writable';
43
        }
44
        return '';
45
    }
46
47
    public function update()
48
    {
49
        $helper = Mage::helper('geoip');
50
51
        $ret = array('status' => 'error');
52
53
        if ($permissions_error = $this->checkFilePermissions()) {
54
            $ret['message'] = $permissions_error;
55
            return $ret;
56
        }
57
        $remote_file_size = $helper->getSize($this->remote_archive);
58
        if ($remote_file_size < 100000) {
59
            $ret['message'] = $helper->__('You are banned from downloading the file. Please try again in several hours.');
60
            return $ret;
61
        }
62
63
        /** @var $_session Mage_Core_Model_Session */
64
        $_session = Mage::getSingleton('core/session');
65
        $_session->setData('_geoip_file_size', $remote_file_size);
66
67
        $ch = curl_init($this->remote_archive);
68
        $fp = fopen($this->local_archive, 'wb');
69
        curl_setopt($ch, CURLOPT_FILE, $fp);
70
        curl_setopt($ch, CURLOPT_HEADER, 0);
71
        curl_exec($ch);
72
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
73
        if ($httpCode != 200) {
74
            $ret['message'] = $helper->__('Failed to Download Zip');
75
            fclose($fp);
76
            return $ret;
77
        }
78
        curl_close($ch);
79
        fclose($fp);
80
81
        $arch_size = new Zend_Validate_File_Size(array('min' => 1000));
82
83
        if (!$arch_size->isValid($this->local_archive)) {
84
            $ret['message'] = $helper->__('Download failed.');
85
            return $ret;
86
        }
87
        if (!$helper->unGZip($this->local_archive, $this->local_dir)) {
88
            $ret['message'] = $helper->__('UnGzipping failed');
89
            return $ret;
90
        }
91
        $ret['status'] = 'success';
92
        $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
93
        $ret['date'] = Mage::app()->getLocale()->date(filemtime($this->local_file))->toString($format);
94
        return $ret;
95
    }
96
}
97