PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/redbean/rb.php

https://bitbucket.org/andreustimm/zurmo
PHP | 8670 lines | 3180 code | 737 blank | 4753 comment | 480 complexity | c8e426c85ddb14355681316e1fd202a1 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause

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. PDO::MYSQL_ATTR_LOCAL_INFILE => true,
  524. )
  525. );
  526. $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
  527. $this->isConnected = true;
  528. }
  529. /**
  530. * Binds parameters. This method binds parameters to a PDOStatement for
  531. * Query Execution. This method binds parameters as NULL, INTEGER or STRING
  532. * and supports both named keys and question mark keys.
  533. *
  534. * @param PDOStatement $s PDO Statement instance
  535. * @param array $aValues values that need to get bound to the statement
  536. *
  537. * @return void
  538. */
  539. protected function bindParams($s,$aValues) {
  540. foreach($aValues as $key=>&$value) {
  541. if (is_integer($key)) {
  542. if (is_null($value)){
  543. $s->bindValue($key+1,null,PDO::PARAM_NULL);
  544. }
  545. elseif (!$this->flagUseStringOnlyBinding && RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
  546. $s->bindParam($key+1,$value,PDO::PARAM_INT);
  547. }
  548. else {
  549. $s->bindParam($key+1,$value,PDO::PARAM_STR);
  550. }
  551. }
  552. else {
  553. if (is_null($value)){
  554. $s->bindValue($key,null,PDO::PARAM_NULL);
  555. }
  556. elseif (!$this->flagUseStringOnlyBinding && RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
  557. $s->bindParam($key,$value,PDO::PARAM_INT);
  558. }
  559. else {
  560. $s->bindParam($key,$value,PDO::PARAM_STR);
  561. }
  562. }
  563. }
  564. }
  565. /**
  566. * Runs a query. Internal function, available for subclasses. This method
  567. * runs the actual SQL query and binds a list of parameters to the query.
  568. * slots. The result of the query will be stored in the protected property
  569. * $rs (always array). The number of rows affected (result of rowcount, if supported by database)
  570. * is stored in protected property $affected_rows. If the debug flag is set
  571. * this function will send debugging output to screen buffer.
  572. *
  573. * @throws RedBean_Exception_SQL
  574. *
  575. * @param string $sql the SQL string to be send to database server
  576. * @param array $aValues the values that need to get bound to the query slots
  577. */
  578. protected function runQuery($sql,$aValues) {
  579. $this->connect();
  580. if ($this->debug && $this->logger) {
  581. $this->logger->log($sql, $aValues);
  582. }
  583. try {
  584. if (strpos('pgsql',$this->dsn)===0) {
  585. $s = $this->pdo->prepare($sql, array(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true));
  586. }
  587. else {
  588. $s = $this->pdo->prepare($sql);
  589. }
  590. $this->bindParams( $s, $aValues );
  591. $s->execute();
  592. $this->affected_rows = $s->rowCount();
  593. if ($s->columnCount()) {
  594. $this->rs = $s->fetchAll();
  595. if ($this->debug && $this->logger) $this->logger->log('resultset: ' . count($this->rs) . ' rows');
  596. }
  597. else {
  598. $this->rs = array();
  599. }
  600. }catch(PDOException $e) {
  601. //Unfortunately the code field is supposed to be int by default (php)
  602. //So we need a property to convey the SQL State code.
  603. $x = new RedBean_Exception_SQL( $e->getMessage(), 0);
  604. $x->setSQLState( $e->getCode() );
  605. throw $x;
  606. }
  607. }
  608. /**
  609. * Runs a query and fetches results as a multi dimensional array.
  610. *
  611. * @param string $sql SQL to be executed
  612. *
  613. * @return array $results result
  614. */
  615. public function GetAll( $sql, $aValues=array() ) {
  616. $this->runQuery($sql,$aValues);
  617. return $this->rs;
  618. }
  619. /**
  620. * Runs a query and fetches results as a column.
  621. *
  622. * @param string $sql SQL Code to execute
  623. *
  624. * @return array $results Resultset
  625. */
  626. public function GetCol($sql, $aValues=array()) {
  627. $rows = $this->GetAll($sql,$aValues);
  628. $cols = array();
  629. if ($rows && is_array($rows) && count($rows)>0) {
  630. foreach ($rows as $row) {
  631. $cols[] = array_shift($row);
  632. }
  633. }
  634. return $cols;
  635. }
  636. /**
  637. * Runs a query an returns results as a single cell.
  638. *
  639. * @param string $sql SQL to execute
  640. *
  641. * @return mixed $cellvalue result cell
  642. */
  643. public function GetCell($sql, $aValues=array()) {
  644. $arr = $this->GetAll($sql,$aValues);
  645. $row1 = array_shift($arr);
  646. $col1 = array_shift($row1);
  647. return $col1;
  648. }
  649. /**
  650. * Runs a query and returns a flat array containing the values of
  651. * one row.
  652. *
  653. * @param string $sql SQL to execute
  654. *
  655. * @return array $row result row
  656. */
  657. public function GetRow($sql, $aValues=array()) {
  658. $arr = $this->GetAll($sql, $aValues);
  659. return array_shift($arr);
  660. }
  661. /**
  662. * Executes SQL code and allows key-value binding.
  663. * This function allows you to provide an array with values to bind
  664. * to query parameters. For instance you can bind values to question
  665. * marks in the query. Each value in the array corresponds to the
  666. * question mark in the query that matches the position of the value in the
  667. * array. You can also bind values using explicit keys, for instance
  668. * array(":key"=>123) will bind the integer 123 to the key :key in the
  669. * SQL. This method has no return value.
  670. *
  671. * @param string $sql SQL Code to execute
  672. * @param array $aValues Values to bind to SQL query
  673. *
  674. * @return void
  675. */
  676. public function Execute( $sql, $aValues=array() ) {
  677. $this->runQuery($sql,$aValues);
  678. return $this->affected_rows;
  679. }
  680. /**
  681. * Escapes a string for use in SQL using the currently selected
  682. * PDO driver.
  683. *
  684. * @param string $string string to be escaped
  685. *
  686. * @return string $string escaped string
  687. */
  688. public function Escape( $str ) {
  689. $this->connect();
  690. return substr(substr($this->pdo->quote($str), 1), 0, -1);
  691. }
  692. /**
  693. * Returns the latest insert ID if driver does support this
  694. * feature.
  695. *
  696. * @return integer $id primary key ID
  697. */
  698. public function GetInsertID() {
  699. $this->connect();
  700. return (int) $this->pdo->lastInsertId();
  701. }
  702. /**
  703. * Returns the number of rows affected by the most recent query
  704. * if the currently selected PDO driver supports this feature.
  705. *
  706. * @return integer $numOfRows number of rows affected
  707. */
  708. public function Affected_Rows() {
  709. $this->connect();
  710. return (int) $this->affected_rows;
  711. }
  712. /**
  713. * Toggles debug mode. In debug mode the driver will print all
  714. * SQL to the screen together with some information about the
  715. * results. All SQL code that passes through the driver will be
  716. * passes on to the screen for inspection.
  717. * This method has no return value.
  718. *
  719. * Additionally you can inject RedBean_ILogger implementation
  720. * where you can define your own log() method
  721. *
  722. * @param boolean $trueFalse turn on/off
  723. * @param RedBean_ILogger $logger
  724. *
  725. * @return void
  726. */
  727. public function setDebugMode( $tf, $logger = NULL ) {
  728. $this->connect();
  729. $this->debug = (bool)$tf;
  730. if ($this->debug and !$logger) $logger = new RedBean_Logger();
  731. $this->setLogger($logger);
  732. }
  733. /**
  734. * Injects RedBean_ILogger object.
  735. *
  736. * @param RedBean_ILogger $logger
  737. */
  738. public function setLogger( RedBean_ILogger $logger ) {
  739. $this->logger = $logger;
  740. }
  741. /**
  742. * Gets RedBean_ILogger object.
  743. *
  744. * @return RedBean_ILogger
  745. */
  746. public function getLogger() {
  747. return $this->logger;
  748. }
  749. /**
  750. * Starts a transaction.
  751. * This method is part of the transaction mechanism of
  752. * RedBeanPHP. All queries in a transaction are executed together.
  753. * In case of an error all commands will be rolled back so none of the
  754. * SQL in the transaction will affect the DB. Using transactions is
  755. * considered best practice.
  756. * This method has no return value.
  757. *
  758. * @return void
  759. */
  760. public function StartTrans() {
  761. $this->connect();
  762. $this->pdo->beginTransaction();
  763. }
  764. /**
  765. * Commits a transaction.
  766. * This method is part of the transaction mechanism of
  767. * RedBeanPHP. All queries in a transaction are executed together.
  768. * In case of an error all commands will be rolled back so none of the
  769. * SQL in the transaction will affect the DB. Using transactions is
  770. * considered best practice.
  771. * This method has no return value.
  772. *
  773. * @return void
  774. */
  775. public function CommitTrans() {
  776. $this->connect();
  777. $this->pdo->commit();
  778. }
  779. /**
  780. * Rolls back a transaction.
  781. * This method is part of the transaction mechanism of
  782. * RedBeanPHP. All queries in a transaction are executed together.
  783. * In case of an error all commands will be rolled back so none of the
  784. * SQL in the transaction will affect the DB. Using transactions is
  785. * considered best practice.
  786. * This method has no return value.
  787. *
  788. * @return void
  789. */
  790. public function FailTrans() {
  791. $this->connect();
  792. $this->pdo->rollback();
  793. }
  794. /**
  795. * Returns the name of the database type/brand: i.e. mysql, db2 etc.
  796. *
  797. * @return string $typeName database identification
  798. */
  799. public function getDatabaseType() {
  800. $this->connect();
  801. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  802. }
  803. /**
  804. * Returns the version number of the database.
  805. *
  806. * @return mixed $version version number of the database
  807. */
  808. public function getDatabaseVersion() {
  809. $this->connect();
  810. return $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION);
  811. }
  812. /**
  813. * Returns the underlying PHP PDO instance.
  814. *
  815. * @return PDO $pdo PDO instance used by PDO wrapper
  816. */
  817. public function getPDO() {
  818. $this->connect();
  819. return $this->pdo;
  820. }
  821. /**
  822. * Closes database connection by destructing PDO.
  823. */
  824. public function close() {
  825. $this->pdo = null;
  826. $this->isConnected = false;
  827. }
  828. /**
  829. * Returns TRUE if the current PDO instance is connected.
  830. *
  831. * @return boolean $yesNO
  832. */
  833. public function isConnected() {
  834. if (!$this->isConnected && !$this->pdo) return false;
  835. return true;
  836. }
  837. }
  838. /**
  839. * RedBean_OODBBean (Object Oriented DataBase Bean)
  840. *
  841. * @file RedBean/RedBean_OODBBean.php
  842. * @description The Bean class used for passing information
  843. *
  844. * @author Gabor de Mooij and the RedBeanPHP community
  845. * @license BSD/GPLv2
  846. *
  847. *
  848. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
  849. * This source file is subject to the BSD/GPLv2 License that is bundled
  850. * with this source code in the file license.txt.
  851. */
  852. class RedBean_OODBBean implements IteratorAggregate, ArrayAccess, Countable {
  853. /**
  854. * Reference to NULL property for magic getter.
  855. * @var Null $null
  856. */
  857. private $null = null;
  858. /**
  859. * Properties of the bean. These are kept in a private
  860. * array called properties and exposed through the array interface.
  861. * @var array $properties
  862. */
  863. private $properties = array();
  864. /**
  865. * Meta Data storage. This is the internal property where all
  866. * Meta information gets stored.
  867. * @var array
  868. */
  869. private $__info = NULL;
  870. /**
  871. * Contains a BeanHelper to access service objects like
  872. * te association manager and OODB.
  873. * @var RedBean_BeanHelper
  874. */
  875. private $beanHelper = NULL;
  876. /**
  877. * Contains the latest Fetch Type.
  878. * A Fetch Type is a preferred type for the next nested bean.
  879. * @var null
  880. */
  881. private $fetchType = NULL;
  882. /** Returns the alias for a type
  883. *
  884. * @param $type aliased type
  885. *
  886. * @return string $type type
  887. */
  888. private function getAlias( $type ) {
  889. if ($this->fetchType) {
  890. $type = $this->fetchType;
  891. $this->fetchType = null;
  892. }
  893. return $type;
  894. }
  895. /**
  896. * Sets the Bean Helper. Normally the Bean Helper is set by OODB.
  897. * Here you can change the Bean Helper. The Bean Helper is an object
  898. * providing access to a toolbox for the bean necessary to retrieve
  899. * nested beans (bean lists: ownBean,sharedBean) without the need to
  900. * rely on static calls to the facade (or make this class dep. on OODB).
  901. *
  902. * @param RedBean_IBeanHelper $helper
  903. * @return void
  904. */
  905. public function setBeanHelper(RedBean_IBeanHelper $helper) {
  906. $this->beanHelper = $helper;
  907. }
  908. /**
  909. * Returns an ArrayIterator so you can treat the bean like
  910. * an array with the properties container as its contents.
  911. *
  912. * @return ArrayIterator $arrayIt an array iterator instance with $properties
  913. */
  914. public function getIterator() {
  915. return new ArrayIterator($this->properties);
  916. }
  917. /**
  918. * Imports all values in associative array $array. Every key is used
  919. * for a property and every value will be assigned to the property
  920. * identified by the key. So basically this method converts the
  921. * associative array to a bean by loading the array. You can filter
  922. * the values using the $selection parameter. If $selection is boolean
  923. * false, no filtering will be applied. If $selection is an array
  924. * only the properties specified (as values) in the $selection
  925. * array will be taken into account. To skip a property, omit it from
  926. * the $selection array. Also, instead of providing an array you may
  927. * pass a comma separated list of property names. This method is
  928. * chainable because it returns its own object.
  929. * Imports data into bean
  930. *
  931. * @param array $array what you want to import
  932. * @param string|array $selection selection of values
  933. * @param boolean $notrim if TRUE values will not be trimmed
  934. *
  935. * @return RedBean_OODBBean $this
  936. */
  937. public function import( $arr, $selection=false, $notrim=false ) {
  938. if (is_string($selection)) $selection = explode(',',$selection);
  939. //trim whitespaces
  940. if (!$notrim && is_array($selection)) foreach($selection as $k=>$s){ $selection[$k]=trim($s); }
  941. foreach($arr as $k=>$v) {
  942. if ($k!='__info') {
  943. if (!$selection || ($selection && in_array($k,$selection))) {
  944. $this->$k = $v;
  945. }
  946. }
  947. }
  948. return $this;
  949. }
  950. /**
  951. * Very superficial export function
  952. * @return array $properties
  953. */
  954. public function getProperties() {
  955. return $this->properties;
  956. }
  957. /**
  958. * Exports the bean as an array.
  959. * This function exports the contents of a bean to an array and returns
  960. * the resulting array. If $meta eq uals boolean TRUE, then the array will
  961. * also contain the __info section containing the meta data inside the
  962. * RedBean_OODBBean Bean object.
  963. * @param boolean $meta
  964. * @return array $arr
  965. */
  966. public function export($meta = false) {
  967. //$arr = $this->properties;
  968. $arr=array();
  969. foreach($this as $k=>$v) {
  970. if (is_array($v)) foreach($v as $i=>$b) $v[$i]=$b->export();
  971. $arr[$k] = $v;
  972. }
  973. if ($meta) $arr['__info'] = $this->__info;
  974. return $arr;
  975. }
  976. /**
  977. * Exports the bean to an object.
  978. * This function exports the contents of a bean to an object.
  979. * @param object $obj
  980. * @return array $arr
  981. */
  982. public function exportToObj($obj) {
  983. foreach($this->properties as $k=>$v) {
  984. if (!is_array($v) && !is_object($v))
  985. $obj->$k = $v;
  986. }
  987. }
  988. /**
  989. * Implements isset() function for use as an array.
  990. * Returns whether bean has an element with key
  991. * named $property. Returns TRUE if such an element exists
  992. * and FALSE otherwise.
  993. * @param string $property
  994. * @return boolean $hasProperty
  995. */
  996. public function __isset($property) {
  997. return (isset($this->properties[$property]));
  998. }
  999. /**
  1000. * Returns the ID of the bean no matter what the ID field is.
  1001. *
  1002. * @return string $id record Identifier for bean
  1003. */
  1004. public function getID() {
  1005. return (string) $this->id;
  1006. }
  1007. /**
  1008. * Unsets a property. This method will load the property first using
  1009. * __get.
  1010. *
  1011. * @param string $property property
  1012. *
  1013. * @return void
  1014. */
  1015. public function __unset($property) {
  1016. $this->__get($property);
  1017. $fieldLink = $property.'_id';
  1018. if (isset($this->$fieldLink)) {
  1019. //wanna unset a bean reference?
  1020. $this->$fieldLink = null;
  1021. }
  1022. if ((isset($this->properties[$property]))) {
  1023. unset($this->properties[$property]);
  1024. }
  1025. }
  1026. /**
  1027. * Removes a property from the properties list without invoking
  1028. * an __unset on the bean.
  1029. *
  1030. * @param string $property property that needs to be unset
  1031. *
  1032. * @return void
  1033. */
  1034. public function removeProperty( $property ) {
  1035. unset($this->properties[$property]);
  1036. }
  1037. /**
  1038. * Magic Getter. Gets the value for a specific property in the bean.
  1039. * If the property does not exist this getter will make sure no error
  1040. * occurs. This is because RedBean allows you to query (probe) for
  1041. * properties. If the property can not be found this method will
  1042. * return NULL instead.
  1043. * @param string $property
  1044. * @return mixed $value
  1045. */
  1046. public function &__get( $property ) {
  1047. if ($this->beanHelper)
  1048. $toolbox = $this->beanHelper->getToolbox();
  1049. if (!isset($this->properties[$property])) {
  1050. $fieldLink = $property.'_id';
  1051. /**
  1052. * All this magic can be become very complex quicly. For instance,
  1053. * my PHP CLI produced a segfault while testing this code. Turns out that
  1054. * if fieldlink equals idfield, scripts tend to recusrively load beans and
  1055. * instead of giving a clue they simply crash and burn isnt that nice?
  1056. */
  1057. if (isset($this->$fieldLink) && $fieldLink != $this->getMeta('sys.idfield')) {
  1058. $this->setMeta('tainted',true);
  1059. $type = $this->getAlias($property);
  1060. $targetType = $this->properties[$fieldLink];
  1061. $bean = $toolbox->getRedBean()->load($type,$targetType);
  1062. //return $bean;
  1063. $this->properties[$property] = $bean;
  1064. return $this->properties[$property];
  1065. }
  1066. if (strpos($property,'own')===0) {
  1067. $firstCharCode = ord(substr($property,3,1));
  1068. if ($firstCharCode>=65 && $firstCharCode<=90) {
  1069. $type = (__lcfirst(str_replace('own','',$property)));
  1070. $myFieldLink = $this->getMeta('type').'_id';
  1071. $beans = $toolbox->getRedBean()->find($type,array(),array(" $myFieldLink = ? ",array($this->getID())));
  1072. $this->properties[$property] = $beans;
  1073. $this->setMeta('sys.shadow.'.$property,$beans);
  1074. $this->setMeta('tainted',true);
  1075. return $this->properties[$property];
  1076. }
  1077. }
  1078. if (strpos($property,'shared')===0) {
  1079. $firstCharCode = ord(substr($property,6,1));
  1080. if ($firstCharCode>=65 && $firstCharCode<=90) {
  1081. $type = (__lcfirst(str_replace('shared','',$property)));
  1082. $keys = $toolbox->getRedBean()->getAssociationManager()->related($this,$type);
  1083. if (!count($keys)) $beans = array(); else
  1084. $beans = $toolbox->getRedBean()->batch($type,$keys);
  1085. $this->properties[$property] = $beans;
  1086. $this->setMeta('sys.shadow.'.$property,$beans);
  1087. $this->setMeta('tainted',true);
  1088. return $this->properties[$property];
  1089. }
  1090. }
  1091. return $this->null;
  1092. }
  1093. return $this->properties[$property];
  1094. }
  1095. /**
  1096. * Magic Setter. Sets the value for a specific property.
  1097. * This setter acts as a hook for OODB to mark beans as tainted.
  1098. * The tainted meta property can be retrieved using getMeta("tainted").
  1099. * The tainted meta property indicates whether a bean has been modified and
  1100. * can be used in various caching mechanisms.
  1101. * @param string $property
  1102. * @param mixed $value
  1103. */
  1104. public function __set($property,$value) {
  1105. $this->__get($property);
  1106. $this->setMeta('tainted',true);
  1107. $linkField = $property.'_id';
  1108. if (isset($this->properties[$linkField]) && !($value instanceof RedBean_OODBBean)) {
  1109. if (is_null($value) || $value === false) {
  1110. return $this->__unset($property);
  1111. }
  1112. else {
  1113. throw new RedBean_Exception_Security('Cannot cast to bean.');
  1114. }
  1115. }
  1116. if ($value===false) {
  1117. $value = '0';
  1118. }
  1119. if ($value===true) {
  1120. $value = '1';
  1121. }
  1122. $this->properties[$property] = $value;
  1123. }
  1124. /**
  1125. * Returns the value of a meta property. A meta property
  1126. * contains extra information about the bean object that will not
  1127. * get stored in the database. Meta information is used to instruct
  1128. * RedBean as well as other systems how to deal with the bean.
  1129. * For instance: $bean->setMeta("buildcommand.unique", array(
  1130. * array("column1", "column2", "column3") ) );
  1131. * Will add a UNIQUE constaint for the bean on columns: column1, column2 and
  1132. * column 3.
  1133. * To access a Meta property we use a dot separated notation.
  1134. * If the property cannot be found this getter will return NULL instead.
  1135. * @param string $path
  1136. * @param mixed $default
  1137. * @return mixed $value
  1138. */
  1139. public function getMeta($path,$default = NULL) {
  1140. return (isset($this->__info[$path])) ? $this->__info[$path] : $default;
  1141. }
  1142. /**
  1143. * Stores a value in the specified Meta information property. $value contains
  1144. * the value you want to store in the Meta section of the bean and $path
  1145. * specifies the dot separated path to the property. For instance "my.meta.property".
  1146. * If "my" and "meta" do not exist they will be created automatically.
  1147. * @param string $path
  1148. * @param mixed $value
  1149. */
  1150. public function setMeta($path,$value) {
  1151. $this->__info[$path] = $value;
  1152. }
  1153. /**
  1154. * Copies the meta information of the specified bean
  1155. * This is a convenience method to enable you to
  1156. * exchange meta information easily.
  1157. * @param RedBean_OODBBean $bean
  1158. * @return RedBean_OODBBean
  1159. */
  1160. public function copyMetaFrom(RedBean_OODBBean $bean) {
  1161. $this->__info = $bean->__info;
  1162. return $this;
  1163. }
  1164. /**
  1165. * Reroutes a call to Model if exists. (new fuse)
  1166. * @param string $method
  1167. * @param array $args
  1168. * @return mixed $mixed
  1169. */
  1170. public function __call($method, $args) {
  1171. return null;
  1172. if (!isset($this->__info['model'])) {
  1173. $model = $this->beanHelper->getModelForBean($this);
  1174. if (!$model) return;
  1175. $this->__info['model'] = $model;
  1176. }
  1177. if (!method_exists($this->__info['model'],$method)) return null;
  1178. return call_user_func_array(array($this->__info['model'],$method), $args);
  1179. }
  1180. /**
  1181. * Implementation of __toString Method
  1182. * Routes call to Model.
  1183. * @return string $string
  1184. */
  1185. public function __toString() {
  1186. $string = $this->__call('__toString',array());
  1187. if ($string === null) {
  1188. return json_encode($this->properties);
  1189. }
  1190. else {
  1191. return $string;
  1192. }
  1193. }
  1194. /**
  1195. * Implementation of Array Access Interface, you can access bean objects
  1196. * like an array.
  1197. * Call gets routed to __set.
  1198. *
  1199. * @param mixed $offset offset string
  1200. * @param mixed $value value
  1201. *
  1202. * @return void
  1203. */
  1204. public function offsetSet($offset, $value) {
  1205. $this->__set($offset, $value);
  1206. }
  1207. /**
  1208. * Implementation of Array Access Interface, you can access bean objects
  1209. * like an array.
  1210. *
  1211. * @param mixed $offset property
  1212. *
  1213. * @return
  1214. */
  1215. public function offsetExists($offset) {
  1216. return isset($this->properties[$offset]);
  1217. }
  1218. /**
  1219. * Implementation of Array Access Interface, you can access bean objects
  1220. * like an array.
  1221. * Unsets a value from the array/bean.
  1222. *
  1223. * @param mixed $offset property
  1224. *
  1225. * @return
  1226. */
  1227. public function offsetUnset($offset) {
  1228. unset($this->properties[$offset]);
  1229. }
  1230. /**
  1231. * Implementation of Array Access Interface, you can access bean objects
  1232. * like an array.
  1233. * Returns value of a property.
  1234. *
  1235. * @param mixed $offset property
  1236. *
  1237. * @return
  1238. */
  1239. public function offsetGet($offset) {
  1240. return $this->__get($offset);
  1241. }
  1242. /**
  1243. * Chainable method to cast a certain ID to a bean; for instance:
  1244. * $person = $club->fetchAs('person')->member;
  1245. * This will load a bean of type person using member_id as ID.
  1246. *
  1247. * @param string $type preferred fetch type
  1248. *
  1249. * @return RedBean_OODBBean
  1250. */
  1251. public function fetchAs($type) {
  1252. $this->fetchType = $type;
  1253. return $this;
  1254. }
  1255. /**
  1256. * Implementation of Countable interface. Makes it possible to use
  1257. * count() function on a bean.
  1258. *
  1259. * @return integer $numberOfProperties number of properties in the bean.
  1260. */
  1261. public function count() {
  1262. return count($this->properties);
  1263. }
  1264. /**
  1265. * Checks wether a bean is empty or not.
  1266. * A bean is empty if it has no other properties than the id field OR
  1267. * if all the other property are empty().
  1268. *
  1269. * @return boolean
  1270. */
  1271. public function isEmpty() {
  1272. $empty = true;
  1273. foreach($this->properties as $key=>$value) {
  1274. if ($key=='id') continue;
  1275. if (!empty($value)) {
  1276. $empty = false;
  1277. }
  1278. }
  1279. return $empty;
  1280. }
  1281. }
  1282. /**
  1283. * Observable
  1284. * Base class for Observables
  1285. *
  1286. * @file RedBean/Observable.php
  1287. * @description Part of the observer pattern in RedBean
  1288. *
  1289. * @author Gabor de Mooij and the RedBeanPHP community
  1290. * @license BSD/GPLv2
  1291. *
  1292. *
  1293. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
  1294. * This source file is subject to the BSD/GPLv2 License that is bundled
  1295. * with this source code in the file license.txt.
  1296. */
  1297. abstract class RedBean_Observab

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