PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/WordPress/WordPress/index.php

http://github.com/Interop-Bridges/Windows-Azure-PHP-Scaffolders
PHP | 336 lines | 163 code | 51 blank | 122 comment | 12 complexity | 7de59b6d8f153f5485e055e9bbb10698 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright 2011 Microsoft Corporation
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. /*
  15. * HOW TO USE THIS FILE
  16. *
  17. * There are only two methods in this file you should need to update, parameters()
  18. * and doWork().
  19. *
  20. * Follow the example in parameters() to add all the required and optional
  21. * command line parameters your scaffold requires.
  22. *
  23. * This scaffold automagically extracts the content and updates your config file,
  24. * however if there are any extra steps you need to take you should do so in
  25. * the doWork() method. This could include work such as downloading archives
  26. * or configuring files.
  27. *
  28. * **** HOW TO CHANGE THE NAME OF YOUR SCAFFOLD ****
  29. * There are several steps required to change the name of a scaffold.
  30. * - Rename the scaffold folder to the name you desire
  31. * - Change the @command-handler in this file to the name you desire
  32. * - Change the class name in this file to the name you desire
  33. */
  34. set_time_limit(600);
  35. define("WP_URL", "http://wordpress.org/wordpress-3.3.1.zip");
  36. define("DB_ABSTRACTION_URL", "http://downloads.wordpress.org/plugin/wordpress-database-abstraction.1.1.4.zip");
  37. define("WAZ_STORAGE_URL", "http://downloads.wordpress.org/plugin/windows-azure-storage.zip");
  38. require_once('Params.class.php');
  39. require_once('FileSystem.class.php');
  40. /**
  41. * @command-handler WordPress
  42. */
  43. class WordPress
  44. extends Microsoft_WindowsAzure_CommandLine_PackageScaffolder_PackageScaffolderAbstract {
  45. // this should be in parent
  46. protected $p;
  47. /**
  48. * Full path to Document Root
  49. * @var String
  50. */
  51. protected $mAppRoot;
  52. /**
  53. * Path to scaffolder file
  54. * @var String
  55. */
  56. protected $mScaffolder;
  57. protected $mRootPath;
  58. /**
  59. * This method controls all the command line parameters you need for
  60. * the scaffold. Set them here to ensure their values are used in your
  61. * ServiceConfiguration.cscfg file, also all values can be accessed
  62. * via $this->p->get(param_name).
  63. *
  64. * Adding a parameter is done with the following structure:
  65. * $this->p->add('cmd_param_name', required(true|false), default value, help message string);
  66. */
  67. public function parameters() {
  68. $this->p = new Params(); // Do not remove this line
  69. /*
  70. * Example of a command line parameter
  71. *
  72. * $this->p->add('cmd_param_name', required(true|false), default value, help message string);
  73. */
  74. $this->p->add('diagnosticsConnectionString', false, 'UseDevelopmentStorage=true', 'Connections string to storage for diagnostics');
  75. $this->p->add('DB_NAME', true, '', 'Name of database to store WordPress data in');
  76. $this->p->add('DB_USER', true, '', 'User account name with permissions to the WordPress database');
  77. $this->p->add('DB_PASSWORD', true, '', 'Password of account with permissions to the WordPress database');
  78. $this->p->add('DB_HOST', true, '', 'URL to database host');
  79. $this->p->add('DB_TYPE', false, 'sqlsrv', 'Database driver to use');
  80. $this->p->add('DB_CHARSET', false, 'utf8', 'Database character set');
  81. $this->p->add('DB_COLLATE', false, '', 'Database collation');
  82. $this->p->add('AUTH_KEY', false, uniqid(), 'Auth key');
  83. $this->p->add('SECURE_AUTH_KEY', false, uniqid(), 'Secure auth key');
  84. $this->p->add('LOGGED_IN_KEY', false, uniqid(), 'Logged in key');
  85. $this->p->add('NONCE_KEY', false, uniqid(), 'Nonce in salt');
  86. $this->p->add('LOGGED_IN_SALT', false, uniqid(), 'Logged in salt');
  87. $this->p->add('NONCE_SALT', false, uniqid(), 'Nonce salt');
  88. $this->p->add('AUTH_SALT', false, uniqid(), 'Auth salt');
  89. $this->p->add('SECURE_AUTH_SALT', false, uniqid(), 'Nonce salt');
  90. $this->p->add('DB_TABLE_PREFIX', false, 'wp_', 'WordPress table prefix');
  91. $this->p->add('WPLANG', false, '', 'WordPress language');
  92. $this->p->add('WP_DEBUG', false, 'false', 'WordPress debugging flag');
  93. $this->p->add('SAVEQUERIES', false, 'false', 'Save queries');
  94. $this->p->add('RELOCATE', false, 'false', 'Relocate WordPress installation');
  95. $this->p->add('WP_ALLOW_MULTISITE', false, 'false', 'WordPress Multisite');
  96. $this->p->add('MULTISITE', false, 'false', 'WordPress Multisite');
  97. $this->p->add('SUBDOMAIN_INSTALL', false, 'false', 'Subdomain install');
  98. $this->p->add('base', false, '/', 'Root of WordPress installation');
  99. $this->p->add('DOMAIN_CURRENT_SITE', false, '', 'Domain of current site');
  100. $this->p->add('PATH_CURRENT_SITE', false, '/', 'Path of current site');
  101. $this->p->add('SITE_ID_CURRENT_SITE', false, '1', 'ID of current site');
  102. $this->p->add('BLOG_ID_CURRENT_SITE', false, '1', 'Blog ID of current site');
  103. $this->p->add('source', false, '', 'If there is an existing WordPress code base you can use it via a path');
  104. if(!$this->p->verify()) die($this->p->getError());
  105. }
  106. /**
  107. * This method allows you to do any additional work beyond unpacking
  108. * the files that is required. This could include work such as downloading
  109. * and unpacking an archive.
  110. *
  111. * The following are some of the methods available to you in this file:
  112. * $this->curlFile($url, $destFolder)
  113. * $this->move($src, $dest)
  114. * $this->unzip($file, $destFolder)
  115. */
  116. public function doWork() {
  117. $fs = new Filesystem();
  118. // Ensure tmp working dir exists
  119. $tmp = $this->mRootPath . "\\tmp";
  120. $this->log("Creating temporary build directory: " . $tmp);
  121. $fs->mkdir($tmp);
  122. if($this->p->get('source') != '' && $fs->exists($this->p->get('source'))) {
  123. // Use WordPress codebase from source parameter
  124. $this->log("Copying WordPress from " . $this->p->get('source'));
  125. $fs->copy($this->p->get('source'), $this->mAppRoot);
  126. } else {
  127. // Download and unpack WordPress
  128. $this->log('Downloading WordPress');
  129. $file = $this->curlFile(WP_URL, $tmp);
  130. $this->log('Extracting WordPress');
  131. $this->unzip($file, $tmp);
  132. $this->log('Moving WordPress files to ' . $this->mAppRoot);
  133. $fs->move("$tmp\wordpress", $this->mAppRoot);
  134. }
  135. // Download and unpack DB abstraction layer
  136. $this->log('Downloading Database Abstraction Layer');
  137. $file = $this->curlFile(DB_ABSTRACTION_URL, $tmp);
  138. $this->log('Extracting Database Abstraction Layer');
  139. $this->unzip($file, $tmp);
  140. $this->log('Moving Database Abstraction Layer files to ' . $this->mAppRoot . "\wp-content\mu-plugins");
  141. $fs->copy("$tmp\wordpress-database-abstraction\wp-db-abstraction\db.php", $this->mAppRoot ."\wp-content\db.php");
  142. $fs->move("$tmp\wordpress-database-abstraction", $this->mAppRoot ."\wp-content\mu-plugins");
  143. // Download and unpack Azure Storage Plugin
  144. $this->log('Downloading Azure Storage Plugin');
  145. $file = $this->curlFile(WAZ_STORAGE_URL, $tmp);
  146. $this->log('Extracting Azure Storage Plugin');
  147. $this->unzip($file, $tmp);
  148. $this->log('Moving Azure Storage Plugin files to ' . $this->mAppRoot . "\wp-content\plugins");
  149. $fs->move("$tmp\windows-azure-storage", $this->mAppRoot . "\wp-content\plugins\windows-azure-storage");
  150. if($this->p->get('WP_ALLOW_MULTISITE') && $this->p->get('WP_ALLOW_MULTISITE') != 'false') {
  151. $fs->mkdir($this->mAppRoot . "\wp-content\blogs.dir");
  152. unlink("$this->mAppRoot.config");
  153. if($this->p->get('SUBDOMAIN_INSTALL')) {
  154. copy($this->mAppRoot . "\\resources\Web-network-subdomains.config", $this->mAppRoot . "\Web.config");
  155. } else {
  156. copy($this->mAppRoot . "\\resources\Web-network-subfolders.config", $this->mAppRoot . "\Web.config");
  157. }
  158. }
  159. // Remove tmp build folder
  160. $fs->rm($tmp);
  161. $fs->rm($this->mRootPath . "/Params.class.php");
  162. $fs->rm($this->mRootPath . "/FileSystem.class.php");
  163. $this->updateWpConfig();
  164. echo "\n\nCongratulations! You now have a brand new Windows Azure WordPress project at " . $this->mRootPath . "\n";
  165. }
  166. /**
  167. * Runs a scaffolder and creates a Windows Azure project structure which can be customized before packaging.
  168. *
  169. * @command-name Run
  170. * @command-description Runs the scaffolder.
  171. *
  172. * @command-parameter-for $scaffolderFile Argv --Phar Required. The scaffolder Phar file path. This is injected automatically.
  173. * @command-parameter-for $rootPath Argv|ConfigFile --OutputPath|-out Required. The path to create the Windows Azure project structure. This is injected automatically.
  174. *
  175. */
  176. public function runCommand($scaffolderFile, $rootPath) {
  177. /**
  178. * DO NOT REMOVE BETWEEN BELOW COMMENT
  179. */
  180. $this->mAppRoot = ($rootPath) . "\WebRole";
  181. $this->mScaffolder = $scaffolderFile;
  182. $this->mRootPath = $rootPath;
  183. $this->parameters();
  184. $this->extractPhar();
  185. $this->updateServiceConfig();
  186. $this->doWork();
  187. /**
  188. * DO NOT REMOVE BETWEEN ABOVE COMMENT
  189. */
  190. }
  191. /**
  192. * Will update the ServiceConfiguration.cscfg file with any values
  193. * specified from the command line paramters. Tags in the .cscfg file
  194. * will be found and replaced. Tags are of the form $tagName$
  195. */
  196. private function updateServiceConfig() {
  197. $this->log("Updating ServiceConfiguration.cscfg\n");
  198. $contents = file_get_contents($this->mRootPath . "/ServiceConfiguration.cscfg");
  199. $values = $this->p->valueArray();
  200. foreach ($values as $key => $value) {
  201. $contents = str_replace('$' . $key . '$', $value, $contents);
  202. }
  203. file_put_contents($this->mRootPath . "/ServiceConfiguration.cscfg", $contents);
  204. }
  205. /**
  206. * Will update the ServiceConfiguration.cscfg file with any values
  207. * specified from the command line paramters. Tags in the .cscfg file
  208. * will be found and replaced. Tags are of the form $tagName$
  209. */
  210. private function updateWpConfig() {
  211. $this->log("Updating wp-config.php\n");
  212. $contents = file_get_contents($this->mRootPath . "/WebRole/wp-config.php");
  213. $values = $this->p->valueArray();
  214. foreach ($values as $key => $value) {
  215. $contents = str_replace('$' . $key . '$', $value, $contents);
  216. }
  217. file_put_contents($this->mRootPath . "/WebRole/wp-config.php", $contents);
  218. }
  219. /**
  220. * Extracts the scaffold files and sets up the project structure
  221. */
  222. private function extractPhar() {
  223. // Load Phar
  224. $phar = new Phar($this->mScaffolder);
  225. // Extract to disk
  226. $this->log("Extracting resources...\n");
  227. $this->createDirectory($this->mRootPath);
  228. $this->extractResources($phar, $this->mRootPath);
  229. $this->log("Extracted resources.\n");
  230. }
  231. /**
  232. * Extracts the contents of a zip archive
  233. *
  234. * @param String $file
  235. * @param String $destFolder
  236. */
  237. private function unzip($file, $destFolder) {
  238. $zip = new ZipArchive();
  239. if($zip->open($file) === true) {
  240. $zip->extractTo("$destFolder");
  241. $zip->close();
  242. } else {
  243. echo "Failed to open archive";
  244. }
  245. }
  246. /**
  247. * Downloads a file from the internet
  248. *
  249. * @param String $url
  250. * @param String $destFolder
  251. * @return String
  252. */
  253. private function curlFile($url, $destFolder) {
  254. $options = array(
  255. CURLOPT_RETURNTRANSFER => true, // return web page
  256. CURLOPT_HEADER => false, // don't return headers
  257. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  258. CURLOPT_ENCODING => "", // handle all encodings
  259. CURLOPT_USERAGENT => "blob curler 1.2", // who am i
  260. CURLOPT_AUTOREFERER => true, // set referer on redirect
  261. CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
  262. CURLOPT_TIMEOUT => 120, // timeout on response
  263. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  264. );
  265. $ch = curl_init( $url );
  266. curl_setopt_array( $ch, $options );
  267. $content = curl_exec( $ch );
  268. $err = curl_errno( $ch );
  269. $errmsg = curl_error( $ch );
  270. $header = curl_getinfo( $ch );
  271. curl_close( $ch );
  272. $header['errno'] = $err;
  273. $header['errmsg'] = $errmsg;
  274. $header['content'] = $content;
  275. $file = explode("/", $url);
  276. $file = $file[count($file)-1];
  277. $this->log("Writing file $destFolder/$file");
  278. file_put_contents("$destFolder/$file", $header['content']);
  279. return "$destFolder/$file";
  280. }
  281. }