1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Paypal
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * PayPal specific model for certificate based authentication
29: */
30: class Mage_Paypal_Model_Cert extends Mage_Core_Model_Abstract
31: {
32: /**
33: * Certificate base path
34: */
35: const BASEPATH_PAYPAL_CERT = 'cert/paypal';
36:
37: /**
38: * Initialize resource model
39: */
40: protected function _construct()
41: {
42: $this->_init('paypal/cert');
43: }
44:
45: /**
46: * Load model by website id
47: *
48: * @param int $websiteId
49: * @param bool $strictLoad
50: * @return Mage_Paypal_Model_Cert
51: */
52: public function loadByWebsite($websiteId, $strictLoad = true)
53: {
54: $this->setWebsiteId($websiteId);
55: $this->_getResource()->loadByWebsite($this, $strictLoad);
56: return $this;
57: }
58:
59: /**
60: * Get path to PayPal certificate file, if file does not exist try to create it
61: *
62: * @return string
63: */
64: public function getCertPath()
65: {
66: if (!$this->getContent()) {
67: Mage::throwException(Mage::helper('paypal')->__('PayPal certificate does not exist.'));
68: }
69:
70: $certFileName = sprintf('cert_%s_%s.pem', $this->getWebsiteId(), strtotime($this->getUpdatedAt()));
71: $certFile = $this->_getBaseDir() . DS . $certFileName;
72:
73: if (!file_exists($certFile)) {
74: $this->_createCertFile($certFile);
75: }
76: return $certFile;
77: }
78:
79: /**
80: * Create physical certificate file based on DB data
81: *
82: * @param string $file
83: */
84: protected function _createCertFile($file)
85: {
86: $certDir = $this->_getBaseDir();
87: if (!is_dir($certDir)) {
88: $ioAdapter = new Varien_Io_File();
89: $ioAdapter->checkAndCreateFolder($certDir);
90: } else {
91: $this->_removeOutdatedCertFile();
92: }
93:
94: file_put_contents($file, Mage::helper('core')->decrypt($this->getContent()));
95: }
96:
97: /**
98: * Check and remove outdated certificate file by website
99: *
100: * @return void
101: */
102: protected function _removeOutdatedCertFile()
103: {
104: $certDir = $this->_getBaseDir();
105: if (is_dir($certDir)) {
106: $entries = scandir($certDir);
107: foreach ($entries as $entry) {
108: if ($entry != '.' && $entry != '..' && strpos($entry, 'cert_' . $this->getWebsiteId()) !== false) {
109: unlink($certDir . DS . $entry);
110: }
111: }
112: }
113: }
114:
115: /**
116: * Retrieve base directory for certificate
117: *
118: * @return string
119: */
120: protected function _getBaseDir()
121: {
122: return Mage::getBaseDir('var') . DS . self::BASEPATH_PAYPAL_CERT;
123: }
124:
125: /**
126: * Delete assigned certificate file after delete object
127: *
128: * @return Mage_Paypal_Model_Cert
129: */
130: protected function _afterDelete()
131: {
132: $this->_removeOutdatedCertFile();
133: return $this;
134: }
135: }
136: