IoncubeEncoderTask.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * $Id: IoncubeEncoderTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. require_once 'phing/tasks/ext/ioncube/IoncubeComment.php';
  23. /**
  24. * Invokes the ionCube Encoder (PHP4 or PHP5)
  25. *
  26. * @author Michiel Rook <michiel.rook@gmail.com>
  27. * @author Andrew Eddie <andrew.eddie@jamboworks.com>
  28. * @version $Id: IoncubeEncoderTask.php 905 2010-10-05 16:28:03Z mrook $
  29. * @package phing.tasks.ext.ioncube
  30. * @since 2.2.0
  31. */
  32. class IoncubeEncoderTask extends Task
  33. {
  34. private $ionSwitches = array();
  35. private $ionOptions = array();
  36. private $ionOptionsXS = array();
  37. private $comments = array();
  38. private $encoderName = 'ioncube_encoder';
  39. private $fromDir = '';
  40. private $ioncubePath = '/usr/local/ioncube';
  41. private $phpVersion = '5';
  42. private $targetOption = '';
  43. private $toDir = '';
  44. /**
  45. * Adds a comment to be used in encoded files
  46. */
  47. function addComment(IoncubeComment $comment)
  48. {
  49. $this->comments[] = $comment;
  50. }
  51. /**
  52. * Sets the allowed server
  53. */
  54. function setAllowedServer($value)
  55. {
  56. $this->ionOptionsXS['allowed-server'] = $value;
  57. }
  58. /**
  59. * Returns the allowed server setting
  60. */
  61. function getAllowedServer()
  62. {
  63. return $this->ionOptionsXS['allowed-server'];
  64. }
  65. /**
  66. * Sets the binary option
  67. */
  68. function setBinary($value)
  69. {
  70. $this->ionSwitches['binary'] = $value;
  71. }
  72. /**
  73. * Returns the binary option
  74. */
  75. function getBinary()
  76. {
  77. return $this->ionSwitches['binary'];
  78. }
  79. /**
  80. * Sets files or folders to copy (separated by space)
  81. */
  82. function setCopy($value)
  83. {
  84. $this->ionOptionsXS['copy'] = $value;
  85. }
  86. /**
  87. * Returns the copy setting
  88. */
  89. function getCopy()
  90. {
  91. return $this->ionOptionsXS['copy'];
  92. }
  93. /**
  94. * Sets additional file patterns, files or directories to encode,
  95. * or to reverse the effect of copy (separated by space)
  96. */
  97. function setEncode($value)
  98. {
  99. $this->ionOptionsXS['encode'] = $value;
  100. }
  101. /**
  102. * Returns the encode setting
  103. */
  104. function getEncode()
  105. {
  106. return $this->ionOptionsXS['encode'];
  107. }
  108. /**
  109. * Sets regexps of additional files to encrypt (separated by space)
  110. */
  111. function setEncrypt($value)
  112. {
  113. $this->ionOptionsXS['encrypt'] = $value;
  114. }
  115. /**
  116. * Returns regexps of additional files to encrypt (separated by space)
  117. */
  118. function getEncrypt()
  119. {
  120. return $this->ionOptionsXS['encrypt'];
  121. }
  122. /**
  123. * Sets a period after which the files expire
  124. */
  125. function setExpirein($value)
  126. {
  127. $this->ionOptions['expire-in'] = $value;
  128. }
  129. /**
  130. * Returns the expireIn setting
  131. */
  132. function getExpirein()
  133. {
  134. return $this->ionOptions['expire-in'];
  135. }
  136. /**
  137. * Sets a YYYY-MM-DD date to expire the files
  138. */
  139. function setExpireon($value)
  140. {
  141. $this->ionOptions['expire-on'] = $value;
  142. }
  143. /**
  144. * Returns the expireOn setting
  145. */
  146. function getExpireon()
  147. {
  148. return $this->ionOptions['expire-on'];
  149. }
  150. /**
  151. * Sets the source directory
  152. */
  153. function setFromDir($value)
  154. {
  155. $this->fromDir = $value;
  156. }
  157. /**
  158. * Returns the source directory
  159. */
  160. function getFromDir()
  161. {
  162. return $this->fromDir;
  163. }
  164. /**
  165. * Set files and directories to ignore entirely and exclude from the target directory
  166. * (separated by space).
  167. */
  168. function setIgnore($value)
  169. {
  170. $this->ionOptionsXS['ignore'] = $value;
  171. }
  172. /**
  173. * Returns the ignore setting
  174. */
  175. function getIgnore()
  176. {
  177. return $this->ionOptionsXS['ignore'];
  178. }
  179. /**
  180. * Sets the path to the ionCube encoder
  181. */
  182. function setIoncubePath($value)
  183. {
  184. $this->ioncubePath = $value;
  185. }
  186. /**
  187. * Returns the path to the ionCube encoder
  188. */
  189. function getIoncubePath()
  190. {
  191. return $this->ioncubePath;
  192. }
  193. /**
  194. * Set files and directories not to be ignored (separated by space).
  195. */
  196. function setKeep($value)
  197. {
  198. $this->ionOptionsXS['keep'] = $value;
  199. }
  200. /**
  201. * Returns the ignore setting
  202. */
  203. function getKeep()
  204. {
  205. return $this->ionOptionsXS['keep'];
  206. }
  207. /**
  208. * Sets the path to the license file to use
  209. */
  210. function setLicensePath($value)
  211. {
  212. $this->ionOptions['with-license'] = $value;
  213. }
  214. /**
  215. * Returns the path to the license file to use
  216. */
  217. function getLicensePath()
  218. {
  219. return $this->ionOptions['with-license'];
  220. }
  221. /**
  222. * Sets the no-doc-comments option
  223. */
  224. function setNoDocComments($value)
  225. {
  226. $this->ionSwitches['no-doc-comment'] = $value;
  227. }
  228. /**
  229. * Returns the no-doc-comments option
  230. */
  231. function getNoDocComments()
  232. {
  233. return $this->ionSwitches['no-doc-comment'];
  234. }
  235. /**
  236. * Sets the obfuscate option
  237. */
  238. function setObfuscate($value)
  239. {
  240. $this->ionOptionsXS['obfuscate'] = $value;
  241. }
  242. /**
  243. * Returns the optimize option
  244. */
  245. function getObfuscate()
  246. {
  247. return $this->ionOptionsXS['obfuscate'];
  248. }
  249. /**
  250. * Sets the obfuscation key (required if using the obfuscate option)
  251. */
  252. function setObfuscationKey($value)
  253. {
  254. $this->ionOptions['obfuscation-key'] = $value;
  255. }
  256. /**
  257. * Returns the optimize option
  258. */
  259. function getObfuscationKey()
  260. {
  261. return $this->ionOptions['obfuscation-key'];
  262. }
  263. /**
  264. * Sets the optimize option
  265. */
  266. function setOptimize($value)
  267. {
  268. $this->ionOptions['optimize'] = $value;
  269. }
  270. /**
  271. * Returns the optimize option
  272. */
  273. function getOptimize()
  274. {
  275. return $this->ionOptions['optimize'];
  276. }
  277. /**
  278. * Sets the passphrase to use when encoding files
  279. */
  280. function setPassPhrase($value)
  281. {
  282. $this->ionOptions['passphrase'] = $value;
  283. }
  284. /**
  285. * Returns the passphrase to use when encoding files
  286. */
  287. function getPassPhrase()
  288. {
  289. return $this->ionOptions['passphrase'];
  290. }
  291. /**
  292. * Sets the version of PHP to use (defaults to 5)
  293. */
  294. function setPhpVersion($value)
  295. {
  296. $this->phpVersion = $value;
  297. }
  298. /**
  299. * Returns the version of PHP to use (defaults to 5)
  300. */
  301. function getPhpVersion()
  302. {
  303. return $this->phpVersion;
  304. }
  305. /**
  306. * Sets the target directory
  307. */
  308. function setToDir($value)
  309. {
  310. $this->toDir = $value;
  311. }
  312. /**
  313. * Returns the target directory
  314. */
  315. function getToDir()
  316. {
  317. return $this->toDir;
  318. }
  319. /**
  320. * Sets the without-runtime-loader-support option
  321. */
  322. function setWithoutRuntimeLoaderSupport($value)
  323. {
  324. $this->ionSwitches['without-runtime-loader-support'] = $value;
  325. }
  326. /**
  327. * Returns the without-runtime-loader-support option
  328. */
  329. function getWithoutRuntimeLoaderSupport()
  330. {
  331. return $this->ionSwitches['without-runtime-loader-support'];
  332. }
  333. /**
  334. * Sets the no-short-open-tags option
  335. */
  336. function setNoShortOpenTags($value)
  337. {
  338. $this->ionSwitches['no-short-open-tags'] = $value;
  339. }
  340. /**
  341. * Returns the no-short-open-tags option
  342. */
  343. function getNoShortOpenTags()
  344. {
  345. return $this->ionSwitches['no-short-open-tags'];
  346. }
  347. /**
  348. * Sets the option to use when encoding target directory already exists (defaults to none)
  349. */
  350. function setTargetOption($targetOption)
  351. {
  352. $this->targetOption = $targetOption;
  353. }
  354. /**
  355. * Returns he option to use when encoding target directory already exists (defaults to none)
  356. */
  357. function getTargetOption()
  358. {
  359. return $this->targetOption;
  360. }
  361. /**
  362. * Sets the callback-file option
  363. */
  364. function setCallbackFile($value)
  365. {
  366. $this->ionOptions['callback-file'] = $value;
  367. }
  368. /**
  369. * Returns the callback-file option
  370. */
  371. function getCallbackFile()
  372. {
  373. return $this->ionOptions['callback-file'];
  374. }
  375. /**
  376. * Sets the obfuscation-exclusions-file option
  377. */
  378. function setObfuscationExclusionFile($value)
  379. {
  380. $this->ionOptions['obfuscation-exclusion-file'] = $value;
  381. }
  382. /**
  383. * Returns the obfuscation-exclusions-file option
  384. */
  385. function getObfuscationExclusionFile()
  386. {
  387. return $this->ionOptions['obfuscation-exclusion-file'];
  388. }
  389. /**
  390. * The main entry point
  391. *
  392. * @throws BuildException
  393. */
  394. function main()
  395. {
  396. $arguments = $this->constructArguments();
  397. $encoder = new PhingFile($this->ioncubePath, $this->encoderName . ($this->phpVersion == 5 ? '5' : ''));
  398. $this->log("Running ionCube Encoder...");
  399. exec($encoder->__toString() . ' ' . $arguments . " 2>&1", $output, $return);
  400. if ($return != 0)
  401. {
  402. throw new BuildException("Could not execute ionCube Encoder: " . implode(' ', $output));
  403. }
  404. }
  405. /**
  406. * Constructs an argument string for the ionCube encoder
  407. */
  408. private function constructArguments()
  409. {
  410. $arguments = '';
  411. foreach ($this->ionSwitches as $name => $value)
  412. {
  413. if ($value)
  414. {
  415. $arguments.= "--$name ";
  416. }
  417. }
  418. foreach ($this->ionOptions as $name => $value)
  419. {
  420. $arguments.= "--$name '$value' ";
  421. }
  422. foreach ($this->ionOptionsXS as $name => $value)
  423. {
  424. foreach (explode(' ', $value) as $arg)
  425. {
  426. $arguments.= "--$name '$arg' ";
  427. }
  428. }
  429. foreach ($this->comments as $comment)
  430. {
  431. $arguments.= "--add-comment '" . $comment->getValue() . "' ";
  432. }
  433. if (!empty($this->targetOption))
  434. {
  435. switch ($this->targetOption)
  436. {
  437. case "replace":
  438. case "merge":
  439. case "update":
  440. case "rename":
  441. {
  442. $arguments.= "--" . $this->targetOption . "-target ";
  443. } break;
  444. default:
  445. {
  446. throw new BuildException("Unknown target option '" . $this->targetOption . "'");
  447. } break;
  448. }
  449. }
  450. if ($this->fromDir != '')
  451. {
  452. $arguments .= $this->fromDir . ' ';
  453. }
  454. if ($this->toDir != '')
  455. {
  456. $arguments .= "-o " . $this->toDir . ' ';
  457. }
  458. return $arguments;
  459. }
  460. }