Profile-Modify.php 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490
  1. <?php
  2. /**
  3. * This file has the primary job of showing and editing people's profiles.
  4. * It also allows the user to change some of their or another's preferences,
  5. * and such things
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2011 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * This defines every profile field known to man.
  20. *
  21. * @param bool $force_reload = false
  22. */
  23. function loadProfileFields($force_reload = false)
  24. {
  25. global $context, $profile_fields, $txt, $scripturl, $modSettings, $user_info, $old_profile, $smcFunc, $cur_profile, $language;
  26. // Don't load this twice!
  27. if (!empty($profile_fields) && !$force_reload)
  28. return;
  29. /* This horrific array defines all the profile fields in the whole world!
  30. In general each "field" has one array - the key of which is the database column name associated with said field. Each item
  31. can have the following attributes:
  32. string $type: The type of field this is - valid types are:
  33. - callback: This is a field which has its own callback mechanism for templating.
  34. - check: A simple checkbox.
  35. - hidden: This doesn't have any visual aspects but may have some validity.
  36. - password: A password box.
  37. - select: A select box.
  38. - text: A string of some description.
  39. string $label: The label for this item - default will be $txt[$key] if this isn't set.
  40. string $subtext: The subtext (Small label) for this item.
  41. int $size: Optional size for a text area.
  42. array $input_attr: An array of text strings to be added to the input box for this item.
  43. string $value: The value of the item. If not set $cur_profile[$key] is assumed.
  44. string $permission: Permission required for this item (Excluded _any/_own subfix which is applied automatically).
  45. function $input_validate: A runtime function which validates the element before going to the database. It is passed
  46. the relevant $_POST element if it exists and should be treated like a reference.
  47. Return types:
  48. - true: Element can be stored.
  49. - false: Skip this element.
  50. - a text string: An error occured - this is the error message.
  51. function $preload: A function that is used to load data required for this element to be displayed. Must return
  52. true to be displayed at all.
  53. string $cast_type: If set casts the element to a certain type. Valid types (bool, int, float).
  54. string $save_key: If the index of this element isn't the database column name it can be overriden
  55. with this string.
  56. bool $is_dummy: If set then nothing is acted upon for this element.
  57. bool $enabled: A test to determine whether this is even available - if not is unset.
  58. string $link_with: Key which links this field to an overall set.
  59. Note that all elements that have a custom input_validate must ensure they set the value of $cur_profile correct to enable
  60. the changes to be displayed correctly on submit of the form.
  61. */
  62. $profile_fields = array(
  63. 'aim' => array(
  64. 'type' => 'text',
  65. 'label' => $txt['aim'],
  66. 'subtext' => $txt['your_aim'],
  67. 'size' => 24,
  68. 'value' => strtr(empty($cur_profile['aim']) ? '' : $cur_profile['aim'], '+', ' '),
  69. 'permission' => 'profile_extra',
  70. 'input_validate' => create_function('&$value', '
  71. $value = strtr($value, \' \', \'+\');
  72. return true;
  73. '),
  74. ),
  75. 'avatar_choice' => array(
  76. 'type' => 'callback',
  77. 'callback_func' => 'avatar_select',
  78. // This handles the permissions too.
  79. 'preload' => 'profileLoadAvatarData',
  80. 'input_validate' => 'profileSaveAvatarData',
  81. 'save_key' => 'avatar',
  82. ),
  83. 'bday1' => array(
  84. 'type' => 'callback',
  85. 'callback_func' => 'birthdate',
  86. 'permission' => 'profile_extra',
  87. 'preload' => create_function('', '
  88. global $cur_profile, $context;
  89. // Split up the birthdate....
  90. list ($uyear, $umonth, $uday) = explode(\'-\', empty($cur_profile[\'birthdate\']) || $cur_profile[\'birthdate\'] == \'0001-01-01\' ? \'0000-00-00\' : $cur_profile[\'birthdate\']);
  91. $context[\'member\'][\'birth_date\'] = array(
  92. \'year\' => $uyear == \'0004\' ? \'0000\' : $uyear,
  93. \'month\' => $umonth,
  94. \'day\' => $uday,
  95. );
  96. return true;
  97. '),
  98. 'input_validate' => create_function('&$value', '
  99. global $profile_vars, $cur_profile;
  100. if (isset($_POST[\'bday2\'], $_POST[\'bday3\']) && $value > 0 && $_POST[\'bday2\'] > 0)
  101. {
  102. // Set to blank?
  103. if ((int) $_POST[\'bday3\'] == 1 && (int) $_POST[\'bday2\'] == 1 && (int) $value == 1)
  104. $value = \'0001-01-01\';
  105. else
  106. $value = checkdate($value, $_POST[\'bday2\'], $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\']) ? sprintf(\'%04d-%02d-%02d\', $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\'], $_POST[\'bday1\'], $_POST[\'bday2\']) : \'0001-01-01\';
  107. }
  108. else
  109. $value = \'0001-01-01\';
  110. $profile_vars[\'birthdate\'] = $value;
  111. $cur_profile[\'birthdate\'] = $value;
  112. return false;
  113. '),
  114. ),
  115. // Setting the birthdate the old style way?
  116. 'birthdate' => array(
  117. 'type' => 'hidden',
  118. 'permission' => 'profile_extra',
  119. 'input_validate' => create_function('&$value', '
  120. global $cur_profile;
  121. // @todo Should we check for this year and tell them they made a mistake :P? (based on coppa at least?)
  122. if (preg_match(\'/(\d{4})[\-\., ](\d{2})[\-\., ](\d{2})/\', $value, $dates) === 1)
  123. {
  124. $value = checkdate($dates[2], $dates[3], $dates[1] < 4 ? 4 : $dates[1]) ? sprintf(\'%04d-%02d-%02d\', $dates[1] < 4 ? 4 : $dates[1], $dates[2], $dates[3]) : \'0001-01-01\';
  125. return true;
  126. }
  127. else
  128. {
  129. $value = empty($cur_profile[\'birthdate\']) ? \'0001-01-01\' : $cur_profile[\'birthdate\'];
  130. return false;
  131. }
  132. '),
  133. ),
  134. 'date_registered' => array(
  135. 'type' => 'text',
  136. 'value' => empty($cur_profile['date_registered']) ? $txt['not_applicable'] : strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
  137. 'label' => $txt['date_registered'],
  138. 'log_change' => true,
  139. 'permission' => 'moderate_forum',
  140. 'input_validate' => create_function('&$value', '
  141. global $txt, $user_info, $modSettings, $cur_profile, $context;
  142. // Bad date! Go try again - please?
  143. if (($value = strtotime($value)) === -1)
  144. {
  145. $value = $cur_profile[\'date_registered\'];
  146. return $txt[\'invalid_registration\'] . \' \' . strftime(\'%d %b %Y \' . (strpos($user_info[\'time_format\'], \'%H\') !== false ? \'%I:%M:%S %p\' : \'%H:%M:%S\'), forum_time(false));
  147. }
  148. // As long as it doesn\'t equal "N/A"...
  149. elseif ($value != $txt[\'not_applicable\'] && $value != strtotime(strftime(\'%Y-%m-%d\', $cur_profile[\'date_registered\'] + ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600)))
  150. $value = $value - ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600;
  151. else
  152. $value = $cur_profile[\'date_registered\'];
  153. return true;
  154. '),
  155. ),
  156. 'email_address' => array(
  157. 'type' => 'text',
  158. 'label' => $txt['email'],
  159. 'subtext' => $txt['valid_email'],
  160. 'log_change' => true,
  161. 'permission' => 'profile_identity',
  162. 'input_validate' => create_function('&$value', '
  163. global $context, $old_profile, $context, $profile_vars, $sourcedir, $modSettings;
  164. if (strtolower($value) == strtolower($old_profile[\'email_address\']))
  165. return false;
  166. $isValid = profileValidateEmail($value, $context[\'id_member\']);
  167. // Do they need to revalidate? If so schedule the function!
  168. if ($isValid === true && !empty($modSettings[\'send_validation_onChange\']) && !allowedTo(\'moderate_forum\'))
  169. {
  170. require_once($sourcedir . \'/Subs-Members.php\');
  171. $profile_vars[\'validation_code\'] = generateValidationCode();
  172. $profile_vars[\'is_activated\'] = 2;
  173. $context[\'profile_execute_on_save\'][] = \'profileSendActivation\';
  174. unset($context[\'profile_execute_on_save\'][\'reload_user\']);
  175. }
  176. return $isValid;
  177. '),
  178. ),
  179. 'gender' => array(
  180. 'type' => 'select',
  181. 'cast_type' => 'int',
  182. 'options' => 'return array(0 => \'\', 1 => $txt[\'male\'], 2 => $txt[\'female\']);',
  183. 'label' => $txt['gender'],
  184. 'permission' => 'profile_extra',
  185. ),
  186. 'hide_email' => array(
  187. 'type' => 'check',
  188. 'value' => empty($cur_profile['hide_email']) ? true : false,
  189. 'label' => $txt['allow_user_email'],
  190. 'permission' => 'profile_identity',
  191. 'input_validate' => create_function('&$value', '
  192. $value = $value == 0 ? 1 : 0;
  193. return true;
  194. '),
  195. ),
  196. 'icq' => array(
  197. 'type' => 'text',
  198. 'label' => $txt['icq'],
  199. 'subtext' => $txt['your_icq'],
  200. 'size' => 24,
  201. 'permission' => 'profile_extra',
  202. // Need to make sure ICQ doesn't equal 0.
  203. 'input_validate' => create_function('&$value', '
  204. if (empty($value))
  205. $value = \'\';
  206. else
  207. $value = (int) $value;
  208. return true;
  209. '),
  210. ),
  211. // Selecting group membership is a complicated one so we treat it separate!
  212. 'id_group' => array(
  213. 'type' => 'callback',
  214. 'callback_func' => 'group_manage',
  215. 'permission' => 'manage_membergroups',
  216. 'preload' => 'profileLoadGroups',
  217. 'log_change' => true,
  218. 'input_validate' => 'profileSaveGroups',
  219. ),
  220. 'id_theme' => array(
  221. 'type' => 'callback',
  222. 'callback_func' => 'theme_pick',
  223. 'permission' => 'profile_extra',
  224. 'enabled' => $modSettings['theme_allow'] || allowedTo('admin_forum'),
  225. 'preload' => create_function('', '
  226. global $smcFunc, $context, $cur_profile, $txt;
  227. $request = $smcFunc[\'db_query\'](\'\', \'
  228. SELECT value
  229. FROM {db_prefix}themes
  230. WHERE id_theme = {int:id_theme}
  231. AND variable = {string:variable}
  232. LIMIT 1\', array(
  233. \'id_theme\' => $cur_profile[\'id_theme\'],
  234. \'variable\' => \'name\',
  235. )
  236. );
  237. list ($name) = $smcFunc[\'db_fetch_row\']($request);
  238. $smcFunc[\'db_free_result\']($request);
  239. $context[\'member\'][\'theme\'] = array(
  240. \'id\' => $cur_profile[\'id_theme\'],
  241. \'name\' => empty($cur_profile[\'id_theme\']) ? $txt[\'theme_forum_default\'] : $name
  242. );
  243. return true;
  244. '),
  245. 'input_validate' => create_function('&$value', '
  246. $value = (int) $value;
  247. return true;
  248. '),
  249. ),
  250. 'karma_good' => array(
  251. 'type' => 'callback',
  252. 'callback_func' => 'karma_modify',
  253. 'permission' => 'admin_forum',
  254. // Set karma_bad too!
  255. 'input_validate' => create_function('&$value', '
  256. global $profile_vars, $cur_profile;
  257. $value = (int) $value;
  258. if (isset($_POST[\'karma_bad\']))
  259. {
  260. $profile_vars[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  261. $cur_profile[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  262. }
  263. return true;
  264. '),
  265. 'preload' => create_function('', '
  266. global $context, $cur_profile;
  267. $context[\'member\'][\'karma\'][\'good\'] = $cur_profile[\'karma_good\'];
  268. $context[\'member\'][\'karma\'][\'bad\'] = $cur_profile[\'karma_bad\'];
  269. return true;
  270. '),
  271. 'enabled' => !empty($modSettings['karmaMode']),
  272. ),
  273. 'lngfile' => array(
  274. 'type' => 'select',
  275. 'options' => 'return $context[\'profile_languages\'];',
  276. 'label' => $txt['preferred_language'],
  277. 'permission' => 'profile_identity',
  278. 'preload' => 'profileLoadLanguages',
  279. 'enabled' => !empty($modSettings['userLanguage']),
  280. 'value' => empty($cur_profile['lngfile']) ? $language : $cur_profile['lngfile'],
  281. 'input_validate' => create_function('&$value', '
  282. global $context, $cur_profile;
  283. // Load the languages.
  284. profileLoadLanguages();
  285. if (isset($context[\'profile_languages\'][$value]))
  286. {
  287. if ($context[\'user\'][\'is_owner\'] && empty($context[\'password_auth_failed\']))
  288. $_SESSION[\'language\'] = $value;
  289. return true;
  290. }
  291. else
  292. {
  293. $value = $cur_profile[\'lngfile\'];
  294. return false;
  295. }
  296. '),
  297. ),
  298. 'location' => array(
  299. 'type' => 'text',
  300. 'label' => $txt['location'],
  301. 'log_change' => true,
  302. 'size' => 50,
  303. 'permission' => 'profile_extra',
  304. ),
  305. // The username is not always editable - so adjust it as such.
  306. 'member_name' => array(
  307. 'type' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? 'text' : 'label',
  308. 'label' => $txt['username'],
  309. 'subtext' => allowedTo('admin_forum') && !isset($_GET['changeusername']) ? '(<a href="' . $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=account;changeusername" style="font-style: italic;">' . $txt['username_change'] . '</a>)' : '',
  310. 'log_change' => true,
  311. 'permission' => 'profile_identity',
  312. 'prehtml' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? '<div class="alert">' . $txt['username_warning'] . '</div>' : '',
  313. 'input_validate' => create_function('&$value', '
  314. global $sourcedir, $context, $user_info, $cur_profile;
  315. if (allowedTo(\'admin_forum\'))
  316. {
  317. // We\'ll need this...
  318. require_once($sourcedir . \'/Subs-Auth.php\');
  319. // Maybe they are trying to change their password as well?
  320. $resetPassword = true;
  321. if (isset($_POST[\'passwrd1\']) && $_POST[\'passwrd1\'] != \'\' && isset($_POST[\'passwrd2\']) && $_POST[\'passwrd1\'] == $_POST[\'passwrd2\'] && validatePassword($_POST[\'passwrd1\'], $value, array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\'])) === null)
  322. $resetPassword = false;
  323. // Do the reset... this will send them an email too.
  324. if ($resetPassword)
  325. resetPassword($context[\'id_member\'], $value);
  326. elseif ($value !== null)
  327. {
  328. validateUsername($context[\'id_member\'], $value);
  329. updateMemberData($context[\'id_member\'], array(\'member_name\' => $value));
  330. }
  331. }
  332. return false;
  333. '),
  334. ),
  335. 'msn' => array(
  336. 'type' => 'text',
  337. 'label' => $txt['msn'],
  338. 'subtext' => $txt['msn_email_address'],
  339. 'size' => 24,
  340. 'permission' => 'profile_extra',
  341. 'input_validate' => create_function('&$value', '
  342. global $cur_profile;
  343. // Make sure the msn one is an email address, not something like \'none\' :P.
  344. if ($value != \'\' && preg_match(\'~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\\\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~\', $value) == 0)
  345. {
  346. $value = $cur_profile[\'msn\'];
  347. return false;
  348. }
  349. return true;
  350. '),
  351. ),
  352. 'passwrd1' => array(
  353. 'type' => 'password',
  354. 'label' => $txt['choose_pass'],
  355. 'subtext' => $txt['password_strength'],
  356. 'size' => 20,
  357. 'value' => '',
  358. 'enabled' => empty($cur_profile['openid_uri']),
  359. 'permission' => 'profile_identity',
  360. 'save_key' => 'passwd',
  361. // Note this will only work if passwrd2 also exists!
  362. 'input_validate' => create_function('&$value', '
  363. global $sourcedir, $user_info, $smcFunc, $cur_profile;
  364. // If we didn\'t try it then ignore it!
  365. if ($value == \'\')
  366. return false;
  367. // Do the two entries for the password even match?
  368. if (!isset($_POST[\'passwrd2\']) || $value != $_POST[\'passwrd2\'])
  369. return \'bad_new_password\';
  370. // Let\'s get the validation function into play...
  371. require_once($sourcedir . \'/Subs-Auth.php\');
  372. $passwordErrors = validatePassword($value, $cur_profile[\'member_name\'], array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\']));
  373. // Were there errors?
  374. if ($passwordErrors != null)
  375. return \'password_\' . $passwordErrors;
  376. // Set up the new password variable... ready for storage.
  377. $value = sha1(strtolower($cur_profile[\'member_name\']) . un_htmlspecialchars($value));
  378. return true;
  379. '),
  380. ),
  381. 'passwrd2' => array(
  382. 'type' => 'password',
  383. 'label' => $txt['verify_pass'],
  384. 'enabled' => empty($cur_profile['openid_uri']),
  385. 'size' => 20,
  386. 'value' => '',
  387. 'permission' => 'profile_identity',
  388. 'is_dummy' => true,
  389. ),
  390. 'personal_text' => array(
  391. 'type' => 'text',
  392. 'label' => $txt['personal_text'],
  393. 'log_change' => true,
  394. 'input_attr' => array('maxlength="50"'),
  395. 'size' => 50,
  396. 'permission' => 'profile_extra',
  397. ),
  398. // This does ALL the pm settings
  399. 'pm_prefs' => array(
  400. 'type' => 'callback',
  401. 'callback_func' => 'pm_settings',
  402. 'permission' => 'pm_read',
  403. 'preload' => create_function('', '
  404. global $context, $cur_profile;
  405. $context[\'display_mode\'] = $cur_profile[\'pm_prefs\'] & 3;
  406. $context[\'send_email\'] = $cur_profile[\'pm_email_notify\'];
  407. $context[\'receive_from\'] = !empty($cur_profile[\'pm_receive_from\']) ? $cur_profile[\'pm_receive_from\'] : 0;
  408. return true;
  409. '),
  410. 'input_validate' => create_function('&$value', '
  411. global $cur_profile, $profile_vars;
  412. // Simple validate and apply the two "sub settings"
  413. $value = max(min($value, 2), 0);
  414. $cur_profile[\'pm_email_notify\'] = $profile_vars[\'pm_email_notify\'] = max(min((int) $_POST[\'pm_email_notify\'], 2), 0);
  415. $cur_profile[\'pm_receive_from\'] = $profile_vars[\'pm_receive_from\'] = max(min((int) $_POST[\'pm_receive_from\'], 4), 0);
  416. return true;
  417. '),
  418. ),
  419. 'posts' => array(
  420. 'type' => 'int',
  421. 'label' => $txt['profile_posts'],
  422. 'log_change' => true,
  423. 'size' => 7,
  424. 'permission' => 'moderate_forum',
  425. 'input_validate' => create_function('&$value', '
  426. $value = $value != \'\' ? strtr($value, array(\',\' => \'\', \'.\' => \'\', \' \' => \'\')) : 0;
  427. return true;
  428. '),
  429. ),
  430. 'real_name' => array(
  431. 'type' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum') ? 'text' : 'label',
  432. 'label' => $txt['name'],
  433. 'subtext' => $txt['display_name_desc'],
  434. 'log_change' => true,
  435. 'input_attr' => array('maxlength="60"'),
  436. 'permission' => 'profile_identity',
  437. 'enabled' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum'),
  438. 'input_validate' => create_function('&$value', '
  439. global $context, $smcFunc, $sourcedir, $cur_profile;
  440. $value = trim(preg_replace(\'~[\s]~\' . ($context[\'utf8\'] ? \'u\' : \'\'), \' \', $value));
  441. if (trim($value) == \'\')
  442. return \'no_name\';
  443. elseif ($smcFunc[\'strlen\']($value) > 60)
  444. return \'name_too_long\';
  445. elseif ($cur_profile[\'real_name\'] != $value)
  446. {
  447. require_once($sourcedir . \'/Subs-Members.php\');
  448. if (isReservedName($value, $context[\'id_member\']))
  449. return \'name_taken\';
  450. }
  451. return true;
  452. '),
  453. ),
  454. 'secret_question' => array(
  455. 'type' => 'text',
  456. 'label' => $txt['secret_question'],
  457. 'subtext' => $txt['secret_desc'],
  458. 'size' => 50,
  459. 'permission' => 'profile_identity',
  460. ),
  461. 'secret_answer' => array(
  462. 'type' => 'text',
  463. 'label' => $txt['secret_answer'],
  464. 'subtext' => $txt['secret_desc2'],
  465. 'size' => 20,
  466. 'postinput' => '<span class="smalltext" style="margin-left: 4ex;"><a href="' . $scripturl . '?action=helpadmin;help=secret_why_blank" onclick="return reqWin(this.href);">' . $txt['secret_why_blank'] . '</a></span>',
  467. 'value' => '',
  468. 'permission' => 'profile_identity',
  469. 'input_validate' => create_function('&$value', '
  470. $value = $value != \'\' ? md5($value) : \'\';
  471. return true;
  472. '),
  473. ),
  474. 'signature' => array(
  475. 'type' => 'callback',
  476. 'callback_func' => 'signature_modify',
  477. 'permission' => 'profile_extra',
  478. 'enabled' => substr($modSettings['signature_settings'], 0, 1) == 1,
  479. 'preload' => 'profileLoadSignatureData',
  480. 'input_validate' => 'profileValidateSignature',
  481. ),
  482. 'show_online' => array(
  483. 'type' => 'check',
  484. 'label' => $txt['show_online'],
  485. 'permission' => 'profile_identity',
  486. 'enabled' => !empty($modSettings['allow_hideOnline']) || allowedTo('moderate_forum'),
  487. ),
  488. 'smiley_set' => array(
  489. 'type' => 'callback',
  490. 'callback_func' => 'smiley_pick',
  491. 'enabled' => !empty($modSettings['smiley_sets_enable']),
  492. 'permission' => 'profile_extra',
  493. 'preload' => create_function('', '
  494. global $modSettings, $context, $txt, $cur_profile;
  495. $context[\'member\'][\'smiley_set\'][\'id\'] = empty($cur_profile[\'smiley_set\']) ? \'\' : $cur_profile[\'smiley_set\'];
  496. $context[\'smiley_sets\'] = explode(\',\', \'none,,\' . $modSettings[\'smiley_sets_known\']);
  497. $set_names = explode("\n", $txt[\'smileys_none\'] . "\n" . $txt[\'smileys_forum_board_default\'] . "\n" . $modSettings[\'smiley_sets_names\']);
  498. foreach ($context[\'smiley_sets\'] as $i => $set)
  499. {
  500. $context[\'smiley_sets\'][$i] = array(
  501. \'id\' => htmlspecialchars($set),
  502. \'name\' => htmlspecialchars($set_names[$i]),
  503. \'selected\' => $set == $context[\'member\'][\'smiley_set\'][\'id\']
  504. );
  505. if ($context[\'smiley_sets\'][$i][\'selected\'])
  506. $context[\'member\'][\'smiley_set\'][\'name\'] = $set_names[$i];
  507. }
  508. return true;
  509. '),
  510. 'input_validate' => create_function('&$value', '
  511. global $modSettings;
  512. $smiley_sets = explode(\',\', $modSettings[\'smiley_sets_known\']);
  513. if (!in_array($value, $smiley_sets) && $value != \'none\')
  514. $value = \'\';
  515. return true;
  516. '),
  517. ),
  518. // Pretty much a dummy entry - it populates all the theme settings.
  519. 'theme_settings' => array(
  520. 'type' => 'callback',
  521. 'callback_func' => 'theme_settings',
  522. 'permission' => 'profile_extra',
  523. 'is_dummy' => true,
  524. 'preload' => create_function('', '
  525. global $context, $user_info;
  526. loadLanguage(\'Settings\');
  527. $context[\'allow_no_censored\'] = false;
  528. if ($user_info[\'is_admin\'] || $context[\'user\'][\'is_owner\'])
  529. $context[\'allow_no_censored\'] = allowedTo(\'disable_censor\');
  530. return true;
  531. '),
  532. ),
  533. 'time_format' => array(
  534. 'type' => 'callback',
  535. 'callback_func' => 'timeformat_modify',
  536. 'permission' => 'profile_extra',
  537. 'preload' => create_function('', '
  538. global $context, $user_info, $txt, $cur_profile, $modSettings;
  539. $context[\'easy_timeformats\'] = array(
  540. array(\'format\' => \'\', \'title\' => $txt[\'timeformat_default\']),
  541. array(\'format\' => \'%B %d, %Y, %I:%M:%S %p\', \'title\' => $txt[\'timeformat_easy1\']),
  542. array(\'format\' => \'%B %d, %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy2\']),
  543. array(\'format\' => \'%Y-%m-%d, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy3\']),
  544. array(\'format\' => \'%d %B %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy4\']),
  545. array(\'format\' => \'%d-%m-%Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy5\'])
  546. );
  547. $context[\'member\'][\'time_format\'] = $cur_profile[\'time_format\'];
  548. $context[\'current_forum_time\'] = timeformat(time() - $user_info[\'time_offset\'] * 3600, false);
  549. $context[\'current_forum_time_js\'] = strftime(\'%Y,\' . ((int) strftime(\'%m\', time() + $modSettings[\'time_offset\'] * 3600) - 1) . \',%d,%H,%M,%S\', time() + $modSettings[\'time_offset\'] * 3600);
  550. $context[\'current_forum_time_hour\'] = (int) strftime(\'%H\', forum_time(false));
  551. return true;
  552. '),
  553. ),
  554. 'time_offset' => array(
  555. 'type' => 'callback',
  556. 'callback_func' => 'timeoffset_modify',
  557. 'permission' => 'profile_extra',
  558. 'preload' => create_function('', '
  559. global $context, $cur_profile;
  560. $context[\'member\'][\'time_offset\'] = $cur_profile[\'time_offset\'];
  561. return true;
  562. '),
  563. 'input_validate' => create_function('&$value', '
  564. // Validate the time_offset...
  565. $value = (float) strtr($value, \',\', \'.\');
  566. if ($value < -23.5 || $value > 23.5)
  567. return \'bad_offset\';
  568. return true;
  569. '),
  570. ),
  571. 'usertitle' => array(
  572. 'type' => 'text',
  573. 'label' => $txt['custom_title'],
  574. 'log_change' => true,
  575. 'size' => 50,
  576. 'permission' => 'profile_title',
  577. 'enabled' => !empty($modSettings['titlesEnable']),
  578. ),
  579. 'website_title' => array(
  580. 'type' => 'text',
  581. 'label' => $txt['website_title'],
  582. 'subtext' => $txt['include_website_url'],
  583. 'size' => 50,
  584. 'permission' => 'profile_extra',
  585. 'link_with' => 'website',
  586. ),
  587. 'website_url' => array(
  588. 'type' => 'text',
  589. 'label' => $txt['website_url'],
  590. 'subtext' => $txt['complete_url'],
  591. 'size' => 50,
  592. 'permission' => 'profile_extra',
  593. // Fix the URL...
  594. 'input_validate' => create_function('&$value', '
  595. if (strlen(trim($value)) > 0 && strpos($value, \'://\') === false)
  596. $value = \'http://\' . $value;
  597. if (strlen($value) < 8 || (substr($value, 0, 7) !== \'http://\' && substr($value, 0, 8) !== \'https://\'))
  598. $value = \'\';
  599. return true;
  600. '),
  601. 'link_with' => 'website',
  602. ),
  603. 'yim' => array(
  604. 'type' => 'text',
  605. 'label' => $txt['yim'],
  606. 'subtext' => $txt['your_yim'],
  607. 'size' => 24,
  608. 'input_attr' => array('maxlength="32"'),
  609. 'permission' => 'profile_extra',
  610. ),
  611. );
  612. $disabled_fields = !empty($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
  613. // For each of the above let's take out the bits which don't apply - to save memory and security!
  614. foreach ($profile_fields as $key => $field)
  615. {
  616. // Do we have permission to do this?
  617. if (isset($field['permission']) && !allowedTo(($context['user']['is_owner'] ? array($field['permission'] . '_own', $field['permission'] . '_any') : $field['permission'] . '_any')) && !allowedTo($field['permission']))
  618. unset($profile_fields[$key]);
  619. // Is it enabled?
  620. if (isset($field['enabled']) && !$field['enabled'])
  621. unset($profile_fields[$key]);
  622. // Is it specifically disabled?
  623. if (in_array($key, $disabled_fields) || (isset($field['link_with']) && in_array($field['link_with'], $disabled_fields)))
  624. unset($profile_fields[$key]);
  625. }
  626. }
  627. /**
  628. * Setup the context for a page load!
  629. *
  630. * @param array $fields
  631. */
  632. function setupProfileContext($fields)
  633. {
  634. global $profile_fields, $context, $cur_profile, $smcFunc, $txt;
  635. // Make sure we have this!
  636. loadProfileFields(true);
  637. // First check for any linked sets.
  638. foreach ($profile_fields as $key => $field)
  639. if (isset($field['link_with']) && in_array($field['link_with'], $fields))
  640. $fields[] = $key;
  641. // Some default bits.
  642. $context['profile_prehtml'] = '';
  643. $context['profile_posthtml'] = '';
  644. $context['profile_javascript'] = '';
  645. $context['profile_onsubmit_javascript'] = '';
  646. $i = 0;
  647. $last_type = '';
  648. foreach ($fields as $key => $field)
  649. {
  650. if (isset($profile_fields[$field]))
  651. {
  652. // Shortcut.
  653. $cur_field = &$profile_fields[$field];
  654. // Does it have a preload and does that preload succeed?
  655. if (isset($cur_field['preload']) && !$cur_field['preload']())
  656. continue;
  657. // If this is anything but complex we need to do more cleaning!
  658. if ($cur_field['type'] != 'callback' && $cur_field['type'] != 'hidden')
  659. {
  660. if (!isset($cur_field['label']))
  661. $cur_field['label'] = isset($txt[$field]) ? $txt[$field] : $field;
  662. // Everything has a value!
  663. if (!isset($cur_field['value']))
  664. {
  665. $cur_field['value'] = isset($cur_profile[$field]) ? $cur_profile[$field] : '';
  666. }
  667. // Any input attributes?
  668. $cur_field['input_attr'] = !empty($cur_field['input_attr']) ? implode(',', $cur_field['input_attr']) : '';
  669. }
  670. // Was there an error with this field on posting?
  671. if (isset($context['profile_errors'][$field]))
  672. $cur_field['is_error'] = true;
  673. // Any javascript stuff?
  674. if (!empty($cur_field['js_submit']))
  675. $context['profile_onsubmit_javascript'] .= $cur_field['js_submit'];
  676. if (!empty($cur_field['js']))
  677. $context['profile_javascript'] .= $cur_field['js'];
  678. // Any template stuff?
  679. if (!empty($cur_field['prehtml']))
  680. $context['profile_prehtml'] .= $cur_field['prehtml'];
  681. if (!empty($cur_field['posthtml']))
  682. $context['profile_posthtml'] .= $cur_field['posthtml'];
  683. // Finally put it into context?
  684. if ($cur_field['type'] != 'hidden')
  685. {
  686. $last_type = $cur_field['type'];
  687. $context['profile_fields'][$field] = &$profile_fields[$field];
  688. }
  689. }
  690. // Bodge in a line break - without doing two in a row ;)
  691. elseif ($field == 'hr' && $last_type != 'hr' && $last_type != '')
  692. {
  693. $last_type = 'hr';
  694. $context['profile_fields'][$i++]['type'] = 'hr';
  695. }
  696. }
  697. // Free up some memory.
  698. unset($profile_fields);
  699. }
  700. /**
  701. * Save the profile changes.
  702. */
  703. function saveProfileFields()
  704. {
  705. global $profile_fields, $profile_vars, $context, $old_profile, $post_errors, $sourcedir, $modSettings, $cur_profile, $smcFunc;
  706. // Load them up.
  707. loadProfileFields();
  708. // This makes things easier...
  709. $old_profile = $cur_profile;
  710. // This allows variables to call activities when they save - by default just to reload their settings
  711. $context['profile_execute_on_save'] = array();
  712. if ($context['user']['is_owner'])
  713. $context['profile_execute_on_save']['reload_user'] = 'profileReloadUser';
  714. // Assume we log nothing.
  715. $context['log_changes'] = array();
  716. // Cycle through the profile fields working out what to do!
  717. foreach ($profile_fields as $key => $field)
  718. {
  719. if (!isset($_POST[$key]) || !empty($field['is_dummy']))
  720. continue;
  721. // What gets updated?
  722. $db_key = isset($field['save_key']) ? $field['save_key'] : $key;
  723. // Right - we have something that is enabled, we can act upon and has a value posted to it. Does it have a validation function?
  724. if (isset($field['input_validate']))
  725. {
  726. $is_valid = $field['input_validate']($_POST[$key]);
  727. // An error occured - set it as such!
  728. if ($is_valid !== true)
  729. {
  730. // Is this an actual error?
  731. if ($is_valid !== false)
  732. {
  733. $post_errors[$key] = $is_valid;
  734. $profile_fields[$key]['is_error'] = $is_valid;
  735. }
  736. // Retain the old value.
  737. $cur_profile[$key] = $_POST[$key];
  738. continue;
  739. }
  740. }
  741. // Are we doing a cast?
  742. $field['cast_type'] = empty($field['cast_type']) ? $field['type'] : $field['cast_type'];
  743. // Finally, clean up certain types.
  744. if ($field['cast_type'] == 'int')
  745. $_POST[$key] = (int) $_POST[$key];
  746. elseif ($field['cast_type'] == 'float')
  747. $_POST[$key] = (float) $_POST[$key];
  748. elseif ($field['cast_type'] == 'check')
  749. $_POST[$key] = !empty($_POST[$key]) ? 1 : 0;
  750. // If we got here we're doing OK.
  751. if ($field['type'] != 'hidden' && (!isset($old_profile[$key]) || $_POST[$key] != $old_profile[$key]))
  752. {
  753. // Set the save variable.
  754. $profile_vars[$db_key] = $_POST[$key];
  755. // And update the user profile.
  756. $cur_profile[$key] = $_POST[$key];
  757. // Are we logging it?
  758. if (!empty($field['log_change']) && isset($old_profile[$key]))
  759. $context['log_changes'][$key] = array(
  760. 'previous' => $old_profile[$key],
  761. 'new' => $_POST[$key],
  762. );
  763. }
  764. // Logging group changes are a bit different...
  765. if ($key == 'id_group' && $field['log_change'])
  766. {
  767. profileLoadGroups();
  768. // Any changes to primary group?
  769. if ($_POST['id_group'] != $old_profile['id_group'])
  770. {
  771. $context['log_changes']['id_group'] = array(
  772. 'previous' => !empty($old_profile[$key]) && isset($context['member_groups'][$old_profile[$key]]) ? $context['member_groups'][$old_profile[$key]]['name'] : '',
  773. 'new' => !empty($_POST[$key]) && isset($context['member_groups'][$_POST[$key]]) ? $context['member_groups'][$_POST[$key]]['name'] : '',
  774. );
  775. }
  776. // Prepare additional groups for comparison.
  777. $additional_groups = array(
  778. 'previous' => !empty($old_profile['additional_groups']) ? explode(',', $old_profile['additional_groups']) : array(),
  779. 'new' => !empty($_POST['additional_groups']) ? array_diff($_POST['additional_groups'], array(0)) : array(),
  780. );
  781. sort($additional_groups['previous']);
  782. sort($additional_groups['new']);
  783. // What about additional groups?
  784. if ($additional_groups['previous'] != $additional_groups['new'])
  785. {
  786. foreach ($additional_groups as $type => $groups)
  787. {
  788. foreach ($groups as $id => $group)
  789. {
  790. if (isset($context['member_groups'][$group]))
  791. $additional_groups[$type][$id] = $context['member_groups'][$group]['name'];
  792. else
  793. unset($additional_groups[$type][$id]);
  794. }
  795. $additional_groups[$type] = implode(', ', $additional_groups[$type]);
  796. }
  797. $context['log_changes']['additional_groups'] = $additional_groups;
  798. }
  799. }
  800. }
  801. // @todo Temporary
  802. if ($context['user']['is_owner'])
  803. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  804. else
  805. $changeOther = allowedTo('profile_extra_any');
  806. if ($changeOther && empty($post_errors))
  807. {
  808. makeThemeChanges($context['id_member'], isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  809. if (!empty($_REQUEST['sa']))
  810. makeCustomFieldChanges($context['id_member'], $_REQUEST['sa'], false);
  811. }
  812. // Free memory!
  813. unset($profile_fields);
  814. }
  815. /**
  816. * Save the profile changes
  817. *
  818. * @param array &$profile_variables
  819. * @param array &$post_errors
  820. * @param int $memID id_member
  821. */
  822. function saveProfileChanges(&$profile_vars, &$post_errors, $memID)
  823. {
  824. global $user_info, $txt, $modSettings, $user_profile;
  825. global $context, $settings, $sourcedir;
  826. global $smcFunc;
  827. // These make life easier....
  828. $old_profile = &$user_profile[$memID];
  829. // Permissions...
  830. if ($context['user']['is_owner'])
  831. {
  832. $changeIdentity = allowedTo(array('profile_identity_any', 'profile_identity_own'));
  833. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  834. }
  835. else
  836. {
  837. $changeIdentity = allowedTo('profile_identity_any');
  838. $changeOther = allowedTo('profile_extra_any');
  839. }
  840. // Arrays of all the changes - makes things easier.
  841. $profile_bools = array(
  842. 'notify_announcements', 'notify_send_body',
  843. );
  844. $profile_ints = array(
  845. 'notify_regularity',
  846. 'notify_types',
  847. );
  848. $profile_floats = array(
  849. );
  850. $profile_strings = array(
  851. 'buddy_list',
  852. 'ignore_boards',
  853. );
  854. if (isset($_POST['sa']) && $_POST['sa'] == 'ignoreboards' && empty($_POST['ignore_brd']))
  855. $_POST['ignore_brd'] = array();
  856. unset($_POST['ignore_boards']); // Whatever it is set to is a dirty fithy thing. Kinda like our minds.
  857. if (isset($_POST['ignore_brd']))
  858. {
  859. if (!is_array($_POST['ignore_brd']))
  860. $_POST['ignore_brd'] = array ($_POST['ignore_brd']);
  861. foreach ($_POST['ignore_brd'] as $k => $d)
  862. {
  863. $d = (int) $d;
  864. if ($d != 0)
  865. $_POST['ignore_brd'][$k] = $d;
  866. else
  867. unset($_POST['ignore_brd'][$k]);
  868. }
  869. $_POST['ignore_boards'] = implode(',', $_POST['ignore_brd']);
  870. unset($_POST['ignore_brd']);
  871. }
  872. // Here's where we sort out all the 'other' values...
  873. if ($changeOther)
  874. {
  875. makeThemeChanges($memID, isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  876. //makeAvatarChanges($memID, $post_errors);
  877. makeNotificationChanges($memID);
  878. if (!empty($_REQUEST['sa']))
  879. makeCustomFieldChanges($memID, $_REQUEST['sa'], false);
  880. foreach ($profile_bools as $var)
  881. if (isset($_POST[$var]))
  882. $profile_vars[$var] = empty($_POST[$var]) ? '0' : '1';
  883. foreach ($profile_ints as $var)
  884. if (isset($_POST[$var]))
  885. $profile_vars[$var] = $_POST[$var] != '' ? (int) $_POST[$var] : '';
  886. foreach ($profile_floats as $var)
  887. if (isset($_POST[$var]))
  888. $profile_vars[$var] = (float) $_POST[$var];
  889. foreach ($profile_strings as $var)
  890. if (isset($_POST[$var]))
  891. $profile_vars[$var] = $_POST[$var];
  892. }
  893. }
  894. /**
  895. * Make any theme changes that are sent with the profile.
  896. *
  897. * @param int $memID
  898. * @param int $id_theme
  899. */
  900. function makeThemeChanges($memID, $id_theme)
  901. {
  902. global $modSettings, $smcFunc, $context;
  903. $reservedVars = array(
  904. 'actual_theme_url',
  905. 'actual_images_url',
  906. 'base_theme_dir',
  907. 'base_theme_url',
  908. 'default_images_url',
  909. 'default_theme_dir',
  910. 'default_theme_url',
  911. 'default_template',
  912. 'images_url',
  913. 'number_recent_posts',
  914. 'smiley_sets_default',
  915. 'theme_dir',
  916. 'theme_id',
  917. 'theme_layers',
  918. 'theme_templates',
  919. 'theme_url',
  920. );
  921. // Can't change reserved vars.
  922. if ((isset($_POST['options']) && array_intersect($_POST['options'], $reservedVars) != array()) || (isset($_POST['default_options']) && array_intersect($_POST['default_options'], $reservedVars) != array()))
  923. fatal_lang_error('no_access', false);
  924. // Don't allow any overriding of custom fields with default or non-default options.
  925. $request = $smcFunc['db_query']('', '
  926. SELECT col_name
  927. FROM {db_prefix}custom_fields
  928. WHERE active = {int:is_active}',
  929. array(
  930. 'is_active' => 1,
  931. )
  932. );
  933. $custom_fields = array();
  934. while ($row = $smcFunc['db_fetch_assoc']($request))
  935. $custom_fields[] = $row['col_name'];
  936. $smcFunc['db_free_result']($request);
  937. // These are the theme changes...
  938. $themeSetArray = array();
  939. if (isset($_POST['options']) && is_array($_POST['options']))
  940. {
  941. foreach ($_POST['options'] as $opt => $val)
  942. {
  943. if (in_array($opt, $custom_fields))
  944. continue;
  945. // These need to be controlled.
  946. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  947. $val = max(0, min($val, 50));
  948. // We don't set this per theme anymore.
  949. elseif ($opt == 'allow_no_censored')
  950. continue;
  951. $themeSetArray[] = array($memID, $id_theme, $opt, is_array($val) ? implode(',', $val) : $val);
  952. }
  953. }
  954. $erase_options = array();
  955. if (isset($_POST['default_options']) && is_array($_POST['default_options']))
  956. foreach ($_POST['default_options'] as $opt => $val)
  957. {
  958. if (in_array($opt, $custom_fields))
  959. continue;
  960. // These need to be controlled.
  961. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  962. $val = max(0, min($val, 50));
  963. // Only let admins and owners change the censor.
  964. elseif ($opt == 'allow_no_censored' && !$user_info['is_admin'] && !$context['user']['is_owner'])
  965. continue;
  966. $themeSetArray[] = array($memID, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  967. $erase_options[] = $opt;
  968. }
  969. // If themeSetArray isn't still empty, send it to the database.
  970. if (empty($context['password_auth_failed']))
  971. {
  972. if (!empty($themeSetArray))
  973. {
  974. $smcFunc['db_insert']('replace',
  975. '{db_prefix}themes',
  976. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  977. $themeSetArray,
  978. array('id_member', 'id_theme', 'variable')
  979. );
  980. }
  981. if (!empty($erase_options))
  982. {
  983. $smcFunc['db_query']('', '
  984. DELETE FROM {db_prefix}themes
  985. WHERE id_theme != {int:id_theme}
  986. AND variable IN ({array_string:erase_variables})
  987. AND id_member = {int:id_member}',
  988. array(
  989. 'id_theme' => 1,
  990. 'id_member' => $memID,
  991. 'erase_variables' => $erase_options
  992. )
  993. );
  994. }
  995. $themes = explode(',', $modSettings['knownThemes']);
  996. foreach ($themes as $t)
  997. cache_put_data('theme_settings-' . $t . ':' . $memID, null, 60);
  998. }
  999. }
  1000. /**
  1001. * Make any notification changes that need to be made.
  1002. *
  1003. * @param int $memID id_member
  1004. */
  1005. function makeNotificationChanges($memID)
  1006. {
  1007. global $smcFunc;
  1008. // Update the boards they are being notified on.
  1009. if (isset($_POST['edit_notify_boards']) && !empty($_POST['notify_boards']))
  1010. {
  1011. // Make sure only integers are deleted.
  1012. foreach ($_POST['notify_boards'] as $index => $id)
  1013. $_POST['notify_boards'][$index] = (int) $id;
  1014. // id_board = 0 is reserved for topic notifications.
  1015. $_POST['notify_boards'] = array_diff($_POST['notify_boards'], array(0));
  1016. $smcFunc['db_query']('', '
  1017. DELETE FROM {db_prefix}log_notify
  1018. WHERE id_board IN ({array_int:board_list})
  1019. AND id_member = {int:selected_member}',
  1020. array(
  1021. 'board_list' => $_POST['notify_boards'],
  1022. 'selected_member' => $memID,
  1023. )
  1024. );
  1025. }
  1026. // We are editing topic notifications......
  1027. elseif (isset($_POST['edit_notify_topics']) && !empty($_POST['notify_topics']))
  1028. {
  1029. foreach ($_POST['notify_topics'] as $index => $id)
  1030. $_POST['notify_topics'][$index] = (int) $id;
  1031. // Make sure there are no zeros left.
  1032. $_POST['notify_topics'] = array_diff($_POST['notify_topics'], array(0));
  1033. $smcFunc['db_query']('', '
  1034. DELETE FROM {db_prefix}log_notify
  1035. WHERE id_topic IN ({array_int:topic_list})
  1036. AND id_member = {int:selected_member}',
  1037. array(
  1038. 'topic_list' => $_POST['notify_topics'],
  1039. 'selected_member' => $memID,
  1040. )
  1041. );
  1042. }
  1043. }
  1044. /**
  1045. * Save any changes to the custom profile fields
  1046. *
  1047. * @param int $memID
  1048. * @param string $area
  1049. * @param bool $sanitize = true
  1050. */
  1051. function makeCustomFieldChanges($memID, $area, $sanitize = true)
  1052. {
  1053. global $context, $smcFunc, $user_profile, $user_info, $modSettings;
  1054. if ($sanitize && isset($_POST['customfield']))
  1055. $_POST['customfield'] = htmlspecialchars__recursive($_POST['customfield']);
  1056. $where = $area == 'register' ? 'show_reg != 0' : 'show_profile = {string:area}';
  1057. // Load the fields we are saving too - make sure we save valid data (etc).
  1058. $request = $smcFunc['db_query']('', '
  1059. SELECT col_name, field_name, field_desc, field_type, field_length, field_options, default_value, show_reg, mask, private
  1060. FROM {db_prefix}custom_fields
  1061. WHERE ' . $where . '
  1062. AND active = {int:is_active}',
  1063. array(
  1064. 'is_active' => 1,
  1065. 'area' => $area,
  1066. )
  1067. );
  1068. $changes = array();
  1069. $log_changes = array();
  1070. while ($row = $smcFunc['db_fetch_assoc']($request))
  1071. {
  1072. /* This means don't save if:
  1073. - The user is NOT an admin.
  1074. - The data is not freely viewable and editable by users.
  1075. - The data is not invisible to users but editable by the owner (or if it is the user is not the owner)
  1076. - The area isn't registration, and if it is that the field is not suppossed to be shown there.
  1077. */
  1078. if ($row['private'] != 0 && !allowedTo('admin_forum') && ($memID != $user_info['id'] || $row['private'] != 2) && ($area != 'register' || $row['show_reg'] == 0))
  1079. continue;
  1080. // Validate the user data.
  1081. if ($row['field_type'] == 'check')
  1082. $value = isset($_POST['customfield'][$row['col_name']]) ? 1 : 0;
  1083. elseif ($row['field_type'] == 'select' || $row['field_type'] == 'radio')
  1084. {
  1085. $value = $row['default_value'];
  1086. foreach (explode(',', $row['field_options']) as $k => $v)
  1087. if (isset($_POST['customfield'][$row['col_name']]) && $_POST['customfield'][$row['col_name']] == $k)
  1088. $value = $v;
  1089. }
  1090. // Otherwise some form of text!
  1091. else
  1092. {
  1093. $value = isset($_POST['customfield'][$row['col_name']]) ? $_POST['customfield'][$row['col_name']] : '';
  1094. if ($row['field_length'])
  1095. $value = $smcFunc['substr']($value, 0, $row['field_length']);
  1096. // Any masks?
  1097. if ($row['field_type'] === 'text' && !empty($row['mask']) && $row['mask'] != 'none')
  1098. {
  1099. // @todo We never error on this - just ignore it at the moment...
  1100. if ($row['mask'] === 'email' && (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $value) === 0 || strlen($value) > 255))
  1101. $value = '';
  1102. elseif ($row['mask'] === 'number')
  1103. {
  1104. $value = (int) $value;
  1105. }
  1106. elseif (strpos($row['mask'], 'regex') === 0 && preg_match(substr($row['mask'], 5), $value) === 0)
  1107. $value = '';
  1108. }
  1109. }
  1110. // Did it change?
  1111. if (!isset($user_profile[$memID]['options'][$row['col_name']]) || $user_profile[$memID]['options'][$row['col_name']] != $value)
  1112. {
  1113. $log_changes[] = array(
  1114. 'action' => 'customfield_' . $row['col_name'],
  1115. 'id_log' => 2,
  1116. 'log_time' => time(),
  1117. 'id_member' => $memID,
  1118. 'ip' => $user_info['ip'],
  1119. 'extra' => serialize(array('previous' => !empty($user_profile[$memID]['options'][$row['col_name']]) ? $user_profile[$memID]['options'][$row['col_name']] : '', 'new' => $value, 'applicator' => $user_info['id'])),
  1120. );
  1121. $changes[] = array(1, $row['col_name'], $value, $memID);
  1122. $user_profile[$memID]['options'][$row['col_name']] = $value;
  1123. }
  1124. }
  1125. $smcFunc['db_free_result']($request);
  1126. // Make those changes!
  1127. if (!empty($changes) && empty($context['password_auth_failed']))
  1128. {
  1129. $smcFunc['db_insert']('replace',
  1130. '{db_prefix}themes',
  1131. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534', 'id_member' => 'int'),
  1132. $changes,
  1133. array('id_theme', 'variable', 'id_member')
  1134. );
  1135. if (!empty($log_changes) && !empty($modSettings['modlog_enabled']))
  1136. $smcFunc['db_insert']('',
  1137. '{db_prefix}log_actions',
  1138. array(
  1139. 'action' => 'string', 'id_log' => 'int', 'log_time' => 'int', 'id_member' => 'int', 'ip' => 'string-16',
  1140. 'extra' => 'string-65534',
  1141. ),
  1142. $log_changes,
  1143. array('id_action')
  1144. );
  1145. }
  1146. }
  1147. /**
  1148. * Show all the users buddies, as well as a add/delete interface.
  1149. *
  1150. * @param int $memID id_member
  1151. */
  1152. function editBuddyIgnoreLists($memID)
  1153. {
  1154. global $sourcedir, $context, $txt, $scripturl, $modSettings, $user_profile;
  1155. // Do a quick check to ensure people aren't getting here illegally!
  1156. if (!$context['user']['is_owner'] || empty($modSettings['enable_buddylist']))
  1157. fatal_lang_error('no_access', false);
  1158. // Can we email the user direct?
  1159. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  1160. $subActions = array(
  1161. 'buddies' => array('editBuddies', $txt['editBuddies']),
  1162. 'ignore' => array('editIgnoreList', $txt['editIgnoreList']),
  1163. );
  1164. $context['list_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : 'buddies';
  1165. // Create the tabs for the template.
  1166. $context[$context['profile_menu_name']]['tab_data'] = array(
  1167. 'title' => $txt['editBuddyIgnoreLists'],
  1168. 'description' => $txt['buddy_ignore_desc'],
  1169. 'icon' => 'profile_sm.gif',
  1170. 'tabs' => array(
  1171. 'buddies' => array(),
  1172. 'ignore' => array(),
  1173. ),
  1174. );
  1175. // Pass on to the actual function.
  1176. $context['sub_template'] = $subActions[$context['list_area']][0];
  1177. $subActions[$context['list_area']][0]($memID);
  1178. }
  1179. /**
  1180. * Show all the users buddies, as well as a add/delete interface.
  1181. *
  1182. * @param int $memID id_member
  1183. */
  1184. function editBuddies($memID)
  1185. {
  1186. global $txt, $scripturl, $modSettings;
  1187. global $context, $user_profile, $memberContext, $smcFunc;
  1188. // For making changes!
  1189. $buddiesArray = explode(',', $user_profile[$memID]['buddy_list']);
  1190. foreach ($buddiesArray as $k => $dummy)
  1191. if ($dummy == '')
  1192. unset($buddiesArray[$k]);
  1193. // Removing a buddy?
  1194. if (isset($_GET['remove']))
  1195. {
  1196. checkSession('get');
  1197. // Heh, I'm lazy, do it the easy way...
  1198. foreach ($buddiesArray as $key => $buddy)
  1199. if ($buddy == (int) $_GET['remove'])
  1200. unset($buddiesArray[$key]);
  1201. // Make the changes.
  1202. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1203. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1204. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1205. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1206. }
  1207. elseif (isset($_POST['new_buddy']))
  1208. {
  1209. checkSession();
  1210. // Prepare the string for extraction...
  1211. $_POST['new_buddy'] = strtr($smcFunc['htmlspecialchars']($_POST['new_buddy'], ENT_QUOTES), array('&quot;' => '"'));
  1212. preg_match_all('~"([^"]+)"~', $_POST['new_buddy'], $matches);
  1213. $new_buddies = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_buddy']))));
  1214. foreach ($new_buddies as $k => $dummy)
  1215. {
  1216. $new_buddies[$k] = strtr(trim($new_buddies[$k]), array('\'' => '&#039;'));
  1217. if (strlen($new_buddies[$k]) == 0 || in_array($new_buddies[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1218. unset($new_buddies[$k]);
  1219. }
  1220. if (!empty($new_buddies))
  1221. {
  1222. // Now find out the id_member of the buddy.
  1223. $request = $smcFunc['db_query']('', '
  1224. SELECT id_member
  1225. FROM {db_prefix}members
  1226. WHERE member_name IN ({array_string:new_buddies}) OR real_name IN ({array_string:new_buddies})
  1227. LIMIT {int:count_new_buddies}',
  1228. array(
  1229. 'new_buddies' => $new_buddies,
  1230. 'count_new_buddies' => count($new_buddies),
  1231. )
  1232. );
  1233. // Add the new member to the buddies array.
  1234. while ($row = $smcFunc['db_fetch_assoc']($request))
  1235. $buddiesArray[] = (int) $row['id_member'];
  1236. $smcFunc['db_free_result']($request);
  1237. // Now update the current users buddy list.
  1238. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1239. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1240. }
  1241. // Back to the buddy list!
  1242. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1243. }
  1244. // Get all the users "buddies"...
  1245. $buddies = array();
  1246. if (!empty($buddiesArray))
  1247. {
  1248. $result = $smcFunc['db_query']('', '
  1249. SELECT id_member
  1250. FROM {db_prefix}members
  1251. WHERE id_member IN ({array_int:buddy_list})
  1252. ORDER BY real_name
  1253. LIMIT {int:buddy_list_count}',
  1254. array(
  1255. 'buddy_list' => $buddiesArray,
  1256. 'buddy_list_count' => substr_count($user_profile[$memID]['buddy_list'], ',') + 1,
  1257. )
  1258. );
  1259. while ($row = $smcFunc['db_fetch_assoc']($result))
  1260. $buddies[] = $row['id_member'];
  1261. $smcFunc['db_free_result']($result);
  1262. }
  1263. $context['buddy_count'] = count($buddies);
  1264. // Load all the members up.
  1265. loadMemberData($buddies, false, 'profile');
  1266. // Setup the context for each buddy.
  1267. $context['buddies'] = array();
  1268. foreach ($buddies as $buddy)
  1269. {
  1270. loadMemberContext($buddy);
  1271. $context['buddies'][$buddy] = $memberContext[$buddy];
  1272. }
  1273. }
  1274. /**
  1275. * Allows the user to view their ignore list, as well as the option to manage members on it.
  1276. *
  1277. * @param int $memID id_member
  1278. */
  1279. function editIgnoreList($memID)
  1280. {
  1281. global $txt, $scripturl, $modSettings;
  1282. global $context, $user_profile, $memberContext, $smcFunc;
  1283. // For making changes!
  1284. $ignoreArray = explode(',', $user_profile[$memID]['pm_ignore_list']);
  1285. foreach ($ignoreArray as $k => $dummy)
  1286. if ($dummy == '')
  1287. unset($ignoreArray[$k]);
  1288. // Removing a member from the ignore list?
  1289. if (isset($_GET['remove']))
  1290. {
  1291. checkSession('get');
  1292. // Heh, I'm lazy, do it the easy way...
  1293. foreach ($ignoreArray as $key => $id_remove)
  1294. if ($id_remove == (int) $_GET['remove'])
  1295. unset($ignoreArray[$key]);
  1296. // Make the changes.
  1297. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1298. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1299. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1300. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1301. }
  1302. elseif (isset($_POST['new_ignore']))
  1303. {
  1304. checkSession();
  1305. // Prepare the string for extraction...
  1306. $_POST['new_ignore'] = strtr($smcFunc['htmlspecialchars']($_POST['new_ignore'], ENT_QUOTES), array('&quot;' => '"'));
  1307. preg_match_all('~"([^"]+)"~', $_POST['new_ignore'], $matches);
  1308. $new_entries = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_ignore']))));
  1309. foreach ($new_entries as $k => $dummy)
  1310. {
  1311. $new_entries[$k] = strtr(trim($new_entries[$k]), array('\'' => '&#039;'));
  1312. if (strlen($new_entries[$k]) == 0 || in_array($new_entries[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1313. unset($new_entries[$k]);
  1314. }
  1315. if (!empty($new_entries))
  1316. {
  1317. // Now find out the id_member for the members in question.
  1318. $request = $smcFunc['db_query']('', '
  1319. SELECT id_member
  1320. FROM {db_prefix}members
  1321. WHERE member_name IN ({array_string:new_entries}) OR real_name IN ({array_string:new_entries})
  1322. LIMIT {int:count_new_entries}',
  1323. array(
  1324. 'new_entries' => $new_entries,
  1325. 'count_new_entries' => count($new_entries),
  1326. )
  1327. );
  1328. // Add the new member to the buddies array.
  1329. while ($row = $smcFunc['db_fetch_assoc']($request))
  1330. $ignoreArray[] = (int) $row['id_member'];
  1331. $smcFunc['db_free_result']($request);
  1332. // Now update the current users buddy list.
  1333. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1334. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1335. }
  1336. // Back to the list of pityful people!
  1337. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1338. }
  1339. // Initialise the list of members we're ignoring.
  1340. $ignored = array();
  1341. if (!empty($ignoreArray))
  1342. {
  1343. $result = $smcFunc['db_query']('', '
  1344. SELECT id_member
  1345. FROM {db_prefix}members
  1346. WHERE id_member IN ({array_int:ignore_list})
  1347. ORDER BY real_name
  1348. LIMIT {int:ignore_list_count}',
  1349. array(
  1350. 'ignore_list' => $ignoreArray,
  1351. 'ignore_list_count' => substr_count($user_profile[$memID]['pm_ignore_list'], ',') + 1,
  1352. )
  1353. );
  1354. while ($row = $smcFunc['db_fetch_assoc']($result))
  1355. $ignored[] = $row['id_member'];
  1356. $smcFunc['db_free_result']($result);
  1357. }
  1358. $context['ignore_count'] = count($ignored);
  1359. // Load all the members up.
  1360. loadMemberData($ignored, false, 'profile');
  1361. // Setup the context for each buddy.
  1362. $context['ignore_list'] = array();
  1363. foreach ($ignored as $ignore_member)
  1364. {
  1365. loadMemberContext($ignore_member);
  1366. $context['ignore_list'][$ignore_member] = $memberContext[$ignore_member];
  1367. }
  1368. }
  1369. /**
  1370. * @todo Needs a description
  1371. *
  1372. * @param int $memID id_member
  1373. */
  1374. function account($memID)
  1375. {
  1376. global $context, $txt;
  1377. loadThemeOptions($memID);
  1378. if (allowedTo(array('profile_identity_own', 'profile_identity_any')))
  1379. loadCustomFields($memID, 'account');
  1380. $context['sub_template'] = 'edit_options';
  1381. $context['page_desc'] = $txt['account_info'];
  1382. setupProfileContext(
  1383. array(
  1384. 'member_name', 'real_name', 'date_registered', 'posts', 'lngfile', 'hr',
  1385. 'id_group', 'hr',
  1386. 'email_address', 'hide_email', 'show_online', 'hr',
  1387. 'passwrd1', 'passwrd2', 'hr',
  1388. 'secret_question', 'secret_answer',
  1389. )
  1390. );
  1391. }
  1392. /**
  1393. * @todo Needs a description
  1394. *
  1395. * @param int $memID id_member
  1396. */
  1397. function forumProfile($memID)
  1398. {
  1399. global $context, $user_profile, $user_info, $txt, $modSettings;
  1400. loadThemeOptions($memID);
  1401. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1402. loadCustomFields($memID, 'forumprofile');
  1403. $context['sub_template'] = 'edit_options';
  1404. $context['page_desc'] = $txt['forumProfile_info'];
  1405. setupProfileContext(
  1406. array(
  1407. 'avatar_choice', 'hr', 'personal_text', 'hr',
  1408. 'bday1', 'location', 'gender', 'hr',
  1409. 'icq', 'aim', 'msn', 'yim', 'hr',
  1410. 'usertitle', 'signature', 'hr',
  1411. 'karma_good', 'hr',
  1412. 'website_title', 'website_url',
  1413. )
  1414. );
  1415. }
  1416. /**
  1417. * Allow the edit of *someone elses* personal message settings.
  1418. *
  1419. * @param int $memID id_member
  1420. */
  1421. function pmprefs($memID)
  1422. {
  1423. global $sourcedir, $context, $txt, $scripturl;
  1424. loadThemeOptions($memID);
  1425. loadCustomFields($memID, 'pmprefs');
  1426. $context['sub_template'] = 'edit_options';
  1427. $context['page_desc'] = $txt['pm_settings_desc'];
  1428. setupProfileContext(
  1429. array(
  1430. 'pm_prefs',
  1431. )
  1432. );
  1433. }
  1434. /**
  1435. * Recursive function to retrieve avatar files
  1436. *
  1437. * @param string $directory
  1438. * @param int $level
  1439. * @return array
  1440. */
  1441. function getAvatars($directory, $level)
  1442. {
  1443. global $context, $txt, $modSettings;
  1444. $result = array();
  1445. // Open the directory..
  1446. $dir = dir($modSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
  1447. $dirs = array();
  1448. $files = array();
  1449. if (!$dir)
  1450. return array();
  1451. while ($line = $dir->read())
  1452. {
  1453. if (in_array($line, array('.', '..', 'blank.gif', 'index.php')))
  1454. continue;
  1455. if (is_dir($modSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
  1456. $dirs[] = $line;
  1457. else
  1458. $files[] = $line;
  1459. }
  1460. $dir->close();
  1461. // Sort the results...
  1462. natcasesort($dirs);
  1463. natcasesort($files);
  1464. if ($level == 0)
  1465. {
  1466. $result[] = array(
  1467. 'filename' => 'blank.gif',
  1468. 'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.gif')),
  1469. 'name' => $txt['no_pic'],
  1470. 'is_dir' => false
  1471. );
  1472. }
  1473. foreach ($dirs as $line)
  1474. {
  1475. $tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
  1476. if (!empty($tmp))
  1477. $result[] = array(
  1478. 'filename' => htmlspecialchars($line),
  1479. 'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
  1480. 'name' => '[' . htmlspecialchars(str_replace('_', ' ', $line)) . ']',
  1481. 'is_dir' => true,
  1482. 'files' => $tmp
  1483. );
  1484. unset($tmp);
  1485. }
  1486. foreach ($files as $line)
  1487. {
  1488. $filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
  1489. $extension = substr(strrchr($line, '.'), 1);
  1490. // Make sure it is an image.
  1491. if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
  1492. continue;
  1493. $result[] = array(
  1494. 'filename' => htmlspecialchars($line),
  1495. 'checked' => $line == $context['member']['avatar']['server_pic'],
  1496. 'name' => htmlspecialchars(str_replace('_', ' ', $filename)),
  1497. 'is_dir' => false
  1498. );
  1499. if ($level == 1)
  1500. $context['avatar_list'][] = $directory . '/' . $line;
  1501. }
  1502. return $result;
  1503. }
  1504. /**
  1505. * @todo needs a description
  1506. *
  1507. * @param int $memID id_member
  1508. */
  1509. function theme($memID)
  1510. {
  1511. global $txt, $context, $user_profile, $modSettings, $settings, $user_info, $smcFunc;
  1512. loadThemeOptions($memID);
  1513. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1514. loadCustomFields($memID, 'theme');
  1515. $context['sub_template'] = 'edit_options';
  1516. $context['page_desc'] = $txt['theme_info'];
  1517. setupProfileContext(
  1518. array(
  1519. 'id_theme', 'smiley_set', 'hr',
  1520. 'time_format', 'time_offset', 'hr',
  1521. 'theme_settings',
  1522. )
  1523. );
  1524. }
  1525. /**
  1526. * Changing authentication method? Only appropriate for people using OpenID.
  1527. *
  1528. * @param int $memID id_member
  1529. * @param bool $saving = false
  1530. */
  1531. function authentication($memID, $saving = false)
  1532. {
  1533. global $context, $cur_profile, $sourcedir, $txt, $post_errors, $modSettings;
  1534. loadLanguage('Login');
  1535. // We are saving?
  1536. if ($saving)
  1537. {
  1538. // Moving to password passed authentication?
  1539. if ($_POST['authenticate'] == 'passwd')
  1540. {
  1541. // Didn't enter anything?
  1542. if ($_POST['passwrd1'] == '')
  1543. $post_errors[] = 'no_password';
  1544. // Do the two entries for the password even match?
  1545. elseif (!isset($_POST['passwrd2']) || $_POST['passwrd1'] != $_POST['passwrd2'])
  1546. $post_errors[] = 'bad_new_password';
  1547. // Is it valid?
  1548. else
  1549. {
  1550. require_once($sourcedir . '/Subs-Auth.php');
  1551. $passwordErrors = validatePassword($_POST['passwrd1'], $cur_profile['member_name'], array($cur_profile['real_name'], $cur_profile['email_address']));
  1552. // Were there errors?
  1553. if ($passwordErrors != null)
  1554. $post_errors[] = 'password_' . $passwordErrors;
  1555. }
  1556. if (empty($post_errors))
  1557. {
  1558. // Integration?
  1559. call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd1']));
  1560. // Go then.
  1561. $passwd = sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd1']));
  1562. // Do the important bits.
  1563. updateMemberData($memID, array('openid_uri' => '', 'passwd' => $passwd));
  1564. if ($context['user']['is_owner'])
  1565. setLoginCookie(60 * $modSettings['cookieTime'], $memID, sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  1566. redirectexit('action=profile;u=' . $memID);
  1567. }
  1568. return true;
  1569. }
  1570. // Not right yet!
  1571. elseif ($_POST['authenticate'] == 'openid' && !empty($_POST['openid_identifier']))
  1572. {
  1573. require_once($sourcedir . '/Subs-OpenID.php');
  1574. $_POST['openid_identifier'] = smf_openID_canonize($_POST['openid_identifier']);
  1575. if (smf_openid_member_exists($_POST['openid_identifier']))
  1576. $post_errors[] = 'openid_in_use';
  1577. elseif (empty($post_errors))
  1578. {
  1579. // Authenticate using the new OpenID URI first to make sure they didn't make a mistake.
  1580. if ($context['user']['is_owner'])
  1581. {
  1582. $_SESSION['new_openid_uri'] = $_POST['openid_identifier'];
  1583. smf_openID_validate($_POST['openid_identifier'], false, null, 'change_uri');
  1584. }
  1585. else
  1586. updateMemberData($memID, array('openid_uri' => $_POST['openid_identifier']));
  1587. }
  1588. }
  1589. }
  1590. // Some stuff.
  1591. $context['member']['openid_uri'] = $cur_profile['openid_uri'];
  1592. $context['auth_method'] = empty($cur_profile['openid_uri']) ? 'password' : 'openid';
  1593. $context['sub_template'] = 'authentication_method';
  1594. }
  1595. /**
  1596. * Display the notifications and settings for changes.
  1597. *
  1598. * @param int $memID id_member
  1599. */
  1600. function notification($memID)
  1601. {
  1602. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc, $sourcedir, $settings;
  1603. // Gonna want this for the list.
  1604. require_once($sourcedir . '/Subs-List.php');
  1605. // Fine, start with the board list.
  1606. $listOptions = array(
  1607. 'id' => 'board_notification_list',
  1608. 'width' => '100%',
  1609. 'no_items_label' => $txt['notifications_boards_none'] . '<br /><br />' . $txt['notifications_boards_howto'],
  1610. 'no_items_align' => 'left',
  1611. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1612. 'default_sort_col' => 'board_name',
  1613. 'get_items' => array(
  1614. 'function' => 'list_getBoardNotifications',
  1615. 'params' => array(
  1616. $memID,
  1617. ),
  1618. ),
  1619. 'columns' => array(
  1620. 'board_name' => array(
  1621. 'header' => array(
  1622. 'value' => $txt['notifications_boards'],
  1623. 'class' => 'lefttext first_th',
  1624. ),
  1625. 'data' => array(
  1626. 'function' => create_function('$board', '
  1627. global $settings, $txt;
  1628. $link = $board[\'link\'];
  1629. if ($board[\'new\'])
  1630. $link .= \' <a href="\' . $board[\'href\'] . \'"><img src="\' . $settings[\'lang_images_url\'] . \'/new.gif" alt="\' . $txt[\'new\'] . \'" /></a>\';
  1631. return $link;
  1632. '),
  1633. ),
  1634. 'sort' => array(
  1635. 'default' => 'name',
  1636. 'reverse' => 'name DESC',
  1637. ),
  1638. ),
  1639. 'delete' => array(
  1640. 'header' => array(
  1641. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1642. 'style' => 'width: 4%;',
  1643. ),
  1644. 'data' => array(
  1645. 'sprintf' => array(
  1646. 'format' => '<input type="checkbox" name="notify_boards[]" value="%1$d" class="input_check" />',
  1647. 'params' => array(
  1648. 'id' => false,
  1649. ),
  1650. ),
  1651. 'style' => 'text-align: center;',
  1652. ),
  1653. ),
  1654. ),
  1655. 'form' => array(
  1656. 'href' => $scripturl . '?action=profile;area=notification;save',
  1657. 'include_sort' => true,
  1658. 'include_start' => true,
  1659. 'hidden_fields' => array(
  1660. 'u' => $memID,
  1661. 'sa' => $context['menu_item_selected'],
  1662. $context['session_var'] => $context['session_id'],
  1663. ),
  1664. 'token' => $context['token_check'],
  1665. ),
  1666. 'additional_rows' => array(
  1667. array(
  1668. 'position' => 'bottom_of_list',
  1669. 'value' => '<input type="submit" name="edit_notify_boards" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1670. 'align' => 'right',
  1671. ),
  1672. ),
  1673. );
  1674. // Create the board notification list.
  1675. createList($listOptions);
  1676. // Now do the topic notifications.
  1677. $listOptions = array(
  1678. 'id' => 'topic_notification_list',
  1679. 'width' => '100%',
  1680. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1681. 'no_items_label' => $txt['notifications_topics_none'] . '<br /><br />' . $txt['notifications_topics_howto'],
  1682. 'no_items_align' => 'left',
  1683. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1684. 'default_sort_col' => 'last_post',
  1685. 'get_items' => array(
  1686. 'function' => 'list_getTopicNotifications',
  1687. 'params' => array(
  1688. $memID,
  1689. ),
  1690. ),
  1691. 'get_count' => array(
  1692. 'function' => 'list_getTopicNotificationCount',
  1693. 'params' => array(
  1694. $memID,
  1695. ),
  1696. ),
  1697. 'columns' => array(
  1698. 'subject' => array(
  1699. 'header' => array(
  1700. 'value' => $txt['notifications_topics'],
  1701. 'class' => 'lefttext first_th',
  1702. ),
  1703. 'data' => array(
  1704. 'function' => create_function('$topic', '
  1705. global $settings, $txt;
  1706. $link = $topic[\'link\'];
  1707. if ($topic[\'new\'])
  1708. $link .= \' <a href="\' . $topic[\'new_href\'] . \'"><img src="\' . $settings[\'lang_images_url\'] . \'/new.gif" alt="\' . $txt[\'new\'] . \'" /></a>\';
  1709. $link .= \'<br /><span class="smalltext"><em>\' . $txt[\'in\'] . \' \' . $topic[\'board_link\'] . \'</em></span>\';
  1710. return $link;
  1711. '),
  1712. ),
  1713. 'sort' => array(
  1714. 'default' => 'ms.subject',
  1715. 'reverse' => 'ms.subject DESC',
  1716. ),
  1717. ),
  1718. 'started_by' => array(
  1719. 'header' => array(
  1720. 'value' => $txt['started_by'],
  1721. 'class' => 'lefttext',
  1722. ),
  1723. 'data' => array(
  1724. 'db' => 'poster_link',
  1725. ),
  1726. 'sort' => array(
  1727. 'default' => 'real_name_col',
  1728. 'reverse' => 'real_name_col DESC',
  1729. ),
  1730. ),
  1731. 'last_post' => array(
  1732. 'header' => array(
  1733. 'value' => $txt['last_post'],
  1734. 'class' => 'lefttext',
  1735. ),
  1736. 'data' => array(
  1737. 'sprintf' => array(
  1738. 'format' => '<span class="smalltext">%1$s<br />' . $txt['by'] . ' %2$s</span>',
  1739. 'params' => array(
  1740. 'updated' => false,
  1741. 'poster_updated_link' => false,
  1742. ),
  1743. ),
  1744. ),
  1745. 'sort' => array(
  1746. 'default' => 'ml.id_msg DESC',
  1747. 'reverse' => 'ml.id_msg',
  1748. ),
  1749. ),
  1750. 'delete' => array(
  1751. 'header' => array(
  1752. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1753. 'style' => 'width: 4%;',
  1754. ),
  1755. 'data' => array(
  1756. 'sprintf' => array(
  1757. 'format' => '<input type="checkbox" name="notify_topics[]" value="%1$d" class="input_check" />',
  1758. 'params' => array(
  1759. 'id' => false,
  1760. ),
  1761. ),
  1762. 'style' => 'text-align: center;',
  1763. ),
  1764. ),
  1765. ),
  1766. 'form' => array(
  1767. 'href' => $scripturl . '?action=profile;area=notification;save',
  1768. 'include_sort' => true,
  1769. 'include_start' => true,
  1770. 'hidden_fields' => array(
  1771. 'u' => $memID,
  1772. 'sa' => $context['menu_item_selected'],
  1773. $context['session_var'] => $context['session_id'],
  1774. ),
  1775. 'token' => $context['token_check'],
  1776. ),
  1777. 'additional_rows' => array(
  1778. array(
  1779. 'position' => 'bottom_of_list',
  1780. 'value' => '<input type="submit" name="edit_notify_topics" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1781. 'align' => 'right',
  1782. ),
  1783. ),
  1784. );
  1785. // Create the notification list.
  1786. createList($listOptions);
  1787. // What options are set?
  1788. $context['member'] += array(
  1789. 'notify_announcements' => $user_profile[$memID]['notify_announcements'],
  1790. 'notify_send_body' => $user_profile[$memID]['notify_send_body'],
  1791. 'notify_types' => $user_profile[$memID]['notify_types'],
  1792. 'notify_regularity' => $user_profile[$memID]['notify_regularity'],
  1793. );
  1794. loadThemeOptions($memID);
  1795. }
  1796. /**
  1797. * @todo needs a description
  1798. *
  1799. * @param int $memID id_member
  1800. * @return string
  1801. */
  1802. function list_getTopicNotificationCount($memID)
  1803. {
  1804. global $smcFunc, $user_info, $context, $modSettings;
  1805. $request = $smcFunc['db_query']('', '
  1806. SELECT COUNT(*)
  1807. FROM {db_prefix}log_notify AS ln' . (!$modSettings['postmod_active'] && $user_info['query_see_board'] === '1=1' ? '' : '
  1808. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)') . ($user_info['query_see_board'] === '1=1' ? '' : '
  1809. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)') . '
  1810. WHERE ln.id_member = {int:selected_member}' . ($user_info['query_see_board'] === '1=1' ? '' : '
  1811. AND {query_see_board}') . ($modSettings['postmod_active'] ? '
  1812. AND t.approved = {int:is_approved}' : ''),
  1813. array(
  1814. 'selected_member' => $memID,
  1815. 'is_approved' => 1,
  1816. )
  1817. );
  1818. list ($totalNotifications) = $smcFunc['db_fetch_row']($request);
  1819. $smcFunc['db_free_result']($request);
  1820. // @todo make this an integer before it gets returned
  1821. return $totalNotifications;
  1822. }
  1823. /**
  1824. * @todo Needs a description
  1825. *
  1826. * @param int $start
  1827. * @param int $items_per_page
  1828. * @param string $sort
  1829. * @param int $memID id_member
  1830. * @return array $notification_topics
  1831. */
  1832. function list_getTopicNotifications($start, $items_per_page, $sort, $memID)
  1833. {
  1834. global $smcFunc, $txt, $scripturl, $user_info, $context, $modSettings;
  1835. // All the topics with notification on...
  1836. $request = $smcFunc['db_query']('', '
  1837. SELECT
  1838. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, b.id_board, b.name,
  1839. t.id_topic, ms.subject, ms.id_member, IFNULL(mem.real_name, ms.poster_name) AS real_name_col,
  1840. ml.id_msg_modified, ml.poster_time, ml.id_member AS id_member_updated,
  1841. IFNULL(mem2.real_name, ml.poster_name) AS last_real_name
  1842. FROM {db_prefix}log_notify AS ln
  1843. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  1844. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board AND {query_see_board})
  1845. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  1846. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  1847. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ms.id_member)
  1848. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = ml.id_member)
  1849. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  1850. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
  1851. WHERE ln.id_member = {int:selected_member}
  1852. ORDER BY {raw:sort}
  1853. LIMIT {int:offset}, {int:items_per_page}',
  1854. array(
  1855. 'current_member' => $user_info['id'],
  1856. 'is_approved' => 1,
  1857. 'selected_member' => $memID,
  1858. 'sort' => $sort,
  1859. 'offset' => $start,
  1860. 'items_per_page' => $items_per_page,
  1861. )
  1862. );
  1863. $notification_topics = array();
  1864. while ($row = $smcFunc['db_fetch_assoc']($request))
  1865. {
  1866. censorText($row['subject']);
  1867. $notification_topics[] = array(
  1868. 'id' => $row['id_topic'],
  1869. 'poster_link' => empty($row['id_member']) ? $row['real_name_col'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name_col'] . '</a>',
  1870. 'poster_updated_link' => empty($row['id_member_updated']) ? $row['last_real_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['last_real_name'] . '</a>',
  1871. 'subject' => $row['subject'],
  1872. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1873. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  1874. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  1875. 'new_from' => $row['new_from'],
  1876. 'updated' => timeformat($row['poster_time']),
  1877. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  1878. 'new_link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new">' . $row['subject'] . '</a>',
  1879. 'board_link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1880. );
  1881. }
  1882. $smcFunc['db_free_result']($request);
  1883. return $notification_topics;
  1884. }
  1885. /**
  1886. * @todo needs a description
  1887. *
  1888. * @param int $start
  1889. * @param int $items_per_page
  1890. * @param string $sort
  1891. * @param int $memID id_member
  1892. * @return array
  1893. */
  1894. function list_getBoardNotifications($start, $items_per_page, $sort, $memID)
  1895. {
  1896. global $smcFunc, $txt, $scripturl, $user_info;
  1897. $request = $smcFunc['db_query']('', '
  1898. SELECT b.id_board, b.name, IFNULL(lb.id_msg, 0) AS board_read, b.id_msg_updated
  1899. FROM {db_prefix}log_notify AS ln
  1900. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board)
  1901. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  1902. WHERE ln.id_member = {int:selected_member}
  1903. AND {query_see_board}
  1904. ORDER BY ' . $sort,
  1905. array(
  1906. 'current_member' => $user_info['id'],
  1907. 'selected_member' => $memID,
  1908. )
  1909. );
  1910. $notification_boards = array();
  1911. while ($row = $smcFunc['db_fetch_assoc']($request))
  1912. $notification_boards[] = array(
  1913. 'id' => $row['id_board'],
  1914. 'name' => $row['name'],
  1915. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  1916. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1917. 'new' => $row['board_read'] < $row['id_msg_updated']
  1918. );
  1919. $smcFunc['db_free_result']($request);
  1920. return $notification_boards;
  1921. }
  1922. /**
  1923. * @todo needs a description
  1924. *
  1925. * @param int $memID id_member
  1926. */
  1927. function loadThemeOptions($memID)
  1928. {
  1929. global $context, $options, $cur_profile, $smcFunc;
  1930. if (isset($_POST['default_options']))
  1931. $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
  1932. if ($context['user']['is_owner'])
  1933. {
  1934. $context['member']['options'] = $options;
  1935. if (isset($_POST['options']) && is_array($_POST['options']))
  1936. foreach ($_POST['options'] as $k => $v)
  1937. $context['member']['options'][$k] = $v;
  1938. }
  1939. else
  1940. {
  1941. $request = $smcFunc['db_query']('', '
  1942. SELECT id_member, variable, value
  1943. FROM {db_prefix}themes
  1944. WHERE id_theme IN (1, {int:member_theme})
  1945. AND id_member IN (-1, {int:selected_member})',
  1946. array(
  1947. 'member_theme' => (int) $cur_profile['id_theme'],
  1948. 'selected_member' => $memID,
  1949. )
  1950. );
  1951. $temp = array();
  1952. while ($row = $smcFunc['db_fetch_assoc']($request))
  1953. {
  1954. if ($row['id_member'] == -1)
  1955. {
  1956. $temp[$row['variable']] = $row['value'];
  1957. continue;
  1958. }
  1959. if (isset($_POST['options'][$row['variable']]))
  1960. $row['value'] = $_POST['options'][$row['variable']];
  1961. $context['member']['options'][$row['variable']] = $row['value'];
  1962. }
  1963. $smcFunc['db_free_result']($request);
  1964. // Load up the default theme options for any missing.
  1965. foreach ($temp as $k => $v)
  1966. {
  1967. if (!isset($context['member']['options'][$k]))
  1968. $context['member']['options'][$k] = $v;
  1969. }
  1970. }
  1971. }
  1972. /**
  1973. * @todo needs a description
  1974. *
  1975. * @param int $memID id_member
  1976. */
  1977. function ignoreboards($memID)
  1978. {
  1979. global $txt, $user_info, $context, $modSettings, $smcFunc, $cur_profile;
  1980. // Have the admins enabled this option?
  1981. if (empty($modSettings['allow_ignore_boards']))
  1982. fatal_lang_error('ignoreboards_disallowed', 'user');
  1983. // Find all the boards this user is allowed to see.
  1984. $request = $smcFunc['db_query']('order_by_board_order', '
  1985. SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level,
  1986. '. (!empty($cur_profile['ignore_boards']) ? 'b.id_board IN ({array_int:ignore_boards})' : '0') . ' AS is_ignored
  1987. FROM {db_prefix}boards AS b
  1988. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  1989. WHERE {query_see_board}
  1990. AND redirect = {string:empty_string}',
  1991. array(
  1992. 'ignore_boards' => !empty($cur_profile['ignore_boards']) ? explode(',', $cur_profile['ignore_boards']) : array(),
  1993. 'empty_string' => '',
  1994. )
  1995. );
  1996. $context['num_boards'] = $smcFunc['db_num_rows']($request);
  1997. $context['categories'] = array();
  1998. while ($row = $smcFunc['db_fetch_assoc']($request))
  1999. {
  2000. // This category hasn't been set up yet..
  2001. if (!isset($context['categories'][$row['id_cat']]))
  2002. $context['categories'][$row['id_cat']] = array(
  2003. 'id' => $row['id_cat'],
  2004. 'name' => $row['cat_name'],
  2005. 'boards' => array()
  2006. );
  2007. // Set this board up, and let the template know when it's a child. (indent them..)
  2008. $context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
  2009. 'id' => $row['id_board'],
  2010. 'name' => $row['name'],
  2011. 'child_level' => $row['child_level'],
  2012. 'selected' => $row['is_ignored'],
  2013. );
  2014. }
  2015. $smcFunc['db_free_result']($request);
  2016. // Now, let's sort the list of categories into the boards for templates that like that.
  2017. $temp_boards = array();
  2018. foreach ($context['categories'] as $category)
  2019. {
  2020. // Include a list of boards per category for easy toggling.
  2021. $context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
  2022. $temp_boards[] = array(
  2023. 'name' => $category['name'],
  2024. 'child_ids' => array_keys($category['boards'])
  2025. );
  2026. $temp_boards = array_merge($temp_boards, array_values($category['boards']));
  2027. }
  2028. $max_boards = ceil(count($temp_boards) / 2);
  2029. if ($max_boards == 1)
  2030. $max_boards = 2;
  2031. // Now, alternate them so they can be shown left and right ;).
  2032. $context['board_columns'] = array();
  2033. for ($i = 0; $i < $max_boards; $i++)
  2034. {
  2035. $context['board_columns'][] = $temp_boards[$i];
  2036. if (isset($temp_boards[$i + $max_boards]))
  2037. $context['board_columns'][] = $temp_boards[$i + $max_boards];
  2038. else
  2039. $context['board_columns'][] = array();
  2040. }
  2041. loadThemeOptions($memID);
  2042. }
  2043. /**
  2044. * Load all the languages for the profile.
  2045. * @return bool
  2046. */
  2047. function profileLoadLanguages()
  2048. {
  2049. global $context, $modSettings, $settings, $cur_profile, $language, $smcFunc;
  2050. $context['profile_languages'] = array();
  2051. // Get our languages!
  2052. getLanguages(true, true);
  2053. // Setup our languages.
  2054. foreach ($context['languages'] as $lang)
  2055. {
  2056. $context['profile_languages'][$lang['filename']] = strtr($lang['name'], array('-utf8' => ''));
  2057. }
  2058. ksort($context['profile_languages']);
  2059. // Return whether we should proceed with this.
  2060. return count($context['profile_languages']) > 1 ? true : false;
  2061. }
  2062. /**
  2063. * @todo needs description
  2064. *
  2065. * @return true
  2066. */
  2067. function profileLoadGroups()
  2068. {
  2069. global $cur_profile, $txt, $context, $smcFunc, $user_settings;
  2070. $context['member_groups'] = array(
  2071. 0 => array(
  2072. 'id' => 0,
  2073. 'name' => $txt['no_primary_membergroup'],
  2074. 'is_primary' => $cur_profile['id_group'] == 0,
  2075. 'can_be_additional' => false,
  2076. 'can_be_primary' => true,
  2077. )
  2078. );
  2079. $curGroups = explode(',', $cur_profile['additional_groups']);
  2080. // Load membergroups, but only those groups the user can assign.
  2081. $request = $smcFunc['db_query']('', '
  2082. SELECT group_name, id_group, hidden
  2083. FROM {db_prefix}membergroups
  2084. WHERE id_group != {int:moderator_group}
  2085. AND min_posts = {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  2086. AND group_type != {int:is_protected}') . '
  2087. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  2088. array(
  2089. 'moderator_group' => 3,
  2090. 'min_posts' => -1,
  2091. 'is_protected' => 1,
  2092. 'newbie_group' => 4,
  2093. )
  2094. );
  2095. while ($row = $smcFunc['db_fetch_assoc']($request))
  2096. {
  2097. // We should skip the administrator group if they don't have the admin_forum permission!
  2098. if ($row['id_group'] == 1 && !allowedTo('admin_forum'))
  2099. continue;
  2100. $context['member_groups'][$row['id_group']] = array(
  2101. 'id' => $row['id_group'],
  2102. 'name' => $row['group_name'],
  2103. 'is_primary' => $cur_profile['id_group'] == $row['id_group'],
  2104. 'is_additional' => in_array($row['id_group'], $curGroups),
  2105. 'can_be_additional' => true,
  2106. 'can_be_primary' => $row['hidden'] != 2,
  2107. );
  2108. }
  2109. $smcFunc['db_free_result']($request);
  2110. $context['member']['group_id'] = $user_settings['id_group'];
  2111. return true;
  2112. }
  2113. /**
  2114. * Load key signature context data.
  2115. *
  2116. * @return true
  2117. */
  2118. function profileLoadSignatureData()
  2119. {
  2120. global $modSettings, $context, $txt, $cur_profile, $smcFunc;
  2121. // Signature limits.
  2122. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2123. $sig_limits = explode(',', $sig_limits);
  2124. $context['signature_enabled'] = isset($sig_limits[0]) ? $sig_limits[0] : 0;
  2125. $context['signature_limits'] = array(
  2126. 'max_length' => isset($sig_limits[1]) ? $sig_limits[1] : 0,
  2127. 'max_lines' => isset($sig_limits[2]) ? $sig_limits[2] : 0,
  2128. 'max_images' => isset($sig_limits[3]) ? $sig_limits[3] : 0,
  2129. 'max_smileys' => isset($sig_limits[4]) ? $sig_limits[4] : 0,
  2130. 'max_image_width' => isset($sig_limits[5]) ? $sig_limits[5] : 0,
  2131. 'max_image_height' => isset($sig_limits[6]) ? $sig_limits[6] : 0,
  2132. 'max_font_size' => isset($sig_limits[7]) ? $sig_limits[7] : 0,
  2133. 'bbc' => !empty($sig_bbc) ? explode(',', $sig_bbc) : array(),
  2134. );
  2135. // Kept this line in for backwards compatibility!
  2136. $context['max_signature_length'] = $context['signature_limits']['max_length'];
  2137. // Warning message for signature image limits?
  2138. $context['signature_warning'] = '';
  2139. if ($context['signature_limits']['max_image_width'] && $context['signature_limits']['max_image_height'])
  2140. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_size'], $context['signature_limits']['max_image_width'], $context['signature_limits']['max_image_height']);
  2141. elseif ($context['signature_limits']['max_image_width'] || $context['signature_limits']['max_image_height'])
  2142. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_' . ($context['signature_limits']['max_image_width'] ? 'width' : 'height')], $context['signature_limits'][$context['signature_limits']['max_image_width'] ? 'max_image_width' : 'max_image_height']);
  2143. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
  2144. $context['member']['signature'] = empty($cur_profile['signature']) ? '' : str_replace(array('<br />', '<', '>', '"', '\''), array("\n", '&lt;', '&gt;', '&quot;', '&#039;'), $cur_profile['signature']);
  2145. return true;
  2146. }
  2147. /**
  2148. * Load avatar context data.
  2149. *
  2150. * @return true
  2151. */
  2152. function profileLoadAvatarData()
  2153. {
  2154. global $context, $cur_profile, $modSettings, $scripturl;
  2155. $context['avatar_url'] = $modSettings['avatar_url'];
  2156. // Default context.
  2157. $context['member']['avatar'] += array(
  2158. 'custom' => stristr($cur_profile['avatar'], 'http://') ? $cur_profile['avatar'] : 'http://',
  2159. 'selection' => $cur_profile['avatar'] == '' || stristr($cur_profile['avatar'], 'http://') ? '' : $cur_profile['avatar'],
  2160. 'id_attach' => $cur_profile['id_attach'],
  2161. 'filename' => $cur_profile['filename'],
  2162. 'allow_server_stored' => allowedTo('profile_server_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2163. 'allow_upload' => allowedTo('profile_upload_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2164. 'allow_external' => allowedTo('profile_remote_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2165. );
  2166. if ($cur_profile['avatar'] == '' && $cur_profile['id_attach'] > 0 && $context['member']['avatar']['allow_upload'])
  2167. {
  2168. $context['member']['avatar'] += array(
  2169. 'choice' => 'upload',
  2170. 'server_pic' => 'blank.gif',
  2171. 'external' => 'http://'
  2172. );
  2173. $context['member']['avatar']['href'] = empty($cur_profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $cur_profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $cur_profile['filename'];
  2174. }
  2175. elseif (stristr($cur_profile['avatar'], 'http://') && $context['member']['avatar']['allow_external'])
  2176. $context['member']['avatar'] += array(
  2177. 'choice' => 'external',
  2178. 'server_pic' => 'blank.gif',
  2179. 'external' => $cur_profile['avatar']
  2180. );
  2181. elseif ($cur_profile['avatar'] != '' && file_exists($modSettings['avatar_directory'] . '/' . $cur_profile['avatar']) && $context['member']['avatar']['allow_server_stored'])
  2182. $context['member']['avatar'] += array(
  2183. 'choice' => 'server_stored',
  2184. 'server_pic' => $cur_profile['avatar'] == '' ? 'blank.gif' : $cur_profile['avatar'],
  2185. 'external' => 'http://'
  2186. );
  2187. else
  2188. $context['member']['avatar'] += array(
  2189. 'choice' => 'none',
  2190. 'server_pic' => 'blank.gif',
  2191. 'external' => 'http://'
  2192. );
  2193. // Get a list of all the avatars.
  2194. if ($context['member']['avatar']['allow_server_stored'])
  2195. {
  2196. $context['avatar_list'] = array();
  2197. $context['avatars'] = is_dir($modSettings['avatar_directory']) ? getAvatars('', 0) : array();
  2198. }
  2199. else
  2200. $context['avatars'] = array();
  2201. // Second level selected avatar...
  2202. $context['avatar_selected'] = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);
  2203. return true;
  2204. }
  2205. /**
  2206. * Save a members group.
  2207. *
  2208. * @param int &$value
  2209. * @return true
  2210. */
  2211. function profileSaveGroups(&$value)
  2212. {
  2213. global $profile_vars, $old_profile, $context, $smcFunc, $cur_profile;
  2214. // Do we need to protect some groups?
  2215. if (!allowedTo('admin_forum'))
  2216. {
  2217. $request = $smcFunc['db_query']('', '
  2218. SELECT id_group
  2219. FROM {db_prefix}membergroups
  2220. WHERE group_type = {int:is_protected}',
  2221. array(
  2222. 'is_protected' => 1,
  2223. )
  2224. );
  2225. $protected_groups = array(1);
  2226. while ($row = $smcFunc['db_fetch_assoc']($request))
  2227. $protected_groups[] = $row['id_group'];
  2228. $smcFunc['db_free_result']($request);
  2229. $protected_groups = array_unique($protected_groups);
  2230. }
  2231. // The account page allows the change of your id_group - but not to a protected group!
  2232. if (empty($protected_groups) || count(array_intersect(array((int) $value, $old_profile['id_group']), $protected_groups)) == 0)
  2233. $value = (int) $value;
  2234. // ... otherwise it's the old group sir.
  2235. else
  2236. $value = $old_profile['id_group'];
  2237. // Find the additional membergroups (if any)
  2238. if (isset($_POST['additional_groups']) && is_array($_POST['additional_groups']))
  2239. {
  2240. $additional_groups = array();
  2241. foreach ($_POST['additional_groups'] as $group_id)
  2242. {
  2243. $group_id = (int) $group_id;
  2244. if (!empty($group_id) && (empty($protected_groups) || !in_array($group_id, $protected_groups)))
  2245. $additional_groups[] = $group_id;
  2246. }
  2247. // Put the protected groups back in there if you don't have permission to take them away.
  2248. $old_additional_groups = explode(',', $old_profile['additional_groups']);
  2249. foreach ($old_additional_groups as $group_id)
  2250. {
  2251. if (!empty($protected_groups) && in_array($group_id, $protected_groups))
  2252. $additional_groups[] = $group_id;
  2253. }
  2254. if (implode(',', $additional_groups) !== $old_profile['additional_groups'])
  2255. {
  2256. $profile_vars['additional_groups'] = implode(',', $additional_groups);
  2257. $cur_profile['additional_groups'] = implode(',', $additional_groups);
  2258. }
  2259. }
  2260. // Too often, people remove delete their own account, or something.
  2261. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  2262. {
  2263. $stillAdmin = $value == 1 || (isset($additional_groups) && in_array(1, $additional_groups));
  2264. // If they would no longer be an admin, look for any other...
  2265. if (!$stillAdmin)
  2266. {
  2267. $request = $smcFunc['db_query']('', '
  2268. SELECT id_member
  2269. FROM {db_prefix}members
  2270. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  2271. AND id_member != {int:selected_member}
  2272. LIMIT 1',
  2273. array(
  2274. 'admin_group' => 1,
  2275. 'selected_member' => $context['id_member'],
  2276. )
  2277. );
  2278. list ($another) = $smcFunc['db_fetch_row']($request);
  2279. $smcFunc['db_free_result']($request);
  2280. if (empty($another))
  2281. fatal_lang_error('at_least_one_admin', 'critical');
  2282. }
  2283. }
  2284. // If we are changing group status, update permission cache as necessary.
  2285. if ($value != $old_profile['id_group'] || isset($profile_vars['additional_groups']))
  2286. {
  2287. if ($context['user']['is_owner'])
  2288. $_SESSION['mc']['time'] = 0;
  2289. else
  2290. updateSettings(array('settings_updated' => time()));
  2291. }
  2292. return true;
  2293. }
  2294. /**
  2295. * The avatar is incredibly complicated, what with the options... and what not.
  2296. * @todo argh, the avatar here. Take this out of here!
  2297. *
  2298. * @param array &$value
  2299. * @return mixed
  2300. */
  2301. function profileSaveAvatarData(&$value)
  2302. {
  2303. global $modSettings, $sourcedir, $smcFunc, $profile_vars, $cur_profile, $context;
  2304. $memID = $context['id_member'];
  2305. if (empty($memID) && !empty($context['password_auth_failed']))
  2306. return false;
  2307. require_once($sourcedir . '/ManageAttachments.php');
  2308. // We need to know where we're going to be putting it..
  2309. if (!empty($modSettings['custom_avatar_enabled']))
  2310. {
  2311. $uploadDir = $modSettings['custom_avatar_dir'];
  2312. $id_folder = 1;
  2313. }
  2314. elseif (!empty($modSettings['currentAttachmentUploadDir']))
  2315. {
  2316. if (!is_array($modSettings['attachmentUploadDir']))
  2317. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  2318. // Just use the current path for temp files.
  2319. $uploadDir = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
  2320. $id_folder = $modSettings['currentAttachmentUploadDir'];
  2321. }
  2322. else
  2323. {
  2324. $uploadDir = $modSettings['attachmentUploadDir'];
  2325. $id_folder = 1;
  2326. }
  2327. $downloadedExternalAvatar = false;
  2328. if ($value == 'external' && allowedTo('profile_remote_avatar') && strtolower(substr($_POST['userpicpersonal'], 0, 7)) == 'http://' && strlen($_POST['userpicpersonal']) > 7 && !empty($modSettings['avatar_download_external']))
  2329. {
  2330. if (!is_writable($uploadDir))
  2331. fatal_lang_error('attachments_no_write', 'critical');
  2332. require_once($sourcedir . '/Subs-Package.php');
  2333. $url = parse_url($_POST['userpicpersonal']);
  2334. $contents = fetch_web_data('http://' . $url['host'] . (empty($url['port']) ? '' : ':' . $url['port']) . str_replace(' ', '%20', trim($url['path'])));
  2335. if ($contents != false && $tmpAvatar = fopen($uploadDir . '/avatar_tmp_' . $memID, 'wb'))
  2336. {
  2337. fwrite($tmpAvatar, $contents);
  2338. fclose($tmpAvatar);
  2339. $downloadedExternalAvatar = true;
  2340. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2341. }
  2342. }
  2343. if ($value == 'none')
  2344. {
  2345. $profile_vars['avatar'] = '';
  2346. // Reset the attach ID.
  2347. $cur_profile['id_attach'] = 0;
  2348. $cur_profile['attachment_type'] = 0;
  2349. $cur_profile['filename'] = '';
  2350. removeAttachments(array('id_member' => $memID));
  2351. }
  2352. elseif ($value == 'server_stored' && allowedTo('profile_server_avatar'))
  2353. {
  2354. $profile_vars['avatar'] = strtr(empty($_POST['file']) ? (empty($_POST['cat']) ? '' : $_POST['cat']) : $_POST['file'], array('&amp;' => '&'));
  2355. $profile_vars['avatar'] = preg_match('~^([\w _!@%*=\-#()\[\]&.,]+/)?[\w _!@%*=\-#()\[\]&.,]+$~', $profile_vars['avatar']) != 0 && preg_match('/\.\./', $profile_vars['avatar']) == 0 && file_exists($modSettings['avatar_directory'] . '/' . $profile_vars['avatar']) ? ($profile_vars['avatar'] == 'blank.gif' ? '' : $profile_vars['avatar']) : '';
  2356. // Clear current profile...
  2357. $cur_profile['id_attach'] = 0;
  2358. $cur_profile['attachment_type'] = 0;
  2359. $cur_profile['filename'] = '';
  2360. // Get rid of their old avatar. (if uploaded.)
  2361. removeAttachments(array('id_member' => $memID));
  2362. }
  2363. elseif ($value == 'external' && allowedTo('profile_remote_avatar') && strtolower(substr($_POST['userpicpersonal'], 0, 7)) == 'http://' && empty($modSettings['avatar_download_external']))
  2364. {
  2365. // We need these clean...
  2366. $cur_profile['id_attach'] = 0;
  2367. $cur_profile['attachment_type'] = 0;
  2368. $cur_profile['filename'] = '';
  2369. // Remove any attached avatar...
  2370. removeAttachments(array('id_member' => $memID));
  2371. $profile_vars['avatar'] = str_replace('%20', '', preg_replace('~action(?:=|%3d)(?!dlattach)~i', 'action-', $_POST['userpicpersonal']));
  2372. if ($profile_vars['avatar'] == 'http://' || $profile_vars['avatar'] == 'http:///')
  2373. $profile_vars['avatar'] = '';
  2374. // Trying to make us do something we'll regret?
  2375. elseif (strpos($profile_vars['avatar'], 'http://') !== 0)
  2376. return 'bad_avatar';
  2377. // Should we check dimensions?
  2378. elseif (!empty($modSettings['avatar_max_height_external']) || !empty($modSettings['avatar_max_width_external']))
  2379. {
  2380. // Now let's validate the avatar.
  2381. $sizes = url_image_size($profile_vars['avatar']);
  2382. if (is_array($sizes) && (($sizes[0] > $modSettings['avatar_max_width_external'] && !empty($modSettings['avatar_max_width_external'])) || ($sizes[1] > $modSettings['avatar_max_height_external'] && !empty($modSettings['avatar_max_height_external']))))
  2383. {
  2384. // Houston, we have a problem. The avatar is too large!!
  2385. if ($modSettings['avatar_action_too_large'] == 'option_refuse')
  2386. return 'bad_avatar';
  2387. elseif ($modSettings['avatar_action_too_large'] == 'option_download_and_resize')
  2388. {
  2389. // @todo remove this if appropriate
  2390. require_once($sourcedir . '/Subs-Graphics.php');
  2391. if (downloadAvatar($profile_vars['avatar'], $memID, $modSettings['avatar_max_width_external'], $modSettings['avatar_max_height_external']))
  2392. {
  2393. $profile_vars['avatar'] = '';
  2394. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2395. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2396. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2397. }
  2398. else
  2399. return 'bad_avatar';
  2400. }
  2401. }
  2402. }
  2403. }
  2404. elseif (($value == 'upload' && allowedTo('profile_upload_avatar')) || $downloadedExternalAvatar)
  2405. {
  2406. if ((isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') || $downloadedExternalAvatar)
  2407. {
  2408. // Get the dimensions of the image.
  2409. if (!$downloadedExternalAvatar)
  2410. {
  2411. if (!is_writable($uploadDir))
  2412. fatal_lang_error('attachments_no_write', 'critical');
  2413. if (!move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadDir . '/avatar_tmp_' . $memID))
  2414. fatal_lang_error('attach_timeout', 'critical');
  2415. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2416. }
  2417. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2418. // No size, then it's probably not a valid pic.
  2419. if ($sizes === false)
  2420. return 'bad_avatar';
  2421. // Check whether the image is too large.
  2422. elseif ((!empty($modSettings['avatar_max_width_upload']) && $sizes[0] > $modSettings['avatar_max_width_upload']) || (!empty($modSettings['avatar_max_height_upload']) && $sizes[1] > $modSettings['avatar_max_height_upload']))
  2423. {
  2424. if (!empty($modSettings['avatar_resize_upload']))
  2425. {
  2426. // Attempt to chmod it.
  2427. @chmod($uploadDir . '/avatar_tmp_' . $memID, 0644);
  2428. // @todo remove this require when appropriate
  2429. require_once($sourcedir . '/Subs-Graphics.php');
  2430. if (!downloadAvatar($uploadDir . '/avatar_tmp_' . $memID, $memID, $modSettings['avatar_max_width_upload'], $modSettings['avatar_max_height_upload']))
  2431. return 'bad_avatar';
  2432. // Reset attachment avatar data.
  2433. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2434. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2435. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2436. }
  2437. else
  2438. return 'bad_avatar';
  2439. }
  2440. elseif (is_array($sizes))
  2441. {
  2442. // Now try to find an infection.
  2443. require_once($sourcedir . '/Subs-Graphics.php');
  2444. if (!checkImageContents($_FILES['attachment']['tmp_name'], !empty($modSettings['avatar_paranoid'])))
  2445. {
  2446. // It's bad. Try to re-encode the contents?
  2447. if (empty($modSettings['avatar_reencode']) || (!reencodeImage($_FILES['attachment']['tmp_name'], $sizes[2])))
  2448. return 'bad_avatar';
  2449. // We were successful. However, at what price?
  2450. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2451. // Hard to believe this would happen, but can you bet?
  2452. if ($sizes === false)
  2453. return 'bad_avatar';
  2454. }
  2455. $extensions = array(
  2456. '1' => 'gif',
  2457. '2' => 'jpg',
  2458. '3' => 'png',
  2459. '6' => 'bmp'
  2460. );
  2461. $extension = isset($extensions[$sizes[2]]) ? $extensions[$sizes[2]] : 'bmp';
  2462. $mime_type = 'image/' . ($extension === 'jpg' ? 'jpeg' : ($extension === 'bmp' ? 'x-ms-bmp' : $extension));
  2463. $destName = 'avatar_' . $memID . '_' . time() . '.' . $extension;
  2464. list ($width, $height) = getimagesize($_FILES['attachment']['tmp_name']);
  2465. $file_hash = empty($modSettings['custom_avatar_enabled']) ? getAttachmentFilename($destName, false, null, true) : '';
  2466. // Remove previous attachments this member might have had.
  2467. removeAttachments(array('id_member' => $memID));
  2468. $smcFunc['db_insert']('',
  2469. '{db_prefix}attachments',
  2470. array(
  2471. 'id_member' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'fileext' => 'string', 'size' => 'int',
  2472. 'width' => 'int', 'height' => 'int', 'mime_type' => 'string', 'id_folder' => 'int',
  2473. ),
  2474. array(
  2475. $memID, (empty($modSettings['custom_avatar_enabled']) ? 0 : 1), $destName, $file_hash, $extension, filesize($_FILES['attachment']['tmp_name']),
  2476. (int) $width, (int) $height, $mime_type, $id_folder,
  2477. ),
  2478. array('id_attach')
  2479. );
  2480. $cur_profile['id_attach'] = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach');
  2481. $cur_profile['filename'] = $destName;
  2482. $cur_profile['attachment_type'] = empty($modSettings['custom_avatar_enabled']) ? 0 : 1;
  2483. $destinationPath = $uploadDir . '/' . (empty($file_hash) ? $destName : $cur_profile['id_attach'] . '_' . $file_hash);
  2484. if (!rename($_FILES['attachment']['tmp_name'], $destinationPath))
  2485. {
  2486. // I guess a man can try.
  2487. removeAttachments(array('id_member' => $memID));
  2488. fatal_lang_error('attach_timeout', 'critical');
  2489. }
  2490. // Attempt to chmod it.
  2491. @chmod($uploadDir . '/' . $destinationPath, 0644);
  2492. }
  2493. $profile_vars['avatar'] = '';
  2494. // Delete any temporary file.
  2495. if (file_exists($uploadDir . '/avatar_tmp_' . $memID))
  2496. @unlink($uploadDir . '/avatar_tmp_' . $memID);
  2497. }
  2498. // Selected the upload avatar option and had one already uploaded before or didn't upload one.
  2499. else
  2500. $profile_vars['avatar'] = '';
  2501. }
  2502. else
  2503. $profile_vars['avatar'] = '';
  2504. // Setup the profile variables so it shows things right on display!
  2505. $cur_profile['avatar'] = $profile_vars['avatar'];
  2506. return false;
  2507. }
  2508. /**
  2509. * Validate the signature
  2510. *
  2511. * @param mixed &$value
  2512. * @return bool|string
  2513. */
  2514. function profileValidateSignature(&$value)
  2515. {
  2516. global $sourcedir, $modSettings, $smcFunc, $txt;
  2517. require_once($sourcedir . '/Subs-Post.php');
  2518. // Admins can do whatever they hell they want!
  2519. if (!allowedTo('admin_forum'))
  2520. {
  2521. // Load all the signature limits.
  2522. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2523. $sig_limits = explode(',', $sig_limits);
  2524. $disabledTags = !empty($sig_bbc) ? explode(',', $sig_bbc) : array();
  2525. $unparsed_signature = strtr(un_htmlspecialchars($value), array("\r" => '', '&#039' => '\''));
  2526. // Too long?
  2527. if (!empty($sig_limits[1]) && $smcFunc['strlen']($unparsed_signature) > $sig_limits[1])
  2528. {
  2529. $_POST['signature'] = trim(htmlspecialchars($smcFunc['substr']($unparsed_signature, 0, $sig_limits[1]), ENT_QUOTES));
  2530. $txt['profile_error_signature_max_length'] = sprintf($txt['profile_error_signature_max_length'], $sig_limits[1]);
  2531. return 'signature_max_length';
  2532. }
  2533. // Too many lines?
  2534. if (!empty($sig_limits[2]) && substr_count($unparsed_signature, "\n") >= $sig_limits[2])
  2535. {
  2536. $txt['profile_error_signature_max_lines'] = sprintf($txt['profile_error_signature_max_lines'], $sig_limits[2]);
  2537. return 'signature_max_lines';
  2538. }
  2539. // Too many images?!
  2540. if (!empty($sig_limits[3]) && (substr_count(strtolower($unparsed_signature), '[img') + substr_count(strtolower($unparsed_signature), '<img')) > $sig_limits[3])
  2541. {
  2542. $txt['profile_error_signature_max_image_count'] = sprintf($txt['profile_error_signature_max_image_count'], $sig_limits[3]);
  2543. return 'signature_max_image_count';
  2544. }
  2545. // What about too many smileys!
  2546. $smiley_parsed = $unparsed_signature;
  2547. parsesmileys($smiley_parsed);
  2548. $smiley_count = substr_count(strtolower($smiley_parsed), '<img') - substr_count(strtolower($unparsed_signature), '<img');
  2549. if (!empty($sig_limits[4]) && $sig_limits[4] == -1 && $smiley_count > 0)
  2550. return 'signature_allow_smileys';
  2551. elseif (!empty($sig_limits[4]) && $sig_limits[4] > 0 && $smiley_count > $sig_limits[4])
  2552. {
  2553. $txt['profile_error_signature_max_smileys'] = sprintf($txt['profile_error_signature_max_smileys'], $sig_limits[4]);
  2554. return 'signature_max_smileys';
  2555. }
  2556. // Maybe we are abusing font sizes?
  2557. if (!empty($sig_limits[7]) && preg_match_all('~\[size=([\d\.]+)?(px|pt|em|x-large|larger)~i', $unparsed_signature, $matches) !== false && isset($matches[2]))
  2558. {
  2559. foreach ($matches[1] as $ind => $size)
  2560. {
  2561. $limit_broke = 0;
  2562. // Attempt to allow all sizes of abuse, so to speak.
  2563. if ($matches[2][$ind] == 'px' && $size > $sig_limits[7])
  2564. $limit_broke = $sig_limits[7] . 'px';
  2565. elseif ($matches[2][$ind] == 'pt' && $size > ($sig_limits[7] * 0.75))
  2566. $limit_broke = ((int) $sig_limits[7] * 0.75) . 'pt';
  2567. elseif ($matches[2][$ind] == 'em' && $size > ((float) $sig_limits[7] / 16))
  2568. $limit_broke = ((float) $sig_limits[7] / 16) . 'em';
  2569. elseif ($matches[2][$ind] != 'px' && $matches[2][$ind] != 'pt' && $matches[2][$ind] != 'em' && $sig_limits[7] < 18)
  2570. $limit_broke = 'large';
  2571. if ($limit_broke)
  2572. {
  2573. $txt['profile_error_signature_max_font_size'] = sprintf($txt['profile_error_signature_max_font_size'], $limit_broke);
  2574. return 'signature_max_font_size';
  2575. }
  2576. }
  2577. }
  2578. // The difficult one - image sizes! Don't error on this - just fix it.
  2579. if ((!empty($sig_limits[5]) || !empty($sig_limits[6])))
  2580. {
  2581. // Get all BBC tags...
  2582. preg_match_all('~\[img(\s+width=([\d]+))?(\s+height=([\d]+))?(\s+width=([\d]+))?\s*\](?:<br />)*([^<">]+?)(?:<br />)*\[/img\]~i', $unparsed_signature, $matches);
  2583. // ... and all HTML ones.
  2584. preg_match_all('~<img\s+src=(?:")?((?:http://|ftp://|https://|ftps://).+?)(?:")?(?:\s+alt=(?:")?(.*?)(?:")?)?(?:\s?/)?>~i', $unparsed_signature, $matches2, PREG_PATTERN_ORDER);
  2585. // And stick the HTML in the BBC.
  2586. if (!empty($matches2))
  2587. {
  2588. foreach ($matches2[0] as $ind => $dummy)
  2589. {
  2590. $matches[0][] = $matches2[0][$ind];
  2591. $matches[1][] = '';
  2592. $matches[2][] = '';
  2593. $matches[3][] = '';
  2594. $matches[4][] = '';
  2595. $matches[5][] = '';
  2596. $matches[6][] = '';
  2597. $matches[7][] = $matches2[1][$ind];
  2598. }
  2599. }
  2600. $replaces = array();
  2601. // Try to find all the images!
  2602. if (!empty($matches))
  2603. {
  2604. foreach ($matches[0] as $key => $image)
  2605. {
  2606. $width = -1; $height = -1;
  2607. // Does it have predefined restraints? Width first.
  2608. if ($matches[6][$key])
  2609. $matches[2][$key] = $matches[6][$key];
  2610. if ($matches[2][$key] && $sig_limits[5] && $matches[2][$key] > $sig_limits[5])
  2611. {
  2612. $width = $sig_limits[5];
  2613. $matches[4][$key] = $matches[4][$key] * ($width / $matches[2][$key]);
  2614. }
  2615. elseif ($matches[2][$key])
  2616. $width = $matches[2][$key];
  2617. // ... and height.
  2618. if ($matches[4][$key] && $sig_limits[6] && $matches[4][$key] > $sig_limits[6])
  2619. {
  2620. $height = $sig_limits[6];
  2621. if ($width != -1)
  2622. $width = $width * ($height / $matches[4][$key]);
  2623. }
  2624. elseif ($matches[4][$key])
  2625. $height = $matches[4][$key];
  2626. // If the dimensions are still not fixed - we need to check the actual image.
  2627. if (($width == -1 && $sig_limits[5]) || ($height == -1 && $sig_limits[6]))
  2628. {
  2629. $sizes = url_image_size($matches[7][$key]);
  2630. if (is_array($sizes))
  2631. {
  2632. // Too wide?
  2633. if ($sizes[0] > $sig_limits[5] && $sig_limits[5])
  2634. {
  2635. $width = $sig_limits[5];
  2636. $sizes[1] = $sizes[1] * ($width / $sizes[0]);
  2637. }
  2638. // Too high?
  2639. if ($sizes[1] > $sig_limits[6] && $sig_limits[6])
  2640. {
  2641. $height = $sig_limits[6];
  2642. if ($width == -1)
  2643. $width = $sizes[0];
  2644. $width = $width * ($height / $sizes[1]);
  2645. }
  2646. elseif ($width != -1)
  2647. $height = $sizes[1];
  2648. }
  2649. }
  2650. // Did we come up with some changes? If so remake the string.
  2651. if ($width != -1 || $height != -1)
  2652. $replaces[$image] = '[img' . ($width != -1 ? ' width=' . round($width) : '') . ($height != -1 ? ' height=' . round($height) : '') . ']' . $matches[7][$key] . '[/img]';
  2653. }
  2654. if (!empty($replaces))
  2655. $value = str_replace(array_keys($replaces), array_values($replaces), $value);
  2656. }
  2657. }
  2658. // Any disabled BBC?
  2659. $disabledSigBBC = implode('|', $disabledTags);
  2660. if (!empty($disabledSigBBC))
  2661. {
  2662. if (preg_match('~\[(' . $disabledSigBBC . ')~i', $unparsed_signature, $matches) !== false && isset($matches[1]))
  2663. {
  2664. $disabledTags = array_unique($disabledTags);
  2665. $txt['profile_error_signature_disabled_bbc'] = sprintf($txt['profile_error_signature_disabled_bbc'], implode(', ', $disabledTags));
  2666. return 'signature_disabled_bbc';
  2667. }
  2668. }
  2669. }
  2670. preparsecode($value);
  2671. return true;
  2672. }
  2673. /**
  2674. * Validate an email address.
  2675. *
  2676. * @param string $email
  2677. * @param int $memID = 0
  2678. * @return bool|string
  2679. */
  2680. function profileValidateEmail($email, $memID = 0)
  2681. {
  2682. global $smcFunc, $context;
  2683. $email = strtr($email, array('&#039;' => '\''));
  2684. // Check the name and email for validity.
  2685. if (trim($email) == '')
  2686. return 'no_email';
  2687. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $email) == 0)
  2688. return 'bad_email';
  2689. // Email addresses should be and stay unique.
  2690. $request = $smcFunc['db_query']('', '
  2691. SELECT id_member
  2692. FROM {db_prefix}members
  2693. WHERE ' . ($memID != 0 ? 'id_member != {int:selected_member} AND ' : '') . '
  2694. email_address = {string:email_address}
  2695. LIMIT 1',
  2696. array(
  2697. 'selected_member' => $memID,
  2698. 'email_address' => $email,
  2699. )
  2700. );
  2701. if ($smcFunc['db_num_rows']($request) > 0)
  2702. return 'email_taken';
  2703. $smcFunc['db_free_result']($request);
  2704. return true;
  2705. }
  2706. /**
  2707. * Reload a users settings.
  2708. */
  2709. function profileReloadUser()
  2710. {
  2711. global $sourcedir, $modSettings, $context, $cur_profile, $smcFunc, $profile_vars;
  2712. // Log them back in - using the verify password as they must have matched and this one doesn't get changed by anyone!
  2713. if (isset($_POST['passwrd2']) && $_POST['passwrd2'] != '')
  2714. {
  2715. require_once($sourcedir . '/Subs-Auth.php');
  2716. setLoginCookie(60 * $modSettings['cookieTime'], $context['id_member'], sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  2717. }
  2718. loadUserSettings();
  2719. writeLog();
  2720. }
  2721. /**
  2722. * Send the user a new activation email if they need to reactivate!
  2723. */
  2724. function profileSendActivation()
  2725. {
  2726. global $sourcedir, $profile_vars, $txt, $context, $scripturl, $smcFunc, $cookiename, $cur_profile, $language, $modSettings;
  2727. require_once($sourcedir . '/Subs-Post.php');
  2728. // Shouldn't happen but just in case.
  2729. if (empty($profile_vars['email_address']))
  2730. return;
  2731. $replacements = array(
  2732. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $context['id_member'] . ';code=' . $profile_vars['validation_code'],
  2733. 'ACTIVATIONCODE' => $profile_vars['validation_code'],
  2734. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $context['id_member'],
  2735. );
  2736. // Send off the email.
  2737. $emaildata = loadEmailTemplate('activate_reactivate', $replacements, empty($cur_profile['lngfile']) || empty($modSettings['userLanguage']) ? $language : $cur_profile['lngfile']);
  2738. sendmail($profile_vars['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  2739. // Log the user out.
  2740. $smcFunc['db_query']('', '
  2741. DELETE FROM {db_prefix}log_online
  2742. WHERE id_member = {int:selected_member}',
  2743. array(
  2744. 'selected_member' => $context['id_member'],
  2745. )
  2746. );
  2747. $_SESSION['log_time'] = 0;
  2748. $_SESSION['login_' . $cookiename] = serialize(array(0, '', 0));
  2749. if (isset($_COOKIE[$cookiename]))
  2750. $_COOKIE[$cookiename] = '';
  2751. loadUserSettings();
  2752. $context['user']['is_logged'] = false;
  2753. $context['user']['is_guest'] = true;
  2754. // Send them to the done-with-registration-login screen.
  2755. loadTemplate('Register');
  2756. $context['page_title'] = $txt['profile'];
  2757. $context['sub_template'] = 'after';
  2758. $context['title'] = $txt['activate_changed_email_title'];
  2759. $context['description'] = $txt['activate_changed_email_desc'];
  2760. // We're gone!
  2761. obExit();
  2762. }
  2763. /**
  2764. * Function to allow the user to choose group membership etc...
  2765. *
  2766. * @param int $memID id_member
  2767. */
  2768. function groupMembership($memID)
  2769. {
  2770. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc;
  2771. $curMember = $user_profile[$memID];
  2772. $context['primary_group'] = $curMember['id_group'];
  2773. // Can they manage groups?
  2774. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2775. $context['can_manage_protected'] = allowedTo('admin_forum');
  2776. $context['can_edit_primary'] = $context['can_manage_protected'];
  2777. $context['update_message'] = isset($_GET['msg']) && isset($txt['group_membership_msg_' . $_GET['msg']]) ? $txt['group_membership_msg_' . $_GET['msg']] : '';
  2778. // Get all the groups this user is a member of.
  2779. $groups = explode(',', $curMember['additional_groups']);
  2780. $groups[] = $curMember['id_group'];
  2781. // Ensure the query doesn't croak!
  2782. if (empty($groups))
  2783. $groups = array(0);
  2784. // Just to be sure...
  2785. foreach ($groups as $k => $v)
  2786. $groups[$k] = (int) $v;
  2787. // Get all the membergroups they can join.
  2788. $request = $smcFunc['db_query']('', '
  2789. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  2790. IFNULL(lgr.id_member, 0) AS pending
  2791. FROM {db_prefix}membergroups AS mg
  2792. LEFT JOIN {db_prefix}log_group_requests AS lgr ON (lgr.id_member = {int:selected_member} AND lgr.id_group = mg.id_group)
  2793. WHERE (mg.id_group IN ({array_int:group_list})
  2794. OR mg.group_type > {int:nonjoin_group_id})
  2795. AND mg.min_posts = {int:min_posts}
  2796. AND mg.id_group != {int:moderator_group}
  2797. ORDER BY group_name',
  2798. array(
  2799. 'group_list' => $groups,
  2800. 'selected_member' => $memID,
  2801. 'nonjoin_group_id' => 1,
  2802. 'min_posts' => -1,
  2803. 'moderator_group' => 3,
  2804. )
  2805. );
  2806. // This beast will be our group holder.
  2807. $context['groups'] = array(
  2808. 'member' => array(),
  2809. 'available' => array()
  2810. );
  2811. while ($row = $smcFunc['db_fetch_assoc']($request))
  2812. {
  2813. // Can they edit their primary group?
  2814. if (($row['id_group'] == $context['primary_group'] && $row['group_type'] > 1) || ($row['hidden'] != 2 && $context['primary_group'] == 0 && in_array($row['id_group'], $groups)))
  2815. $context['can_edit_primary'] = true;
  2816. // If they can't manage (protected) groups, and it's not publically joinable or already assigned, they can't see it.
  2817. if (((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0)) && $row['id_group'] != $context['primary_group'])
  2818. continue;
  2819. $context['groups'][in_array($row['id_group'], $groups) ? 'member' : 'available'][$row['id_group']] = array(
  2820. 'id' => $row['id_group'],
  2821. 'name' => $row['group_name'],
  2822. 'desc' => $row['description'],
  2823. 'color' => $row['online_color'],
  2824. 'type' => $row['group_type'],
  2825. 'pending' => $row['pending'],
  2826. 'is_primary' => $row['id_group'] == $context['primary_group'],
  2827. 'can_be_primary' => $row['hidden'] != 2,
  2828. // Anything more than this needs to be done through account settings for security.
  2829. 'can_leave' => $row['id_group'] != 1 && $row['group_type'] > 1 ? true : false,
  2830. );
  2831. }
  2832. $smcFunc['db_free_result']($request);
  2833. // Add registered members on the end.
  2834. $context['groups']['member'][0] = array(
  2835. 'id' => 0,
  2836. 'name' => $txt['regular_members'],
  2837. 'desc' => $txt['regular_members_desc'],
  2838. 'type' => 0,
  2839. 'is_primary' => $context['primary_group'] == 0 ? true : false,
  2840. 'can_be_primary' => true,
  2841. 'can_leave' => 0,
  2842. );
  2843. // No changing primary one unless you have enough groups!
  2844. if (count($context['groups']['member']) < 2)
  2845. $context['can_edit_primary'] = false;
  2846. // In the special case that someone is requesting membership of a group, setup some special context vars.
  2847. if (isset($_REQUEST['request']) && isset($context['groups']['available'][(int) $_REQUEST['request']]) && $context['groups']['available'][(int) $_REQUEST['request']]['type'] == 2)
  2848. $context['group_request'] = $context['groups']['available'][(int) $_REQUEST['request']];
  2849. }
  2850. /**
  2851. * This function actually makes all the group changes
  2852. *
  2853. * @param array $profile_vars
  2854. * @param array $post_errors
  2855. * @param int $memID id_member
  2856. * @return mixed
  2857. */
  2858. function groupMembership2($profile_vars, $post_errors, $memID)
  2859. {
  2860. global $user_info, $sourcedir, $context, $user_profile, $modSettings, $txt, $smcFunc, $scripturl, $language;
  2861. // Let's be extra cautious...
  2862. if (!$context['user']['is_owner'] || empty($modSettings['show_group_membership']))
  2863. isAllowedTo('manage_membergroups');
  2864. if (!isset($_REQUEST['gid']) && !isset($_POST['primary']))
  2865. fatal_lang_error('no_access', false);
  2866. checkSession(isset($_GET['gid']) ? 'get' : 'post');
  2867. $old_profile = &$user_profile[$memID];
  2868. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2869. $context['can_manage_protected'] = allowedTo('admin_forum');
  2870. // By default the new primary is the old one.
  2871. $newPrimary = $old_profile['id_group'];
  2872. $addGroups = array_flip(explode(',', $old_profile['additional_groups']));
  2873. $canChangePrimary = $old_profile['id_group'] == 0 ? 1 : 0;
  2874. $changeType = isset($_POST['primary']) ? 'primary' : (isset($_POST['req']) ? 'request' : 'free');
  2875. // One way or another, we have a target group in mind...
  2876. $group_id = isset($_REQUEST['gid']) ? (int) $_REQUEST['gid'] : (int) $_POST['primary'];
  2877. $foundTarget = $changeType == 'primary' && $group_id == 0 ? true : false;
  2878. // Sanity check!!
  2879. if ($group_id == 1)
  2880. isAllowedTo('admin_forum');
  2881. // Protected groups too!
  2882. else
  2883. {
  2884. $request = $smcFunc['db_query']('', '
  2885. SELECT group_type
  2886. FROM {db_prefix}membergroups
  2887. WHERE id_group = {int:current_group}
  2888. LIMIT {int:limit}',
  2889. array(
  2890. 'current_group' => $group_id,
  2891. 'limit' => 1,
  2892. )
  2893. );
  2894. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  2895. $smcFunc['db_free_result']($request);
  2896. if ($is_protected == 1)
  2897. isAllowedTo('admin_forum');
  2898. }
  2899. // What ever we are doing, we need to determine if changing primary is possible!
  2900. $request = $smcFunc['db_query']('', '
  2901. SELECT id_group, group_type, hidden, group_name
  2902. FROM {db_prefix}membergroups
  2903. WHERE id_group IN ({int:group_list}, {int:current_group})',
  2904. array(
  2905. 'group_list' => $group_id,
  2906. 'current_group' => $old_profile['id_group'],
  2907. )
  2908. );
  2909. while ($row = $smcFunc['db_fetch_assoc']($request))
  2910. {
  2911. // Is this the new group?
  2912. if ($row['id_group'] == $group_id)
  2913. {
  2914. $foundTarget = true;
  2915. $group_name = $row['group_name'];
  2916. // Does the group type match what we're doing - are we trying to request a non-requestable group?
  2917. if ($changeType == 'request' && $row['group_type'] != 2)
  2918. fatal_lang_error('no_access', false);
  2919. // What about leaving a requestable group we are not a member of?
  2920. elseif ($changeType == 'free' && $row['group_type'] == 2 && $old_profile['id_group'] != $row['id_group'] && !isset($addGroups[$row['id_group']]))
  2921. fatal_lang_error('no_access', false);
  2922. elseif ($changeType == 'free' && $row['group_type'] != 3 && $row['group_type'] != 2)
  2923. fatal_lang_error('no_access', false);
  2924. // We can't change the primary group if this is hidden!
  2925. if ($row['hidden'] == 2)
  2926. $canChangePrimary = false;
  2927. }
  2928. // If this is their old primary, can we change it?
  2929. if ($row['id_group'] == $old_profile['id_group'] && ($row['group_type'] > 1 || $context['can_manage_membergroups']) && $canChangePrimary !== false)
  2930. $canChangePrimary = 1;
  2931. // If we are not doing a force primary move, don't do it automatically if current primary is not 0.
  2932. if ($changeType != 'primary' && $old_profile['id_group'] != 0)
  2933. $canChangePrimary = false;
  2934. // If this is the one we are acting on, can we even act?
  2935. if ((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0))
  2936. $canChangePrimary = false;
  2937. }
  2938. $smcFunc['db_free_result']($request);
  2939. // Didn't find the target?
  2940. if (!$foundTarget)
  2941. fatal_lang_error('no_access', false);
  2942. // Final security check, don't allow users to promote themselves to admin.
  2943. if ($context['can_manage_membergroups'] && !allowedTo('admin_forum'))
  2944. {
  2945. $request = $smcFunc['db_query']('', '
  2946. SELECT COUNT(permission)
  2947. FROM {db_prefix}permissions
  2948. WHERE id_group = {int:selected_group}
  2949. AND permission = {string:admin_forum}
  2950. AND add_deny = {int:not_denied}',
  2951. array(
  2952. 'selected_group' => $group_id,
  2953. 'not_denied' => 1,
  2954. 'admin_forum' => 'admin_forum',
  2955. )
  2956. );
  2957. list ($disallow) = $smcFunc['db_fetch_row']($request);
  2958. $smcFunc['db_free_result']($request);
  2959. if ($disallow)
  2960. isAllowedTo('admin_forum');
  2961. }
  2962. // If we're requesting, add the note then return.
  2963. if ($changeType == 'request')
  2964. {
  2965. $request = $smcFunc['db_query']('', '
  2966. SELECT id_member
  2967. FROM {db_prefix}log_group_requests
  2968. WHERE id_member = {int:selected_member}
  2969. AND id_group = {int:selected_group}',
  2970. array(
  2971. 'selected_member' => $memID,
  2972. 'selected_group' => $group_id,
  2973. )
  2974. );
  2975. if ($smcFunc['db_num_rows']($request) != 0)
  2976. fatal_lang_error('profile_error_already_requested_group');
  2977. $smcFunc['db_free_result']($request);
  2978. // Log the request.
  2979. $smcFunc['db_insert']('',
  2980. '{db_prefix}log_group_requests',
  2981. array(
  2982. 'id_member' => 'int', 'id_group' => 'int', 'time_applied' => 'int', 'reason' => 'string-65534',
  2983. ),
  2984. array(
  2985. $memID, $group_id, time(), $_POST['reason'],
  2986. ),
  2987. array('id_request')
  2988. );
  2989. // Send an email to all group moderators etc.
  2990. require_once($sourcedir . '/Subs-Post.php');
  2991. // Do we have any group moderators?
  2992. $request = $smcFunc['db_query']('', '
  2993. SELECT id_member
  2994. FROM {db_prefix}group_moderators
  2995. WHERE id_group = {int:selected_group}',
  2996. array(
  2997. 'selected_group' => $group_id,
  2998. )
  2999. );
  3000. $moderators = array();
  3001. while ($row = $smcFunc['db_fetch_assoc']($request))
  3002. $moderators[] = $row['id_member'];
  3003. $smcFunc['db_free_result']($request);
  3004. // Otherwise this is the backup!
  3005. if (empty($moderators))
  3006. {
  3007. require_once($sourcedir . '/Subs-Members.php');
  3008. $moderators = membersAllowedTo('manage_membergroups');
  3009. }
  3010. if (!empty($moderators))
  3011. {
  3012. $request = $smcFunc['db_query']('', '
  3013. SELECT id_member, email_address, lngfile, member_name, mod_prefs
  3014. FROM {db_prefix}members
  3015. WHERE id_member IN ({array_int:moderator_list})
  3016. AND notify_types != {int:no_notifications}
  3017. ORDER BY lngfile',
  3018. array(
  3019. 'moderator_list' => $moderators,
  3020. 'no_notifications' => 4,
  3021. )
  3022. );
  3023. while ($row = $smcFunc['db_fetch_assoc']($request))
  3024. {
  3025. // Check whether they are interested.
  3026. if (!empty($row['mod_prefs']))
  3027. {
  3028. list(,, $pref_binary) = explode('|', $row['mod_prefs']);
  3029. if (!($pref_binary & 4))
  3030. continue;
  3031. }
  3032. $replacements = array(
  3033. 'RECPNAME' => $row['member_name'],
  3034. 'APPYNAME' => $old_profile['member_name'],
  3035. 'GROUPNAME' => $group_name,
  3036. 'REASON' => $_POST['reason'],
  3037. 'MODLINK' => $scripturl . '?action=moderate;area=groups;sa=requests',
  3038. );
  3039. $emaildata = loadEmailTemplate('request_membership', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  3040. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  3041. }
  3042. $smcFunc['db_free_result']($request);
  3043. }
  3044. return $changeType;
  3045. }
  3046. // Otherwise we are leaving/joining a group.
  3047. elseif ($changeType == 'free')
  3048. {
  3049. // Are we leaving?
  3050. if ($old_profile['id_group'] == $group_id || isset($addGroups[$group_id]))
  3051. {
  3052. if ($old_profile['id_group'] == $group_id)
  3053. $newPrimary = 0;
  3054. else
  3055. unset($addGroups[$group_id]);
  3056. }
  3057. // ... if not, must be joining.
  3058. else
  3059. {
  3060. // Can we change the primary, and do we want to?
  3061. if ($canChangePrimary)
  3062. {
  3063. if ($old_profile['id_group'] != 0)
  3064. $addGroups[$old_profile['id_group']] = -1;
  3065. $newPrimary = $group_id;
  3066. }
  3067. // Otherwise it's an additional group...
  3068. else
  3069. $addGroups[$group_id] = -1;
  3070. }
  3071. }
  3072. // Finally, we must be setting the primary.
  3073. elseif ($canChangePrimary)
  3074. {
  3075. if ($old_profile['id_group'] != 0)
  3076. $addGroups[$old_profile['id_group']] = -1;
  3077. if (isset($addGroups[$group_id]))
  3078. unset($addGroups[$group_id]);
  3079. $newPrimary = $group_id;
  3080. }
  3081. // Finally, we can make the changes!
  3082. foreach ($addGroups as $id => $dummy)
  3083. if (empty($id))
  3084. unset($addGroups[$id]);
  3085. $addGroups = implode(',', array_flip($addGroups));
  3086. // Ensure that we don't cache permissions if the group is changing.
  3087. if ($context['user']['is_owner'])
  3088. $_SESSION['mc']['time'] = 0;
  3089. else
  3090. updateSettings(array('settings_updated' => time()));
  3091. updateMemberData($memID, array('id_group' => $newPrimary, 'additional_groups' => $addGroups));
  3092. return $changeType;
  3093. }
  3094. ?>