BxDolForm.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_FORM_METHOD_GET', 'get');
  10. define('BX_DOL_FORM_METHOD_POST', 'post');
  11. define('BX_DOL_FORM_METHOD_SPECIFIC', 'specific');
  12. define('BX_DATA_LISTS_KEY_PREFIX', '#!');
  13. define('BX_DATA_VALUES_DEFAULT', 'LKey'); ///< Use default values for data items, @see BxDolForm::getDataItems
  14. define('BX_DATA_VALUES_ADDITIONAL', 'LKey2'); ///< Use additional values for data items, @see BxDolForm::getDataItems
  15. define('BX_DATA_VALUES_ALL', 'all'); ///< Return all available info for data items, @see BxDolForm::getDataItems
  16. /**
  17. * Forms allows to display forms from data stored in DB tables, before it was possible to display forms from PHP arrays only.
  18. *
  19. *
  20. * The Form Objects have the following main advantages:
  21. * - Minimal coding is needed to create different forms
  22. * - Easy forms alterations
  23. * - Multiple representations of the same form
  24. * - Automated data inserting into database
  25. * - Automated data updating
  26. * - Automated data checking
  27. * - Automatic security checking
  28. * - Automatic spam filter
  29. *
  30. *
  31. * Forms API uses several definitions:
  32. * - Form or Form Object - record in sys_objects_form table, or instance of Form class.
  33. * - Form Display - set of some form inputs in particular order, displayed for some purpose; one form can have several displays, for example add and edit displays.
  34. * - Form Input or Form Field - form input field, like textarea, checkbox or set of radio buttons.
  35. *
  36. *
  37. * @section form_creating_object Creating the Form Object
  38. *
  39. * 1. Create Form Object, add record to sys_objects_form table:
  40. * - object - name of the Form Object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_group - for group data processing, like group adding or editing.
  41. * - title - Form Object title to display in studio forms builder.
  42. * - action - url to submit form to, if url is not full and not empty, then site url is added automatically.
  43. * - form_attrs - serialized array of additional form attributes.
  44. * - submit_name - name of form field with submit button, it is used to determine if form is submitted.
  45. * - table - DB table name (for automatic saving/updating).
  46. * - key - DB table field with unique ID (for automatic updating).
  47. * - uri - DB table field with URI (for automatic URI generation, aling with uri_title).
  48. * - uri_title - DB table field with data title (for automatic URI generation, aling with uri).
  49. * - params - serialized array of additional form parameters:
  50. * - checker_helper - name of custom BxDolFormCheckerHelper class.
  51. * - csrf - array of Cross-site request forgery attack prevention parameters, for now only one boolean parameter is supported - disabled, so it can be disabled for particular form.
  52. * - active - 1 or 0, if form is inactive then it can not be used anywhere.
  53. * - override_class_name - user defined class name which is derived from BxTemplFormView.
  54. * - override_class_file - the location of the user defined class, leave it empty if class is located in system folders.
  55. *
  56. *
  57. * 2. Create Form Displays, add records to sys_form_displays table:
  58. * - display_name - name of the Form Display, in the format:
  59. * form object name, underscore, internal identifier or nothing;
  60. * for example: bx_group_add - for displaying group adding form, bx_group_edit - for displaying group editing form
  61. * - module - module name this display belongs to, it must be associated with name field in sys_modules table.
  62. * - object - form object name from sys_objects_form table this Form Display belongs to.
  63. * - title - Form Display title to display in studio forms builder.
  64. * - view_mode - display form as read-only.
  65. *
  66. *
  67. * 3. Create Form Fields, add records to sys_form_inputs table:
  68. * - object - form object name from sys_objects_form table this Form Field belongs to.
  69. * - module - module name this field belongs to.
  70. * - name - unique Form Field name in particular From Object.
  71. * - value - default value, or empty if there is no default value.
  72. * - values - possible values, for certain form field types.
  73. * - checked - 0 or 1, it determines if form field is checked, for certain form field types.
  74. * - type - form field type, for now the following types are supported:
  75. * - text - text input field.
  76. * - password - password input field.
  77. * - textarea - multiline input field.
  78. * - number - number input field.
  79. * - select - select one from all available values.
  80. * - select_multiple - select one, multiple or all items from all available values.
  81. * - switcher - on/off switcher.
  82. * - checkbox - one checkbox.
  83. * - checkbox_set - set of checkboxes.
  84. * - radio_set - set of radio buttons.
  85. * - slider - select some numeric value within the range using slider control.
  86. * - doublerange - select range values within the range using slider control.
  87. * - datepicker - date selection control.
  88. * - datetime - date/time selection control.
  89. * - captcha - image captcha.
  90. * - hidden - hidden input field.
  91. * - file - file upload input.
  92. * - button - button control.
  93. * - image - form image button.
  94. * - reset - form reset button.
  95. * - submit - form submit button.
  96. * - value - just displaying value without any crontol.
  97. * - block_header - start group of field.
  98. * - custom - custom control.
  99. * - input_set - set of other form controls.
  100. * detailed description of every type will be described below.
  101. * - caption - input title.
  102. * - info - some info to help user to input data into the field, it's better to specify format and limits here.
  103. * - required - indicate that the input is required by displaying asterisk near the field,
  104. * please note that this field don't perform any checking automatically,
  105. * since you mark field as required you need to specify checked function which will check entered value.
  106. * - collapsed - display section as collapsed by default, for block_header field type only.
  107. * - html - display visual editor of certain type, for textarea field type only.
  108. * - 0 - no visual editor, leave textarea field as it is.
  109. * - 1 - standard(default) visual editor, @see BxDolEditor.
  110. * - 2 - full visual editor, @see BxDolEditor.
  111. * - 3 - mini visual editor, @see BxDolEditor.
  112. * - attrs - serialized array of additional input attributes.
  113. * - attrs_tr - serialized array of additional attributes for the whole input row.
  114. * - attrs_wrapper - serialized array of additional attributes for input wrapper.
  115. * - checker_func - checked function, if you marked field as required in textarea field you need to point one of the following checked functions:
  116. * - Length - check value length, additional params must contain min and/or max values for checking.
  117. * - Date - check if date is entered correctly.
  118. * - DateTime - check if datetime is entered correctly.
  119. * - Preg - check value with provided regular expression in checker_params field.
  120. * - Avail - just check if value isn't 0 or empty string, additional function parameters are not used.
  121. * - Email - check if value is written in valid email format.
  122. * - Captcha - check if captcha is entered correctly, for captcha field type only.
  123. * You can inherit BxDolFormCheckerHelper class and add own checker functions, you will need to point your custom class in Form Object params array.
  124. * - checker_params - serialized array of checker_func parameters.
  125. * - checker_error - error message to show in case of checking function returns false.
  126. * - db_pass - function to pass value through before saving to database and after restoring from database (for example when date need to be converted from/to timestamp value),
  127. * available values are the following:
  128. * - Int - convert value to integer.
  129. * - Float - convert value to floating point number.
  130. * - Date,DateUtc - convert value to timestamp value before saving to database, and convert from timespamp value after restoring from database.
  131. * - DateTime,DateTimeUtc - convert value to timestamp value before saving to database, and convert from timespamp value after restoring from database.
  132. * - Xss - it warns you that this text can contain XSS vulnerabilities and you need to be extra careful with this, and always use Forms engine to output string to the browser or use bx_process_output if going to output text manually.
  133. * - XssHtml - this text cam have HTML tags, so perform XSS vulnerabilies cleaning before saving to database.
  134. * - All - do not perform any conversion and pass text as it is, be careful with this, use it only when no other function can be used, and make all necessary security checking by yourself.
  135. * - Preg - perform regular expression on the text before saving data to database, regular expression can be provided in db_params field.
  136. * - Boolean - this is used for checkboxes with 'on' value which need to be converted into boolean value.
  137. * - Set - convert set of values into bit integer before saving to database, and restore bit integer into array of values upon restoration from database, it can be used for select_multiple and checkbox_set field types.
  138. * Please note that values for this field must be 1,2,4,8,... (values of power of 2); the max number of values are 31 for 32bit hardware and 63 for 64bit hardware.
  139. * You can inherit BxDolFormCheckerHelper class and add own pass functions, you will need to point your custom class in Form Object params array.
  140. * - db_params - serialized array of db_pass parameters.
  141. * - editable - allow to edit this field from admin forms builder.
  142. * - deletable - allow to delete this field from admin forms builder.
  143. *
  144. *
  145. * 4. Add Form Fields and Form Displays associations, add records to sys_form_display_inputs table:
  146. * - display_name - name of the Form Display from sys_form_displays table.
  147. * - input_name - name of the Form Field from sys_form_inputs table.
  148. * - visible_for_levels - bit field with set of member level ids. To use member level id in bit field the level id minus 1 is used as power of 2, for example:
  149. * - user level id = 1 -> 2^(1-1) = 1
  150. * - user level id = 2 -> 2^(2-1) = 2
  151. * - user level id = 3 -> 2^(3-1) = 4
  152. * - user level id = 4 -> 2^(4-1) = 8
  153. * - active - 1 - form field displayed on form, or 0 - isn't displayed.
  154. * - order - fields are displayed in this order.
  155. *
  156. *
  157. * @section form_field_types Form Field Types
  158. *
  159. * Detailed description of Form Field Types.
  160. *
  161. * Almost all fields have the following common parameters:
  162. * - object
  163. * - name
  164. * - type
  165. * - caption
  166. * - info
  167. * - required
  168. * - attrs
  169. * - attrs_tr
  170. * - attrs_wrapper
  171. *
  172. * We will not describe above list of parameters in every type, since they work the same way for all types.
  173. *
  174. * The list below are field types with their unique parameters, which are designed especially for this field, or some parameters which work differently for the specified field type.
  175. *
  176. * text - text input field. It is displayed as regular single line text input.
  177. * Parameters:
  178. * - value - default value, or empty if there is no default value.
  179. * - values - not applicable here.
  180. * - checked - not applicable here.
  181. * - collapsed - not applicable here.
  182. * - html - not applicable here.
  183. * - checker_func
  184. * Can be used here: Length, Preg, Avail, Email
  185. * Make no sense to use it here: Date, DateTime, Captcha
  186. * - db_pass
  187. * Can be used here: Int, Float, Xss, All, Preg
  188. * Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
  189. *
  190. * password - password input field. It is displayed as HTML input element with invisible input.
  191. * Parameters:
  192. * - value - default value, or empty if there is no default value.
  193. * - values - not applicable here.
  194. * - checked - not applicable here.
  195. * - collapsed - not applicable here.
  196. * - html - not applicable here.
  197. * - checker_func
  198. * Can be used here: Length, Preg, Avail.
  199. * Make no sense to use it here: Date, DateTime, Captcha, Email.
  200. * - db_pass
  201. * Can be used here: Xss, All.
  202. * Make no sense to use it here: Int, Float, Date, DateTime, XssHtml, Boolean, Set, Preg.
  203. *
  204. * textarea - multiline input field. It can be displayed as regular textarea field or as visual HTML editor.
  205. * Parameters:
  206. * - value - default value, or empty if there is no default value.
  207. * - values - not applicable here.
  208. * - checked - not applicable here.
  209. * - collapsed - not applicable here.
  210. * - html - use visual editor or not.
  211. * - 0 - no visual editor, leave textarea field as it is.
  212. * - 1 - standard(default) visual editor, @see BxDolEditor.
  213. * - 2 - full visual editor, @see BxDolEditor.
  214. * - 3 - mini visual editor, @see BxDolEditor.
  215. * - checker_func
  216. * Can be used here: Length, Preg, Avail
  217. * Make no sense to use it here: Email, Date, DateTime, Captcha
  218. * - db_pass
  219. * Can be used here: Int, Float, Xss, XssHtml, All, Preg
  220. * Make no sense to use it here: Date, DateTime, Boolean, Set
  221. *
  222. * number - number input field. It is displayed as HTL text input, but with limited width. Also some browsers can add additional controls to this field.
  223. * Parameters:
  224. * - value - default value, or empty if there is no default value.
  225. * - values - not applicable here.
  226. * - checked - not applicable here.
  227. * - collapsed - not applicable here.
  228. * - html - not applicable here.
  229. * - checker_func
  230. * Can be used here: Length, Preg, Avail
  231. * Make no sense to use it here: Email, Date, DateTime, Captcha
  232. * - db_pass
  233. * Can be used here: Int, Float
  234. * Make no sense to use it here: Xss, XssHtml, All, Preg, Date, DateTime, Boolean, Set
  235. *
  236. * select - select one from all available values. It is displayed as HTML combo-box.
  237. * Parameters:
  238. * - value - default value (array index of selected item from values array), or empty - if there is no default value.
  239. * - values - serialized array of available values, or reference to predefined set of values.
  240. * - checked - not applicable here.
  241. * - collapsed - not applicable here.
  242. * - html - not applicable here.
  243. * - checker_func
  244. * Can be used here: Length, Preg, Avail
  245. * Make no sense to use it here: Email, Date, DateTime, Captcha
  246. * - db_pass
  247. * Can be used here: Int, Float, Xss, All, Preg
  248. * Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
  249. *
  250. * select_multiple - select one, multiple or all items from all available values. It is displayed as HTML multiple selection input.
  251. * Parameters:
  252. * - value - default value (bit integer of array indexes of selected items from values array), or empty - if there is no default value.
  253. * - values - serialized array of available values, or reference to predefined set of values. Array index must be power of 2. Max number of values is 31 on 32bit hardware or 63 on 64bit hardware.
  254. * - checked - not applicable here.
  255. * - collapsed - not applicable here.
  256. * - html - not applicable here.
  257. * - checker_func
  258. * Can be used here: Length, Preg, Avail
  259. * Make no sense to use it here: Email, Date, DateTime, Captcha
  260. * - db_pass
  261. * Can be used here: Int
  262. * Make no sense to use it here: Float, Xss, All, Preg, Date, DateTime, XssHtml, Boolean, Set
  263. *
  264. * switcher - on/off switcher. It is displayed as custom HTML element with own styles, but on background it works as regular HTML checkbox element.
  265. * Parameters:
  266. * - value - the value which will be submitted if switcher is on, if switcher is off - nothing is submitted.
  267. * - values - not applicable here.
  268. * - checked - if set to 1 then switcher is on by default, 0 - it is off by default.
  269. * - collapsed - not applicable here.
  270. * - html - not applicable here.
  271. * - checker_func
  272. * Can be used here: Length, Preg, Avail
  273. * Make no sense to use it here: Email, Date, DateTime, Captcha
  274. * - db_pass
  275. * Can be used here: Int, Float, Xss, All, Preg, Boolean
  276. * Make no sense to use it here: Date, DateTime, XssHtml, Set
  277. *
  278. * checkbox - one checkbox. Displayed as HTML checkbox input element.
  279. * Parameters:
  280. * - value - the value which will be submitted if checkbox is checked, if checkbox isn't checked - nothing is submitted.
  281. * - values - not applicable here.
  282. * - checked - if set to 1 then checkbox is checked by default, 0 - it is unchecked by default.
  283. * - collapsed - not applicable here.
  284. * - html - not applicable here.
  285. * - checker_func
  286. * Can be used here: Length, Preg, Avail
  287. * Make no sense to use it here: Email, Date, DateTime, Captcha
  288. * - db_pass
  289. * Can be used here: Int, Float, Xss, All, Preg, Boolean
  290. * Make no sense to use it here: Date, DateTime, XssHtml, Set
  291. *
  292. * checkbox_set - set of checkboxes. It is displayed as set of checkboxes. It is displayed in one row if number of items is equal or less than 3 or every item is displayed on new line if there is more than 3 items in the set.
  293. * Parameters:
  294. * - value - default value (bit integer of array indexes of selected items from values array), or empty - if there is no default value.
  295. * - values - serialized array of available values, or reference to predefined set of values. Array index must be power of 2. Max number of values is 31 on 32bit hardware or 63 on 64bit hardware.
  296. * - checked - not applicable here.
  297. * - collapsed - not applicable here.
  298. * - html - not applicable here.
  299. * - checker_func
  300. * Can be used here: Length, Preg, Avail
  301. * Make no sense to use it here: Email, Date, DateTime, Captcha
  302. * - db_pass
  303. * Can be used here: Int
  304. * Make no sense to use it here: Float, Xss, All, Preg, Date, DateTime, XssHtml, Boolean, Set
  305. *
  306. * radio_set - set of radio buttons. It is displayed as set of radio buttons. It is displayed in one row if number of items is equal or less than 3 or every item is displayed on new line if there is more than 3 items in the set.
  307. * Parameters:
  308. * - value - default value (array index of selected radio button from values array), or empty - if there is no default value.
  309. * - values - serialized array of available values, or reference to predefined set of values.
  310. * - checked - not applicable here.
  311. * - collapsed - not applicable here.
  312. * - html - not applicable here.
  313. * - checker_func
  314. * Can be used here: Length, Preg, Avail
  315. * Make no sense to use it here: Email, Date, DateTime, Captcha
  316. * - db_pass
  317. * Can be used here: Int, Float, Xss, All, Preg
  318. * Make no sense to use it here: Date, DateTime, XssHtml, Boolean, Set
  319. *
  320. * slider - select some numeric value within the range using slider control. It is displayed as jQuery UI HTML control, but on background it works as regular HTML text input element.
  321. * Parameters:
  322. * - value - default value in the format, or empty if there is no default value.
  323. * - values - not applicable here.
  324. * - checked - not applicable here.
  325. * - collapsed - not applicable here.
  326. * - html - not applicable here.
  327. * - attrs - the following additional attributes can be used here:
  328. * - min - minimal value, default is 0.
  329. * - max - maximal value, default is 100.
  330. * - step - value can be changed by this step only, default is 1.
  331. * - checker_func
  332. * Can be used here: Length, Avail
  333. * Make no sense to use it here: Preg, Email, Date, DateTime, Captcha
  334. * - db_pass
  335. * Can be used here: Int, Float
  336. * Make no sense to use it here: Xss, XssHtml, All, Preg, Date, DateTime, Boolean, Set
  337. *
  338. * doublerange - select range values within the range using slider control.
  339. * Parameters:
  340. * - value - default value in the format [min value]-[max value], for example 16-99, or empty if there is no default value.
  341. * - values - not applicable here.
  342. * - checked - not applicable here.
  343. * - collapsed - not applicable here.
  344. * - html - not applicable here.
  345. * - attrs - the following additional attributes can be used here:
  346. * - min - minimal value, default is 0.
  347. * - max - maximal value, default is 100.
  348. * - step - value can be changed by this step only, default is 1.
  349. * - checker_func
  350. * Can be used here: Length, Avail
  351. * Make no sense to use it here: Preg, Email, Date, DateTime, Captcha
  352. * - db_pass
  353. * Can be used here: Xss, All, Preg
  354. * Make no sense to use it here: Int, Float, XssHtml, Date, DateTime, Boolean, Set
  355. *
  356. * datepicker - date selection control. It is displayed as HTML text input control, when clicking on this input then popup with date selector control is appeared.
  357. * Parameters:
  358. * - value - default value, in the format YYYY-MMM-DD, or empty if there is no default value.
  359. * - values - not applicable here.
  360. * - checked - not applicable here.
  361. * - collapsed - not applicable here.
  362. * - html - not applicable here.
  363. * - checker_func
  364. * Can be used here: Date
  365. * Make no sense to use it here: Length, Preg, Avail, Email, DateTime, Captcha
  366. * - db_pass
  367. * Can be used here: Date, DateUtc
  368. * Make no sense to use it here: Int, Float, Xss, All, Preg, DateTime, XssHtml, Boolean, Set
  369. *
  370. * datetime - date/time selection control.
  371. * Parameters:
  372. * - value - default value, in the format YYYY-MMM-DD HH:MM:SS, or empty if there is no default value.
  373. * - values - not applicable here.
  374. * - checked - not applicable here.
  375. * - collapsed - not applicable here.
  376. * - html - not applicable here.
  377. * - checker_func
  378. * Can be used here: DateTime
  379. * Make no sense to use it here: Length, Preg, Avail, Email, Date, Captcha
  380. * - db_pass
  381. * Can be used here: DateTime, DateTimeUtc
  382. * Make no sense to use it here: Int, Float, Xss, All, Preg, Date, XssHtml, Boolean, Set
  383. *
  384. * captcha - image captcha. Displayed as image with some text along with HTML text input for entering displayed on the image text.
  385. * Parameters:
  386. * - value - not applicable here.
  387. * - values - not applicable here.
  388. * - checked - not applicable here.
  389. * - collapsed - not applicable here.
  390. * - html - not applicable here.
  391. * - checker_func
  392. * Can be used here: Captcha
  393. * Make no sense to use it here: Length, Preg, Avail, Email, Date, DateTime
  394. * - db_pass
  395. * Can be used here: Xss, All, Preg
  396. * Make no sense to use it here: Int, Float, Date, DateTime, XssHtml, Boolean, Set
  397. *
  398. * hidden - hidden input field. Displayed as hidden HTML input.
  399. * Parameters:
  400. * - value - hidden input value.
  401. * - values - not applicable here.
  402. * - checked - not applicable here.
  403. * - collapsed - not applicable here.
  404. * - html - not applicable here.
  405. * - checker_func
  406. * Can be used here: Length, Preg, Avail, Email, Date, DateTime
  407. * Make no sense to use it here: Captcha
  408. * - db_pass
  409. * Can be used here: Int, Float, Xss, All, Preg, Date, DateUtc, DateTime, DateTimeUtc, XssHtml, Boolean
  410. * Make no sense to use it here: Set
  411. *
  412. * file - file upload input. Displayed as file upload HTML input.
  413. * Parameters:
  414. * - value - not applicable here.
  415. * - values - not applicable here.
  416. * - checked - not applicable here.
  417. * - collapsed - not applicable here.
  418. * - html - not applicable here.
  419. * - checker_func
  420. * File name is passed for checking.
  421. * Can be used here: Avail, Length, Preg
  422. * Make no sense to use it here: Email, Date, DateTime, Captcha
  423. * - db_pass
  424. * File can't be stored in the database, so this field isn't applicable here.
  425. *
  426. * files - files upload input. Displayed as complex uploading HTML control.
  427. * This control is too complex to describe it using default set of database fields, you need to use custom class to display this control.
  428. *
  429. * button - button control. Displayes as HTML button element.
  430. * Parameters:
  431. * - value - translatable button caption.
  432. * - values - not applicable here.
  433. * - checked - not applicable here.
  434. * - collapsed - not applicable here.
  435. * - html - not applicable here.
  436. * - checker_func - not applicable here.
  437. * - db_pass - not applicable here.
  438. *
  439. * image - form image button. It is displayed as HTML form image input element.
  440. * Parameters:
  441. * - value - not applicable here.
  442. * - values - not applicable here.
  443. * - checked - not applicable here.
  444. * - collapsed - not applicable here.
  445. * - html - not applicable here.
  446. * - attrs - the following mandatory attribute must be specified here:
  447. * - src - image URL.
  448. * - checker_func - not applicable here.
  449. * - db_pass - not applicable here.
  450. *
  451. * reset - form reset button. Displayed as HTML form reset input button. By clicking on this button the form is reset to its default state.
  452. * Parameters:
  453. * - value - translatable button caption.
  454. * - values - not applicable here.
  455. * - checked - not applicable here.
  456. * - collapsed - not applicable here.
  457. * - html - not applicable here.
  458. * - checker_func - not applicable here.
  459. * - db_pass - not applicable here.
  460. *
  461. * submit - form submit button. Displayed as HTML form submit input button. This button have the primary button style to distinguish it from other buttons. By clicking on this button the form is submitted.
  462. * Parameters:
  463. * - value - translatable button caption.
  464. * - values - not applicable here.
  465. * - checked - not applicable here.
  466. * - collapsed - not applicable here.
  467. * - html - not applicable here.
  468. * - checker_func - not applicable here.
  469. * - db_pass - not applicable here.
  470. *
  471. * value - just displaying value without any control.
  472. * Parameters:
  473. * - value - the value to display.
  474. * - values - not applicable here.
  475. * - checked - not applicable here.
  476. * - collapsed - not applicable here.
  477. * - html - not applicable here.
  478. * - checker_func - not applicable here.
  479. * - db_pass - not applicable here.
  480. *
  481. * block_header - start group of fields. Displayed as form fields divider with caption - then it can be collapsible or without caption - then it is just divider without any functionality.
  482. * Parameters:
  483. * - value - not applicable here.
  484. * - values - not applicable here.
  485. * - checked - not applicable here.
  486. * - collapsed - display group of field collapsed by default, 1 - the group is collapsed, 0 - expanded (default value).
  487. * - html - not applicable here.
  488. * - checker_func - not applicable here.
  489. * - db_pass - not applicable here.
  490. *
  491. * custom - custom control. You need custom class to display this control, so the exact used values are determined by particular realisation.
  492. *
  493. * input_set - set of other form controls.
  494. * Parameters:
  495. * - value - not applicable here.
  496. * - values - comma separated list of field names (by name field) of fields to display here.
  497. * - checked - not applicable here.
  498. * - collapsed - not applicable here.
  499. * - html - not applicable here.
  500. * - checker_func - not applicable here.
  501. * - db_pass - not applicable here.
  502. *
  503. *
  504. * @section form_using_own_class Using own class for custom behavior
  505. *
  506. * It is possible to provide own class for displaying and processing the form.
  507. * To do this you need to point it in override_class_name and override_class_file fields in sys_objects_form table.
  508. * Your custom class must be inherited from BxTemplFormView class or its descendants.
  509. *
  510. *
  511. * @section form_display_custom_control Displaying custom field control
  512. *
  513. * It is possible to leave form field with default caption and override only the form field control.
  514. * To override some field you need to define the following function:
  515. * @code
  516. * protected function genCustomInput[field name] ($aInput).
  517. * @endcode
  518. * Where [field name] is form field name.
  519. * For example:
  520. *
  521. * @code
  522. * protected function genCustomInputCustom ($aInput) {
  523. * return
  524. * 'r: <input type="text" size="2" value="'.(isset($aInput['value'][0]) ? $aInput['value'][0] : '').'" name="'.$aInput['name'].'[]" />' .
  525. * 'g: <input type="text" size="2" value="'.(isset($aInput['value'][1]) ? $aInput['value'][1] : '').'" name="'.$aInput['name'].'[]" />' .
  526. * 'b: <input type="text" size="2" value="'.(isset($aInput['value'][2]) ? $aInput['value'][2] : '').'" name="'.$aInput['name'].'[]" />';
  527. * }
  528. * @endcode
  529. *
  530. *
  531. * @section form_display_custom_row Displaying custom field row
  532. *
  533. * Form row consists of caption and control, by default it is displayed with default design and functionality.
  534. * If you need to display some field with custom header and control you need to declare the following function:
  535. * @code
  536. * protected function genCustomRow[field name] ($aInput).
  537. * @endcode
  538. * Where [field name] is form field name.
  539. *
  540. *
  541. * @section example Example of usage
  542. *
  543. * Printing the form for adding new record to the database:
  544. *
  545. * @code
  546. * $oForm = BxDolForm::getObjectInstance('sample_form_objects', 'sample_form_objects_add'); // get form instance for specified form object and display
  547. * if (!$oForm)
  548. * die('"sample_form_objects_add" form object or "sample_form_objects_add" display is not defined');
  549. * $oForm->initChecker(); // init form checker witout any data - adding new record
  550. * if ($oForm->isSubmittedAndValid())
  551. * echo 'inserted id: ' . $oForm->insert (); // add new record to the database
  552. * echo $oForm->getCode(); // display form
  553. * @endcode
  554. *
  555. * Printing the form for editing existing record in the database:
  556. *
  557. * @code
  558. * // $iEditId - ID of edited row, for example from _GET parameter
  559. * $oDb = BxDolDb::getInstance();
  560. * $sQuery = $oDb->prepare("SELECT * FROM `sample_input_types` WHERE id = ?", $iEditId);
  561. * $aRecord = $oDb->getRow();
  562. * if (!$aRecord)
  563. * die("$iEditId record wasn't found.");
  564. *
  565. * $oForm = BxDolForm::getObjectInstance('sample_form_objects', 'sample_form_objects_edit'); // get form instance for specified form object and display
  566. * if (!$oForm)
  567. * die('"sample_form_objects_edit" form object or "sample_form_objects_edit" display is not defined');
  568. * $oForm->initChecker($aRecord); // init form checker with edited data
  569. * if ($oForm->isSubmittedAndValid())
  570. * echo 'updated: ' . $oForm->update ($iEditId); // update database
  571. * echo $oForm->getCode(); // display form
  572. * @endcode
  573. *
  574. * Example of custom form class and custom checking helper class:
  575. *
  576. * @code
  577. * class BxSampleForm extends BxTemplFormView {
  578. *
  579. * public function __construct ($aInfo, $oTemplate = false) {
  580. * parent::__construct ($aInfo, $oTemplate);
  581. * }
  582. *
  583. *
  584. * // display input with 'custom' name
  585. * protected function genCustomInputCustom ($aInput) {
  586. * return
  587. * 'r: <input type="text" size="2" value="'.(isset($aInput['value'][0]) ? $aInput['value'][0] : '').'" name="'.$aInput['name'].'[]" />' .
  588. * 'g: <input type="text" size="2" value="'.(isset($aInput['value'][1]) ? $aInput['value'][1] : '').'" name="'.$aInput['name'].'[]" />' .
  589. * 'b: <input type="text" size="2" value="'.(isset($aInput['value'][2]) ? $aInput['value'][2] : '').'" name="'.$aInput['name'].'[]" />';
  590. * }
  591. *
  592. * }
  593. *
  594. * class BxSampleFormCheckerHelper extends BxDolFormCheckerHelper {
  595. *
  596. * protected $_sDiv = ',';
  597. *
  598. * // prepare RBG values to save to the DB
  599. * function passRgb ($s) {
  600. * if (!is_array($s))
  601. * return false;
  602. *
  603. * $sRet = '';
  604. * foreach ($s as $k => $v)
  605. * $sRet .= (int)trim($v) . $this->_sDiv;
  606. *
  607. * return trim($sRet, $this->_sDiv);
  608. * }
  609. *
  610. * // prepare RGB values to output to the screen
  611. * function displayRgb ($s) {
  612. * return explode($this->_sDiv, $s);
  613. * }
  614. * }
  615. * @endcode
  616. *
  617. * The recommended way is to define forms in database, if it is impossible for some reasons you can init form object from array, there is an example:
  618. *
  619. * @code
  620. * $aForm = array(
  621. * 'form_attrs' => array(
  622. * 'name' => 'form_my',
  623. * 'method' => 'post',
  624. * ),
  625. *
  626. * 'params' => array (
  627. * 'db' => array(
  628. * 'table' => 'table_name', // table name
  629. * 'key' => 'ID', // key field name
  630. * 'uri' => 'EntryUri', // uri field name
  631. * 'uri_title' => 'Title', // title field to generate uri from
  632. * 'submit_name' => 'submit_form', // some filed name with non empty value to determine if the for was submitted,
  633. * in most cases it is submit button name
  634. * ),
  635. * 'csrf' => array(
  636. * 'disable' => true, //if it wasn't set or has some other value then CSRF checking is enabled for current form, take a look at sys_security_form_token_enable to disable CSRF checking completely.
  637. * )
  638. * ),
  639. *
  640. * 'inputs' => array(
  641. *
  642. * 'Title' => array(
  643. * 'type' => 'text',
  644. * 'name' => 'Title', // the same as key and database field name
  645. * 'caption' => 'Some caption',
  646. * 'required' => true,
  647. *
  648. * // checker params
  649. * 'checker' => array (
  650. * 'func' => 'length', // see BxDolFormCheckerHelper class for all check* functions
  651. * 'params' => array(3,100),
  652. * 'error' => 'length must be from 3 to 100 characters',
  653. * ),
  654. * // database params
  655. * 'db' => array (
  656. * 'pass' => 'Xss', // do XSS clear before getting this value, see BxDolFormCheckerHelper class for all pass* functions
  657. * ),
  658. * ),
  659. *
  660. * 'Description' => array(
  661. * 'type' => 'textarea',
  662. * 'name' => 'Description', // the same as key and database field name
  663. * 'caption' => 'Some caption',
  664. * 'required' => true,
  665. *
  666. * // checker params
  667. * 'checker' => array (
  668. * 'func' => 'length',
  669. * 'error' => 'enter at least 3 characters',
  670. * 'params' => array(3,64000),
  671. * ),
  672. * 'db' => array (
  673. * 'pass' => 'XssHtml', // do XSS clear, but keep HTML before getting this value
  674. * ),
  675. * ),
  676. * );
  677. * @endcode
  678. *
  679. * Using of above array:
  680. *
  681. * @code
  682. * $oForm = new BxTemplFormView ($aForm);
  683. * $oForm->initChecker();
  684. *
  685. * if ($oForm->isSubmittedAndValid ()) {
  686. *
  687. * // add additional vars to database, in this case creation date field is added
  688. * $aValsAdd = array (
  689. * 'Date' => time(),
  690. * );
  691. *
  692. * echo 'insert last id: ' . $oForm->insert ($aValsAdd); // insert validated data to database
  693. *
  694. * } else {
  695. *
  696. * echo $oForm->getCode (); // show form
  697. *
  698. * }
  699. *
  700. * @endcode
  701. *
  702. */
  703. class BxDolForm extends BxDol implements iBxDolReplaceable
  704. {
  705. static $TYPES_SKIP = array('files' => 1);
  706. static $TYPES_CHECKBOX = array('checkbox' => 1, 'switcher' => 1);
  707. static $TYPES_TEXT = array('text' => 1, 'textarea' => 1);
  708. static $TYPES_FILE = array('file' => 1);
  709. static $FUNC_SKIP_DOMAIN_CHECK = array('email' => 1, 'emails' => 1, 'emailexist' => 1, 'emailuniq' => 1, 'emailexistorempty' => 1, 'hostdomain' => 1, 'hostdomainchat' => 1, 'emailorempty' => 1);
  710. protected $_aMarkers = array ();
  711. protected $oTemplate;
  712. protected $_isValid = true;
  713. protected $_sCheckerHelper;
  714. protected $_aSpecificValues;
  715. public $aFormAttrs; ///< form html element attributes
  716. public $aInputs; ///< form inputs
  717. public $aParams; ///< additional form parameters
  718. public $id; ///< Form element id
  719. protected $_aFieldsCheckForSpam = array(); ///< additional fields names to check for spam(profanity filter), now only fields with 'textarea' and 'text' are checked for spam, 'textarea' fields are checked for spam and filter for profanity, while 'text' fields are filtetered for profanity only
  720. public function __construct ($aInfo, $oTemplate)
  721. {
  722. parent::__construct();
  723. if ($oTemplate)
  724. $this->oTemplate = $oTemplate;
  725. else
  726. $this->oTemplate = BxDolTemplate::getInstance();
  727. $this->aFormAttrs = isset($aInfo['form_attrs']) ? $aInfo['form_attrs'] : array();
  728. $this->aInputs = isset($aInfo['inputs']) ? $aInfo['inputs'] : array();
  729. $this->aParams = isset($aInfo['params']) ? $aInfo['params'] : array();
  730. // get form element id
  731. $this->id = $this->aFormAttrs['id'] = (!empty($this->aFormAttrs['id']) ? $this->aFormAttrs['id'] : (!empty($this->aFormAttrs['name']) ? $this->aFormAttrs['name'] : 'form_advanced'));
  732. // set default method
  733. if (!isset($this->aFormAttrs['method']))
  734. $this->aFormAttrs['method'] = BX_DOL_FORM_METHOD_POST;
  735. // set default action
  736. if (!isset($this->aFormAttrs['action']))
  737. $this->aFormAttrs['action'] = '';
  738. $this->_sCheckerHelper = isset($this->aParams['checker_helper']) ? $this->aParams['checker_helper'] : '';
  739. }
  740. /**
  741. * Get form object instance by object name
  742. * @param $sObject object name
  743. * @return object instance or false on error
  744. */
  745. static public function getObjectInstance($sObject, $sDisplayName, $oTemplate = false, $sParam = '')
  746. {
  747. $sKey = 'BxDolForm!'.$sObject.'!'.$sDisplayName.'!'.$sParam;
  748. if (isset($GLOBALS['bxDolClasses'][$sKey]))
  749. return $GLOBALS['bxDolClasses'][$sKey];
  750. $aObject = BxDolFormQuery::getFormArray($sObject, $sDisplayName);
  751. if (!$aObject || !is_array($aObject))
  752. return false;
  753. $sClass = 'BxTemplFormView';
  754. if (!empty($aObject['override_class_name'])) {
  755. $sClass = $aObject['override_class_name'];
  756. if (!empty($aObject['override_class_file']))
  757. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  758. }
  759. $o = new $sClass($aObject, $oTemplate);
  760. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  761. }
  762. /**
  763. * Get data items array
  764. * @param $sKey data items identifier
  765. * @param $isUseForSet convert data items keys to use in set fields, make it power of 2 (1,2,4,8,16,etc).
  766. * @param $sUseValues use default(BX_DATA_VALUES_DEFAULT) or additional(BX_DATA_VALUES_ADDITIONAL) value titles, if additinal value title is missing default title is used
  767. * @return data items array
  768. */
  769. public static function getDataItems($sKey, $isUseForSet = false, $sUseValues = BX_DATA_VALUES_DEFAULT)
  770. {
  771. return BxDolFormQuery::getDataItems($sKey, $isUseForSet, $sUseValues);
  772. }
  773. function initChecker ($aValues = array (), $aSpecificValues = array())
  774. {
  775. $this->_aSpecificValues = $aSpecificValues;
  776. $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
  777. $oChecker->setFormMethod($this->aFormAttrs['method'], $aSpecificValues);
  778. $oChecker->setFormParams($this->aParams);
  779. $oChecker->setFieldsCheckForSpam($this->_aFieldsCheckForSpam);
  780. // init form with default values
  781. $aValuesDef = array ();
  782. foreach ($this->aInputs as $k => $a) {
  783. if (!isset($a['value']) || !isset($a['db']['pass']) || isset(self::$TYPES_CHECKBOX[$a['type']]) || isset(self::$TYPES_SKIP[$a['type']]))
  784. continue;
  785. $aValuesDef[$k] = $a['value'];
  786. }
  787. $oChecker->fillWithValues($this->aInputs, $aValuesDef);
  788. // init form with provided values
  789. if ($aValues)
  790. $oChecker->fillWithValues($this->aInputs, $aValues);
  791. if ($this->isSubmitted ()) {
  792. // init form with submitted data, overwrite prevously declared values
  793. $oChecker->enableFormCsrfChecking(isset($this->aParams['csrf']['disable']) && $this->aParams['csrf']['disable'] === true ? false : true);
  794. $this->_isValid = $oChecker->check($this->aInputs, $aValues);
  795. if (!$this->_initCheckerNestedForms ())
  796. $this->_isValid = false;
  797. }
  798. }
  799. function getKeyValuesPair ()
  800. {
  801. $aRet = array ();
  802. foreach ($this->aInputs as $k => $a) {
  803. if (!isset($a['value']) || isset(self::$TYPES_CHECKBOX[$a['type']]) || isset(self::$TYPES_SKIP[$a['type']]))
  804. continue;
  805. $aRet[$k] = $a['value'];
  806. }
  807. return $aRet;
  808. }
  809. function insert ($aValsToAdd = array(), $isIgnore = false)
  810. {
  811. $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
  812. $oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
  813. $sSql = $oChecker->dbInsert($this->aParams['db'], $this->aInputs, $aValsToAdd, $isIgnore);
  814. if (!$sSql)
  815. return false;
  816. $oDb = BxDolDb::getInstance();
  817. if ($oDb->query($sSql))
  818. return $oDb->lastId();
  819. return false;
  820. }
  821. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  822. {
  823. $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
  824. $oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
  825. $sSql = $oChecker->dbUpdate($val, $this->aParams['db'], $this->aInputs, $aValsToAdd, $aTrackTextFieldsChanges);
  826. if (!$sSql)
  827. return false;
  828. return BxDolDb::getInstance()->query($sSql);
  829. }
  830. function delete ($val)
  831. {
  832. $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
  833. $oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
  834. $sSql = $oChecker->dbDelete($val, $this->aParams['db'], $this->aInputs);
  835. if (!$sSql)
  836. return false;
  837. return BxDolDb::getInstance()->query($sSql);
  838. }
  839. function generateUri ()
  840. {
  841. $f = &$this->aParams['db'];
  842. $sUri = $this->getCleanValue ($f['uri_title']);
  843. return uriGenerate($sUri, $f['table'], $f['uri']);
  844. }
  845. function getCleanValue ($sName)
  846. {
  847. $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
  848. $oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
  849. $a = isset($this->aInputs[$sName]) ? $this->aInputs[$sName] : false;
  850. if ($a && isset($a['db']['pass']))
  851. return $oChecker->get ($a['name'], $a['db']['pass'], isset($a['db']['params']) && $a['db']['params'] ? $a['db']['params'] : array());
  852. else
  853. return $oChecker->get ($sName);
  854. }
  855. function isSubmitted ()
  856. {
  857. $aSubmitName = $this->aParams['db']['submit_name'];
  858. if(!is_array($aSubmitName))
  859. $aSubmitName = array($aSubmitName);
  860. foreach($aSubmitName as $sSubmitName)
  861. if(BxDolForm::getSubmittedValue($sSubmitName, $this->aFormAttrs['method'], $this->_aSpecificValues))
  862. return true;
  863. return false;
  864. }
  865. function getId ()
  866. {
  867. return $this->id;
  868. }
  869. function setId ($sId)
  870. {
  871. $this->id = $sId;
  872. $this->aFormAttrs['id'] = $sId;
  873. }
  874. function setName($sName)
  875. {
  876. $this->aFormAttrs['name'] = $sName;
  877. }
  878. function setValid ($isValid)
  879. {
  880. $this->_isValid = $isValid;
  881. }
  882. function isValid ()
  883. {
  884. return $this->_isValid;
  885. }
  886. function isSubmittedAndValid ()
  887. {
  888. return ($this->isSubmitted() && $this->isValid());
  889. }
  890. function getSpecificValues()
  891. {
  892. return $this->_aSpecificValues;
  893. }
  894. public static function getSubmittedValue($sKey, $sMethod, &$aSpecificValues = false)
  895. {
  896. $aData = array();
  897. if($sMethod == BX_DOL_FORM_METHOD_GET) {
  898. $aData = &$_GET;
  899. } else if($sMethod == BX_DOL_FORM_METHOD_POST) {
  900. $aData = &$_POST;
  901. } else if($sMethod == BX_DOL_FORM_METHOD_SPECIFIC) {
  902. $aData = &$aSpecificValues;
  903. }
  904. return isset($aData[$sKey]) ? $aData[$sKey] : false;
  905. }
  906. public static function setSubmittedValue($sKey, $mixedValue, $sMethod, &$aSpecificValues = false)
  907. {
  908. $aData = array();
  909. if($sMethod == BX_DOL_FORM_METHOD_GET) {
  910. $aData = &$_GET;
  911. } else if($sMethod == BX_DOL_FORM_METHOD_POST) {
  912. $aData = &$_POST;
  913. } else if($sMethod == BX_DOL_FORM_METHOD_SPECIFIC) {
  914. $aData = &$aSpecificValues;
  915. }
  916. return $aData[$sKey] = $mixedValue;
  917. }
  918. /**
  919. * Check if field exists
  920. * @param $sName field name
  921. * @param $bCheckAllFields true - check all fields; false - all fields except: password, captcha, hidden, file, button, image, reset, submit, block_header, input_set
  922. * @return true if field exists or false otherwise
  923. */
  924. public function isFieldExist($sName, $bCheckAllFields = false)
  925. {
  926. if (!isset($this->aInputs[$sName]))
  927. return false;
  928. return $bCheckAllFields ? true : !in_array($this->aInputs[$sName]['type'], array('password', 'captcha', 'hidden', 'button', 'reset', 'submit', 'block_header', 'input_set', 'location'));
  929. }
  930. // Static Methods related to CSRF Tocken
  931. public static function genCsrfToken($bReturn = false)
  932. {
  933. // TODO: remove
  934. }
  935. public static function getCsrfToken()
  936. {
  937. if (getParam('sys_security_form_token_enable') != 'on')
  938. return false;
  939. if (!($oKeys = BxDolKey::getInstance()))
  940. return false;
  941. return $oKeys->getNewKey (false, (int)getParam('sys_security_form_token_lifetime'));
  942. }
  943. public static function isCsrfTokenValid($s)
  944. {
  945. if (getParam('sys_security_form_token_enable') != 'on')
  946. return true;
  947. if (!($oKeys = BxDolKey::getInstance()))
  948. return true;
  949. if ($oKeys->isKeyExists($s)) {
  950. $oKeys->removeKey($s);
  951. return true;
  952. }
  953. return false;
  954. }
  955. function _initCheckerNestedForms ()
  956. {
  957. $isValid = true;
  958. // process nested forms
  959. foreach ($this->aInputs as $sKey => $aInput) {
  960. if (!isset($aInput['ghost_template']))
  961. continue;
  962. if (!(is_array($aInput['ghost_template']) && isset($aInput['ghost_template']['inputs'])) && !(is_object($aInput['ghost_template']) && $aInput['ghost_template'] instanceof BxDolFormNested))
  963. continue;
  964. $sName = $aInput['name'];
  965. $aIds = $this->getSubmittedValue($sName, $this->aFormAttrs['method']);
  966. if (!$aIds)
  967. continue;
  968. $aNestedForms = array ();
  969. foreach ($aIds as $i => $iFileId) {
  970. // create separate form instance for each file
  971. $oFormNested = false;
  972. if (is_object($aInput['ghost_template'])) {
  973. $oFormNested = clone($aInput['ghost_template']);
  974. } else {
  975. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  976. }
  977. if ($iFileId && 0 != $iFileId)
  978. $aNestedForms[$iFileId] = $oFormNested;
  979. else
  980. $aNestedForms[] = $oFormNested;
  981. // collect nested form values
  982. $aSpecificValues = array ();
  983. if (isset($this->aParams['db']['submit_name']))
  984. $aSpecificValues = array ($this->aParams['db']['submit_name'] => 1);
  985. foreach ($oFormNested->aInputs as $r) {
  986. $sName = str_replace('[]', '', $r['name']);
  987. $aValue = $this->getSubmittedValue($sName, $this->aFormAttrs['method']);
  988. $aSpecificValues[$sName] = $aValue[$i];
  989. }
  990. $oFormNested->initChecker(array(), $aSpecificValues);
  991. // if nested form in invalid - then the whole worm is failed
  992. if (!$oFormNested->isValid ())
  993. $isValid = false;
  994. }
  995. if ($aNestedForms)
  996. $this->aInputs[$sKey]['ghost_template'] = $aNestedForms;
  997. }
  998. return $isValid;
  999. }
  1000. /**
  1001. * Check if form field is visible.
  1002. * @param $aInput form field array
  1003. * @return boolean
  1004. */
  1005. static public function isVisible ($aInput)
  1006. {
  1007. return isset($aInput['visible_for_levels']) ? BxDolAcl::getInstance()->isMemberLevelInSet($aInput['visible_for_levels']) : true;
  1008. }
  1009. protected function _genMethodName ($s)
  1010. {
  1011. return bx_gen_method_name($s);
  1012. }
  1013. /**
  1014. * Add replace markers. Curently markers are replaced in action, form_attrs fields.
  1015. * @param $a array of markers as key => value
  1016. * @return true on success or false on error
  1017. */
  1018. public function addMarkers ($a)
  1019. {
  1020. if (empty($a) || !is_array($a))
  1021. return false;
  1022. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  1023. return true;
  1024. }
  1025. /**
  1026. * Replace provided markers in form array
  1027. * @param $a form description array
  1028. * @return array where markes are replaced with real values
  1029. */
  1030. protected function _replaceMarkers ($a)
  1031. {
  1032. return bx_replace_markers($a, $this->_aMarkers);
  1033. }
  1034. }
  1035. class BxDolFormChecker
  1036. {
  1037. protected $_oChecker;
  1038. protected $_sFormMethod;
  1039. protected $_aFormParams;
  1040. protected $_bFormCsrfChecking;
  1041. protected $_aSpecificValues;
  1042. protected $_aFieldsCheckForSpam;
  1043. function __construct ($sHelper = '')
  1044. {
  1045. $this->_sFormMethod = BX_DOL_FORM_METHOD_GET;
  1046. $this->_bFormCsrfChecking = true;
  1047. $sCheckerName = !empty($sHelper) ? $sHelper : 'BxDolFormCheckerHelper';
  1048. $this->_oChecker = new $sCheckerName();
  1049. }
  1050. function setFormMethod($sMethod, $aSpecificValues = array())
  1051. {
  1052. $this->_sFormMethod = $sMethod;
  1053. $this->_aSpecificValues = $aSpecificValues;
  1054. }
  1055. function setFormParams($aParams)
  1056. {
  1057. $this->_aFormParams = $aParams;
  1058. }
  1059. function setFieldsCheckForSpam($a)
  1060. {
  1061. $this->_aFieldsCheckForSpam = $a;
  1062. }
  1063. function enableFormCsrfChecking($bFormCsrfChecking)
  1064. {
  1065. $this->_bFormCsrfChecking = $bFormCsrfChecking;
  1066. }
  1067. // check function
  1068. function check (&$aInputs, $aValues = array())
  1069. {
  1070. $oChecker = $this->_oChecker;
  1071. $iErrors = 0;
  1072. $sSubmitName = false;
  1073. foreach ($aInputs as $k => $a) {
  1074. if (isset($a['visible_for_levels']) && !BxDolForm::isVisible($a))
  1075. continue;
  1076. if (empty($a['name']) || 'submit' == $a['type'] || 'reset' == $a['type'] || 'button' == $a['type'] || 'value' == $a['type']) {
  1077. if (isset($a['type']) && 'submit' == $a['type'])
  1078. $sSubmitName = $k;
  1079. continue;
  1080. }
  1081. if ('input_set' == $a['type'])
  1082. foreach ($a as $r)
  1083. if (isset($r['type']) && 'submit' == $r['type'])
  1084. $sSubmitName = $k;
  1085. $a['name'] = str_replace('[]', '', $a['name']);
  1086. $val = BxDolForm::getSubmittedValue($a['name'], $this->_sFormMethod, $this->_aSpecificValues);
  1087. if (isset(BxDolForm::$TYPES_FILE[$a['type']]))
  1088. $val = isset($_FILES[$a['name']]['name']) ? $_FILES[$a['name']]['name'] : '';
  1089. // check for unique
  1090. if(isset($aInputs[$k]['unique']) && $aInputs[$k]['unique'] && !empty($this->_aFormParams['db']['key']) && !empty($this->_aFormParams['db']['table'])) {
  1091. $sKey = $this->_aFormParams['db']['key'];
  1092. $sTable = $this->_aFormParams['db']['table'];
  1093. $sError = _t('_sys_form_err_not_unique_value');
  1094. if(!empty($aValues[$sKey])) {
  1095. $sValDb = BxDolFormQuery::fieldGetValue($sTable, $aInputs[$k]['name'], $sKey, $aValues[$sKey]);
  1096. if($val != $sValDb && !BxDolFormQuery::fieldCheckUnique($sTable, $aInputs[$k]['name'], $val)) {
  1097. ++$iErrors;
  1098. $aInputs[$k]['error'] = $sError;
  1099. }
  1100. }
  1101. else if(!BxDolFormQuery::fieldCheckUnique($sTable, $aInputs[$k]['name'], $val)) {
  1102. ++$iErrors;
  1103. $aInputs[$k]['error'] = $sError;
  1104. }
  1105. }
  1106. // check for links in text fields
  1107. $sCheckerFunc = isset($a['checker']['func']) ? strtolower($a['checker']['func']) : '';
  1108. if (
  1109. !isset(BxDolForm::$FUNC_SKIP_DOMAIN_CHECK[$sCheckerFunc]) &&
  1110. (!isset($a['skip_domain_check']) || false == $a['skip_domain_check']) &&
  1111. isset(BxDolForm::$TYPES_TEXT[$a['type']]) &&
  1112. bx_is_url_in_content($val) &&
  1113. !isAdmin())
  1114. {
  1115. $aCheck = checkActionModule(bx_get_logged_profile_id(), 'post links', 'system');
  1116. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED) {
  1117. ++$iErrors;
  1118. $aInputs[$k]['error'] = $aCheck[CHECK_ACTION_MESSAGE];
  1119. }
  1120. else {
  1121. checkActionModule(bx_get_logged_profile_id(), 'post links', 'system', true);
  1122. }
  1123. }
  1124. if (!isset ($a['checker'])) {
  1125. if (isset(BxDolForm::$TYPES_CHECKBOX[$a['type']]))
  1126. $aInputs[$k]['checked'] = (isset($aInputs[$k]['value']) && $aInputs[$k]['value'] == $val);
  1127. elseif (!isset(BxDolForm::$TYPES_FILE[$a['type']]) && !isset(BxDolForm::$TYPES_SKIP[$a['type']]))
  1128. $aInputs[$k]['value'] = bx_process_input($val);
  1129. continue;
  1130. }
  1131. // special check for location field
  1132. if ('location' == $aInputs[$k]['type'] && $val && !BxDolForm::getSubmittedValue($a['name'] . '_country', $this->_sFormMethod, $this->_aSpecificValues)) {
  1133. $aInputs[$k]['value'] = '';
  1134. $val = '';
  1135. }
  1136. $sCheckFunction = array($oChecker, 'check' . bx_gen_method_name($a['checker']['func']));
  1137. if (is_callable($sCheckFunction))
  1138. $bool = call_user_func_array ($sCheckFunction, !empty($a['checker']['params']) ? array_merge(array($val), $a['checker']['params']) : array ($val));
  1139. else
  1140. $bool = true;
  1141. if (is_string($bool)) {
  1142. ++$iErrors;
  1143. $aInputs[$k]['error'] = $bool;
  1144. } elseif (!$bool) {
  1145. ++$iErrors;
  1146. if (isset($a['checker']['error']))
  1147. $aInputs[$k]['error'] = $a['checker']['error'];
  1148. }
  1149. if (isset(BxDolForm::$TYPES_CHECKBOX[$a['type']]))
  1150. $aInputs[$k]['checked'] = ($aInputs[$k]['value'] == $val);
  1151. elseif (!isset(BxDolForm::$TYPES_FILE[$a['type']]) && !isset(BxDolForm::$TYPES_SKIP[$a['type']]))
  1152. $aInputs[$k]['value'] = bx_process_input($val);
  1153. }
  1154. // check CSRF token if it's needed.
  1155. if (getParam('sys_security_form_token_enable') == 'on' && $this->_bFormCsrfChecking === true) {
  1156. $mixedCsrfTokenUsr = BxDolForm::getSubmittedValue('csrf_token', $this->_sFormMethod, $this->_aSpecificValues);
  1157. unset($aInputs['csrf_token']);
  1158. if ($mixedCsrfTokenUsr === false || !BxDolForm::isCsrfTokenValid($mixedCsrfTokenUsr)) {
  1159. $aInputs[$sSubmitName]['error'] = _t('_sys_txt_form_submission_error_csrf_expired');
  1160. return false;
  1161. }
  1162. }
  1163. // check for spam
  1164. if (!$iErrors) {
  1165. foreach ($aInputs as $k => $a) {
  1166. if (!isset($a['name']))
  1167. continue;
  1168. $a['name'] = str_replace('[]', '', $a['name']);
  1169. if ($a['type'] != 'textarea' && $a['type'] != 'text' && !in_array($a['name'], $this->_aFieldsCheckForSpam))
  1170. continue;
  1171. $val = BxDolForm::getSubmittedValue($a['name'], $this->_sFormMethod, $this->_aSpecificValues);
  1172. if (!$val)
  1173. continue;
  1174. if (!$oChecker->checkIsSpam($val, $a['type'])){
  1175. BxDolForm::setSubmittedValue($a['name'], $val, $this->_sFormMethod, $this->_aSpecificValues);
  1176. continue;
  1177. }
  1178. ++$iErrors;
  1179. $sErr = _t('_sys_spam_detected');
  1180. if (BxDolRequest::serviceExists('bx_contact', 'get_contact_page_url') && ($sUrl = BxDolService::call('bx_contact', 'get_contact_page_url')))
  1181. $sErr = _t('_sys_spam_detected_contact', $sUrl);
  1182. $aInputs[$k]['error'] = $sErr;
  1183. }
  1184. }
  1185. // add error message near submit button
  1186. if ($iErrors && $sSubmitName)
  1187. $aInputs[$sSubmitName]['error'] = _t('_sys_txt_form_submission_error');
  1188. return $iErrors ? false : true;
  1189. }
  1190. // get clean value from GET/POST
  1191. function get ($sName, $sPass = 'Xss', $aParams = array())
  1192. {
  1193. if (!$sPass)
  1194. $sPass = 'Xss';
  1195. $this->_oChecker;
  1196. $val = BxDolForm::getSubmittedValue($sName, $this->_sFormMethod, $this->_aSpecificValues);
  1197. return call_user_func_array (array($this->_oChecker, 'pass'.ucfirst($sPass)), $aParams ? array_merge(array($val), $aParams) : array ($val));
  1198. }
  1199. // db functions
  1200. function serializeDbValues (&$aInputs, &$aValsToAdd, &$aTrackTextFieldsChanges = null, $aUnset = null)
  1201. {
  1202. $aValsToUpdate = array();
  1203. if (null !== $aTrackTextFieldsChanges && isset($aTrackTextFieldsChanges['data']))
  1204. $aTrackTextFieldsChanges['changed_fields'] = array();
  1205. // get values from form description array
  1206. foreach ($aInputs as $k => $a) {
  1207. if (!isset ($a['db']) || !BxDolForm::isVisible($a)) continue;
  1208. $sKey = str_replace('[]', '', $a['name']);
  1209. $valClean = $this->get ($sKey, $a['db']['pass'], !empty($a['db']['params']) ? $a['db']['params'] : array());
  1210. $aValsToUpdate[$sKey] = $valClean;
  1211. $aInputs[$k]['db']['value'] = $valClean;
  1212. if (null !== $aTrackTextFieldsChanges && isset(BxDolForm::$TYPES_TEXT[$aInputs[$k]['type']]) && isset($aTrackTextFieldsChanges['data'][$a['name']]) && $aTrackTextFieldsChanges['data'][$a['name']] != $valClean)
  1213. $aTrackTextFieldsChanges['changed_fields'][] = $a['name'];
  1214. }
  1215. // get values which are provided manually
  1216. foreach ($aValsToAdd as $k => $val) {
  1217. $aValsToUpdate[$k] = $val;
  1218. }
  1219. // don't update id field for update statement
  1220. if ($aUnset) {
  1221. foreach ($aUnset as $sKey) {
  1222. if (isset($aValsToUpdate[$sKey]))
  1223. unset($aValsToUpdate[$sKey]);
  1224. }
  1225. }
  1226. // build SQL query part
  1227. return BxDolDb::getInstance()->arrayToSQL($aValsToUpdate);
  1228. }
  1229. function dbInsert (&$aDb, &$aInputs, $aValsToAdd = array(), $isIgnore = false)
  1230. {
  1231. if (!$aDb['table'])
  1232. return '';
  1233. $sFields = $this->serializeDbValues ($aInputs, $aValsToAdd);
  1234. if (!$sFields)
  1235. return '';
  1236. return "INSERT " . ($isIgnore ? 'IGNORE' : '') . " INTO `{$aDb['table']}` SET $sFields";
  1237. }
  1238. function dbUpdate ($val, &$aDb, &$aInputs, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  1239. {
  1240. if (!$aDb['table'] || !$aDb['key'])
  1241. return '';
  1242. $oDb = BxDolDb::getInstance();
  1243. if (null !== $aTrackTextFieldsChanges && !isset($aTrackTextFieldsChanges['data'])) {
  1244. // get row values to compare old and new values
  1245. $sQuery = $oDb->prepare("SELECT * FROM `{$aDb['table']}` WHERE `{$aDb['key']}` = ?", $val);
  1246. $aTrackTextFieldsChanges['data'] = $oDb->getRow($sQuery);
  1247. }
  1248. $sFields = $this->serializeDbValues ($aInputs, $aValsToAdd, $aTrackTextFieldsChanges, array($aDb['key']));
  1249. if (!$sFields)
  1250. return '';
  1251. return $oDb->prepare("UPDATE `{$aDb['table']}` SET $sFields WHERE `{$aDb['key']}` = ?", $val);
  1252. }
  1253. function dbDelete ($val, &$aDb, &$aInputs)
  1254. {
  1255. if (!$aDb['table'] || !$aDb['key'])
  1256. return '';
  1257. $oDb = BxDolDb::getInstance();
  1258. return $oDb->prepare("DELETE FROM `{$aDb['table']}` WHERE `{$aDb['key']}` = ?", $val);
  1259. }
  1260. function fillWithValues (&$aInputs, &$aValues)
  1261. {
  1262. foreach ($aInputs as $k => $a) {
  1263. if (!isset($aValues[$k]) || isset(BxDolForm::$TYPES_SKIP[$aInputs[$k]['type']]))
  1264. continue;
  1265. if (isset(BxDolForm::$TYPES_CHECKBOX[$aInputs[$k]['type']])) {
  1266. $aInputs[$k]['checked'] = isset($aInputs[$k]['value']) ? ($aInputs[$k]['value'] == $aValues[$k]) : false;
  1267. } else {
  1268. $sMethod = 'display' . (isset($a['db']['pass']) ? ucfirst($a['db']['pass']) : 'Undefined');
  1269. if (method_exists($this->_oChecker, $sMethod))
  1270. $aInputs[$k]['value'] = call_user_func_array (array($this->_oChecker, $sMethod), !empty($a['db']['params']) ? array_merge(array($aValues[$k]), $a['db']['params']) : array ($aValues[$k]));
  1271. else
  1272. $aInputs[$k]['value'] = $aValues[$k];
  1273. }
  1274. }
  1275. }
  1276. }
  1277. class BxDolFormCheckerHelper
  1278. {
  1279. // check functions - check values for limits or patterns
  1280. static public function checkLength ($s, $iLenMin, $iLenMax)
  1281. {
  1282. if (is_array($s)) {
  1283. foreach ($s as $k => $v) {
  1284. $iLen = get_mb_len ($v);
  1285. if ($iLen < $iLenMin || $iLen > $iLenMax)
  1286. return false;
  1287. }
  1288. return true;
  1289. }
  1290. $iLen = get_mb_len ($s);
  1291. return $iLen >= $iLenMin && $iLen <= $iLenMax ? true : false;
  1292. }
  1293. static public function checkDate ($s)
  1294. {
  1295. return self::checkPreg ($s, '#^\d+\-\d+\-\d+$#');
  1296. }
  1297. static public function checkDateRange ($s, $iMin, $iMax, $bRequired = false)
  1298. {
  1299. if (!$bRequired && (empty($s) || '0000-00-00' == $s))
  1300. return true;
  1301. if (self::checkPreg ($s, '#^\d+\-\d+\-\d+$#')){
  1302. $dDate = new DateTime($s);
  1303. $dDateMax = new DateTime("-" . $iMin . " years");
  1304. $dDateMin = new DateTime("-" . $iMax . " years");
  1305. if ($dDate > $dDateMin && $dDate < $dDateMax){
  1306. return true;
  1307. }
  1308. }
  1309. return false;
  1310. }
  1311. static public function checkDateTime ($s)
  1312. {
  1313. // remove unnecessary opera's input value;
  1314. $s = str_replace('T', ' ', $s);
  1315. $s = str_replace('Z', ':00', $s);
  1316. return self::checkPreg ($s, '#^\d+\-\d+\-\d+[\sT]{1}\d+:\d+[:\d+]$#');
  1317. }
  1318. static public function checkPreg ($s, $r)
  1319. {
  1320. if (is_array($s)) {
  1321. foreach ($s as $k => $v)
  1322. if (!preg_match($r, $v))
  1323. return false;
  1324. return true;
  1325. }
  1326. return preg_match($r, $s) ? true : false;
  1327. }
  1328. static public function checkAvail ($s)
  1329. {
  1330. if (is_array($s)) {
  1331. return !self::_isEmptyArray($s);
  1332. }
  1333. return $s ? true : false;
  1334. }
  1335. static public function checkEmailOrEmpty($s)
  1336. {
  1337. if (empty($s))
  1338. return true;
  1339. return self::checkEmail($s);
  1340. }
  1341. static public function checkEmail($s)
  1342. {
  1343. if (false === strpos($s, '@')) // simple check
  1344. return false;
  1345. if (!preg_match("/^[\pL\pNd]/u", $s)) // must start with letter or number
  1346. return false;
  1347. if (!preg_match("/@[\pL\pNd\.\-]+$/u", $s)) // validate domain
  1348. return false;
  1349. $s = str_replace(array('@', '.', '-', '+', '_'), '', $s); // allowed symbols
  1350. if (preg_match("/[^\pL^\pNd]/u", $s)) // check for undesirable chars
  1351. return false;
  1352. return true;
  1353. // return self::checkPreg ($s, "/(([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?/");
  1354. }
  1355. static public function checkCaptcha($s)
  1356. {
  1357. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1358. if (!$oCaptcha)
  1359. return true;
  1360. return $oCaptcha->check ();
  1361. }
  1362. static public function checkIsSpam(&$val, $sType = 'textarea')
  1363. {
  1364. $bSpam = false;
  1365. bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => &$val, 'where' => 'form', 'type' => $sType));
  1366. return $bSpam;
  1367. }
  1368. // pass functions, prepare values to insert to database
  1369. static public function passInt ($s)
  1370. {
  1371. if (is_array($s)) {
  1372. $a = array ();
  1373. foreach ($s as $k => $v) {
  1374. $a[$k] = (int)trim($v);
  1375. }
  1376. return $a;
  1377. }
  1378. return (int)$s;
  1379. }
  1380. static public function passFloat ($s)
  1381. {
  1382. if (is_array($s)) {
  1383. $a = array ();
  1384. foreach ($s as $k => $v) {
  1385. $a[$k] = (float)$v;
  1386. }
  1387. return $a;
  1388. }
  1389. return (float)$s;
  1390. }
  1391. static public function passDate ($s)
  1392. {
  1393. if (is_array($s)) {
  1394. $a = array ();
  1395. foreach ($s as $k => $v) {
  1396. $a[$k] = self::_passDate ($v, BX_DATA_DATE);
  1397. }
  1398. return $a;
  1399. }
  1400. return self::_passDate ($s, BX_DATA_DATE);
  1401. }
  1402. static public function passDateTs ($s)
  1403. {
  1404. if (is_array($s)) {
  1405. $a = array ();
  1406. foreach ($s as $k => $v) {
  1407. $a[$k] = self::_passDate ($v, BX_DATA_DATE_TS);
  1408. }
  1409. return $a;
  1410. }
  1411. return self::_passDate ($s, BX_DATA_DATE_TS);
  1412. }
  1413. static public function passDateUtc ($s)
  1414. {
  1415. if (is_array($s)) {
  1416. $a = array ();
  1417. foreach ($s as $k => $v) {
  1418. $a[$k] = self::_passDate ($v, BX_DATA_DATE_TS_UTC);
  1419. }
  1420. return $a;
  1421. }
  1422. return self::_passDate ($s, BX_DATA_DATE_TS_UTC);
  1423. }
  1424. static public function passDateTime ($s)
  1425. {
  1426. if (is_array($s)) {
  1427. $a = array ();
  1428. foreach ($s as $k => $v) {
  1429. $a[$k] = self::_passDate ($v, BX_DATA_DATETIME);
  1430. }
  1431. return $a;
  1432. }
  1433. return self::_passDate ($s, BX_DATA_DATETIME);
  1434. }
  1435. static public function passDateTimeTs ($s)
  1436. {
  1437. if (is_array($s)) {
  1438. $a = array ();
  1439. foreach ($s as $k => $v) {
  1440. $a[$k] = self::_passDate ($v, BX_DATA_DATETIME_TS);
  1441. }
  1442. return $a;
  1443. }
  1444. return self::_passDate ($s, BX_DATA_DATETIME_TS);
  1445. }
  1446. static public function passDateTimeUtc ($s)
  1447. {
  1448. if (is_array($s)) {
  1449. $a = array ();
  1450. foreach ($s as $k => $v) {
  1451. $a[$k] = self::_passDate ($v, BX_DATA_DATETIME_TS_UTC);
  1452. }
  1453. return $a;
  1454. }
  1455. return self::_passDate ($s, BX_DATA_DATETIME_TS_UTC);
  1456. }
  1457. static public function _passDate ($s, $iFormat)
  1458. {
  1459. $iRet = bx_process_input ($s, $iFormat);
  1460. if (false === $iRet)
  1461. return 0;
  1462. return $iRet;
  1463. }
  1464. static public function passXss ($s)
  1465. {
  1466. if (is_array($s)) {
  1467. $a = array ();
  1468. foreach ($s as $k => $v) {
  1469. $a[$k] = bx_process_input ($v, BX_DATA_TEXT); // "strip tags" option was here in 7.0
  1470. }
  1471. return $a;
  1472. }
  1473. return bx_process_input ($s, BX_DATA_TEXT); // "strip tags" option was here in 7.0
  1474. }
  1475. static public function passXssMultiline ($s)
  1476. {
  1477. if (is_array($s)) {
  1478. $a = array ();
  1479. foreach ($s as $k => $v) {
  1480. $a[$k] = bx_process_input ($v, BX_DATA_TEXT_MULTILINE);
  1481. }
  1482. return $a;
  1483. }
  1484. return bx_process_input ($s, BX_DATA_TEXT_MULTILINE);
  1485. }
  1486. static public function passXssHtml ($s)
  1487. {
  1488. if (is_array($s)) {
  1489. $a = array ();
  1490. foreach ($s as $k => $v) {
  1491. $a[$k] = bx_process_input ($v, BX_DATA_HTML);
  1492. }
  1493. return $a;
  1494. }
  1495. return bx_process_input ($s, BX_DATA_HTML);
  1496. }
  1497. static public function passAll ($s)
  1498. {
  1499. if (is_array($s)) {
  1500. $a = array ();
  1501. foreach ($s as $k => $v) {
  1502. $a[$k] = bx_process_input ($v);
  1503. }
  1504. return $a;
  1505. }
  1506. return bx_process_input ($s);
  1507. }
  1508. static public function passPreg ($s, $r)
  1509. {
  1510. if (is_array($s)) {
  1511. $a = array ();
  1512. foreach ($s as $k => $v) {
  1513. $a[$k] = self::_passPreg ($v, $r);
  1514. }
  1515. return $a;
  1516. }
  1517. return self::_passPreg($s, $r);
  1518. }
  1519. static public function _passPreg ($s, $r)
  1520. {
  1521. if (preg_match ($r, $s, $m)) {
  1522. return $m[1];
  1523. }
  1524. return '';
  1525. }
  1526. static public function passBoolean ($s)
  1527. {
  1528. if (is_array($s)) {
  1529. $a = array ();
  1530. foreach ($s as $k => $v) {
  1531. $a[$k] = $v == 'on' ? true : false;
  1532. }
  1533. return $a;
  1534. }
  1535. return $s == 'on' ? true : false;
  1536. }
  1537. static public function passSet ($s)
  1538. {
  1539. if (is_array($s)) {
  1540. $i = 0;
  1541. foreach ($s as $v)
  1542. $i |= pow (2, $v - 1);
  1543. return $i;
  1544. }
  1545. return (int)$s;
  1546. }
  1547. // display functions, prepare values to output to the screen
  1548. static public function displayDate ($i)
  1549. {
  1550. return bx_process_output ($i, BX_DATA_DATE);
  1551. }
  1552. static public function displayDateTs ($i)
  1553. {
  1554. return bx_process_output ($i, BX_DATA_DATE_TS);
  1555. }
  1556. static public function displayDateUtc ($i)
  1557. {
  1558. return bx_process_output ($i, BX_DATA_DATE_TS_UTC);
  1559. }
  1560. static public function displayDateTime ($i)
  1561. {
  1562. return bx_process_output ($i, BX_DATA_DATETIME);
  1563. }
  1564. static public function displayDateTimeTs ($i)
  1565. {
  1566. return bx_process_output ($i, BX_DATA_DATETIME_TS);
  1567. }
  1568. static public function displayDateTimeUtc ($i)
  1569. {
  1570. return bx_process_output ($i, BX_DATA_DATETIME_TS_UTC);
  1571. }
  1572. static public function displaySet ($i)
  1573. {
  1574. $i = (int)$i;
  1575. $bit = 1;
  1576. $bitpos = 1;
  1577. $a = array();
  1578. while ($bit < BX_DOL_INT_MAX && $bit > 0) {
  1579. if ($bit & $i)
  1580. $a[] = $bitpos;
  1581. $bit <<= 1;
  1582. $bitpos++;
  1583. }
  1584. return $a;
  1585. }
  1586. // for internal usage only
  1587. static public function _isEmptyArray ($a)
  1588. {
  1589. return bx_is_empty_array($a);
  1590. }
  1591. static public function _isFullArray ($a)
  1592. {
  1593. return bx_is_full_array($a);
  1594. }
  1595. }
  1596. /** @} */