PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

/redbean/rb.php

https://bitbucket.org/ddonthula/zurmonew
PHP | 8595 lines | 3140 code | 733 blank | 4722 comment | 473 complexity | 2ff5ad438ce1b2f9d3d10221c2b22cab MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php /*
  2. .______.
  3. _______ ____ __| _/\_ |__ ____ _____ ____
  4. \_ __ \_/ __ \ / __ | | __ \_/ __ \\__ \ / \
  5. | | \/\ ___// /_/ | | \_\ \ ___/ / __ \| | \
  6. |__| \___ >____ | |___ /\___ >____ /___| /
  7. \/ \/ \/ \/ \/ \/
  8. RedBean Database Objects -
  9. Written by Gabor de Mooij (c) copyright 2009-2012
  10. RedBean is DUAL Licensed BSD and GPLv2. You may choose the license that fits
  11. best for your project.
  12. BSD/GPLv2 License
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are met:
  15. * Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. * Neither the name of RedBeanPHP nor the
  21. names of its contributors may be used to endorse or promote products
  22. derived from this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY GABOR DE MOOIJ ''AS IS'' AND ANY
  24. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. DISCLAIMED. IN NO EVENT SHALL GABOR DE MOOIJ BE LIABLE FOR ANY
  27. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. RedBeanPHP is Written by Gabor de Mooij (G.J.G.T de Mooij) Copyright (c) 2011.
  34. GPLv2 LICENSE
  35. GNU GENERAL PUBLIC LICENSE
  36. Version 2, June 1991
  37. Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  38. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  39. Everyone is permitted to copy and distribute verbatim copies
  40. of this license document, but changing it is not allowed.
  41. Preamble
  42. The licenses for most software are designed to take away your
  43. freedom to share and change it. By contrast, the GNU General Public
  44. License is intended to guarantee your freedom to share and change free
  45. software--to make sure the software is free for all its users. This
  46. General Public License applies to most of the Free Software
  47. Foundation's software and to any other program whose authors commit to
  48. using it. (Some other Free Software Foundation software is covered by
  49. the GNU Lesser General Public License instead.) You can apply it to
  50. your programs, too.
  51. When we speak of free software, we are referring to freedom, not
  52. price. Our General Public Licenses are designed to make sure that you
  53. have the freedom to distribute copies of free software (and charge for
  54. this service if you wish), that you receive source code or can get it
  55. if you want it, that you can change the software or use pieces of it
  56. in new free programs; and that you know you can do these things.
  57. To protect your rights, we need to make restrictions that forbid
  58. anyone to deny you these rights or to ask you to surrender the rights.
  59. These restrictions translate to certain responsibilities for you if you
  60. distribute copies of the software, or if you modify it.
  61. For example, if you distribute copies of such a program, whether
  62. gratis or for a fee, you must give the recipients all the rights that
  63. you have. You must make sure that they, too, receive or can get the
  64. source code. And you must show them these terms so they know their
  65. rights.
  66. We protect your rights with two steps: (1) copyright the software, and
  67. (2) offer you this license which gives you legal permission to copy,
  68. distribute and/or modify the software.
  69. Also, for each author's protection and ours, we want to make certain
  70. that everyone understands that there is no warranty for this free
  71. software. If the software is modified by someone else and passed on, we
  72. want its recipients to know that what they have is not the original, so
  73. that any problems introduced by others will not reflect on the original
  74. authors' reputations.
  75. Finally, any free program is threatened constantly by software
  76. patents. We wish to avoid the danger that redistributors of a free
  77. program will individually obtain patent licenses, in effect making the
  78. program proprietary. To prevent this, we have made it clear that any
  79. patent must be licensed for everyone's free use or not licensed at all.
  80. The precise terms and conditions for copying, distribution and
  81. modification follow.
  82. GNU GENERAL PUBLIC LICENSE
  83. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  84. 0. This License applies to any program or other work which contains
  85. a notice placed by the copyright holder saying it may be distributed
  86. under the terms of this General Public License. The "Program", below,
  87. refers to any such program or work, and a "work based on the Program"
  88. means either the Program or any derivative work under copyright law:
  89. that is to say, a work containing the Program or a portion of it,
  90. either verbatim or with modifications and/or translated into another
  91. language. (Hereinafter, translation is included without limitation in
  92. the term "modification".) Each licensee is addressed as "you".
  93. Activities other than copying, distribution and modification are not
  94. covered by this License; they are outside its scope. The act of
  95. running the Program is not restricted, and the output from the Program
  96. is covered only if its contents constitute a work based on the
  97. Program (independent of having been made by running the Program).
  98. Whether that is true depends on what the Program does.
  99. 1. You may copy and distribute verbatim copies of the Program's
  100. source code as you receive it, in any medium, provided that you
  101. conspicuously and appropriately publish on each copy an appropriate
  102. copyright notice and disclaimer of warranty; keep intact all the
  103. notices that refer to this License and to the absence of any warranty;
  104. and give any other recipients of the Program a copy of this License
  105. along with the Program.
  106. You may charge a fee for the physical act of transferring a copy, and
  107. you may at your option offer warranty protection in exchange for a fee.
  108. 2. You may modify your copy or copies of the Program or any portion
  109. of it, thus forming a work based on the Program, and copy and
  110. distribute such modifications or work under the terms of Section 1
  111. above, provided that you also meet all of these conditions:
  112. a) You must cause the modified files to carry prominent notices
  113. stating that you changed the files and the date of any change.
  114. b) You must cause any work that you distribute or publish, that in
  115. whole or in part contains or is derived from the Program or any
  116. part thereof, to be licensed as a whole at no charge to all third
  117. parties under the terms of this License.
  118. c) If the modified program normally reads commands interactively
  119. when run, you must cause it, when started running for such
  120. interactive use in the most ordinary way, to print or display an
  121. announcement including an appropriate copyright notice and a
  122. notice that there is no warranty (or else, saying that you provide
  123. a warranty) and that users may redistribute the program under
  124. these conditions, and telling the user how to view a copy of this
  125. License. (Exception: if the Program itself is interactive but
  126. does not normally print such an announcement, your work based on
  127. the Program is not required to print an announcement.)
  128. These requirements apply to the modified work as a whole. If
  129. identifiable sections of that work are not derived from the Program,
  130. and can be reasonably considered independent and separate works in
  131. themselves, then this License, and its terms, do not apply to those
  132. sections when you distribute them as separate works. But when you
  133. distribute the same sections as part of a whole which is a work based
  134. on the Program, the distribution of the whole must be on the terms of
  135. this License, whose permissions for other licensees extend to the
  136. entire whole, and thus to each and every part regardless of who wrote it.
  137. Thus, it is not the intent of this section to claim rights or contest
  138. your rights to work written entirely by you; rather, the intent is to
  139. exercise the right to control the distribution of derivative or
  140. collective works based on the Program.
  141. In addition, mere aggregation of another work not based on the Program
  142. with the Program (or with a work based on the Program) on a volume of
  143. a storage or distribution medium does not bring the other work under
  144. the scope of this License.
  145. 3. You may copy and distribute the Program (or a work based on it,
  146. under Section 2) in object code or executable form under the terms of
  147. Sections 1 and 2 above provided that you also do one of the following:
  148. a) Accompany it with the complete corresponding machine-readable
  149. source code, which must be distributed under the terms of Sections
  150. 1 and 2 above on a medium customarily used for software interchange; or,
  151. b) Accompany it with a written offer, valid for at least three
  152. years, to give any third party, for a charge no more than your
  153. cost of physically performing source distribution, a complete
  154. machine-readable copy of the corresponding source code, to be
  155. distributed under the terms of Sections 1 and 2 above on a medium
  156. customarily used for software interchange; or,
  157. c) Accompany it with the information you received as to the offer
  158. to distribute corresponding source code. (This alternative is
  159. allowed only for noncommercial distribution and only if you
  160. received the program in object code or executable form with such
  161. an offer, in accord with Subsection b above.)
  162. The source code for a work means the preferred form of the work for
  163. making modifications to it. For an executable work, complete source
  164. code means all the source code for all modules it contains, plus any
  165. associated interface definition files, plus the scripts used to
  166. control compilation and installation of the executable. However, as a
  167. special exception, the source code distributed need not include
  168. anything that is normally distributed (in either source or binary
  169. form) with the major components (compiler, kernel, and so on) of the
  170. operating system on which the executable runs, unless that component
  171. itself accompanies the executable.
  172. If distribution of executable or object code is made by offering
  173. access to copy from a designated place, then offering equivalent
  174. access to copy the source code from the same place counts as
  175. distribution of the source code, even though third parties are not
  176. compelled to copy the source along with the object code.
  177. 4. You may not copy, modify, sublicense, or distribute the Program
  178. except as expressly provided under this License. Any attempt
  179. otherwise to copy, modify, sublicense or distribute the Program is
  180. void, and will automatically terminate your rights under this License.
  181. However, parties who have received copies, or rights, from you under
  182. this License will not have their licenses terminated so long as such
  183. parties remain in full compliance.
  184. 5. You are not required to accept this License, since you have not
  185. signed it. However, nothing else grants you permission to modify or
  186. distribute the Program or its derivative works. These actions are
  187. prohibited by law if you do not accept this License. Therefore, by
  188. modifying or distributing the Program (or any work based on the
  189. Program), you indicate your acceptance of this License to do so, and
  190. all its terms and conditions for copying, distributing or modifying
  191. the Program or works based on it.
  192. 6. Each time you redistribute the Program (or any work based on the
  193. Program), the recipient automatically receives a license from the
  194. original licensor to copy, distribute or modify the Program subject to
  195. these terms and conditions. You may not impose any further
  196. restrictions on the recipients' exercise of the rights granted herein.
  197. You are not responsible for enforcing compliance by third parties to
  198. this License.
  199. 7. If, as a consequence of a court judgment or allegation of patent
  200. infringement or for any other reason (not limited to patent issues),
  201. conditions are imposed on you (whether by court order, agreement or
  202. otherwise) that contradict the conditions of this License, they do not
  203. excuse you from the conditions of this License. If you cannot
  204. distribute so as to satisfy simultaneously your obligations under this
  205. License and any other pertinent obligations, then as a consequence you
  206. may not distribute the Program at all. For example, if a patent
  207. license would not permit royalty-free redistribution of the Program by
  208. all those who receive copies directly or indirectly through you, then
  209. the only way you could satisfy both it and this License would be to
  210. refrain entirely from distribution of the Program.
  211. If any portion of this section is held invalid or unenforceable under
  212. any particular circumstance, the balance of the section is intended to
  213. apply and the section as a whole is intended to apply in other
  214. circumstances.
  215. It is not the purpose of this section to induce you to infringe any
  216. patents or other property right claims or to contest validity of any
  217. such claims; this section has the sole purpose of protecting the
  218. integrity of the free software distribution system, which is
  219. implemented by public license practices. Many people have made
  220. generous contributions to the wide range of software distributed
  221. through that system in reliance on consistent application of that
  222. system; it is up to the author/donor to decide if he or she is willing
  223. to distribute software through any other system and a licensee cannot
  224. impose that choice.
  225. This section is intended to make thoroughly clear what is believed to
  226. be a consequence of the rest of this License.
  227. 8. If the distribution and/or use of the Program is restricted in
  228. certain countries either by patents or by copyrighted interfaces, the
  229. original copyright holder who places the Program under this License
  230. may add an explicit geographical distribution limitation excluding
  231. those countries, so that distribution is permitted only in or among
  232. countries not thus excluded. In such case, this License incorporates
  233. the limitation as if written in the body of this License.
  234. 9. The Free Software Foundation may publish revised and/or new versions
  235. of the General Public License from time to time. Such new versions will
  236. be similar in spirit to the present version, but may differ in detail to
  237. address new problems or concerns.
  238. Each version is given a distinguishing version number. If the Program
  239. specifies a version number of this License which applies to it and "any
  240. later version", you have the option of following the terms and conditions
  241. either of that version or of any later version published by the Free
  242. Software Foundation. If the Program does not specify a version number of
  243. this License, you may choose any version ever published by the Free Software
  244. Foundation.
  245. 10. If you wish to incorporate parts of the Program into other free
  246. programs whose distribution conditions are different, write to the author
  247. to ask for permission. For software which is copyrighted by the Free
  248. Software Foundation, write to the Free Software Foundation; we sometimes
  249. make exceptions for this. Our decision will be guided by the two goals
  250. of preserving the free status of all derivatives of our free software and
  251. of promoting the sharing and reuse of software generally.
  252. NO WARRANTY
  253. 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  254. FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
  255. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  256. PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  257. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  258. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
  259. TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
  260. PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  261. REPAIR OR CORRECTION.
  262. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  263. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  264. REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  265. INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  266. OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  267. TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  268. YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  269. PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  270. POSSIBILITY OF SUCH DAMAGES.
  271. */
  272. /**
  273. * Interface for database drivers
  274. *
  275. * @file RedBean/Driver.php
  276. * @description Describes the API for database classes
  277. * The Driver API conforms to the ADODB pseudo standard
  278. * for database drivers.
  279. * @author Gabor de Mooij and the RedBeanPHP Community
  280. * @license BSD/GPLv2
  281. *
  282. *
  283. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
  284. * This source file is subject to the BSD/GPLv2 License that is bundled
  285. * with this source code in the file license.txt.
  286. */
  287. interface RedBean_Driver {
  288. /**
  289. * Runs a query and fetches results as a multi dimensional array.
  290. *
  291. * @param string $sql SQL to be executed
  292. *
  293. * @return array $results result
  294. */
  295. public function GetAll( $sql, $aValues=array() );
  296. /**
  297. * Runs a query and fetches results as a column.
  298. *
  299. * @param string $sql SQL Code to execute
  300. *
  301. * @return array $results Resultset
  302. */
  303. public function GetCol( $sql, $aValues=array() );
  304. /**
  305. * Runs a query an returns results as a single cell.
  306. *
  307. * @param string $sql SQL to execute
  308. *
  309. * @return mixed $cellvalue result cell
  310. */
  311. public function GetCell( $sql, $aValues=array() );
  312. /**
  313. * Runs a query and returns a flat array containing the values of
  314. * one row.
  315. *
  316. * @param string $sql SQL to execute
  317. *
  318. * @return array $row result row
  319. */
  320. public function GetRow( $sql, $aValues=array() );
  321. /**
  322. * Executes SQL code and allows key-value binding.
  323. * This function allows you to provide an array with values to bind
  324. * to query parameters. For instance you can bind values to question
  325. * marks in the query. Each value in the array corresponds to the
  326. * question mark in the query that matches the position of the value in the
  327. * array. You can also bind values using explicit keys, for instance
  328. * array(":key"=>123) will bind the integer 123 to the key :key in the
  329. * SQL. This method has no return value.
  330. *
  331. * @param string $sql SQL Code to execute
  332. * @param array $aValues Values to bind to SQL query
  333. *
  334. * @return void
  335. */
  336. public function Execute( $sql, $aValues=array() );
  337. /**
  338. * Escapes a string for use in SQL using the currently selected
  339. * driver driver.
  340. *
  341. * @param string $string string to be escaped
  342. *
  343. * @return string $string escaped string
  344. */
  345. public function Escape( $str );
  346. /**
  347. * Returns the latest insert ID if driver does support this
  348. * feature.
  349. *
  350. * @return integer $id primary key ID
  351. */
  352. public function GetInsertID();
  353. /**
  354. * Returns the number of rows affected by the most recent query
  355. * if the currently selected driver driver supports this feature.
  356. *
  357. * @return integer $numOfRows number of rows affected
  358. */
  359. public function Affected_Rows();
  360. /**
  361. * Toggles debug mode. In debug mode the driver will print all
  362. * SQL to the screen together with some information about the
  363. * results. All SQL code that passes through the driver will be
  364. * passes on to the screen for inspection.
  365. * This method has no return value.
  366. *
  367. * @param boolean $trueFalse turn on/off
  368. *
  369. * @return void
  370. */
  371. public function setDebugMode( $tf );
  372. /**
  373. * Starts a transaction.
  374. * This method is part of the transaction mechanism of
  375. * RedBeanPHP. All queries in a transaction are executed together.
  376. * In case of an error all commands will be rolled back so none of the
  377. * SQL in the transaction will affect the DB. Using transactions is
  378. * considered best practice.
  379. * This method has no return value.
  380. *
  381. * @return void
  382. */
  383. public function CommitTrans();
  384. /**
  385. * Commits a transaction.
  386. * This method is part of the transaction mechanism of
  387. * RedBeanPHP. All queries in a transaction are executed together.
  388. * In case of an error all commands will be rolled back so none of the
  389. * SQL in the transaction will affect the DB. Using transactions is
  390. * considered best practice.
  391. * This method has no return value.
  392. *
  393. * @return void
  394. */
  395. public function StartTrans();
  396. /**
  397. * Rolls back a transaction.
  398. * This method is part of the transaction mechanism of
  399. * RedBeanPHP. All queries in a transaction are executed together.
  400. * In case of an error all commands will be rolled back so none of the
  401. * SQL in the transaction will affect the DB. Using transactions is
  402. * considered best practice.
  403. * This method has no return value.
  404. *
  405. * @return void
  406. */
  407. public function FailTrans();
  408. }
  409. /**
  410. * PDO Driver
  411. * @file RedBean/PDO.php
  412. * @description PDO Driver
  413. * This Driver implements the RedBean Driver API
  414. * @author Gabor de Mooij and the RedBeanPHP Community, Desfrenes
  415. * @license BSD/GPLv2
  416. *
  417. *
  418. * (c) copyright Desfrenes & Gabor de Mooij and the RedBeanPHP community
  419. * This source file is subject to the BSD/GPLv2 License that is bundled
  420. * with this source code in the file license.txt.
  421. *
  422. */
  423. class RedBean_Driver_PDO implements RedBean_Driver {
  424. /**
  425. * Contains database DSN for connecting to database.
  426. * @var string
  427. */
  428. protected $dsn;
  429. /**
  430. * Whether we are in debugging mode or not.
  431. * @var boolean
  432. */
  433. protected $debug = false;
  434. /**
  435. * Holds an instance of ILogger implementation.
  436. * @var RedBean_ILogger
  437. */
  438. protected $logger = NULL;
  439. /**
  440. * Holds the PDO instance.
  441. * @var PDO
  442. */
  443. protected $pdo;
  444. /**
  445. * Holds integer number of affected rows from latest query
  446. * if driver supports this feature.
  447. * @var integer
  448. */
  449. protected $affected_rows;
  450. /**
  451. * Holds result resource.
  452. * @var integer
  453. */
  454. protected $rs;
  455. /**
  456. * Contains arbitrary connection data.
  457. * @var array
  458. */
  459. protected $connectInfo = array();
  460. /**
  461. * Whether you want to use classic String Only binding -
  462. * backward compatibility.
  463. * @var bool
  464. */
  465. public $flagUseStringOnlyBinding = false;
  466. /**
  467. * Whether we are currently connected or not.
  468. * This flag is being used to delay the connection until necessary.
  469. * Delaying connections is a good practice to speed up scripts that
  470. * don't need database connectivity but for some reason want to
  471. * init RedbeanPHP.
  472. * @var boolean
  473. */
  474. protected $isConnected = false;
  475. /**
  476. * Constructor. You may either specify dsn, user and password or
  477. * just give an existing PDO connection.
  478. * Examples:
  479. * $driver = new RedBean_Driver_PDO($dsn, $user, $password);
  480. * $driver = new RedBean_Driver_PDO($existingConnection);
  481. *
  482. * @param string|PDO $dsn database connection string
  483. * @param string $user optional
  484. * @param string $pass optional
  485. *
  486. * @return void
  487. */
  488. public function __construct($dsn, $user = null, $pass = null) {
  489. if ($dsn instanceof PDO) {
  490. $this->pdo = $dsn;
  491. $this->isConnected = true;
  492. $this->pdo->setAttribute(1002, 'SET NAMES utf8');
  493. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  494. $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  495. // make sure that the dsn at least contains the type
  496. $this->dsn = $this->getDatabaseType();
  497. } else {
  498. $this->dsn = $dsn;
  499. $this->connectInfo = array( 'pass'=>$pass, 'user'=>$user );
  500. }
  501. }
  502. /**
  503. * Establishes a connection to the database using PHP PDO
  504. * functionality. If a connection has already been established this
  505. * method will simply return directly. This method also turns on
  506. * UTF8 for the database and PDO-ERRMODE-EXCEPTION as well as
  507. * PDO-FETCH-ASSOC.
  508. *
  509. * @return void
  510. */
  511. public function connect() {
  512. if ($this->isConnected) return;
  513. $user = $this->connectInfo['user'];
  514. $pass = $this->connectInfo['pass'];
  515. //PDO::MYSQL_ATTR_INIT_COMMAND
  516. $this->pdo = new PDO(
  517. $this->dsn,
  518. $user,
  519. $pass,
  520. array(1002 => 'SET NAMES utf8',
  521. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  522. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  523. )
  524. );
  525. $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
  526. $this->isConnected = true;
  527. }
  528. /**
  529. * Binds parameters. This method binds parameters to a PDOStatement for
  530. * Query Execution. This method binds parameters as NULL, INTEGER or STRING
  531. * and supports both named keys and question mark keys.
  532. *
  533. * @param PDOStatement $s PDO Statement instance
  534. * @param array $aValues values that need to get bound to the statement
  535. *
  536. * @return void
  537. */
  538. protected function bindParams($s,$aValues) {
  539. foreach($aValues as $key=>&$value) {
  540. if (is_integer($key)) {
  541. if (is_null($value)){
  542. $s->bindValue($key+1,null,PDO::PARAM_NULL);
  543. }
  544. elseif (!$this->flagUseStringOnlyBinding && RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
  545. $s->bindParam($key+1,$value,PDO::PARAM_INT);
  546. }
  547. else {
  548. $s->bindParam($key+1,$value,PDO::PARAM_STR);
  549. }
  550. }
  551. else {
  552. if (is_null($value)){
  553. $s->bindValue($key,null,PDO::PARAM_NULL);
  554. }
  555. elseif (!$this->flagUseStringOnlyBinding && RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
  556. $s->bindParam($key,$value,PDO::PARAM_INT);
  557. }
  558. else {
  559. $s->bindParam($key,$value,PDO::PARAM_STR);
  560. }
  561. }
  562. }
  563. }
  564. /**
  565. * Runs a query. Internal function, available for subclasses. This method
  566. * runs the actual SQL query and binds a list of parameters to the query.
  567. * slots. The result of the query will be stored in the protected property
  568. * $rs (always array). The number of rows affected (result of rowcount, if supported by database)
  569. * is stored in protected property $affected_rows. If the debug flag is set
  570. * this function will send debugging output to screen buffer.
  571. *
  572. * @throws RedBean_Exception_SQL
  573. *
  574. * @param string $sql the SQL string to be send to database server
  575. * @param array $aValues the values that need to get bound to the query slots
  576. */
  577. protected function runQuery($sql,$aValues) {
  578. $this->connect();
  579. if ($this->debug && $this->logger) {
  580. $this->logger->log($sql, $aValues);
  581. }
  582. try {
  583. if (strpos('pgsql',$this->dsn)===0) {
  584. $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
  585. }
  586. else {
  587. $s = $this->pdo->prepare($sql);
  588. }
  589. $this->bindParams( $s, $aValues );
  590. $s->execute();
  591. $this->affected_rows = $s->rowCount();
  592. if ($s->columnCount()) {
  593. $this->rs = $s->fetchAll();
  594. if ($this->debug && $this->logger) $this->logger->log('resultset: ' . count($this->rs) . ' rows');
  595. }
  596. else {
  597. $this->rs = array();
  598. }
  599. }catch(PDOException $e) {
  600. //Unfortunately the code field is supposed to be int by default (php)
  601. //So we need a property to convey the SQL State code.
  602. $x = new RedBean_Exception_SQL( $e->getMessage(), 0);
  603. $x->setSQLState( $e->getCode() );
  604. throw $x;
  605. }
  606. }
  607. /**
  608. * Runs a query and fetches results as a multi dimensional array.
  609. *
  610. * @param string $sql SQL to be executed
  611. *
  612. * @return array $results result
  613. */
  614. public function GetAll( $sql, $aValues=array() ) {
  615. $this->runQuery($sql,$aValues);
  616. return $this->rs;
  617. }
  618. /**
  619. * Runs a query and fetches results as a column.
  620. *
  621. * @param string $sql SQL Code to execute
  622. *
  623. * @return array $results Resultset
  624. */
  625. public function GetCol($sql, $aValues=array()) {
  626. $rows = $this->GetAll($sql,$aValues);
  627. $cols = array();
  628. if ($rows && is_array($rows) && count($rows)>0) {
  629. foreach ($rows as $row) {
  630. $cols[] = array_shift($row);
  631. }
  632. }
  633. return $cols;
  634. }
  635. /**
  636. * Runs a query an returns results as a single cell.
  637. *
  638. * @param string $sql SQL to execute
  639. *
  640. * @return mixed $cellvalue result cell
  641. */
  642. public function GetCell($sql, $aValues=array()) {
  643. $arr = $this->GetAll($sql,$aValues);
  644. $row1 = array_shift($arr);
  645. $col1 = array_shift($row1);
  646. return $col1;
  647. }
  648. /**
  649. * Runs a query and returns a flat array containing the values of
  650. * one row.
  651. *
  652. * @param string $sql SQL to execute
  653. *
  654. * @return array $row result row
  655. */
  656. public function GetRow($sql, $aValues=array()) {
  657. $arr = $this->GetAll($sql, $aValues);
  658. return array_shift($arr);
  659. }
  660. /**
  661. * Executes SQL code and allows key-value binding.
  662. * This function allows you to provide an array with values to bind
  663. * to query parameters. For instance you can bind values to question
  664. * marks in the query. Each value in the array corresponds to the
  665. * question mark in the query that matches the position of the value in the
  666. * array. You can also bind values using explicit keys, for instance
  667. * array(":key"=>123) will bind the integer 123 to the key :key in the
  668. * SQL. This method has no return value.
  669. *
  670. * @param string $sql SQL Code to execute
  671. * @param array $aValues Values to bind to SQL query
  672. *
  673. * @return void
  674. */
  675. public function Execute( $sql, $aValues=array() ) {
  676. $this->runQuery($sql,$aValues);
  677. return $this->affected_rows;
  678. }
  679. /**
  680. * Escapes a string for use in SQL using the currently selected
  681. * PDO driver.
  682. *
  683. * @param string $string string to be escaped
  684. *
  685. * @return string $string escaped string
  686. */
  687. public function Escape( $str ) {
  688. $this->connect();
  689. return substr(substr($this->pdo->quote($str), 1), 0, -1);
  690. }
  691. /**
  692. * Returns the latest insert ID if driver does support this
  693. * feature.
  694. *
  695. * @return integer $id primary key ID
  696. */
  697. public function GetInsertID() {
  698. $this->connect();
  699. return (int) $this->pdo->lastInsertId();
  700. }
  701. /**
  702. * Returns the number of rows affected by the most recent query
  703. * if the currently selected PDO driver supports this feature.
  704. *
  705. * @return integer $numOfRows number of rows affected
  706. */
  707. public function Affected_Rows() {
  708. $this->connect();
  709. return (int) $this->affected_rows;
  710. }
  711. /**
  712. * Toggles debug mode. In debug mode the driver will print all
  713. * SQL to the screen together with some information about the
  714. * results. All SQL code that passes through the driver will be
  715. * passes on to the screen for inspection.
  716. * This method has no return value.
  717. *
  718. * Additionally you can inject RedBean_ILogger implementation
  719. * where you can define your own log() method
  720. *
  721. * @param boolean $trueFalse turn on/off
  722. * @param RedBean_ILogger $logger
  723. *
  724. * @return void
  725. */
  726. public function setDebugMode( $tf, $logger = NULL ) {
  727. $this->connect();
  728. $this->debug = (bool)$tf;
  729. if ($this->debug and !$logger) $logger = new RedBean_Logger();
  730. $this->setLogger($logger);
  731. }
  732. /**
  733. * Injects RedBean_ILogger object.
  734. *
  735. * @param RedBean_ILogger $logger
  736. */
  737. public function setLogger( RedBean_ILogger $logger ) {
  738. $this->logger = $logger;
  739. }
  740. /**
  741. * Gets RedBean_ILogger object.
  742. *
  743. * @return RedBean_ILogger
  744. */
  745. public function getLogger() {
  746. return $this->logger;
  747. }
  748. /**
  749. * Starts a transaction.
  750. * This method is part of the transaction mechanism of
  751. * RedBeanPHP. All queries in a transaction are executed together.
  752. * In case of an error all commands will be rolled back so none of the
  753. * SQL in the transaction will affect the DB. Using transactions is
  754. * considered best practice.
  755. * This method has no return value.
  756. *
  757. * @return void
  758. */
  759. public function StartTrans() {
  760. $this->connect();
  761. $this->pdo->beginTransaction();
  762. }
  763. /**
  764. * Commits a transaction.
  765. * This method is part of the transaction mechanism of
  766. * RedBeanPHP. All queries in a transaction are executed together.
  767. * In case of an error all commands will be rolled back so none of the
  768. * SQL in the transaction will affect the DB. Using transactions is
  769. * considered best practice.
  770. * This method has no return value.
  771. *
  772. * @return void
  773. */
  774. public function CommitTrans() {
  775. $this->connect();
  776. $this->pdo->commit();
  777. }
  778. /**
  779. * Rolls back a transaction.
  780. * This method is part of the transaction mechanism of
  781. * RedBeanPHP. All queries in a transaction are executed together.
  782. * In case of an error all commands will be rolled back so none of the
  783. * SQL in the transaction will affect the DB. Using transactions is
  784. * considered best practice.
  785. * This method has no return value.
  786. *
  787. * @return void
  788. */
  789. public function FailTrans() {
  790. $this->connect();
  791. $this->pdo->rollback();
  792. }
  793. /**
  794. * Returns the name of the database type/brand: i.e. mysql, db2 etc.
  795. *
  796. * @return string $typeName database identification
  797. */
  798. public function getDatabaseType() {
  799. $this->connect();
  800. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  801. }
  802. /**
  803. * Returns the version number of the database.
  804. *
  805. * @return mixed $version version number of the database
  806. */
  807. public function getDatabaseVersion() {
  808. $this->connect();
  809. return $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION);
  810. }
  811. /**
  812. * Returns the underlying PHP PDO instance.
  813. *
  814. * @return PDO $pdo PDO instance used by PDO wrapper
  815. */
  816. public function getPDO() {
  817. $this->connect();
  818. return $this->pdo;
  819. }
  820. /**
  821. * Closes database connection by destructing PDO.
  822. */
  823. public function close() {
  824. $this->pdo = null;
  825. $this->isConnected = false;
  826. }
  827. /**
  828. * Returns TRUE if the current PDO instance is connected.
  829. *
  830. * @return boolean $yesNO
  831. */
  832. public function isConnected() {
  833. if (!$this->isConnected && !$this->pdo) return false;
  834. return true;
  835. }
  836. }
  837. /**
  838. * RedBean_OODBBean (Object Oriented DataBase Bean)
  839. *
  840. * @file RedBean/RedBean_OODBBean.php
  841. * @description The Bean class used for passing information
  842. *
  843. * @author Gabor de Mooij and the RedBeanPHP community
  844. * @license BSD/GPLv2
  845. *
  846. *
  847. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
  848. * This source file is subject to the BSD/GPLv2 License that is bundled
  849. * with this source code in the file license.txt.
  850. */
  851. class RedBean_OODBBean implements IteratorAggregate, ArrayAccess, Countable {
  852. /**
  853. * Reference to NULL property for magic getter.
  854. * @var Null $null
  855. */
  856. private $null = null;
  857. /**
  858. * Properties of the bean. These are kept in a private
  859. * array called properties and exposed through the array interface.
  860. * @var array $properties
  861. */
  862. private $properties = array();
  863. /**
  864. * Meta Data storage. This is the internal property where all
  865. * Meta information gets stored.
  866. * @var array
  867. */
  868. private $__info = NULL;
  869. /**
  870. * Contains a BeanHelper to access service objects like
  871. * te association manager and OODB.
  872. * @var RedBean_BeanHelper
  873. */
  874. private $beanHelper = NULL;
  875. /**
  876. * Contains the latest Fetch Type.
  877. * A Fetch Type is a preferred type for the next nested bean.
  878. * @var null
  879. */
  880. private $fetchType = NULL;
  881. /** Returns the alias for a type
  882. *
  883. * @param $type aliased type
  884. *
  885. * @return string $type type
  886. */
  887. private function getAlias( $type ) {
  888. if ($this->fetchType) {
  889. $type = $this->fetchType;
  890. $this->fetchType = null;
  891. }
  892. return $type;
  893. }
  894. /**
  895. * Sets the Bean Helper. Normally the Bean Helper is set by OODB.
  896. * Here you can change the Bean Helper. The Bean Helper is an object
  897. * providing access to a toolbox for the bean necessary to retrieve
  898. * nested beans (bean lists: ownBean,sharedBean) without the need to
  899. * rely on static calls to the facade (or make this class dep. on OODB).
  900. *
  901. * @param RedBean_IBeanHelper $helper
  902. * @return void
  903. */
  904. public function setBeanHelper(RedBean_IBeanHelper $helper) {
  905. $this->beanHelper = $helper;
  906. }
  907. /**
  908. * Returns an ArrayIterator so you can treat the bean like
  909. * an array with the properties container as its contents.
  910. *
  911. * @return ArrayIterator $arrayIt an array iterator instance with $properties
  912. */
  913. public function getIterator() {
  914. return new ArrayIterator($this->properties);
  915. }
  916. /**
  917. * Imports all values in associative array $array. Every key is used
  918. * for a property and every value will be assigned to the property
  919. * identified by the key. So basically this method converts the
  920. * associative array to a bean by loading the array. You can filter
  921. * the values using the $selection parameter. If $selection is boolean
  922. * false, no filtering will be applied. If $selection is an array
  923. * only the properties specified (as values) in the $selection
  924. * array will be taken into account. To skip a property, omit it from
  925. * the $selection array. Also, instead of providing an array you may
  926. * pass a comma separated list of property names. This method is
  927. * chainable because it returns its own object.
  928. * Imports data into bean
  929. *
  930. * @param array $array what you want to import
  931. * @param string|array $selection selection of values
  932. * @param boolean $notrim if TRUE values will not be trimmed
  933. *
  934. * @return RedBean_OODBBean $this
  935. */
  936. public function import( $arr, $selection=false, $notrim=false ) {
  937. if (is_string($selection)) $selection = explode(',',$selection);
  938. //trim whitespaces
  939. if (!$notrim && is_array($selection)) foreach($selection as $k=>$s){ $selection[$k]=trim($s); }
  940. foreach($arr as $k=>$v) {
  941. if ($k!='__info') {
  942. if (!$selection || ($selection && in_array($k,$selection))) {
  943. $this->$k = $v;
  944. }
  945. }
  946. }
  947. return $this;
  948. }
  949. /**
  950. * Very superficial export function
  951. * @return array $properties
  952. */
  953. public function getProperties() {
  954. return $this->properties;
  955. }
  956. /**
  957. * Exports the bean as an array.
  958. * This function exports the contents of a bean to an array and returns
  959. * the resulting array. If $meta eq uals boolean TRUE, then the array will
  960. * also contain the __info section containing the meta data inside the
  961. * RedBean_OODBBean Bean object.
  962. * @param boolean $meta
  963. * @return array $arr
  964. */
  965. public function export($meta = false) {
  966. //$arr = $this->properties;
  967. $arr=array();
  968. foreach($this as $k=>$v) {
  969. if (is_array($v)) foreach($v as $i=>$b) $v[$i]=$b->export();
  970. $arr[$k] = $v;
  971. }
  972. if ($meta) $arr['__info'] = $this->__info;
  973. return $arr;
  974. }
  975. /**
  976. * Exports the bean to an object.
  977. * This function exports the contents of a bean to an object.
  978. * @param object $obj
  979. * @return array $arr
  980. */
  981. public function exportToObj($obj) {
  982. foreach($this->properties as $k=>$v) {
  983. if (!is_array($v) && !is_object($v))
  984. $obj->$k = $v;
  985. }
  986. }
  987. /**
  988. * Implements isset() function for use as an array.
  989. * Returns whether bean has an element with key
  990. * named $property. Returns TRUE if such an element exists
  991. * and FALSE otherwise.
  992. * @param string $property
  993. * @return boolean $hasProperty
  994. */
  995. public function __isset($property) {
  996. return (isset($this->properties[$property]));
  997. }
  998. /**
  999. * Returns the ID of the bean no matter what the ID field is.
  1000. *
  1001. * @return string $id record Identifier for bean
  1002. */
  1003. public function getID() {
  1004. return (string) $this->id;
  1005. }
  1006. /**
  1007. * Unsets a property. This method will load the property first using
  1008. * __get.
  1009. *
  1010. * @param string $property property
  1011. *
  1012. * @return void
  1013. */
  1014. public function __unset($property) {
  1015. $this->__get($property);
  1016. $fieldLink = $property.'_id';
  1017. if (isset($this->$fieldLink)) {
  1018. //wanna unset a bean reference?
  1019. $this->$fieldLink = null;
  1020. }
  1021. if ((isset($this->properties[$property]))) {
  1022. unset($this->properties[$property]);
  1023. }
  1024. }
  1025. /**
  1026. * Removes a property from the properties list without invoking
  1027. * an __unset on the bean.
  1028. *
  1029. * @param string $property property that needs to be unset
  1030. *
  1031. * @return void
  1032. */
  1033. public function removeProperty( $property ) {
  1034. unset($this->properties[$property]);
  1035. }
  1036. /**
  1037. * Magic Getter. Gets the value for a specific property in the bean.
  1038. * If the property does not exist this getter will make sure no error
  1039. * occurs. This is because RedBean allows you to query (probe) for
  1040. * properties. If the property can not be found this method will
  1041. * return NULL instead.
  1042. * @param string $property
  1043. * @return mixed $value
  1044. */
  1045. public function &__get( $property ) {
  1046. if ($this->beanHelper)
  1047. $toolbox = $this->beanHelper->getToolbox();
  1048. if (!isset($this->properties[$property])) {
  1049. $fieldLink = $property.'_id';
  1050. /**
  1051. * All this magic can be become very complex quicly. For instance,
  1052. * my PHP CLI produced a segfault while testing this code. Turns out that
  1053. * if fieldlink equals idfield, scripts tend to recusrively load beans and
  1054. * instead of giving a clue they simply crash and burn isnt that nice?
  1055. */
  1056. if (isset($this->$fieldLink) && $fieldLink != $this->getMeta('sys.idfield')) {
  1057. $this->setMeta('tainted',true);
  1058. $type = $this->getAlias($property);
  1059. $targetType = $this->properties[$fieldLink];
  1060. $bean = $toolbox->getRedBean()->load($type,$targetType);
  1061. //return $bean;
  1062. $this->properties[$property] = $bean;
  1063. return $this->properties[$property];
  1064. }
  1065. if (strpos($property,'own')===0) {
  1066. $firstCharCode = ord(substr($property,3,1));
  1067. if ($firstCharCode>=65 && $firstCharCode<=90) {
  1068. $type = (__lcfirst(str_replace('own','',$property)));
  1069. $myFieldLink = $this->getMeta('type').'_id';
  1070. $beans = $toolbox->getRedBean()->find($type,array(),array(" $myFieldLink = ? ",array($this->getID())));
  1071. $this->properties[$property] = $beans;
  1072. $this->setMeta('sys.shadow.'.$property,$beans);
  1073. $this->setMeta('tainted',true);
  1074. return $this->properties[$property];
  1075. }
  1076. }
  1077. if (strpos($property,'shared')===0) {
  1078. $firstCharCode = ord(substr($property,6,1));
  1079. if ($firstCharCode>=65 && $firstCharCode<=90) {
  1080. $type = (__lcfirst(str_replace('shared','',$property)));
  1081. $keys = $toolbox->getRedBean()->getAssociationManager()->related($this,$type);
  1082. if (!count($keys)) $beans = array(); else
  1083. $beans = $toolbox->getRedBean()->batch($type,$keys);
  1084. $this->properties[$property] = $beans;
  1085. $this->setMeta('sys.shadow.'.$property,$beans);
  1086. $this->setMeta('tainted',true);
  1087. return $this->properties[$property];
  1088. }
  1089. }
  1090. return $this->null;
  1091. }
  1092. return $this->properties[$property];
  1093. }
  1094. /**
  1095. * Magic Setter. Sets the value for a specific property.
  1096. * This setter acts as a hook for OODB to mark beans as tainted.
  1097. * The tainted meta property can be retrieved using getMeta("tainted").
  1098. * The tainted meta property indicates whether a bean has been modified and
  1099. * can be used in various caching mechanisms.
  1100. * @param string $property
  1101. * @param mixed $value
  1102. */
  1103. public function __set($property,$value) {
  1104. $this->__get($property);
  1105. $this->setMeta('tainted',true);
  1106. $linkField = $property.'_id';
  1107. if (isset($this->properties[$linkField]) && !($value instanceof RedBean_OODBBean)) {
  1108. if (is_null($value) || $value === false) {
  1109. return $this->__unset($property);
  1110. }
  1111. else {
  1112. throw new RedBean_Exception_Security('Cannot cast to bean.');
  1113. }
  1114. }
  1115. if ($value===false) {
  1116. $value = '0';
  1117. }
  1118. if ($value===true) {
  1119. $value = '1';
  1120. }
  1121. $this->properties[$property] = $value;
  1122. }
  1123. /**
  1124. * Returns the value of a meta property. A meta property
  1125. * contains extra information about the bean object that will not
  1126. * get stored in the database. Meta information is used to instruct
  1127. * RedBean as well as other systems how to deal with the bean.
  1128. * For instance: $bean->setMeta("buildcommand.unique", array(
  1129. * array("column1", "column2", "column3") ) );
  1130. * Will add a UNIQUE constaint for the bean on columns: column1, column2 and
  1131. * column 3.
  1132. * To access a Meta property we use a dot separated notation.
  1133. * If the property cannot be found this getter will return NULL instead.
  1134. * @param string $path
  1135. * @param mixed $default
  1136. * @return mixed $value
  1137. */
  1138. public function getMeta($path,$default = NULL) {
  1139. return (isset($this->__info[$path])) ? $this->__info[$path] : $default;
  1140. }
  1141. /**
  1142. * Stores a value in the specified Meta information property. $value contains
  1143. * the value you want to store in the Meta section of the bean and $path
  1144. * specifies the dot separated path to the property. For instance "my.meta.property".
  1145. * If "my" and "meta" do not exist they will be created automatically.
  1146. * @param string $path
  1147. * @param mixed $value
  1148. */
  1149. public function setMeta($path,$value) {
  1150. $this->__info[$path] = $value;
  1151. }
  1152. /**
  1153. * Copies the meta information of the specified bean
  1154. * This is a convenience method to enable you to
  1155. * exchange meta information easily.
  1156. * @param RedBean_OODBBean $bean
  1157. * @return RedBean_OODBBean
  1158. */
  1159. public function copyMetaFrom(RedBean_OODBBean $bean) {
  1160. $this->__info = $bean->__info;
  1161. return $this;
  1162. }
  1163. /**
  1164. * Reroutes a call to Model if exists. (new fuse)
  1165. * @param string $method
  1166. * @param array $args
  1167. * @return mixed $mixed
  1168. */
  1169. public function __call($method, $args) {
  1170. return null;
  1171. if (!isset($this->__info['model'])) {
  1172. $model = $this->beanHelper->getModelForBean($this);
  1173. if (!$model) return;
  1174. $this->__info['model'] = $model;
  1175. }
  1176. if (!method_exists($this->__info['model'],$method)) return null;
  1177. return call_user_func_array(array($this->__info['model'],$method), $args);
  1178. }
  1179. /**
  1180. * Implementation of __toString Method
  1181. * Routes call to Model.
  1182. * @return string $string
  1183. */
  1184. public function __toString() {
  1185. $string = $this->__call('__toString',array());
  1186. if ($string === null) {
  1187. return json_encode($this->properties);
  1188. }
  1189. else {
  1190. return $string;
  1191. }
  1192. }
  1193. /**
  1194. * Implementation of Array Access Interface, you can access bean objects
  1195. * like an array.
  1196. * Call gets routed to __set.
  1197. *
  1198. * @param mixed $offset offset string
  1199. * @param mixed $value value
  1200. *
  1201. * @return void
  1202. */
  1203. public function offsetSet($offset, $value) {
  1204. $this->__set($offset, $value);
  1205. }
  1206. /**
  1207. * Implementation of Array Access Interface, you can access bean objects
  1208. * like an array.
  1209. *
  1210. * @param mixed $offset property
  1211. *
  1212. * @return
  1213. */
  1214. public function offsetExists($offset) {
  1215. return isset($this->properties[$offset]);
  1216. }
  1217. /**
  1218. * Implementation of Array Access Interface, you can access bean objects
  1219. * like an array.
  1220. * Unsets a value from the array/bean.
  1221. *
  1222. * @param mixed $offset property
  1223. *
  1224. * @return
  1225. */
  1226. public function offsetUnset($offset) {
  1227. unset($this->properties[$offset]);
  1228. }
  1229. /**
  1230. * Implementation of Array Access Interface, you can access bean objects
  1231. * like an array.
  1232. * Returns value of a property.
  1233. *
  1234. * @param mixed $offset property
  1235. *
  1236. * @return
  1237. */
  1238. public function offsetGet($offset) {
  1239. return $this->__get($offset);
  1240. }
  1241. /**
  1242. * Chainable method to cast a certain ID to a bean; for instance:
  1243. * $person = $club->fetchAs('person')->member;
  1244. * This will load a bean of type person using member_id as ID.
  1245. *
  1246. * @param string $type preferred fetch type
  1247. *
  1248. * @return RedBean_OODBBean
  1249. */
  1250. public function fetchAs($type) {
  1251. $this->fetchType = $type;
  1252. return $this;
  1253. }
  1254. /**
  1255. * Implementation of Countable interface. Makes it possible to use
  1256. * count() function on a bean.
  1257. *
  1258. * @return integer $numberOfProperties number of properties in the bean.
  1259. */
  1260. public function count() {
  1261. return count($this->properties);
  1262. }
  1263. /**
  1264. * Checks wether a bean is empty or not.
  1265. * A bean is empty if it has no other properties than the id field OR
  1266. * if all the other property are empty().
  1267. *
  1268. * @return boolean
  1269. */
  1270. public function isEmpty() {
  1271. $empty = true;
  1272. foreach($this->properties as $key=>$value) {
  1273. if ($key=='id') continue;
  1274. if (!empty($value)) {
  1275. $empty = false;
  1276. }
  1277. }
  1278. return $empty;
  1279. }
  1280. }
  1281. /**
  1282. * Observable
  1283. * Base class for Observables
  1284. *
  1285. * @file RedBean/Observable.php
  1286. * @description Part of the observer pattern in RedBean
  1287. *
  1288. * @author Gabor de Mooij and the RedBeanPHP community
  1289. * @license BSD/GPLv2
  1290. *
  1291. *
  1292. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
  1293. * This source file is subject to the BSD/GPLv2 License that is bundled
  1294. * with this source code in the file license.txt.
  1295. */
  1296. abstract class RedBean_Observable {
  1297. /**
  1298. * Array that keeps track of observers.…

Large files files are truncated, but you can click here to view the full file