PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/music/lib/lib/ID3/Frame/COMR.php

http://xepec.googlecode.com/
PHP | 400 lines | 141 code | 43 blank | 216 comment | 8 complexity | d4b8400fb30d894288fa21caa63ad060 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHP Reader Library
  4. *
  5. * Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of the project workgroup nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * @package php-reader
  32. * @subpackage ID3
  33. * @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
  34. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  35. * @version $Id: COMR.php 129 2008-12-28 19:00:44Z svollbehr $
  36. */
  37. /**#@+ @ignore */
  38. require_once("ID3/Frame.php");
  39. require_once("ID3/Encoding.php");
  40. /**#@-*/
  41. /**
  42. * The <i>Commercial frame</i> enables several competing offers in the same tag
  43. * by bundling all needed information. That makes this frame rather complex but
  44. * it's an easier solution than if one tries to achieve the same result with
  45. * several frames.
  46. *
  47. * There may be more than one commercial frame in a tag, but no two may be
  48. * identical.
  49. *
  50. * @package php-reader
  51. * @subpackage ID3
  52. * @author Sven Vollbehr <svollbehr@gmail.com>
  53. * @author Ryan Butterfield <buttza@gmail.com>
  54. * @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
  55. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  56. * @version $Rev: 129 $
  57. */
  58. final class ID3_Frame_COMR extends ID3_Frame
  59. implements ID3_Encoding
  60. {
  61. /**
  62. * The delivery types.
  63. *
  64. * @var Array
  65. */
  66. public static $types = array
  67. ("Other", "Standard CD album with other songs", "Compressed audio on CD",
  68. "File over the Internet", "Stream over the Internet", "As note sheets",
  69. "As note sheets in a book with other sheets", "Music on other media",
  70. "Non-musical merchandise");
  71. /** @var integer */
  72. private $_encoding;
  73. /** @var string */
  74. private $_currency = "EUR";
  75. /** @var string */
  76. private $_price;
  77. /** @var string */
  78. private $_date;
  79. /** @var string */
  80. private $_contact;
  81. /** @var integer */
  82. private $_delivery = 0;
  83. /** @var string */
  84. private $_seller;
  85. /** @var string */
  86. private $_description;
  87. /** @var string */
  88. private $_mimeType = false;
  89. /** @var string */
  90. private $_imageData;
  91. /** @var integer */
  92. private $_imageSize = 0;
  93. /**
  94. * Constructs the class with given parameters and parses object related data.
  95. *
  96. * @param Reader $reader The reader object.
  97. * @param Array $options The options array.
  98. */
  99. public function __construct($reader = null, &$options = array())
  100. {
  101. parent::__construct($reader, $options);
  102. $this->_encoding = $this->getOption("encoding", ID3_Encoding::UTF8);
  103. if ($reader === null)
  104. return;
  105. $encoding = Transform::fromUInt8($this->_data[0]);
  106. list($pricing, $this->_data) =
  107. $this->explodeString8(substr($this->_data, 1), 2);
  108. $this->_currency = substr($pricing, 0, 3);
  109. $this->_price = substr($pricing, 3);
  110. $this->_date = substr($this->_data, 0, 8);
  111. list($this->_contact, $this->_data) =
  112. $this->explodeString8(substr($this->_data, 8), 2);
  113. $this->_delivery = Transform::fromUInt8($this->_data[0]);
  114. $this->_data = substr($this->_data, 1);
  115. switch ($encoding) {
  116. case self::UTF16:
  117. list ($this->_seller, $this->_description, $this->_data) =
  118. $this->explodeString16($this->_data, 3);
  119. $this->_seller = $this->convertString
  120. (Transform::fromString16($this->_seller), "utf-16");
  121. $this->_description = $this->convertString
  122. (Transform::fromString16($this->_description), "utf-16");
  123. break;
  124. case self::UTF16BE:
  125. list ($this->_seller, $this->_description, $this->_data) =
  126. $this->explodeString16($this->_data, 3);
  127. $this->_seller = $this->convertString
  128. (Transform::fromString16BE($this->_seller), "utf-16be");
  129. $this->_description = $this->convertString
  130. (Transform::fromString16BE($this->_description), "utf-16be");
  131. break;
  132. case self::UTF8:
  133. list ($this->_seller, $this->_description, $this->_data) =
  134. $this->explodeString8($this->_data, 3);
  135. $this->_seller = $this->convertString
  136. (Transform::fromString8($this->_seller), "utf-8");
  137. $this->_description = $this->convertString
  138. (Transform::fromString8($this->_description), "utf-8");
  139. break;
  140. default:
  141. list ($this->_seller, $this->_description, $this->_data) =
  142. $this->explodeString8($this->_data, 3);
  143. $this->_seller = $this->convertString
  144. (Transform::fromString8($this->_seller), "iso-8859-1");
  145. $this->_description = $this->convertString
  146. (Transform::fromString8($this->_description), "iso-8859-1");
  147. }
  148. if (strlen($this->_data) == 0)
  149. return;
  150. list($this->_mimeType, $this->_imageData) =
  151. $this->explodeString8($this->_data, 2);
  152. $this->_imageSize = strlen($this->_imageData);
  153. }
  154. /**
  155. * Returns the text encoding.
  156. *
  157. * All the strings read from a file are automatically converted to the
  158. * character encoding specified with the <var>encoding</var> option. See
  159. * {@link ID3v2} for details. This method returns the original text encoding
  160. * used to write the frame.
  161. *
  162. * @return integer
  163. */
  164. public function getEncoding() { return $this->_encoding; }
  165. /**
  166. * Sets the text encoding.
  167. *
  168. * All the string written to the frame are done so using given character
  169. * encoding. No conversions of existing data take place upon the call to this
  170. * method thus all texts must be given in given character encoding.
  171. *
  172. * The default character encoding used to write the frame is UTF-8.
  173. *
  174. * @see ID3_Encoding
  175. * @param integer $encoding The text encoding.
  176. */
  177. public function setEncoding($encoding) { $this->_encoding = $encoding; }
  178. /**
  179. * Returns the currency code, encoded according to
  180. * {@link http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm
  181. * ISO 4217} alphabetic currency code.
  182. *
  183. * @return string
  184. */
  185. public function getCurrency() { return $this->_currency; }
  186. /**
  187. * Sets the currency used in transaction, encoded according to
  188. * {@link http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm
  189. * ISO 4217} alphabetic currency code.
  190. *
  191. * @param string $currency The currency code.
  192. */
  193. public function setCurrency($currency) { $this->_currency = $currency; }
  194. /**
  195. * Returns the price as a numerical string using "." as the decimal separator.
  196. *
  197. * In the price string several prices may be concatenated, separated by a "/"
  198. * character, but there may only be one currency of each type.
  199. *
  200. * @return string
  201. */
  202. public function getPrice() { return $this->_price; }
  203. /**
  204. * Sets the price. The price must use "." as the decimal separator and have
  205. * multiple values be separated by a "/" character.
  206. *
  207. * @param string $price The price.
  208. */
  209. public function setPrice($price)
  210. {
  211. $this->_price = $price;
  212. }
  213. /**
  214. * Returns the date as an 8 character date string (YYYYMMDD), describing for
  215. * how long the price is valid.
  216. *
  217. * @return string
  218. */
  219. public function getDate() { return $this->_date; }
  220. /**
  221. * Sets the date describing for how long the price is valid for. The date must
  222. * be an 8 character date string (YYYYMMDD).
  223. *
  224. * @param string $date The date string.
  225. */
  226. public function setDate($date) { $this->_date = $date; }
  227. /**
  228. * Returns the contact URL, with which the user can contact the seller.
  229. *
  230. * @return string
  231. */
  232. public function getContact() { return $this->_contact; }
  233. /**
  234. * Sets the contact URL, with which the user can contact the seller.
  235. *
  236. * @param string $contact The contact URL.
  237. */
  238. public function setContact($contact) { $this->_contact = $contact; }
  239. /**
  240. * Returns the delivery type with whitch the audio was delivered when bought.
  241. *
  242. * @return integer
  243. */
  244. public function getDelivery() { return $this->_delivery; }
  245. /**
  246. * Sets the delivery type with whitch the audio was delivered when bought.
  247. *
  248. * @param integer $delivery The delivery type code.
  249. */
  250. public function setDelivery($delivery) { $this->_delivery = $delivery; }
  251. /**
  252. * Returns the name of the seller.
  253. *
  254. * @return string
  255. */
  256. public function getSeller() { return $this->_seller; }
  257. /**
  258. * Sets the name of the seller using given encoding. The seller text encoding
  259. * must be that of the description text.
  260. *
  261. * @param string $seller The name of the seller.
  262. * @param integer $encoding The text encoding.
  263. */
  264. public function setSeller($seller, $encoding = false)
  265. {
  266. $this->_seller = $seller;
  267. if ($encoding !== false)
  268. $this->_encoding = $encoding;
  269. }
  270. /**
  271. * Returns the short description of the product.
  272. *
  273. * @return string
  274. */
  275. public function getDescription() { return $this->_description; }
  276. /**
  277. * Sets the content description text using given encoding. The description
  278. * encoding must be that of the seller text.
  279. *
  280. * @param string $description The content description text.
  281. * @param integer $encoding The text encoding.
  282. */
  283. public function setDescription($description, $encoding = false)
  284. {
  285. $this->_description = $description;
  286. if ($encoding !== false)
  287. $this->_encoding = $encoding;
  288. }
  289. /**
  290. * Returns the MIME type of the seller's company logo, if attached, or
  291. * <var>false</var> otherwise. Currently only "image/png" and "image/jpeg"
  292. * are allowed.
  293. *
  294. * @return string
  295. */
  296. public function getMimeType() { return $this->_mimeType; }
  297. /**
  298. * Sets the MIME type. Currently only "image/png" and "image/jpeg" are
  299. * allowed. The MIME type is always ISO-8859-1 encoded.
  300. *
  301. * @param string $mimeType The MIME type.
  302. */
  303. public function setMimeType($mimeType) { $this->_mimeType = $mimeType; }
  304. /**
  305. * Returns the embedded image binary data.
  306. *
  307. * @return string
  308. */
  309. public function getImageData() { return $this->_imageData; }
  310. /**
  311. * Sets the embedded image data. Also updates the image size to correspond the
  312. * new data.
  313. *
  314. * @param string $imageData The image data.
  315. */
  316. public function setImageData($imageData)
  317. {
  318. $this->_imageData = $imageData;
  319. $this->_imageSize = strlen($imageData);
  320. }
  321. /**
  322. * Returns the size of the embedded image data.
  323. *
  324. * @return integer
  325. */
  326. public function getImageSize() { return $this->_imageSize; }
  327. /**
  328. * Returns the frame raw data.
  329. *
  330. * @return string
  331. */
  332. public function __toString()
  333. {
  334. $data = Transform::toUInt8($this->_encoding) . $this->_currency .
  335. $this->_price . "\0" . $this->_date . $this->_contact . "\0" .
  336. Transform::toUInt8($this->_delivery);
  337. switch ($this->_encoding) {
  338. case self::UTF16:
  339. case self::UTF16LE:
  340. $order = $this->_encoding == self::UTF16 ?
  341. Transform::MACHINE_ENDIAN_ORDER : Transform::LITTLE_ENDIAN_ORDER;
  342. $data .= Transform::toString16($this->_seller, $order) . "\0\0" .
  343. Transform::toString16($this->_description, $order) . "\0\0";
  344. break;
  345. case self::UTF16BE:
  346. $data .= Transform::toString16BE
  347. ($this->_seller . "\0\0" . $this->_description . "\0\0");
  348. break;
  349. default:
  350. $data .= $this->_seller . "\0" . $this->_description . "\0";
  351. }
  352. parent::setData
  353. ($data . ($this->_mimeType ?
  354. $this->_mimeType . "\0" . $this->_imageData : ""));
  355. return parent::__toString();
  356. }
  357. }