1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825 |
- <?php
- require_once 'PEAR.php';
- define('DB_DATAOBJECT_FETCHMODE_ORDERED', 1);
- define('DB_DATAOBJECT_FETCHMODE_ASSOC', 2);
- define('DB_DATAOBJECT_INT', 1);
- define('DB_DATAOBJECT_STR', 2);
- define('DB_DATAOBJECT_DATE', 4);
- define('DB_DATAOBJECT_TIME', 8);
- define('DB_DATAOBJECT_BOOL', 16);
- define('DB_DATAOBJECT_TXT', 32);
- define('DB_DATAOBJECT_BLOB', 64);
- define('DB_DATAOBJECT_NOTNULL', 128);
- define('DB_DATAOBJECT_MYSQLTIMESTAMP', 256);
- define('DB_DATAOBJECT_ERROR_INVALIDARGS', -1);
- define('DB_DATAOBJECT_ERROR_NODATA', -2);
- define('DB_DATAOBJECT_ERROR_INVALIDCONFIG', -3);
- define('DB_DATAOBJECT_ERROR_NOCLASS', -4);
- define('DB_DATAOBJECT_ERROR_INVALID_CALL', -7);
- define('DB_DATAOBJECT_WHEREADD_ONLY', true);
- $GLOBALS['_DB_DATAOBJECT']['RESULTS'] = array();
- $GLOBALS['_DB_DATAOBJECT']['RESULTSEQ'] = 1;
- $GLOBALS['_DB_DATAOBJECT']['RESULTFIELDS'] = array();
- $GLOBALS['_DB_DATAOBJECT']['CONNECTIONS'] = array();
- $GLOBALS['_DB_DATAOBJECT']['INI'] = array();
- $GLOBALS['_DB_DATAOBJECT']['LINKS'] = array();
- $GLOBALS['_DB_DATAOBJECT']['SEQUENCE'] = array();
- $GLOBALS['_DB_DATAOBJECT']['LASTERROR'] = null;
- $GLOBALS['_DB_DATAOBJECT']['CONFIG'] = array();
- $GLOBALS['_DB_DATAOBJECT']['CACHE'] = array();
- $GLOBALS['_DB_DATAOBJECT']['OVERLOADED'] = false;
- $GLOBALS['_DB_DATAOBJECT']['QUERYENDTIME'] = 0;
- if (!defined('DB_DATAOBJECT_NO_OVERLOAD')) {
- class DB_DataObject_Overload
- {
- public function __call($method, $args)
- {
- $return = null;
- $this->_call($method, $args, $return);
- return $return;
- }
- public function __sleep()
- {
- return array_keys(get_object_vars($this));
- }
- }
- } else {
- class DB_DataObject_Overload
- {
- }
- }
- class DB_DataObject extends DB_DataObject_Overload
- {
-
- public $_DB_DataObject_version = "1.11.3";
-
- public $__table = '';
-
- public $N = 0;
-
-
-
-
-
- public $_database_dsn = '';
-
- public $_database_dsn_md5 = '';
-
- public $_database = '';
-
- public $_query = array(
- 'condition' => '',
- 'group_by' => '',
- 'order_by' => '',
- 'having' => '',
- 'useindex' => '',
- 'limit_start' => '',
- 'limit_count' => '',
- 'data_select' => '*',
- 'unions' => array(),
- 'derive_table' => '',
- 'derive_select' => '',
- );
-
- public $_DB_resultid;
-
- public $_resultFields = false;
-
- public $_link_loaded = false;
-
- public $_join = '';
-
- public $_lastError = false;
-
- public static function debugLevel($v = null)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- (new DB_DataObject)->_loadConfig();
- }
- if ($v !== null) {
- $r = isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0;
- $_DB_DATAOBJECT['CONFIG']['debug'] = $v;
- return $r;
- }
- return isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0;
- }
-
- public function _loadConfig()
- {
- global $_DB_DATAOBJECT;
- $_DB_DATAOBJECT['CONFIG'] = &(new PEAR)->getStaticProperty('DB_DataObject', 'options');
- return null;
- }
-
- public static function staticGet($class, $k, $v = null)
- {
- $lclass = strtolower($class);
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- (new DB_DataObject)->_loadConfig();
- }
- $key = "$k:$v";
- if ($v === null) {
- $key = $k;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- (new DB_DataObject)->debug("$class $key", "STATIC GET - TRY CACHE");
- }
- if (!empty($_DB_DATAOBJECT['CACHE'][$lclass][$key])) {
- return $_DB_DATAOBJECT['CACHE'][$lclass][$key];
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- (new DB_DataObject)->debug("$class $key", "STATIC GET - NOT IN CACHE");
- }
- $obj = DB_DataObject::factory(substr($class, strlen($_DB_DATAOBJECT['CONFIG']['class_prefix'])));
- if ((new PEAR)->isError($obj)) {
- $dor = new DB_DataObject();
- $dor->raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS);
- $r = false;
- return $r;
- }
- if (!isset($_DB_DATAOBJECT['CACHE'][$lclass])) {
- $_DB_DATAOBJECT['CACHE'][$lclass] = array();
- }
- if (!$obj->get($k, $v)) {
- $dor = new DB_DataObject();
- $dor->raiseError("No Data return from get $k $v", DB_DATAOBJECT_ERROR_NODATA);
- $r = false;
- return $r;
- }
- $_DB_DATAOBJECT['CACHE'][$lclass][$key] = $obj;
- return $_DB_DATAOBJECT['CACHE'][$lclass][$key];
- }
-
- public function debug($message, $logtype = 0, $level = 1)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG']['debug']) ||
- (is_numeric($_DB_DATAOBJECT['CONFIG']['debug']) && $_DB_DATAOBJECT['CONFIG']['debug'] < $level)) {
- return null;
- }
-
-
- $class = (isset($this) && is_a($this, 'DB_DataObject')) ? get_class($this) : 'DB_DataObject';
- if (!is_string($message)) {
- $message = print_r($message, true);
- }
- if (!is_numeric($_DB_DATAOBJECT['CONFIG']['debug']) && is_callable($_DB_DATAOBJECT['CONFIG']['debug'])) {
- return call_user_func($_DB_DATAOBJECT['CONFIG']['debug'], $class, $message, $logtype, $level);
- }
- if (!ini_get('html_errors')) {
- echo "$class : $logtype : $message\n";
- flush();
- return null;
- }
- if (!is_string($message)) {
- $message = print_r($message, true);
- }
- $colorize = ($logtype == 'ERROR') ? '<font color="red">' : '<font>';
- echo "<code>{$colorize}<B>$class: $logtype:</B> " . nl2br(htmlspecialchars($message)) . "</font></code><BR>\n";
- }
-
- public static function factory($table = '')
- {
- global $_DB_DATAOBJECT;
-
- $database = '';
- if (strpos($table, '/') !== false) {
- list($database, $table) = explode('.', $table, 2);
- }
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- (new DB_DataObject)->_loadConfig();
- }
-
- if (!empty($database) && empty($_DB_DATAOBJECT['CONFIG']['database_' . $database])) {
- $do = new DB_DataObject();
- $do->raiseError(
- "unable to find database_{$database} in Configuration, It is required for factory with database",
- 0,
- PEAR_ERROR_DIE
- );
- }
-
-
- $cp = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ?
- explode(PATH_SEPARATOR, $_DB_DATAOBJECT['CONFIG']['class_prefix']) : '';
-
-
- $tbl = preg_replace('/[^A-Z0-9]/i', '_', ucfirst($table));
- if (is_array($cp)) {
- $class = array();
- foreach ($cp as $cpr) {
- $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($cpr . $tbl, false) : class_exists($cpr . $tbl);
- if ($ce) {
- $class = $cpr . $tbl;
- break;
- }
- $class[] = $cpr . $tbl;
- }
- } else {
- $class = $tbl;
- $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($class, false) : class_exists($class);
- }
- $rclass = $ce ? $class : (new DB_DataObject)->_autoloadClass($class, $table);
-
- if (!$rclass && isset($_DB_DATAOBJECT['CONFIG']['proxy'])) {
- (new DB_DataObject)->debug("FAILED TO Autoload $database.$table - using proxy.", "FACTORY", 1);
- $proxyMethod = 'getProxy' . $_DB_DATAOBJECT['CONFIG']['proxy'];
-
- class_exists('DB_DataObject_Generator') ? '' :
-
- require_once 'Generator.php';
- $d = new DB_DataObject;
- $d->__table = $table;
- $ret = $d->_connect();
- if (is_object($ret) && is_a($ret, 'PEAR_Error')) {
- return $ret;
- }
- $x = new DB_DataObject_Generator;
- return $x->$proxyMethod($d->_database, $table);
- }
- if (!$rclass || !class_exists($rclass)) {
- $dor = new DB_DataObject();
- return $dor->raiseError(
- "factory could not find class " .
- (is_array($class) ? implode(PATH_SEPARATOR, $class) : $class) .
- "from $table",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- }
- $ret = new $rclass();
- if (!empty($database)) {
- (new DB_DataObject)->debug("Setting database to $database", "FACTORY", 1);
- $ret->database($database);
- }
- return $ret;
- }
-
- public function raiseError($message, $type = null, $behaviour = null)
- {
- global $_DB_DATAOBJECT;
- if ($behaviour == PEAR_ERROR_DIE && !empty($_DB_DATAOBJECT['CONFIG']['dont_die'])) {
- $behaviour = null;
- }
- $error = &(new PEAR)->getStaticProperty('DB_DataObject', 'lastError');
-
- DB_DataObject::debug($message, 'ERROR', 1);
- if ((new PEAR)->isError($message)) {
- $error = $message;
- } else {
- require_once 'DB/DataObject/Error.php';
- $dor = new PEAR();
- $error = $dor->raiseError(
- $message,
- $type,
- $behaviour,
- $opts = null,
- $userinfo = null,
- 'DB_DataObject_Error'
- );
- }
-
-
- $_DB_DATAOBJECT['LASTERROR'] = $error;
- if (isset($this) && is_object($this) && is_subclass_of($this, 'db_dataobject')) {
- $this->_lastError = $error;
- }
- return $error;
- }
-
- public function _autoloadClass($class, $table = false)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
- $class_prefix = empty($_DB_DATAOBJECT['CONFIG']['class_prefix']) ?
- '' : $_DB_DATAOBJECT['CONFIG']['class_prefix'];
- $table = $table ? $table : substr($class, strlen($class_prefix));
-
- if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) || empty($_DB_DATAOBJECT['CONFIG']['class_location'])) {
- return false;
- }
-
-
-
-
-
- $cl = $_DB_DATAOBJECT['CONFIG']['class_location'];
- switch (true) {
- case (strpos($cl, '%s') !== false):
- $file = sprintf($cl, preg_replace('/[^A-Z0-9]/i', '_', ucfirst($table)));
- break;
- case (strpos($cl, PATH_SEPARATOR) !== false):
- $file = array();
- foreach (explode(PATH_SEPARATOR, $cl) as $p) {
- $file[] = $p . '/' . preg_replace('/[^A-Z0-9]/i', '_', ucfirst($table)) . ".php";
- }
- break;
- default:
- $file = $cl . '/' . preg_replace('/[^A-Z0-9]/i', '_', ucfirst($table)) . ".php";
- break;
- }
- $cls = is_array($class) ? $class : array($class);
- if (is_array($file) || !file_exists($file)) {
- $found = false;
- $file = is_array($file) ? $file : array($file);
- $search = implode(PATH_SEPARATOR, $file);
- foreach ($file as $f) {
- foreach (explode(PATH_SEPARATOR, '' . PATH_SEPARATOR . ini_get('include_path')) as $p) {
- $ff = empty($p) ? $f : "$p/$f";
- if (file_exists($ff)) {
- $file = $ff;
- $found = true;
- break;
- }
- }
- if ($found) {
- break;
- }
- }
- if (!$found) {
- $dor = new DB_DataObject();
- $dor->raiseError(
- "autoload:Could not find class " . implode(',', $cls) .
- " using class_location value :" . $search .
- " using include_path value :" . ini_get('include_path'),
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return false;
- }
- }
- include_once $file;
- $ce = false;
- foreach ($cls as $c) {
- $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($c, false) : class_exists($c);
- if ($ce) {
- $class = $c;
- break;
- }
- }
- if (!$ce) {
- $dor = new DB_DataObject();
- $dor->raiseError(
- "autoload:Could not autoload " . implode(',', $cls),
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return false;
- }
- return $class;
- }
-
- public function _connect()
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- $this->_loadConfig();
- }
-
- $db_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ?
- 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver'];
-
- if ($this->_database_dsn_md5 && !empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
-
- if ((new PEAR)->isError($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- return $this->raiseError(
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->message,
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->code,
- PEAR_ERROR_DIE
- );
- }
- if (empty($this->_database)) {
- $this->_database = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database'];
- $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase');
- $this->_database = ($db_driver != 'DB' && $hasGetDatabase)
- ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase()
- : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database'];
- if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite')
- && is_file($this->_database)) {
- $this->_database = basename($this->_database);
- }
- if ($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'ibase') {
- $this->_database = substr(basename($this->_database), 0, -4);
- }
- }
-
-
- return true;
- }
-
-
- $options = $_DB_DATAOBJECT['CONFIG'];
-
- $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null;
- if (!$dsn) {
- if (!$this->_database && !strlen($this->tableName())) {
- $this->_database = isset($options["table_{$this->tableName()}"]) ? $options["table_{$this->tableName()}"] : null;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("Checking for database specific ini ('{$this->_database}') : database_{$this->_database} in options", "CONNECT");
- }
- if ($this->_database && !empty($options["database_{$this->_database}"])) {
- $dsn = $options["database_{$this->_database}"];
- } elseif (!empty($options['database'])) {
- $dsn = $options['database'];
- }
- }
-
- if (!$dsn) {
- return $this->raiseError(
- "No database name / dsn found anywhere",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG,
- PEAR_ERROR_DIE
- );
- }
- if (is_string($dsn)) {
- $this->_database_dsn_md5 = md5($dsn);
- } else {
-
- $this->_database_dsn_md5 = md5(serialize($dsn));
- }
- if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("USING CACHED CONNECTION", "CONNECT", 3);
- }
- if (!$this->_database) {
- $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase');
- $this->_database = ($db_driver != 'DB' && $hasGetDatabase)
- ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase()
- : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database'];
- if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite')
- && is_file($this->_database)) {
- $this->_database = basename($this->_database);
- }
- }
- return true;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("NEW CONNECTION TP DATABASE :" . $this->_database, "CONNECT", 3);
-
- $this->debug(print_r($dsn, true) . " {$this->_database_dsn_md5}", "CONNECT", 3);
- }
-
- if ($db_driver == 'DB') {
-
-
- $db_options = (new PEAR)->getStaticProperty('DB', 'options');
- require_once 'DB.php';
- if ($db_options) {
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn, $db_options);
- } else {
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn);
- }
- } else {
-
- require_once 'MDB2.php';
-
- $db_options = (new PEAR)->getStaticProperty('MDB2', 'options');
- $db_options = is_array($db_options) ? $db_options : array();
- $db_options['portability'] = isset($db_options['portability'])
- ? $db_options['portability'] : MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE;
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = MDB2::connect($dsn, $db_options);
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug(print_r($_DB_DATAOBJECT['CONNECTIONS'], true), "CONNECT", 5);
- }
- if ((new PEAR)->isError($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $this->debug($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->toString(), "CONNECT FAILED", 5);
- return $this->raiseError(
- "Connect failed, turn on debugging to 5 see why",
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->code,
- PEAR_ERROR_DIE
- );
- }
- if (empty($this->_database)) {
- $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase');
- $this->_database = ($db_driver != 'DB' && $hasGetDatabase)
- ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase()
- : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database'];
- if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite')
- && is_file($this->_database)) {
- $this->_database = basename($this->_database);
- }
- }
-
- return true;
- }
-
- public function tableName()
- {
- global $_DB_DATAOBJECT;
- $args = func_get_args();
- if (count($args)) {
- $this->__table = $args[0];
- }
- if (empty($this->__table)) {
- return '';
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) {
- return strtolower($this->__table);
- }
- return $this->__table;
- }
-
- public function get($k = null, $v = null)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
- $keys = array();
- if ($v === null) {
- $v = $k;
- $keys = $this->keys();
- if (!$keys) {
- $this->raiseError("No Keys available for {$this->tableName()}", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
- return false;
- }
- $k = $keys[0];
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("$k $v " . print_r($keys, true), "GET");
- }
- if ($v === null) {
- $this->raiseError("No Value specified for get", DB_DATAOBJECT_ERROR_INVALIDARGS);
- return false;
- }
- $this->$k = $v;
- return $this->find(1);
- }
-
- public function keys()
- {
-
-
- $args = func_get_args();
- if (count($args)) {
- $this->_database_keys = $args;
- }
- if (isset($this->_database_keys)) {
- return $this->_database_keys;
- }
- global $_DB_DATAOBJECT;
- if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $this->_connect();
- }
- if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName() . "__keys"])) {
- return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName() . "__keys"]);
- }
- $this->databaseStructure();
- if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName() . "__keys"])) {
- return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName() . "__keys"]);
- }
- return array();
- }
-
- public function databaseStructure()
- {
- global $_DB_DATAOBJECT;
-
- if ($args = func_get_args()) {
- if (count($args) == 1) {
-
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("Loading Generator as databaseStructure called with args", 1);
- }
- $x = new DB_DataObject;
- $x->_database = $args[0];
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $tables = $DB->getListOf('tables');
- class_exists('DB_DataObject_Generator') ? '' :
-
- require_once 'Generator.php';
- foreach ($tables as $table) {
- $y = new DB_DataObject_Generator;
- $y->fillTableSchema($x->_database, $table);
- }
- return $_DB_DATAOBJECT['INI'][$x->_database];
- } else {
- $_DB_DATAOBJECT['INI'][$args[0]] = isset($_DB_DATAOBJECT['INI'][$args[0]]) ?
- $_DB_DATAOBJECT['INI'][$args[0]] + $args[1] : $args[1];
- if (isset($args[1])) {
- $_DB_DATAOBJECT['LINKS'][$args[0]] = isset($_DB_DATAOBJECT['LINKS'][$args[0]]) ?
- $_DB_DATAOBJECT['LINKS'][$args[0]] + $args[2] : $args[2];
- }
- return true;
- }
- }
- if (!$this->_database) {
- $this->_connect();
- }
-
- if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
- return true;
- }
-
- if (empty($_DB_DATAOBJECT['INI'][$this->_database])) {
- $_DB_DATAOBJECT['INI'][$this->_database] = array();
- }
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
-
-
- if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("Loading Generator to fetch Schema", 1);
- }
- class_exists('DB_DataObject_Generator') ? '' :
-
- require_once 'Generator.php';
- $x = new DB_DataObject_Generator;
- $x->fillTableSchema($this->_database, $this->tableName());
- return true;
- }
-
-
- $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ?
- array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") :
- array();
- if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) {
- $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ?
- $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] :
- explode(PATH_SEPARATOR, $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]);
- }
- $_DB_DATAOBJECT['INI'][$this->_database] = array();
- foreach ($schemas as $ini) {
- if (file_exists($ini) && is_file($ini)) {
- $_DB_DATAOBJECT['INI'][$this->_database] = array_merge(
- $_DB_DATAOBJECT['INI'][$this->_database],
- parse_ini_file($ini, true)
- );
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- if (!is_readable($ini)) {
- $this->debug("ini file is not readable: $ini", "databaseStructure", 1);
- } else {
- $this->debug("Loaded ini file: $ini", "databaseStructure", 1);
- }
- }
- } else {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("Missing ini file: $ini", "databaseStructure", 1);
- }
- }
- }
-
- if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) {
- foreach ($_DB_DATAOBJECT['INI'][$this->_database] as $k => $v) {
-
- $_DB_DATAOBJECT['INI'][$this->_database][strtolower($k)] = $v;
- }
- }
-
- if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
- return true;
- }
-
- if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) {
- class_exists('DB_DataObject_Generator') ? '' :
-
- require_once 'Generator.php';
- $x = new DB_DataObject_Generator;
- $x->fillTableSchema($this->_database, $this->tableName());
-
- return true;
- }
- $this->debug("Cant find database schema: {$this->_database}/{$this->tableName()} \n" .
- "in links file data: " . print_r($_DB_DATAOBJECT['INI'], true), "databaseStructure", 5);
-
- $this->raiseError("Unable to load schema for database and table (turn debugging up to 5 for full error message)", DB_DATAOBJECT_ERROR_INVALIDARGS, PEAR_ERROR_DIE);
- return false;
- }
-
- public function find($n = false)
- {
- global $_DB_DATAOBJECT;
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug($n, "find", 1);
- }
- if (!strlen($this->tableName())) {
-
- trigger_error("NO \$__table SPECIFIED in class definition", E_USER_ERROR);
- }
- $this->N = 0;
- $query_before = $this->_query;
- $this->_build_condition($this->table());
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $sql = $this->_build_select();
- foreach ($this->_query['unions'] as $union_ar) {
- $sql .= $union_ar[1] . $union_ar[0]->_build_select() . " \n";
- }
- $sql .= $this->_query['order_by'] . " \n";
-
- if ((!isset($_DB_DATAOBJECT['CONFIG']['db_driver'])) ||
- ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB')) {
-
- if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) {
- $sql = $DB->modifyLimitQuery($sql, $this->_query['limit_start'], $this->_query['limit_count']);
- }
- } else {
-
- if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) {
- $DB->setLimit($this->_query['limit_count'], $this->_query['limit_start']);
- }
- }
- $err = $this->_query($sql);
- if (is_a($err, 'PEAR_Error')) {
- return false;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("CHECK autofetchd $n", "find", 1);
- }
-
- $ret = $this->N;
- if (!$ret && !empty($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) {
-
- unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]);
- }
- if ($n && $this->N > 0) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("ABOUT TO AUTOFETCH", "find", 1);
- }
- $fs = $this->fetch();
-
-
- $ret = ($ret === true) ? $fs : $ret;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("DONE", "find", 1);
- }
- $this->_query = $query_before;
- return $ret;
- }
-
-
-
-
- public function _build_condition($keys, $filter = array(), $negative_filter = array())
- {
- global $_DB_DATAOBJECT;
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- $options = $_DB_DATAOBJECT['CONFIG'];
-
- if ($this->_query === false) {
- $x = new DB_DataObject;
- $this->_query = $x->_query;
- }
- foreach ($keys as $k => $v) {
-
-
- if ($filter) {
- if (!in_array($k, $filter)) {
- continue;
- }
- }
- if ($negative_filter) {
- if (in_array($k, $negative_filter)) {
- continue;
- }
- }
- if (!isset($this->$k)) {
- continue;
- }
- $kSql = $quoteIdentifiers
- ? ($DB->quoteIdentifier($this->tableName()) . '.' . $DB->quoteIdentifier($k))
- : "{$this->tableName()}.{$k}";
- if (is_object($this->$k) && is_a($this->$k, 'DB_DataObject_Cast')) {
- $dbtype = $DB->dsn["phptype"];
- $value = $this->$k->toString($v, $DB);
- if ((new PEAR)->isError($value)) {
- $this->raiseError($value->getMessage(), DB_DATAOBJECT_ERROR_INVALIDARG);
- return false;
- }
- if ((strtolower($value) === 'null') && !($v & DB_DATAOBJECT_NOTNULL)) {
- $this->whereAdd(" $kSql IS NULL");
- continue;
- }
- $this->whereAdd(" $kSql = $value");
- continue;
- }
- if (!($v & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($this, $k)) {
- $this->whereAdd(" $kSql IS NULL");
- continue;
- }
- if ($v & DB_DATAOBJECT_STR) {
- $this->whereAdd(" $kSql = " . $this->_quote((string)(
- ($v & DB_DATAOBJECT_BOOL) ?
-
-
- (($this->$k === 'f') ? 0 : (int)(bool)$this->$k) :
- $this->$k
- )));
- continue;
- }
- if (is_numeric($this->$k)) {
- $this->whereAdd(" $kSql = {$this->$k}");
- continue;
- }
-
- $this->whereAdd(" $kSql = " . intval($this->$k));
- }
- return "";
- }
-
- public function whereAdd($cond = false, $logic = 'AND')
- {
-
- $_query = $this->_query;
- if (!isset($this->_query) || ($_query === false)) {
- return $this->raiseError(
- "You cannot do two queries on the same object (clone it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- }
- if ($cond === false) {
- $r = $this->_query['condition'];
- $_query['condition'] = '';
- $this->_query = $_query;
- return preg_replace('/^\s+WHERE\s+/', '', $r);
- }
-
- if (!trim($cond)) {
- return $this->raiseError("WhereAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- $r = $_query['condition'];
- if ($_query['condition']) {
- $_query['condition'] .= " {$logic} ( {$cond} )";
- $this->_query = $_query;
- return $r;
- }
- $_query['condition'] = " WHERE ( {$cond} ) ";
- $this->_query = $_query;
- return $r;
- }
-
- public function _is_null($obj_or_ar, $prop)
- {
- global $_DB_DATAOBJECT;
- $isset = $prop === false ? isset($obj_or_ar) :
- (is_array($obj_or_ar) ? isset($obj_or_ar[$prop]) : isset($obj_or_ar->$prop));
- $value = $isset ?
- ($prop === false ? $obj_or_ar :
- (is_array($obj_or_ar) ? $obj_or_ar[$prop] : $obj_or_ar->$prop))
- : null;
- $options = $_DB_DATAOBJECT['CONFIG'];
- $null_strings = !isset($options['disable_null_strings'])
- || $options['disable_null_strings'] === false;
- $crazy_null = isset($options['disable_null_strings'])
- && is_string($options['disable_null_strings'])
- && strtolower($options['disable_null_strings'] === 'full');
- if ($null_strings && $isset && is_string($value) && (strtolower($value) === 'null')) {
- return true;
- }
- if ($crazy_null && !$isset) {
- return true;
- }
- return false;
- }
-
- public function _quote($str)
- {
- global $_DB_DATAOBJECT;
- return (empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ||
- ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB'))
- ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->quoteSmart($str)
- : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->quote($str);
- }
-
- public function table()
- {
-
-
- $args = func_get_args();
- if (count($args)) {
- $this->_database_fields = $args[0];
- }
- if (isset($this->_database_fields)) {
- return $this->_database_fields;
- }
- global $_DB_DATAOBJECT;
- if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $this->_connect();
- }
- if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
- return $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()];
- }
- $this->databaseStructure();
- $ret = array();
- if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
- $ret = $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()];
- }
- return $ret;
- }
-
- public function _build_select()
- {
- global $_DB_DATAOBJECT;
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- if ($quoteIdentifiers) {
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- }
- $tn = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName());
- if (!empty($this->_query['derive_table']) && !empty($this->_query['derive_select'])) {
-
-
- $sql = 'SELECT ' .
- $this->_query['derive_select']
- . ' FROM ( SELECT' .
- $this->_query['data_select'] . " \n" .
- " FROM $tn " . $this->_query['useindex'] . " \n" .
- $this->_join . " \n" .
- $this->_query['condition'] . " \n" .
- $this->_query['group_by'] . " \n" .
- $this->_query['having'] . " \n" .
- ') ' . $this->_query['derive_table'];
- return $sql;
- }
- $sql = 'SELECT ' .
- $this->_query['data_select'] . " \n" .
- " FROM $tn " . $this->_query['useindex'] . " \n" .
- $this->_join . " \n" .
- $this->_query['condition'] . " \n" .
- $this->_query['group_by'] . " \n" .
- $this->_query['having'] . " \n";
- return $sql;
- }
-
-
-
-
- public function _query($string)
- {
- global $_DB_DATAOBJECT;
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $options = $_DB_DATAOBJECT['CONFIG'];
- $_DB_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ?
- 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver'];
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug($string, $log = "QUERY");
- }
- if (
- strtoupper($string) == 'BEGIN' ||
- strtoupper($string) == 'START TRANSACTION'
- ) {
- if ($_DB_driver == 'DB') {
- $DB->autoCommit(false);
- $DB->simpleQuery('BEGIN');
- } else {
- $DB->beginTransaction();
- }
- return true;
- }
- if (strtoupper($string) == 'COMMIT') {
- $res = $DB->commit();
- if ($_DB_driver == 'DB') {
- $DB->autoCommit(true);
- }
- return $res;
- }
- if (strtoupper($string) == 'ROLLBACK') {
- $DB->rollback();
- if ($_DB_driver == 'DB') {
- $DB->autoCommit(true);
- }
- return true;
- }
- if (!empty($options['debug_ignore_updates']) &&
- (strtolower(substr(trim($string), 0, 6)) != 'select') &&
- (strtolower(substr(trim($string), 0, 4)) != 'show') &&
- (strtolower(substr(trim($string), 0, 8)) != 'describe')) {
- $this->debug('Disabling Update as you are in debug mode');
- return $this->raiseError("Disabling Update as you are in debug mode", null);
- }
-
-
-
-
-
- $t = explode(' ', microtime());
- $_DB_DATAOBJECT['QUERYENDTIME'] = $time = $t[0] + $t[1];
- for ($tries = 0; $tries < 3; $tries++) {
- if ($_DB_driver == 'DB') {
- $result = $DB->query($string);
- } else {
- switch (strtolower(substr(trim($string), 0, 6))) {
- case 'insert':
- case 'update':
- case 'delete':
- $result = $DB->exec($string);
- break;
- default:
- $result = $DB->query($string);
- break;
- }
- }
-
- if (!is_object($result) || !is_a($result, 'PEAR_Error')) {
- break;
- }
- if ($result->getCode() != -14) {
- break;
- }
- sleep(1);
- $DB->connect($DB->dsn);
- }
- if (is_object($result) && is_a($result, 'PEAR_Error')) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug($result->toString(), "Query Error", 1);
- }
- $this->N = false;
- return $this->raiseError($result);
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $t = explode(' ', microtime());
- $_DB_DATAOBJECT['QUERYENDTIME'] = $t[0] + $t[1];
- $this->debug('QUERY DONE IN ' . ($t[0] + $t[1] - $time) . " seconds", 'query', 1);
- }
- switch (strtolower(substr(trim($string), 0, 6))) {
- case 'insert':
- case 'update':
- case 'delete':
- if ($_DB_driver == 'DB') {
-
- return $DB->affectedRows();
- }
- return $result;
- }
- if (is_object($result)) {
-
- $_DB_resultid = $GLOBALS['_DB_DATAOBJECT']['RESULTSEQ']++;
- $_DB_DATAOBJECT['RESULTS'][$_DB_resultid] = $result;
- $this->_DB_resultid = $_DB_resultid;
- }
- $this->N = 0;
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug(serialize($result), 'RESULT', 5);
- }
- if (method_exists($result, 'numRows')) {
- if ($_DB_driver == 'DB') {
- $DB->expectError(DB_ERROR_UNSUPPORTED);
- } else {
- $DB->expectError(MDB2_ERROR_UNSUPPORTED);
- }
- $this->N = $result->numRows();
-
- if (is_object($this->N) && is_a($this->N, 'PEAR_Error')) {
- $this->N = true;
- }
- $DB->popExpect();
- }
- return null;
- }
-
- public function fetch()
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
- if (empty($this->N)) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("No data returned from FIND (eg. N is 0)", "FETCH", 3);
- }
- return false;
- }
- if (empty($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]) ||
- !is_object($result = $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug('fetched on object after fetch completed (no results found)');
- }
- return false;
- }
- $array = $result->fetchRow(DB_DATAOBJECT_FETCHMODE_ASSOC);
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug(serialize($array), "FETCH");
- }
-
- if ($array === null) {
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $t = explode(' ', microtime());
- $this->debug(
- "Last Data Fetch'ed after " .
- ($t[0] + $t[1] - $_DB_DATAOBJECT['QUERYENDTIME']) .
- " seconds",
- "FETCH",
- 1
- );
- }
-
- unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]);
-
-
- if (!empty($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) {
- $this->_resultFields = $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid];
- unset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]);
- }
-
-
- return false;
- }
-
- $this->_resultFields = false;
- if (!isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) {
-
- $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid] = array_flip(array_keys($array));
- }
- $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'];
- if ($dbtype === 'pgsql') {
- if (($_DB_DATAOBJECT['CONFIG']['db_driver'] ?? 'DB') === 'DB') {
- $tableInfo = $result->tableInfo();
- } elseif ($result->db->supports('result_introspection')) {
- $result->db->loadModule('Reverse', null, true);
- $tableInfo = $result->db->reverse->tableInfo($result);
- }
- }
- $replace = array('.', ' ');
- foreach (array_keys($array) as $i => $k) {
-
- $kk = (strpos($k, '.') === false && strpos($k, ' ') === false) ?
- $k : str_replace($replace, '_', $k);
- if ($dbtype === 'pgsql') {
- switch ($tableInfo[$i]['type']) {
- case 'bool':
- $array[$k] = str_replace(['t', 'f'], ['1', '0'], $array[$k]);
- break;
- case 'bytea':
- $array[$k] = pg_unescape_bytea($array[$k]);
- break;
- }
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("$kk = " . $array[$k], "fetchrow LINE", 3);
- }
- $this->$kk = $array[$k];
- }
-
- $this->_link_loaded = false;
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("{$this->tableName()} DONE", "fetchrow", 2);
- }
- if (($this->_query !== false) && empty($_DB_DATAOBJECT['CONFIG']['keep_query_after_fetch'])) {
- $this->_query = false;
- }
- return true;
- }
-
- public function pid()
- {
- $keys = $this->keys();
- if (!$keys) {
- $this->raiseError(
- "No Keys available for {$this->tableName()}",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return false;
- }
- $k = $keys[0];
- if (empty($this->$k)) {
- $this->raiseError(
- "pid() called on Object where primary key value not available",
- DB_DATAOBJECT_ERROR_NODATA
- );
- return false;
- }
- return $this->$k;
- }
-
- public function fetchAll($k = false, $v = false, $method = false)
- {
-
- if ($k !== false &&
- (
- empty($this->_query['data_select']) ||
- ($this->_query['data_select'] == '*')
- )
- ) {
- $this->selectAdd();
- $this->selectAdd($k);
- if ($v !== false) {
- $this->selectAdd($v);
- }
- }
- $this->find();
- $ret = array();
- while ($this->fetch()) {
- if ($v !== false) {
- $ret[$this->$k] = $this->$v;
- continue;
- }
- $ret[] = $k === false ?
- ($method == false ? clone($this) : $this->$method())
- : $this->$k;
- }
- return $ret;
- }
-
- public function selectAdd($k = null)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($k === null) {
- $old = $this->_query['data_select'];
- $this->_query['data_select'] = '';
- return $old;
- }
-
- if (!trim($k)) {
- return $this->raiseError("selectAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- if ($this->_query['data_select']) {
- $this->_query['data_select'] .= ', ';
- }
- $this->_query['data_select'] .= " $k ";
- return null;
- }
-
- public function whereAddIn($key, $list, $type, $logic = 'AND')
- {
- $not = '';
- if ($key[0] == '!') {
- $not = 'NOT ';
- $key = substr($key, 1);
- }
-
- $type = $type == 'int' ? 'integer' : $type;
- if ($type == 'string') {
- $this->_connect();
- }
- $ar = array();
- foreach ($list as $k) {
- settype($k, $type);
- $ar[] = $type == 'string' ? $this->_quote($k) : $k;
- }
- if (!$ar) {
- return $not ? $this->_query['condition'] : $this->whereAdd("1=0");
- }
- return $this->whereAdd("$key $not IN (" . implode(',', $ar) . ')', $logic);
- }
-
-
-
-
- public function orderBy($order = false)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($order === false) {
- $this->_query['order_by'] = '';
- return null;
- }
-
- if (!trim($order)) {
- return $this->raiseError("orderBy: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- if (!$this->_query['order_by']) {
- $this->_query['order_by'] = " ORDER BY {$order} ";
- return null;
- }
- $this->_query['order_by'] .= " , {$order}";
- return null;
- }
-
- public function groupBy($group = false)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($group === false) {
- $this->_query['group_by'] = '';
- return null;
- }
-
- if (!trim($group)) {
- return $this->raiseError("groupBy: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- if (!$this->_query['group_by']) {
- $this->_query['group_by'] = " GROUP BY {$group} ";
- return null;
- }
- $this->_query['group_by'] .= " , {$group}";
- return null;
- }
-
- public function having($having = false)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($having === false) {
- $this->_query['having'] = '';
- return null;
- }
-
- if (!trim($having)) {
- return $this->raiseError("Having: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- if (!$this->_query['having']) {
- $this->_query['having'] = " HAVING {$having} ";
- return null;
- }
- $this->_query['having'] .= " AND {$having}";
- return null;
- }
-
- public function useIndex($index = false)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($index === false) {
- $this->_query['useindex'] = '';
- return null;
- }
-
- if ((is_string($index) && !trim($index)) || (is_array($index) && !count($index))) {
- return $this->raiseError("Having: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- $index = is_array($index) ? implode(', ', $index) : $index;
- if (!$this->_query['useindex']) {
- $this->_query['useindex'] = " USE INDEX ({$index}) ";
- return null;
- }
- $this->_query['useindex'] = substr($this->_query['useindex'], 0, -2) . ", {$index}) ";
- return null;
- }
-
- public function limit($a = null, $b = null)
- {
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($a === null) {
- $this->_query['limit_start'] = '';
- $this->_query['limit_count'] = '';
- return null;
- }
-
- if ((!is_int($a) && ((string)((int)$a) !== (string)$a))
- || (($b !== null) && (!is_int($b) && ((string)((int)$b) !== (string)$b)))) {
- return $this->raiseError("limit: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
- }
- global $_DB_DATAOBJECT;
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $this->_query['limit_start'] = ($b == null) ? 0 : (int)$a;
- $this->_query['limit_count'] = ($b == null) ? (int)$a : (int)$b;
- return null;
- }
-
- public function insert()
- {
- global $_DB_DATAOBJECT;
-
-
- if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $this->_connect();
- }
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $items = $this->table();
- if (!$items) {
- $this->raiseError(
- "insert:No table definition for {$this->tableName()}",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return false;
- }
- $options = $_DB_DATAOBJECT['CONFIG'];
- $datasaved = 1;
- $leftq = '';
- $rightq = '';
- $seqKeys = isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()]) ?
- $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] :
- $this->sequenceKey();
- $key = isset($seqKeys[0]) ? $seqKeys[0] : false;
- $useNative = isset($seqKeys[1]) ? $seqKeys[1] : false;
- $seq = isset($seqKeys[2]) ? $seqKeys[2] : false;
- $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn["phptype"];
-
-
- if (($key !== false) && !$useNative) {
- if (!$seq) {
- $keyvalue = $DB->nextId($this->tableName());
- } else {
- $f = $DB->getOption('seqname_format');
- $DB->setOption('seqname_format', '%s');
- $keyvalue = $DB->nextId($seq);
- $DB->setOption('seqname_format', $f);
- }
- if ((new PEAR)->isError($keyvalue)) {
- $this->raiseError($keyvalue->toString(), DB_DATAOBJECT_ERROR_INVALIDCONFIG);
- return false;
- }
- $this->$key = $keyvalue;
- }
-
- $ignore_null = !isset($options['disable_null_strings'])
- || !is_string($options['disable_null_strings'])
- || strtolower($options['disable_null_strings']) !== 'full';
- foreach ($items as $k => $v) {
-
- if ($key && ($k == $key) && $useNative) {
- continue;
- }
-
- if ((!isset($this->$k) || ($v == 1 && $this->$k === ''))
- && $ignore_null
- ) {
- continue;
- }
-
-
- if ($v & DB_DATAOBJECT_MYSQLTIMESTAMP) {
- continue;
- }
- if ($leftq) {
- $leftq .= ', ';
- $rightq .= ', ';
- }
- $leftq .= ($quoteIdentifiers ? ($DB->quoteIdentifier($k) . ' ') : "$k ");
- if (is_object($this->$k) && is_a($this->$k, 'DB_DataObject_Cast')) {
- $value = $this->$k->toString($v, $DB);
- if ((new PEAR)->isError($value)) {
- $this->raiseError($value->toString(), DB_DATAOBJECT_ERROR_INVALIDARGS);
- return false;
- }
- $rightq .= $value;
- continue;
- }
- if (!($v & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($this, $k)) {
- $rightq .= " NULL ";
- continue;
- }
-
-
- if (!$this->$k &&
- (($v & DB_DATAOBJECT_DATE) || ($v & DB_DATAOBJECT_TIME)) &&
- !($v & DB_DATAOBJECT_NOTNULL)) {
- $rightq .= " NULL ";
- continue;
- }
- if ($v & DB_DATAOBJECT_STR) {
- $rightq .= $this->_quote((string)(
- ($v & DB_DATAOBJECT_BOOL) ?
-
-
- (($this->$k === 'f') ? 0 : (int)(bool)$this->$k) :
- $this->$k
- )) . " ";
- continue;
- }
- if (is_numeric($this->$k)) {
- $rightq .= " {$this->$k} ";
- continue;
- }
-
- if (is_object($this->$k) || is_array($this->$k)) {
- $this->debug('ODD DATA: ' . $k . ' ' . print_r($this->$k, true), 'ERROR');
- }
-
-
- $rightq .= ' ' . intval($this->$k) . ' ';
- }
-
- if ($leftq || $useNative) {
- $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName());
- if (($dbtype == 'pgsql') && empty($leftq)) {
- $r = $this->_query("INSERT INTO {$table} DEFAULT VALUES");
- } else {
- $r = $this->_query("INSERT INTO {$table} ($leftq) VALUES ($rightq) ");
- }
- if ((new PEAR)->isError($r)) {
- $this->raiseError($r);
- return false;
- }
- if ($r < 1) {
- return 0;
- }
-
- if ($key && $useNative) {
- switch ($dbtype) {
- case 'mysql':
- case 'mysqli':
- $method = "{$dbtype}_insert_id";
- $this->$key = $method(
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->connection
- );
- break;
- case 'mssql':
-
-
-
-
-
- $db_driver = empty($options['db_driver']) ? 'DB' : $options['db_driver'];
- $method = ($db_driver == 'DB') ? 'getOne' : 'queryOne';
- $mssql_key = $DB->$method("SELECT @@IDENTITY");
- if ((new PEAR)->isError($mssql_key)) {
- $this->raiseError($mssql_key);
- return false;
- }
- $this->$key = $mssql_key;
- break;
- case 'pgsql':
- if (!$seq) {
- $seq = $DB->getSequenceName(strtolower($this->tableName() . '_' . $key));
- }
- $db_driver = empty($options['db_driver']) ? 'DB' : $options['db_driver'];
- $method = ($db_driver == 'DB') ? 'getOne' : 'queryOne';
- $pgsql_key = $DB->$method("SELECT currval('" . $seq . "')");
- if ((new PEAR)->isError($pgsql_key)) {
- $this->raiseError($pgsql_key);
- return false;
- }
- $this->$key = $pgsql_key;
- break;
- case 'ifx':
- $this->$key = array_shift(
- ifx_fetch_row(
- ifx_query(
- "select DBINFO('sqlca.sqlerrd1') FROM systables where tabid=1",
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->connection,
- IFX_SCROLL
- ),
- "FIRST"
- )
- );
- break;
- }
- }
- if (isset($_DB_DATAOBJECT['CACHE'][strtolower(get_class($this))])) {
- $this->_clear_cache();
- }
- if ($key) {
- return $this->$key;
- }
- return true;
- }
- $this->raiseError("insert: No Data specifed for query", DB_DATAOBJECT_ERROR_NODATA);
- return false;
- }
-
- public function sequenceKey()
- {
- global $_DB_DATAOBJECT;
-
- if (!$this->_database) {
- $this->_connect();
- }
- if (!isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database])) {
- $_DB_DATAOBJECT['SEQUENCE'][$this->_database] = array();
- }
- $args = func_get_args();
- if (count($args)) {
- $args[1] = isset($args[1]) ? $args[1] : false;
- $args[2] = isset($args[2]) ? $args[2] : false;
- $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = $args;
- }
- if (isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()])) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()];
- }
-
- $keys = $this->keys();
- if (!$keys) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()]
- = array(false, false, false);
- }
- $table = $this->table();
- $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'];
- $usekey = $keys[0];
- $seqname = false;
- if (!empty($_DB_DATAOBJECT['CONFIG']['sequence_' . $this->tableName()])) {
- $seqname = $_DB_DATAOBJECT['CONFIG']['sequence_' . $this->tableName()];
- if (strpos($seqname, ':') !== false) {
- list($usekey, $seqname) = explode(':', $seqname);
- }
- }
-
- if (empty($table[$usekey]) || !($table[$usekey] & DB_DATAOBJECT_INT)) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false, false, false);
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'])) {
- $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'];
- if (is_string($ignore) && (strtoupper($ignore) == 'ALL')) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false, false, $seqname);
- }
- if (is_string($ignore)) {
- $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'] = explode(',', $ignore);
- }
- if (in_array($this->tableName(), $ignore)) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false, false, $seqname);
- }
- }
- $realkeys = $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName() . "__keys"];
-
- if (is_numeric($realkeys[$usekey])) {
- $realkeys[$usekey] = 'N';
- }
-
- if (($realkeys[$usekey] == 'K') && (count($keys) > 1)) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false, false, $seqname);
- }
-
-
-
-
- if (in_array($dbtype, array('pgsql')) &&
- ($table[$usekey] & DB_DATAOBJECT_INT) &&
- isset($realkeys[$usekey]) && strlen($realkeys[$usekey]) > 1) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey, true, $realkeys[$usekey]);
- }
- if (in_array($dbtype, array('pgsql', 'mysql', 'mysqli', 'mssql', 'ifx')) &&
- ($table[$usekey] & DB_DATAOBJECT_INT) &&
- isset($realkeys[$usekey]) && ($realkeys[$usekey] == 'N')
- ) {
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey, true, $seqname);
- }
-
- if (($realkeys[$usekey] != 'N') &&
- !empty($_DB_DATAOBJECT['CONFIG']['dont_use_pear_sequences'])) {
- return array(false, false, false);
- }
-
- return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey, false, $seqname);
- }
-
- public function _clear_cache()
- {
- global $_DB_DATAOBJECT;
- $class = strtolower(get_class($this));
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("Clearing Cache for " . $class, 1);
- }
- if (!empty($_DB_DATAOBJECT['CACHE'][$class])) {
- unset($_DB_DATAOBJECT['CACHE'][$class]);
- }
- }
-
- public function update($dataObject = false)
- {
- global $_DB_DATAOBJECT;
-
- $this->_connect();
- $original_query = $this->_query;
- $items = $this->table();
-
- $seq = $this->sequenceKey();
- if ($seq[0] !== false) {
- $keys = array($seq[0]);
- if (!isset($this->{$keys[0]}) && $dataObject !== true) {
- $this->raiseError("update: trying to perform an update without
- the key set, and argument to update is not
- DB_DATAOBJECT_WHEREADD_ONLY
- " . print_r(array('seq' => $seq, 'keys' => $keys), true), DB_DATAOBJECT_ERROR_INVALIDARGS);
- return false;
- }
- } else {
- $keys = $this->keys();
- }
- if (!$items) {
- $this->raiseError("update:No table definition for {$this->tableName()}", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
- return false;
- }
- $datasaved = 1;
- $settings = '';
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $dbtype = $DB->dsn["phptype"];
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- $options = $_DB_DATAOBJECT['CONFIG'];
- $ignore_null = !isset($options['disable_null_strings'])
- || !is_string($options['disable_null_strings'])
- || strtolower($options['disable_null_strings']) !== 'full';
- foreach ($items as $k => $v) {
-
- if ((!isset($this->$k) || ($v == 1 && $this->$k === ''))
- && $ignore_null
- ) {
- continue;
- }
-
-
- if (($dataObject !== false) && isset($dataObject->$k) && ($dataObject->$k === $this->$k)) {
- continue;
- }
-
- if (in_array($k, $keys)) {
- continue;
- }
-
-
- if ($v & DB_DATAOBJECT_MYSQLTIMESTAMP) {
- continue;
- }
- if ($settings) {
- $settings .= ', ';
- }
- $kSql = ($quoteIdentifiers ? $DB->quoteIdentifier($k) : $k);
- if (is_object($this->$k) && is_a($this->$k, 'DB_DataObject_Cast')) {
- $value = $this->$k->toString($v, $DB);
- if ((new PEAR)->isError($value)) {
- $this->raiseError($value->getMessage(), DB_DATAOBJECT_ERROR_INVALIDARG);
- return false;
- }
- $settings .= "$kSql = $value ";
- continue;
- }
-
- if (!($v & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($this, $k)) {
- $settings .= "$kSql = NULL ";
- continue;
- }
-
-
- if (!$this->$k &&
- (($v & DB_DATAOBJECT_DATE) || ($v & DB_DATAOBJECT_TIME)) &&
- !($v & DB_DATAOBJECT_NOTNULL)) {
- $settings .= "$kSql = NULL ";
- continue;
- }
- if ($v & DB_DATAOBJECT_STR) {
- $settings .= "$kSql = " . $this->_quote((string)(
- ($v & DB_DATAOBJECT_BOOL) ?
-
-
- (($this->$k === 'f') ? 0 : (int)(bool)$this->$k) :
- $this->$k
- )) . ' ';
- continue;
- }
- if (is_numeric($this->$k)) {
- $settings .= "$kSql = {$this->$k} ";
- continue;
- }
-
-
- $settings .= "$kSql = " . intval($this->$k) . ' ';
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("got keys as " . serialize($keys), 3);
- }
- if ($dataObject !== true) {
- $this->_build_condition($items, $keys);
- } else {
-
- if (empty($this->_query['condition'])) {
- $this->raiseError("update: global table update not available
- do \$do->whereAdd('1=1'); if you really want to do that.
- ", DB_DATAOBJECT_ERROR_INVALIDARGS);
- return false;
- }
- }
-
- if ($settings && isset($this->_query) && $this->_query['condition']) {
- $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName());
- $r = $this->_query("UPDATE {$table} SET {$settings} {$this->_query['condition']} ");
-
- $this->_query = $original_query;
- if ((new PEAR)->isError($r)) {
- $this->raiseError($r);
- return false;
- }
- if ($r < 1) {
- return 0;
- }
- $this->_clear_cache();
- return $r;
- }
-
- $this->_query = $original_query;
-
- if ($dataObject !== false) {
- return true;
- }
- $this->raiseError(
- "update: No Data specifed for query $settings , {$this->_query['condition']}",
- DB_DATAOBJECT_ERROR_NODATA
- );
- return false;
- }
-
- public function delete($useWhere = false)
- {
- global $_DB_DATAOBJECT;
-
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- $extra_cond = ' ' . (isset($this->_query['order_by']) ? $this->_query['order_by'] : '');
- if (!$useWhere) {
- $keys = $this->keys();
- $this->_query = array();
- $this->_query['condition'] = '';
- $this->_build_condition($this->table(), $keys);
-
- if (!$this->_query['condition']) {
- $this->_build_condition($this->table(), array(), $keys);
- }
- $extra_cond = '';
- }
-
- if (($this->_query !== false) && $this->_query['condition']) {
- $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName());
- $sql = "DELETE ";
-
- $sql .= (!empty($this->_join) && $useWhere) ?
- "{$table} FROM {$table} {$this->_join} " :
- "FROM {$table} ";
- $sql .= $this->_query['condition'] . $extra_cond;
-
- if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) {
- if (!isset($_DB_DATAOBJECT['CONFIG']['db_driver']) ||
- ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB')) {
-
- $sql = $DB->modifyLimitQuery($sql, $this->_query['limit_start'], $this->_query['limit_count']);
- } else {
-
- $DB->setLimit($this->_query['limit_count'], $this->_query['limit_start']);
- }
- }
- $r = $this->_query($sql);
- if ((new PEAR)->isError($r)) {
- $this->raiseError($r);
- return false;
- }
- if ($r < 1) {
- return 0;
- }
- $this->_clear_cache();
- return $r;
- } else {
- $this->raiseError("delete: No condition specifed for query", DB_DATAOBJECT_ERROR_NODATA);
- return false;
- }
- }
-
- public function fetchRow($row = null)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- $this->_loadConfig();
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("{$this->tableName()} $row of {$this->N}", "fetchrow", 3);
- }
- if (!$this->tableName()) {
- $this->raiseError("fetchrow: No table", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
- return false;
- }
- if ($row === null) {
- $this->raiseError("fetchrow: No row specified", DB_DATAOBJECT_ERROR_INVALIDARGS);
- return false;
- }
- if (!$this->N) {
- $this->raiseError("fetchrow: No results avaiable", DB_DATAOBJECT_ERROR_NODATA);
- return false;
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("{$this->tableName()} $row of {$this->N}", "fetchrow", 3);
- }
- $result = $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid];
- $array = $result->fetchrow(DB_DATAOBJECT_FETCHMODE_ASSOC, $row);
- if (!is_array($array)) {
- $this->raiseError("fetchrow: No results available", DB_DATAOBJECT_ERROR_NODATA);
- return false;
- }
- $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'];
- if ($dbtype === 'pgsql') {
- if (($_DB_DATAOBJECT['CONFIG']['db_driver'] ?? 'DB') === 'DB') {
- $tableInfo = $result->tableInfo();
- } elseif ($result->db->supports('result_introspection')) {
- $result->db->loadModule('Reverse', null, true);
- $tableInfo = $result->db->reverse->tableInfo($result);
- }
- }
- $replace = array('.', ' ');
- foreach (array_keys($array) as $i => $k) {
-
- $kk = (strpos($k, '.') === false && strpos($k, ' ') === false) ?
- $k : str_replace($replace, '_', $k);
- if ($dbtype === 'pgsql') {
- switch ($tableInfo[$i]['type']) {
- case 'bool':
- $array[$k] = str_replace(['t', 'f'], ['1', '0'], $array[$k]);
- break;
- case 'bytea':
- $array[$k] = pg_unescape_bytea($array[$k]);
- break;
- }
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("$kk = " . $array[$k], "fetchrow LINE", 3);
- }
- $this->$kk = $array[$k];
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug("{$this->tableName()} DONE", "fetchrow", 3);
- }
- return true;
- }
-
- public function count($countWhat = false, $whereAddOnly = false)
- {
- global $_DB_DATAOBJECT;
- if (is_bool($countWhat)) {
- $whereAddOnly = $countWhat;
- }
- $t = clone($this);
- $items = $t->table();
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- if (!isset($t->_query)) {
- $this->raiseError(
- "You cannot do run count after you have run fetch()",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- if (!$whereAddOnly && $items) {
- $t->_build_condition($items);
- }
- $keys = $this->keys();
- if (empty($keys[0]) && (!is_string($countWhat) || (strtoupper($countWhat) == 'DISTINCT'))) {
- $this->raiseError(
- "You cannot do run count without keys - use \$do->count('id'), or use \$do->count('distinct id')';",
- DB_DATAOBJECT_ERROR_INVALIDARGS,
- PEAR_ERROR_DIE
- );
- return false;
- }
- $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName());
- $key_col = empty($keys[0]) ? '' : (($quoteIdentifiers ? $DB->quoteIdentifier($keys[0]) : $keys[0]));
- $as = ($quoteIdentifiers ? $DB->quoteIdentifier('DATAOBJECT_NUM') : 'DATAOBJECT_NUM');
-
- $countWhat = (strtoupper($countWhat) == 'DISTINCT') ?
- "DISTINCT {$table}.{$key_col}" : $countWhat;
- $countWhat = is_string($countWhat) ? $countWhat : "{$table}.{$key_col}";
- $r = $t->_query(
- "SELECT count({$countWhat}) as $as
- FROM $table {$t->_join} {$t->_query['condition']}"
- );
- if ((new PEAR)->isError($r)) {
- return false;
- }
- $result = $_DB_DATAOBJECT['RESULTS'][$t->_DB_resultid];
- $l = $result->fetchRow(DB_DATAOBJECT_FETCHMODE_ORDERED);
-
- $t->free();
- if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
- $this->debug('Count returned ' . $l[0], 1);
- }
- return (int)$l[0];
- }
-
- public function free()
- {
- global $_DB_DATAOBJECT;
- if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) {
- unset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]);
- }
- if (isset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) {
- unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]);
- }
-
- $this->_clear_cache();
-
- if (isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->num_rows = array();
- }
- if (is_array($this->_link_loaded)) {
- foreach ($this->_link_loaded as $do) {
- if (
- !empty($this->{$do}) &&
- is_object($this->{$do}) &&
- method_exists($this->{$do}, 'free')
- ) {
- $this->{$do}->free();
- }
- }
- }
- return null;
- }
-
- public function query($string)
- {
- return $this->_query($string);
- }
-
- public function escape($string, $likeEscape = false)
- {
- global $_DB_DATAOBJECT;
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
-
- $dd = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver'];
- $ret = ($dd == 'DB') ? $DB->escapeSimple($string) : $DB->escape($string);
- if ($likeEscape) {
- $ret = str_replace(array('_', '%'), array('\_', '\%'), $ret);
- }
- return $ret;
- }
-
- public function database()
- {
- $args = func_get_args();
- if (count($args)) {
- $this->_database = $args[0];
- } else {
- $this->_connect();
- }
- return $this->_database;
- }
-
- public function link($field, $set_args = array())
- {
-
- require_once 'Links.php';
- $l = new DB_DataObject_Links($this);
- return $l->link($field, $set_args);
- }
-
- public function getLinks($format = '_%s')
- {
-
- require_once 'Links.php';
- $l = new DB_DataObject_Links($this);
- return $l->applyLinks($format);
- }
-
- public function getLink($row, $table = null, $link = false)
- {
-
- require_once 'Links.php';
- $l = new DB_DataObject_Links($this);
- return $l->getLink($row, $table === null ? false : $table, $link);
- }
-
- public function getLinkArray($row, $table = null)
- {
-
- require_once 'Links.php';
- $l = new DB_DataObject_Links($this);
- return $l->getLinkArray($row, $table === null ? false : $table);
- }
-
- public function unionAdd($obj, $is_all = '')
- {
- if ($obj === false) {
- $ret = $this->_query['unions'];
- $this->_query['unions'] = array();
- return $ret;
- }
- $this->_query['unions'][] = array($obj, 'UNION ' . $is_all . ' ');
- return $obj;
- }
-
- public function autoJoin($cfg = array())
- {
- global $_DB_DATAOBJECT;
-
- $pre_links = $this->links();
- if (!empty($cfg['links'])) {
- $this->links(array_merge($pre_links, $cfg['links']));
- }
- $map = $this->links();
- $this->databaseStructure();
- $dbstructure = $_DB_DATAOBJECT['INI'][$this->_database];
-
- $tabdef = $this->table();
-
- $keys = array_keys($tabdef);
- if (!empty($cfg['exclude'])) {
- $keys = array_intersect($keys, array_diff($keys, $cfg['exclude']));
- }
- if (!empty($cfg['include'])) {
- $keys = array_intersect($keys, $cfg['include']);
- }
- $selectAs = array();
- if (!empty($keys)) {
- $selectAs = array(array($keys, '%s', false));
- }
- $ret = array(
- 'cols' => array(),
- 'join_names' => array(),
- 'count' => false,
- );
- $has_distinct = false;
- if (!empty($cfg['distinct']) && $keys) {
-
- $cols = array();
-
- foreach ($keys as $c) {
-
- if ($cfg['distinct'] == $c) {
- $has_distinct = 'DISTINCT( ' . $this->tableName() . '.' . $c . ') as ' . $c;
- $ret['count'] = 'DISTINCT ' . $this->tableName() . '.' . $c . '';
- continue;
- }
-
- $cols = $c;
- }
-
- $selectAs = empty($cols) ? array() : array(array(array($cols), '%s', false));
- }
- foreach ($keys as $k) {
- $ret['cols'][$k] = $this->tableName() . '.' . $k;
- }
- foreach ($map as $ocl => $info) {
- list($tab, $col) = explode(':', $info);
-
-
- if (!isset($dbstructure[$tab])) {
- continue;
- }
- $xx = DB_DataObject::factory($tab);
- if (!is_object($xx) || !is_a($xx, 'DB_DataObject')) {
- continue;
- }
-
-
-
- $this->joinAdd($xx, 'LEFT', 'join_' . $ocl . '_' . $col, $ocl);
- if (!empty($cfg['exclude']) && in_array($ocl, $cfg['exclude'])) {
- continue;
- }
- $tabdef = $xx->table();
- $table = $xx->tableName();
- $keys = array_keys($tabdef);
- if (!empty($cfg['exclude'])) {
- $keys = array_intersect($keys, array_diff($keys, $cfg['exclude']));
- foreach ($keys as $k) {
- if (in_array($ocl . '_' . $k, $cfg['exclude'])) {
- $keys = array_diff($keys, $k);
- }
- }
- }
- if (!empty($cfg['include'])) {
-
- $nkeys = array();
- foreach ($keys as $k) {
- if (in_array(sprintf($ocl . '_%s', $k), $cfg['include'])) {
- $nkeys[] = $k;
- }
- }
- $keys = $nkeys;
- }
- if (empty($keys)) {
- continue;
- }
-
- if (!$has_distinct && !empty($cfg['distinct'])) {
- $cols = array();
- foreach ($keys as $c) {
- $tn = sprintf($ocl . '_%s', $c);
- if ($tn == $cfg['distinct']) {
- $has_distinct = 'DISTINCT( ' . 'join_' . $ocl . '_' . $col . '.' . $c . ') as ' . $tn;
- $ret['count'] = 'DISTINCT join_' . $ocl . '_' . $col . '.' . $c;
-
- continue;
- }
- $cols[] = $c;
- }
- if (!empty($cols)) {
- $selectAs[] = array($cols, $ocl . '_%s', 'join_' . $ocl . '_' . $col);
- }
- } else {
- $selectAs[] = array($keys, $ocl . '_%s', 'join_' . $ocl . '_' . $col);
- }
- foreach ($keys as $k) {
- $ret['cols'][sprintf('%s_%s', $ocl, $k)] = $tab . '.' . $k;
- $ret['join_names'][sprintf('%s_%s', $ocl, $k)] = sprintf('join_%s_%s.%s', $ocl, $col, $k);
- }
- }
-
- $this->selectAdd();
- if ($has_distinct) {
- $this->selectAdd($has_distinct);
- }
- foreach ($selectAs as $ar) {
- $this->selectAs($ar[0], $ar[1], $ar[2]);
- }
-
- $this->links($pre_links);
- return $ret;
- }
-
- public function links()
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- $this->_loadConfig();
- }
-
- $this->_connect();
-
- $lcfg = &$_DB_DATAOBJECT['LINKS'];
- $cfg = $_DB_DATAOBJECT['CONFIG'];
- if ($args = func_get_args()) {
-
-
- if (empty($lcfg[$this->_database])) {
- $lcfg[$this->_database] = array();
- }
- $lcfg[$this->_database][$this->tableName()] = $args[0];
- }
-
- if (isset($lcfg[$this->_database][$this->tableName()])) {
- return $lcfg[$this->_database][$this->tableName()];
- }
-
- if (isset($lcfg[$this->_database])) {
-
- return $lcfg[$this->_database] === false ? null : array();
- }
-
- $schemas = isset($cfg['schema_location']) ?
- array("{$cfg['schema_location']}/{$this->_database}.ini") :
- array();
-
-
- if (isset($cfg["ini_{$this->_database}"])) {
- $schemas = is_array($cfg["ini_{$this->_database}"]) ?
- $cfg["ini_{$this->_database}"] :
- explode(PATH_SEPARATOR, $cfg["ini_{$this->_database}"]);
- }
-
- $lcfg[$this->_database] = false;
- foreach ($schemas as $ini) {
- $links = isset($cfg["links_{$this->_database}"]) ?
- $cfg["links_{$this->_database}"] :
- str_replace('.ini', '.links.ini', $ini);
-
- if (!file_exists($links) || !is_file($links)) {
- if (!empty($cfg['debug'])) {
- $this->debug("Missing links.ini file: $links", "links", 1);
- }
- continue;
- }
-
- $lcfg[$this->_database] = empty($lcfg[$this->_database]) ? array() : $lcfg[$this->_database];
-
- $lcfg[$this->_database] = array_merge(
- $lcfg[$this->_database],
- parse_ini_file($links, true)
- );
- if (!empty($cfg['debug'])) {
- $this->debug("Loaded links.ini file: $links", "links", 1);
- }
- }
- if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) {
- foreach ($lcfg[$this->_database] as $k => $v) {
- $nk = strtolower($k);
-
- $lcfg[$this->_database][$nk] = isset($lcfg[$this->_database][$nk])
- ? $lcfg[$this->_database][$nk] : array();
- foreach ($v as $kk => $vv) {
-
- $vv = explode(':', $vv);
- $vv[0] = strtolower($vv[0]);
- $lcfg[$this->_database][$nk][$kk] = implode(':', $vv);
- }
- }
- }
-
-
-
- if ($lcfg[$this->_database] === false) {
- return null;
- }
- if (isset($lcfg[$this->_database][$this->tableName()])) {
- return $lcfg[$this->_database][$this->tableName()];
- }
- return array();
- }
-
- public function joinAdd($obj = false, $joinType = 'INNER', $joinAs = false, $joinCol = false)
- {
- global $_DB_DATAOBJECT;
- if ($obj === false) {
- $this->_join = '';
- return null;
- }
-
- $useWhereAsOn = false;
-
- if (is_array($joinType)) {
-
- $useWhereAsOn = !empty($joinType['useWhereAsOn']);
- $joinCol = isset($joinType['joinCol']) ? $joinType['joinCol'] : $joinCol;
- $joinAs = isset($joinType['joinAs']) ? $joinType['joinAs'] : $joinAs;
- $joinType = isset($joinType['joinType']) ? $joinType['joinType'] : 'INNER';
- }
-
-
-
- $ofield = false;
- $tfield = false;
- $toTable = false;
- if (is_array($obj)) {
- $tfield = $obj[0];
- if (count($obj) == 3) {
- $ofield = $obj[2];
- $obj = $obj[1];
- } else {
- list($toTable, $ofield) = explode(':', $obj[1]);
- $obj = is_string($toTable) ? DB_DataObject::factory($toTable) : $toTable;
- if (!$obj || !is_object($obj) || is_a($obj, 'PEAR_Error')) {
- $obj = new DB_DataObject;
- $obj->__table = $toTable;
- }
- $obj->_connect();
- }
-
-
- $items = array();
- }
- if (!is_object($obj) || !is_a($obj, 'DB_DataObject')) {
- return $this->raiseError("joinAdd: called without an object", DB_DATAOBJECT_ERROR_NODATA, PEAR_ERROR_DIE);
- }
-
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
-
-
-
- if (($ofield === false) && ($links = $this->links())) {
-
-
-
-
- foreach ($links as $k => $linkVar) {
- if (!is_array($linkVar)) {
- $linkVar = array($linkVar);
- }
- foreach ($linkVar as $v) {
-
- $ar = explode(':', $v);
-
- if (strpos($k, ',') !== false) {
- $k = explode(',', $k);
- }
- if (strpos($ar[1], ',') !== false) {
- $ar[1] = explode(',', $ar[1]);
- }
- if ($ar[0] != $obj->tableName()) {
- continue;
- }
- if ($joinCol !== false) {
- if ($k == $joinCol) {
-
- $tfield = $k;
- $ofield = $ar[1];
- break;
- }
- continue;
- }
- $tfield = $k;
- $ofield = $ar[1];
- break;
- }
- }
- }
-
-
- if (!$ofield && ($olinks = $obj->links())) {
- foreach ($olinks as $k => $linkVar) {
-
- if (!is_array($linkVar)) {
- $linkVar = array($linkVar);
- }
- foreach ($linkVar as $v) {
-
- $ar = explode(':', $v);
-
- $links_key_array = strpos($k, ',');
- if ($links_key_array !== false) {
- $k = explode(',', $k);
- }
- $ar_array = strpos($ar[1], ',');
- if ($ar_array !== false) {
- $ar[1] = explode(',', $ar[1]);
- }
- if ($ar[0] != $this->tableName()) {
- continue;
- }
-
-
-
- if ($joinCol !== false) {
- $this->raiseError(
- "joinAdd: You cannot target a join column in the " .
- "'link from' table ({$obj->tableName()}). " .
- "Either remove the fourth argument to joinAdd() " .
- "({$joinCol}), or alter your links.ini file.",
- DB_DATAOBJECT_ERROR_NODATA
- );
- return false;
- }
- $ofield = $k;
- $tfield = $ar[1];
- break;
- }
- }
- }
-
- if (($ofield === false) && $joinCol) {
- $ofield = $joinCol;
- $tfield = $joinCol;
- }
-
- if ($ofield === false) {
- $this->raiseError(
- "joinAdd: {$obj->tableName()} has no link with {$this->tableName()}",
- DB_DATAOBJECT_ERROR_NODATA
- );
- return false;
- }
- $joinType = strtoupper($joinType);
-
- if ($joinAs === false) {
- $joinAs = $obj->tableName();
- }
- $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
- $options = $_DB_DATAOBJECT['CONFIG'];
-
- $objTable = $quoteIdentifiers ?
- $DB->quoteIdentifier($obj->tableName()) :
- $obj->tableName();
- $dbPrefix = '';
- if (strlen($obj->_database) && in_array($DB->dsn['phptype'], array('mysql', 'mysqli'))) {
- $dbPrefix = ($quoteIdentifiers
- ? $DB->quoteIdentifier($obj->_database)
- : $obj->_database) . '.';
- }
-
- if ($obj->_database == $this->_database) {
- $dbPrefix = '';
- }
-
-
-
-
- $objTable = $dbPrefix . $objTable;
- $cond = '';
-
-
-
-
- if (strtolower(get_class($obj)) != 'db_dataobject') {
-
- $items = $obj->table();
-
-
- if (!$items) {
- $this->raiseError(
- "joinAdd: No table definition for {$obj->tableName()}",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return false;
- }
- $ignore_null = !isset($options['disable_null_strings'])
- || !is_string($options['disable_null_strings'])
- || strtolower($options['disable_null_strings']) !== 'full';
- foreach ($items as $k => $v) {
- if (!isset($obj->$k) && $ignore_null) {
- continue;
- }
- $kSql = ($quoteIdentifiers ? $DB->quoteIdentifier($k) : $k);
- if (DB_DataObject::_is_null($obj, $k)) {
- $obj->whereAdd("{$joinAs}.{$kSql} IS NULL");
- continue;
- }
- if ($v & DB_DATAOBJECT_STR) {
- $obj->whereAdd("{$joinAs}.{$kSql} = " . $this->_quote((string)(
- ($v & DB_DATAOBJECT_BOOL) ?
-
-
- (($obj->$k === 'f') ? 0 : (int)(bool)$obj->$k) :
- $obj->$k
- )));
- continue;
- }
- if (is_numeric($obj->$k)) {
- $obj->whereAdd("{$joinAs}.{$kSql} = {$obj->$k}");
- continue;
- }
- if (is_object($obj->$k) && is_a($obj->$k, 'DB_DataObject_Cast')) {
- $value = $obj->$k->toString($v, $DB);
- if ((new PEAR)->isError($value)) {
- $this->raiseError($value->getMessage(), DB_DATAOBJECT_ERROR_INVALIDARG);
- return false;
- }
- $obj->whereAdd("{$joinAs}.{$kSql} = $value");
- continue;
- }
-
- $obj->whereAdd("{$joinAs}.{$kSql} = 0");
- }
- if ($this->_query === false) {
- $this->raiseError(
- "joinAdd can not be run from a object that has had a query run on it,
- clone the object or create a new one and use setFrom()",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- }
-
- if ($obj->_query['condition']) {
- $cond = preg_replace('/^\sWHERE/i', '', $obj->_query['condition']);
- if (!$useWhereAsOn) {
- $this->whereAdd($cond);
- }
- }
-
- $appendJoin = '';
- if ($obj->_join) {
-
-
-
- if (in_array($DB->dsn["phptype"], array('pgsql'))) {
- $objTable = "($objTable {$obj->_join})";
- } else {
- $appendJoin = $obj->_join;
- }
- }
-
-
- if ($useWhereAsOn && strlen($cond)) {
- $appendJoin = ' AND ' . $cond . ' ' . $appendJoin;
- }
- $table = $this->tableName();
- if ($quoteIdentifiers) {
- $joinAs = $DB->quoteIdentifier($joinAs);
- $table = $DB->quoteIdentifier($table);
- $ofield = (is_array($ofield)) ? array_map(array($DB, 'quoteIdentifier'), $ofield) : $DB->quoteIdentifier($ofield);
- $tfield = (is_array($tfield)) ? array_map(array($DB, 'quoteIdentifier'), $tfield) : $DB->quoteIdentifier($tfield);
- }
-
- $fullJoinAs = '';
- $addJoinAs = ($quoteIdentifiers ? $DB->quoteIdentifier($obj->tableName()) : $obj->tableName()) != $joinAs;
- if ($addJoinAs) {
-
-
-
- $fullJoinAs = in_array($DB->dsn["phptype"], array('mysql', 'mysqli', 'pgsql')) ? "AS {$joinAs}" : $joinAs;
- } else {
-
- $joinAs = $dbPrefix . $joinAs;
- }
- switch ($joinType) {
- case 'INNER':
- case 'LEFT':
- case 'RIGHT':
-
- $jadd = "\n {$joinType} JOIN {$objTable} {$fullJoinAs}";
-
- if (is_array($ofield)) {
- $key_count = count($ofield);
- for ($i = 0; $i < $key_count; $i++) {
- if ($i == 0) {
- $jadd .= " ON ({$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]}) ";
- } else {
- $jadd .= " AND {$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]} ";
- }
- }
- $jadd .= ' ' . $appendJoin . ' ';
- } else {
- $jadd .= " ON ({$joinAs}.{$ofield}={$table}.{$tfield}) {$appendJoin} ";
- }
-
-
- $this->_join .= $jadd;
- break;
- case '':
- $this->_join .= "\n , {$objTable} {$fullJoinAs} {$appendJoin}";
- $this->whereAdd("{$joinAs}.{$ofield}={$table}.{$tfield}");
- }
- return true;
- }
-
- public function selectAs($from = null, $format = '%s', $tableName = false)
- {
- global $_DB_DATAOBJECT;
- if ($this->_query === false) {
- $this->raiseError(
- "You cannot do two queries on the same object (copy it before finding)",
- DB_DATAOBJECT_ERROR_INVALIDARGS
- );
- return false;
- }
- if ($from === null) {
-
- $this->selectAdd();
- $from = $this;
- }
- $table = $this->tableName();
- if (is_object($from)) {
- $table = $from->tableName();
- $from = array_keys($from->table());
- }
- if ($tableName !== false) {
- $table = $tableName;
- }
- $s = '%s';
- if (!empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers'])) {
- $this->_connect();
- $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- $s = $DB->quoteIdentifier($s);
- $format = $DB->quoteIdentifier($format);
- }
- foreach ($from as $k) {
- $this->selectAdd(sprintf("{$s}.{$s} as {$format}", $table, $k, $k));
- }
- $this->_query['data_select'] .= "\n";
- }
-
- public function sqlValue($value)
- {
- $method = 'sql';
- if (func_num_args() == 2) {
- $method = $value;
- $value = func_get_arg(1);
- }
-
- require_once 'Cast.php';
- return call_user_func(array('DB_DataObject_Cast', $method), $value);
- }
-
-
- public function setFrom($from, $format = '%s', $skipEmpty = false)
- {
- global $_DB_DATAOBJECT;
- $keys = $this->keys();
- $items = $this->table();
- if (!$items) {
- $this->raiseError(
- "setFrom:Could not find table definition for {$this->tableName()}",
- DB_DATAOBJECT_ERROR_INVALIDCONFIG
- );
- return null;
- }
- $overload_return = array();
- foreach (array_keys($items) as $k) {
- if (in_array($k, $keys)) {
- continue;
- }
- if (!$k) {
- continue;
- }
- $chk = is_object($from) &&
- (
- version_compare(phpversion(), "5.1.0", ">=") ?
- property_exists($from, sprintf($format, $k)) :
- array_key_exists(sprintf($format, $k), get_class_vars($from))
- );
-
- if ($chk) {
- $kk = (strtolower($k) == 'from') ? '_from' : $k;
- if (method_exists($this, 'set' . $kk)) {
- $ret = $this->{'set' . $kk}($from->{sprintf($format, $k)});
- if (is_string($ret)) {
- $overload_return[$k] = $ret;
- }
- continue;
- }
- $this->$k = $from->{sprintf($format, $k)};
- continue;
- }
- if (is_object($from)) {
- continue;
- }
- if (empty($from[sprintf($format, $k)]) && $skipEmpty) {
- continue;
- }
- if (!isset($from[sprintf($format, $k)]) && !DB_DataObject::_is_null($from, sprintf($format, $k))) {
- continue;
- }
- $kk = (strtolower($k) == 'from') ? '_from' : $k;
- if (method_exists($this, 'set' . $kk)) {
- $ret = $this->{'set' . $kk}($from[sprintf($format, $k)]);
- if (is_string($ret)) {
- $overload_return[$k] = $ret;
- }
- continue;
- }
- $val = $from[sprintf($format, $k)];
- if (is_a($val, 'DB_DataObject_Cast')) {
- $this->$k = $val;
- continue;
- }
- if (is_object($val) || is_array($val)) {
- continue;
- }
- $ret = $this->fromValue($k, $val);
- if ($ret !== true) {
- $overload_return[$k] = 'Not A Valid Value';
- }
-
- }
- if ($overload_return) {
- return $overload_return;
- }
- return true;
- }
-
- public function fromValue($col, $value)
- {
- global $_DB_DATAOBJECT;
- $options = $_DB_DATAOBJECT['CONFIG'];
- $cols = $this->table();
-
- if (!isset($cols[$col]) || is_a($value, 'DB_DataObject_Cast')) {
- $this->$col = $value;
- return true;
- }
-
- switch (true) {
-
- case ((!($cols[$col] & DB_DATAOBJECT_NOTNULL)) && DB_DataObject::_is_null($value, false)):
- case (is_object($value) && is_a($value, 'DB_DataObject_Cast')):
- $this->$col = $value;
- return true;
-
- case (($cols[$col] & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($value, false)):
- return false;
- case (($cols[$col] & DB_DATAOBJECT_DATE) && ($cols[$col] & DB_DATAOBJECT_TIME)):
-
- if (!$value) {
- $this->$col = '';
- }
- if (is_numeric($value)) {
- $this->$col = date('Y-m-d H:i:s', $value);
- return true;
- }
-
- $this->$col = (string)$value;
- return true;
- case ($cols[$col] & DB_DATAOBJECT_DATE):
-
- if (!$value) {
- $this->$col = '';
- return true;
- }
- if (is_numeric($value)) {
- $this->$col = date('Y-m-d', $value);
- return true;
- }
-
- require_once 'Date.php';
- $x = new Date($value);
- $this->$col = $x->format("%Y-%m-%d");
- return true;
- case ($cols[$col] & DB_DATAOBJECT_TIME):
-
- if (!$value) {
- $this->$col = '';
- }
- $guess = strtotime($value);
- if ($guess != -1) {
- $this->$col = date('H:i:s', $guess);
- return $return = true;
- }
-
- return false;
- case ($cols[$col] & DB_DATAOBJECT_STR):
- $this->$col = (string)$value;
- return true;
-
- default:
- $this->$col = $value;
- return true;
- }
- }
-
- public function toArray($format = '%s', $hideEmpty = false)
- {
- global $_DB_DATAOBJECT;
-
- $format = $format == '%s' ? false : $format;
- $ret = array();
- $rf = ($this->_resultFields !== false) ? $this->_resultFields :
- (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]) ?
- $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid] : false);
- $ar = ($rf !== false) ?
- (($hideEmpty === 0) ? $rf : array_merge($rf, $this->table())) :
- $this->table();
- foreach ($ar as $k => $v) {
- if (!isset($this->$k)) {
- if (!$hideEmpty) {
- $ret[$format === false ? $k : sprintf($format, $k)] = '';
- }
- continue;
- }
-
- if (method_exists($this, 'get' . $k) && !in_array(strtolower($k), array('links', 'link'))) {
- $ret[$format === false ? $k : sprintf($format, $k)] = $this->{'get' . $k}();
- continue;
- }
-
- $ret[$format === false ? $k : sprintf($format, $k)] = $this->$k;
- }
- if (!$this->_link_loaded) {
- return $ret;
- }
- foreach ($this->_link_loaded as $k) {
- $ret[$format === false ? $k : sprintf($format, $k)] = $this->$k->toArray();
- }
- return $ret;
- }
-
- public function validate()
- {
- global $_DB_DATAOBJECT;
- require_once 'Validate.php';
- $table = $this->table();
- $ret = array();
- $seq = $this->sequenceKey();
- $options = $_DB_DATAOBJECT['CONFIG'];
- foreach ($table as $key => $val) {
-
- $method = "Validate" . ucfirst($key);
- if (method_exists($this, $method)) {
- $ret[$key] = $this->$method();
- continue;
- }
-
- if ($val & DB_DATAOBJECT_NOTNULL && DB_DataObject::_is_null($this, $key)) {
-
- if (($key == $seq[0]) && ($seq[1] == true)) {
- continue;
- }
- $ret[$key] = false;
- continue;
- }
- if (DB_DataObject::_is_null($this, $key)) {
- if ($val & DB_DATAOBJECT_NOTNULL) {
- $this->debug("'null' field used for '$key', but it is defined as NOT NULL", 'VALIDATION', 4);
- $ret[$key] = false;
- continue;
- }
- continue;
- }
-
- if (!isset($this->$key)) {
- continue;
- }
-
- if (!is_object($this->$key) && !is_array($this->$key) && !strlen((string)$this->$key)) {
- continue;
- }
-
- if (is_object($this->$key) && is_a($this->$key, 'DB_DataObject_Cast')) {
- continue;
- }
-
-
-
- switch (true) {
-
- case ($val & DB_DATAOBJECT_STR):
- $ret[$key] = (new Validate)->string($this->$key, VALIDATE_PUNCTUATION . VALIDATE_NAME);
- break;
- case ($val & DB_DATAOBJECT_INT):
- $ret[$key] = (new Validate)->number($this->$key, array('decimal' => '.'));
- break;
- }
- }
-
- foreach ($ret as $key => $val) {
- if ($val !== true) {
- return $ret;
- }
- }
- return true;
- }
-
- public function getDatabaseConnection()
- {
- global $_DB_DATAOBJECT;
- if (($e = $this->_connect()) !== true) {
- return $e;
- }
- if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
- $r = false;
- return $r;
- }
- return $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
- }
-
- public function getDatabaseResult()
- {
- global $_DB_DATAOBJECT;
- $this->_connect();
- if (!isset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) {
- $r = false;
- return $r;
- }
- return $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid];
- }
-
- public function _call($method, $params, &$return)
- {
-
-
- if (strtolower($method) == strtolower(get_class($this))) {
- return true;
- }
- $type = strtolower(substr($method, 0, 3));
- $class = get_class($this);
- if (($type != 'set') && ($type != 'get')) {
- return false;
- }
-
- if (strtolower($method) == 'set_from') {
- $return = $this->toValue('from', isset($params[0]) ? $params[0] : null);
- return true;
- }
- $element = substr($method, 3);
-
- $array = array_keys(get_class_vars($class));
-
- if (class_exists('ReflectionClass')) {
- $reflection = new ReflectionClass($class);
- $array = array_keys($reflection->getdefaultProperties());
- }
- if (!in_array($element, $array)) {
-
- foreach ($array as $k) {
- $case[strtolower($k)] = $k;
- }
- if ((substr(phpversion(), 0, 1) == 5) && isset($case[strtolower($element)])) {
- trigger_error("PHP5 set/get calls should match the case of the variable", E_USER_WARNING);
- $element = strtolower($element);
- }
-
- if (!isset($case[$element])) {
- return false;
- }
-
- $element = $case[$element];
- }
- if ($type == 'get') {
- $return = $this->toValue($element, isset($params[0]) ? $params[0] : null);
- return true;
- }
- $return = $this->fromValue($element, $params[0]);
- return true;
- }
-
- public function toValue($col, $format = null)
- {
- if (is_null($format)) {
- return $this->$col;
- }
- $cols = $this->table();
- switch (true) {
- case (($cols[$col] & DB_DATAOBJECT_DATE) && ($cols[$col] & DB_DATAOBJECT_TIME)):
- if (!$this->$col) {
- return '';
- }
- $guess = strtotime($this->$col);
- if ($guess != -1) {
- return strftime($format, $guess);
- }
-
- return $this->$col;
- case ($cols[$col] & DB_DATAOBJECT_DATE):
- if (!$this->$col) {
- return '';
- }
- $guess = strtotime($this->$col);
- if ($guess != -1) {
- return strftime($format, $guess);
- }
-
- require_once 'Date.php';
- $x = new Date($this->$col);
- return $x->format($format);
- case ($cols[$col] & DB_DATAOBJECT_TIME):
- if (!$this->$col) {
- return '';
- }
- $guess = strtotime($this->$col);
- if ($guess > -1) {
- return strftime($format, $guess);
- }
-
- return $this->$col;
- case ($cols[$col] & DB_DATAOBJECT_MYSQLTIMESTAMP):
- if (!$this->$col) {
- return '';
- }
- require_once 'Date.php';
- $x = new Date($this->$col);
- return $x->format($format);
- case ($cols[$col] & DB_DATAOBJECT_BOOL):
- if ($cols[$col] & DB_DATAOBJECT_STR) {
-
- return ($this->$col === 't');
- }
- return (bool)$this->$col;
- default:
- return sprintf($format, $this->col);
- }
- }
-
- public function staticAutoloadTable($table)
- {
- global $_DB_DATAOBJECT;
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- DB_DataObject::_loadConfig();
- }
- $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ?
- $_DB_DATAOBJECT['CONFIG']['class_prefix'] : '';
- $class = $p . preg_replace('/[^A-Z0-9]/i', '_', ucfirst($table));
- $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($class, false) : class_exists($class);
- $class = $ce ? $class : DB_DataObject::_autoloadClass($class);
- return $class;
- }
-
- public function _get_table()
- {
- return $this->table();
- }
- public function _get_keys()
- {
- return $this->keys();
- }
- }
- if (!defined('DB_DATAOBJECT_NO_OVERLOAD')) {
- if ((phpversion() != '4.3.2-RC1') && (version_compare(phpversion(), "4.3.1") > 0)) {
- if (version_compare(phpversion(), "5") < 0) {
- overload('DB_DataObject');
- }
- $GLOBALS['_DB_DATAOBJECT']['OVERLOADED'] = true;
- }
- }
|