PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/themegenerator/themegenerator.php

http://litepublisher.googlecode.com/
PHP | 326 lines | 264 code | 52 blank | 10 comment | 48 complexity | 52e5bb403ac922b414869e1c4e8b5971 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010 - 2013 Vladimir Yushko http://litepublisher.ru/ http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. class tthemegenerator extends tmenu {
  9. public $colors;
  10. private $type;
  11. private $colorsuploaded;
  12. public static function i($id = 0) {
  13. return self::singleinstance(__class__);
  14. }
  15. protected function create() {
  16. parent::create();
  17. $this->cache = false;
  18. $this->type = 'midle';
  19. $this->data['values'] = array();
  20. $this->data['selectors'] = array();
  21. $this->data['leftview'] = 0;
  22. $this->data['rightview'] = 0;
  23. $this->colors = array();
  24. $this->colorsuploaded = false;
  25. }
  26. public function cron() {
  27. tfiler::callback(array($this, 'deleteold'), litepublisher::$paths->files . 'themegen', false);
  28. }
  29. public function deleteold($filename) {
  30. $filename = litepublisher::$paths->files . 'themegen' . DIRECTORY_SEPARATOR . $filename;
  31. if (@filectime ($filename) + 24*3600 < time()) unlink($filename);
  32. }
  33. public function getres() {
  34. return litepublisher::$paths->plugins . 'themegenerator' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR;
  35. }
  36. public function parseselectors() {
  37. $this->data['selectors'] = array();
  38. $s = file_get_contents($this->res . 'scheme.tml');
  39. $lines = explode("\n", str_replace(array("\r\n", "\r"), "\n", trim($s)));
  40. foreach ($lines as $line) {
  41. $line = trim($line);
  42. if ($line == '') continue;
  43. $css = explode('{', $line);
  44. $sel = rtrim($css[0]);
  45. $proplist = explode(';', trim($css[1], '{}; '));
  46. $props = array();
  47. foreach ($proplist as $v) {
  48. $v =trim($v, '; ');
  49. if ($v == '') continue;
  50. $prop = explode(':', $v);
  51. $propname = trim($prop[0]);
  52. $propvalue =trim($prop[1]);
  53. if (preg_match_all('/%%(\w*+)%%/', $propvalue, $m, PREG_SET_ORDER)) {
  54. foreach ($m as $item) {
  55. $this->data['selectors'][] = array(
  56. 'name' => $item[1],
  57. 'sel' => $sel,
  58. 'propname' => $propname,
  59. 'value' => $propvalue
  60. );
  61. }
  62. }
  63. }
  64. }
  65. $this->save();
  66. }
  67. public function gethead() {
  68. $pickerpath = litepublisher::$site->files . '/plugins/colorpicker/';
  69. $result = '<link type="text/css" href="' . $pickerpath . 'css/colorpicker.css" rel="stylesheet" />';
  70. $template = ttemplate::i();
  71. $template->ltoptions['colors'] = $this->data['selectors'];
  72. $result .= $template->getjavascript($template->jsmerger_themegenerator);
  73. //$result .= $template->getjavascript('/plugins/colorpicker/js/colorpicker.js');
  74. //$result .= $template->getjavascript('/js/swfupload/swfupload.js');
  75. //$result .= $template->getjavascript('/plugins/themegenerator/themegenerator.js');
  76. if ($this->colorsuploaded) {
  77. $args = new targs();
  78. foreach ($this->data['colors'] as $name => $value) {
  79. $args->$name = $value;
  80. }
  81. $res = $this->res;
  82. $css = strtr(file_get_contents($res . 'scheme.tml'), $args->data);
  83. $result .= "<style type=\"text/css\">\n$css</style>\n";
  84. }
  85. return $result;
  86. }
  87. public function getidview() {
  88. switch ($this->type) {
  89. case 'left':
  90. return$this->leftview;
  91. case 'right':
  92. return $this->rightview;
  93. default:
  94. return parent::getidview();
  95. }
  96. }
  97. public function request($arg) {
  98. //$this->parseselectors();
  99. if (isset($_GET['type'])) {
  100. $this->type = trim($_GET['type']) == 'left' ? 'left': 'right';
  101. }
  102. tlocal::usefile('themegenerator');
  103. $lang = tlocal::i('themegenerator');
  104. $this->colors = $lang->ini['themecolors'];
  105. parent::request($arg);
  106. if (isset($_POST['formtype']) && (($_POST['formtype'] == 'headerurl') || ($_POST['formtype'] == 'logourl'))) return $this->formresult;
  107. }
  108. public function setcolor($name, $value) {
  109. if (isset($this->colors[$name])) {
  110. $value = trim($value);
  111. if (strend($name, 'url') || preg_match('/^[0-9a-zA-Z]*+$/', $value)) {
  112. $this->colors[$name] = $value;
  113. }
  114. }
  115. }
  116. public function processform() {
  117. switch ($_POST['formtype']) {
  118. case 'colors':
  119. foreach ($_POST as $name => $value) {
  120. if (strbegin($name, 'color_')) {
  121. $name = substr($name, strlen('color_'));
  122. $this->setcolor($name, $value);
  123. }
  124. }
  125. $this->sendfile();
  126. break;
  127. case 'uploadcolors':
  128. if (isset($_FILES['filename'])) {
  129. if (isset($_FILES['filename']['error']) && $_FILES['filename']['error'] > 0) {
  130. $lang = tlocal::admin('uploaderrors');
  131. return sprintf('<h4>%s</h4>', $lang->__get($_FILES['filename']['error']));
  132. } elseif (!is_uploaded_file($_FILES['filename']['tmp_name'])) {
  133. return sprintf('<h4>%s</h4>', $lng['attack']);
  134. } else {
  135. $this->colorsuploaded = true;
  136. $colors = parse_ini_file($_FILES['filename']['tmp_name']);
  137. foreach ($colors as $name => $value) {
  138. $this->setcolor($name, $value);
  139. }
  140. }
  141. }
  142. break;
  143. case 'headerurl':
  144. if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) ||
  145. $_FILES['Filedata']['error'] != 0) return 403;
  146. if ($result = $this->imageresize($_FILES['Filedata']['name'], $_FILES['Filedata']['tmp_name'], $this->colors['headerwidth'], $this->colors['headerheight'])) {
  147. return turlmap::htmlheader(false) . $result;
  148. }
  149. return 403;
  150. case 'logourl':
  151. if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) ||
  152. $_FILES['Filedata']['error'] != 0) return 403;
  153. if ($result = $this->uploadlogo($_FILES['Filedata']['name'], $_FILES['Filedata']['tmp_name'], $this->colors['logopadding'], $this->colors['logoheight'])) {
  154. return turlmap::htmlheader(false) . json_encode($result);
  155. }
  156. return 403;
  157. }
  158. return '';
  159. }
  160. public function sendfile() {
  161. $themename = isset($_POST['themename']) ? trim($_POST['themename']) : '';
  162. if ($themename != '') $themename = tlinkgenerator::i()->filterfilename($themename);
  163. if ($themename == '') $themename = time();
  164. $path = "themes/generator-$themename/";
  165. litepublisher::$classes->include_file(litepublisher::$paths->libinclude . 'zip.lib.php');
  166. $zip = new zipfile();
  167. $themedir = litepublisher::$paths->plugins . 'themegenerator' .DIRECTORY_SEPARATOR . $this->type . DIRECTORY_SEPARATOR;
  168. $args = new targs();
  169. $colors = "[themecolors]\nthemename = \"$themename\"\n";
  170. foreach ($this->colors as $name => $value) {
  171. $colors .= "$name = \"$value\"\n";
  172. $args->$name = $value;
  173. }
  174. foreach (array('headerurl', 'logourl') as $name) {
  175. if (strbegin($this->colors[$name], 'http://')) {
  176. $basename = substr($this->colors[$name], strrpos($this->colors[$name], '/') + 1);
  177. $filename = litepublisher::$paths->files . 'themegen' . DIRECTORY_SEPARATOR . $basename;
  178. $zip->addFile(file_get_contents($filename), $path . 'images/' . $basename);
  179. $args->$name = 'images/' . $basename;
  180. }
  181. }
  182. $res = $this->res;
  183. $css = strtr(tfilestorage::getfile($res . 'scheme.tml'), $args->data);
  184. $zip->addFile($colors, $path . 'colors.ini');
  185. $filelist = tfiler::getfiles($themedir);
  186. foreach ($filelist as $filename) {
  187. $content = tfilestorage::getfile($themedir . $filename);
  188. switch ($filename) {
  189. case 'style.css':
  190. $content .= $css;
  191. break;
  192. case 'about.ini':
  193. $content = str_replace('name = generator', "name = generator-$themename", $content);
  194. break;
  195. }
  196. $zip->addFile($content, $path . $filename);
  197. }
  198. $result = $zip->file();
  199. if (ob_get_level()) @ob_end_clean ();
  200. header('HTTP/1.1 200 OK', true, 200);
  201. header('Content-type: application/octet-stream');
  202. header('Content-Disposition: attachment; filename=generator.theme.' . $themename . '.zip');
  203. header('Content-Length: ' .strlen($result));
  204. header('Last-Modified: ' . date('r'));
  205. Header( 'Cache-Control: no-cache, must-revalidate');
  206. Header( 'Pragma: no-cache');
  207. echo $result;
  208. exit();
  209. }
  210. public function imageresize($name, $filename, $width, $height) {
  211. if (!($source = tmediaparser::readimage($filename))) return false;
  212. $sourcex = imagesx($source);
  213. $sourcey = imagesy($source);
  214. if ($height == $sourcey) {
  215. if (!($result = tmediaparser::move_uploaded($name, $filename, 'themegen'))) return false;
  216. @chmod(litepublisher::$paths->files . str_replace('/', DIRECTORY_SEPARATOR, $result), 0666);
  217. return litepublisher::$site->files . '/files/' . $result;
  218. }
  219. $result = tmediaparser::prepare_filename($name, 'themegen');
  220. $realfilename = litepublisher::$paths->files . str_replace('/', DIRECTORY_SEPARATOR, $result);
  221. $x = ceil($sourcex * ($height / $sourcey ));
  222. $dest = imagecreatetruecolor($x, $height);
  223. imagecopyresampled($dest, $source, 0, 0, 0, 0, $x, $height, $sourcex, $sourcey);
  224. switch (substr($result, strrpos($result, '.')+ 1)) {
  225. case 'jpg':
  226. imagejpeg($dest, $realfilename, 100);
  227. break;
  228. case 'png':
  229. imagepng($dest, $realfilename);
  230. break;
  231. case 'gif':
  232. imagegif($dest, $realfilename);
  233. break;
  234. default:
  235. $realfilename .= '.jpg';
  236. $result .= '.jpg';
  237. imagejpeg($dest, $realfilename, 100);
  238. }
  239. imagedestroy($dest);
  240. imagedestroy($source);
  241. @chmod($realfilename, 0666);
  242. return litepublisher::$site->files . '/files/'. $result;
  243. }
  244. public function uploadlogo($name, $filename, $padding, $height) {
  245. if (!($source = tmediaparser::readimage($filename))) return false;
  246. $sourcex = imagesx($source);
  247. $sourcey = imagesy($source);
  248. if ($height == $sourcey) {
  249. if (!($result = tmediaparser::move_uploaded($name, $filename, 'themegen'))) return false;
  250. @chmod(litepublisher::$paths->files . str_replace('/', DIRECTORY_SEPARATOR, $result), 0666);
  251. return array(
  252. 'url' => litepublisher::$site->files . '/files/' . $result,
  253. 'width' => $sourcex + $padding,
  254. );
  255. }
  256. $result = tmediaparser::prepare_filename($name, 'themegen');
  257. $realfilename = litepublisher::$paths->files . str_replace('/', DIRECTORY_SEPARATOR, $result);
  258. $x = ceil($sourcex * ($height / $sourcey ));
  259. $dest = imagecreatetruecolor($x, $height);
  260. imagealphablending( $dest, false );
  261. imagesavealpha( $dest, true );
  262. $transparent = imagecolorallocatealpha($dest, 255, 255, 255, 127);
  263. imagefilledrectangle($dest, 0, 0, $x, $height, $transparent);
  264. imagecopyresampled($dest, $source, 0, 0, 0, 0, $x, $height, $sourcex, $sourcey);
  265. imagepng($dest, $realfilename);
  266. imagedestroy($dest);
  267. imagedestroy($source);
  268. @chmod($realfilename, 0666);
  269. return array(
  270. 'url'=> litepublisher::$site->files . '/files/'. $result,
  271. 'width' => $x + $padding,
  272. );
  273. }
  274. }//class