Profile-Modify.php 127 KB

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