DTwigManager.php (13271B)
1 <?php 2 3 /** 4 * Opencart Extension class. 5 * 6 * This class is used by Opencart as a twig extension and must not be used directly. 7 * 8 */ 9 class Twig_Extension_DTwigManager extends Twig_Extension 10 { 11 protected $registry; 12 protected $is_admin; 13 14 /** 15 * @param Registry $registry 16 */ 17 public function __construct(\Registry $registry) 18 { 19 $this->registry = $registry; 20 21 $this->is_admin = defined('DIR_CATALOG'); 22 } 23 24 /** 25 * {@inheritdoc} 26 */ 27 public function getFunctions() 28 { 29 return array( 30 new \Twig_SimpleFunction('link', array($this, 'linkFunction')), 31 new \Twig_SimpleFunction('lang', array($this, 'langFunction')), 32 new \Twig_SimpleFunction('config', array($this, 'configFunction')), 33 new \Twig_SimpleFunction('paginate', array($this, 'paginateFunction')), 34 new \Twig_SimpleFunction('image', array($this, 'imageFunction')), 35 new \Twig_SimpleFunction('asset', array($this, 'assetFunction')), 36 new \Twig_SimpleFunction('load', array($this, 'loadFunction')), 37 new \Twig_SimpleFunction('can_access', array($this, 'canAccessFunction')), 38 new \Twig_SimpleFunction('can_modify', array($this, 'canModifyFunction')), 39 new \Twig_SimpleFunction('template', array($this, 'templateFunction')), 40 new \Twig_SimpleFunction('content', array($this, 'contentFunction')), 41 ); 42 } 43 44 /** 45 * @param null $route 46 * @param array $args 47 * @param bool $secure 48 * 49 * @return string 50 */ 51 public function linkFunction($route = null, $args = array(), $secure = false) 52 { 53 $url = $this->registry->get('url'); 54 $session = $this->registry->get('session'); 55 $token = isset($session->data['token']) ? $session->data['token'] : null; 56 57 if ($this->is_admin && $token) { 58 $args['token'] = $token; 59 } 60 61 if (is_array($args)) { 62 $args = http_build_query($args); 63 } 64 65 if (!empty($route)) { 66 return $url->link($route, $args, $secure); 67 } else if ($secure) { 68 return !empty($args) ? HTTPS_SERVER . 'index.php?' . $args : HTTPS_SERVER; 69 } 70 71 return !empty($args) ? HTTP_SERVER . 'index.php?' . $args : HTTP_SERVER; 72 } 73 74 /** 75 * @param $key 76 * @param null $file 77 * 78 * @return mixed 79 */ 80 public function langFunction($key, $file = null) 81 { 82 $language = $this->registry->get('language'); 83 84 if ($file) { 85 $language->load($file); 86 } 87 88 return $language->get($key); 89 } 90 91 /** 92 * @param $key 93 * @param null $file 94 * 95 * @return mixed 96 */ 97 public function configFunction($key, $file = null) 98 { 99 $config = $this->registry->get('config'); 100 101 if ($file) { 102 $config->load($file); 103 } 104 105 return $config->get($key); 106 } 107 108 /** 109 * @param $value 110 * 111 * @return bool 112 */ 113 public function canAccessFunction($value) 114 { 115 $user = $this->registry->get('user'); 116 117 if ($user) { 118 return $user->hasPermission('access', $value); 119 } 120 121 return false; 122 } 123 124 /** 125 * @param $value 126 * 127 * @return bool 128 */ 129 public function canModifyFunction($value) 130 { 131 $user = $this->registry->get('user'); 132 133 if ($user) { 134 return $user->hasPermission('modify', $value); 135 } 136 137 return false; 138 } 139 140 /** 141 * @param $filename 142 * @param string $context 143 * 144 * @param null $width 145 * @param null $height 146 * 147 * @return string|void 148 */ 149 public function imageFunction($filename, $context = 'product', $width = null, $height = null) 150 { 151 if (!is_file(DIR_IMAGE . $filename)) { 152 return; 153 } 154 155 $request = $this->registry->get('request'); 156 $config = $this->registry->get('config'); 157 158 if (!$width) { 159 $width = $config->get('config_image_' . $context . '_width'); 160 } 161 162 if (!$height) { 163 $height = $config->get('config_image_' . $context . '_height'); 164 } 165 166 $extension = pathinfo($filename, PATHINFO_EXTENSION); 167 168 $old_image = $filename; 169 $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; 170 171 if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image))) { 172 $path = ''; 173 174 $directories = explode('/', dirname(str_replace('../', '', $new_image))); 175 176 foreach ($directories as $directory) { 177 $path = $path . '/' . $directory; 178 179 if (!is_dir(DIR_IMAGE . $path)) { 180 @mkdir(DIR_IMAGE . $path, 0777); 181 } 182 } 183 184 list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); 185 186 if ($width_orig != $width || $height_orig != $height) { 187 $image = new Image(DIR_IMAGE . $old_image); 188 $image->resize($width, $height); 189 $image->save(DIR_IMAGE . $new_image); 190 } else { 191 copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); 192 } 193 } 194 195 if ($request->server['HTTPS']) { 196 return $this->is_admin ? HTTPS_CATALOG . 'image/' . $new_image : HTTPS_SERVER . 'image/' . $new_image; 197 } else { 198 return $this->is_admin ? HTTP_CATALOG . 'image/' . $new_image : HTTP_SERVER . 'image/' . $new_image; 199 } 200 } 201 202 /** 203 * @param $path 204 * 205 * @return string 206 */ 207 public function assetFunction($path) 208 { 209 if (!$this->is_admin) { 210 if (file_exists(DIR_TEMPLATE . $this->registry->get('config')->get('config_template') . '/' . $path)) { 211 return 'catalog/view/theme/' . $this->registry->get('config')->get('config_template') . '/' . $path; 212 } else if (file_exists(DIR_TEMPLATE . 'default/' . $path)) { 213 return 'catalog/view/theme/default/' . $path; 214 } 215 } else if (file_exists(DIR_TEMPLATE . '../' . $path)) { 216 return 'admin/view/' . $path; 217 } 218 219 return $path; 220 } 221 222 /** 223 * @param $file 224 * 225 * @return mixed 226 */ 227 public function loadFunction($file) 228 { 229 $loader = $this->registry->get('load'); 230 231 return $loader->controller($file); 232 } 233 234 /** 235 * @param $total 236 * @param null $route 237 * @param array $args 238 * @param null $template 239 * 240 * @return string 241 */ 242 public function paginateFunction($total, $limit = 5, $route = null, $args = array(), $template = null) 243 { 244 $request = $this->registry->get('request'); 245 $page = isset($request->get['page']) ? $request->get['page'] : 1; 246 $secure = $request->server['HTTPS']; 247 248 $pagination = new \Pagination(); 249 $pagination->total = $total; 250 $pagination->page = $page; 251 $pagination->limit = $limit; 252 253 $args['page'] = '{page}'; 254 255 $pagination->url = $this->linkFunction($route, $args, $secure); 256 257 if ($template) { 258 $loader = $this->registry->get('load'); 259 260 return $loader->view($template, get_object_vars($pagination)); 261 } else { 262 return $pagination->render(); 263 } 264 } 265 266 267 public function contentFunction($view, $extension_file = 'twig') 268 { 269 return file_get_contents(DIR_TEMPLATE . $this->templateFunction($view, $extension_file)); 270 } 271 272 public function templateFunction($view, $extension_file = 'twig') 273 { 274 if (!$this->is_admin) { 275 $config = $this->registry->get('config'); 276 if ($config->get('config_theme') == 'default') { 277 $theme = $config->get('theme_default_directory'); 278 } else { 279 $theme = $config->get('config_theme'); 280 } 281 282 if (!$theme) { 283 $theme = $config->get('config_template'); 284 } 285 286 if (is_file(DIR_TEMPLATE . $theme . '/template/' . $view . '.' . $extension_file)) { 287 $view = $theme . '/template/' . $view . '.' . $extension_file; 288 } elseif (is_file(DIR_TEMPLATE . 'default/template/' . $view . '.' . $extension_file)) { 289 $view = 'default/template/' . $view . '.' . $extension_file; 290 } else { 291 $view = 'default/template/partial/d_empty.twig'; 292 } 293 } else { 294 $view .= $extension_file; 295 } 296 return $view; 297 } 298 299 /** 300 * {@inheritdoc} 301 */ 302 public function getFilters() 303 { 304 return array( 305 new \Twig_SimpleFilter('money', array($this, 'moneyFilter')), 306 new \Twig_SimpleFilter('tax', array($this, 'taxFilter')), 307 new \Twig_SimpleFilter('len', array($this, 'lenFilter')), 308 new \Twig_SimpleFilter('wei', array($this, 'weiFilter')), 309 new \Twig_SimpleFilter('truncate', array($this, 'truncateFilter')), 310 new \Twig_SimpleFilter('encrypt', array($this, 'encryptFilter')), 311 new \Twig_SimpleFilter('decrypt', array($this, 'decryptFilter')), 312 ); 313 } 314 315 /** 316 * @param $number 317 * @param string $currency 318 * @param string $value 319 * @param bool $format 320 * 321 * @return mixed 322 */ 323 public function moneyFilter($number, $currency = '', $value = '', $format = true) 324 { 325 $lib = $this->registry->get('currency'); 326 327 return $lib->format($number, $currency, $value, $format); 328 } 329 330 /** 331 * @param $value 332 * @param $tax_class_id 333 * @param bool $calculate 334 * 335 * @return mixed 336 */ 337 public function taxFilter($value, $tax_class_id, $calculate = true) 338 { 339 $tax = $this->registry->get('tax'); 340 341 return $tax->calculate($value, $tax_class_id, $calculate); 342 } 343 344 /** 345 * @param $value 346 * @param $length_class_id 347 * @param string $decimal_point 348 * @param string $thousand_point 349 * 350 * @return mixed 351 */ 352 public function lenFilter($value, $length_class_id, $decimal_point = '.', $thousand_point = ',') 353 { 354 $length = $this->registry->get('length'); 355 356 return $length->format($value, $length_class_id, $decimal_point, $thousand_point); 357 } 358 359 /** 360 * @param $value 361 * @param $weight_class_id 362 * @param string $decimal_point 363 * @param string $thousand_point 364 * 365 * @return mixed 366 */ 367 public function weiFilter($value, $weight_class_id, $decimal_point = '.', $thousand_point = ',') 368 { 369 $weight = $this->registry->get('weight'); 370 371 return $weight->format($value, $weight_class_id, $decimal_point, $thousand_point); 372 } 373 374 /** 375 * @param $value 376 * @param string $end 377 * @param null $limit 378 * 379 * @return string 380 */ 381 public function truncateFilter($value, $end = '...', $limit = null) 382 { 383 $config = $this->registry->get('config'); 384 385 if (!$limit) { 386 $limit = $config->get('config_product_description_length'); 387 } 388 389 $str = strip_tags(html_entity_decode($value, ENT_QUOTES, 'UTF-8')); 390 391 if (strlen($str) > $limit) { 392 $str = utf8_substr($str, 0, $limit) . $end; 393 } 394 395 return $str; 396 } 397 398 /** 399 * @param $value 400 * 401 * @return string 402 */ 403 public function encryptFilter($value) 404 { 405 $config = $this->registry->get('config'); 406 407 $encription = new \Encryption($config->get('config_encription')); 408 409 return $encription->encrypt($value); 410 } 411 412 /** 413 * @param $value 414 * 415 * @return string 416 */ 417 public function decryptFilter($value) 418 { 419 $config = $this->registry->get('config'); 420 421 $encription = new \Encryption($config->get('config_encription')); 422 423 return $encription->decrypt($value); 424 } 425 426 /** 427 * {@inheritdoc} 428 */ 429 public function getGlobals() 430 { 431 $document = $this->registry->get('document'); 432 433 $globals = array( 434 'document_title' => $document->getTitle(), 435 'document_description' => $document->getDescription(), 436 'document_keywords' => $document->getKeywords(), 437 'document_links' => $document->getLinks(), 438 'document_styles' => $document->getStyles(), 439 'document_scripts' => $document->getScripts(), 440 'route' => isset($this->registry->get('request')->get['route']) ? $this->registry->get('request') : '', 441 ); 442 443 if ($this->is_admin) { 444 $user = $this->registry->get('user'); 445 $globals['user'] = $user; 446 $globals['is_logged'] = $user->isLogged(); 447 } else { 448 $customer = $this->registry->get('customer'); 449 $globals['customer'] = $customer; 450 $globals['is_logged'] = $customer->isLogged(); 451 452 $globals['cart'] = $this->registry->get('cart'); 453 } 454 455 return $globals; 456 } 457 458 /** 459 * {@inheritdoc} 460 */ 461 public function getName() 462 { 463 return 'opencart'; 464 } 465 466 }