PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/install-dev/classes/controllerHttp.php

https://github.com/netplayer/PrestaShop
PHP | 449 lines | 229 code | 59 blank | 161 comment | 41 complexity | 9910403f759e4fc127d1660a9c818a81 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  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@prestashop.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 PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. abstract class InstallControllerHttp
  27. {
  28. /**
  29. * @var array List of installer steps
  30. */
  31. protected static $steps = array('welcome', 'license', 'system', 'configure', 'database', 'process');
  32. protected $phone;
  33. protected static $instances = array();
  34. /**
  35. * @var string Current step
  36. */
  37. public $step;
  38. /**
  39. * @var array List of errors
  40. */
  41. public $errors = array();
  42. /**
  43. * @var InstallController
  44. */
  45. public $controller;
  46. /**
  47. * @var InstallSession
  48. */
  49. public $session;
  50. /**
  51. * @var InstallLanguages
  52. */
  53. public $language;
  54. /**
  55. * @var bool If false, disable next button access
  56. */
  57. public $next_button = true;
  58. /**
  59. * @var bool If false, disable previous button access
  60. */
  61. public $previous_button = true;
  62. /**
  63. * @var InstallAbstractModel
  64. */
  65. public $model;
  66. /**
  67. * @var array Magic vars
  68. */
  69. protected $__vars = array();
  70. /**
  71. * Process form to go to next step
  72. */
  73. abstract public function processNextStep();
  74. /**
  75. * Validate current step
  76. */
  77. abstract public function validate();
  78. /**
  79. * Display current step view
  80. */
  81. abstract public function display();
  82. final public static function execute()
  83. {
  84. $session = InstallSession::getInstance();
  85. if (!$session->last_step || $session->last_step == 'welcome')
  86. Tools::generateIndex();
  87. if (Tools::getValue('compile_templates'))
  88. {
  89. require_once (_PS_INSTALL_CONTROLLERS_PATH_.'http/smarty_compile.php');
  90. exit;
  91. }
  92. // Include all controllers
  93. foreach (self::$steps as $step)
  94. {
  95. if (!file_exists(_PS_INSTALL_CONTROLLERS_PATH_.'http/'.$step.'.php'))
  96. throw new PrestashopInstallerException("Controller file 'http/{$step}.php' not found");
  97. require_once _PS_INSTALL_CONTROLLERS_PATH_.'http/'.$step.'.php';
  98. $classname = 'InstallControllerHttp'.$step;
  99. self::$instances[$step] = new $classname($step);
  100. }
  101. if (!$session->last_step || !in_array($session->last_step, self::$steps))
  102. $session->last_step = self::$steps[0];
  103. // Set timezone
  104. if ($session->shop_timezone)
  105. @date_default_timezone_set($session->shop_timezone);
  106. // Get current step (check first if step is changed, then take it from session)
  107. if (Tools::getValue('step'))
  108. {
  109. $current_step = Tools::getValue('step');
  110. $session->step = $current_step;
  111. }
  112. else
  113. $current_step = (isset($session->step)) ? $session->step : self::$steps[0];
  114. if (!in_array($current_step, self::$steps))
  115. $current_step = self::$steps[0];
  116. // Validate all steps until current step. If a step is not valid, use it as current step.
  117. foreach (self::$steps as $check_step)
  118. {
  119. // Do not validate current step
  120. if ($check_step == $current_step)
  121. break;
  122. if (!self::$instances[$check_step]->validate())
  123. {
  124. $current_step = $check_step;
  125. $session->step = $current_step;
  126. $session->last_step = $current_step;
  127. break;
  128. }
  129. }
  130. // Submit form to go to next step
  131. if (Tools::getValue('submitNext'))
  132. {
  133. self::$instances[$current_step]->processNextStep();
  134. // If current step is validated, let's go to next step
  135. if (self::$instances[$current_step]->validate())
  136. $current_step = self::$instances[$current_step]->findNextStep();
  137. $session->step = $current_step;
  138. // Change last step
  139. if (self::getStepOffset($current_step) > self::getStepOffset($session->last_step))
  140. $session->last_step = $current_step;
  141. }
  142. // Go to previous step
  143. else if (Tools::getValue('submitPrevious') && $current_step != self::$steps[0])
  144. {
  145. $current_step = self::$instances[$current_step]->findPreviousStep($current_step);
  146. $session->step = $current_step;
  147. }
  148. self::$instances[$current_step]->process();
  149. self::$instances[$current_step]->display();
  150. }
  151. final public function __construct($step)
  152. {
  153. $this->step = $step;
  154. $this->session = InstallSession::getInstance();
  155. // Set current language
  156. $this->language = InstallLanguages::getInstance();
  157. $detect_language = $this->language->detectLanguage();
  158. if (isset($this->session->lang))
  159. $lang = $this->session->lang;
  160. else
  161. $lang = (isset($detect_language['primarytag'])) ? $detect_language['primarytag'] : false;
  162. if (!in_array($lang, $this->language->getIsoList()))
  163. $lang = 'en';
  164. $this->language->setLanguage($lang);
  165. $this->init();
  166. }
  167. public function init()
  168. {
  169. }
  170. public function process()
  171. {
  172. }
  173. /**
  174. * Get steps list
  175. *
  176. * @return array
  177. */
  178. public function getSteps()
  179. {
  180. return self::$steps;
  181. }
  182. public function getLastStep()
  183. {
  184. return $this->session->last_step;
  185. }
  186. /**
  187. * Find offset of a step by name
  188. *
  189. * @param string $step Step name
  190. * @return int
  191. */
  192. static public function getStepOffset($step)
  193. {
  194. static $flip = null;
  195. if (is_null($flip))
  196. $flip = array_flip(self::$steps);
  197. return $flip[$step];
  198. }
  199. /**
  200. * Make a HTTP redirection to a step
  201. *
  202. * @param string $step
  203. */
  204. public function redirect($step)
  205. {
  206. header('location: index.php?step='.$step);
  207. exit;
  208. }
  209. /**
  210. * Get translated string
  211. *
  212. * @param string $str String to translate
  213. * @param ... All other params will be used with sprintf
  214. * @return string
  215. */
  216. public function l($str)
  217. {
  218. $args = func_get_args();
  219. return call_user_func_array(array($this->language, 'l'), $args);
  220. }
  221. /**
  222. * Find previous step
  223. *
  224. * @param string $step
  225. */
  226. public function findPreviousStep()
  227. {
  228. return (isset(self::$steps[$this->getStepOffset($this->step) - 1])) ? self::$steps[$this->getStepOffset($this->step) - 1] : false;
  229. }
  230. /**
  231. * Find next step
  232. *
  233. * @param string $step
  234. */
  235. public function findNextStep()
  236. {
  237. $nextStep = (isset(self::$steps[$this->getStepOffset($this->step) + 1])) ? self::$steps[$this->getStepOffset($this->step) + 1] : false;
  238. if ($nextStep == 'system' && self::$instances[$nextStep]->validate())
  239. $nextStep = self::$instances[$nextStep]->findNextStep();
  240. return $nextStep;
  241. }
  242. /**
  243. * Check if current step is first step in list of steps
  244. *
  245. * @return bool
  246. */
  247. public function isFirstStep()
  248. {
  249. return self::getStepOffset($this->step) == 0;
  250. }
  251. /**
  252. * Check if current step is last step in list of steps
  253. *
  254. * @return bool
  255. */
  256. public function isLastStep()
  257. {
  258. return self::getStepOffset($this->step) == (count(self::$steps) - 1);
  259. }
  260. /**
  261. * Check is given step is already finished
  262. *
  263. * @param string $step
  264. * @return bool
  265. */
  266. public function isStepFinished($step)
  267. {
  268. return self::getStepOffset($step) < self::getStepOffset($this->getLastStep());
  269. }
  270. /**
  271. * Get telephone used for this language
  272. *
  273. * @return string
  274. */
  275. public function getPhone()
  276. {
  277. if (InstallSession::getInstance()->support_phone != null)
  278. return InstallSession::getInstance()->support_phone;
  279. if ($this->phone === null)
  280. {
  281. $this->phone = $this->language->getInformation('phone', false);
  282. if ($iframe = Tools::file_get_contents('http://api.prestashop.com/iframe/install.php?lang='.$this->language->getLanguageIso(), false, null, 3))
  283. if (preg_match('/<img.+alt="([^"]+)".*>/Ui', $iframe, $matches) && isset($matches[1]))
  284. $this->phone = $matches[1];
  285. }
  286. InstallSession::getInstance()->support_phone = $this->phone;
  287. return $this->phone;
  288. }
  289. /**
  290. * Get link to documentation for this language
  291. *
  292. * Enter description here ...
  293. */
  294. public function getDocumentationLink()
  295. {
  296. return $this->language->getInformation('documentation');
  297. }
  298. /**
  299. * Get link to forum for this language
  300. *
  301. * Enter description here ...
  302. */
  303. public function getForumLink()
  304. {
  305. return $this->language->getInformation('forum');
  306. }
  307. /**
  308. * Get link to blog for this language
  309. *
  310. * Enter description here ...
  311. */
  312. public function getBlogLink()
  313. {
  314. return $this->language->getInformation('blog');
  315. }
  316. /**
  317. * Get link to support for this language
  318. *
  319. * Enter description here ...
  320. */
  321. public function getSupportLink()
  322. {
  323. return $this->language->getInformation('support');
  324. }
  325. public function getDocumentationUpgradeLink()
  326. {
  327. return $this->language->getInformation('documentation_upgrade', true);
  328. }
  329. /**
  330. * Send AJAX response in JSON format {success: bool, message: string}
  331. *
  332. * @param bool $success
  333. * @param string $message
  334. */
  335. public function ajaxJsonAnswer($success, $message = '')
  336. {
  337. die(Tools::jsonEncode(array(
  338. 'success' => (bool)$success,
  339. 'message' => $message,
  340. // 'memory' => round(memory_get_peak_usage()/1024/1024, 2).' Mo',
  341. )));
  342. }
  343. /**
  344. * Display a template
  345. *
  346. * @param string $template Template name
  347. * @param bool $get_output Is true, return template html
  348. * @return string
  349. */
  350. public function displayTemplate($template, $get_output = false, $path = null)
  351. {
  352. if (!$path)
  353. $path = _PS_INSTALL_PATH_.'theme/views/';
  354. if (!file_exists($path.$template.'.phtml'))
  355. throw new PrestashopInstallerException("Template '{$template}.phtml' not found");
  356. if ($get_output)
  357. ob_start();
  358. include($path.$template.'.phtml');
  359. if ($get_output)
  360. {
  361. $content = ob_get_contents();
  362. if (ob_get_level() && ob_get_length() > 0)
  363. ob_end_clean();
  364. return $content;
  365. }
  366. }
  367. public function &__get($varname)
  368. {
  369. if (isset($this->__vars[$varname]))
  370. $ref = &$this->__vars[$varname];
  371. else
  372. {
  373. $null = null;
  374. $ref = &$null;
  375. }
  376. return $ref;
  377. }
  378. public function __set($varname, $value)
  379. {
  380. $this->__vars[$varname] = $value;
  381. }
  382. public function __isset($varname)
  383. {
  384. return isset($this->__vars[$varname]);
  385. }
  386. public function __unset($varname)
  387. {
  388. unset($this->__vars[$varname]);
  389. }
  390. }