Profile-Modify.php 117 KB

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