Profile-Modify.php 117 KB

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