Changeset 1760657
- Timestamp:
- 11/08/2017 10:05:53 AM (8 years ago)
- Location:
- fv-community-news/trunk/fvcn-includes
- Files:
-
- 4 edited
-
fvcn-core-classes.php (modified) (2 diffs)
-
fvcn-core-sync.php (modified) (4 diffs)
-
fvcn-core-validate.php (modified) (1 diff)
-
fvcn-post-template.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
fv-community-news/trunk/fvcn-includes/fvcn-core-classes.php
r595527 r1760657 95 95 96 96 /** 97 * getCrypt()98 *99 * @version 20120709100 * @return FvCommunityNews_Crypt101 */102 public function getCrypt()103 {104 if (isset($this->_objects['crypt'])) {105 return $this->_objects['crypt'];106 }107 108 return $this->_objects['crypt'] = new FvCommunityNews_Crypt( fvcn_get_option('_fvcn_sync_key') );109 }110 111 /**112 97 * getJavascript() 113 98 * … … 166 151 } 167 152 168 return $this->_objects['sync'] = new FvCommunityNews_Sync( $this->getCrypt());153 return $this->_objects['sync'] = new FvCommunityNews_Sync(); 169 154 } 170 155 -
fv-community-news/trunk/fvcn-includes/fvcn-core-sync.php
r595527 r1760657 13 13 if (!defined('ABSPATH')) { 14 14 die('Direct access is not allowed!'); 15 }16 17 18 /**19 * FvCommunityNews_Crypt20 *21 * Rijndael 128 bit encryption.22 *23 * @author Frank Verhoeven <info@frank-verhoeven.com>24 */25 class FvCommunityNews_Crypt26 {27 const CIPHER = MCRYPT_RIJNDAEL_128;28 const MODE = MCRYPT_MODE_CBC;29 30 /**31 * @var string32 */33 protected $_key = null;34 35 /**36 * @var string37 */38 protected $_iv = null;39 40 /**41 * __construct()42 *43 * @version 2012070144 * @param string $key45 * @param string $iv46 */47 public function __construct($key, $iv=null)48 {49 $this->setKey($key);50 51 if (null !== $iv) {52 $this->setIv($iv);53 }54 }55 56 /**57 * canEncrypt()58 *59 * @version 2012070160 * @return bool61 */62 public function canEncrypt()63 {64 return extension_loaded('mcrypt');65 }66 67 /**68 * setKey()69 *70 * @version 2012070171 * @param string $key72 * @return FvCommunityNews_Crypt73 */74 public function setKey($key)75 {76 $this->_key = (string) $key;77 return $this;78 }79 80 /**81 * getKey()82 *83 * @version 2012070184 * @return string85 */86 public function getKey()87 {88 if (strlen($this->_key) > mcrypt_get_key_size(self::CIPHER, self::MODE)) {89 return substr($this->_key, 1, mcrypt_get_key_size(self::CIPHER, self::MODE));90 }91 92 return $this->_key;93 }94 95 /**96 * createIv()97 *98 * @version 2012070199 * @return string100 */101 public function createIv()102 {103 $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER, self::MODE), MCRYPT_RAND);104 105 if (false === $iv) {106 throw new Exception('Failed to create an initialization vector.');107 }108 109 return $this->setIv( $iv )->getIv();110 }111 112 /**113 * setIv()114 *115 * @version 20120701116 * @param string $iv117 * @return FvCommunityNews_Crypt118 */119 public function setIv($iv)120 {121 if (0 !== mcrypt_get_iv_size(self::CIPHER, self::MODE) && strlen($iv) != mcrypt_get_iv_size(self::CIPHER, self::MODE)) {122 throw new Exception('Invallid IV size.');123 }124 125 $this->_iv = $iv;126 return $this;127 }128 129 /**130 * getIv()131 *132 * @version 20120701133 * @return string134 */135 public function getIv()136 {137 if (null !== $this->_iv) {138 return $this->_iv;139 }140 141 return $this->createIv();142 }143 144 /**145 * encrypt()146 *147 * @version 20120701148 * @param string $value149 * @return string150 */151 public function encrypt($value)152 {153 $encrypted = mcrypt_encrypt(self::CIPHER, $this->getKey(), trim($value), self::MODE, $this->getIv());154 155 return base64_encode( $encrypted );156 }157 158 /**159 * decrypt()160 *161 * @version 20120701162 * @param string $value163 * @return string164 */165 public function decrypt($value)166 {167 $decrypted = mcrypt_decrypt(self::CIPHER, $this->getKey(), base64_decode($value), self::MODE, $this->getIv());168 169 return rtrim($decrypted, "\0\4");170 }171 15 } 172 16 … … 201 45 */ 202 46 protected $_registered = false; 203 204 /** 205 * @var FvCommunityNews_Crypt 206 */ 207 protected $_crypt = null; 208 47 209 48 /** 210 49 * __construct() 211 50 * 212 51 * @version 20120716 213 * @param FvCommunityNews_Crypt $crypt 214 */ 215 public function __construct(FvCommunityNews_Crypt $crypt) 216 { 217 $this->_crypt = $crypt; 52 */ 53 public function __construct() 54 { 218 55 $this->_setupOptions(); 219 56 … … 285 122 protected function _encryptData(array $data, $root=true) 286 123 { 287 if (!$this->_crypt->canEncrypt()) {288 if ($root) {289 $data['encrypted'] = false;290 }291 292 return $data;293 }294 295 foreach ($data as $key=>$val) {296 if (is_array($val)) {297 $data[ $key ] = $this->_encryptData($val, false);298 } else {299 $data[ $key ] = $this->_crypt->encrypt($val);300 }301 }302 303 if ($root) {304 $data['encrypted'] = true;305 $data['validator'] = $this->_crypt->encrypt( home_url('/') );306 $data['iv'] = base64_encode( $this->_crypt->getIv() );307 }308 309 124 return $data; 310 125 } … … 385 200 return $this; 386 201 } 387 388 /**389 * increasePostViewCount()390 *391 * @version 20120712392 * @paranint $postId393 * @return FvCommunityNews_Sync394 */202 203 /** 204 * increasePostViewCount() 205 * 206 * @version 20120712 207 * @param int $postId 208 * @return FvCommunityNews_Sync 209 */ 395 210 public function increasePostViewCount($postId) 396 211 { -
fv-community-news/trunk/fvcn-includes/fvcn-core-validate.php
r595527 r1760657 392 392 public function isValid($value) 393 393 { 394 $crypt = new FvCommunityNews_Crypt( hash('sha256', wp_create_nonce('fvcn-post-form-time-key')) ); 395 if ($crypt->canEncrypt()) { 396 $value = explode(':', $value); 397 if (2 != count($value)) { 398 return false; 399 } 400 401 try { 402 $time = (int) $crypt->setIv( base64_decode($value[0]) )->decrypt($value[1]); 403 } catch (Exception $e) { 404 return false; 405 } 406 } else { 407 $time = (int) base64_decode($value); 408 } 409 394 $time = (int) base64_decode($value); 395 410 396 // min 15 sec, max 1 hour 411 397 if ($time+15 > time() || time()-3600 > $time) { -
fv-community-news/trunk/fvcn-includes/fvcn-post-template.php
r603255 r1760657 1244 1244 <input type="hidden" name="fvcn_post_form_action" id="fvcn_post_form_action" value="fvcn-new-post" /> 1245 1245 <?php wp_nonce_field('fvcn-new-post', 'fvcn_post_form_nonce'); ?> 1246 1247 <?php 1248 $crypt = new FvCommunityNews_Crypt( hash('sha256', wp_create_nonce('fvcn-post-form-time-key')) ); 1249 1250 if ($crypt->canEncrypt()) { 1251 $value = base64_encode($crypt->getIv()) . ':' . $crypt->encrypt( time() ); 1252 } else { 1253 $value = base64_encode( time() ); 1254 } 1255 ?> 1256 1246 <?php $value = base64_encode( time() ); ?> 1257 1247 <input type="hidden" name="fvcn_post_form_time_key" id="fvcn_post_form_time_key" value="<?php echo $value; ?>" /> 1258 1248
Note: See TracChangeset
for help on using the changeset viewer.